Updated Branches: refs/heads/trunk f9da62be2 -> 507ff1371
FLUME-2156. Unregister then re-register MonitoredCounterGroup JMX MBeans on reconfigure (Mike Percy via Hari Shreedharan) Project: http://git-wip-us.apache.org/repos/asf/flume/repo Commit: http://git-wip-us.apache.org/repos/asf/flume/commit/507ff137 Tree: http://git-wip-us.apache.org/repos/asf/flume/tree/507ff137 Diff: http://git-wip-us.apache.org/repos/asf/flume/diff/507ff137 Branch: refs/heads/trunk Commit: 507ff137160021dda2c4a08199746228827c9432 Parents: f9da62b Author: Hari Shreedharan <[email protected]> Authored: Sun Aug 11 00:11:48 2013 -0700 Committer: Hari Shreedharan <[email protected]> Committed: Sun Aug 11 00:12:55 2013 -0700 ---------------------------------------------------------------------- .../instrumentation/MonitoredCounterGroup.java | 20 +++++++++++++----- .../TestMonitoredCounterGroup.java | 22 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flume/blob/507ff137/flume-ng-core/src/main/java/org/apache/flume/instrumentation/MonitoredCounterGroup.java ---------------------------------------------------------------------- diff --git a/flume-ng-core/src/main/java/org/apache/flume/instrumentation/MonitoredCounterGroup.java b/flume-ng-core/src/main/java/org/apache/flume/instrumentation/MonitoredCounterGroup.java index c5c2956..1d3f0f1 100644 --- a/flume-ng-core/src/main/java/org/apache/flume/instrumentation/MonitoredCounterGroup.java +++ b/flume-ng-core/src/main/java/org/apache/flume/instrumentation/MonitoredCounterGroup.java @@ -29,6 +29,7 @@ import java.util.concurrent.atomic.AtomicLong; import javax.management.ObjectName; +import com.google.common.annotations.VisibleForTesting; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -95,20 +96,29 @@ public abstract class MonitoredCounterGroup { } /** - * Registers the counter. This method should be used only for testing, and - * there should be no need for any implementations to directly call this - * method. + * Registers the counter. + * This method is exposed only for testing, and there should be no need for + * any implementations to call this method directly. */ + @VisibleForTesting void register() { if (!registered) { try { ObjectName objName = new ObjectName("org.apache.flume." + type.name().toLowerCase() + ":type=" + this.name); + if (ManagementFactory.getPlatformMBeanServer().isRegistered(objName)) { + logger.debug("Monitored counter group for type: " + type + ", name: " + + name + ": Another MBean is already registered with this name. " + + "Unregistering that pre-existing MBean now..."); + ManagementFactory.getPlatformMBeanServer().unregisterMBean(objName); + logger.debug("Monitored counter group for type: " + type + ", name: " + + name + ": Successfully unregistered pre-existing MBean."); + } ManagementFactory.getPlatformMBeanServer().registerMBean(this, objName); + logger.info("Monitored counter group for type: " + type + ", name: " + + name + ": Successfully registered new MBean."); registered = true; - logger.info("Monitoried counter group for type: " + type + ", name: " + name - + ", registered successfully."); } catch (Exception ex) { logger.error("Failed to register monitored counter group for type: " + type + ", name: " + name, ex); http://git-wip-us.apache.org/repos/asf/flume/blob/507ff137/flume-ng-core/src/test/java/org/apache/flume/instrumentation/TestMonitoredCounterGroup.java ---------------------------------------------------------------------- diff --git a/flume-ng-core/src/test/java/org/apache/flume/instrumentation/TestMonitoredCounterGroup.java b/flume-ng-core/src/test/java/org/apache/flume/instrumentation/TestMonitoredCounterGroup.java index e417fb3..b1f637f 100644 --- a/flume-ng-core/src/test/java/org/apache/flume/instrumentation/TestMonitoredCounterGroup.java +++ b/flume-ng-core/src/test/java/org/apache/flume/instrumentation/TestMonitoredCounterGroup.java @@ -314,6 +314,28 @@ public class TestMonitoredCounterGroup { 0L, 0L, 0L, 0L); } + @Test + public void testRegisterTwice() throws Exception { + String name = "re-register-" + getRandomName(); + + SourceCounter c1 = new SourceCounter(name); + c1.register(); + ObjectName on = new ObjectName(SOURCE_OBJ_NAME_PREFIX + name); + + Assert.assertEquals("StartTime", 0L, getStartTime(on)); + Assert.assertEquals("StopTime", 0L, getStopTime(on)); + c1.start(); + c1.stop(); + Assert.assertTrue("StartTime", getStartTime(on) > 0L); + Assert.assertTrue("StopTime", getStopTime(on) > 0L); + + SourceCounter c2 = new SourceCounter(name); + c2.register(); + + Assert.assertEquals("StartTime", 0L, getStartTime(on)); + Assert.assertEquals("StopTime", 0L, getStopTime(on)); + } + private void assertSrcCounterState(ObjectName on, long eventReceivedCount, long eventAcceptedCount, long appendReceivedCount, long appendAcceptedCount, long appendBatchReceivedCount,
