Search in sources :

Example 1 with ReadOnlyMetricManager

use of org.apache.ignite.spi.metric.ReadOnlyMetricManager in project ignite by apache.

the class MetricsSelfTest method testAddBeforeRemoveCompletes.

/**
 */
@Test
public void testAddBeforeRemoveCompletes() throws Exception {
    MetricExporterSpi checkSpi = new NoopMetricExporterSpi() {

        private ReadOnlyMetricManager registry;

        private Set<String> names = new HashSet<>();

        @Override
        public void spiStart(@Nullable String igniteInstanceName) throws IgniteSpiException {
            registry.addMetricRegistryCreationListener(mreg -> {
                assertFalse(mreg.name() + " should be unique", names.contains(mreg.name()));
                names.add(mreg.name());
            });
            registry.addMetricRegistryRemoveListener(mreg -> names.remove(mreg.name()));
        }

        @Override
        public void setMetricRegistry(ReadOnlyMetricManager registry) {
            this.registry = registry;
        }
    };
    CountDownLatch rmvStarted = new CountDownLatch(1);
    CountDownLatch rmvCompleted = new CountDownLatch(1);
    MetricExporterSpi blockingSpi = new NoopMetricExporterSpi() {

        private ReadOnlyMetricManager registry;

        @Override
        public void spiStart(@Nullable String igniteInstanceName) throws IgniteSpiException {
            registry.addMetricRegistryRemoveListener(mreg -> {
                rmvStarted.countDown();
                try {
                    rmvCompleted.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
        }

        @Override
        public void setMetricRegistry(ReadOnlyMetricManager registry) {
            this.registry = registry;
        }
    };
    IgniteConfiguration cfg = new IgniteConfiguration().setMetricExporterSpi(blockingSpi, checkSpi);
    GridTestKernalContext ctx = new GridTestKernalContext(log(), cfg);
    ctx.start();
    // Add metric registry.
    ctx.metric().registry("test");
    // Removes it async, blockingSpi will block remove procedure.
    IgniteInternalFuture rmvFut = runAsync(() -> ctx.metric().remove("test"));
    rmvStarted.await();
    CountDownLatch addStarted = new CountDownLatch(1);
    IgniteInternalFuture addFut = runAsync(() -> {
        addStarted.countDown();
        ctx.metric().registry("test");
    });
    // Waiting for creation to start.
    addStarted.await();
    Thread.sleep(100);
    // Complete removal.
    rmvCompleted.countDown();
    rmvFut.get(getTestTimeout());
    addFut.get(getTestTimeout());
}
Also used : HashSet(java.util.HashSet) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) ReadOnlyMetricManager(org.apache.ignite.spi.metric.ReadOnlyMetricManager) GridTestKernalContext(org.apache.ignite.testframework.junits.GridTestKernalContext) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) NoopMetricExporterSpi(org.apache.ignite.spi.metric.noop.NoopMetricExporterSpi) NoopMetricExporterSpi(org.apache.ignite.spi.metric.noop.NoopMetricExporterSpi) MetricExporterSpi(org.apache.ignite.spi.metric.MetricExporterSpi) Nullable(org.jetbrains.annotations.Nullable) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

HashSet (java.util.HashSet)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 MetricExporterSpi (org.apache.ignite.spi.metric.MetricExporterSpi)1 ReadOnlyMetricManager (org.apache.ignite.spi.metric.ReadOnlyMetricManager)1 NoopMetricExporterSpi (org.apache.ignite.spi.metric.noop.NoopMetricExporterSpi)1 GridTestKernalContext (org.apache.ignite.testframework.junits.GridTestKernalContext)1 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)1 Nullable (org.jetbrains.annotations.Nullable)1 Test (org.junit.Test)1