HADOOP-14954. MetricsSystemImpl#init should increment refCount when already initialized. Contributed by John Zhuge.

This commit is contained in:
John Zhuge 2017-10-16 13:29:29 -07:00 committed by John Zhuge
parent 281d83604d
commit 14b3c2695b
2 changed files with 57 additions and 1 deletions

View File

@ -140,6 +140,16 @@ public MetricsSystemImpl() {
this(null);
}
@VisibleForTesting
boolean isMonitoring() {
return monitoring;
}
@VisibleForTesting
int getRefCount() {
return refCount;
}
/**
* Initialized the metrics system with a prefix.
* @param prefix the system will look for configs with the prefix
@ -147,12 +157,12 @@ public MetricsSystemImpl() {
*/
@Override
public synchronized MetricsSystem init(String prefix) {
++refCount;
if (monitoring && !DefaultMetricsSystem.inMiniClusterMode()) {
LOG.warn(this.prefix +" metrics system already initialized!");
return this;
}
this.prefix = checkNotNull(prefix, "prefix");
++refCount;
if (monitoring) {
// in mini cluster mode
LOG.info(this.prefix +" metrics system started (again)");

View File

@ -26,6 +26,8 @@
import javax.annotation.Nullable;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -559,6 +561,50 @@ public void testRegisterSourceJmxCacheTTL() {
ms.shutdown();
}
@Test
public void testInitShutdown() {
boolean oldMode = DefaultMetricsSystem.inMiniClusterMode();
try {
DefaultMetricsSystem.setMiniClusterMode(true);
runInitShutdownTests();
DefaultMetricsSystem.setMiniClusterMode(false);
runInitShutdownTests();
} finally {
DefaultMetricsSystem.setMiniClusterMode(oldMode);
}
}
private void runInitShutdownTests() {
MetricsSystemImpl ms = new MetricsSystemImpl();
Assert.assertThat(ms.isMonitoring(), CoreMatchers.is(false));
Assert.assertThat(ms.getRefCount(), CoreMatchers.is(0));
ms.init("TestMetricsSystem1");
Assert.assertThat(ms.isMonitoring(), CoreMatchers.is(true));
Assert.assertThat(ms.getRefCount(), CoreMatchers.is(1));
ms.shutdown();
Assert.assertThat(ms.isMonitoring(), CoreMatchers.is(false));
Assert.assertThat(ms.getRefCount(), CoreMatchers.is(0));
ms.init("TestMetricsSystem2");
Assert.assertThat(ms.isMonitoring(), CoreMatchers.is(true));
Assert.assertThat(ms.getRefCount(), CoreMatchers.is(1));
ms.init("TestMetricsSystem3");
Assert.assertThat(ms.isMonitoring(), CoreMatchers.is(true));
Assert.assertThat(ms.getRefCount(), CoreMatchers.is(2));
ms.shutdown();
Assert.assertThat(ms.isMonitoring(), CoreMatchers.is(true));
Assert.assertThat(ms.getRefCount(), CoreMatchers.is(1));
ms.shutdown();
Assert.assertThat(ms.isMonitoring(), CoreMatchers.is(false));
Assert.assertThat(ms.getRefCount(), CoreMatchers.is(0));
}
@Metrics(context="test")
private static class TestSource {
@Metric("C1 desc") MutableCounterLong c1;