use of javax.management.DynamicMBean in project atlasdb by palantir.
the class JMXUtils method registerMBeanWeakRefPlusCatchAndLogExceptions.
/**
* You must retain a reference to the returned {@link DynamicMBean} for as
* long as you wish this bean to be registered.
* <p>
* Some mbeans have references to some pretty big classes and we have no
* good way to de-register these beans because these objects don't have good
* life-cyle management.
* <p>
* This method will register your mbean with JMX but JMX will hold onto it
* weakly so the large object may be GC'ed as usual.
* <p>
* When there is no more reference to the underlying MBean from anywhere
* else it may be freed. If this happens, the next call to this objectName
* will fail with an {@link IllegalStateException}. When this happens, this
* objectName will also be unregisterd with
* {@link #unregisterMBeanCatchAndLogExceptions(String)}
* <p>
* Because of the weird JMX naming conventions, this method uses
* {@link StandardMBean} to proxy the bean as a {@link DynamicMBean} to get
* it through the framework
*
* @return the DynamicMBean whose lifecycle controls how long this bean is
* registered. null will be returned if this bean is not regsitered
* correctly
*/
public static <T> DynamicMBean registerMBeanWeakRefPlusCatchAndLogExceptions(final T mbean, final Class<T> clazz, final String objectName) {
final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
try {
final DynamicMBean bean = new StandardMBean(mbean, clazz);
final DynamicMBean weakMBean = new WeakMBeanHandler(objectName, bean);
final ObjectName on = new ObjectName(objectName);
server.registerMBean(weakMBean, on);
return bean;
} catch (final Exception e) {
AssertUtils.assertAndLogWithException(log, false, "Failed to register mbean for name " + objectName, e);
return null;
}
}
use of javax.management.DynamicMBean in project ignite by apache.
the class IgniteSnapshotMXBeanTest method testRestoreSnapshot.
/**
* @throws Exception If fails.
*/
@Test
public void testRestoreSnapshot() throws Exception {
// TODO IGNITE-14999 Support dynamic restoration of encrypted snapshots.
if (encryption)
return;
IgniteEx ignite = startGridsWithSnapshot(2, CACHE_KEYS_RANGE, false);
DynamicMBean mReg0 = metricRegistry(grid(0).name(), null, SNAPSHOT_RESTORE_METRICS);
DynamicMBean mReg1 = metricRegistry(grid(1).name(), null, SNAPSHOT_RESTORE_METRICS);
assertEquals(0, (long) getMetric("endTime", mReg0));
assertEquals(0, (long) getMetric("endTime", mReg1));
getMxBean(ignite.name(), METRIC_GROUP, SnapshotMXBeanImpl.class, SnapshotMXBean.class).restoreSnapshot(SNAPSHOT_NAME, null);
assertTrue(GridTestUtils.waitForCondition(() -> (long) getMetric("endTime", mReg0) > 0, TIMEOUT));
assertTrue(GridTestUtils.waitForCondition(() -> (long) getMetric("endTime", mReg1) > 0, TIMEOUT));
assertCacheKeys(ignite.cache(DEFAULT_CACHE_NAME), CACHE_KEYS_RANGE);
}
use of javax.management.DynamicMBean in project ignite by apache.
the class IgniteSnapshotMXBeanTest method testCreateSnapshot.
/**
* @throws Exception If fails.
*/
@Test
public void testCreateSnapshot() throws Exception {
IgniteEx ignite = startGridsWithCache(2, dfltCacheCfg, CACHE_KEYS_RANGE);
DynamicMBean snpMBean = metricRegistry(ignite.name(), null, SNAPSHOT_METRICS);
assertEquals("Snapshot end time must be undefined on first snapshot operation starts.", 0, (long) getMetric("LastSnapshotEndTime", snpMBean));
SnapshotMXBean mxBean = getMxBean(ignite.name(), METRIC_GROUP, SnapshotMXBeanImpl.class, SnapshotMXBean.class);
mxBean.createSnapshot(SNAPSHOT_NAME);
assertTrue("Waiting for snapshot operation failed.", GridTestUtils.waitForCondition(() -> (long) getMetric("LastSnapshotEndTime", snpMBean) > 0, TIMEOUT));
stopAllGrids();
IgniteEx snp = startGridsFromSnapshot(2, SNAPSHOT_NAME);
assertSnapshotCacheKeys(snp.cache(dfltCacheCfg.getName()));
}
use of javax.management.DynamicMBean in project ignite by apache.
the class IgniteSnapshotMXBeanTest method testCancelRestoreSnapshot.
/**
* @throws Exception If fails.
*/
@Test
public void testCancelRestoreSnapshot() throws Exception {
// TODO IGNITE-14999 Support dynamic restoration of encrypted snapshots.
if (encryption)
return;
IgniteEx ignite = startGridsWithSnapshot(2, CACHE_KEYS_RANGE, false);
SnapshotMXBean mxBean = getMxBean(ignite.name(), METRIC_GROUP, SnapshotMXBeanImpl.class, SnapshotMXBean.class);
DynamicMBean mReg0 = metricRegistry(grid(0).name(), null, SNAPSHOT_RESTORE_METRICS);
DynamicMBean mReg1 = metricRegistry(grid(1).name(), null, SNAPSHOT_RESTORE_METRICS);
assertEquals("", getMetric("error", mReg0));
assertEquals("", getMetric("error", mReg1));
assertEquals(0, (long) getMetric("endTime", mReg0));
assertEquals(0, (long) getMetric("endTime", mReg1));
TestRecordingCommunicationSpi spi = TestRecordingCommunicationSpi.spi(grid(1));
spi.blockMessages((node, msg) -> msg instanceof SingleNodeMessage && ((SingleNodeMessage<?>) msg).type() == RESTORE_CACHE_GROUP_SNAPSHOT_PREPARE.ordinal());
IgniteFuture<Void> fut = ignite.snapshot().restoreSnapshot(SNAPSHOT_NAME, null);
spi.waitForBlocked();
IgniteInternalFuture<Boolean> interruptFut = GridTestUtils.runAsync(() -> {
try {
return GridTestUtils.waitForCondition(() -> !"".equals(getMetric("error", mReg0)) && !"".equals(getMetric("error", mReg1)), TIMEOUT);
} finally {
spi.stopBlock();
}
});
mxBean.cancelSnapshotRestore(SNAPSHOT_NAME);
assertTrue(interruptFut.get());
String expErrMsg = "Operation has been canceled by the user.";
assertThrowsAnyCause(log, () -> fut.get(TIMEOUT), IgniteCheckedException.class, expErrMsg);
assertTrue((long) getMetric("endTime", mReg0) > 0);
assertTrue((long) getMetric("endTime", mReg1) > 0);
assertTrue(((String) getMetric("error", mReg0)).contains(expErrMsg));
assertTrue(((String) getMetric("error", mReg1)).contains(expErrMsg));
assertNull(ignite.cache(DEFAULT_CACHE_NAME));
}
use of javax.management.DynamicMBean in project ignite by apache.
the class IgniteUtils method registerMBean.
/**
* Registers MBean with the server.
*
* @param <T> Type of mbean.
* @param mbeanSrv MBean server.
* @param name MBean object name.
* @param impl MBean implementation.
* @param itf MBean interface.
* @return JMX object name.
* @throws MBeanRegistrationException if MBeans are disabled.
* @throws JMException If MBean creation failed.
* @throws IgniteException If MBean creation are not allowed.
*/
public static <T> ObjectName registerMBean(MBeanServer mbeanSrv, ObjectName name, T impl, Class<T> itf) throws JMException {
if (IGNITE_MBEANS_DISABLED)
throw new MBeanRegistrationException(new IgniteIllegalStateException("MBeans are disabled."));
assert mbeanSrv != null;
assert name != null;
assert itf != null;
DynamicMBean mbean;
if (impl instanceof DynamicMBean) {
mbean = (DynamicMBean) impl;
} else {
mbean = new IgniteStandardMXBean(impl, itf);
mbean.getMBeanInfo();
}
return mbeanSrv.registerMBean(mbean, name).getObjectName();
}
Aggregations