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

[incubator-iotdb] branch cluster updated: add config test

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 7b92bc9  add config test
7b92bc9 is described below

commit 7b92bc950733ca538b5d3ee72a3836ba62f656bd
Author: XuYi <xu...@126.com>
AuthorDate: Thu Apr 4 18:10:38 2019 +0800

    add config test
---
 .../apache/iotdb/cluster/config/ClusterConfig.java |  13 +-
 .../iotdb/cluster/config/ClusterDescriptor.java    |   6 +-
 .../apache/iotdb/cluster/utils/hash/Router.java    |   2 +-
 .../cluster/config/ClusterDescriptorTest.java      | 194 +++++++++++++++++++++
 4 files changed, 210 insertions(+), 5 deletions(-)

diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/config/ClusterConfig.java b/cluster/src/main/java/org/apache/iotdb/cluster/config/ClusterConfig.java
index 2adef86..baf55b7 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/config/ClusterConfig.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/config/ClusterConfig.java
@@ -27,6 +27,8 @@ import org.apache.iotdb.db.utils.FilePathUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.alipay.sofa.jraft.util.OnlyForTest;
+
 public class ClusterConfig {
 
   private static final Logger LOGGER = LoggerFactory.getLogger(ClusterConfig.class);
@@ -132,13 +134,13 @@ public class ClusterConfig {
     this.raftMetadataPath = raftDir + File.separatorChar + DEFAULT_RAFT_METADATA_DIR;
   }
 
-  public void createAllPath(){
+  public void createAllPath() {
     createPath(this.raftSnapshotPath);
     createPath(this.raftLogPath);
     createPath(this.raftMetadataPath);
   }
 
-  private void createPath(String path){
+  private void createPath(String path) {
     try {
       FileUtils.forceMkdir(new File(path));
     } catch (IOException e) {
@@ -146,6 +148,13 @@ public class ClusterConfig {
     }
   }
 
+  @OnlyForTest
+  public void deleteAllPath() throws IOException {
+    FileUtils.deleteDirectory(new File(this.raftSnapshotPath));
+    FileUtils.deleteDirectory(new File(this.raftLogPath));
+    FileUtils.deleteDirectory(new File(this.raftMetadataPath));
+  }
+
   public String[] getNodes() {
     return nodes;
   }
diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/config/ClusterDescriptor.java b/cluster/src/main/java/org/apache/iotdb/cluster/config/ClusterDescriptor.java
index 4a4c6ef..ee6a536 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/config/ClusterDescriptor.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/config/ClusterDescriptor.java
@@ -46,9 +46,11 @@ public class ClusterDescriptor {
   }
 
   /**
-   * load an property file and set ClusterConfig variables.
+   * Load an property file and set ClusterConfig variables.
+   * Change this method to public only for test.
+   * In most case, you should invoke this method.
    */
-  private void loadProps() {
+  public void loadProps() {
     conf.setDefaultPath();
     InputStream inputStream;
     String url = System.getProperty(IoTDBConstant.IOTDB_CONF, null);
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 6b441d6..0d8e896 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
@@ -94,7 +94,7 @@ public class Router {
     int numOfVirtualNodes = config.getNumOfVirtualNodes();
     for (String host : hosts) {
       String[] values = host.split(":");
-      PhysicalNode node = new PhysicalNode(values[0], Integer.parseInt(values[1]));
+      PhysicalNode node = new PhysicalNode(values[0].trim(), Integer.parseInt(values[1].trim()));
       addNode(node, numOfVirtualNodes);
     }
     PhysicalNode[] nodes = physicalRing.values().toArray(new PhysicalNode[0]);
diff --git a/cluster/src/test/java/org/apache/iotdb/cluster/config/ClusterDescriptorTest.java b/cluster/src/test/java/org/apache/iotdb/cluster/config/ClusterDescriptorTest.java
new file mode 100644
index 0000000..db185c9
--- /dev/null
+++ b/cluster/src/test/java/org/apache/iotdb/cluster/config/ClusterDescriptorTest.java
@@ -0,0 +1,194 @@
+package org.apache.iotdb.cluster.config;
+
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.iotdb.db.conf.IoTDBConstant;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ClusterDescriptorTest {
+
+  private String path = "src" + File.separatorChar + "test" + File.separatorChar + "resources";
+  private String absoultePath;
+
+  private String testNodesNew = "192.168.130.1:8888,192.168.130.2:8888,192.168.130.3:8888";
+  private String testReplicationNew = "5";
+  private String testIPNew = "192.168.130.1";
+  private String testPortNew = "123456";
+  private String testRaftLogPathNew = "/tmp/log";
+  private String testRaftSnapshotPathNew = "/tmp/snapshot";
+  private String testRaftMetadataPathNew = "/tmp/metadata";
+  private String testMaxCatchUpLogNumNew = "50000";
+  private String testDelaySnapshotNew = "true";
+  private String testDelayHoursNew = "1111";
+  private String testTaskRedoCountNew = "6";
+  private String testTaskTimeoutMSNew = "100000";
+  private String testVNodesNew = "4";
+  private String testClientNumNew = "400000";
+  private String testQueueLenNew = "300000";
+  private String testMetadataConsistencyNew = "2";
+  private String testDataConsistencyNew = "4";
+
+  private String[] testNodesOld;
+  private int testReplicationOld;
+  private String testIPOld;
+  private int testPortOld;
+  private String testRaftLogPathOld;
+  private String testRaftSnapshotPathOld;
+  private String testRaftMetadataPathOld;
+  private int testMaxCatchUpLogNumOld;
+  private boolean testDelaySnapshotOld;
+  private int testDelayHoursOld;
+  private int testTaskRedoCountOld;
+  private int testTaskTimeoutMSOld;
+  private int testVNodesOld;
+  private int testClientNumOld;
+  private int testQueueLenOld;
+  private int testMetadataConsistencyOld;
+  private int testDataConsistencyOld;
+  private Map<String, String> testConfigMap = new HashMap<String, String>() {
+    private static final long serialVersionUID = 7832408957178621132L;
+
+    {
+      put("nodes", testNodesNew);
+      put("replication", testReplicationNew);
+      put("ip", testIPNew);
+      put("port", testPortNew);
+      put("raft_log_path", testRaftLogPathNew);
+      put("raft_snapshot_path", testRaftSnapshotPathNew);
+      put("raft_metadata_path", testRaftMetadataPathNew);
+      put("max_catch_up_log_num", testMaxCatchUpLogNumNew);
+      put("delay_snapshot", testDelaySnapshotNew);
+      put("delay_hours", testDelayHoursNew);
+      put("task_redo_count", testTaskRedoCountNew);
+      put("task_timeout_ms", testTaskTimeoutMSNew);
+      put("num_of_virtula_nodes", testVNodesNew);
+      put("max_num_of_inner_rpc_client", testClientNumNew);
+      put("max_queue_num_of_inner_rpc_client", testQueueLenNew);
+      put("read_metadata_consistency_level", testMetadataConsistencyNew);
+      put("read_data_consistency_level", testDataConsistencyNew);
+    }
+  };
+
+  @Before
+  public void setUp() throws Exception {
+    deleteConfigFile();
+    createTestConfigFile();
+    storeOldConfig();
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    restoreOldConfig();
+    deleteConfigFile();
+  }
+
+  @Test
+  public void test() throws IOException {
+    System.out.println(absoultePath);
+    System.setProperty(IoTDBConstant.IOTDB_CONF, absoultePath);
+    ClusterDescriptor.getInstance().loadProps();
+    ClusterConfig config = ClusterDescriptor.getInstance().getConfig();
+    StringBuilder builder = new StringBuilder();
+    for (String node : config.getNodes()) {
+      builder.append(node.trim() + ",");
+    }
+    assertEquals(testNodesNew, builder.toString().substring(0, builder.length() - 1));
+    assertEquals(testReplicationNew, config.getReplication() + "");
+    assertEquals(testIPNew, config.getIp() + "");
+    assertEquals(testPortNew, config.getPort() + "");
+    assertEquals(testRaftLogPathNew, config.getRaftLogPath() + "");
+    assertEquals(testRaftSnapshotPathNew, config.getRaftSnapshotPath() + "");
+    assertEquals(testRaftMetadataPathNew, config.getRaftMetadataPath() + "");
+    assertEquals(testMaxCatchUpLogNumNew, config.getMaxCatchUpLogNum() + "");
+    assertEquals(testDelaySnapshotNew, config.isDelaySnapshot() + "");
+    assertEquals(testDelayHoursNew, config.getDelayHours() + "");
+    assertEquals(testTaskRedoCountNew, config.getTaskRedoCount() + "");
+    assertEquals(testTaskTimeoutMSNew, config.getTaskTimeoutMs() + "");
+    assertEquals(testVNodesNew, config.getNumOfVirtualNodes() + "");
+    assertEquals(testClientNumNew, config.getMaxNumOfInnerRpcClient() + "");
+    assertEquals(testQueueLenNew, config.getMaxQueueNumOfInnerRpcClient() + "");
+    assertEquals(testMetadataConsistencyNew, config.getReadMetadataConsistencyLevel() + "");
+    assertEquals(testDataConsistencyNew, config.getReadDataConsistencyLevel() + "");
+
+    System.setProperty(IoTDBConstant.IOTDB_CONF, "");
+    config.deleteAllPath();
+  }
+
+  private void deleteConfigFile() throws IOException {
+    File f = new File(path + File.separatorChar + ClusterConfig.CONFIG_NAME);
+    try {
+      FileUtils.forceDelete(f);
+    } catch (Exception e) {
+      // TODO: handle exception
+    }
+
+  }
+
+  private void createTestConfigFile() throws IOException {
+    File f = new File(path + File.separatorChar + ClusterConfig.CONFIG_NAME);
+    absoultePath = f.getParentFile().getAbsolutePath();
+    if (f.createNewFile()) {
+      FileWriter writer = new FileWriter(f, false);
+      for (Entry<String, String> entry : testConfigMap.entrySet()) {
+        writer.write(String.format("%s=%s%s", entry.getKey(),
+            entry.getValue(), System.getProperty("line.separator")));
+      }
+      writer.close();
+    } else {
+      fail();
+    }
+  }
+
+  private void storeOldConfig() {
+    ClusterConfig config = ClusterDescriptor.getInstance().getConfig();
+    testNodesOld = config.getNodes();
+    testReplicationOld = config.getReplication();
+    testIPOld = config.getIp();
+    testPortOld = config.getPort();
+    testRaftLogPathOld = config.getRaftLogPath();
+    testRaftSnapshotPathOld = config.getRaftSnapshotPath();
+    testRaftMetadataPathOld = config.getRaftMetadataPath();
+    testMaxCatchUpLogNumOld = config.getMaxCatchUpLogNum();
+    testDelaySnapshotOld = config.isDelaySnapshot();
+    testDelayHoursOld = config.getDelayHours();
+    testTaskRedoCountOld = config.getTaskRedoCount();
+    testTaskTimeoutMSOld = config.getTaskTimeoutMs();
+    testVNodesOld = config.getNumOfVirtualNodes();
+    testClientNumOld = config.getMaxNumOfInnerRpcClient();
+    testQueueLenOld = config.getMaxQueueNumOfInnerRpcClient();
+    testMetadataConsistencyOld = config.getReadMetadataConsistencyLevel();
+    testDataConsistencyOld = config.getReadDataConsistencyLevel();
+  }
+
+  private void restoreOldConfig() {
+    ClusterConfig config = ClusterDescriptor.getInstance().getConfig();
+    config.setNodes(testNodesOld);
+    config.setReplication(testReplicationOld);
+    config.setIp(testIPOld);
+    config.setPort(testPortOld);
+    config.setRaftLogPath(testRaftLogPathOld);
+    config.setRaftMetadataPath(testRaftMetadataPathOld);
+    config.setRaftSnapshotPath(testRaftSnapshotPathOld);
+    config.setMaxCatchUpLogNum(testMaxCatchUpLogNumOld);
+    config.setDelayHours(testDelayHoursOld);
+    config.setDelaySnapshot(testDelaySnapshotOld);
+    config.setTaskRedoCount(testTaskRedoCountOld);
+    config.setTaskTimeoutMs(testTaskTimeoutMSOld);
+    config.setNumOfVirtualNodes(testVNodesOld);
+    config.setMaxNumOfInnerRpcClient(testClientNumOld);
+    config.setMaxQueueNumOfInnerRpcClient(testQueueLenOld);
+    config.setReadMetadataConsistencyLevel(testMetadataConsistencyOld);
+    config.setReadDataConsistencyLevel(testDataConsistencyOld);
+  }
+
+}