Author: jbellis
Date: Tue Sep 14 21:49:00 2010
New Revision: 997109
URL: http://svn.apache.org/viewvc?rev=997109&view=rev
Log:
switch from Properties to HashMap in PropertyFileEndpointSnitch to avoid
synchronization bottleneck. patch by goffinet and jbellis; reviewed by
brandonwilliams for CASSANDRA-1481
Modified:
cassandra/branches/cassandra-0.6/CHANGES.txt
cassandra/branches/cassandra-0.6/contrib/property_snitch/src/java/org/apache/cassandra/locator/PropertyFileEndPointSnitch.java
Modified: cassandra/branches/cassandra-0.6/CHANGES.txt
URL:
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.6/CHANGES.txt?rev=997109&r1=997108&r2=997109&view=diff
==============================================================================
--- cassandra/branches/cassandra-0.6/CHANGES.txt (original)
+++ cassandra/branches/cassandra-0.6/CHANGES.txt Tue Sep 14 21:49:00 2010
@@ -16,6 +16,8 @@
* avoid ConcurrentModificationException in Gossiper after removing
a dead StorageProxy client, decommissioned node, or partially
bootstrapped one (CASSANDRA-1494)
+ * switch from Properties to HashMap in PropertyFileEndpointSnitch to
+ avoid synchronization bottleneck (CASSANDRA-1481)
* nodes that coordinated a loadbalance in the past could not be seen by
newly added nodes (CASSANDRA-1467)
Modified:
cassandra/branches/cassandra-0.6/contrib/property_snitch/src/java/org/apache/cassandra/locator/PropertyFileEndPointSnitch.java
URL:
http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.6/contrib/property_snitch/src/java/org/apache/cassandra/locator/PropertyFileEndPointSnitch.java?rev=997109&r1=997108&r2=997109&view=diff
==============================================================================
---
cassandra/branches/cassandra-0.6/contrib/property_snitch/src/java/org/apache/cassandra/locator/PropertyFileEndPointSnitch.java
(original)
+++
cassandra/branches/cassandra-0.6/contrib/property_snitch/src/java/org/apache/cassandra/locator/PropertyFileEndPointSnitch.java
Tue Sep 14 21:49:00 2010
@@ -22,87 +22,72 @@ import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.management.ManagementFactory;
+import java.net.InetAddress;
import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
-
import javax.management.MBeanServer;
import javax.management.ObjectName;
-import org.apache.cassandra.locator.EndPointSnitch;
-import java.net.InetAddress;
import org.apache.log4j.Logger;
/**
* PropertyFileEndPointSnitch
- *
+ * <p/>
* PropertyFileEndPointSnitch is used by Digg to determine if two IP's are in
the same
* datacenter or on the same rack.
- *
- * @author Sammy Yu <[email protected]>
- *
*/
public class PropertyFileEndPointSnitch extends EndPointSnitch implements
PropertyFileEndPointSnitchMBean {
/**
- * A list of properties with keys being host:port and values being
datacenter:rack
- */
- private Properties hostProperties = new Properties();
-
- /**
* The default rack property file to be read.
*/
- private static String DEFAULT_RACK_PROPERTY_FILE =
"/etc/cassandra/rack.properties";
+ private static String DEFAULT_RACK_PROPERTY_FILE =
"/etc/cassandra/rack.properties";
/**
* Whether to use the parent for detection of same node
*/
private boolean runInBaseMode = false;
-
+
/**
* Reference to the logger.
*/
- private static Logger logger_ =
Logger.getLogger(PropertyFileEndPointSnitch.class);
+ private static Logger logger_ =
Logger.getLogger(PropertyFileEndPointSnitch.class);
+ private static Map<InetAddress, String[]> endpointMap = new
HashMap<InetAddress, String[]>();
+ private static String[] defaultDCRack;
public PropertyFileEndPointSnitch() throws IOException {
reloadConfiguration();
- try
- {
+ try {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
mbs.registerMBean(this, new ObjectName(MBEAN_OBJECT_NAME));
}
- catch (Exception e)
- {
+ catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 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 String[] getEndPointInfo(InetAddress endPoint) {
- String key = endPoint.toString();
- String value = hostProperties.getProperty(key);
+ String[] value = endpointMap.get(endPoint);
if (value == null)
{
- logger_.error("Could not find end point information for " + key +
", will use default.");
- value = hostProperties.getProperty("default");
- }
- StringTokenizer st = new StringTokenizer(value, ":");
- if (st.countTokens() < 2)
- {
- logger_.error("Value for " + key + " is invalid: " + value);
- return new String [] {"default", "default"};
+ if (logger_.isDebugEnabled())
+ logger_.debug("Could not find end point information for " +
endPoint + ", will use default.");
+ return defaultDCRack;
}
- return new String[] {st.nextToken(), st.nextToken()};
+ return value;
}
/**
* Return the data center for which an endpoint resides in
- *
+ *
* @param endPoint the endPoint to process
* @return string of data center
*/
@@ -115,9 +100,8 @@ public class PropertyFileEndPointSnitch
}
/**
* Return the rack for which an endpoint resides in
- *
+ *
* @param endPoint the endPoint to process
- *
* @return string of rack
*/
public String getRackForEndPoint(InetAddress endPoint) {
@@ -127,8 +111,7 @@ public class PropertyFileEndPointSnitch
@Override
public boolean isInSameDataCenter(InetAddress host, InetAddress host2)
throws UnknownHostException {
- if (runInBaseMode)
- {
+ if (runInBaseMode) {
return super.isInSameDataCenter(host, host2);
}
return
getDataCenterForEndPoint(host).equals(getDataCenterForEndPoint(host2));
@@ -137,36 +120,48 @@ public class PropertyFileEndPointSnitch
@Override
public boolean isOnSameRack(InetAddress host, InetAddress host2)
throws UnknownHostException {
- if (runInBaseMode)
- {
+ if (runInBaseMode) {
return super.isOnSameRack(host, host2);
}
- if (!isInSameDataCenter(host, host2))
- {
+ if (!isInSameDataCenter(host, host2)) {
return false;
}
- return getRackForEndPoint(host).equals(getRackForEndPoint(host2));
+ return getRackForEndPoint(host).equals(getRackForEndPoint(host2));
}
- @Override
public String displayConfiguration() {
StringBuffer configurationString = new StringBuffer("Current rack
configuration\n=================\n");
- for (Object key: hostProperties.keySet()) {
- String endpoint = (String) key;
- String value = hostProperties.getProperty(endpoint);
- configurationString.append(endpoint + "=" + value + "\n");
+ for (Map.Entry<InetAddress, String[]> entry : endpointMap.entrySet())
+ {
+ String[] dcRack = entry.getValue();
+ configurationString.append(String.format("%s=%s:%s\n",
entry.getKey(), dcRack[0], dcRack[1]));
}
return configurationString.toString();
}
-
- @Override
- public void reloadConfiguration() throws IOException {
+
+ public void reloadConfiguration() throws IOException {
String rackPropertyFilename = System.getProperty("rackFile",
DEFAULT_RACK_PROPERTY_FILE);
- try
- {
- Properties localHostProperties = new Properties();
- localHostProperties.load(new FileReader(rackPropertyFilename));
- hostProperties = localHostProperties;
+ endpointMap.clear();
+ try {
+ Properties properties = new Properties();
+ properties.load(new FileReader(rackPropertyFilename));
+ for (Map.Entry<Object, Object> entry : properties.entrySet()) {
+ String key = (String) entry.getKey();
+ String value = (String) entry.getValue();
+
+ if (key.equals("default")) {
+ defaultDCRack = value.split(":");
+ if (defaultDCRack.length < 2)
+ defaultDCRack = new String[]{"default", "default"};
+ }
+ else {
+ InetAddress host = InetAddress.getByName(key.replace("/",
""));
+ String[] token = value.split(":");
+ if (token.length < 2)
+ token = new String[]{"default", "default"};
+ endpointMap.put(host, token);
+ }
+ }
runInBaseMode = false;
}
catch (FileNotFoundException fnfe) {