cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbel...@apache.org
Subject svn commit: r928820 - /cassandra/trunk/test/unit/org/apache/cassandra/locator/RackAwareStrategyTest.java
Date Mon, 29 Mar 2010 16:13:08 GMT
Author: jbellis
Date: Mon Mar 29 16:13:07 2010
New Revision: 928820

URL: http://svn.apache.org/viewvc?rev=928820&view=rev
Log:
add RackAwareStrategyTest.  patch by Rodrigo Peinado; reviewed by jbellis for CASSANDRA-776

Added:
    cassandra/trunk/test/unit/org/apache/cassandra/locator/RackAwareStrategyTest.java   (with
props)

Added: cassandra/trunk/test/unit/org/apache/cassandra/locator/RackAwareStrategyTest.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/locator/RackAwareStrategyTest.java?rev=928820&view=auto
==============================================================================
--- cassandra/trunk/test/unit/org/apache/cassandra/locator/RackAwareStrategyTest.java (added)
+++ cassandra/trunk/test/unit/org/apache/cassandra/locator/RackAwareStrategyTest.java Mon
Mar 29 16:13:07 2010
@@ -0,0 +1,172 @@
+/*
+* 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 static org.junit.Assert.assertEquals;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.cassandra.config.DatabaseDescriptor;
+import org.apache.cassandra.dht.BigIntegerToken;
+import org.apache.cassandra.dht.Token;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class RackAwareStrategyTest
+{
+    private List<Token> endPointTokens;
+    private List<Token> keyTokens;
+    private TokenMetadata tmd;
+    private Map<String, ArrayList<InetAddress>> expectedResults;
+
+    @Before
+    public void init()
+    {
+        endPointTokens = new ArrayList<Token>();
+        keyTokens = new ArrayList<Token>();
+        tmd = new TokenMetadata();
+        expectedResults = new HashMap<String, ArrayList<InetAddress>>();
+    }
+
+    /**
+     * 4 same rack endpoints
+     *
+     * @throws UnknownHostException
+     */
+    @Test
+    public void testBigIntegerEndpointsA() throws UnknownHostException
+    {
+        EndPointSnitch endPointSnitch = new EndPointSnitch();
+
+        AbstractReplicationStrategy strategy = new RackAwareStrategy(tmd, endPointSnitch);
+        addEndPoint("0", "5", "254.0.0.1");
+        addEndPoint("10", "15", "254.0.0.2");
+        addEndPoint("20", "25", "254.0.0.3");
+        addEndPoint("30", "35", "254.0.0.4");
+
+        expectedResults.put("5", buildResult("254.0.0.2", "254.0.0.3", "254.0.0.4"));
+        expectedResults.put("15", buildResult("254.0.0.3", "254.0.0.4", "254.0.0.1"));
+        expectedResults.put("25", buildResult("254.0.0.4", "254.0.0.1", "254.0.0.2"));
+        expectedResults.put("35", buildResult("254.0.0.1", "254.0.0.2", "254.0.0.3"));
+
+        runTestForReplicatedTables(strategy);
+    }
+
+    /**
+     * 3 same rack endpoints
+     * 1 external datacenter
+     *
+     * @throws UnknownHostException
+     */
+    @Test
+    public void testBigIntegerEndpointsB() throws UnknownHostException
+    {
+        EndPointSnitch endPointSnitch = new EndPointSnitch();
+
+        AbstractReplicationStrategy strategy = new RackAwareStrategy(tmd, endPointSnitch);
+        addEndPoint("0", "5", "254.0.0.1");
+        addEndPoint("10", "15", "254.0.0.2");
+        addEndPoint("20", "25", "254.1.0.3");
+        addEndPoint("30", "35", "254.0.0.4");
+
+        expectedResults.put("5", buildResult("254.0.0.2", "254.1.0.3", "254.0.0.4"));
+        expectedResults.put("15", buildResult("254.1.0.3", "254.0.0.4", "254.0.0.1"));
+        expectedResults.put("25", buildResult("254.0.0.4", "254.1.0.3", "254.0.0.1"));
+        expectedResults.put("35", buildResult("254.0.0.1", "254.1.0.3", "254.0.0.2"));
+
+        runTestForReplicatedTables(strategy);
+    }
+
+    /**
+     * 2 same rack endpoints
+     * 1 same datacenter, different rack endpoints
+     * 1 external datacenter
+     *
+     * @throws UnknownHostException
+     */
+    @Test
+    public void testBigIntegerEndpointsC() throws UnknownHostException
+    {
+        EndPointSnitch endPointSnitch = new EndPointSnitch();
+
+        AbstractReplicationStrategy strategy = new RackAwareStrategy(tmd, endPointSnitch);
+        addEndPoint("0", "5", "254.0.0.1");
+        addEndPoint("10", "15", "254.0.0.2");
+        addEndPoint("20", "25", "254.0.1.3");
+        addEndPoint("30", "35", "254.1.0.4");
+
+        expectedResults.put("5", buildResult("254.0.0.2", "254.0.1.3", "254.1.0.4"));
+        expectedResults.put("15", buildResult("254.0.1.3", "254.1.0.4", "254.0.0.1"));
+        expectedResults.put("25", buildResult("254.1.0.4", "254.0.0.1", "254.0.0.2"));
+        expectedResults.put("35", buildResult("254.0.0.1", "254.0.1.3", "254.1.0.4"));
+
+        runTestForReplicatedTables(strategy);
+    }
+
+    private void runTestForReplicatedTables(AbstractReplicationStrategy strategy) throws
UnknownHostException
+    {
+        for (String table : DatabaseDescriptor.getNonSystemTables())
+        {
+            if (DatabaseDescriptor.getReplicationFactor(table) == 3)
+                testGetEndpoints(strategy, keyTokens.toArray(new Token[0]), table);
+        }
+    }
+
+    private ArrayList<InetAddress> buildResult(String... addresses) throws UnknownHostException
+    {
+        ArrayList<InetAddress> result = new ArrayList<InetAddress>();
+        for (String address : addresses)
+        {
+            result.add(InetAddress.getByName(address));
+        }
+        return result;
+    }
+
+    private void addEndPoint(String endPointTokenID, String keyTokenID, String endPointAddress)
throws UnknownHostException
+    {
+        BigIntegerToken endPointToken = new BigIntegerToken(endPointTokenID);
+        endPointTokens.add(endPointToken);
+
+        BigIntegerToken keyToken = new BigIntegerToken(keyTokenID);
+        keyTokens.add(keyToken);
+
+        InetAddress ep = InetAddress.getByName(endPointAddress);
+        tmd.updateNormalToken(endPointToken, ep);
+    }
+
+    private void testGetEndpoints(AbstractReplicationStrategy strategy, Token[] keyTokens,
String table) throws UnknownHostException
+    {
+        for (Token keyToken : keyTokens)
+        {
+            List<InetAddress> endPoints = strategy.getNaturalEndpoints(keyToken, tmd,
table);
+            for (int j = 0; j < endPoints.size(); j++)
+            {
+                ArrayList<InetAddress> hostsExpected = expectedResults.get(keyToken.toString());
+                assertEquals(endPoints.get(j), hostsExpected.get(j));
+            }
+        }
+    }
+
+}
\ No newline at end of file

Propchange: cassandra/trunk/test/unit/org/apache/cassandra/locator/RackAwareStrategyTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message