From b15630b48b5c345d12a45bc7e854a4db0c32b5b5 Mon Sep 17 00:00:00 2001 From: Liu Xuxin <37140360+THUMarkLau@users.noreply.github.com> Date: Tue, 14 Mar 2023 15:56:16 +0800 Subject: [PATCH] [IOTDB-5663] Add connection num metrics (#9307) --- .../iotdb/confignode/service/ConfigNode.java | 2 +- .../Apache-IoTDB-Network-Dashboard.json | 92 ++++++++++++++++++- .../metricsets/net/INetMetricManager.java | 4 + .../metricsets/net/LinuxNetMetricManager.java | 37 ++++++++ .../metrics/metricsets/net/NetMetrics.java | 16 ++++ .../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 3b990256cd4e..bd6619016372 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 @@ private void setUpInternalServices() throws StartupException { 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 40eb7174d045..f63d532a7ba0 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 f6ae15d37c28..35f551e2a5c6 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 @@ default Map getTransmittedPackets() { default Set 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 4e3fab70617d..098463dea49a 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 ifaceSet; private final Map receivedBytesMapForIface; @@ -70,6 +80,12 @@ public LinuxNetMetricManager() { 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 Map getTransmittedPackets() { return transmittedPacketsMapForIface; } + @Override + public int getConnectionNum() { + return connectionNum; + } + private void checkUpdate() { if (System.currentTimeMillis() - lastUpdateTime >= UPDATE_INTERVAL) { updateNetStatus(); @@ -193,5 +214,21 @@ private void updateNetStatus() { } 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 e1e565c049c6..223808976fd3 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 void bindTo(AbstractMetricService metricService) { IFACE_NAME, iface); } + metricService.createAutoGauge( + CONNECTION_NUM, + MetricLevel.IMPORTANT, + netMetricManager, + x -> x.getConnectionNum(), + PROCESS_NAME, + this.processName); } @Override @@ -94,5 +109,6 @@ public void unbindFrom(AbstractMetricService metricService) { 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 e116b52119de..5e76a37efbc8 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 static void bind() { 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