Search in sources :

Example 16 with IgniteDiscoverySpi

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

the class IgniteClientReconnectCacheTest method testReconnect.

/**
 * @throws Exception If failed.
 */
@Test
public void testReconnect() throws Exception {
    IgniteEx client = startClientGrid(SRV_CNT);
    final IgniteDiscoverySpi clientSpi = spi0(client);
    Ignite srv = ignite(0);
    DiscoverySpi srvSpi = ignite(0).configuration().getDiscoverySpi();
    final IgniteCache<Object, Object> cache = client.getOrCreateCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME)).withAllowAtomicOpsInTx();
    final IgniteCache<Object, Object> staticCache = client.cache(STATIC_CACHE).withAllowAtomicOpsInTx();
    staticCache.put(1, 1);
    assertEquals(1, staticCache.get(1));
    CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setName(NEAR_CACHE_NAME);
    final IgniteCache<Object, Object> nearCache = client.getOrCreateCache(ccfg, new NearCacheConfiguration<>()).withAllowAtomicOpsInTx();
    nearCache.put(1, 1);
    assertEquals(1, nearCache.localPeek(1));
    cache.put(1, 1);
    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 AtomicReference<IgniteInternalFuture> blockPutRef = new AtomicReference<>();
    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());
                blockPutRef.set(GridTestUtils.runAsync(new Callable() {

                    @Override
                    public Object call() throws Exception {
                        log.info("Start put.");
                        try {
                            cache.put(2, 2);
                            fail();
                        } catch (CacheException e) {
                            log.info("Expected exception: " + e);
                            IgniteClientDisconnectedException e0 = (IgniteClientDisconnectedException) e.getCause();
                            e0.reconnectFuture().get();
                        }
                        cache.put(2, 2);
                        log.info("Finish put.");
                        return null;
                    }
                }));
                disconnectLatch.countDown();
            } else if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
                info("Reconnected: " + evt);
                assertEquals(0, disconnectLatch.getCount());
                reconnectLatch.countDown();
            }
            return true;
        }
    }, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
    log.info("Fail client.");
    srvSpi.failNode(client.cluster().localNode().id(), null);
    waitReconnectEvent(disconnectLatch);
    IgniteInternalFuture putFut = blockPutRef.get();
    assertNotDone(putFut);
    U.sleep(5000);
    assertNotDone(putFut);
    log.info("Allow reconnect.");
    lsnr.stopBlockJoin();
    assertTrue(reconnectLatch.await(5000, MILLISECONDS));
    checkCacheDiscoveryData(srv, client, DEFAULT_CACHE_NAME, true, true, false);
    checkCacheDiscoveryData(srv, client, NEAR_CACHE_NAME, true, true, true);
    checkCacheDiscoveryData(srv, client, STATIC_CACHE, true, true, false);
    assertEquals(1, cache.get(1));
    putFut.get();
    assertEquals(2, cache.get(2));
    cache.put(3, 3);
    assertEquals(3, cache.get(3));
    assertNull(nearCache.localPeek(1));
    staticCache.put(10, 10);
    assertEquals(10, staticCache.get(10));
    nearCache.put(20, 20);
    srv.cache(nearCache.getName()).put(20, 21);
    assertEquals(21, nearCache.localPeek(20));
    IgniteEx srv2 = startGrid(SRV_CNT + 1);
    Integer key = primaryKey(srv2.cache(DEFAULT_CACHE_NAME));
    cache.put(key, 4);
    assertEquals(4, cache.get(key));
    checkCacheDiscoveryData(srv2, client, DEFAULT_CACHE_NAME, true, true, false);
    checkCacheDiscoveryData(srv2, client, NEAR_CACHE_NAME, true, true, true);
    checkCacheDiscoveryData(srv2, client, STATIC_CACHE, true, true, false);
    staticCache.put(20, 20);
    assertEquals(20, staticCache.get(20));
    for (int i = 0; i < 100; i++) {
        srv.cache(nearCache.getName()).put(i, 22);
        Object actual = nearCache.localPeek(i);
        // null-values can be valid in such case.
        if (actual == null) {
            actual = nearCache.get(i);
            assertEquals(22, actual);
            actual = nearCache.localPeek(i);
        }
        assertEquals(22, actual);
    }
}
Also used : CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteException(org.apache.ignite.IgniteException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) CacheException(javax.cache.CacheException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DiscoverySpi(org.apache.ignite.spi.discovery.DiscoverySpi) IgniteDiscoverySpi(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Ignite(org.apache.ignite.Ignite) IgniteDiscoverySpi(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Test(org.junit.Test)

Example 17 with IgniteDiscoverySpi

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

the class IgniteClientReconnectCacheTest method reconnectTransactionInProgress1.

/**
 * @param client Client.
 * @param txConcurrency Transaction concurrency mode.
 * @param cache Cache.
 * @throws Exception If failed.
 */
private void reconnectTransactionInProgress1(IgniteEx client, final TransactionConcurrency txConcurrency, final IgniteCache<Object, Object> cache) throws Exception {
    Ignite srv = ignite(0);
    final IgniteDiscoverySpi clientSpi = spi0(client);
    final 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();
    client.events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) {
                info("Disconnected: " + evt);
                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);
    final IgniteTransactions txs = client.transactions();
    final CountDownLatch afterPut1 = new CountDownLatch(1);
    final CountDownLatch afterPut2 = new CountDownLatch(1);
    final CountDownLatch putFailed = new CountDownLatch(1);
    IgniteInternalFuture<Boolean> fut = GridTestUtils.runAsync(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            try {
                log.info("Start tx1: " + txConcurrency);
                try (Transaction tx = txs.txStart(txConcurrency, REPEATABLE_READ)) {
                    cache.put(1, 1);
                    afterPut1.countDown();
                    afterPut2.await();
                    cache.put(2, 2);
                    fail();
                } catch (CacheException e) {
                    log.info("Expected exception: " + e);
                    putFailed.countDown();
                    IgniteClientDisconnectedException e0 = (IgniteClientDisconnectedException) e.getCause();
                    e0.reconnectFuture().get();
                }
                log.info("Start tx2: " + txConcurrency);
                try (Transaction tx = txs.txStart(txConcurrency, REPEATABLE_READ)) {
                    cache.put(1, 1);
                    cache.put(2, 2);
                    tx.commit();
                }
                assertEquals(1, cache.get(1));
                assertEquals(2, cache.get(2));
                try (Transaction tx = txs.txStart(txConcurrency, REPEATABLE_READ)) {
                    cache.put(3, 3);
                    cache.put(4, 4);
                    tx.commit();
                }
                assertEquals(1, cache.get(1));
                assertEquals(2, cache.get(2));
                assertEquals(3, cache.get(3));
                assertEquals(4, cache.get(4));
                cache.removeAll();
                return true;
            } catch (AssertionError e) {
                throw e;
            } catch (Throwable e) {
                log.error("Unexpected error", e);
                fail("Unexpected error: " + e);
                return false;
            }
        }
    });
    assertTrue(afterPut1.await(5000, MILLISECONDS));
    assertNotDone(fut);
    srvSpi.failNode(client.localNode().id(), null);
    waitReconnectEvent(disconnectLatch);
    afterPut2.countDown();
    assertTrue(putFailed.await(5000, MILLISECONDS));
    lsnr.stopBlockJoin();
    waitReconnectEvent(reconnectLatch);
    assertTrue(fut.get());
}
Also used : CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteTransactions(org.apache.ignite.IgniteTransactions) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteException(org.apache.ignite.IgniteException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) CacheException(javax.cache.CacheException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) Transaction(org.apache.ignite.transactions.Transaction) DiscoverySpi(org.apache.ignite.spi.discovery.DiscoverySpi) IgniteDiscoverySpi(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Ignite(org.apache.ignite.Ignite) IgniteDiscoverySpi(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi)

Example 18 with IgniteDiscoverySpi

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

the class IgniteClientReconnectContinuousProcessorTest method testEventListenerReconnect.

/**
 * @throws Exception If failed.
 */
@Test
public void testEventListenerReconnect() throws Exception {
    Ignite client = grid(serverCount());
    assertTrue(client.cluster().localNode().isClient());
    Ignite srv = ignite(0);
    IgniteDiscoverySpi srvSpi = spi0(srv);
    EventListener lsnr = new EventListener();
    UUID opId = client.events().remoteListen(lsnr, null, EventType.EVT_JOB_STARTED);
    lsnr.latch = new CountDownLatch(1);
    log.info("Created remote listener: " + opId);
    final CountDownLatch reconnectLatch = new CountDownLatch(1);
    client.events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
                info("Reconnected: " + evt);
                reconnectLatch.countDown();
            }
            return true;
        }
    }, EVT_CLIENT_NODE_RECONNECTED);
    srvSpi.failNode(client.cluster().localNode().id(), null);
    waitReconnectEvent(reconnectLatch);
    client.compute().run(new DummyJob());
    assertTrue(lsnr.latch.await(5000, MILLISECONDS));
    lsnr.latch = new CountDownLatch(1);
    srv.compute().run(new DummyJob());
    assertTrue(lsnr.latch.await(5000, MILLISECONDS));
    lsnr.latch = new CountDownLatch(1);
    log.info("Stop listen, should not get events anymore.");
    client.events().stopRemoteListen(opId);
    assertFalse(lsnr.latch.await(3000, MILLISECONDS));
}
Also used : Event(org.apache.ignite.events.Event) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Ignite(org.apache.ignite.Ignite) IgniteDiscoverySpi(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 19 with IgniteDiscoverySpi

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

the class IgniteClientReconnectContinuousProcessorTest method testMessageListenerReconnect.

/**
 * @param stopFromClient If {@code true} stops listener from client node, otherwise from server.
 * @throws Exception If failed.
 */
private void testMessageListenerReconnect(boolean stopFromClient) throws Exception {
    Ignite client = grid(serverCount());
    assertTrue(client.cluster().localNode().isClient());
    Ignite srv = ignite(0);
    IgniteDiscoverySpi srvSpi = spi0(srv);
    final String topic = "testTopic";
    MessageListener locLsnr = new MessageListener();
    UUID opId = client.message().remoteListen(topic, new RemoteMessageListener());
    client.message().localListen(topic, locLsnr);
    final CountDownLatch reconnectLatch = new CountDownLatch(1);
    client.events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
                info("Reconnected: " + evt);
                reconnectLatch.countDown();
            }
            return true;
        }
    }, EVT_CLIENT_NODE_RECONNECTED);
    srvSpi.failNode(client.cluster().localNode().id(), null);
    waitReconnectEvent(reconnectLatch);
    locLsnr.latch = new CountDownLatch(1);
    latch = new CountDownLatch(2);
    client.message().send(topic, "msg1");
    assertTrue(locLsnr.latch.await(5000, MILLISECONDS));
    assertTrue(latch.await(5000, MILLISECONDS));
    locLsnr.latch = new CountDownLatch(1);
    latch = new CountDownLatch(2);
    srv.message().send(topic, "msg2");
    assertTrue(locLsnr.latch.await(5000, MILLISECONDS));
    assertTrue(latch.await(5000, MILLISECONDS));
    Ignite stopFrom = (stopFromClient ? client : srv);
    log.info("Stop listen, should not get remote messages anymore [from=" + stopFrom.name() + ']');
    stopFrom.message().stopRemoteListen(opId);
    srv.message().send(topic, "msg3");
    locLsnr.latch = new CountDownLatch(1);
    latch = new CountDownLatch(1);
    assertTrue(locLsnr.latch.await(5000, MILLISECONDS));
    assertFalse(latch.await(3000, MILLISECONDS));
    log.info("New nodes should not register stopped listeners.");
    startGrid(serverCount() + 1);
    srv.message().send(topic, "msg4");
    locLsnr.latch = new CountDownLatch(1);
    latch = new CountDownLatch(1);
    assertTrue(locLsnr.latch.await(5000, MILLISECONDS));
    assertFalse(latch.await(3000, MILLISECONDS));
    stopGrid(serverCount() + 1);
}
Also used : Event(org.apache.ignite.events.Event) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Ignite(org.apache.ignite.Ignite) IgniteDiscoverySpi(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 20 with IgniteDiscoverySpi

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

the class IgniteClientReconnectContinuousProcessorTest method continuousQueryReconnect.

/**
 * @param client Client.
 * @param clientCache Client cache.
 * @param lsnr Continuous query listener.
 * @throws Exception If failed.
 */
private void continuousQueryReconnect(Ignite client, IgniteCache<Object, Object> clientCache, CacheEventListener lsnr) throws Exception {
    Ignite srv = ignite(0);
    IgniteDiscoverySpi srvSpi = spi0(srv);
    final CountDownLatch reconnectLatch = new CountDownLatch(1);
    IgnitePredicate<Event> p = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
                info("Reconnected: " + evt);
                reconnectLatch.countDown();
            }
            return true;
        }
    };
    client.events().localListen(p, EVT_CLIENT_NODE_RECONNECTED);
    srvSpi.failNode(client.cluster().localNode().id(), null);
    waitReconnectEvent(reconnectLatch);
    client.events().stopLocalListen(p);
    lsnr.latch = new CountDownLatch(1);
    clientCache.put(1, 1);
    assertTrue(lsnr.latch.await(5000, MILLISECONDS));
    lsnr.latch = new CountDownLatch(1);
    srv.cache(DEFAULT_CACHE_NAME).put(2, 2);
    assertTrue(lsnr.latch.await(5000, MILLISECONDS));
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Ignite(org.apache.ignite.Ignite) IgniteDiscoverySpi(org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi) CountDownLatch(java.util.concurrent.CountDownLatch)

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