Make Ec2Region's datacenter name configurable

patch by Jason Brown; reviewed by Vijay for CASSANDRA-5155
This commit is contained in:
Vijay Parthasarathy 2013-01-14 20:57:25 -08:00
parent fc0f2472d8
commit e90dcd51d0
5 changed files with 94 additions and 47 deletions

View File

@ -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=

View File

@ -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 + ".");
}

View File

@ -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
*

View File

@ -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);
}
}

View File

@ -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=