priyeshkaratha commented on code in PR #9413:
URL: https://github.com/apache/ozone/pull/9413#discussion_r2588917225


##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/DataNodeMetricsCollectionTask.java:
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.recon.tasks;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.concurrent.Callable;
+import org.apache.commons.io.IOUtils;
+import org.apache.hadoop.ozone.recon.api.types.DatanodePendingDeletionMetrics;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Task for collecting pending deletion metrics from a DataNode using JMX.
+ * This class implements the Callable interface and retrieves pending deletion
+ * information (e.g., total pending block bytes) from a DataNode by invoking 
its
+ * JMX endpoint. The metrics are parsed and encapsulated in the
+ * {@link DatanodePendingDeletionMetrics} object.
+ */
+public class DataNodeMetricsCollectionTask implements 
Callable<DatanodePendingDeletionMetrics> {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(DataNodeMetricsCollectionTask.class);
+  private final String host;
+  private final int port;
+  private final String nodeUuid;
+  private static ObjectMapper objectMapper = new ObjectMapper();
+
+  public DataNodeMetricsCollectionTask(String host, int port, String nodeUuid) 
{
+    this.host = host;
+    this.port = port;
+    this.nodeUuid = nodeUuid;
+  }
+
+  @Override
+  public DatanodePendingDeletionMetrics call() throws Exception {
+    LOG.debug("Collecting pending deletion metrics from DataNode {}:{}", host, 
port);
+    try (CloseableHttpClient httpClient = HttpClients.custom()
+        .setDefaultRequestConfig(RequestConfig.custom()
+            .setConnectTimeout(60000)
+            .setSocketTimeout(60000)
+            .build())
+        .build()) {
+
+      InputStream in = httpClient.execute(new 
HttpGet(getJmxMetricsUrl())).getEntity().getContent();
+      byte[] responseBytes = IOUtils.toByteArray(in);
+      String jsonResponse = new String(responseBytes, StandardCharsets.UTF_8);

Review Comment:
   Handled failure scenarios.



##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/DataNodeMetricsCollectionTask.java:
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.recon.tasks;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.concurrent.Callable;
+import org.apache.commons.io.IOUtils;
+import org.apache.hadoop.ozone.recon.api.types.DatanodePendingDeletionMetrics;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Task for collecting pending deletion metrics from a DataNode using JMX.
+ * This class implements the Callable interface and retrieves pending deletion
+ * information (e.g., total pending block bytes) from a DataNode by invoking 
its
+ * JMX endpoint. The metrics are parsed and encapsulated in the
+ * {@link DatanodePendingDeletionMetrics} object.
+ */
+public class DataNodeMetricsCollectionTask implements 
Callable<DatanodePendingDeletionMetrics> {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(DataNodeMetricsCollectionTask.class);
+  private final String host;
+  private final int port;
+  private final String nodeUuid;
+  private static ObjectMapper objectMapper = new ObjectMapper();
+
+  public DataNodeMetricsCollectionTask(String host, int port, String nodeUuid) 
{
+    this.host = host;
+    this.port = port;
+    this.nodeUuid = nodeUuid;
+  }
+
+  @Override
+  public DatanodePendingDeletionMetrics call() throws Exception {
+    LOG.debug("Collecting pending deletion metrics from DataNode {}:{}", host, 
port);
+    try (CloseableHttpClient httpClient = HttpClients.custom()
+        .setDefaultRequestConfig(RequestConfig.custom()
+            .setConnectTimeout(60000)
+            .setSocketTimeout(60000)
+            .build())
+        .build()) {
+
+      InputStream in = httpClient.execute(new 
HttpGet(getJmxMetricsUrl())).getEntity().getContent();
+      byte[] responseBytes = IOUtils.toByteArray(in);
+      String jsonResponse = new String(responseBytes, StandardCharsets.UTF_8);
+      return new DatanodePendingDeletionMetrics(host, nodeUuid,
+          parseMetrics(jsonResponse, "BlockDeletingService", 
"TotalPendingBlockBytes"));
+    }
+  }
+
+  private static long parseMetrics(String jsonResponse, String serviceName, 
String keyName)
+      throws IOException {
+    if (jsonResponse == null || jsonResponse.isEmpty()) {
+      return -1L;
+    }
+    JsonNode root = objectMapper.readTree(jsonResponse);
+    JsonNode beans = root.get("beans");
+    if (beans != null && beans.isArray()) {
+      // Find the bean matching the service name
+      for (JsonNode bean : beans) {
+        String beanName = bean.path("name").asText("");
+        if (beanName.contains(serviceName)) {
+          // Extract and return the metric value from the bean
+          return extractMetrics(bean, keyName);
+        }
+      }
+    }
+    return -1L;
+  }
+
+  private static long extractMetrics(JsonNode beanNode, String keyName) {
+    return beanNode.path(keyName).asLong(0L);
+  }
+
+  private String getJmxMetricsUrl() {
+    return String.format("%s://%s:%d/jmx?qry=Hadoop:service=%s,name=%s", 
"http",

Review Comment:
   Handled it.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to