Search in sources :

Example 61 with DynamicMBean

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;
    }
}
Also used : DynamicMBean(javax.management.DynamicMBean) StandardMBean(javax.management.StandardMBean) AttributeNotFoundException(javax.management.AttributeNotFoundException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 62 with DynamicMBean

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);
}
Also used : DynamicMBean(javax.management.DynamicMBean) IgniteEx(org.apache.ignite.internal.IgniteEx) SnapshotMXBean(org.apache.ignite.mxbean.SnapshotMXBean) Test(org.junit.Test)

Example 63 with DynamicMBean

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()));
}
Also used : DynamicMBean(javax.management.DynamicMBean) IgniteEx(org.apache.ignite.internal.IgniteEx) SnapshotMXBean(org.apache.ignite.mxbean.SnapshotMXBean) Test(org.junit.Test)

Example 64 with DynamicMBean

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));
}
Also used : DynamicMBean(javax.management.DynamicMBean) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) IgniteEx(org.apache.ignite.internal.IgniteEx) SingleNodeMessage(org.apache.ignite.internal.util.distributed.SingleNodeMessage) SnapshotMXBean(org.apache.ignite.mxbean.SnapshotMXBean) Test(org.junit.Test)

Example 65 with DynamicMBean

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();
}
Also used : DynamicMBean(javax.management.DynamicMBean) IgniteStandardMXBean(org.apache.ignite.internal.mxbean.IgniteStandardMXBean) MBeanRegistrationException(javax.management.MBeanRegistrationException) IgniteIllegalStateException(org.apache.ignite.IgniteIllegalStateException)

Aggregations

DynamicMBean (javax.management.DynamicMBean)78 MBeanException (javax.management.MBeanException)29 ObjectName (javax.management.ObjectName)24 MalformedObjectNameException (javax.management.MalformedObjectNameException)20 ManagedBean (org.apache.tomcat.util.modeler.ManagedBean)18 RuntimeOperationsException (javax.management.RuntimeOperationsException)16 Test (org.junit.Test)15 AttributeNotFoundException (javax.management.AttributeNotFoundException)14 InstanceNotFoundException (javax.management.InstanceNotFoundException)13 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)13 ListenerNotFoundException (javax.management.ListenerNotFoundException)13 ReflectionException (javax.management.ReflectionException)12 RuntimeErrorException (javax.management.RuntimeErrorException)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 Attribute (javax.management.Attribute)8 JMRuntimeException (javax.management.JMRuntimeException)7 MBeanRegistrationException (javax.management.MBeanRegistrationException)7 NamedObject (com.sun.jmx.mbeanserver.NamedObject)6 Method (java.lang.reflect.Method)6 RuntimeMBeanException (javax.management.RuntimeMBeanException)6