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/18 10:20:31 UTC

[incubator-iotdb] 02/05: add NodeTool

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

commit 7dd9ff13ddc029934a34c1f50b8c26921ade7aa8
Author: mdf369 <95...@qq.com>
AuthorDate: Wed Apr 17 11:01:14 2019 +0800

    add NodeTool
---
 .../org/apache/iotdb/cluster/entity/Server.java    |  4 +-
 .../iotdb/cluster/service/ClusterMonitor.java      | 24 ++++++++
 .../iotdb/cluster/service/ClusterMonitorMBean.java |  8 +++
 .../{ClusterMonitorMBean.java => NodeTool.java}    | 23 +++++--
 .../org/apache/iotdb/cluster/utils/RaftUtils.java  |  6 ++
 iotdb/iotdb/bin/nodetool.sh                        | 71 ++++++++++++++++++++++
 6 files changed, 130 insertions(+), 6 deletions(-)

diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/entity/Server.java b/cluster/src/main/java/org/apache/iotdb/cluster/entity/Server.java
index 5fbfadc..a42b201 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/entity/Server.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/entity/Server.java
@@ -85,12 +85,12 @@ public class Server {
 
   private RegisterManager registerManager = new RegisterManager();
 
-  public static void main(String[] args) {
+  public static void main(String[] args) throws ProcessorException {
     Server server = Server.getInstance();
     server.start();
   }
 
-  public void start() {
+  public void start() throws ProcessorException {
     /** Stand-alone version of IoTDB, be careful to replace the internal JDBC Server with a cluster version **/
     iotdb = new IoTDB();
     iotdb.active();
diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/service/ClusterMonitor.java b/cluster/src/main/java/org/apache/iotdb/cluster/service/ClusterMonitor.java
index 2432116..0c43ba6 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/service/ClusterMonitor.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/service/ClusterMonitor.java
@@ -18,7 +18,11 @@
  */
 package org.apache.iotdb.cluster.service;
 
+import com.alipay.sofa.jraft.entity.PeerId;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.SortedMap;
+import org.apache.iotdb.cluster.utils.RaftUtils;
 import org.apache.iotdb.cluster.utils.hash.PhysicalNode;
 import org.apache.iotdb.cluster.utils.hash.Router;
 import org.apache.iotdb.cluster.utils.hash.VirtualNode;
@@ -72,4 +76,24 @@ public class ClusterMonitor implements ClusterMonitorMBean, IService {
   public SortedMap<Integer, VirtualNode> getVirtualRing() {
     return router.getVirtualRing();
   }
+
+  @Override
+  public Map<String, String> getAllLeaders() {
+    Map<String, String> map = new HashMap<>();
+    RaftUtils.getGroupLeaderCache().entrySet().forEach(entry -> map.put(entry.getKey(), entry.getValue().toString()));
+    return map;
+  }
+
+  @Override
+  public Map<String, String[]> getAllGroups() {
+    return null;
+  }
+
+  @Override
+  public String getLeaderOfSG(String sg) {
+    PhysicalNode[] group = router.routeGroup(sg);
+    String groupId = router.getGroupID(group);
+    PeerId leader = RaftUtils.getLeaderPeerID(groupId);
+    return leader.toString();
+  }
 }
diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/service/ClusterMonitorMBean.java b/cluster/src/main/java/org/apache/iotdb/cluster/service/ClusterMonitorMBean.java
index 445157a..6b7b888 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/service/ClusterMonitorMBean.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/service/ClusterMonitorMBean.java
@@ -18,6 +18,8 @@
  */
 package org.apache.iotdb.cluster.service;
 
+import java.util.List;
+import java.util.Map;
 import java.util.SortedMap;
 import org.apache.iotdb.cluster.utils.hash.PhysicalNode;
 import org.apache.iotdb.cluster.utils.hash.VirtualNode;
@@ -27,4 +29,10 @@ public interface ClusterMonitorMBean {
   SortedMap<Integer, PhysicalNode> getPhysicalRing();
 
   SortedMap<Integer, VirtualNode> getVirtualRing();
+
+  Map<String, String> getAllLeaders();
+
+  Map<String, String[]> getAllGroups();
+
+  String getLeaderOfSG(String sg);
 }
diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/service/ClusterMonitorMBean.java b/cluster/src/main/java/org/apache/iotdb/cluster/service/NodeTool.java
similarity index 58%
copy from cluster/src/main/java/org/apache/iotdb/cluster/service/ClusterMonitorMBean.java
copy to cluster/src/main/java/org/apache/iotdb/cluster/service/NodeTool.java
index 445157a..2173a41 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/service/ClusterMonitorMBean.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/service/NodeTool.java
@@ -18,13 +18,28 @@
  */
 package org.apache.iotdb.cluster.service;
 
+import java.util.Map;
 import java.util.SortedMap;
 import org.apache.iotdb.cluster.utils.hash.PhysicalNode;
-import org.apache.iotdb.cluster.utils.hash.VirtualNode;
 
-public interface ClusterMonitorMBean {
+public class NodeTool {
 
-  SortedMap<Integer, PhysicalNode> getPhysicalRing();
+  public static void main(String... args)
+  {
+    ClusterMonitor monitor = ClusterMonitor.INSTANCE;
+    if (args.length == 0) {
+      SortedMap<Integer, PhysicalNode> physicalRing = monitor.getPhysicalRing();
+      physicalRing.entrySet()
+          .forEach(entry -> System.out.println(entry.getValue() + "\t-->\t" + entry.getKey()));
+    } else if ("showleader".equals(args[0])) {
+      if (args.length > 1) {
+        String leader = monitor.getLeaderOfSG(args[1]);
+        System.out.println(leader);
+      } else {
+        Map<String, String> groupIdLeaderMap = monitor.getAllLeaders();
+      }
+    }
 
-  SortedMap<Integer, VirtualNode> getVirtualRing();
+    System.exit(0);
+  }
 }
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 744730f..2b44185 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
@@ -29,7 +29,9 @@ import com.alipay.sofa.jraft.util.Bits;
 import com.alipay.sofa.jraft.util.OnlyForTest;
 
 import java.nio.ByteBuffer;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -302,4 +304,8 @@ public class RaftUtils {
     status.setCode(-1);
     return status;
   }
+
+  public static ConcurrentHashMap<String, PeerId> getGroupLeaderCache() {
+    return groupLeaderCache;
+  }
 }
diff --git a/iotdb/iotdb/bin/nodetool.sh b/iotdb/iotdb/bin/nodetool.sh
new file mode 100755
index 0000000..4a595c2
--- /dev/null
+++ b/iotdb/iotdb/bin/nodetool.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+#
+# 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.
+#
+
+if [ -z "${IOTDB_HOME}" ]; then
+  export IOTDB_HOME="`dirname "$0"`/.."
+fi
+
+IOTDB_CONF=${IOTDB_HOME}/conf
+# IOTDB_LOGS=${IOTDB_HOME}/logs
+
+if [ -f "$IOTDB_CONF/iotdb-env.sh" ]; then
+    . "$IOTDB_CONF/iotdb-env.sh"
+else
+    echo "can't find $IOTDB_CONF/iotdb-env.sh"
+fi
+
+if [ -n "$JAVA_HOME" ]; then
+    for java in "$JAVA_HOME"/bin/amd64/java "$JAVA_HOME"/bin/java; do
+        if [ -x "$java" ]; then
+            JAVA="$java"
+            break
+        fi
+    done
+else
+    JAVA=java
+fi
+
+if [ -z $JAVA ] ; then
+    echo Unable to find java executable. Check JAVA_HOME and PATH environment variables.  > /dev/stderr
+    exit 1;
+fi
+
+CLASSPATH=""
+for f in ${IOTDB_HOME}/lib_cluster/*.jar; do
+  CLASSPATH=${CLASSPATH}":"$f
+done
+classname=org.apache.iotdb.cluster.service.NodeTool
+
+launch_service()
+{
+	class="$1"
+	iotdb_parms="-Dlogback.configurationFile=${IOTDB_CONF}/logback.xml"
+	iotdb_parms="$iotdb_parms -DIOTDB_HOME=${IOTDB_HOME}"
+	iotdb_parms="$iotdb_parms -DTSFILE_HOME=${IOTDB_HOME}"
+	iotdb_parms="$iotdb_parms -DIOTDB_CONF=${IOTDB_CONF}"
+	iotdb_parms="$iotdb_parms -Dname=iotdb\.Cluster"
+	exec "$JAVA" $iotdb_parms $IOTDB_JMX_OPTS $iotdb_parms -cp "$CLASSPATH"  "$class"
+	return $?
+}
+
+# Start up the service
+launch_service "$classname"
+
+exit $?