org.apache.cassandra.locator.PropertyFileSnitch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cassandra-all Show documentation
Show all versions of cassandra-all Show documentation
A fork of the Apache Cassandra Project ready to embed Elasticsearch.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;
/**
*
* Used to determine if two IP's are in the same datacenter or on the same rack.
*
* Based on a properties file in the following format:
*
* 10.0.0.13=DC1:RAC2
* 10.21.119.14=DC3:RAC2
* 10.20.114.15=DC2:RAC2
* default=DC1:r1
*/
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 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);
}
}
/**
* Get the raw information about an end point
*
* @param endpoint endpoint to process
* @return a array of string with the first index being the data center and the second being the rack
*/
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;
}
/**
* Return the data center for which an endpoint resides in
*
* @param endpoint the endpoint to process
* @return string of data center
*/
public String getDatacenter(InetAddress endpoint)
{
String[] info = getEndpointInfo(endpoint);
assert info != null : "No location defined for endpoint " + endpoint;
return info[0];
}
/**
* Return the rack for which an endpoint resides in
*
* @param endpoint the endpoint to process
* @return string of rack
*/
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 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
© 2015 - 2024 Weber Informatics LLC | Privacy Policy