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:29 UTC

[incubator-iotdb] branch cluster_nodetool created (now d2c2b22)

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

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


      at d2c2b22  add Host

This branch includes the following new commits:

     new 76ece16  add JMXService CLusterMonitor
     new 7dd9ff1  add NodeTool
     new 8888850  add Ring and Leader
     new 78b6ffe  update
     new d2c2b22  add Host

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[incubator-iotdb] 01/05: add JMXService CLusterMonitor

Posted by ea...@apache.org.
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 76ece16ebed5dcd23b60d317a2d09c095e94db44
Author: mdf369 <95...@qq.com>
AuthorDate: Tue Apr 16 15:07:42 2019 +0800

    add JMXService CLusterMonitor
---
 .../org/apache/iotdb/cluster/entity/Server.java    | 16 +++++
 .../iotdb/cluster/service/ClusterMonitor.java      | 75 ++++++++++++++++++++++
 .../iotdb/cluster/service/ClusterMonitorMBean.java | 30 +++++++++
 .../apache/iotdb/cluster/utils/hash/Router.java    |  9 +++
 .../org/apache/iotdb/db/service/ServiceType.java   |  1 +
 5 files changed, 131 insertions(+)

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 a4c02cd..5fbfadc 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
@@ -39,11 +39,15 @@ import org.apache.iotdb.cluster.rpc.raft.processor.QueryMetadataInStringAsyncPro
 import org.apache.iotdb.cluster.rpc.raft.processor.QueryPathsAsyncProcessor;
 import org.apache.iotdb.cluster.rpc.raft.processor.QuerySeriesTypeAsyncProcessor;
 import org.apache.iotdb.cluster.rpc.raft.processor.QueryTimeSeriesAsyncProcessor;
+import org.apache.iotdb.cluster.service.ClusterMonitor;
 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.db.conf.IoTDBConstant;
+import org.apache.iotdb.db.exception.StartupException;
 import org.apache.iotdb.db.exception.ProcessorException;
 import org.apache.iotdb.db.service.IoTDB;
+import org.apache.iotdb.db.service.RegisterManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -79,6 +83,8 @@ public class Server {
    */
   private IoTDB iotdb;
 
+  private RegisterManager registerManager = new RegisterManager();
+
   public static void main(String[] args) {
     Server server = Server.getInstance();
     server.start();
@@ -125,6 +131,14 @@ public class Server {
       Router.getInstance().showPhysicalNodes(groupId);
     }
 
+    try {
+      registerManager.register(ClusterMonitor.INSTANCE);
+    } catch (StartupException e) {
+      LOGGER.error("meet error while starting up.", e);
+      stop();
+      LOGGER.error("{} exit", IoTDBConstant.GLOBAL_DB_NAME);
+      return;
+    }
   }
 
   public void stop() throws ProcessorException {
@@ -135,6 +149,8 @@ public class Server {
     for (DataPartitionHolder dataPartitionHolder : dataPartitionHolderMap.values()) {
       dataPartitionHolder.stop();
     }
+
+    registerManager.deregisterAll();
   }
 
   public PeerId getServerId() {
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
new file mode 100644
index 0000000..2432116
--- /dev/null
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/service/ClusterMonitor.java
@@ -0,0 +1,75 @@
+/**
+ * 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.service;
+
+import java.util.SortedMap;
+import org.apache.iotdb.cluster.utils.hash.PhysicalNode;
+import org.apache.iotdb.cluster.utils.hash.Router;
+import org.apache.iotdb.cluster.utils.hash.VirtualNode;
+import org.apache.iotdb.db.conf.IoTDBConstant;
+import org.apache.iotdb.db.exception.StartupException;
+import org.apache.iotdb.db.service.IService;
+import org.apache.iotdb.db.service.JMXService;
+import org.apache.iotdb.db.service.ServiceType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ClusterMonitor implements ClusterMonitorMBean, IService {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(ClusterMonitor.class);
+
+  public static final ClusterMonitor INSTANCE = new ClusterMonitor();
+  private final String mbeanName = String
+      .format("%s:%s=%s", IoTDBConstant.IOTDB_PACKAGE, IoTDBConstant.JMX_TYPE,
+          getID().getJmxName());
+  private Router router = Router.getInstance();
+
+  @Override
+  public void start() throws StartupException {
+    try {
+      JMXService.registerMBean(INSTANCE, mbeanName);
+    } catch (Exception e) {
+      String errorMessage = String
+          .format("Failed to start %s because of %s", this.getID().getName(),
+              e.getMessage());
+      LOGGER.error(errorMessage);
+      throw new StartupException(errorMessage);
+    }
+  }
+
+  @Override
+  public void stop() {
+    JMXService.deregisterMBean(mbeanName);
+  }
+
+  @Override
+  public ServiceType getID() {
+    return ServiceType.CLUSTER_MONITOR_SERVICE;
+  }
+
+  @Override
+  public SortedMap<Integer, PhysicalNode> getPhysicalRing() {
+    return router.getPhysicalRing();
+  }
+
+  @Override
+  public SortedMap<Integer, VirtualNode> getVirtualRing() {
+    return router.getVirtualRing();
+  }
+}
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
new file mode 100644
index 0000000..445157a
--- /dev/null
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/service/ClusterMonitorMBean.java
@@ -0,0 +1,30 @@
+/**
+ * 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.service;
+
+import java.util.SortedMap;
+import org.apache.iotdb.cluster.utils.hash.PhysicalNode;
+import org.apache.iotdb.cluster.utils.hash.VirtualNode;
+
+public interface ClusterMonitorMBean {
+
+  SortedMap<Integer, PhysicalNode> getPhysicalRing();
+
+  SortedMap<Integer, VirtualNode> getVirtualRing();
+}
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 7c7b2be..2031676 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
@@ -66,6 +66,7 @@ public class Router {
   public static final String DATA_GROUP_STR = "data-group-";
 
   private HashFunction hashFunction = new MD5Hash();
+
   private final SortedMap<Integer, PhysicalNode> physicalRing = new TreeMap<>();
   private final SortedMap<Integer, VirtualNode> virtualRing = new TreeMap<>();
 
@@ -236,4 +237,12 @@ public class Router {
   public Set<String> getAllGroupId() {
     return groupIdMapNodeCache.keySet();
   }
+
+  public SortedMap<Integer, PhysicalNode> getPhysicalRing() {
+    return physicalRing;
+  }
+
+  public SortedMap<Integer, VirtualNode> getVirtualRing() {
+    return virtualRing;
+  }
 }
diff --git a/iotdb/src/main/java/org/apache/iotdb/db/service/ServiceType.java b/iotdb/src/main/java/org/apache/iotdb/db/service/ServiceType.java
index 4b396f2..9bddfd5 100644
--- a/iotdb/src/main/java/org/apache/iotdb/db/service/ServiceType.java
+++ b/iotdb/src/main/java/org/apache/iotdb/db/service/ServiceType.java
@@ -24,6 +24,7 @@ public enum ServiceType {
   JMX_SERVICE("JMX ServerService", "JMX ServerService"),
   JDBC_SERVICE("JDBC ServerService", "JDBCService"),
   MONITOR_SERVICE("Monitor ServerService", "Monitor"),
+  CLUSTER_MONITOR_SERVICE("Cluster Monitor ServerService", "Cluster Monitor"),
   STAT_MONITOR_SERVICE("Statistics ServerService", ""),
   WAL_SERVICE("WAL ServerService", ""),
   CLOSE_MERGE_SERVICE("Close&Merge ServerService", ""),


[incubator-iotdb] 03/05: add Ring and Leader

Posted by ea...@apache.org.
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 88888505dbd5945c211799855422d064d998c24a
Author: mdf369 <95...@qq.com>
AuthorDate: Wed Apr 17 20:27:44 2019 +0800

    add Ring and Leader
---
 .../iotdb/cluster/service/ClusterMonitor.java      |  23 ++--
 .../iotdb/cluster/service/ClusterMonitorMBean.java |   8 +-
 .../org/apache/iotdb/cluster/utils/RaftUtils.java  |  25 +++-
 .../iotdb/cluster/utils/hash/VirtualNode.java      |   7 +-
 {iotdb/iotdb => iotdb-cli/cli}/bin/nodetool.sh     |  43 ++----
 iotdb-cli/pom.xml                                  |  10 ++
 .../java/org/apache/iotdb/cli/service/Leader.java  |  31 +++--
 .../org/apache/iotdb/cli/service/NodeTool.java     | 147 +++++++++++++++++++++
 .../java/org/apache/iotdb/cli/service/Ring.java    |  32 ++---
 9 files changed, 237 insertions(+), 89 deletions(-)

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 0c43ba6..f06cc56 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,14 +18,9 @@
  */
 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;
 import org.apache.iotdb.db.conf.IoTDBConstant;
 import org.apache.iotdb.db.exception.StartupException;
 import org.apache.iotdb.db.service.IService;
@@ -42,7 +37,10 @@ public class ClusterMonitor implements ClusterMonitorMBean, IService {
   private final String mbeanName = String
       .format("%s:%s=%s", IoTDBConstant.IOTDB_PACKAGE, IoTDBConstant.JMX_TYPE,
           getID().getJmxName());
-  private Router router = Router.getInstance();
+
+  public String getMbeanName() {
+    return mbeanName;
+  }
 
   @Override
   public void start() throws StartupException {
@@ -68,13 +66,13 @@ public class ClusterMonitor implements ClusterMonitorMBean, IService {
   }
 
   @Override
-  public SortedMap<Integer, PhysicalNode> getPhysicalRing() {
-    return router.getPhysicalRing();
+  public Map<Integer, String> getPhysicalRing() {
+    return RaftUtils.getPhysicalRing();
   }
 
   @Override
-  public SortedMap<Integer, VirtualNode> getVirtualRing() {
-    return router.getVirtualRing();
+  public Map<Integer, String> getVirtualRing() {
+    return RaftUtils.getVirtualRing();
   }
 
   @Override
@@ -91,9 +89,6 @@ public class ClusterMonitor implements ClusterMonitorMBean, IService {
 
   @Override
   public String getLeaderOfSG(String sg) {
-    PhysicalNode[] group = router.routeGroup(sg);
-    String groupId = router.getGroupID(group);
-    PeerId leader = RaftUtils.getLeaderPeerID(groupId);
-    return leader.toString();
+    return RaftUtils.getLeaderOfSG(sg).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 6b7b888..0914025 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,17 +18,13 @@
  */
 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;
 
 public interface ClusterMonitorMBean {
 
-  SortedMap<Integer, PhysicalNode> getPhysicalRing();
+  Map<Integer, String> getPhysicalRing();
 
-  SortedMap<Integer, VirtualNode> getVirtualRing();
+  Map<Integer, String> getVirtualRing();
 
   Map<String, String> getAllLeaders();
 
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 2b44185..6ec5975 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,9 +29,10 @@ 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.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.SortedMap;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -48,6 +49,7 @@ import org.apache.iotdb.cluster.rpc.raft.response.BasicResponse;
 import org.apache.iotdb.cluster.rpc.raft.response.MetaGroupNonQueryResponse;
 import org.apache.iotdb.cluster.utils.hash.PhysicalNode;
 import org.apache.iotdb.cluster.utils.hash.Router;
+import org.apache.iotdb.cluster.utils.hash.VirtualNode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -308,4 +310,25 @@ public class RaftUtils {
   public static ConcurrentHashMap<String, PeerId> getGroupLeaderCache() {
     return groupLeaderCache;
   }
+
+  public static Map<Integer, String> getPhysicalRing() {
+    SortedMap<Integer, PhysicalNode> hashNodeMap = router.getPhysicalRing();
+    Map<Integer, String> res = new LinkedHashMap<>();
+    hashNodeMap.entrySet().forEach(entry -> res.put(entry.getKey(), entry.getValue().toString()));
+    return res;
+  }
+
+  public static Map<Integer, String> getVirtualRing() {
+    SortedMap<Integer, VirtualNode> hashNodeMap = router.getVirtualRing();
+    Map<Integer, String> res = new LinkedHashMap<>();
+    hashNodeMap.entrySet().forEach(entry -> res.put(entry.getKey(), entry.getValue().getPhysicalNode().toString()));
+    return res;
+  }
+
+  public static PeerId getLeaderOfSG(String sg) {
+    PhysicalNode[] group = router.routeGroup(sg);
+    String groupId = router.getGroupID(group);
+    PeerId leader = RaftUtils.getLeaderPeerID(groupId);
+    return leader;
+  }
 }
diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/utils/hash/VirtualNode.java b/cluster/src/main/java/org/apache/iotdb/cluster/utils/hash/VirtualNode.java
index 88816cf..49ac281 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/utils/hash/VirtualNode.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/utils/hash/VirtualNode.java
@@ -28,11 +28,16 @@ public class VirtualNode {
     this.physicalNode = physicalNode;
   }
 
-  PhysicalNode getPhysicalNode() {
+  public PhysicalNode getPhysicalNode() {
     return this.physicalNode;
   }
 
   String getKey() {
     return String.format("%s-%d", physicalNode.getKey(), replicaIndex);
   }
+
+  @Override
+  public String toString() {
+    return getKey();
+  }
 }
diff --git a/iotdb/iotdb/bin/nodetool.sh b/iotdb-cli/cli/bin/nodetool.sh
similarity index 54%
rename from iotdb/iotdb/bin/nodetool.sh
rename to iotdb-cli/cli/bin/nodetool.sh
index 4a595c2..1a1097d 100755
--- a/iotdb/iotdb/bin/nodetool.sh
+++ b/iotdb-cli/cli/bin/nodetool.sh
@@ -19,17 +19,18 @@
 #
 
 if [ -z "${IOTDB_HOME}" ]; then
-  export IOTDB_HOME="`dirname "$0"`/.."
+  export IOTDB_HOME="$(cd "`dirname "$0"`"/..; pwd)"
 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
+MAIN_CLASS=org.apache.iotdb.cli.service.NodeTool
+
+
+CLASSPATH=""
+for f in ${IOTDB_HOME}/lib/*.jar; do
+  CLASSPATH=${CLASSPATH}":"$f
+done
+
 
 if [ -n "$JAVA_HOME" ]; then
     for java in "$JAVA_HOME"/bin/amd64/java "$JAVA_HOME"/bin/java; do
@@ -42,30 +43,6 @@ 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"
+exec "$JAVA" -cp "$CLASSPATH" "$MAIN_CLASS" "$@"
 
 exit $?
diff --git a/iotdb-cli/pom.xml b/iotdb-cli/pom.xml
index 6977892..8546eb9 100644
--- a/iotdb-cli/pom.xml
+++ b/iotdb-cli/pom.xml
@@ -44,6 +44,11 @@
             <version>0.8.0-SNAPSHOT</version>
         </dependency>
         <dependency>
+            <groupId>org.apache.iotdb</groupId>
+            <artifactId>IoTDB-cluster</artifactId>
+            <version>0.8.0-SNAPSHOT</version>
+        </dependency>
+        <dependency>
             <groupId>commons-cli</groupId>
             <artifactId>commons-cli</artifactId>
             <version>${common.cli.version}</version>
@@ -58,6 +63,11 @@
             <artifactId>jline</artifactId>
             <version>${jline.version}</version>
         </dependency>
+        <dependency>
+            <groupId>io.airlift</groupId>
+            <artifactId>airline</artifactId>
+            <version>0.8</version>
+        </dependency>
     </dependencies>
     <build>
         <plugins>
diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/utils/hash/VirtualNode.java b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Leader.java
similarity index 52%
copy from cluster/src/main/java/org/apache/iotdb/cluster/utils/hash/VirtualNode.java
copy to iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Leader.java
index 88816cf..4ba014e 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/utils/hash/VirtualNode.java
+++ b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Leader.java
@@ -16,23 +16,26 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.iotdb.cluster.utils.hash;
+package org.apache.iotdb.cli.service;
 
-public class VirtualNode {
-  //the index of the virtual node in the physicalNode
-  private final int replicaIndex;
-  private final PhysicalNode physicalNode;
+import io.airlift.airline.Arguments;
+import io.airlift.airline.Command;
+import org.apache.iotdb.cli.service.NodeTool.NodeToolCmd;
+import org.apache.iotdb.cluster.config.ClusterConfig;
+import org.apache.iotdb.cluster.service.ClusterMonitorMBean;
 
-  VirtualNode(int replicaIndex, PhysicalNode physicalNode) {
-    this.replicaIndex = replicaIndex;
-    this.physicalNode = physicalNode;
-  }
+@Command(name = "leader", description = "Print leader host information of specific storage group")
+public class Leader extends NodeToolCmd {
 
-  PhysicalNode getPhysicalNode() {
-    return this.physicalNode;
-  }
+  @Arguments(description = "Specify a storage group for accurate leader information")
+  private String sg = null;
 
-  String getKey() {
-    return String.format("%s-%d", physicalNode.getKey(), replicaIndex);
+  @Override
+  public void execute(ClusterMonitorMBean proxy) {
+    if (sg == null) {
+      sg = ClusterConfig.METADATA_GROUP_ID;
+    }
+    String leader = proxy.getLeaderOfSG(sg);
+    System.out.println(leader);
   }
 }
diff --git a/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/NodeTool.java b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/NodeTool.java
new file mode 100644
index 0000000..7043269
--- /dev/null
+++ b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/NodeTool.java
@@ -0,0 +1,147 @@
+/**
+ * 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.cli.service;
+
+import static com.google.common.base.Throwables.getStackTraceAsString;
+import static com.google.common.collect.Lists.newArrayList;
+import static java.lang.String.format;
+
+import com.google.common.base.Throwables;
+import io.airlift.airline.Cli;
+import io.airlift.airline.Help;
+import io.airlift.airline.Option;
+import io.airlift.airline.OptionType;
+import io.airlift.airline.ParseArgumentsMissingException;
+import io.airlift.airline.ParseArgumentsUnexpectedException;
+import io.airlift.airline.ParseCommandMissingException;
+import io.airlift.airline.ParseCommandUnrecognizedException;
+import io.airlift.airline.ParseOptionConversionException;
+import io.airlift.airline.ParseOptionMissingException;
+import io.airlift.airline.ParseOptionMissingValueException;
+import java.io.IOException;
+import java.util.List;
+import javax.management.JMX;
+import javax.management.MBeanServerConnection;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import javax.management.remote.JMXConnector;
+import javax.management.remote.JMXConnectorFactory;
+import javax.management.remote.JMXServiceURL;
+import org.apache.iotdb.cluster.service.ClusterMonitor;
+import org.apache.iotdb.cluster.service.ClusterMonitorMBean;
+
+public class NodeTool {
+
+  private static ClusterMonitor clusterMonitor = ClusterMonitor.INSTANCE;
+
+  public static void main(String... args) {
+    List<Class<? extends Runnable>> commands = newArrayList(
+        Help.class,
+        Ring.class,
+        Leader.class
+    );
+
+    Cli.CliBuilder<Runnable> builder = Cli.builder("nodetool");
+
+    builder.withDescription("Manage your IoTDB cluster")
+        .withDefaultCommand(Help.class)
+        .withCommands(commands);
+
+    Cli<Runnable> parser = builder.build();
+
+    int status = 0;
+    try {
+      Runnable parse = parser.parse(args);
+      parse.run();
+    } catch (IllegalArgumentException |
+        IllegalStateException |
+        ParseArgumentsMissingException |
+        ParseArgumentsUnexpectedException |
+        ParseOptionConversionException |
+        ParseOptionMissingException |
+        ParseOptionMissingValueException |
+        ParseCommandMissingException |
+        ParseCommandUnrecognizedException e) {
+      badUse(e);
+      status = 1;
+    } catch (Throwable throwable) {
+      err(Throwables.getRootCause(throwable));
+      status = 2;
+    }
+
+    System.exit(status);
+  }
+
+  private static void badUse(Exception e) {
+    System.out.println("nodetool: " + e.getMessage());
+    System.out.println("See 'nodetool help' or 'nodetool help <command>'.");
+  }
+
+  private static void err(Throwable e) {
+    System.err.println("error: " + e.getMessage());
+    System.err.println("-- StackTrace --");
+    System.err.println(getStackTraceAsString(e));
+  }
+
+  public static abstract class NodeToolCmd implements Runnable {
+
+    @Option(type = OptionType.GLOBAL, name = {"-h",
+        "--host"}, description = "Node hostname or ip address")
+    private String host = "127.0.0.1";
+
+    @Option(type = OptionType.GLOBAL, name = {"-p",
+        "--port"}, description = "Remote jmx agent port number")
+    private String port = "31999";
+
+    private final String JMX_URL_FORMAT = "service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi";
+
+    @Override
+    public void run() {
+      try {
+        MBeanServerConnection mbsc = connect();
+        ObjectName name = new ObjectName(clusterMonitor.getMbeanName());
+        ClusterMonitorMBean clusterMonitorProxy = JMX
+            .newMBeanProxy(mbsc, name, ClusterMonitorMBean.class);
+        execute(clusterMonitorProxy);
+      } catch (MalformedObjectNameException e) {
+        e.printStackTrace();
+      }
+    }
+
+    protected abstract void execute(ClusterMonitorMBean probe);
+
+    private MBeanServerConnection connect() {
+      MBeanServerConnection mbsc = null;
+
+      try {
+        String jmxURL = String.format(JMX_URL_FORMAT, host, port);
+        JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);
+        JMXConnector connector = JMXConnectorFactory.connect(serviceURL);
+        mbsc = connector.getMBeanServerConnection();
+      } catch (IOException e) {
+        Throwable rootCause = Throwables.getRootCause(e);
+        System.err.println(format("nodetool: Failed to connect to '%s:%s' - %s: '%s'.", host, port,
+            rootCause.getClass().getSimpleName(), rootCause.getMessage()));
+        System.exit(1);
+      }
+
+      return mbsc;
+    }
+  }
+}
diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/service/NodeTool.java b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Ring.java
similarity index 52%
rename from cluster/src/main/java/org/apache/iotdb/cluster/service/NodeTool.java
rename to iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Ring.java
index 2173a41..cc34c85 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/service/NodeTool.java
+++ b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Ring.java
@@ -16,30 +16,22 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.iotdb.cluster.service;
+package org.apache.iotdb.cli.service;
 
+import io.airlift.airline.Command;
 import java.util.Map;
-import java.util.SortedMap;
-import org.apache.iotdb.cluster.utils.hash.PhysicalNode;
+import org.apache.iotdb.cli.service.NodeTool.NodeToolCmd;
+import org.apache.iotdb.cluster.service.ClusterMonitorMBean;
 
-public class NodeTool {
+@Command(name = "ring", description = "Print information about the hash ring")
+public class Ring extends NodeToolCmd
+{
 
-  public static void main(String... args)
+  @Override
+  public void execute(ClusterMonitorMBean proxy)
   {
-    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();
-      }
-    }
-
-    System.exit(0);
+    Map<Integer, String> map = proxy.getVirtualRing();
+    map.entrySet().forEach(entry -> System.out.println(entry.getKey() + "\t->\t" + entry.getValue()));
   }
 }
+


[incubator-iotdb] 05/05: add Host

Posted by ea...@apache.org.
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 d2c2b220dad1c85dbe4aaceced24781215611788
Author: mdf369 <95...@qq.com>
AuthorDate: Thu Apr 18 18:20:09 2019 +0800

    add Host
---
 .../cluster/entity/raft/MetadataStateManchine.java |  2 +-
 .../iotdb/cluster/entity/raft/RaftService.java     |  3 +
 .../cluster/qp/executor/QueryMetadataExecutor.java | 16 ++---
 .../iotdb/cluster/service/ClusterMonitor.java      | 10 +++
 .../iotdb/cluster/service/ClusterMonitorMBean.java |  4 ++
 .../org/apache/iotdb/cluster/utils/RaftUtils.java  | 43 ++++++++++++
 .../apache/iotdb/cluster/utils/hash/Router.java    |  2 +
 .../java/org/apache/iotdb/cli/service/Host.java    | 76 ++++++++++++++++++++++
 .../org/apache/iotdb/cli/service/NodeTool.java     |  3 +-
 .../cli/service/{Leader.java => StorageGroup.java} |  6 +-
 .../java/org/apache/iotdb/db/metadata/MGraph.java  |  2 +-
 .../org/apache/iotdb/db/metadata/MManager.java     |  2 +-
 12 files changed, 150 insertions(+), 19 deletions(-)

diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/entity/raft/MetadataStateManchine.java b/cluster/src/main/java/org/apache/iotdb/cluster/entity/raft/MetadataStateManchine.java
index 9592718..5b4a9e6 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/entity/raft/MetadataStateManchine.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/entity/raft/MetadataStateManchine.java
@@ -148,7 +148,7 @@ public class MetadataStateManchine extends StateMachineAdapter {
     mManager.setStorageLevelToMTree(sg);
   }
 
-  public Set<String> getAllStorageGroups() throws PathErrorException {
+  public Set<String> getAllStorageGroups() {
     return mManager.getAllStorageGroup();
   }
 
diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/entity/raft/RaftService.java b/cluster/src/main/java/org/apache/iotdb/cluster/entity/raft/RaftService.java
index 1d08f09..7ef54bf 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/entity/raft/RaftService.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/entity/raft/RaftService.java
@@ -93,4 +93,7 @@ public class RaftService implements IService {
     this.node = node;
   }
 
+  public StateMachine getFsm() {
+    return fsm;
+  }
 }
diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/qp/executor/QueryMetadataExecutor.java b/cluster/src/main/java/org/apache/iotdb/cluster/qp/executor/QueryMetadataExecutor.java
index 1dfbc7e..d003181 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/qp/executor/QueryMetadataExecutor.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/qp/executor/QueryMetadataExecutor.java
@@ -331,12 +331,8 @@ public class QueryMetadataExecutor extends ClusterQPExecutor {
     MetadataRaftHolder metadataHolder = (MetadataRaftHolder) server.getMetadataHolder();
     if (readMetadataConsistencyLevel == ClusterConstant.WEAK_CONSISTENCY_LEVEL) {
       QueryStorageGroupResponse response;
-      try {
-        response = QueryStorageGroupResponse
-            .createSuccessResponse(metadataHolder.getFsm().getAllStorageGroups());
-      } catch (final PathErrorException e) {
-        response = QueryStorageGroupResponse.createErrorResponse(e.getMessage());
-      }
+      response = QueryStorageGroupResponse
+          .createSuccessResponse(metadataHolder.getFsm().getAllStorageGroups());
       task.run(response);
     } else {
       ((RaftService) metadataHolder.getService()).getNode()
@@ -346,12 +342,8 @@ public class QueryMetadataExecutor extends ClusterQPExecutor {
             public void run(Status status, long index, byte[] reqCtx) {
               QueryStorageGroupResponse response;
               if (status.isOk()) {
-                try {
-                  response = QueryStorageGroupResponse
-                      .createSuccessResponse(metadataHolder.getFsm().getAllStorageGroups());
-                } catch (final PathErrorException e) {
-                  response = QueryStorageGroupResponse.createErrorResponse(e.getMessage());
-                }
+                response = QueryStorageGroupResponse
+                    .createSuccessResponse(metadataHolder.getFsm().getAllStorageGroups());
               } else {
                 response = QueryStorageGroupResponse.createErrorResponse(status.getErrorMsg());
               }
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 f17beeb..f73306b 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
@@ -98,4 +98,14 @@ public class ClusterMonitor implements ClusterMonitorMBean, IService {
     }
     return builder.toString();
   }
+
+  @Override
+  public Map<String[], String[]> getDataPartitonOfNode(String ip) {
+    return RaftUtils.getDataPartitionOfNode(ip);
+  }
+
+  @Override
+  public Map<String[], String[]> getDataPartitonOfNode(String ip, int port) {
+    return RaftUtils.getDataPartitionOfNode(ip, port);
+  }
 }
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 1f3dea7..65d4ae3 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,7 @@
  */
 package org.apache.iotdb.cluster.service;
 
+import java.util.List;
 import java.util.Map;
 
 public interface ClusterMonitorMBean {
@@ -31,4 +32,7 @@ public interface ClusterMonitorMBean {
   Map<String, String[]> getAllGroups();
 
   String getDataPartitionOfSG(String sg);
+
+  Map<String[], String[]> getDataPartitonOfNode(String ip);
+  Map<String[], String[]> getDataPartitonOfNode(String ip, int port);
 }
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 978d06c..1b95ad5 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,13 +29,19 @@ import com.alipay.sofa.jraft.util.Bits;
 import com.alipay.sofa.jraft.util.OnlyForTest;
 
 import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
 import java.util.SortedMap;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.iotdb.cluster.config.ClusterDescriptor;
+import org.apache.iotdb.cluster.entity.raft.MetadataStateManchine;
 import org.apache.iotdb.cluster.qp.callback.QPTask;
 import org.apache.iotdb.cluster.qp.callback.SingleQPTask;
 import org.apache.iotdb.cluster.config.ClusterConfig;
@@ -50,6 +56,7 @@ import org.apache.iotdb.cluster.rpc.raft.response.MetaGroupNonQueryResponse;
 import org.apache.iotdb.cluster.utils.hash.PhysicalNode;
 import org.apache.iotdb.cluster.utils.hash.Router;
 import org.apache.iotdb.cluster.utils.hash.VirtualNode;
+import org.apache.iotdb.db.exception.PathErrorException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -59,6 +66,7 @@ public class RaftUtils {
   private static final Server server = Server.getInstance();
   private static final Router router = Router.getInstance();
   private static final AtomicInteger requestId = new AtomicInteger(0);
+  private static final ClusterConfig config = ClusterDescriptor.getInstance().getConfig();
 
   /**
    * The cache will be update in two case: 1. When @onLeaderStart() method of state machine is
@@ -354,4 +362,39 @@ public class RaftUtils {
     }
     return nodes;
   }
+
+  public static Map<String[], String[]> getDataPartitionOfNode(String ip) {
+    return getDataPartitionOfNode(ip, config.getPort());
+  }
+
+  public static Map<String[], String[]> getDataPartitionOfNode(String ip, int port) {
+    PhysicalNode[][] groups = router.getGroupsNodes(ip, port);
+
+    Map<PhysicalNode[], List<String>> groupSGMap = new LinkedHashMap<>();
+    for (int i = 0; i < groups.length; i++) {
+      groupSGMap.put(groups[i], new ArrayList<>());
+    }
+    Set<String> allSGList = ((MetadataStateManchine)((RaftService)server.getMetadataHolder().getService()).getFsm()).getAllStorageGroups();
+    for (String sg : allSGList) {
+      PhysicalNode[] tempGroup = router.routeGroup(sg);
+      if (groupSGMap.containsKey(tempGroup)) {
+        groupSGMap.get(tempGroup).add(sg);
+      }
+    }
+
+    String[][] groupIps = new String[groups.length][];
+    for (int i = 0; i < groups.length; i++) {
+      groupIps[i] = new String[groups[i].length];
+      for (int j = 0; j < groups[i].length; j++) {
+        groupIps[i][j] = groups[i][j].ip;
+      }
+    }
+
+    Map<String[], String[]> res = new HashMap<>();
+    int index = 0;
+    for (Entry<PhysicalNode[], List<String>> entry : groupSGMap.entrySet()) {
+      res.put(groupIps[index], (String[]) entry.getValue().toArray());
+    }
+    return res;
+  }
 }
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..e7fac4f 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
@@ -19,8 +19,10 @@
 package org.apache.iotdb.cluster.utils.hash;
 
 import com.alipay.sofa.jraft.util.OnlyForTest;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
diff --git a/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Host.java b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Host.java
new file mode 100644
index 0000000..2093f21
--- /dev/null
+++ b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Host.java
@@ -0,0 +1,76 @@
+/**
+ * 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.cli.service;
+
+import io.airlift.airline.Command;
+import io.airlift.airline.Option;
+import java.util.Map;
+import java.util.Map.Entry;
+import org.apache.iotdb.cli.service.NodeTool.NodeToolCmd;
+import org.apache.iotdb.cluster.config.ClusterConfig;
+import org.apache.iotdb.cluster.service.ClusterMonitorMBean;
+
+@Command(name = "host", description = "Print all data partitions information which specific host belongs to")
+public class Host extends NodeToolCmd {
+
+  @Option(title = "ip", name = {"-i", "--ip"}, required = true, description = "Specify the host ip for accurate hosts information")
+  private String ip = null;
+
+  @Option(title = "port", name = {"-p", "--port"}, description = "Specify the host port for accurate hosts information")
+  private int port = -1;
+
+  @Option(title = "sg_detail", name = {"-d", "--detail"}, description = "Show path of storage groups")
+  private boolean detail = false;
+
+  @Override
+  public void execute(ClusterMonitorMBean proxy) {
+    Map<String[], String[]> map;
+    if (port == -1) {
+      map = proxy.getDataPartitonOfNode(ip);
+    } else {
+      map = proxy.getDataPartitonOfNode(ip, port);
+    }
+
+    for (Entry<String[], String[]> entry : map.entrySet()) {
+      StringBuilder builder = new StringBuilder();
+      String[] ips = entry.getKey();
+      String[] sgs = entry.getValue();
+      builder.append('(');
+      for (int i = 0; i < ips.length; i++) {
+        builder.append(ips[i]).append(", ");
+      }
+      builder.delete(builder.length() - 2, builder.length());
+      builder.append(')');
+
+      builder.append("\t->\t");
+      if (detail) {
+        builder.append('(');
+        for (int i = 0; i < sgs.length; i++) {
+          builder.append(sgs[i]).append(", ");
+        }
+        builder.delete(builder.length() - 2, builder.length());
+        builder.append(')');
+      } else {
+        builder.append(sgs.length);
+      }
+
+      System.out.println(builder.toString());
+    }
+  }
+}
diff --git a/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/NodeTool.java b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/NodeTool.java
index 7043269..58b79f2 100644
--- a/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/NodeTool.java
+++ b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/NodeTool.java
@@ -54,7 +54,8 @@ public class NodeTool {
     List<Class<? extends Runnable>> commands = newArrayList(
         Help.class,
         Ring.class,
-        Leader.class
+        StorageGroup.class,
+        Host.class
     );
 
     Cli.CliBuilder<Runnable> builder = Cli.builder("nodetool");
diff --git a/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Leader.java b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/StorageGroup.java
similarity index 89%
rename from iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Leader.java
rename to iotdb-cli/src/main/java/org/apache/iotdb/cli/service/StorageGroup.java
index 897bfa2..1c112e3 100644
--- a/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Leader.java
+++ b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/StorageGroup.java
@@ -24,10 +24,10 @@ import org.apache.iotdb.cli.service.NodeTool.NodeToolCmd;
 import org.apache.iotdb.cluster.config.ClusterConfig;
 import org.apache.iotdb.cluster.service.ClusterMonitorMBean;
 
-@Command(name = "leader", description = "Print leader host information of specific storage group")
-public class Leader extends NodeToolCmd {
+@Command(name = "storagegroup", description = "Print all hosts information of specific storage group")
+public class StorageGroup extends NodeToolCmd {
 
-  @Arguments(description = "Specify a storage group for accurate leader information")
+  @Arguments(description = "Specify a storage group for accurate hosts information")
   private String sg = null;
 
   @Override
diff --git a/iotdb/src/main/java/org/apache/iotdb/db/metadata/MGraph.java b/iotdb/src/main/java/org/apache/iotdb/db/metadata/MGraph.java
index c3a5f5a..26e5109 100644
--- a/iotdb/src/main/java/org/apache/iotdb/db/metadata/MGraph.java
+++ b/iotdb/src/main/java/org/apache/iotdb/db/metadata/MGraph.java
@@ -244,7 +244,7 @@ public class MGraph implements Serializable {
     return new Metadata(seriesMap, deviceIdMap);
   }
 
-  public HashSet<String> getAllStorageGroup() throws PathErrorException {
+  public HashSet<String> getAllStorageGroup() {
     return mtree.getAllStorageGroup();
   }
 
diff --git a/iotdb/src/main/java/org/apache/iotdb/db/metadata/MManager.java b/iotdb/src/main/java/org/apache/iotdb/db/metadata/MManager.java
index 5a752f8..af768fd 100644
--- a/iotdb/src/main/java/org/apache/iotdb/db/metadata/MManager.java
+++ b/iotdb/src/main/java/org/apache/iotdb/db/metadata/MManager.java
@@ -542,7 +542,7 @@ public class MManager {
    *
    * @return A HashSet instance which stores all storage group info
    */
-  public Set<String> getAllStorageGroup() throws PathErrorException {
+  public Set<String> getAllStorageGroup() {
 
     lock.readLock().lock();
     try {


[incubator-iotdb] 02/05: add NodeTool

Posted by ea...@apache.org.
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 $?


[incubator-iotdb] 04/05: update

Posted by ea...@apache.org.
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 78b6ffe813088e3454ef72043fbab1e27f949809
Author: mdf369 <95...@qq.com>
AuthorDate: Thu Apr 18 15:16:29 2019 +0800

    update
---
 .../iotdb/cluster/service/ClusterMonitor.java      | 11 +++++--
 .../iotdb/cluster/service/ClusterMonitorMBean.java |  2 +-
 .../org/apache/iotdb/cluster/utils/RaftUtils.java  | 35 ++++++++++++++++++----
 .../java/org/apache/iotdb/cli/service/Leader.java  |  4 +--
 .../java/org/apache/iotdb/cli/service/Ring.java    | 10 ++++++-
 5 files changed, 50 insertions(+), 12 deletions(-)

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 f06cc56..f17beeb 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,6 +18,7 @@
  */
 package org.apache.iotdb.cluster.service;
 
+import com.alipay.sofa.jraft.entity.PeerId;
 import java.util.HashMap;
 import java.util.Map;
 import org.apache.iotdb.cluster.utils.RaftUtils;
@@ -88,7 +89,13 @@ public class ClusterMonitor implements ClusterMonitorMBean, IService {
   }
 
   @Override
-  public String getLeaderOfSG(String sg) {
-    return RaftUtils.getLeaderOfSG(sg).toString();
+  public String getDataPartitionOfSG(String sg) {
+    PeerId[] nodes = RaftUtils.getDataPartitionOfSG(sg);
+    StringBuilder builder = new StringBuilder();
+    builder.append(nodes[0]).append(" (leader)");
+    for (int i = 1; i < nodes.length; i++) {
+      builder.append(", ").append(nodes[i].getIp());
+    }
+    return builder.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 0914025..1f3dea7 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
@@ -30,5 +30,5 @@ public interface ClusterMonitorMBean {
 
   Map<String, String[]> getAllGroups();
 
-  String getLeaderOfSG(String sg);
+  String getDataPartitionOfSG(String sg);
 }
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 6ec5975..978d06c 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
@@ -314,21 +314,44 @@ public class RaftUtils {
   public static Map<Integer, String> getPhysicalRing() {
     SortedMap<Integer, PhysicalNode> hashNodeMap = router.getPhysicalRing();
     Map<Integer, String> res = new LinkedHashMap<>();
-    hashNodeMap.entrySet().forEach(entry -> res.put(entry.getKey(), entry.getValue().toString()));
+    hashNodeMap.entrySet().forEach(entry -> res.put(entry.getKey(), entry.getValue().getIp()));
     return res;
   }
 
   public static Map<Integer, String> getVirtualRing() {
     SortedMap<Integer, VirtualNode> hashNodeMap = router.getVirtualRing();
     Map<Integer, String> res = new LinkedHashMap<>();
-    hashNodeMap.entrySet().forEach(entry -> res.put(entry.getKey(), entry.getValue().getPhysicalNode().toString()));
+    hashNodeMap.entrySet().forEach(entry -> res.put(entry.getKey(), entry.getValue().getPhysicalNode().getIp()));
     return res;
   }
 
-  public static PeerId getLeaderOfSG(String sg) {
-    PhysicalNode[] group = router.routeGroup(sg);
-    String groupId = router.getGroupID(group);
+  /**
+   * Get all node information of the data group of input storage group.
+   * The first node is the current leader
+   *
+   * @param sg storage group ID. If null, return metadata group info
+   */
+  public static PeerId[] getDataPartitionOfSG(String sg) {
+    String groupId;
+    PeerId[] nodes;
+    if (sg == null) {
+      groupId = ClusterConfig.METADATA_GROUP_ID;
+      nodes = (PeerId[]) ((RaftService) server.getMetadataHolder().getService()).getPeerIdList()
+          .toArray();
+    } else {
+      PhysicalNode[] group = router.routeGroup(sg);
+      groupId = router.getGroupID(group);
+      nodes = getPeerIdArrayFrom(group);
+    }
     PeerId leader = RaftUtils.getLeaderPeerID(groupId);
-    return leader;
+    for (int i = 0; i < nodes.length; i++) {
+      if (leader.equals(nodes[i])) {
+        PeerId t = nodes[i];
+        nodes[i] = nodes[0];
+        nodes[0] = t;
+        break;
+      }
+    }
+    return nodes;
   }
 }
diff --git a/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Leader.java b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Leader.java
index 4ba014e..897bfa2 100644
--- a/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Leader.java
+++ b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Leader.java
@@ -35,7 +35,7 @@ public class Leader extends NodeToolCmd {
     if (sg == null) {
       sg = ClusterConfig.METADATA_GROUP_ID;
     }
-    String leader = proxy.getLeaderOfSG(sg);
-    System.out.println(leader);
+    String nodes = proxy.getDataPartitionOfSG(sg);
+    System.out.println(nodes);
   }
 }
diff --git a/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Ring.java b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Ring.java
index cc34c85..5c34ba7 100644
--- a/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Ring.java
+++ b/iotdb-cli/src/main/java/org/apache/iotdb/cli/service/Ring.java
@@ -19,6 +19,7 @@
 package org.apache.iotdb.cli.service;
 
 import io.airlift.airline.Command;
+import io.airlift.airline.Option;
 import java.util.Map;
 import org.apache.iotdb.cli.service.NodeTool.NodeToolCmd;
 import org.apache.iotdb.cluster.service.ClusterMonitorMBean;
@@ -26,11 +27,18 @@ import org.apache.iotdb.cluster.service.ClusterMonitorMBean;
 @Command(name = "ring", description = "Print information about the hash ring")
 public class Ring extends NodeToolCmd
 {
+  @Option(title = "physical_ring", name = {"-p", "--physical"}, description = "Show physical nodes instead of virtual ones")
+  private boolean physical = false;
 
   @Override
   public void execute(ClusterMonitorMBean proxy)
   {
-    Map<Integer, String> map = proxy.getVirtualRing();
+    Map<Integer, String> map;
+    if (physical) {
+      map = proxy.getPhysicalRing();
+    } else {
+      map = proxy.getVirtualRing();
+    }
     map.entrySet().forEach(entry -> System.out.println(entry.getKey() + "\t->\t" + entry.getValue()));
   }
 }