package org.apache.cassandra.locator;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.cassandra.service.StorageService;
import org.apache.cassandra.utils.FBUtilities;
import org.apache.cassandra.utils.ResourceWatcher;
import org.apache.cassandra.utils.WrappedRunnable;
import org.apache.commons.lang3.StringUtils;
public class PropertyFileSnitch extends AbstractNetworkTopologySnitch
{
private static final Logger logger = LoggerFactory.getLogger(PropertyFileSnitch.class);
public static final String SNITCH_PROPERTIES_FILENAME = "cassandra-topology.properties";
private static final int DEFAULT_REFRESH_PERIOD_IN_SECONDS = 5;
private static volatile Map<InetAddress, String[]> endpointMap;
private static volatile String[] defaultDCRack;
private volatile boolean gossipStarted;
public PropertyFileSnitch() throws ConfigurationException
{
this(DEFAULT_REFRESH_PERIOD_IN_SECONDS);
}
public PropertyFileSnitch(int refreshPeriodInSeconds) throws ConfigurationException
{
reloadConfiguration(false);
try
{
FBUtilities.resourceToFile(SNITCH_PROPERTIES_FILENAME);
Runnable runnable = new WrappedRunnable()
{
protected void runMayThrow() throws ConfigurationException
{
reloadConfiguration(true);
}
};
ResourceWatcher.watch(SNITCH_PROPERTIES_FILENAME, runnable, refreshPeriodInSeconds * 1000);
}
catch (ConfigurationException ex)
{
logger.error("{} found, but does not look like a plain file. Will not watch it for changes", SNITCH_PROPERTIES_FILENAME);
}
}
public static String[] getEndpointInfo(InetAddress endpoint)
{
String[] rawEndpointInfo = getRawEndpointInfo(endpoint);
if (rawEndpointInfo == null)
throw new RuntimeException("Unknown host " + endpoint + " with no default configured");
return rawEndpointInfo;
}
private static String[] getRawEndpointInfo(InetAddress endpoint)
{
String[] value = endpointMap.get(endpoint);
if (value == null)
{
logger.trace("Could not find end point information for {}, will use default", endpoint);
return defaultDCRack;
}
return value;
}
public String getDatacenter(InetAddress endpoint)
{
String[] info = getEndpointInfo(endpoint);
assert info != null : "No location defined for endpoint " + endpoint;
return info[0];
}
public String getRack(InetAddress endpoint)
{
String[] info = getEndpointInfo(endpoint);
assert info != null : "No location defined for endpoint " + endpoint;
return info[1];
}
public void reloadConfiguration(boolean isUpdate) throws ConfigurationException
{
HashMap<InetAddress, String[]> reloadedMap = new HashMap<>();
String[] reloadedDefaultDCRack = null;
Properties properties = new Properties();
try (InputStream stream = getClass().getClassLoader().getResourceAsStream(SNITCH_PROPERTIES_FILENAME))
{
properties.load(stream);
}
catch (Exception e)
{
throw new ConfigurationException("Unable to read " + SNITCH_PROPERTIES_FILENAME, e);
}
for (Map.Entry<Object, Object> entry : properties.entrySet())
{
String key = (String) entry.getKey();
String value = (String) entry.getValue();
if ("default".equals(key))
{
String[] newDefault = value.split(":");
if (newDefault.length < 2)
reloadedDefaultDCRack = new String[] { "default", "default" };
else
reloadedDefaultDCRack = new String[] { newDefault[0].trim(), newDefault[1].trim() };
}
else
{
InetAddress host;
String hostString = StringUtils.remove(key, '/');
try
{
host = InetAddress.getByName(hostString);
}
catch (UnknownHostException e)
{
throw new ConfigurationException("Unknown host " + hostString, e);
}
String[] token = value.split(":");
if (token.length < 2)
token = new String[] { "default", "default" };
else
token = new String[] { token[0].trim(), token[1].trim() };
reloadedMap.put(host, token);
}
}
InetAddress broadcastAddress = FBUtilities.getBroadcastAddress();
String[] localInfo = reloadedMap.get(broadcastAddress);
if (reloadedDefaultDCRack == null && localInfo == null)
throw new ConfigurationException(String.format("Snitch definitions at %s do not define a location for " +
"this node's broadcast address %s, nor does it provides a default",
SNITCH_PROPERTIES_FILENAME, broadcastAddress));
InetAddress localAddress = FBUtilities.getLocalAddress();
if (!localAddress.equals(broadcastAddress) && !reloadedMap.containsKey(localAddress))
reloadedMap.put(localAddress, localInfo);
if (isUpdate && !livenessCheck(reloadedMap, reloadedDefaultDCRack))
return;
if (logger.isTraceEnabled())
{
StringBuilder sb = new StringBuilder();
for (Map.Entry<InetAddress, String[]> entry : reloadedMap.entrySet())
sb.append(entry.getKey()).append(':').append(Arrays.toString(entry.getValue())).append(", ");
logger.trace("Loaded network topology from property file: {}", StringUtils.removeEnd(sb.toString(), ", "));
}
defaultDCRack = reloadedDefaultDCRack;
endpointMap = reloadedMap;
if (StorageService.instance != null)
{
if (isUpdate)
StorageService.instance.updateTopology();
else
StorageService.instance.getTokenMetadata().invalidateCachedRings();
}
if (gossipStarted)
StorageService.instance.gossipSnitchInfo();
}
private static boolean livenessCheck(HashMap<InetAddress, String[]> reloadedMap, String[] reloadedDefaultDCRack)
{
Set<InetAddress> hosts = Arrays.equals(defaultDCRack, reloadedDefaultDCRack)
? Sets.intersection(StorageService.instance.getLiveRingMembers(),
Sets.union(endpointMap.keySet(), reloadedMap.keySet()))
: StorageService.instance.getLiveRingMembers();
for (InetAddress host : hosts)
{
String[] origValue = endpointMap.containsKey(host) ? endpointMap.get(host) : defaultDCRack;
String[] updateValue = reloadedMap.containsKey(host) ? reloadedMap.get(host) : reloadedDefaultDCRack;
if (!Arrays.equals(origValue, updateValue))
{
logger.error("Cannot update data center or rack from {} to {} for live host {}, property file NOT RELOADED",
origValue,
updateValue,
host);
return false;
}
}
return true;
}
@Override
public void gossiperStarting()
{
gossipStarted = true;
}
}