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 f0abfd4ff75..1e7149bf1a6 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 @@ -211,7 +211,7 @@ public void completeOpportunisticContainer(Resource res) { public void addResource(Resource res) { availableMB = availableMB + res.getMemorySize(); - availableGB.incr((int)Math.floor(availableMB/1024d)); + availableGB.set((int)Math.floor(availableMB/1024d)); availableVCores.incr(res.getVirtualCores()); } 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 a08ee82e759..6a6279a241a 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 @@ -86,6 +86,22 @@ checkMetrics(10, 1, 1, 1, 1, 1, 4, 7, 4, 13, 3); } + @Test + public void testAddResource(){ + DefaultMetricsSystem.initialize("NodeManager"); + NodeManagerMetrics metrics = NodeManagerMetrics.create(); + Resource total = Records.newRecord(Resource.class); + total.setMemorySize(8*GiB); + total.setVirtualCores(16); + metrics.addResource(total); + MetricsRecordBuilder rb = getMetrics("NodeManagerMetrics"); + assertGauge("AvailableGB", 8, rb); + // Update resource + metrics.addResource(total); + rb = getMetrics("NodeManagerMetrics"); + assertGauge("AvailableGB", 16, rb); + } + private void checkMetrics(int launched, int completed, int failed, int killed, int initing, int running, int allocatedGB, int allocatedContainers, int availableGB, int allocatedVCores,