Search in sources :

Example 16 with ConcurrentSkipListSet

use of java.util.concurrent.ConcurrentSkipListSet in project ignite by apache.

the class GridEventConsumeSelfTest method testRemoteProjection.

/**
     * @throws Exception If failed.
     */
public void testRemoteProjection() throws Exception {
    final Collection<UUID> nodeIds = new ConcurrentSkipListSet<>();
    final AtomicInteger cnt = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(GRID_CNT - 1);
    UUID consumeId = events(grid(0).cluster().forRemotes()).remoteListen(new P2<UUID, Event>() {

        @Override
        public boolean apply(UUID nodeId, Event evt) {
            info("Event from " + nodeId + " [" + evt.shortDisplay() + ']');
            assertEquals(EVT_JOB_STARTED, evt.type());
            nodeIds.add(nodeId);
            cnt.incrementAndGet();
            latch.countDown();
            return true;
        }
    }, null, EVT_JOB_STARTED);
    try {
        assertNotNull(consumeId);
        grid(0).compute().broadcast(F.noop());
        assert latch.await(2, SECONDS);
        assertEquals(GRID_CNT - 1, nodeIds.size());
        assertEquals(GRID_CNT - 1, cnt.get());
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
}
Also used : ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 17 with ConcurrentSkipListSet

use of java.util.concurrent.ConcurrentSkipListSet in project ignite by apache.

the class CacheSerializableTransactionsTest method incrementTxMultiple.

/**
     * @param nearCache If {@code true} near cache is enabled.
     * @param store If {@code true} cache store is enabled.
     * @param restart If {@code true} restarts one node.
     * @throws Exception If failed.
     */
private void incrementTxMultiple(boolean nearCache, boolean store, final boolean restart) throws Exception {
    final Ignite srv = ignite(1);
    CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, store, false);
    final List<Ignite> clients = clients();
    final String cacheName = srv.createCache(ccfg).getName();
    final AtomicBoolean stop = new AtomicBoolean();
    try {
        final List<IgniteCache<Integer, Integer>> caches = new ArrayList<>();
        for (Ignite client : clients) {
            if (nearCache)
                caches.add(client.createNearCache(cacheName, new NearCacheConfiguration<Integer, Integer>()));
            else
                caches.add(client.<Integer, Integer>cache(cacheName));
        }
        IgniteInternalFuture<?> restartFut = restart ? restartFuture(stop, null) : null;
        for (int i = 0; i < 20; i += 2) {
            final AtomicInteger cntr = new AtomicInteger();
            final Integer key1 = i;
            final Integer key2 = i + 1;
            final AtomicInteger threadIdx = new AtomicInteger();
            final int THREADS = 10;
            final CyclicBarrier barrier = new CyclicBarrier(THREADS);
            final ConcurrentSkipListSet<Integer> vals1 = new ConcurrentSkipListSet<>();
            final ConcurrentSkipListSet<Integer> vals2 = new ConcurrentSkipListSet<>();
            GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    int idx = threadIdx.getAndIncrement() % caches.size();
                    IgniteCache<Integer, Integer> cache = caches.get(idx);
                    Ignite ignite = cache.unwrap(Ignite.class);
                    IgniteTransactions txs = ignite.transactions();
                    log.info("Started update thread: " + ignite.name());
                    barrier.await();
                    for (int i = 0; i < 1000; i++) {
                        try {
                            try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                                Integer val1 = cache.get(key1);
                                Integer val2 = cache.get(key2);
                                Integer newVal1 = val1 == null ? 1 : val1 + 1;
                                Integer newVal2 = val2 == null ? 1 : val2 + 1;
                                cache.put(key1, newVal1);
                                cache.put(key2, newVal2);
                                tx.commit();
                                assertTrue(vals1.add(newVal1));
                                assertTrue(vals2.add(newVal2));
                            }
                            cntr.incrementAndGet();
                        } catch (TransactionOptimisticException ignore) {
                        // Retry.
                        } catch (IgniteException | CacheException e) {
                            assertTrue("Unexpected exception [err=" + e + ", cause=" + e.getCause() + ']', restart && X.hasCause(e, ClusterTopologyCheckedException.class));
                        }
                    }
                    return null;
                }
            }, THREADS, "update-thread").get();
            log.info("Iteration [iter=" + i + ", val=" + cntr.get() + ']');
            assertTrue(cntr.get() > 0);
            checkValue(key1, cntr.get(), cacheName, restart);
            checkValue(key2, cntr.get(), cacheName, restart);
        }
        stop.set(true);
        if (restartFut != null)
            restartFut.get();
    } finally {
        stop.set(true);
        destroyCache(cacheName);
    }
}
Also used : CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) IgniteTransactions(org.apache.ignite.IgniteTransactions) Callable(java.util.concurrent.Callable) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) IgniteCache(org.apache.ignite.IgniteCache) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Aggregations

ConcurrentSkipListSet (java.util.concurrent.ConcurrentSkipListSet)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 ByteBuffer (java.nio.ByteBuffer)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 NavigableSet (java.util.NavigableSet)2 Set (java.util.Set)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Ignite (org.apache.ignite.Ignite)2 DefaultMQPullConsumer (com.alibaba.rocketmq.client.consumer.DefaultMQPullConsumer)1 OffsetStore (com.alibaba.rocketmq.client.consumer.store.OffsetStore)1 MQClientException (com.alibaba.rocketmq.client.exception.MQClientException)1 MessageExt (com.alibaba.rocketmq.common.message.MessageExt)1 MessageQueue (com.alibaba.rocketmq.common.message.MessageQueue)1 LongObjectHashMap (com.carrotsearch.hppc.LongObjectHashMap)1