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

sammichen 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 565775c7dd HDDS-7464. Container Report at SCM is not coming separately 
for ICR and FCR in prometheus endpoint (#4035)
565775c7dd is described below

commit 565775c7dd9920ef792248d605d566f551bc6074
Author: Sumit Agrawal <[email protected]>
AuthorDate: Wed Dec 7 08:21:10 2022 +0530

    HDDS-7464. Container Report at SCM is not coming separately for ICR and FCR 
in prometheus endpoint (#4035)
---
 .../FixedThreadPoolWithAffinityExecutor.java       |   7 +-
 .../hdds/server/events/SingleThreadExecutor.java   |   6 +-
 .../org/apache/hadoop/hdds/utils/MetricsUtil.java  | 100 +++++++++++++++++++++
 3 files changed, 106 insertions(+), 7 deletions(-)

diff --git 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/events/FixedThreadPoolWithAffinityExecutor.java
 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/events/FixedThreadPoolWithAffinityExecutor.java
index 4949195cdd..f53bce533d 100644
--- 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/events/FixedThreadPoolWithAffinityExecutor.java
+++ 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/events/FixedThreadPoolWithAffinityExecutor.java
@@ -18,6 +18,7 @@
 package org.apache.hadoop.hdds.server.events;
 
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import org.apache.hadoop.hdds.utils.MetricsUtil;
 import org.apache.hadoop.metrics2.annotation.Metric;
 import org.apache.hadoop.metrics2.annotation.Metrics;
 import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
@@ -126,10 +127,8 @@ public class FixedThreadPoolWithAffinityExecutor<P, Q>
       ++i;
     }
 
-    DefaultMetricsSystem.instance()
-        .register(EVENT_QUEUE + name,
-            "Event Executor metrics ",
-            this);
+    MetricsUtil.registerDynamic(this, EVENT_QUEUE + name,
+        "Event Executor metrics ", "EventQueue");
   }
   
   public void setQueueWaitThreshold(long queueWaitThreshold) {
diff --git 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/events/SingleThreadExecutor.java
 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/events/SingleThreadExecutor.java
index 09be9cfd5f..bc8f7425b6 100644
--- 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/events/SingleThreadExecutor.java
+++ 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/events/SingleThreadExecutor.java
@@ -17,6 +17,7 @@
  */
 package org.apache.hadoop.hdds.server.events;
 
+import org.apache.hadoop.hdds.utils.MetricsUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -25,7 +26,6 @@ import java.util.concurrent.Executors;
 
 import org.apache.hadoop.metrics2.annotation.Metric;
 import org.apache.hadoop.metrics2.annotation.Metrics;
-import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
 import org.apache.hadoop.metrics2.lib.MutableCounterLong;
 
 /**
@@ -64,8 +64,8 @@ public class SingleThreadExecutor<P> implements 
EventExecutor<P> {
    */
   public SingleThreadExecutor(String name) {
     this.name = name;
-    DefaultMetricsSystem.instance()
-        .register(EVENT_QUEUE + name, "Event Executor metrics ", this);
+    MetricsUtil.registerDynamic(this, EVENT_QUEUE + name,
+        "Event Executor metrics ", "EventQueue");
 
     executor = Executors.newSingleThreadExecutor(
         runnable -> {
diff --git 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/MetricsUtil.java
 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/MetricsUtil.java
new file mode 100644
index 0000000000..0850bea1ea
--- /dev/null
+++ 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/MetricsUtil.java
@@ -0,0 +1,100 @@
+/*
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.hdds.utils;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Map;
+import org.apache.hadoop.metrics2.annotation.Metrics;
+import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Metrics util for metrics.
+ */
+public final class MetricsUtil {
+  private static final String ANNOTATIONS = "annotations";
+  private static final String ANNOTATION_DATA = "annotationData";
+  private static final Class<? extends Annotation> ANNOTATION_TO_ALTER
+      = Metrics.class;
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(MetricsUtil.class);
+  
+  private MetricsUtil() {
+  }
+
+  /**
+   * register metric with changing class annotation for metrics.
+   * 
+   * @param source source to register
+   * @param name name of metric
+   * @param desc description of metric
+   * @param context context of metric
+   * @param <T> source type
+   */
+  public static <T> void registerDynamic(
+      T source, String name, String desc, String context) {
+    updateAnnotation(source.getClass(), name, desc, context);
+    DefaultMetricsSystem.instance().register(name, desc, source);
+  }
+  
+  private static void updateAnnotation(
+      Class clz, String name, String desc, String context) {
+    try {
+      Annotation annotationValue = new Metrics() {
+
+        @Override
+        public Class<? extends Annotation> annotationType() {
+          return ANNOTATION_TO_ALTER;
+        }
+
+        @Override
+        public String name() {
+          return name;
+        }
+
+        @Override
+        public String about() {
+          return desc;
+        }
+
+        @Override
+        public String context() {
+          return context;
+        }
+      };
+      
+      Method method = clz.getClass().getDeclaredMethod(
+          ANNOTATION_DATA, null);
+      method.setAccessible(true);
+      Object annotationData = method.invoke(clz);
+      Field annotations = annotationData.getClass()
+          .getDeclaredField(ANNOTATIONS);
+      annotations.setAccessible(true);
+      Map<Class<? extends Annotation>, Annotation> map =
+          (Map<Class<? extends Annotation>, Annotation>) annotations
+              .get(annotationData);
+      map.put(ANNOTATION_TO_ALTER, annotationValue);
+    } catch (Exception e) {
+      LOG.error("Update Metrics annotation failed. ", e);
+    }
+  }
+}


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

Reply via email to