Search in sources :

Example 11 with IgniteMessaging

use of org.apache.ignite.IgniteMessaging in project ignite by apache.

the class IgniteClientCheckClusterGroupLocalIdAfterReconnect method testClusterGroupLocalIdAfterClientReconnect.

/**
 * Test checks that local id in cluster group was change and client
 * will be able to send the message to itself after reconnect.
 */
@Test
public void testClusterGroupLocalIdAfterClientReconnect() throws Exception {
    Ignite server = startGrid(0);
    Ignite client = startClientGrid(1);
    UUID clientId = client.cluster().node().id();
    ClusterGroup cg1 = client.cluster().forLocal();
    assertNotNull("Local client ID is different with local ClusterGroup node id. ", cg1.node(clientId));
    // check sending messages is possible while connected
    IgniteMessaging messaging = client.message(client.cluster().forLocal());
    CountDownLatch topicSignal = new CountDownLatch(2);
    messaging.localListen("topic", (IgniteBiPredicate<UUID, Object>) (uuid, n) -> {
        topicSignal.countDown();
        return true;
    });
    // countDown latch = 1
    messaging.send("topic", new External());
    CountDownLatch discSignal = new CountDownLatch(1);
    client.events().localListen((IgnitePredicate<DiscoveryEvent>) evt -> {
        discSignal.countDown();
        return true;
    }, EventType.EVT_CLIENT_NODE_DISCONNECTED);
    server.close();
    assertTrue("client did not disconnect", discSignal.await(LATCH_TIMEOUT, TimeUnit.SECONDS));
    startGrid(0);
    // wait for client reconnect
    IgniteFuture future = client.cluster().clientReconnectFuture();
    assertNotNull(future);
    // throws if times out
    future.get(20_000);
    ClusterGroup cg2 = client.cluster().forLocal();
    UUID newClientId = client.cluster().localNode().id();
    assertNotNull("Local client ID wasn't changed for local ClusterGroup.", cg2.node(newClientId));
    awaitPartitionMapExchange();
    // check sending messages is possible after reconnecting
    // countDown latch = 0
    messaging = client.message(client.cluster().forLocal());
    messaging.send("topic", new External());
    assertTrue("Message wasn't received", topicSignal.await(LATCH_TIMEOUT, TimeUnit.SECONDS));
}
Also used : IgniteMessaging(org.apache.ignite.IgniteMessaging) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) IgniteFuture(org.apache.ignite.lang.IgniteFuture) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Externalizable(java.io.Externalizable) EventType(org.apache.ignite.events.EventType) ObjectOutput(java.io.ObjectOutput) IOException(java.io.IOException) Test(org.junit.Test) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) After(org.junit.After) ObjectInput(java.io.ObjectInput) IgniteMessaging(org.apache.ignite.IgniteMessaging) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 12 with IgniteMessaging

use of org.apache.ignite.IgniteMessaging in project ignite by apache.

the class IgniteClientReconnectApiExceptionTest method igniteOperationsTest.

/**
 * @throws Exception If failed.
 */
private void igniteOperationsTest() throws Exception {
    final Ignite client = startClientGrid(serverCount());
    final IgniteCache<Object, Object> dfltCache = client.cache(DEFAULT_CACHE_NAME);
    final CountDownLatch recvLatch = new CountDownLatch(1);
    assertNotNull(dfltCache);
    doTestIgniteOperationOnDisconnect(client, Arrays.asList(// Check compute.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.compute();
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.compute();
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            IgniteCompute comp = (IgniteCompute) o;
            Collection<UUID> uuids = comp.broadcast(new IgniteCallable<UUID>() {

                @IgniteInstanceResource
                private Ignite ignite;

                @Override
                public UUID call() throws Exception {
                    return ignite.cluster().localNode().id();
                }
            });
            assertFalse(uuids.isEmpty());
            for (UUID uuid : uuids) assertNotNull(uuid);
            return true;
        }
    }), // Check ping node.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.cluster().pingNode(new UUID(0, 0));
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.cluster().pingNode(new UUID(0, 0));
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            Boolean pingNode = (Boolean) o;
            assertFalse(pingNode);
            return true;
        }
    }), // Check register remote listener.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.events().remoteListen(null, new IgnitePredicate<Event>() {

                    @Override
                    public boolean apply(Event event) {
                        return true;
                    }
                });
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.events().remoteListen(null, new IgnitePredicate<Event>() {

                @Override
                public boolean apply(Event event) {
                    return true;
                }
            });
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            UUID remoteId = (UUID) o;
            assertNotNull(remoteId);
            client.events().stopRemoteListen(remoteId);
            return true;
        }
    }), // Check message operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.message().remoteListen(null, new IgniteBiPredicate<UUID, Object>() {

                    @Override
                    public boolean apply(UUID uuid, Object o) {
                        if (o.equals("Test message."))
                            recvLatch.countDown();
                        return true;
                    }
                });
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.message().remoteListen(null, new IgniteBiPredicate<UUID, Object>() {

                @Override
                public boolean apply(UUID uuid, Object o) {
                    if (o.equals("Test message."))
                        recvLatch.countDown();
                    return true;
                }
            });
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNotNull(o);
            IgniteMessaging msg = client.message();
            msg.send(null, "Test message.");
            try {
                assertTrue(recvLatch.await(2, SECONDS));
            } catch (InterruptedException ignored) {
                fail("Message wasn't received.");
            }
            return true;
        }
    }), // Check executor.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.executorService().submit(new Callable<Integer>() {

                    @Override
                    public Integer call() throws Exception {
                        return 42;
                    }
                });
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.executorService().submit(new Callable<Integer>() {

                @Override
                public Integer call() throws Exception {
                    return 42;
                }
            });
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNotNull(o);
            Future<Integer> fut = (Future<Integer>) o;
            try {
                assertEquals(42, (int) fut.get());
            } catch (Exception ignored) {
                fail("Failed submit task.");
            }
            return true;
        }
    })));
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteMessaging(org.apache.ignite.IgniteMessaging) IgniteCallable(org.apache.ignite.lang.IgniteCallable) Collection(java.util.Collection) Event(org.apache.ignite.events.Event) Future(java.util.concurrent.Future) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) T2(org.apache.ignite.internal.util.typedef.T2) IgniteCompute(org.apache.ignite.IgniteCompute)

Aggregations

IgniteMessaging (org.apache.ignite.IgniteMessaging)12 UUID (java.util.UUID)7 Ignite (org.apache.ignite.Ignite)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)6 Test (org.junit.Test)6 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)3 IOException (java.io.IOException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)2 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)2 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)2 Externalizable (java.io.Externalizable)1 ObjectInput (java.io.ObjectInput)1 ObjectOutput (java.io.ObjectOutput)1 AccessControlException (java.security.AccessControlException)1 Collection (java.util.Collection)1 Callable (java.util.concurrent.Callable)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1