Make Ec2Region's datacenter name configurable patch by Jason Brown; reviewed by Vijay for CASSANDRA-5155
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/e90dcd51 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/e90dcd51 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/e90dcd51 Branch: refs/heads/trunk Commit: e90dcd51d010fbe4fa2e2de6b91a7b93cb3ac9ec Parents: fc0f247 Author: Vijay Parthasarathy <vijay2...@gmail.com> Authored: Mon Jan 14 20:57:25 2013 -0800 Committer: Vijay Parthasarathy <vijay2...@gmail.com> Committed: Mon Jan 14 20:57:25 2013 -0800 ---------------------------------------------------------------------- conf/cassandra-rackdc.properties | 6 ++- .../org/apache/cassandra/locator/Ec2Snitch.java | 2 + .../locator/GossipingPropertyFileSnitch.java | 55 +++------------ .../apache/cassandra/locator/SnitchProperties.java | 54 ++++++++++++++ test/conf/cassandra-rackdc.properties | 24 ++++++ 5 files changed, 94 insertions(+), 47 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/e90dcd51/conf/cassandra-rackdc.properties ---------------------------------------------------------------------- diff --git a/conf/cassandra-rackdc.properties b/conf/cassandra-rackdc.properties index b792885..be2e7d2 100644 --- a/conf/cassandra-rackdc.properties +++ b/conf/cassandra-rackdc.properties @@ -14,7 +14,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -# This file is specifically for the GossipingPropertyFileSnitch and will +# These properties are used with GossipingPropertyFileSnitch and will # indicate the rack and dc for this node dc=DC1 rack=RAC1 + +# Add a suffix to a datacenter name. Used by the Ec2Snitch and Ec2MultiRegionSnitch +# to append a string to the EC2 region name. +#dc_suffix= http://git-wip-us.apache.org/repos/asf/cassandra/blob/e90dcd51/src/java/org/apache/cassandra/locator/Ec2Snitch.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/locator/Ec2Snitch.java b/src/java/org/apache/cassandra/locator/Ec2Snitch.java index e5b5126..a28e2a6 100644 --- a/src/java/org/apache/cassandra/locator/Ec2Snitch.java +++ b/src/java/org/apache/cassandra/locator/Ec2Snitch.java @@ -59,6 +59,8 @@ public class Ec2Snitch extends AbstractNetworkTopologySnitch if (ec2region.endsWith("1")) ec2region = az.substring(0, az.length() - 3); + String datacenterSuffix = SnitchProperties.get("dc_suffix", ""); + ec2region = ec2region.concat(datacenterSuffix); logger.info("EC2Snitch using region: " + ec2region + ", zone: " + ec2zone + "."); } http://git-wip-us.apache.org/repos/asf/cassandra/blob/e90dcd51/src/java/org/apache/cassandra/locator/GossipingPropertyFileSnitch.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/locator/GossipingPropertyFileSnitch.java b/src/java/org/apache/cassandra/locator/GossipingPropertyFileSnitch.java index 405d8e4..aa4c4e6 100644 --- a/src/java/org/apache/cassandra/locator/GossipingPropertyFileSnitch.java +++ b/src/java/org/apache/cassandra/locator/GossipingPropertyFileSnitch.java @@ -18,39 +18,31 @@ package org.apache.cassandra.locator; +import java.net.InetAddress; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.apache.cassandra.exceptions.ConfigurationException; import org.apache.cassandra.gms.ApplicationState; import org.apache.cassandra.gms.EndpointState; import org.apache.cassandra.gms.Gossiper; -import org.apache.cassandra.io.util.FileUtils; import org.apache.cassandra.utils.FBUtilities; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.InputStream; -import java.net.InetAddress; -import java.util.Map; -import java.util.Properties; public class GossipingPropertyFileSnitch extends AbstractNetworkTopologySnitch { private static final Logger logger = LoggerFactory.getLogger(GossipingPropertyFileSnitch.class); - public static final String RACKDC_PROPERTY_FILENAME = "cassandra-rackdc.properties"; private PropertyFileSnitch psnitch; private String myDC; private String myRack; public GossipingPropertyFileSnitch() throws ConfigurationException { - try - { - loadConfiguration(); - } - catch (ConfigurationException e) - { - throw new RuntimeException("Unable to load " + RACKDC_PROPERTY_FILENAME + " : ", e); - } + myDC = SnitchProperties.get("dc", null); + myRack = SnitchProperties.get("rack", null); + if (myDC == null || myRack == null) + throw new ConfigurationException("DC or rack not found in snitch properties"); try { psnitch = new PropertyFileSnitch(); @@ -62,35 +54,6 @@ public class GossipingPropertyFileSnitch extends AbstractNetworkTopologySnitch } } - private void loadConfiguration() throws ConfigurationException - { - InputStream stream = GossipingPropertyFileSnitch.class.getClassLoader().getResourceAsStream(RACKDC_PROPERTY_FILENAME); - Properties properties = new Properties(); - try - { - properties.load(stream); - } - catch (Exception e) - { - throw new ConfigurationException("Unable to read " + RACKDC_PROPERTY_FILENAME, e); - } - finally - { - FileUtils.closeQuietly(stream); - } - for (Map.Entry<Object, Object> entry : properties.entrySet()) - { - String key = (String) entry.getKey(); - String value = (String) entry.getValue(); - if (key.equals("dc")) - myDC = value; - else if (key.equals("rack")) - myRack = value; - } - if (myDC == null || myRack == null) - throw new ConfigurationException("DC or rack not found in " + RACKDC_PROPERTY_FILENAME); - } - /** * Return the data center for which an endpoint resides in * http://git-wip-us.apache.org/repos/asf/cassandra/blob/e90dcd51/src/java/org/apache/cassandra/locator/SnitchProperties.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/locator/SnitchProperties.java b/src/java/org/apache/cassandra/locator/SnitchProperties.java new file mode 100644 index 0000000..16989ff --- /dev/null +++ b/src/java/org/apache/cassandra/locator/SnitchProperties.java @@ -0,0 +1,54 @@ +/* + * 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.util.Properties; + +import org.apache.cassandra.io.util.FileUtils; + +public class SnitchProperties +{ + public static final String RACKDC_PROPERTY_FILENAME = "cassandra-rackdc.properties"; + private static Properties properties = new Properties(); + + static + { + InputStream stream = SnitchProperties.class.getClassLoader().getResourceAsStream(RACKDC_PROPERTY_FILENAME); + try + { + properties.load(stream); + } + catch (Exception e) + { + throw new RuntimeException("Unable to read " + RACKDC_PROPERTY_FILENAME, e); + } + finally + { + FileUtils.closeQuietly(stream); + } + } + + /** + * Get a snitch property value or return null if not defined. + */ + public static String get(String propertyName, String defaultValue) + { + return properties.getProperty(propertyName, defaultValue); + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/e90dcd51/test/conf/cassandra-rackdc.properties ---------------------------------------------------------------------- diff --git a/test/conf/cassandra-rackdc.properties b/test/conf/cassandra-rackdc.properties new file mode 100644 index 0000000..be2e7d2 --- /dev/null +++ b/test/conf/cassandra-rackdc.properties @@ -0,0 +1,24 @@ +# 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. + +# These properties are used with GossipingPropertyFileSnitch and will +# indicate the rack and dc for this node +dc=DC1 +rack=RAC1 + +# Add a suffix to a datacenter name. Used by the Ec2Snitch and Ec2MultiRegionSnitch +# to append a string to the EC2 region name. +#dc_suffix=