From 53bc888f0b6d2b1cc635d6d2e21900008a5fadb9 Mon Sep 17 00:00:00 2001 From: Ivan Mitic Date: Thu, 26 Dec 2013 19:42:24 +0000 Subject: [PATCH] HADOOP-10090. Jobtracker metrics not updated properly after execution of a mapreduce job. Contributed by Ivan Mitic. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1553561 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 3 + .../impl/TestMetricsSourceAdapter.java | 87 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/impl/TestMetricsSourceAdapter.java diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index dcc8f5bde6..11b567e698 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -568,6 +568,9 @@ Release 2.3.0 - UNRELEASED HADOOP-10175. Har files system authority should preserve userinfo. (Chuan Liu via cnauroth) + HADOOP-10090. Jobtracker metrics not updated properly after execution + of a mapreduce job. (ivanmi) + Release 2.2.0 - 2013-10-13 INCOMPATIBLE CHANGES diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/impl/TestMetricsSourceAdapter.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/impl/TestMetricsSourceAdapter.java new file mode 100644 index 0000000000..724d449fcc --- /dev/null +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/impl/TestMetricsSourceAdapter.java @@ -0,0 +1,87 @@ +/** + * 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.metrics2.impl; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.hadoop.metrics2.MetricsSource; +import org.apache.hadoop.metrics2.MetricsTag; +import org.apache.hadoop.metrics2.annotation.Metric; +import org.apache.hadoop.metrics2.annotation.Metrics; +import org.apache.hadoop.metrics2.lib.MetricsAnnotations; +import org.apache.hadoop.metrics2.lib.MetricsRegistry; +import org.apache.hadoop.metrics2.lib.MetricsSourceBuilder; +import org.apache.hadoop.metrics2.lib.MutableCounterLong; +import org.junit.Test; + +public class TestMetricsSourceAdapter { + + @Test + public void testGetMetricsAndJmx() throws Exception { + // create test source with a single metric counter of value 0 + TestSource source = new TestSource("test"); + MetricsSourceBuilder sb = MetricsAnnotations.newSourceBuilder(source); + final MetricsSource s = sb.build(); + + List injectedTags = new ArrayList(); + MetricsSourceAdapter sa = new MetricsSourceAdapter( + "test", "test", "test desc", s, injectedTags, null, null, 1, false); + + // all metrics are initially assumed to have changed + MetricsCollectorImpl builder = new MetricsCollectorImpl(); + Iterable metricsRecords = sa.getMetrics(builder, true); + + // Validate getMetrics and JMX initial values + MetricsRecordImpl metricsRecord = metricsRecords.iterator().next(); + assertEquals(0L, + metricsRecord.metrics().iterator().next().value().longValue()); + + Thread.sleep(100); // skip JMX cache TTL + assertEquals(0L, (Number)sa.getAttribute("C1")); + + // change metric value + source.incrementCnt(); + + // validate getMetrics and JMX + builder = new MetricsCollectorImpl(); + metricsRecords = sa.getMetrics(builder, true); + metricsRecord = metricsRecords.iterator().next(); + assertTrue(metricsRecord.metrics().iterator().hasNext()); + Thread.sleep(100); // skip JMX cache TTL + assertEquals(1L, (Number)sa.getAttribute("C1")); + } + + @SuppressWarnings("unused") + @Metrics(context="test") + private static class TestSource { + @Metric("C1 desc") MutableCounterLong c1; + final MetricsRegistry registry; + + TestSource(String recName) { + registry = new MetricsRegistry(recName); + } + + public void incrementCnt() { + c1.incr(); + } + } +}