Repository: cassandra
Updated Branches:
  refs/heads/cassandra-2.0 4ca1a4d68 -> 57fc8fd6d
  refs/heads/cassandra-2.1 44fa2cdb4 -> cc77ac8b6


Add Google Compute Engine snitch

Patch by Brian Lynch, reviewed by brandonwilliams for CASSANDRA-7132


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/ad404512
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/ad404512
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/ad404512

Branch: refs/heads/cassandra-2.0
Commit: ad404512dd30b4c0030e94c78fee4179ea683708
Parents: b573d0f
Author: Brandon Williams <brandonwilli...@apache.org>
Authored: Thu May 1 18:45:15 2014 -0500
Committer: Brandon Williams <brandonwilli...@apache.org>
Committed: Thu May 1 18:45:15 2014 -0500

----------------------------------------------------------------------
 CHANGES.txt                                     |   1 +
 .../cassandra/locator/GoogleCloudSnitch.java    | 111 +++++++++++++++++++
 .../locator/GoogleCloudSnitchTest.java          |  84 ++++++++++++++
 3 files changed, 196 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/ad404512/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index fa9a156..d951568 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 1.2.17
+ * Add Google Compute Engine snitch (CASSANDRA-7132)
  * Fix nodetool display with vnodes (CASSANDRA-7082)
  * Fix schema concurrency exceptions (CASSANDRA-6841)
  * Fix BatchlogManager#deleteBatch() use of millisecond timsestamps

http://git-wip-us.apache.org/repos/asf/cassandra/blob/ad404512/src/java/org/apache/cassandra/locator/GoogleCloudSnitch.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/locator/GoogleCloudSnitch.java 
b/src/java/org/apache/cassandra/locator/GoogleCloudSnitch.java
new file mode 100644
index 0000000..b7da787
--- /dev/null
+++ b/src/java/org/apache/cassandra/locator/GoogleCloudSnitch.java
@@ -0,0 +1,111 @@
+/*
+ * 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.DataInputStream;
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.InetAddress;
+import java.net.URL;
+
+import com.google.common.base.Charsets;
+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.utils.FBUtilities;
+
+/**
+ * A snitch that assumes a GCE region is a DC and a GCE availability_zone
+ *  is a rack. This information is available in the config for the node.
+ */
+public class GoogleCloudSnitch extends AbstractNetworkTopologySnitch
+{
+    protected static final Logger logger = 
LoggerFactory.getLogger(GoogleCloudSnitch.class);
+    protected static final String ZONE_NAME_QUERY_URL = 
"http://metadata.google.internal/computeMetadata/v1/instance/zone";;
+    private static final String DEFAULT_DC = "UNKNOWN-DC";
+    private static final String DEFAULT_RACK = "UNKNOWN-RACK";
+    protected String gceZone;
+    protected String gceRegion;
+
+    public GoogleCloudSnitch() throws IOException, ConfigurationException
+    {
+        String response = gceApiCall(ZONE_NAME_QUERY_URL);
+        String[] splits = response.split("/");
+        String az = splits[splits.length - 1];
+
+        // Split "us-central1-a" or "asia-east1-a" into "us-central1"/"a" and 
"asia-east1"/"a".
+        splits = az.split("-");
+        gceZone = splits[splits.length - 1];
+
+        int lastRegionIndex = az.lastIndexOf("-");
+        gceRegion = az.substring(0, lastRegionIndex);
+
+        String datacenterSuffix = (new SnitchProperties()).get("dc_suffix", 
"");
+        gceRegion = gceRegion.concat(datacenterSuffix);
+        logger.info("GCESnitch using region: {}, zone: {}.", gceRegion, 
gceZone);
+    }
+
+    String gceApiCall(String url) throws IOException, ConfigurationException
+    {
+        // Populate the region and zone by introspection, fail if 404 on 
metadata
+        HttpURLConnection conn = (HttpURLConnection) new 
URL(url).openConnection();
+        try
+        {
+            conn.setRequestMethod("GET");
+           conn.setRequestProperty("Metadata-Flavor", "Google");
+            if (conn.getResponseCode() != 200)
+                throw new ConfigurationException("GoogleCloudSnitch was unable 
to execute the API call. Not a gce node?");
+
+            // Read the information. I wish I could say (String) 
conn.getContent() here...
+            int cl = conn.getContentLength();
+            byte[] b = new byte[cl];
+            DataInputStream d = new DataInputStream((FilterInputStream) 
conn.getContent());
+            d.readFully(b);
+            return new String(b, Charsets.UTF_8);
+        }
+        finally
+        {
+            conn.disconnect();
+        }
+    }
+
+    public String getRack(InetAddress endpoint)
+    {
+        if (endpoint.equals(FBUtilities.getBroadcastAddress()))
+            return gceZone;
+        EndpointState state = 
Gossiper.instance.getEndpointStateForEndpoint(endpoint);
+        if (state == null || state.getApplicationState(ApplicationState.RACK) 
== null)
+            return DEFAULT_RACK;
+        return state.getApplicationState(ApplicationState.RACK).value;
+    }
+
+    public String getDatacenter(InetAddress endpoint)
+    {
+        if (endpoint.equals(FBUtilities.getBroadcastAddress()))
+            return gceRegion;
+        EndpointState state = 
Gossiper.instance.getEndpointStateForEndpoint(endpoint);
+        if (state == null || state.getApplicationState(ApplicationState.DC) == 
null)
+            return DEFAULT_DC;
+        return state.getApplicationState(ApplicationState.DC).value;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cassandra/blob/ad404512/test/unit/org/apache/cassandra/locator/GoogleCloudSnitchTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/locator/GoogleCloudSnitchTest.java 
b/test/unit/org/apache/cassandra/locator/GoogleCloudSnitchTest.java
new file mode 100644
index 0000000..40a662d
--- /dev/null
+++ b/test/unit/org/apache/cassandra/locator/GoogleCloudSnitchTest.java
@@ -0,0 +1,84 @@
+package org.apache.cassandra.locator;
+/*
+ *
+ * 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.
+ *
+ */
+
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.util.Map;
+
+import org.apache.cassandra.exceptions.ConfigurationException;
+import org.apache.cassandra.gms.ApplicationState;
+import org.apache.cassandra.gms.Gossiper;
+import org.apache.cassandra.gms.VersionedValue;
+import org.apache.cassandra.service.StorageService;
+import org.junit.Test;
+
+public class GoogleCloudSnitchTest
+{
+    private static String az;
+
+    private class TestGoogleCloudSnitch extends GoogleCloudSnitch
+    {
+        public TestGoogleCloudSnitch() throws IOException, 
ConfigurationException
+        {
+            super();
+        }
+
+        @Override
+        String gceApiCall(String url) throws IOException, 
ConfigurationException
+        {
+            return az;
+        }
+    }
+
+    @Test
+    public void testRac() throws IOException, ConfigurationException
+    {
+        az = "us-central1-a";
+        GoogleCloudSnitch snitch = new TestGoogleCloudSnitch();
+        InetAddress local = InetAddress.getByName("127.0.0.1");
+        InetAddress nonlocal = InetAddress.getByName("127.0.0.7");
+
+        Gossiper.instance.addSavedEndpoint(nonlocal);
+        Map<ApplicationState,VersionedValue> stateMap = 
Gossiper.instance.getEndpointStateForEndpoint(nonlocal).getApplicationStateMap();
+        stateMap.put(ApplicationState.DC, 
StorageService.instance.valueFactory.datacenter("europe-west1"));
+        stateMap.put(ApplicationState.RACK, 
StorageService.instance.valueFactory.datacenter("a"));
+
+        assertEquals("europe-west1", snitch.getDatacenter(nonlocal));
+        assertEquals("a", snitch.getRack(nonlocal));
+
+        assertEquals("us-central1", snitch.getDatacenter(local));
+        assertEquals("a", snitch.getRack(local));
+    }
+    
+    @Test
+    public void testNewRegions() throws IOException, ConfigurationException
+    {
+        az = "asia-east1-a";
+        GoogleCloudSnitch snitch = new TestGoogleCloudSnitch();
+        InetAddress local = InetAddress.getByName("127.0.0.1");
+        assertEquals("asia-east1", snitch.getDatacenter(local));
+        assertEquals("a", snitch.getRack(local));
+    }
+}

Reply via email to