From 993945833ce3d7edf2fdc694d3907cace8f5f149 Mon Sep 17 00:00:00 2001 From: Vijay Parthasarathy Date: Tue, 15 Jan 2013 10:15:27 -0800 Subject: [PATCH] Make-Ec2Region-s-datacenter-name-configurable patch by Jason Brown; reviewed by Vijay for CASSANDRA-5155 --- conf/cassandra-rackdc.properties | 6 ++- .../apache/cassandra/locator/Ec2Snitch.java | 3 ++ .../locator/GossipingPropertyFileSnitch.java | 42 ++------------- .../cassandra/locator/SnitchProperties.java | 54 +++++++++++++++++++ test/conf/cassandra-rackdc.properties | 24 +++++++++ 5 files changed, 90 insertions(+), 39 deletions(-) create mode 100644 src/java/org/apache/cassandra/locator/SnitchProperties.java create mode 100644 test/conf/cassandra-rackdc.properties diff --git a/conf/cassandra-rackdc.properties b/conf/cassandra-rackdc.properties index b792885ba5..be2e7d2190 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= diff --git a/src/java/org/apache/cassandra/locator/Ec2Snitch.java b/src/java/org/apache/cassandra/locator/Ec2Snitch.java index 1dced958b2..b0cefeedd1 100644 --- a/src/java/org/apache/cassandra/locator/Ec2Snitch.java +++ b/src/java/org/apache/cassandra/locator/Ec2Snitch.java @@ -60,6 +60,9 @@ public class Ec2Snitch extends AbstractNetworkTopologySnitch ec2region = az.substring(0, az.length() - 1); 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 + "."); } diff --git a/src/java/org/apache/cassandra/locator/GossipingPropertyFileSnitch.java b/src/java/org/apache/cassandra/locator/GossipingPropertyFileSnitch.java index 9a22544eb8..72c9bb8a59 100644 --- a/src/java/org/apache/cassandra/locator/GossipingPropertyFileSnitch.java +++ b/src/java/org/apache/cassandra/locator/GossipingPropertyFileSnitch.java @@ -36,21 +36,16 @@ 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 +57,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 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 * 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 0000000000..16989ff6ff --- /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); + } +} diff --git a/test/conf/cassandra-rackdc.properties b/test/conf/cassandra-rackdc.properties new file mode 100644 index 0000000000..be2e7d2190 --- /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=