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

adoroszlai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 37cf5f892d4 HDDS-17168. Add datanode gRPC server metrics (#12049)
37cf5f892d4 is described below

commit 37cf5f892d457841dab9a175a37ab193760a23a8
Author: Vincent Hsiao <[email protected]>
AuthorDate: Wed Nov 25 15:33:01 2026 +0800

    HDDS-17168. Add datanode gRPC server metrics (#12049)
---
 .../common/transport/server/XceiverServerGrpc.java |   9 +-
 .../transport/server/XceiverServerGrpcMetrics.java |  89 ++++++++++++++
 .../server/TestXceiverServerGrpcMetrics.java       | 133 +++++++++++++++++++++
 3 files changed, 229 insertions(+), 2 deletions(-)

diff --git 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/XceiverServerGrpc.java
 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/XceiverServerGrpc.java
index 9b01ff95834..c0abdeba259 100644
--- 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/XceiverServerGrpc.java
+++ 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/XceiverServerGrpc.java
@@ -80,6 +80,7 @@ public final class XceiverServerGrpc implements 
XceiverServerSpi {
   private boolean isStarted;
   private DatanodeDetails datanodeDetails;
   private EventLoopGroup eventLoopGroup;
+  private final XceiverServerGrpcMetrics metrics;
 
   /**
    * Constructs a Grpc server class.
@@ -120,6 +121,7 @@ public XceiverServerGrpc(DatanodeDetails datanodeDetails,
                   "ChunkReader-%d")
               .build());
     }
+    metrics = new XceiverServerGrpcMetrics(readExecutors, 
datanodeDetails.getUuidString());
 
     ThreadFactory factory = new ThreadFactoryBuilder()
         .setDaemon(true)
@@ -159,7 +161,8 @@ public XceiverServerGrpc(DatanodeDetails datanodeDetails,
         .keepAliveTimeout(30, TimeUnit.SECONDS)
         .addService(ServerInterceptors.intercept(
             xceiverService.bindServiceWithZeroCopy(),
-            new GrpcServerInterceptor()));
+            new GrpcServerInterceptor()))
+        .addTransportFilter(metrics.newTransportFilter());
 
     SecurityConfig secConf = new SecurityConfig(conf);
     if (secConf.isSecurityEnabled() && secConf.isGrpcTlsEnabled()) {
@@ -179,6 +182,7 @@ public XceiverServerGrpc(DatanodeDetails datanodeDetails,
     }
     server = nettyServerBuilder.build();
     storageContainer = dispatcher;
+    metrics.register();
   }
 
   @VisibleForTesting
@@ -222,7 +226,9 @@ public void start() throws IOException {
 
   @Override
   public void stop() {
+    metrics.unregister();
     if (isStarted) {
+      isStarted = false;
       try {
         server.shutdown();
         server.awaitTermination(5, TimeUnit.SECONDS);
@@ -232,7 +238,6 @@ public void stop() {
         LOG.error("failed to shutdown XceiverServerGrpc", e);
         Thread.currentThread().interrupt();
       }
-      isStarted = false;
     }
   }
 
diff --git 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/XceiverServerGrpcMetrics.java
 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/XceiverServerGrpcMetrics.java
new file mode 100644
index 00000000000..92a3a3eebb3
--- /dev/null
+++ 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/XceiverServerGrpcMetrics.java
@@ -0,0 +1,89 @@
+/*
+ * 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.hadoop.ozone.container.common.transport.server;
+
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.atomic.AtomicLong;
+import org.apache.hadoop.metrics2.MetricsCollector;
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+import org.apache.hadoop.metrics2.MetricsSource;
+import org.apache.hadoop.metrics2.annotation.Metrics;
+import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
+import org.apache.hadoop.metrics2.lib.Interns;
+import org.apache.hadoop.ozone.OzoneConsts;
+import org.apache.hadoop.ozone.util.MetricUtil;
+import org.apache.ratis.thirdparty.io.grpc.Attributes;
+import org.apache.ratis.thirdparty.io.grpc.ServerTransportFilter;
+
+/**
+ * Metrics for the datanode read executor shared by gRPC and domain socket 
reads,
+ * and for gRPC client connections.
+ */
+@Metrics(about = "Datanode read executor and gRPC client connection metrics", 
context = OzoneConsts.OZONE)
+public final class XceiverServerGrpcMetrics implements MetricsSource {
+
+  public static final String SOURCE_NAME = 
XceiverServerGrpcMetrics.class.getSimpleName();
+
+  private final ThreadPoolExecutor readExecutor;
+  private final AtomicLong activeClientConnections = new AtomicLong();
+  private final String sourceName;
+
+  XceiverServerGrpcMetrics(ThreadPoolExecutor readExecutor, String 
datanodeUuid) {
+    this.readExecutor = readExecutor;
+    this.sourceName = MetricUtil.getMetricsSourceName(SOURCE_NAME, 
datanodeUuid);
+  }
+
+  void register() {
+    DefaultMetricsSystem.instance().register(sourceName,
+        "Datanode read executor and gRPC client connection metrics", this);
+  }
+
+  public void unregister() {
+    DefaultMetricsSystem.instance().unregisterSource(sourceName);
+  }
+
+  ServerTransportFilter newTransportFilter() {
+    return new ServerTransportFilter() {
+      @Override
+      public Attributes transportReady(Attributes transportAttrs) {
+        activeClientConnections.incrementAndGet();
+        return transportAttrs;
+      }
+
+      @Override
+      public void transportTerminated(Attributes transportAttrs) {
+        if (transportAttrs != null) {
+          activeClientConnections.decrementAndGet();
+        }
+      }
+    };
+  }
+
+  @Override
+  public void getMetrics(MetricsCollector collector, boolean all) {
+    MetricsRecordBuilder builder = 
collector.addRecord(sourceName).setContext(OzoneConsts.OZONE);
+    builder.addGauge(Interns.info("ActiveReadExecutorThreads",
+        "Number of active threads in the shared read executor"), 
readExecutor.getActiveCount())
+        .addGauge(Interns.info("MaxReadExecutorThreads",
+            "Maximum number of threads in the shared read executor"), 
readExecutor.getMaximumPoolSize())
+        .addGauge(Interns.info("ReadExecutorQueueLength",
+            "Number of requests waiting in the shared read executor queue"), 
readExecutor.getQueue().size())
+        .addGauge(Interns.info("ActiveClientConnections",
+            "Number of active gRPC client connections"), 
activeClientConnections.get());
+  }
+}
diff --git 
a/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/transport/server/TestXceiverServerGrpcMetrics.java
 
b/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/transport/server/TestXceiverServerGrpcMetrics.java
new file mode 100644
index 00000000000..a84d47cd203
--- /dev/null
+++ 
b/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/transport/server/TestXceiverServerGrpcMetrics.java
@@ -0,0 +1,133 @@
+/*
+ * 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.hadoop.ozone.container.common.transport.server;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.hdds.protocol.DatanodeDetails;
+import org.apache.hadoop.hdds.protocol.MockDatanodeDetails;
+import org.apache.hadoop.metrics2.AbstractMetric;
+import org.apache.hadoop.metrics2.MetricsSource;
+import org.apache.hadoop.metrics2.impl.MetricsCollectorImpl;
+import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
+import org.apache.hadoop.ozone.OzoneConfigKeys;
+import org.apache.hadoop.ozone.OzoneConsts;
+import org.apache.hadoop.ozone.container.common.interfaces.ContainerDispatcher;
+import org.apache.hadoop.ozone.util.MetricUtil;
+import org.apache.ratis.thirdparty.io.grpc.Attributes;
+import org.apache.ratis.thirdparty.io.grpc.ServerTransportFilter;
+import org.junit.jupiter.api.Test;
+
+class TestXceiverServerGrpcMetrics {
+
+  @Test
+  void reportsReadExecutorAndClientConnectionMetrics() throws Exception {
+    ThreadPoolExecutor readExecutor = new ThreadPoolExecutor(1, 3, 60, 
TimeUnit.SECONDS,
+        new LinkedBlockingQueue<>());
+    CountDownLatch taskStarted = new CountDownLatch(1);
+    CountDownLatch releaseTask = new CountDownLatch(1);
+    XceiverServerGrpcMetrics metrics = new 
XceiverServerGrpcMetrics(readExecutor, "test-datanode");
+    boolean registered = false;
+    try {
+      readExecutor.execute(() -> {
+        taskStarted.countDown();
+        try {
+          releaseTask.await();
+        } catch (InterruptedException e) {
+          Thread.currentThread().interrupt();
+        }
+      });
+      assertTrue(taskStarted.await(10, TimeUnit.SECONDS));
+      readExecutor.execute(() -> { });
+      readExecutor.execute(() -> { });
+
+      String sourceName = 
MetricUtil.getMetricsSourceName(XceiverServerGrpcMetrics.SOURCE_NAME, 
"test-datanode");
+      assertNull(DefaultMetricsSystem.instance().getSource(sourceName));
+      metrics.register();
+      registered = true;
+      ServerTransportFilter transportFilter = metrics.newTransportFilter();
+      transportFilter.transportTerminated(null);
+      transportFilter.transportReady(Attributes.EMPTY);
+      transportFilter.transportReady(Attributes.EMPTY);
+      transportFilter.transportTerminated(Attributes.EMPTY);
+
+      MetricsCollectorImpl collector = new MetricsCollectorImpl();
+      MetricsSource source = 
DefaultMetricsSystem.instance().getSource(sourceName);
+      assertNotNull(source);
+      source.getMetrics(collector, true);
+
+      assertEquals(OzoneConsts.OZONE, collector.getRecords().get(0).context());
+      Iterable<AbstractMetric> all = collector.getRecords().get(0).metrics();
+      assertEquals(1L, findLong(all, "ActiveReadExecutorThreads"));
+      assertEquals(3L, findLong(all, "MaxReadExecutorThreads"));
+      assertEquals(2L, findLong(all, "ReadExecutorQueueLength"));
+      assertEquals(1L, findLong(all, "ActiveClientConnections"));
+      metrics.unregister();
+      registered = false;
+      assertNull(DefaultMetricsSystem.instance().getSource(sourceName));
+    } finally {
+      releaseTask.countDown();
+      readExecutor.shutdownNow();
+      if (registered) {
+        metrics.unregister();
+      }
+    }
+  }
+
+  @Test
+  void unregistersMetricsWhenServerStops() throws Exception {
+    DatanodeDetails datanode = MockDatanodeDetails.randomDatanodeDetails();
+    String sourceName = 
MetricUtil.getMetricsSourceName(XceiverServerGrpcMetrics.SOURCE_NAME, 
datanode.getUuidString());
+    OzoneConfiguration conf = new OzoneConfiguration();
+    conf.setBoolean(OzoneConfigKeys.HDDS_CONTAINER_IPC_RANDOM_PORT, true);
+    ThreadPoolExecutor readExecutor = new ThreadPoolExecutor(1, 1, 60, 
TimeUnit.SECONDS,
+        new LinkedBlockingQueue<>());
+    assertNull(DefaultMetricsSystem.instance().getSource(sourceName));
+    XceiverServerGrpc server = new XceiverServerGrpc(datanode, conf, 
readExecutor,
+        mock(ContainerDispatcher.class), null);
+    try {
+      assertNotNull(DefaultMetricsSystem.instance().getSource(sourceName));
+      server.start();
+    } finally {
+      try {
+        server.stop();
+      } finally {
+        readExecutor.shutdownNow();
+      }
+    }
+    assertNull(DefaultMetricsSystem.instance().getSource(sourceName));
+  }
+
+  private static long findLong(Iterable<AbstractMetric> metrics, String name) {
+    for (AbstractMetric metric : metrics) {
+      if (metric.name().equals(name)) {
+        return metric.value().longValue();
+      }
+    }
+    throw new AssertionError("Metric not found: " + name);
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to