This is an automated email from the ASF dual-hosted git repository.

dope pushed a commit to branch cluster
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git


The following commit(s) were added to refs/heads/cluster by this push:
     new 7d02eb7  add hash function test
7d02eb7 is described below

commit 7d02eb72f76a76953bb04e628552379d789783eb
Author: XuYi <[email protected]>
AuthorDate: Tue Mar 26 12:11:11 2019 +0800

    add hash function test
---
 .../apache/iotdb/cluster/utils/PhysicalNode.java   |  23 +---
 .../org/apache/iotdb/cluster/utils/Router.java     |  38 +++---
 .../apache/iotdb/cluster/utils/MD5HashTest.java    |  43 ++++--
 .../iotdb/cluster/utils/PhysicalNodeTest.java      |  48 +++----
 .../org/apache/iotdb/cluster/utils/RouterTest.java | 147 +++++++++++++++++++--
 5 files changed, 217 insertions(+), 82 deletions(-)

diff --git 
a/cluster/src/main/java/org/apache/iotdb/cluster/utils/PhysicalNode.java 
b/cluster/src/main/java/org/apache/iotdb/cluster/utils/PhysicalNode.java
index 57a5bea..fa12f68 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/utils/PhysicalNode.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/utils/PhysicalNode.java
@@ -1,21 +1,3 @@
-/**
- * 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.iotdb.cluster.utils;
 
 
@@ -62,4 +44,9 @@ public class PhysicalNode {
     }
     return this.ip.equals(other.ip);
   }
+
+  @Override
+  public String toString() {
+    return getKey();
+  }
 }
diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/utils/Router.java 
b/cluster/src/main/java/org/apache/iotdb/cluster/utils/Router.java
index fe1b475..45542db 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/utils/Router.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/utils/Router.java
@@ -1,19 +1,19 @@
 
 package org.apache.iotdb.cluster.utils;
 
-import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.SortedMap;
 import java.util.TreeMap;
 import java.util.concurrent.ConcurrentHashMap;
 
+import org.apache.iotdb.cluster.config.ClusterConfig;
+import org.apache.iotdb.cluster.config.ClusterDescriptor;
 import org.apache.iotdb.cluster.exception.ErrorConfigureExecption;
 
 public class Router {
 
-  private List<PhysicalNode> nodes = new ArrayList<>();
   // Replication number
   private int replicator;
   private final int numOfVirtulaNodes = 2;
@@ -31,13 +31,6 @@ public class Router {
   }
 
   private Router() {
-    // TODO get form config file
-    // String[] ipList = {"192.168.130.1", "192.168.130.2", "192.168.130.3"};
-    // this.replicator = replicator;
-    // int port = 7777;
-//    for (String ip : ipList) {
-//      nodes.add(new PhysicalNode(ip, port));
-//    }
     init();
   }
 
@@ -45,12 +38,17 @@ public class Router {
     return RouterHolder.INSTANCE;
   }
 
-  private void init() {
+  public void init() {
     reset();
-    for (PhysicalNode node : nodes) {
-      addNode(node, this.numOfVirtulaNodes);
+    ClusterConfig config = ClusterDescriptor.getInstance().getConfig();
+    String[] ipList = config.getNodes();
+    this.replicator = config.getReplication();
+    int port = config.getPort();
+    for (String ip : ipList) {
+      PhysicalNode node = new PhysicalNode(ip, port);
+      addNode(node, numOfVirtulaNodes);
     }
-    PhysicalNode[] nodes = (PhysicalNode[]) physicalRing.values().toArray();
+    PhysicalNode[] nodes = physicalRing.values().toArray(new 
PhysicalNode[physicalRing.size()]);
     int len = nodes.length;
     for (int i = 0; i < len; i++) {
       PhysicalNode first = nodes[i];
@@ -60,14 +58,14 @@ public class Router {
       } else if (len == replicator) {
         PhysicalNode[][] val = new PhysicalNode[1][len];
         for (int j = 0; j < len; j++) {
-          val[0][j] = nodes[i + j % len];
+          val[0][j] = nodes[(i + j) % len];
         }
         dataPartitionCache.put(first, val);
       } else {
         PhysicalNode[][] val = new PhysicalNode[replicator][replicator];
         for (int j = 0; j < replicator; j++) {
           for (int k = 0; k < replicator; k++) {
-            val[j][k] = nodes[(i - j + k) % len];
+            val[j][k] = nodes[(i - j + k + len) % len];
           }
         }
         dataPartitionCache.put(first, val);
@@ -112,10 +110,16 @@ public class Router {
     return dataPartitionCache.get(node);
   }
 
-  private void reset(){
+  private void reset() {
     physicalRing.clear();
     virtualRing.clear();
     router.clear();
     dataPartitionCache.clear();
   }
+
+  public void showPhysicalRing() {
+    for (Entry<Integer, PhysicalNode> entry : physicalRing.entrySet()) {
+      System.out.println(String.format("%d-%s", entry.getKey(), 
entry.getValue().getKey()));
+    }
+  }
 }
diff --git 
a/cluster/src/test/java/org/apache/iotdb/cluster/utils/MD5HashTest.java 
b/cluster/src/test/java/org/apache/iotdb/cluster/utils/MD5HashTest.java
index e6c1c63..7490995 100644
--- a/cluster/src/test/java/org/apache/iotdb/cluster/utils/MD5HashTest.java
+++ b/cluster/src/test/java/org/apache/iotdb/cluster/utils/MD5HashTest.java
@@ -2,23 +2,46 @@ package org.apache.iotdb.cluster.utils;
 
 import static org.junit.Assert.*;
 
+import java.util.concurrent.CountDownLatch;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
 public class MD5HashTest {
 
-       @Before
-       public void setUp() throws Exception {
-       }
+  private MD5Hash function = new MD5Hash();
+  private final String str = "root.device.sensor";
+  private final int result = 1936903121;
+  private CountDownLatch latch;
 
-       @After
-       public void tearDown() throws Exception {
-       }
+  @Before
+  public void setUp() throws Exception {
+    latch = new CountDownLatch(2);
+  }
 
-       @Test
-       public void testHash() {
-               fail("Not yet implemented");
-       }
+  @After
+  public void tearDown() throws Exception {
+  }
+
+  @Test
+  public void testHash() throws InterruptedException {
+    Thread t1 = new TestThread();
+    Thread t2 = new TestThread();
+    t1.start();
+    t2.start();
+    latch.await();
+  }
+
+  private class TestThread extends Thread {
+
+    @Override
+    public void run() {
+      for (int i = 0; i < 100000; i++) {
+        assertEquals(result, function.hash(str));
+      }
+      System.out.println("Thread exit");
+      latch.countDown();
+    }
+  }
 
 }
diff --git 
a/cluster/src/test/java/org/apache/iotdb/cluster/utils/PhysicalNodeTest.java 
b/cluster/src/test/java/org/apache/iotdb/cluster/utils/PhysicalNodeTest.java
index b006194..84c6b9c 100644
--- a/cluster/src/test/java/org/apache/iotdb/cluster/utils/PhysicalNodeTest.java
+++ b/cluster/src/test/java/org/apache/iotdb/cluster/utils/PhysicalNodeTest.java
@@ -8,27 +8,29 @@ import org.junit.Test;
 
 public class PhysicalNodeTest {
 
-       @Before
-       public void setUp() throws Exception {
-       }
-
-       @After
-       public void tearDown() throws Exception {
-       }
-
-       @Test
-       public void testHashCode() {
-               fail("Not yet implemented");
-       }
-
-       @Test
-       public void testGetKey() {
-               fail("Not yet implemented");
-       }
-
-       @Test
-       public void testEqualsObject() {
-               fail("Not yet implemented");
-       }
-
+  @Before
+  public void setUp() throws Exception {
+  }
+
+  @After
+  public void tearDown() throws Exception {
+  }
+
+  @Test
+  public void testEqualsObject1() {
+    PhysicalNode n1 = new PhysicalNode("192.168.130.1", 6666);
+    PhysicalNode n2 = new PhysicalNode("192.168.130.1", 6666);
+    assertEquals(n1, n2);
+    assertEquals(n1.getKey(), n2.getKey());
+    assertEquals(n1.hashCode(), n2.hashCode());
+  }
+
+  @Test
+  public void testEqualsObject2() {
+    PhysicalNode n1 = new PhysicalNode(null, 6666);
+    PhysicalNode n2 = new PhysicalNode(null, 6666);
+    assertEquals(n1, n2);
+    assertEquals(n1.getKey(), n2.getKey());
+    assertEquals(n1.hashCode(), n2.hashCode());
+  }
 }
diff --git 
a/cluster/src/test/java/org/apache/iotdb/cluster/utils/RouterTest.java 
b/cluster/src/test/java/org/apache/iotdb/cluster/utils/RouterTest.java
index d6e13bf..7cbf89c 100644
--- a/cluster/src/test/java/org/apache/iotdb/cluster/utils/RouterTest.java
+++ b/cluster/src/test/java/org/apache/iotdb/cluster/utils/RouterTest.java
@@ -2,28 +2,147 @@ package org.apache.iotdb.cluster.utils;
 
 import static org.junit.Assert.*;
 
+import org.apache.iotdb.cluster.config.ClusterConfig;
+import org.apache.iotdb.cluster.config.ClusterDescriptor;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
 public class RouterTest {
 
-       @Before
-       public void setUp() throws Exception {
-       }
+  ClusterConfig config = ClusterDescriptor.getInstance().getConfig();
+  String[] ipListOld;
+  int portOld;
+  int replicatorOld;
 
-       @After
-       public void tearDown() throws Exception {
-       }
+  @Before
+  public void setUp() throws Exception {
+    ipListOld = config.getNodes();
+    portOld = config.getPort();
+    replicatorOld = config.getReplication();
 
-       @Test
-       public void testRouteGroup() {
-               fail("Not yet implemented");
-       }
+  }
 
-       @Test
-       public void testGenerateGroups() {
-               fail("Not yet implemented");
-       }
+  @After
+  public void tearDown() throws Exception {
+    config.setNodes(ipListOld);
+    config.setPort(portOld);
+    config.setReplication(replicatorOld);
+  }
 
+//     @Test
+//     public void testRouteGroup() {
+//
+//     }
+
+  @Test
+  public void testGenerateGroups1() {
+    String[] ipList = {"192.168.130.1", "192.168.130.2", "192.168.130.3", 
"192.168.130.4",
+        "192.168.130.5",};
+    int port = 7777;
+    int replicator = 3;
+    config.setNodes(ipList);
+    config.setPort(port);
+    config.setReplication(replicator);
+
+    Router router = Router.getInstance();
+    router.init();
+//    router.showPhysicalRing();
+    String[][][] ipIndex = {
+        {
+            {"192.168.130.1", "192.168.130.3", "192.168.130.4",},
+            {"192.168.130.2", "192.168.130.1", "192.168.130.3",},
+            {"192.168.130.5", "192.168.130.2", "192.168.130.1",},
+        },
+        {
+            {"192.168.130.2", "192.168.130.1", "192.168.130.3",},
+            {"192.168.130.5", "192.168.130.2", "192.168.130.1",},
+            {"192.168.130.4", "192.168.130.5", "192.168.130.2",},
+        },
+        {
+            {"192.168.130.3", "192.168.130.4", "192.168.130.5",},
+            {"192.168.130.1", "192.168.130.3", "192.168.130.4",},
+            {"192.168.130.2", "192.168.130.1", "192.168.130.3",},
+        },
+        {
+            {"192.168.130.4", "192.168.130.5", "192.168.130.2",},
+            {"192.168.130.3", "192.168.130.4", "192.168.130.5",},
+            {"192.168.130.1", "192.168.130.3", "192.168.130.4",},
+        },
+        {
+            {"192.168.130.5", "192.168.130.2", "192.168.130.1",},
+            {"192.168.130.4", "192.168.130.5", "192.168.130.2",},
+            {"192.168.130.3", "192.168.130.4", "192.168.130.5",},
+        },
+    };
+    for (int i = 1; i < 5; i++) {
+      PhysicalNode[][] expected = generateNodesArray(ipIndex[i - 1], 3, 3, 
port);
+      assertEquals(expected, router.generateGroups("192.168.130." + i, port));
+    }
+  }
+
+  @Test
+  public void testGenerateGroups2() {
+    String[] ipList = {"192.168.130.1", "192.168.130.2", "192.168.130.3"};
+    int port = 7777;
+    int replicator = 3;
+    config.setNodes(ipList);
+    config.setPort(port);
+    config.setReplication(replicator);
+
+    Router router = Router.getInstance();
+    router.init();
+//    router.showPhysicalRing();
+    String[][][] ipIndex = {
+        {
+            {"192.168.130.1", "192.168.130.3", "192.168.130.2",},
+        },
+        {
+            {"192.168.130.2", "192.168.130.1", "192.168.130.3",},
+        },
+        {
+            {"192.168.130.3", "192.168.130.2", "192.168.130.1",},
+        },
+    };
+    for (int i = 1; i < 4; i++) {
+      PhysicalNode[][] expected = generateNodesArray(ipIndex[i - 1], 1, 3, 
port);
+      assertEquals(expected, router.generateGroups("192.168.130." + i, port));
+    }
+  }
+
+  boolean assertEquals(PhysicalNode[][] expect, PhysicalNode[][] actual) {
+    if (expect.length != actual.length) {
+      return false;
+    }
+    int len = expect.length;
+    for (int i = 0; i < len; i++) {
+      if (!assertEquals(expect[i], actual[i])) {
+        return false;
+      }
+    }
+    return true;
+  }
+
+  boolean assertEquals(PhysicalNode[] expect, PhysicalNode[] actual) {
+    if (expect.length != actual.length) {
+      return false;
+    }
+    int len = expect.length;
+    for (int i = 0; i < len; i++) {
+      if (!expect[i].equals(actual[i])) {
+        return false;
+      }
+    }
+    return true;
+  }
+
+  PhysicalNode[][] generateNodesArray(String[][] ip, int row, int col, int 
port) {
+    PhysicalNode[][] result = new PhysicalNode[row][col];
+    for (int i = 0; i < row; i++) {
+      for (int j = 0; j < col; j++) {
+        result[i][j] = new PhysicalNode(ip[i][j], port);
+      }
+    }
+    return result;
+  }
 }

Reply via email to