You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2022/02/18 08:57:38 UTC

[GitHub] [iotdb] neuyilan commented on a change in pull request #5081: [ISSUE-3857] Support show now() function

neuyilan commented on a change in pull request #5081:
URL: https://github.com/apache/iotdb/pull/5081#discussion_r809785166



##########
File path: server/src/main/java/org/apache/iotdb/db/qp/utils/ShowNowUtils.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.db.qp.utils;
+
+import org.apache.iotdb.db.query.dataset.ShowNowResult;
+
+import com.sun.management.OperatingSystemMXBean;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import oshi.SystemInfo;
+import oshi.hardware.CentralProcessor;
+
+import java.lang.management.ManagementFactory;
+import java.net.InetAddress;
+import java.text.DateFormat;
+import java.text.DecimalFormat;
+import java.text.SimpleDateFormat;
+import java.util.LinkedList;
+import java.util.List;
+
+public class ShowNowUtils {
+  private static final Logger logger = LoggerFactory.getLogger(ShowNowUtils.class);
+
+  private String ipAddress;
+  private String systemTime;
+  private String cpuLoad;
+  private String totalMemorySize;
+  private String freeMemorySize;
+
+  public List<ShowNowResult> getShowNowResults() {
+    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

Review comment:
       It's better that the time format can be transferred from the SQL.

##########
File path: server/src/main/java/org/apache/iotdb/db/qp/utils/ShowNowUtils.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.db.qp.utils;
+
+import org.apache.iotdb.db.query.dataset.ShowNowResult;
+
+import com.sun.management.OperatingSystemMXBean;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import oshi.SystemInfo;
+import oshi.hardware.CentralProcessor;
+
+import java.lang.management.ManagementFactory;
+import java.net.InetAddress;
+import java.text.DateFormat;
+import java.text.DecimalFormat;
+import java.text.SimpleDateFormat;
+import java.util.LinkedList;
+import java.util.List;
+
+public class ShowNowUtils {
+  private static final Logger logger = LoggerFactory.getLogger(ShowNowUtils.class);
+
+  private String ipAddress;
+  private String systemTime;
+  private String cpuLoad;
+  private String totalMemorySize;
+  private String freeMemorySize;
+
+  public List<ShowNowResult> getShowNowResults() {
+    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
+    SystemInfo systemInfo = new SystemInfo();
+    try {
+      ipAddress = InetAddress.getLocalHost().getHostAddress();
+    } catch (Exception e) {
+      e.printStackTrace();
+    }

Review comment:
       Why just use the `iotdb-engine.properties` `rpc_address` as the default ip  address?

##########
File path: server/src/main/java/org/apache/iotdb/db/qp/utils/ShowNowUtils.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.db.qp.utils;
+
+import org.apache.iotdb.db.query.dataset.ShowNowResult;
+
+import com.sun.management.OperatingSystemMXBean;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import oshi.SystemInfo;
+import oshi.hardware.CentralProcessor;
+
+import java.lang.management.ManagementFactory;
+import java.net.InetAddress;
+import java.text.DateFormat;
+import java.text.DecimalFormat;
+import java.text.SimpleDateFormat;
+import java.util.LinkedList;
+import java.util.List;
+
+public class ShowNowUtils {
+  private static final Logger logger = LoggerFactory.getLogger(ShowNowUtils.class);
+
+  private String ipAddress;
+  private String systemTime;
+  private String cpuLoad;
+  private String totalMemorySize;
+  private String freeMemorySize;
+
+  public List<ShowNowResult> getShowNowResults() {
+    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
+    SystemInfo systemInfo = new SystemInfo();
+    try {
+      ipAddress = InetAddress.getLocalHost().getHostAddress();
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+    try {
+      OperatingSystemMXBean osmxb =
+          (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
+      CentralProcessor processor = systemInfo.getHardware().getProcessor();
+      totalMemorySize =
+          new DecimalFormat("#.##")
+                  .format(osmxb.getTotalPhysicalMemorySize() / 1024.0 / 1024 / 1024)
+              + "G";
+      freeMemorySize =
+          new DecimalFormat("#.##").format(osmxb.getFreePhysicalMemorySize() / 1024.0 / 1024 / 1024)
+              + "G";
+      systemTime = df.format(System.currentTimeMillis());
+      cpuLoad = new DecimalFormat("#.##").format(processor.getSystemCpuLoad() * 100) + "%";
+    } catch (Exception e) {
+      e.printStackTrace();

Review comment:
       It's better to log the exception.

##########
File path: antlr/src/main/antlr4/org/apache/iotdb/db/qp/sql/IoTDBSqlParser.g4
##########
@@ -285,6 +284,11 @@ showAllTTL
     : SHOW ALL TTL
     ;
 
+// Show NOW
+showNow
+    : SHOW NOW LR_BRACKET RR_BRACKET
+    ;

Review comment:
       I think it would be better if the syntax is the same with MySQL or PostgreSQL.

##########
File path: cluster/src/main/java/org/apache/iotdb/cluster/query/shownow/ClusterShowNowQueryExecutor.java
##########
@@ -0,0 +1,182 @@
+/*
+ * 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.query.shownow;
+
+import org.apache.iotdb.cluster.ClusterIoTDB;
+import org.apache.iotdb.cluster.client.async.AsyncDataClient;
+import org.apache.iotdb.cluster.client.sync.SyncClientAdaptor;
+import org.apache.iotdb.cluster.client.sync.SyncDataClient;
+import org.apache.iotdb.cluster.config.ClusterConstant;
+import org.apache.iotdb.cluster.config.ClusterDescriptor;
+import org.apache.iotdb.cluster.rpc.thrift.Node;
+import org.apache.iotdb.cluster.server.member.MetaGroupMember;
+import org.apache.iotdb.db.exception.metadata.MetadataException;
+import org.apache.iotdb.db.qp.physical.sys.ShowNowPlan;
+import org.apache.iotdb.db.qp.utils.ShowNowUtils;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.dataset.ShowNowDataSet;
+import org.apache.iotdb.db.query.dataset.ShowNowResult;
+import org.apache.iotdb.db.query.executor.ShowNowQueryExecutor;
+import org.apache.iotdb.tsfile.read.query.dataset.QueryDataSet;
+
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.*;
+
+import static org.apache.iotdb.cluster.query.ClusterPlanExecutor.*;
+
+public class ClusterShowNowQueryExecutor extends ShowNowQueryExecutor {
+  private static final Logger logger = LoggerFactory.getLogger(ClusterShowNowQueryExecutor.class);
+  private MetaGroupMember metaGroupMember;
+
+  private static ExecutorService showNowQueryPool =
+      Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
+
+  public ClusterShowNowQueryExecutor(ShowNowPlan showNowPlan, MetaGroupMember metaGroupMember) {
+    super();
+    this.metaGroupMember = metaGroupMember;
+  }
+
+  @Override
+  public QueryDataSet execute(QueryContext context, ShowNowPlan showNowPlan)
+      throws MetadataException {
+    ConcurrentSkipListSet<ShowNowResult> resultSet = new ConcurrentSkipListSet<>();
+
+    ExecutorService pool =
+        new ThreadPoolExecutor(
+            THREAD_POOL_SIZE, THREAD_POOL_SIZE, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
+    List<Node> nodeList = metaGroupMember.getPartitionTable().getAllNodes();
+    List<Future<Void>> futureList = new ArrayList<>();
+    for (Node node : nodeList) {
+      futureList.add(
+          pool.submit(
+              () -> {
+                try {
+                  showNow(node, showNowPlan, resultSet, context);
+                } catch (Exception e) {
+                  logger.error("Cannot get show now result of {} from {}", showNowPlan, node);
+                }
+                return null;
+              }));
+    }
+
+    waitForThreadPool(futureList, pool, "showNow()");
+    List<ShowNowResult> showNowResults = applyShowNow(resultSet);
+    return new ShowNowDataSet(showNowResults, context, showNowPlan);
+  }
+
+  private void showNow(
+      Node node, ShowNowPlan showNowPlan, Set<ShowNowResult> resultSet, QueryContext context) {
+    if (node.equals(metaGroupMember.getThisNode())) {
+      showLocalNow(node, showNowPlan, resultSet, context);
+    } else {
+      showRemoteNow(node, showNowPlan, resultSet);
+    }
+  }
+
+  private void showLocalNow(
+      Node node, ShowNowPlan plan, Set<ShowNowResult> resultSet, QueryContext context) {
+    try {
+      List<ShowNowResult> localResult = null;
+      try {
+        localResult = new ShowNowUtils().getShowNowResults();
+      } catch (Exception e) {
+        e.printStackTrace();
+        logger.error("ClusterShowNowQueryExecutor showLocalNow");

Review comment:
       delete the `e.printStackTrace();` and log the exception in the log. 
   The same as others.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org