Search in sources :

Example 1 with LifecycleBean

use of org.apache.ignite.lifecycle.LifecycleBean in project ignite by apache.

the class SpringCacheManagerContextInjectionTest method testBeanInjectionUsingConfigPath.

/**
 * @throws Exception If failed.
 */
public void testBeanInjectionUsingConfigPath() throws Exception {
    new AnnotationConfigApplicationContext(TestPathConfiguration.class);
    Ignite grid = IgnitionEx.grid("springInjectionTest");
    IgniteConfiguration cfg = grid.configuration();
    LifecycleBean[] beans = cfg.getLifecycleBeans();
    assertEquals(2, beans.length);
    TestInjectionLifecycleBean bean1 = (TestInjectionLifecycleBean) beans[0];
    TestInjectionLifecycleBean bean2 = (TestInjectionLifecycleBean) beans[1];
    bean1.checkState();
    bean2.checkState();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) LifecycleBean(org.apache.ignite.lifecycle.LifecycleBean) TestInjectionLifecycleBean(org.apache.ignite.TestInjectionLifecycleBean) Ignite(org.apache.ignite.Ignite) TestInjectionLifecycleBean(org.apache.ignite.TestInjectionLifecycleBean)

Example 2 with LifecycleBean

use of org.apache.ignite.lifecycle.LifecycleBean in project ignite by apache.

the class IgniteExchangeLatchManagerDiscoHistoryTest method testProperException.

/**
 * @throws Exception If failed.
 */
@Test
@WithSystemProperty(key = IGNITE_DISCOVERY_HISTORY_SIZE, value = DISCO_HISTORY_SIZE)
public void testProperException() throws Exception {
    final IgniteEx crd = startGrid(0);
    final CountDownLatch exchangeLatch = new CountDownLatch(1);
    final CountDownLatch startSrvsLatch = new CountDownLatch(1);
    final AtomicReference<Exception> err = new AtomicReference<>();
    // Lifecycle bean that is used to register PartitionsExchangeAware listener.
    lifecycleBean = new LifecycleBean() {

        /**
         * Ignite instance.
         */
        @IgniteInstanceResource
        IgniteEx ignite;

        /**
         * {@inheritDoc}
         */
        @Override
        public void onLifecycleEvent(LifecycleEventType evt) throws IgniteException {
            if (evt == LifecycleEventType.BEFORE_NODE_START) {
                // The goal is registering PartitionsExchangeAware listener before the discovery manager is started.
                ignite.context().internalSubscriptionProcessor().registerDistributedMetastorageListener(new DistributedMetastorageLifecycleListener() {

                    @Override
                    public void onReadyForRead(ReadableDistributedMetaStorage metastorage) {
                        ignite.context().cache().context().exchange().registerExchangeAwareComponent(new PartitionsExchangeAware() {

                            /**
                             * {@inheritDoc}
                             */
                            @Override
                            public void onInitBeforeTopologyLock(GridDhtPartitionsExchangeFuture fut) {
                                try {
                                    // Let's start nodes.
                                    startSrvsLatch.countDown();
                                    // Blocks the initial exchange and waits for other nodes.
                                    exchangeLatch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
                                } catch (Exception e) {
                                    err.compareAndSet(null, e);
                                }
                            }
                        });
                    }
                });
            }
        }
    };
    // Start server node with short topology history.
    victim = true;
    GridTestUtils.runAsync(() -> startGrid(1));
    // Waits for the initial exchange.
    startSrvsLatch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
    victim = false;
    lifecycleBean = null;
    List<IgniteInternalFuture> srvFuts = new ArrayList<>(TOPOLOGY_HISTORY_SIZE);
    try {
        // Major topology version that is corresponding to the start of the node with short topology history.
        final long topVer = 2;
        // Starting server nodes to exhaust the topology history.
        for (int i = 2; i < 3 * TOPOLOGY_HISTORY_SIZE && !disco.isEmptyTopologyHistory(topVer); ++i) {
            final int currNodeIdx = i;
            final int joinedNodesCnt = disco.totalJoinedNodes();
            srvFuts.add(GridTestUtils.runAsync(() -> startGrid(currNodeIdx)));
            assertTrue("Failed to wait for a new server node [joinedNodesCnt=" + joinedNodesCnt + "]", GridTestUtils.waitForCondition(() -> disco.totalJoinedNodes() >= (joinedNodesCnt + 1), DEFAULT_TIMEOUT));
        }
        assertTrue("Disco cache history is not empty for the topology [majorTopVer=" + topVer + ']', disco.isEmptyTopologyHistory(topVer));
        // Let's continue the ongoing exchange.
        exchangeLatch.countDown();
        boolean failureHnd = GridTestUtils.waitForCondition(() -> cpFailureCtx.get() != null, DEFAULT_TIMEOUT);
        assertNull("Unexpected exception (probably, the topology history still exists [err=" + err + ']', err.get());
        assertTrue("Failure handler was not triggered.", failureHnd);
        // Check that IgniteException was thrown instead of NullPointerException.
        assertTrue("IgniteException must be thrown.", X.hasCause(cpFailureCtx.get().error(), IgniteException.class));
        // Check that message contains a hint to fix the issue.
        GridTestUtils.assertContains(log, cpFailureCtx.get().error().getMessage(), "Consider increasing IGNITE_DISCOVERY_HISTORY_SIZE property. Current value is " + DISCO_HISTORY_SIZE);
    } finally {
        IgnitionEx.stop(getTestIgniteInstanceName(1), true, ShutdownPolicy.IMMEDIATE, true);
        srvFuts.forEach(f -> {
            try {
                f.get(DEFAULT_TIMEOUT);
            } catch (IgniteCheckedException e) {
                err.compareAndSet(null, e);
            }
        });
    }
    assertNull("Unexpected exception [err=" + err.get() + ']', err.get());
}
Also used : ReadableDistributedMetaStorage(org.apache.ignite.internal.processors.metastorage.ReadableDistributedMetaStorage) GridDhtPartitionsExchangeFuture(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) PartitionsExchangeAware(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.PartitionsExchangeAware) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteException(org.apache.ignite.IgniteException) IgniteEx(org.apache.ignite.internal.IgniteEx) LifecycleBean(org.apache.ignite.lifecycle.LifecycleBean) DistributedMetastorageLifecycleListener(org.apache.ignite.internal.processors.metastorage.DistributedMetastorageLifecycleListener) LifecycleEventType(org.apache.ignite.lifecycle.LifecycleEventType) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test) WithSystemProperty(org.apache.ignite.testframework.junits.WithSystemProperty)

Example 3 with LifecycleBean

use of org.apache.ignite.lifecycle.LifecycleBean in project ignite by apache.

the class GridCachePartitionExchangeManagerWarningsTest method testDumpLongRunningOperationsWaitForFullyInitializedExchangeManager.

/**
 */
@Test
public void testDumpLongRunningOperationsWaitForFullyInitializedExchangeManager() throws Exception {
    long waitingTimeout = 5_000;
    PrintStream errStream = System.err;
    CountDownLatch startLatch = new CountDownLatch(1);
    CountDownLatch waitLatch = new CountDownLatch(1);
    try {
        // GridCachePartitionExchangeManager#dumpLongRunningOperations() uses diagnostic log,
        // which can be non-initialized, and so, error messgaes are logged into standard error output stream.
        ByteArrayOutputStream testOut = new ByteArrayOutputStream(16 * 1024);
        System.setErr(new PrintStream(testOut));
        AtomicReference<IgniteInternalFuture<?>> dumpOpsFut = new AtomicReference<>();
        IgniteInternalFuture<Ignite> startFut = null;
        // Lyficycle bean allows to register DatabaseLifecycleListener and trigger dumpLongRunningOperations
        // before GridCachePartitionExchangeManager is started.
        lifecycleBean = new LifecycleBean() {

            /**
             * Ignite instance.
             */
            @IgniteInstanceResource
            IgniteEx ignite;

            /**
             * {@inheritDoc}
             */
            @Override
            public void onLifecycleEvent(LifecycleEventType evt) throws IgniteException {
                if (evt == LifecycleEventType.BEFORE_NODE_START) {
                    ignite.context().internalSubscriptionProcessor().registerDatabaseListener(new DatabaseLifecycleListener() {

                        @Override
                        public void onInitDataRegions(IgniteCacheDatabaseSharedManager mgr) throws IgniteCheckedException {
                            dumpOpsFut.set(GridTestUtils.runAsync(() -> ignite.context().cache().context().exchange().dumpLongRunningOperations(1_000)));
                            // Let's allow to check that dumpLongRunningOperations is triggered.
                            startLatch.countDown();
                            // Wait for the check
                            try {
                                if (!waitLatch.await(waitingTimeout * 3, TimeUnit.MILLISECONDS))
                                    throw new IgniteCheckedException("Failed to wait for a check of dumpLongRunningOperations");
                            } catch (InterruptedException e) {
                                throw new IgniteCheckedException(e);
                            }
                        }
                    });
                }
            }
        };
        startFut = GridTestUtils.runAsync(new Callable<Ignite>() {

            @Override
            public Ignite call() throws Exception {
                return startGrid(0);
            }
        });
        assertTrue("Server node did not start in " + waitingTimeout + " ms.", startLatch.await(waitingTimeout, TimeUnit.MILLISECONDS));
        // Check that dumpLongRunningOperations did not produce any error.
        if (GridTestUtils.waitForCondition(() -> dumpOpsFut.get().isDone(), waitingTimeout)) {
            // Check that error output stream does not contain NullPointerException.
            String output = testOut.toString();
            assertTrue("Unexpected error [err=" + output + ']', output.isEmpty());
        }
        // Unblock starting the node.
        waitLatch.countDown();
        assertTrue("Dumping log running operations is not completed yet.", GridTestUtils.waitForCondition(() -> dumpOpsFut.get().isDone(), waitingTimeout));
        // Check that error output stream does not contain any error.
        String output = testOut.toString();
        assertTrue("Unexpected error [err=" + output + ']', output.isEmpty());
        startFut.get(waitingTimeout, TimeUnit.MILLISECONDS);
    } finally {
        startLatch.countDown();
        waitLatch.countDown();
        System.setErr(errStream);
    }
}
Also used : PrintStream(java.io.PrintStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteException(org.apache.ignite.IgniteException) LifecycleBean(org.apache.ignite.lifecycle.LifecycleBean) Ignite(org.apache.ignite.Ignite) LifecycleEventType(org.apache.ignite.lifecycle.LifecycleEventType) DatabaseLifecycleListener(org.apache.ignite.internal.processors.cache.persistence.DatabaseLifecycleListener) IgniteCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.IgniteCacheDatabaseSharedManager) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 4 with LifecycleBean

use of org.apache.ignite.lifecycle.LifecycleBean in project ignite by apache.

the class GridCacheDhtPreloadUnloadSelfTest method testUnloadOneBackThreeNodesWithLifeCycleBean.

/**
 * @throws Exception If failed.
 */
@Test
public void testUnloadOneBackThreeNodesWithLifeCycleBean() throws Exception {
    preloadMode = SYNC;
    backups = 1;
    try {
        final int cnt = 1000;
        lbean = new LifecycleBean() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public void onLifecycleEvent(LifecycleEventType evt) {
                if (evt == LifecycleEventType.AFTER_NODE_START) {
                    IgniteCache<Integer, String> c = ignite.cache(DEFAULT_CACHE_NAME);
                    if (c.putIfAbsent(-1, "true")) {
                        populate(ignite.<Integer, String>cache(DEFAULT_CACHE_NAME), cnt);
                        info(">>> POPULATED GRID <<<");
                    }
                }
            }
        };
        int gridCnt = 3;
        for (int i = 0; i < gridCnt; i++) {
            startGrid(i);
            for (int j = 0; j < i; j++) info("Grid size [i=" + i + ", size=" + grid(j).cache(DEFAULT_CACHE_NAME).localSize() + ']');
        }
        long wait = 3000;
        waitForUnload(gridCnt, cnt, wait);
    } finally {
        lbean = null;
        stopAllGrids();
    }
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteCache(org.apache.ignite.IgniteCache) LifecycleBean(org.apache.ignite.lifecycle.LifecycleBean) Ignite(org.apache.ignite.Ignite) LifecycleEventType(org.apache.ignite.lifecycle.LifecycleEventType) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 5 with LifecycleBean

use of org.apache.ignite.lifecycle.LifecycleBean in project ignite by apache.

the class SpringTransactionManagerContextInjectionTest method testBeanInjectionUsingConfigPath.

/**
 * @throws Exception If failed.
 */
public void testBeanInjectionUsingConfigPath() throws Exception {
    BeanFactory factory = new AnnotationConfigApplicationContext(TestPathConfiguration.class);
    Ignite grid = IgnitionEx.grid("springInjectionTest");
    IgniteConfiguration cfg = grid.configuration();
    LifecycleBean[] beans = cfg.getLifecycleBeans();
    assertEquals(2, beans.length);
    TestInjectionLifecycleBean bean1 = (TestInjectionLifecycleBean) beans[0];
    TestInjectionLifecycleBean bean2 = (TestInjectionLifecycleBean) beans[1];
    bean1.checkState();
    bean2.checkState();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BeanFactory(org.springframework.beans.factory.BeanFactory) LifecycleBean(org.apache.ignite.lifecycle.LifecycleBean) TestInjectionLifecycleBean(org.apache.ignite.TestInjectionLifecycleBean) Ignite(org.apache.ignite.Ignite) TestInjectionLifecycleBean(org.apache.ignite.TestInjectionLifecycleBean)

Aggregations

LifecycleBean (org.apache.ignite.lifecycle.LifecycleBean)10 Ignite (org.apache.ignite.Ignite)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 IgniteException (org.apache.ignite.IgniteException)4 LifecycleEventType (org.apache.ignite.lifecycle.LifecycleEventType)3 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)3 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)3 Test (org.junit.Test)3 RuntimeMXBean (java.lang.management.RuntimeMXBean)2 DecimalFormat (java.text.DecimalFormat)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 IgniteSystemProperties.getBoolean (org.apache.ignite.IgniteSystemProperties.getBoolean)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 GridCheckpointManager (org.apache.ignite.internal.managers.checkpoint.GridCheckpointManager)2 GridCollisionManager (org.apache.ignite.internal.managers.collision.GridCollisionManager)2 GridIoManager (org.apache.ignite.internal.managers.communication.GridIoManager)2 GridDeploymentManager (org.apache.ignite.internal.managers.deployment.GridDeploymentManager)2