You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ea...@apache.org on 2019/04/23 08:27:00 UTC

[incubator-iotdb] branch cluster_nodetool updated: add RaftUtil UT

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

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


The following commit(s) were added to refs/heads/cluster_nodetool by this push:
     new 00cc9e6  add RaftUtil UT
00cc9e6 is described below

commit 00cc9e6d78d50659f50f4c46c537e539051787f6
Author: mdf369 <95...@qq.com>
AuthorDate: Tue Apr 23 16:26:45 2019 +0800

    add RaftUtil UT
---
 .../org/apache/iotdb/cluster/utils/RaftUtils.java  |  8 +++
 .../apache/iotdb/cluster/utils/hash/Router.java    |  5 +-
 .../apache/iotdb/cluster/utils/RaftUtilsTest.java  | 71 +++++++++++++++++++++-
 .../iotdb/monitor/service/ClusterMonitorMBean.java |  4 ++
 4 files changed, 85 insertions(+), 3 deletions(-)

diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/utils/RaftUtils.java b/cluster/src/main/java/org/apache/iotdb/cluster/utils/RaftUtils.java
index dd5570a..b6f4816 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/utils/RaftUtils.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/utils/RaftUtils.java
@@ -343,6 +343,10 @@ public class RaftUtils {
    * @param sg storage group ID. If null, return metadata group info
    */
   public static PeerId[] getDataPartitionOfSG(String sg) {
+    return getDataPartitionOfSG(sg, server, router);
+  }
+
+  public static PeerId[] getDataPartitionOfSG(String sg, Server server, Router router) {
     String groupId;
     PeerId[] nodes;
     if (sg == null) {
@@ -371,6 +375,10 @@ public class RaftUtils {
   }
 
   public static Map<String[], String[]> getDataPartitionOfNode(String ip, int port) {
+    return getDataPartitionOfNode(ip, port, server, router);
+  }
+
+  public static Map<String[], String[]> getDataPartitionOfNode(String ip, int port, Server server, Router router) {
     PhysicalNode[][] groups = router.getGroupsNodes(ip, port);
 
     Map<String, List<String>> groupSGMap = new LinkedHashMap<>();
diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/utils/hash/Router.java b/cluster/src/main/java/org/apache/iotdb/cluster/utils/hash/Router.java
index 2031676..a0a18ba 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/utils/hash/Router.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/utils/hash/Router.java
@@ -87,8 +87,11 @@ public class Router {
    * Change this method to public for test, you should not invoke this method explicitly.
    */
   public void init() {
+    init(ClusterDescriptor.getInstance().getConfig());
+  }
+
+  public void init(ClusterConfig config) {
     reset();
-    ClusterConfig config = ClusterDescriptor.getInstance().getConfig();
     String[] hosts = config.getNodes();
     int replicator = config.getReplication();
     int numOfVirtualNodes = config.getNumOfVirtualNodes();
diff --git a/cluster/src/test/java/org/apache/iotdb/cluster/utils/RaftUtilsTest.java b/cluster/src/test/java/org/apache/iotdb/cluster/utils/RaftUtilsTest.java
index 7205031..912b57e 100644
--- a/cluster/src/test/java/org/apache/iotdb/cluster/utils/RaftUtilsTest.java
+++ b/cluster/src/test/java/org/apache/iotdb/cluster/utils/RaftUtilsTest.java
@@ -29,13 +29,18 @@ import com.alipay.sofa.jraft.entity.Task;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 
 import com.alipay.sofa.jraft.entity.PeerId;
+import java.util.Map;
 import org.apache.iotdb.cluster.config.ClusterConfig;
 import org.apache.iotdb.cluster.config.ClusterDescriptor;
 import org.apache.iotdb.cluster.entity.Server;
 import org.apache.iotdb.cluster.entity.raft.MetadataRaftHolder;
+import org.apache.iotdb.cluster.entity.raft.MetadataStateManchine;
 import org.apache.iotdb.cluster.entity.raft.RaftService;
 import org.apache.iotdb.cluster.qp.callback.QPTask;
 import org.apache.iotdb.cluster.qp.callback.SingleQPTask;
@@ -67,6 +72,14 @@ public class RaftUtilsTest {
   };
   private int replicator = 3;
 
+  private String[] storageGroups = {
+      "root.d1",
+      "root.d2",
+      "root.d3",
+      "root.d4",
+      "root.d5"
+  };
+
   @Mock
   private Server server;
   @Mock
@@ -87,6 +100,11 @@ public class RaftUtilsTest {
   @Mock
   private SingleQPTask nullReadTask;
 
+  @Mock
+  Router router;
+  @Mock
+  MetadataStateManchine metadataStateManchine;
+
   private List<PeerId> peerIds;
 
   @Before
@@ -100,6 +118,8 @@ public class RaftUtilsTest {
     when(metadataHolder.getService()).thenReturn(service);
     when(service.getPeerIdList()).thenReturn(peerIds);
     when(service.getNode()).thenReturn(node);
+    when((service).getFsm()).thenReturn(metadataStateManchine);
+    when(metadataStateManchine.getAllStorageGroups()).thenReturn(new HashSet<>(Arrays.asList(storageGroups)));
     Mockito.doNothing().when(node).apply(any(Task.class));
     Mockito.doNothing().when(response).addResult(any(boolean.class));
     Mockito.doNothing().when(response).setErrorMsg(any(String.class));
@@ -108,11 +128,12 @@ public class RaftUtilsTest {
     numOfVirtualNodesOld = config.getNumOfVirtualNodes();
 
     int numOfVirtualNodes = 2;
+    config.setPort(PORT);
     config.setNodes(ipList);
     config.setReplication(replicator);
     config.setNumOfVirtualNodes(numOfVirtualNodes);
-    Router router = Router.getInstance();
-    router.init();
+    router = Router.getInstance();
+    router.init(config);
     router.showPhysicalRing();
   }
 
@@ -234,4 +255,50 @@ public class RaftUtilsTest {
     Mockito.doNothing().when(nullReadTask).await();
     RaftUtils.handleNullReadToMetaGroup(new Status(), server, nullReadTask);
   }
+
+  @Test
+  public void testGetDataPartitionOfSG() {
+    PeerId metadtaLeader = PeerId.parsePeer(ipList[0]);
+    RaftUtils.updateRaftGroupLeader(ClusterConfig.METADATA_GROUP_ID, metadtaLeader);
+    PeerId[] metadataGroup = RaftUtils.getDataPartitionOfSG(null, server, router);
+    assertArrayEquals(peerIds.toArray(new PeerId[peerIds.size()]), metadataGroup);
+
+    String sg = storageGroups[0];
+    PhysicalNode[] physicalGroup = router.routeGroup(sg);
+    String groupId = router.getGroupID(physicalGroup);
+    PeerId[] expectGroup = RaftUtils.getPeerIdArrayFrom(physicalGroup);
+    RaftUtils.updateRaftGroupLeader(groupId, expectGroup[0]);
+    PeerId[] dataGroup = RaftUtils.getDataPartitionOfSG(sg, server, router);
+    assertArrayEquals(expectGroup, dataGroup);
+  }
+
+  @Test
+  public void testGetDataPartitionOfNode() {
+    PeerId node = peerIds.get(0);
+    Map<String[], String[]> dataPartition = RaftUtils.getDataPartitionOfNode(node.getIp(), node.getPort(), server, router);
+    Map<String, String[]> dataPartitionMap = new HashMap<>();
+    dataPartition.forEach((key, value) -> dataPartitionMap.put(buildKey(key), value));
+
+    String[][] keys = {{"192.168.130.1", "192.168.130.3", "192.168.130.4"},
+        {"192.168.130.3", "192.168.130.4", "192.168.130.5"},
+        {"192.168.130.2", "192.168.130.4", "192.168.130.5"}};
+    String[][] values = {{},
+        {"root.d1", "root.d4"},
+        {"root.d3", "root.d2"}};
+    Map<String, String[]> expectMap = new HashMap<>();
+    for (int i = 0; i < keys.length; i++) {
+      expectMap.put(buildKey(keys[i]), values[i]);
+    }
+
+    assertEquals(dataPartitionMap.size(), expectMap.size());
+    dataPartitionMap.forEach((key, value) -> assertArrayEquals(value, expectMap.get(key)));
+  }
+
+  private String buildKey(String[] ss) {
+    StringBuilder builder = new StringBuilder();
+    for (int i = 0; i < ss.length; i++) {
+      builder.append(ss[i]).append('#');
+    }
+    return builder.toString();
+  }
 }
diff --git a/monitor/src/main/java/org/apache/iotdb/monitor/service/ClusterMonitorMBean.java b/monitor/src/main/java/org/apache/iotdb/monitor/service/ClusterMonitorMBean.java
index bce36c4..f54573e 100644
--- a/monitor/src/main/java/org/apache/iotdb/monitor/service/ClusterMonitorMBean.java
+++ b/monitor/src/main/java/org/apache/iotdb/monitor/service/ClusterMonitorMBean.java
@@ -23,6 +23,10 @@ import java.util.Map;
 
 public interface ClusterMonitorMBean {
 
+  /**
+   * Original format = String.format("%s:%s=%s",
+   * IoTDBConstant.IOTDB_PACKAGE, IoTDBConstant.JMX_TYPE, getID().getJmxName()
+   */
   public static final String MBEAN_NAME = "org.apache.iotdb.service:type=Cluster Monitor";
 
   /**