You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by ay...@apache.org on 2023/09/13 07:52:29 UTC

[hive] branch master updated: HIVE-17350: Metrics errors when retrying HS2 startup (#4630). (Mayank Kunwar, reviewed by Laszlo Bodor, Ayush Saxena)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 4880e53f162 HIVE-17350: Metrics errors when retrying HS2 startup (#4630). (Mayank Kunwar, reviewed by Laszlo Bodor, Ayush Saxena)
4880e53f162 is described below

commit 4880e53f16293f9b7389a54ff3a16d0187700aa6
Author: Mayank Kunwar <55...@users.noreply.github.com>
AuthorDate: Wed Sep 13 13:22:22 2023 +0530

    HIVE-17350: Metrics errors when retrying HS2 startup (#4630). (Mayank Kunwar, reviewed by Laszlo Bodor, Ayush Saxena)
---
 .../common/metrics/metrics2/CodahaleMetrics.java   | 16 ++++++++++-
 .../metrics2/TestCodahaleReportersConf.java        | 33 ++++++++++++++++++++--
 2 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/common/metrics/metrics2/CodahaleMetrics.java b/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/common/metrics/metrics2/CodahaleMetrics.java
index e34ddef968f..a69e6639888 100644
--- a/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/common/metrics/metrics2/CodahaleMetrics.java
+++ b/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/common/metrics/metrics2/CodahaleMetrics.java
@@ -49,6 +49,7 @@ import org.apache.hadoop.hive.common.metrics.common.MetricsConstant;
 import org.apache.hadoop.hive.common.metrics.common.MetricsScope;
 import org.apache.hadoop.hive.common.metrics.common.MetricsVariable;
 import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
+import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -83,6 +84,8 @@ public class CodahaleMetrics implements org.apache.hadoop.hive.common.metrics.co
   private LoadingCache<String, Meter> meters;
   private ConcurrentHashMap<String, Gauge> gauges;
 
+  private boolean hadoopMetricsStarted = false;
+
   private Configuration conf;
   private final Set<Closeable> reporters = new HashSet<>();
 
@@ -197,6 +200,10 @@ public class CodahaleMetrics implements org.apache.hadoop.hive.common.metrics.co
     for (Map.Entry<String, Metric> metric : metricRegistry.getMetrics().entrySet()) {
       metricRegistry.remove(metric.getKey());
     }
+    if (hadoopMetricsStarted) {
+      DefaultMetricsSystem.shutdown();
+      hadoopMetricsStarted = false;
+    }
     timers.invalidateAll();
     counters.invalidateAll();
     meters.invalidateAll();
@@ -402,7 +409,9 @@ public class CodahaleMetrics implements org.apache.hadoop.hive.common.metrics.co
       return false;
     }
 
-    for (String reporterClass : reporterClasses) {
+    // Removing duplicates from the list
+    Set<String> reporterClassesSet = new HashSet<>(reporterClasses);
+    for (String reporterClass : reporterClassesSet) {
       Class<?> name;
       try {
         name = conf.getClassByName(reporterClass);
@@ -417,6 +426,11 @@ public class CodahaleMetrics implements org.apache.hadoop.hive.common.metrics.co
         CodahaleReporter reporter = (CodahaleReporter) constructor.newInstance(metricRegistry, conf);
         reporter.start();
         reporters.add(reporter);
+        // hadoopMetricsStarted flag is set to true when Metrics2Reporter is initialised
+        // so that we could gracefully shutdown DefaultMetricsSystem in close() method
+        if (name.getName().equals(Metrics2Reporter.class.getName())) {
+          hadoopMetricsStarted = true;
+        }
       } catch (NoSuchMethodException | InstantiationException |
           IllegalAccessException | InvocationTargetException e) {
         LOGGER.error("Unable to instantiate using constructor(MetricRegistry, HiveConf) for"
diff --git a/standalone-metastore/metastore-common/src/test/java/org/apache/hadoop/hive/common/metrics/metrics2/TestCodahaleReportersConf.java b/standalone-metastore/metastore-common/src/test/java/org/apache/hadoop/hive/common/metrics/metrics2/TestCodahaleReportersConf.java
index 501bd5dc105..48454042923 100644
--- a/standalone-metastore/metastore-common/src/test/java/org/apache/hadoop/hive/common/metrics/metrics2/TestCodahaleReportersConf.java
+++ b/standalone-metastore/metastore-common/src/test/java/org/apache/hadoop/hive/common/metrics/metrics2/TestCodahaleReportersConf.java
@@ -19,8 +19,6 @@ package org.apache.hadoop.hive.common.metrics.metrics2;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import java.lang.reflect.InvocationTargetException;
-
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
 import org.apache.hadoop.hive.common.metrics.MetricsTestUtils;
@@ -29,8 +27,11 @@ import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.File;
+import java.lang.reflect.InvocationTargetException;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -39,6 +40,7 @@ import java.util.concurrent.TimeUnit;
 @org.junit.Ignore("HIVE-23945")
 public class TestCodahaleReportersConf {
 
+  private static final Logger LOGGER = LoggerFactory.getLogger(TestCodahaleReportersConf.class);
   private static File workDir = new File(System.getProperty("test.tmp.dir"));
   private static File jsonReportFile;
 
@@ -149,4 +151,31 @@ public class TestCodahaleReportersConf {
 
     Assert.assertFalse(jsonReportFile.exists());
   }
+
+  /**
+   * Tests if MetricsFactory is initialised properly when Metrics2Reporter is tried to add more than once.
+   *
+   */
+  @Test
+  public void testMetricsFactoryInitMetrics2ReporterAddedTwice() throws Exception {
+    Configuration conf = new Configuration();
+
+    jsonReportFile = File.createTempFile("TestCodahaleMetrics", ".json");
+    LOGGER.info("Json metrics saved in {}", jsonReportFile.getAbsolutePath());
+
+    conf.set(MetastoreConf.ConfVars.METRICS_CLASS.getHiveName(), CodahaleMetrics.class.getCanonicalName());
+    conf.set(MetastoreConf.ConfVars.HIVE_CODAHALE_METRICS_REPORTER_CLASSES.getHiveName(),
+            "org.apache.hadoop.hive.common.metrics.metrics2.Metrics2Reporter, "
+                    + "org.apache.hadoop.hive.common.metrics.metrics2.Metrics2Reporter");
+    conf.set(MetastoreConf.ConfVars.METRICS_JSON_FILE_LOCATION.getHiveName(), jsonReportFile.getAbsolutePath());
+    conf.setTimeDuration(MetastoreConf.ConfVars.METRICS_JSON_FILE_INTERVAL.getHiveName(), 2000,
+            TimeUnit.MILLISECONDS);
+
+    MetricsFactory.init(conf);
+    Assert.assertNotNull(MetricsFactory.getInstance());
+    // closing MetricsFactory and re-initiating it to check if everything works fine
+    MetricsFactory.close();
+    MetricsFactory.init(conf);
+    Assert.assertNotNull(MetricsFactory.getInstance());
+  }
 }