You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ta...@apache.org on 2021/11/25 10:06:01 UTC

[iotdb] branch xianyi updated: support show cluster status && select count( redirect in cli

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

tanxinyu pushed a commit to branch xianyi
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/xianyi by this push:
     new 9b511b9  support show cluster status && select count( redirect in cli
9b511b9 is described below

commit 9b511b9cdfb621c3370be6bccb9d1b446ebc0f50
Author: LebronAl <TX...@gmail.com>
AuthorDate: Thu Nov 25 18:05:21 2021 +0800

    support show cluster status && select count( redirect in cli
---
 .../java/org/apache/iotdb/cli/AbstractCli.java     | 69 ++++++++++++++++++++++
 cli/src/main/java/org/apache/iotdb/cli/Cli.java    |  1 +
 .../org/apache/iotdb/jdbc/IoTDBConnection.java     | 11 ++++
 3 files changed, 81 insertions(+)

diff --git a/cli/src/main/java/org/apache/iotdb/cli/AbstractCli.java b/cli/src/main/java/org/apache/iotdb/cli/AbstractCli.java
index b6f339a..74f1ae2 100644
--- a/cli/src/main/java/org/apache/iotdb/cli/AbstractCli.java
+++ b/cli/src/main/java/org/apache/iotdb/cli/AbstractCli.java
@@ -20,10 +20,12 @@ package org.apache.iotdb.cli;
 
 import org.apache.iotdb.exception.ArgsErrorException;
 import org.apache.iotdb.jdbc.AbstractIoTDBJDBCResultSet;
+import org.apache.iotdb.jdbc.Config;
 import org.apache.iotdb.jdbc.IoTDBConnection;
 import org.apache.iotdb.jdbc.IoTDBJDBCResultSet;
 import org.apache.iotdb.rpc.IoTDBConnectionException;
 import org.apache.iotdb.rpc.RpcUtils;
+import org.apache.iotdb.service.rpc.thrift.EndPoint;
 import org.apache.iotdb.service.rpc.thrift.ServerProperties;
 import org.apache.iotdb.tool.ImportCsv;
 
@@ -35,6 +37,7 @@ import org.apache.commons.lang3.ArrayUtils;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.sql.DriverManager;
 import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
@@ -42,6 +45,7 @@ import java.sql.Statement;
 import java.time.ZoneId;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -83,6 +87,8 @@ public abstract class AbstractCli {
   static final String SHOW_TIMESTAMP_DISPLAY = "show time_display_type";
   static final String SET_TIME_ZONE = "set time_zone";
   static final String SHOW_TIMEZONE = "show time_zone";
+  static final String SHOW_CLUSTER_STATUS = "show cluster status";
+  static final String SELECT_COUNT = "select count(";
   static final String SET_FETCH_SIZE = "set fetch_size";
   static final String SHOW_FETCH_SIZE = "show fetch_size";
   private static final String HELP = "help";
@@ -112,6 +118,8 @@ public abstract class AbstractCli {
   static String execute;
   static boolean hasExecuteSQL = false;
 
+  static List<EndPoint> nodeList = Collections.emptyList();
+
   static Set<String> keywordSet = new HashSet<>();
 
   static ServerProperties properties = null;
@@ -332,6 +340,11 @@ public abstract class AbstractCli {
       return OperationResult.CONTINUE_OPER;
     }
 
+    if (specialCmd.startsWith(SHOW_CLUSTER_STATUS)) {
+      showClusterStatus();
+      return OperationResult.CONTINUE_OPER;
+    }
+
     if (specialCmd.startsWith(SET_TIME_ZONE)) {
       setTimeZone(specialCmd, cmd, connection);
       return OperationResult.CONTINUE_OPER;
@@ -365,6 +378,30 @@ public abstract class AbstractCli {
       return OperationResult.CONTINUE_OPER;
     }
 
+    if (specialCmd.startsWith(SELECT_COUNT) && nodeList.size() != 0) {
+      String[] paths = specialCmd.split("\\.");
+      String deviceId = paths[paths.length - 1];
+      EndPoint endpoint = nodeList.get(deviceId.hashCode() % nodeList.size());
+      if (endpoint.port == Integer.parseInt(port)) {
+        executeQuery(connection, cmd);
+      } else {
+        try (IoTDBConnection nextConnection =
+            (IoTDBConnection)
+                DriverManager.getConnection(
+                    Config.IOTDB_URL_PREFIX + endpoint.ip + ":" + endpoint.port + "/",
+                    username,
+                    password)) {
+          executeQuery(nextConnection, cmd);
+        } catch (SQLException e) {
+          println(
+              String.format(
+                  "%s> %s Host is %s, port is %s.",
+                  IOTDB_CLI_PREFIX, e.getMessage(), endpoint.getIp(), endpoint.getPort()));
+        }
+      }
+      return OperationResult.NO_OPER;
+    }
+
     executeQuery(connection, cmd);
     return OperationResult.NO_OPER;
   }
@@ -390,6 +427,38 @@ public abstract class AbstractCli {
             SET_MAX_DISPLAY_NUM));
   }
 
+  private static void showClusterStatus() {
+    List<Integer> maxSizeList =
+        new ArrayList<Integer>() {
+          {
+            add(20);
+            add(10);
+          }
+        };
+    List<List<String>> lists = new ArrayList<>();
+    List<String> node =
+        new ArrayList<String>() {
+          {
+            add("node");
+          }
+        };
+    List<String> status =
+        new ArrayList<String>() {
+          {
+            add("status");
+          }
+        };
+    for (int i = 0; i < nodeList.size(); i++) {
+      node.add(String.format("%s:%s", nodeList.get(i).getIp(), nodeList.get(i).getPort()));
+      status.add("on");
+    }
+
+    lists.add(node);
+    lists.add(status);
+
+    output(lists, maxSizeList);
+  }
+
   private static void setTimestampDisplay(String specialCmd, String cmd) {
     String[] values = specialCmd.split("=");
     if (values.length != 2) {
diff --git a/cli/src/main/java/org/apache/iotdb/cli/Cli.java b/cli/src/main/java/org/apache/iotdb/cli/Cli.java
index 6e57d27..40b7867 100644
--- a/cli/src/main/java/org/apache/iotdb/cli/Cli.java
+++ b/cli/src/main/java/org/apache/iotdb/cli/Cli.java
@@ -150,6 +150,7 @@ public class Cli extends AbstractCli {
                 Config.IOTDB_URL_PREFIX + host + ":" + port + "/", username, password)) {
       String s;
       properties = connection.getServerProperties();
+      nodeList = connection.getNodeList();
       AGGREGRATE_TIME_LIST.addAll(properties.getSupportedTimeAggregationOperations());
       timestampPrecision = properties.getTimestampPrecision();
 
diff --git a/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java b/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java
index b9a0eab..19e6d8f 100644
--- a/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java
+++ b/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java
@@ -21,6 +21,7 @@ package org.apache.iotdb.jdbc;
 import org.apache.iotdb.rpc.RpcTransportFactory;
 import org.apache.iotdb.rpc.RpcUtils;
 import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.service.rpc.thrift.EndPoint;
 import org.apache.iotdb.service.rpc.thrift.ServerProperties;
 import org.apache.iotdb.service.rpc.thrift.TSCloseSessionReq;
 import org.apache.iotdb.service.rpc.thrift.TSIService;
@@ -55,6 +56,8 @@ import java.sql.Savepoint;
 import java.sql.Statement;
 import java.sql.Struct;
 import java.time.ZoneId;
+import java.util.Collections;
+import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.concurrent.Executor;
@@ -72,6 +75,7 @@ public class IoTDBConnection implements Connection {
   private boolean isClosed = true;
   private SQLWarning warningChain = null;
   private TTransport transport;
+  private List<EndPoint> nodeList = Collections.emptyList();
   /**
    * Timeout of query can be set by users. Unit: s If not set, default value 0 will be used, which
    * will use server configuration.
@@ -116,6 +120,10 @@ public class IoTDBConnection implements Connection {
     return url;
   }
 
+  public List<EndPoint> getNodeList() {
+    return nodeList;
+  }
+
   @Override
   public boolean isWrapperFor(Class<?> arg0) throws SQLException {
     throw new SQLException("Does not support isWrapperFor");
@@ -468,6 +476,9 @@ public class IoTDBConnection implements Connection {
     try {
       openResp = client.openSession(openReq);
       sessionId = openResp.getSessionId();
+      if (openResp.isSetNodeList()) {
+        nodeList = openResp.nodeList;
+      }
       // validate connection
       RpcUtils.verifySuccess(openResp.getStatus());