From 889bebf8bba18e2991bbb3082667c5e98b450021 Mon Sep 17 00:00:00 2001 From: Haibo Chen Date: Tue, 2 Jan 2018 10:04:56 -0800 Subject: [PATCH] YARN-7602. NM should reference the singleton JvmMetrics instance. (cherry picked from commit 2f6c038be6c23522ea64fc4e415910fb72493eb2) --- .../metrics2/source/TestJvmMetrics.java | 26 +++++++++++++++++++ .../metrics/NodeManagerMetrics.java | 6 ++--- .../metrics/TestNodeManagerMetrics.java | 25 ++++++++++++++++-- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/source/TestJvmMetrics.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/source/TestJvmMetrics.java index aa1b0096921..37a3a2affcc 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/source/TestJvmMetrics.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/source/TestJvmMetrics.java @@ -200,4 +200,30 @@ public class TestJvmMetrics { Assert.assertTrue(alerter.numAlerts > 0); Assert.assertTrue(alerter.maxGcTimePercentage >= alertGcPerc); } + + @Test + public void testJvmMetricsSingletonWithSameProcessName() { + JvmMetrics jvmMetrics1 = org.apache.hadoop.metrics2.source.JvmMetrics + .initSingleton("test", null); + JvmMetrics jvmMetrics2 = org.apache.hadoop.metrics2.source.JvmMetrics + .initSingleton("test", null); + Assert.assertEquals("initSingleton should return the singleton instance", + jvmMetrics1, jvmMetrics2); + } + + @Test + public void testJvmMetricsSingletonWithDifferentProcessNames() { + final String process1Name = "process1"; + JvmMetrics jvmMetrics1 = org.apache.hadoop.metrics2.source.JvmMetrics + .initSingleton(process1Name, null); + final String process2Name = "process2"; + JvmMetrics jvmMetrics2 = org.apache.hadoop.metrics2.source.JvmMetrics + .initSingleton(process2Name, null); + Assert.assertEquals("initSingleton should return the singleton instance", + jvmMetrics1, jvmMetrics2); + Assert.assertEquals("unexpected process name of the singleton instance", + process1Name, jvmMetrics1.processName); + Assert.assertEquals("unexpected process name of the singleton instance", + process1Name, jvmMetrics2.processName); + } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/metrics/NodeManagerMetrics.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/metrics/NodeManagerMetrics.java index 21e5140b91f..d963ba394e5 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/metrics/NodeManagerMetrics.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/metrics/NodeManagerMetrics.java @@ -86,7 +86,7 @@ public class NodeManagerMetrics { private long availableMB; private long allocatedOpportunisticMB; - public NodeManagerMetrics(JvmMetrics jvmMetrics) { + private NodeManagerMetrics(JvmMetrics jvmMetrics) { this.jvmMetrics = jvmMetrics; } @@ -94,8 +94,8 @@ public class NodeManagerMetrics { return create(DefaultMetricsSystem.instance()); } - static NodeManagerMetrics create(MetricsSystem ms) { - JvmMetrics jm = JvmMetrics.create("NodeManager", null, ms); + private static NodeManagerMetrics create(MetricsSystem ms) { + JvmMetrics jm = JvmMetrics.initSingleton("NodeManager", null); return ms.register(new NodeManagerMetrics(jm)); } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/metrics/TestNodeManagerMetrics.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/metrics/TestNodeManagerMetrics.java index 5dead916c28..d21e7ad7706 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/metrics/TestNodeManagerMetrics.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/metrics/TestNodeManagerMetrics.java @@ -19,19 +19,40 @@ package org.apache.hadoop.yarn.server.nodemanager.metrics; import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; import org.apache.hadoop.metrics2.MetricsRecordBuilder; +import org.apache.hadoop.metrics2.source.JvmMetrics; import static org.apache.hadoop.test.MetricsAsserts.*; import org.apache.hadoop.yarn.api.records.Resource; import org.apache.hadoop.yarn.util.Records; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; public class TestNodeManagerMetrics { static final int GiB = 1024; // MiB - @Test public void testNames() { + private NodeManagerMetrics metrics; + + @Before + public void setup() { DefaultMetricsSystem.initialize("NodeManager"); - NodeManagerMetrics metrics = NodeManagerMetrics.create(); + metrics = NodeManagerMetrics.create(); + } + + @After + public void tearDown() { + DefaultMetricsSystem.shutdown(); + } + + @Test + public void testReferenceOfSingletonJvmMetrics() { + JvmMetrics jvmMetrics = JvmMetrics.initSingleton("NodeManagerModule", null); + Assert.assertEquals("NodeManagerMetrics should reference the singleton" + + " JvmMetrics instance", jvmMetrics, metrics.getJvmMetrics()); + } + + @Test public void testNames() { Resource total = Records.newRecord(Resource.class); total.setMemorySize(8*GiB); total.setVirtualCores(16);