Search in sources :

Example 26 with ClusterGroup

use of org.apache.ignite.cluster.ClusterGroup in project ignite by apache.

the class ClosureServiceClientsNodesTest method testDefaultService.

/**
     * @throws Exception If failed.
     */
public void testDefaultService() throws Exception {
    UUID clientNodeId = grid(0).cluster().localNode().id();
    for (int i = 0; i < NODES_CNT; i++) {
        log.info("Iteration: " + i);
        final Ignite ignite = grid(i);
        ignite.services().deployNodeSingleton(SINGLETON_NAME, new TestService());
        final ClusterGroup grp = ignite.cluster();
        assertEquals(NODES_CNT, grp.nodes().size());
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return ignite.services(grp).serviceDescriptors().size() == 1;
            }
        }, 5000);
        Collection<ServiceDescriptor> srvDscs = ignite.services(grp).serviceDescriptors();
        assertEquals(1, srvDscs.size());
        Map<UUID, Integer> nodesMap = F.first(srvDscs).topologySnapshot();
        assertEquals(NODES_CNT - 1, nodesMap.size());
        for (Map.Entry<UUID, Integer> nodeInfo : nodesMap.entrySet()) {
            assertFalse(clientNodeId.equals(nodeInfo.getKey()));
            assertEquals(1, nodeInfo.getValue().intValue());
        }
        ignite.services().cancelAll();
    }
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) ServiceDescriptor(org.apache.ignite.services.ServiceDescriptor) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) Map(java.util.Map)

Example 27 with ClusterGroup

use of org.apache.ignite.cluster.ClusterGroup in project ignite by apache.

the class GridMessagingSelfTest method testSendReceiveMessageWithStringTopic.

/**
     * Tests simple message sending-receiving with string topic.
     *
     * @throws Exception If error occurs.
     */
public void testSendReceiveMessageWithStringTopic() throws Exception {
    final Collection<Object> rcvMsgs = new GridConcurrentHashSet<>();
    //to make it modifiable
    final AtomicBoolean error = new AtomicBoolean(false);
    final CountDownLatch rcvLatch = new CountDownLatch(3);
    ignite1.message().localListen(S_TOPIC_1, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ", topic=" + S_TOPIC_1 + ']');
                if (!nodeId.equals(ignite1.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                if (!MSG_1.equals(msg)) {
                    log.error("Unexpected message " + msg + " for topic: " + S_TOPIC_1);
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ignite1.message().localListen(S_TOPIC_2, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ", topic=" + S_TOPIC_2 + ']');
                if (!nodeId.equals(ignite1.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                if (!MSG_2.equals(msg)) {
                    log.error("Unexpected message " + msg + " for topic: " + S_TOPIC_2);
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ignite1.message().localListen(null, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ", topic=default]");
                if (!nodeId.equals(ignite1.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                if (!MSG_3.equals(msg)) {
                    log.error("Unexpected message " + msg + " for topic: default");
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ClusterGroup rNode1 = ignite1.cluster().forLocal();
    message(rNode1).send(S_TOPIC_1, MSG_1);
    message(rNode1).send(S_TOPIC_2, MSG_2);
    message(rNode1).send(null, MSG_3);
    assertTrue(rcvLatch.await(3, TimeUnit.SECONDS));
    assertFalse(error.get());
    assertTrue(rcvMsgs.contains(MSG_1));
    assertTrue(rcvMsgs.contains(MSG_2));
    assertTrue(rcvMsgs.contains(MSG_3));
}
Also used : GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID)

Example 28 with ClusterGroup

use of org.apache.ignite.cluster.ClusterGroup in project ignite by apache.

the class GridMessagingSelfTest method testRemoteListen.

/**
     * Tests simple message sending-receiving with the use of
     * remoteListen() method.
     *
     * @throws Exception If error occurs.
     */
public void testRemoteListen() throws Exception {
    final Collection<Object> rcvMsgs = new GridConcurrentHashSet<>();
    rcvLatch = new CountDownLatch(4);
    ignite2.message().remoteListen(null, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ']');
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    // Includes node from grid2.
    ClusterGroup prj2 = ignite1.cluster().forRemotes();
    message(prj2).send(null, MSG_1);
    message(prj2).send(null, MSG_2);
    message(ignite2.cluster().forLocal()).send(null, MSG_3);
    // We should get only 3 message.
    assertFalse(rcvLatch.await(3, TimeUnit.SECONDS));
    assertTrue(rcvMsgs.contains(MSG_1));
    assertTrue(rcvMsgs.contains(MSG_2));
    assertTrue(rcvMsgs.contains(MSG_3));
}
Also used : GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID)

Example 29 with ClusterGroup

use of org.apache.ignite.cluster.ClusterGroup in project ignite by apache.

the class GridMessagingSelfTest method testRemoteListenWithIntTopic.

/**
     * Tests simple message sending-receiving with the use of
     * remoteListen() method and topics.
     *
     * @throws Exception If error occurs.
     */
public void testRemoteListenWithIntTopic() throws Exception {
    final Collection<Object> rcvMsgs = new GridConcurrentHashSet<>();
    //to make it modifiable
    final AtomicBoolean error = new AtomicBoolean(false);
    rcvLatch = new CountDownLatch(3);
    ignite2.message().remoteListen(I_TOPIC_1, new P2<UUID, Object>() {

        @IgniteInstanceResource
        private transient Ignite g;

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            assertEquals(ignite2, g);
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ", topic=" + I_TOPIC_1 + ']');
                if (!nodeId.equals(ignite1.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                if (!MSG_1.equals(msg)) {
                    log.error("Unexpected message " + msg + " for topic: " + I_TOPIC_1);
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ignite2.message().remoteListen(I_TOPIC_2, new P2<UUID, Object>() {

        @IgniteInstanceResource
        private transient Ignite g;

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            assertEquals(ignite2, g);
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ", topic=" + I_TOPIC_2 + ']');
                if (!nodeId.equals(ignite1.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                if (!MSG_2.equals(msg)) {
                    log.error("Unexpected message " + msg + " for topic: " + I_TOPIC_2);
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ignite2.message().remoteListen(null, new P2<UUID, Object>() {

        @IgniteInstanceResource
        private transient Ignite g;

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            assertEquals(ignite2, g);
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ", topic=default]");
                if (!nodeId.equals(ignite1.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                if (!MSG_3.equals(msg)) {
                    log.error("Unexpected message " + msg + " for topic: default");
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    // Includes node from grid2.
    ClusterGroup prj2 = ignite1.cluster().forRemotes();
    message(prj2).send(I_TOPIC_1, MSG_1);
    message(prj2).send(I_TOPIC_2, MSG_2);
    message(prj2).send(null, MSG_3);
    assertTrue(rcvLatch.await(3, TimeUnit.SECONDS));
    assertFalse(error.get());
    assertTrue(rcvMsgs.contains(MSG_1));
    assertTrue(rcvMsgs.contains(MSG_2));
    assertTrue(rcvMsgs.contains(MSG_3));
}
Also used : GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID)

Example 30 with ClusterGroup

use of org.apache.ignite.cluster.ClusterGroup in project ignite by apache.

the class GridMarshallerAbstractTest method testServices.

/**
     * @throws Exception If failed.
     */
public void testServices() throws Exception {
    IgniteConfiguration cfg = optimize(getConfiguration("g1"));
    try (Ignite g1 = G.start(cfg)) {
        IgniteServices services = grid().services(grid().cluster().forNode(g1.cluster().localNode()));
        services.deployNodeSingleton("test", new DummyService());
        GridMarshallerTestBean inBean = newTestBean(services);
        byte[] buf = marshal(inBean);
        GridMarshallerTestBean outBean = unmarshal(buf);
        assert inBean.getObjectField() != null;
        assert outBean.getObjectField() != null;
        assert inBean.getObjectField().getClass().equals(IgniteServicesImpl.class);
        assert outBean.getObjectField().getClass().equals(IgniteServicesImpl.class);
        assert inBean != outBean;
        assert inBean.equals(outBean);
        ClusterGroup inPrj = services.clusterGroup();
        ClusterGroup outPrj = ((IgniteServices) outBean.getObjectField()).clusterGroup();
        assert inPrj.getClass().equals(outPrj.getClass());
        assert F.eqNotOrdered(inPrj.nodes(), outPrj.nodes());
        outBean.checkNullResources();
    }
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) DummyService(org.apache.ignite.internal.processors.service.DummyService) IgniteServices(org.apache.ignite.IgniteServices) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignite(org.apache.ignite.Ignite)

Aggregations

ClusterGroup (org.apache.ignite.cluster.ClusterGroup)80 Ignite (org.apache.ignite.Ignite)42 UUID (java.util.UUID)21 CountDownLatch (java.util.concurrent.CountDownLatch)16 ClusterNode (org.apache.ignite.cluster.ClusterNode)14 IgniteException (org.apache.ignite.IgniteException)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 IgniteCompute (org.apache.ignite.IgniteCompute)5 GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)5 ArrayList (java.util.ArrayList)4 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)4 IgniteEx (org.apache.ignite.internal.IgniteEx)4 Map (java.util.Map)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IgniteClusterEx (org.apache.ignite.internal.cluster.IgniteClusterEx)3 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)3 List (java.util.List)2 CacheException (javax.cache.CacheException)2 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)2 IgniteCluster (org.apache.ignite.IgniteCluster)2