Search in sources :

Example 1 with IgniteDiscoverySpi

use of org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi in project ignite by apache.

the class ClusterCachesInfo method validateNoNewCachesWithNewFormat.

/**
 * Validates that joining node doesn't have newly configured caches
 * in case when there is no cluster-wide support of SPLITTED_CACHE_CONFIGURATIONS.
 *
 * If validation is failed that caches will be destroyed cluster-wide and node joining process will be failed.
 *
 * @param clusterWideCacheData Cluster wide cache data.
 */
public void validateNoNewCachesWithNewFormat(CacheNodeCommonDiscoveryData clusterWideCacheData) {
    IgniteDiscoverySpi spi = (IgniteDiscoverySpi) ctx.discovery().getInjectedDiscoverySpi();
    boolean allowSplitCacheConfigurations = spi.allNodesSupport(IgniteFeatures.SPLITTED_CACHE_CONFIGURATIONS);
    if (!allowSplitCacheConfigurations) {
        List<String> cachesToDestroy = new ArrayList<>();
        for (DynamicCacheDescriptor cacheDescriptor : registeredCaches().values()) {
            CacheData clusterCacheData = clusterWideCacheData.caches().get(cacheDescriptor.cacheName());
            // Node spawned new cache.
            if (clusterCacheData.receivedFrom().equals(cacheDescriptor.receivedFrom()))
                cachesToDestroy.add(cacheDescriptor.cacheName());
        }
        if (!cachesToDestroy.isEmpty()) {
            ctx.cache().dynamicDestroyCaches(cachesToDestroy, false);
            throw new IllegalStateException("Node can't join to cluster in compatibility mode with newly configured caches: " + cachesToDestroy);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteDiscoverySpi(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi)

Example 2 with IgniteDiscoverySpi

use of org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi in project ignite by apache.

the class IgniteClientReconnectFailoverAbstractTest method reconnectFailover.

/**
 * @param c Test closure.
 * @throws Exception If failed.
 */
protected final void reconnectFailover(final Callable<Void> c) throws Exception {
    final Ignite client = grid(serverCount());
    assertTrue(client.cluster().localNode().isClient());
    Ignite srv = ignite(0);
    IgniteDiscoverySpi srvSpi = spi0(srv);
    final AtomicBoolean stop = new AtomicBoolean(false);
    final IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                int iter = 0;
                while (!stop.get()) {
                    try {
                        c.call();
                    } catch (CacheException e) {
                        checkAndWait(e);
                    } catch (IgniteClientDisconnectedException e) {
                        checkAndWait(e);
                    }
                    if (++iter % 100 == 0)
                        log.info("Iteration: " + iter);
                    if (barrier != null)
                        barrier.await();
                }
                return null;
            } catch (Throwable e) {
                log.error("Unexpected error in operation thread: " + e, e);
                stop.set(true);
                throw e;
            }
        }
    }, THREADS, "test-operation-thread");
    final AtomicReference<CountDownLatch> disconnected = new AtomicReference<>();
    final AtomicReference<CountDownLatch> reconnected = new AtomicReference<>();
    IgnitePredicate<Event> p = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
                info("Reconnected: " + evt);
                CountDownLatch latch = reconnected.get();
                assertNotNull(latch);
                assertEquals(1, latch.getCount());
                latch.countDown();
            } else if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) {
                info("Disconnected: " + evt);
                CountDownLatch latch = disconnected.get();
                assertNotNull(latch);
                assertEquals(1, latch.getCount());
                latch.countDown();
            }
            return true;
        }
    };
    client.events().localListen(p, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
    try {
        long stopTime = System.currentTimeMillis() + TEST_TIME;
        String err = null;
        while (System.currentTimeMillis() < stopTime && !fut.isDone()) {
            U.sleep(500);
            CountDownLatch disconnectLatch = new CountDownLatch(1);
            CountDownLatch reconnectLatch = new CountDownLatch(1);
            disconnected.set(disconnectLatch);
            reconnected.set(reconnectLatch);
            UUID nodeId = client.cluster().localNode().id();
            log.info("Fail client: " + nodeId);
            srvSpi.failNode(nodeId, null);
            if (!disconnectLatch.await(10_000, MILLISECONDS)) {
                err = "Failed to wait for disconnect";
                break;
            }
            if (!reconnectLatch.await(10_000, MILLISECONDS)) {
                err = "Failed to wait for reconnect";
                break;
            }
            barrier = new CyclicBarrier(THREADS + 1, new Runnable() {

                @Override
                public void run() {
                    barrier = null;
                }
            });
            try {
                barrier.await(10, SECONDS);
            } catch (TimeoutException ignored) {
                err = "Operations hang or fail with unexpected error.";
                break;
            }
        }
        if (err != null) {
            log.error("Test error: " + err);
            U.dumpThreads(log);
            CyclicBarrier barrier0 = barrier;
            if (barrier0 != null) {
                barrier = null;
                barrier0.reset();
            }
            stop.set(true);
            fut.get();
            fail(err);
        }
        stop.set(true);
        fut.get();
    } finally {
        client.events().stopLocalListen(p);
        stop.set(true);
    }
}
Also used : CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) TimeoutException(java.util.concurrent.TimeoutException) CacheException(javax.cache.CacheException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) IgniteDiscoverySpi(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi) UUID(java.util.UUID) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with IgniteDiscoverySpi

use of org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi in project ignite by apache.

the class IgniteClientReconnectApiExceptionTest method doTestIgniteOperationOnDisconnect.

/**
 * @param client Client.
 * @param ops Operations closures.
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
private void doTestIgniteOperationOnDisconnect(Ignite client, final List<T2<Callable, C1<Object, Boolean>>> ops) throws Exception {
    assertNotNull(client.cache(DEFAULT_CACHE_NAME));
    final IgniteDiscoverySpi clientSpi = spi0(client);
    Ignite srv = clientRouter(client);
    DiscoverySpi srvSpi = spi0(srv);
    final CountDownLatch disconnectLatch = new CountDownLatch(1);
    final CountDownLatch reconnectLatch = new CountDownLatch(1);
    log.info("Block reconnect.");
    DiscoverySpiTestListener lsnr = new DiscoverySpiTestListener();
    clientSpi.setInternalListener(lsnr);
    lsnr.startBlockJoin();
    final List<IgniteInternalFuture> futs = new ArrayList<>();
    client.events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) {
                info("Disconnected: " + evt);
                assertEquals(1, reconnectLatch.getCount());
                for (T2<Callable, C1<Object, Boolean>> op : ops) futs.add(GridTestUtils.runAsync(op.get1()));
                disconnectLatch.countDown();
            } else if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
                info("Reconnected: " + evt);
                reconnectLatch.countDown();
            }
            return true;
        }
    }, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
    try {
        log.info("Fail client.");
        srvSpi.failNode(client.cluster().localNode().id(), null);
        waitReconnectEvent(disconnectLatch);
        assertEquals(ops.size(), futs.size());
        for (IgniteInternalFuture<?> fut : futs) assertNotDone(fut);
        U.sleep(2000);
        for (IgniteInternalFuture<?> fut : futs) assertNotDone(fut);
        log.info("Allow reconnect.");
        lsnr.stopBlockJoin();
        waitReconnectEvent(reconnectLatch);
        // Check operation after reconnect working.
        for (int i = 0; i < futs.size(); i++) {
            final int i0 = i;
            try {
                final Object futRes = futs.get(i0).get(2, SECONDS);
                assertTrue(GridTestUtils.runAsync(new Callable<Boolean>() {

                    @Override
                    public Boolean call() throws Exception {
                        return ops.get(i0).get2().apply(futRes);
                    }
                }).get(2, SECONDS));
            } catch (IgniteFutureTimeoutCheckedException e) {
                e.printStackTrace();
                fail("Operation timeout. Iteration: " + i + ".");
            }
        }
    } finally {
        lsnr.stopBlockJoin();
        for (IgniteInternalFuture fut : futs) fut.cancel();
        stopAllGrids();
    }
}
Also used : ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteDiscoverySpi(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi) DiscoverySpi(org.apache.ignite.spi.discovery.DiscoverySpi) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) IgniteDiscoverySpi(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi) T2(org.apache.ignite.internal.util.typedef.T2)

Example 4 with IgniteDiscoverySpi

use of org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi in project ignite by apache.

the class IgniteClientReconnectAbstractTest method reconnectClientNodes.

/**
 * Reconnect client node.
 *
 * @param log  Logger.
 * @param clients Clients.
 * @param srv Server.
 * @param disconnectedC Closure which will be run when client node disconnected.
 * @throws Exception If failed.
 */
protected static void reconnectClientNodes(final IgniteLogger log, List<Ignite> clients, Ignite srv, @Nullable Runnable disconnectedC) throws Exception {
    final IgniteDiscoverySpi srvSpi = spi0(srv);
    final CountDownLatch disconnectLatch = new CountDownLatch(clients.size());
    final CountDownLatch reconnectLatch = new CountDownLatch(clients.size());
    log.info("Block reconnect.");
    List<DiscoverySpiTestListener> blockLsnrs = new ArrayList<>();
    for (Ignite client : clients) {
        DiscoverySpiTestListener lsnr = new DiscoverySpiTestListener();
        lsnr.startBlockJoin();
        blockLsnrs.add(lsnr);
        spi0(client).setInternalListener(lsnr);
    }
    IgnitePredicate<Event> p = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) {
                log.info("Disconnected: " + evt);
                disconnectLatch.countDown();
            } else if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
                log.info("Reconnected: " + evt);
                reconnectLatch.countDown();
            }
            return true;
        }
    };
    try {
        for (Ignite client : clients) client.events().localListen(p, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
        for (Ignite client : clients) srvSpi.failNode(client.cluster().localNode().id(), null);
        waitReconnectEvent(log, disconnectLatch);
        if (disconnectedC != null)
            disconnectedC.run();
        log.info("Allow reconnect.");
        for (DiscoverySpiTestListener blockLsnr : blockLsnrs) blockLsnr.stopBlockJoin();
        waitReconnectEvent(log, reconnectLatch);
        for (Ignite client : clients) client.events().stopLocalListen(p);
    } finally {
        for (DiscoverySpiTestListener blockLsnr : blockLsnrs) blockLsnr.stopBlockJoin();
    }
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) IgniteDiscoverySpi(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 5 with IgniteDiscoverySpi

use of org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi in project ignite by apache.

the class CacheLateAffinityAssignmentTest method testDelayAssignmentCacheDestroyCreate.

/**
 * Wait for rebalance, cache is destroyed and created again.
 *
 * @throws Exception If failed.
 */
@Test
public void testDelayAssignmentCacheDestroyCreate() throws Exception {
    Ignite ignite0 = startServer(0, 1);
    CacheConfiguration ccfg = cacheConfiguration();
    ccfg.setName(CACHE_NAME2);
    ignite0.createCache(ccfg);
    DiscoverySpiTestListener lsnr = new DiscoverySpiTestListener();
    ((IgniteDiscoverySpi) ignite0.configuration().getDiscoverySpi()).setInternalListener(lsnr);
    TestRecordingCommunicationSpi spi = (TestRecordingCommunicationSpi) ignite0.configuration().getCommunicationSpi();
    blockSupplySend(spi, CACHE_NAME2);
    lsnr.blockCustomEvent(CacheAffinityChangeMessage.class);
    startServer(1, 2);
    startGrid(3);
    checkAffinity(3, topVer(3, 0), false);
    spi.stopBlock();
    lsnr.waitCustomEvent();
    ignite0.destroyCache(CACHE_NAME2);
    ccfg = cacheConfiguration();
    ccfg.setName(CACHE_NAME2);
    ccfg.setAffinity(affinityFunction(10));
    ignite0.createCache(ccfg);
    lsnr.stopBlockCustomEvents();
    checkAffinity(3, topVer(3, 1), false);
    checkAffinity(3, topVer(3, 2), false);
    idealAff.get(2L).remove(CU.cacheId(CACHE_NAME2));
    calculateAffinity(3);
    checkAffinity(3, topVer(3, 3), true);
}
Also used : TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) DiscoverySpiTestListener(org.apache.ignite.internal.DiscoverySpiTestListener) Ignite(org.apache.ignite.Ignite) IgniteDiscoverySpi(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

IgniteDiscoverySpi (org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi)21 Ignite (org.apache.ignite.Ignite)15 CountDownLatch (java.util.concurrent.CountDownLatch)10 Test (org.junit.Test)10 Event (org.apache.ignite.events.Event)9 DiscoverySpiTestListener (org.apache.ignite.internal.DiscoverySpiTestListener)7 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)7 UUID (java.util.UUID)6 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)5 CacheException (javax.cache.CacheException)4 IgniteException (org.apache.ignite.IgniteException)4 DiscoverySpi (org.apache.ignite.spi.discovery.DiscoverySpi)4 ArrayList (java.util.ArrayList)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CacheEntryEvent (javax.cache.event.CacheEntryEvent)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)3 TestRecordingCommunicationSpi (org.apache.ignite.internal.TestRecordingCommunicationSpi)3 TcpDiscoverySpi (org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)3 IOException (java.io.IOException)2