Search in sources :

Example 31 with GridConcurrentHashSet

use of org.apache.ignite.internal.util.GridConcurrentHashSet in project ignite by apache.

the class GridMessagingSelfTest method testSendReceiveMessageWithEnumTopic.

/**
 * Tests simple message sending-receiving with enumerated topic.
 *
 * @throws Exception If error occurs.
 */
@Test
public void testSendReceiveMessageWithEnumTopic() 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(TestTopic.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=" + TestTopic.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: " + TestTopic.TOPIC_1);
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ignite1.message().localListen(TestTopic.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=" + TestTopic.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: " + TestTopic.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(TestTopic.TOPIC_1, MSG_1);
    message(rNode1).send(TestTopic.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) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 32 with GridConcurrentHashSet

use of org.apache.ignite.internal.util.GridConcurrentHashSet in project ignite by apache.

the class GridMessagingSelfTest method testSendReceiveMessage.

/**
 * Tests simple message sending-receiving.
 *
 * @throws Exception If error occurs.
 */
@Test
public void testSendReceiveMessage() 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(null, new P2<UUID, Object>() {

        @Override
        public boolean apply(UUID nodeId, Object msg) {
            try {
                log.info("Received new message [msg=" + msg + ", senderNodeId=" + nodeId + ']');
                if (!nodeId.equals(ignite2.cluster().localNode().id())) {
                    log.error("Unexpected sender node: " + nodeId);
                    error.set(true);
                    return false;
                }
                rcvMsgs.add(msg);
                return true;
            } finally {
                rcvLatch.countDown();
            }
        }
    });
    ClusterGroup rNode1 = ignite2.cluster().forRemotes();
    message(rNode1).send(null, MSG_1);
    message(rNode1).send(null, 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) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 33 with GridConcurrentHashSet

use of org.apache.ignite.internal.util.GridConcurrentHashSet in project ignite by apache.

the class GridEventConsumeSelfTest method testMultithreadedWithNodeRestart.

/**
 * @throws Exception If failed.
 */
@Test
public void testMultithreadedWithNodeRestart() throws Exception {
    final AtomicBoolean stop = new AtomicBoolean();
    final BlockingQueue<IgniteBiTuple<Integer, UUID>> queue = new LinkedBlockingQueue<>();
    final Collection<UUID> started = new GridConcurrentHashSet<>();
    final Collection<UUID> stopped = new GridConcurrentHashSet<>();
    final Random rnd = new Random();
    final int consumeCnt = tcpDiscovery() ? CONSUME_CNT : CONSUME_CNT / 5;
    try {
        IgniteInternalFuture<?> starterFut = multithreadedAsync(() -> {
            for (int i = 0; i < consumeCnt; i++) {
                int idx = rnd.nextInt(GRID_CNT);
                try {
                    IgniteEvents evts = grid(idx).events();
                    UUID consumeId = evts.remoteListenAsync((P2<UUID, Event>) (uuid, evt) -> true, null, EVT_JOB_STARTED).get(30_000);
                    started.add(consumeId);
                    queue.add(F.t(idx, consumeId));
                } catch (ClusterTopologyException e) {
                    log.error("Failed during consume starter", e);
                }
                U.sleep(10);
            }
            return null;
        }, 6, "consume-starter");
        starterFut.listen((fut) -> stop.set(true));
        IgniteInternalFuture<?> stopperFut = multithreadedAsync(() -> {
            while (!stop.get() || !queue.isEmpty()) {
                IgniteBiTuple<Integer, UUID> t = queue.poll(1, SECONDS);
                if (t == null)
                    continue;
                int idx = t.get1();
                UUID consumeId = t.get2();
                try {
                    IgniteEvents evts = grid(idx).events();
                    evts.stopRemoteListenAsync(consumeId).get(30_000);
                    stopped.add(consumeId);
                } catch (Exception e) {
                    log.error("Failed during consume stopper", e);
                    queue.add(t);
                }
            }
            return null;
        }, 3, "consume-stopper");
        IgniteInternalFuture<?> nodeRestarterFut = multithreadedAsync(() -> {
            while (!stop.get()) {
                startGrid("anotherGrid");
                stopGrid("anotherGrid");
            }
            return null;
        }, 1, "node-restarter");
        IgniteInternalFuture<?> jobRunnerFut = multithreadedAsync(() -> {
            while (!stop.get()) {
                int idx = rnd.nextInt(GRID_CNT);
                try {
                    grid(idx).compute().runAsync(F.noop()).get(30_000);
                } catch (IgniteException ignored) {
                // Ignore all job execution related errors.
                }
            }
            return null;
        }, 1, "job-runner");
        GridTestUtils.waitForAllFutures(starterFut, stopperFut, nodeRestarterFut, jobRunnerFut);
        Collection<UUID> notStopped = F.lose(started, true, stopped);
        assertEquals("Not stopped IDs: " + notStopped, 0, notStopped.size());
    } finally {
        stop.set(true);
        queue.clear();
    }
}
Also used : P2(org.apache.ignite.internal.util.typedef.P2) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) IgniteException(org.apache.ignite.IgniteException) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) IgniteEvents(org.apache.ignite.IgniteEvents) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) UUID(java.util.UUID) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 34 with GridConcurrentHashSet

use of org.apache.ignite.internal.util.GridConcurrentHashSet in project ignite by apache.

the class GridMultithreadedJobStealingSelfTest method testTwoJobsMultithreaded.

/**
 * Test 2 jobs on 2 nodes.
 *
 * @throws Exception If test failed.
 */
@Test
public void testTwoJobsMultithreaded() throws Exception {
    final AtomicReference<Exception> fail = new AtomicReference<>(null);
    final AtomicInteger stolen = new AtomicInteger(0);
    final AtomicInteger noneStolen = new AtomicInteger(0);
    final GridConcurrentHashSet nodes = new GridConcurrentHashSet();
    int threadsNum = 10;
    GridTestUtils.runMultiThreaded(new Runnable() {

        /**
         */
        @Override
        public void run() {
            try {
                JobStealingResult res = ignite.compute().execute(new JobStealingTask(2), null);
                info("Task result: " + res);
                stolen.addAndGet(res.stolen);
                noneStolen.addAndGet(res.nonStolen);
                nodes.addAll(res.nodes);
            } catch (IgniteException e) {
                log.error("Failed to execute task.", e);
                fail.getAndSet(e);
            }
        }
    }, threadsNum, "JobStealingThread");
    for (Ignite g : G.allGrids()) info("Metrics [nodeId=" + g.cluster().localNode().id() + ", metrics=" + g.cluster().localNode().metrics() + ']');
    assertNull("Test failed with exception: ", fail.get());
    // Total jobs number is threadsNum * 2
    assertEquals("Incorrect processed jobs number", threadsNum * 2, stolen.get() + noneStolen.get());
    assertFalse("No jobs were stolen.", stolen.get() == 0);
    for (Ignite g : G.allGrids()) assertTrue("Node get no jobs.", nodes.contains(g.name()));
    // Under these circumstances we should not have  more than 2 jobs
    // difference.
    // (but muted to 4 due to very rare fails and low priority of fix)
    assertTrue("Stats [stolen=" + stolen + ", noneStolen=" + noneStolen + ']', Math.abs(stolen.get() - noneStolen.get()) <= 4);
}
Also used : GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Ignite(org.apache.ignite.Ignite) IgniteException(org.apache.ignite.IgniteException) GridCommonTest(org.apache.ignite.testframework.junits.common.GridCommonTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 35 with GridConcurrentHashSet

use of org.apache.ignite.internal.util.GridConcurrentHashSet in project ignite by apache.

the class GridMultithreadedJobStealingSelfTest method testJoinedNodeCanStealJobs.

/**
 * Test newly joined node can steal jobs.
 *
 * @throws Exception If test failed.
 */
@Test
public void testJoinedNodeCanStealJobs() throws Exception {
    final AtomicReference<Exception> fail = new AtomicReference<>(null);
    final AtomicInteger stolen = new AtomicInteger(0);
    final AtomicInteger noneStolen = new AtomicInteger(0);
    final GridConcurrentHashSet nodes = new GridConcurrentHashSet();
    int threadsNum = 10;
    final int jobsPerTask = 4;
    jobExecutedLatch = new CountDownLatch(threadsNum);
    final IgniteInternalFuture<Long> future = GridTestUtils.runMultiThreadedAsync(new Runnable() {

        /**
         */
        @Override
        public void run() {
            try {
                final IgniteCompute compute = ignite.compute().withAsync();
                compute.execute(new JobStealingTask(jobsPerTask), null);
                JobStealingResult res = (JobStealingResult) compute.future().get();
                info("Task result: " + res);
                stolen.addAndGet(res.stolen);
                noneStolen.addAndGet(res.nonStolen);
                nodes.addAll(res.nodes);
            } catch (IgniteException e) {
                log.error("Failed to execute task.", e);
                fail.getAndSet(e);
            }
        }
    }, threadsNum, "JobStealingThread");
    // Wait for first job begin execution.
    jobExecutedLatch.await();
    startGrid(2);
    for (Ignite g : G.allGrids()) info("Metrics [nodeId=" + g.cluster().localNode().id() + ", metrics=" + g.cluster().localNode().metrics() + ']');
    future.get();
    assertNull("Test failed with exception: ", fail.get());
    // Total jobs number is threadsNum * 4
    assertEquals("Incorrect processed jobs number", threadsNum * jobsPerTask, stolen.get() + noneStolen.get());
    assertFalse("No jobs were stolen.", stolen.get() == 0);
    for (Ignite g : G.allGrids()) assertTrue("Node get no jobs.", nodes.contains(g.name()));
    assertTrue("Stats [stolen=" + stolen + ", noneStolen=" + noneStolen + ']', Math.abs(stolen.get() - 2 * noneStolen.get()) <= 8);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteException(org.apache.ignite.IgniteException) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) IgniteCompute(org.apache.ignite.IgniteCompute) GridCommonTest(org.apache.ignite.testframework.junits.common.GridCommonTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)42 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 Ignite (org.apache.ignite.Ignite)21 Test (org.junit.Test)21 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)16 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)16 IgniteException (org.apache.ignite.IgniteException)15 IOException (java.io.IOException)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 UUID (java.util.UUID)9 IgniteCache (org.apache.ignite.IgniteCache)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)8 IgniteLock (org.apache.ignite.IgniteLock)8 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)8 ExpectedException (org.junit.rules.ExpectedException)8 Transaction (org.apache.ignite.transactions.Transaction)7