You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ma...@apache.org on 2023/03/13 08:43:23 UTC

[iotdb] branch IOTDB-5663 updated: add connection metrics

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

marklau99 pushed a commit to branch IOTDB-5663
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/IOTDB-5663 by this push:
     new c630fb98f5 add connection metrics
c630fb98f5 is described below

commit c630fb98f55e9442f45dff5565049d5831ee6a81
Author: Liu Xuxin <li...@outlook.com>
AuthorDate: Mon Mar 13 16:43:18 2023 +0800

    add connection metrics
---
 .../iotdb/confignode/service/ConfigNode.java       |  2 +-
 .../Apache-IoTDB-Network-Dashboard.json            | 92 +++++++++++++++++++++-
 .../metrics/metricsets/net/INetMetricManager.java  |  4 +
 .../metricsets/net/LinuxNetMetricManager.java      | 37 +++++++++
 .../iotdb/metrics/metricsets/net/NetMetrics.java   | 16 ++++
 .../db/service/metrics/DataNodeMetricsHelper.java  |  2 +-
 6 files changed, 150 insertions(+), 3 deletions(-)

diff --git a/confignode/src/main/java/org/apache/iotdb/confignode/service/ConfigNode.java b/confignode/src/main/java/org/apache/iotdb/confignode/service/ConfigNode.java
index 3b990256cd..bd66190163 100644
--- a/confignode/src/main/java/org/apache/iotdb/confignode/service/ConfigNode.java
+++ b/confignode/src/main/java/org/apache/iotdb/confignode/service/ConfigNode.java
@@ -219,7 +219,7 @@ public class ConfigNode implements ConfigNodeMBean {
     MetricService.getInstance().addMetricSet(new ProcessMetrics());
     MetricService.getInstance().addMetricSet(new SystemMetrics(false));
     MetricService.getInstance().addMetricSet(new DiskMetrics(IoTDBConstant.CN_ROLE));
-    MetricService.getInstance().addMetricSet(new NetMetrics());
+    MetricService.getInstance().addMetricSet(new NetMetrics(IoTDBConstant.CN_ROLE));
 
     LOGGER.info("Successfully setup internal services.");
   }
diff --git a/docs/UserGuide/Monitor-Alert/Apache-IoTDB-Network-Dashboard.json b/docs/UserGuide/Monitor-Alert/Apache-IoTDB-Network-Dashboard.json
index 40eb7174d0..f63d532a7b 100644
--- a/docs/UserGuide/Monitor-Alert/Apache-IoTDB-Network-Dashboard.json
+++ b/docs/UserGuide/Monitor-Alert/Apache-IoTDB-Network-Dashboard.json
@@ -367,6 +367,96 @@
       ],
       "title": "Packet Speed",
       "type": "timeseries"
+    },
+    {
+      "datasource": {
+        "type": "prometheus",
+        "uid": "${DS_PROMETHEUS}"
+      },
+      "fieldConfig": {
+        "defaults": {
+          "color": {
+            "mode": "palette-classic"
+          },
+          "custom": {
+            "axisCenteredZero": false,
+            "axisColorMode": "text",
+            "axisGridShow": true,
+            "axisLabel": "",
+            "axisPlacement": "auto",
+            "barAlignment": 0,
+            "drawStyle": "line",
+            "fillOpacity": 0,
+            "gradientMode": "none",
+            "hideFrom": {
+              "legend": false,
+              "tooltip": false,
+              "viz": false
+            },
+            "lineInterpolation": "linear",
+            "lineWidth": 1,
+            "pointSize": 5,
+            "scaleDistribution": {
+              "type": "linear"
+            },
+            "showPoints": "auto",
+            "spanNulls": false,
+            "stacking": {
+              "group": "A",
+              "mode": "none"
+            },
+            "thresholdsStyle": {
+              "mode": "off"
+            }
+          },
+          "decimals": 0,
+          "mappings": [],
+          "thresholds": {
+            "mode": "absolute",
+            "steps": [
+              {
+                "color": "green",
+                "value": null
+              }
+            ]
+          }
+        },
+        "overrides": []
+      },
+      "gridPos": {
+        "h": 10,
+        "w": 24,
+        "x": 0,
+        "y": 29
+      },
+      "id": 8,
+      "options": {
+        "legend": {
+          "calcs": [],
+          "displayMode": "list",
+          "placement": "bottom",
+          "showLegend": true
+        },
+        "tooltip": {
+          "mode": "multi",
+          "sort": "none"
+        }
+      },
+      "targets": [
+        {
+          "datasource": {
+            "type": "prometheus",
+            "uid": "${DS_PROMETHEUS}"
+          },
+          "editorMode": "code",
+          "expr": "connection_num{instance=~\"$instance\"}",
+          "legendFormat": "{{job}}-{{instance}}",
+          "range": true,
+          "refId": "A"
+        }
+      ],
+      "title": "Connection Num",
+      "type": "timeseries"
     }
   ],
   "refresh": "15s",
@@ -439,6 +529,6 @@
   "timezone": "",
   "title": "Apache IoTDB Network Dashboard",
   "uid": "AXEPYc-Vz",
-  "version": 17,
+  "version": 19,
   "weekStart": ""
 }
\ No newline at end of file
diff --git a/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/net/INetMetricManager.java b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/net/INetMetricManager.java
index f6ae15d37c..35f551e2a5 100644
--- a/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/net/INetMetricManager.java
+++ b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/net/INetMetricManager.java
@@ -55,4 +55,8 @@ public interface INetMetricManager {
   default Set<String> getIfaceSet() {
     return Collections.emptySet();
   }
+
+  default int getConnectionNum() {
+    return 0;
+  }
 }
diff --git a/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/net/LinuxNetMetricManager.java b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/net/LinuxNetMetricManager.java
index 4e3fab7061..098463dea4 100644
--- a/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/net/LinuxNetMetricManager.java
+++ b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/net/LinuxNetMetricManager.java
@@ -19,12 +19,17 @@
 
 package org.apache.iotdb.metrics.metricsets.net;
 
+import org.apache.iotdb.metrics.config.MetricConfig;
+import org.apache.iotdb.metrics.config.MetricConfigDescriptor;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -50,11 +55,16 @@ public class LinuxNetMetricManager implements INetMetricManager {
   private static final long UPDATE_INTERVAL = 10_000L;
 
   private static final int IFACE_NAME_INDEX = 0;
+
+  private static final MetricConfig METRIC_CONFIG =
+      MetricConfigDescriptor.getInstance().getMetricConfig();
   // initialized after reading status file
   private int receivedBytesIndex = 0;
   private int transmittedBytesIndex = 0;
   private int receivedPacketsIndex = 0;
   private int transmittedPacketsIndex = 0;
+  private int connectionNum = 0;
+  private final String[] getConnectNumCmd;
   private Set<String> ifaceSet;
 
   private final Map<String, Long> receivedBytesMapForIface;
@@ -70,6 +80,12 @@ public class LinuxNetMetricManager implements INetMetricManager {
     receivedPacketsMapForIface = new HashMap<>(ifaceSet.size() + 1, 1);
     transmittedPacketsMapForIface = new HashMap<>(ifaceSet.size() + 1, 1);
     collectNetStatusIndex();
+    this.getConnectNumCmd =
+        new String[] {
+          "/bin/sh",
+          "-c",
+          String.format("ls -l /proc/%s/fd | grep socket: | wc -l", METRIC_CONFIG.getPid())
+        };
   }
 
   private long lastUpdateTime = 0L;
@@ -104,6 +120,11 @@ public class LinuxNetMetricManager implements INetMetricManager {
     return transmittedPacketsMapForIface;
   }
 
+  @Override
+  public int getConnectionNum() {
+    return connectionNum;
+  }
+
   private void checkUpdate() {
     if (System.currentTimeMillis() - lastUpdateTime >= UPDATE_INTERVAL) {
       updateNetStatus();
@@ -193,5 +214,21 @@ public class LinuxNetMetricManager implements INetMetricManager {
     } catch (IOException e) {
       log.error("Meets error when reading {} for net status", NET_STATUS_PATH, e);
     }
+
+    // update socket num
+    try {
+      Process process = Runtime.getRuntime().exec(this.getConnectNumCmd);
+      StringBuilder result = new StringBuilder();
+      try (BufferedReader input =
+          new BufferedReader(new InputStreamReader(process.getInputStream()))) {
+        String line;
+        while ((line = input.readLine()) != null) {
+          result.append(line);
+        }
+      }
+      this.connectionNum = Integer.parseInt(result.toString().trim());
+    } catch (IOException e) {
+      log.error("Failed to get socket num", e);
+    }
   }
 }
diff --git a/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/net/NetMetrics.java b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/net/NetMetrics.java
index e1e565c049..223808976f 100644
--- a/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/net/NetMetrics.java
+++ b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/net/NetMetrics.java
@@ -33,11 +33,19 @@ public class NetMetrics implements IMetricSet {
   private static final String RECEIVED_PACKETS = "received_packets";
   private static final String TRANSMITTED_BYTES = "transmitted_bytes";
   private static final String TRANSMITTED_PACKETS = "transmitted_packets";
+  private static final String CONNECTION_NUM = "connection_num";
 
   private static final String TYPE = "type";
   private static final String IFACE_NAME = "iface_name";
   private static final String RECEIVE = "receive";
   private static final String TRANSMIT = "transmit";
+  private static final String PROCESS_NAME = "process_num";
+
+  private final String processName;
+
+  public NetMetrics(String processName) {
+    this.processName = processName;
+  }
 
   @Override
   public void bindTo(AbstractMetricService metricService) {
@@ -81,6 +89,13 @@ public class NetMetrics implements IMetricSet {
           IFACE_NAME,
           iface);
     }
+    metricService.createAutoGauge(
+        CONNECTION_NUM,
+        MetricLevel.IMPORTANT,
+        netMetricManager,
+        x -> x.getConnectionNum(),
+        PROCESS_NAME,
+        this.processName);
   }
 
   @Override
@@ -94,5 +109,6 @@ public class NetMetrics implements IMetricSet {
       metricService.remove(
           MetricType.AUTO_GAUGE, TRANSMITTED_PACKETS, TYPE, TRANSMIT, IFACE_NAME, iface);
     }
+    metricService.remove(MetricType.AUTO_GAUGE, CONNECTION_NUM, PROCESS_NAME, this.processName);
   }
 }
diff --git a/server/src/main/java/org/apache/iotdb/db/service/metrics/DataNodeMetricsHelper.java b/server/src/main/java/org/apache/iotdb/db/service/metrics/DataNodeMetricsHelper.java
index e116b52119..5e76a37efb 100644
--- a/server/src/main/java/org/apache/iotdb/db/service/metrics/DataNodeMetricsHelper.java
+++ b/server/src/main/java/org/apache/iotdb/db/service/metrics/DataNodeMetricsHelper.java
@@ -43,7 +43,7 @@ public class DataNodeMetricsHelper {
     MetricService.getInstance().addMetricSet(new ProcessMetrics());
     MetricService.getInstance().addMetricSet(new SystemMetrics(true));
     MetricService.getInstance().addMetricSet(new DiskMetrics(IoTDBConstant.DN_ROLE));
-    MetricService.getInstance().addMetricSet(new NetMetrics());
+    MetricService.getInstance().addMetricSet(new NetMetrics(IoTDBConstant.DN_ROLE));
     MetricService.getInstance().addMetricSet(new WritingMetrics());
 
     // bind query related metrics