Search in sources :

Example 36 with IgniteAtomicSequence

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

the class IgniteDataStructureUniqueNameTest method testUniqueName.

/**
 * @param singleGrid If {@code true} uses single grid.
 * @throws Exception If failed.
 */
private void testUniqueName(final boolean singleGrid) throws Exception {
    final String name = IgniteUuid.randomUuid().toString();
    final int DS_TYPES = 6;
    final int THREADS = DS_TYPES * 3;
    for (int iter = 0; iter < 20; iter++) {
        log.info("Iteration: " + iter);
        List<IgniteInternalFuture<Object>> futs = new ArrayList<>(THREADS);
        final CyclicBarrier barrier = new CyclicBarrier(THREADS);
        for (int i = 0; i < THREADS; i++) {
            final int idx = i;
            IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    try {
                        Thread.currentThread().setName("test thread-" + idx);
                        barrier.await();
                        Ignite ignite = singleGrid ? ignite(0) : ignite(idx % gridCount());
                        Object res;
                        switch(idx % DS_TYPES) {
                            case 0:
                                log.info("Create atomic long, grid: " + ignite.name());
                                res = ignite.atomicLong(name, 0, true);
                                break;
                            case 1:
                                log.info("Create atomic sequence, grid: " + ignite.name());
                                res = ignite.atomicSequence(name, 0, true);
                                break;
                            case 2:
                                log.info("Create atomic stamped, grid: " + ignite.name());
                                res = ignite.atomicStamped(name, 0, true, true);
                                break;
                            case 3:
                                log.info("Create atomic reference, grid: " + ignite.name());
                                res = ignite.atomicReference(name, null, true);
                                break;
                            case 4:
                                log.info("Create queue, grid: " + ignite.name());
                                res = ignite.queue(name, 0, config(false));
                                break;
                            case 5:
                                log.info("Create set, grid: " + ignite.name());
                                res = ignite.set(name, config(false));
                                break;
                            default:
                                fail();
                                return null;
                        }
                        log.info("Thread created: " + res);
                        return res;
                    } catch (IgniteException e) {
                        log.info("Failed: " + e);
                        return e;
                    }
                }
            });
            futs.add(fut);
        }
        Closeable dataStructure = null;
        int createdCnt = 0;
        for (IgniteInternalFuture<Object> fut : futs) {
            Object res = fut.get();
            if (res instanceof IgniteException || res instanceof IgniteCheckedException)
                continue;
            assertTrue("Unexpected object: " + res, res instanceof IgniteAtomicLong || res instanceof IgniteAtomicSequence || res instanceof IgniteAtomicReference || res instanceof IgniteAtomicStamped || res instanceof IgniteCountDownLatch || res instanceof IgniteQueue || res instanceof IgniteSet || res instanceof IgniteSemaphore || res instanceof IgniteLock);
            log.info("Data structure created: " + dataStructure);
            createdCnt++;
            if (dataStructure != null)
                assertEquals(dataStructure.getClass(), res.getClass());
            else
                dataStructure = (Closeable) res;
        }
        assertNotNull(dataStructure);
        assertEquals(3, createdCnt);
        dataStructure.close();
    }
}
Also used : IgniteAtomicReference(org.apache.ignite.IgniteAtomicReference) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CyclicBarrier(java.util.concurrent.CyclicBarrier) IgniteSet(org.apache.ignite.IgniteSet) IgniteAtomicStamped(org.apache.ignite.IgniteAtomicStamped) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteQueue(org.apache.ignite.IgniteQueue) IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) Ignite(org.apache.ignite.Ignite) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) IgniteLock(org.apache.ignite.IgniteLock)

Example 37 with IgniteAtomicSequence

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

the class CacheClientsConcurrentStartTest method doTest.

/**
 * @throws Exception If failed.
 */
private void doTest() throws Exception {
    final AtomicBoolean failed = new AtomicBoolean();
    startGrids(SRV_CNT);
    for (int i = 0; i < SRV_CNT; i++) {
        ((TestRecordingCommunicationSpi) ignite(i).configuration().getCommunicationSpi()).blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

            @Override
            public boolean apply(ClusterNode node, Message msg) {
                if (msg instanceof GridDhtPartitionsFullMessage) {
                    try {
                        U.sleep(ThreadLocalRandom.current().nextLong(500) + 100);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return false;
            }
        });
    }
    List<IgniteInternalFuture<?>> futs = new ArrayList<>();
    for (int i = 0; i < CLIENTS_CNT; i++) {
        final int idx = i;
        IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {

            @Override
            public void run() {
                Random rnd = new Random();
                try {
                    Ignite ignite = startClientGrid(SRV_CNT + idx);
                    assertTrue(ignite.configuration().isClientMode());
                    for (int i = 0; i < CACHES / 2; i++) {
                        String cacheName = "cache-" + rnd.nextInt(CACHES);
                        IgniteCache<Object, Object> cache = getCache(ignite, cacheName);
                        cache.put(ignite.cluster().localNode().id(), UUID.randomUUID());
                        IgniteAtomicSequence seq = ignite.atomicSequence("seq-" + rnd.nextInt(20), 0, true);
                        seq.getAndIncrement();
                    }
                    while (!stopped) {
                        IgniteCache<Object, Object> cache = getCache(ignite, "cache-" + rnd.nextInt(CACHES));
                        int val = Math.abs(rnd.nextInt(100));
                        if (val >= 0 && val < 40)
                            cache.containsKey(ignite.cluster().localNode().id());
                        else if (val >= 40 && val < 80)
                            cache.get(ignite.cluster().localNode().id());
                        else
                            cache.put(ignite.cluster().localNode().id(), UUID.randomUUID());
                        Thread.sleep(10);
                    }
                } catch (Exception e) {
                    log.error("Unexpected error: " + e, e);
                    failed.set(true);
                }
            }
        }, 1, "client-thread");
        futs.add(fut);
    }
    Thread.sleep(10_000);
    stopped = true;
    for (IgniteInternalFuture<?> fut : futs) fut.get();
    assertFalse(failed.get());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) TcpDiscoveryCustomEventMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryCustomEventMessage) TcpDiscoveryAbstractMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryAbstractMessage) GridDhtPartitionsFullMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsFullMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) ArrayList(java.util.ArrayList) IgniteCache(org.apache.ignite.IgniteCache) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) GridDhtPartitionsFullMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsFullMessage) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) Ignite(org.apache.ignite.Ignite)

Example 38 with IgniteAtomicSequence

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

the class GridCachePartitionedAtomicSequenceMultiThreadedTest method testGetAndIncrement.

/**
 * @throws Exception If failed.
 */
@Test
public void testGetAndIncrement() throws Exception {
    // Random sequence names.
    String seqName = UUID.randomUUID().toString();
    final IgniteAtomicSequence seq = grid(0).atomicSequence(seqName, 0L, true);
    runSequenceClosure(new GridInUnsafeClosure<IgniteAtomicSequence>() {

        @Override
        public void apply(IgniteAtomicSequence t) {
            t.getAndIncrement();
        }
    }, seq, ITERATION_NUM, THREAD_NUM);
    assertEquals(ITERATION_NUM * THREAD_NUM, seq.get());
}
Also used : IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) IgniteAtomicsAbstractTest(org.apache.ignite.internal.processors.cache.datastructures.IgniteAtomicsAbstractTest) Test(org.junit.Test)

Example 39 with IgniteAtomicSequence

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

the class GridCachePartitionedAtomicSequenceMultiThreadedTest method testGetAndIncrementAsync.

/**
 * @throws Exception If failed.
 */
@Test
public void testGetAndIncrementAsync() throws Exception {
    // Random sequence names.
    String seqName = UUID.randomUUID().toString();
    final IgniteAtomicSequence seq = grid(0).atomicSequence(seqName, 0L, true);
    runSequenceClosure(new GridInUnsafeClosure<IgniteAtomicSequence>() {

        @Override
        public void apply(IgniteAtomicSequence t) {
            t.getAndIncrement();
        }
    }, seq, ITERATION_NUM, THREAD_NUM);
    assertEquals(ITERATION_NUM * THREAD_NUM, seq.get());
}
Also used : IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) IgniteAtomicsAbstractTest(org.apache.ignite.internal.processors.cache.datastructures.IgniteAtomicsAbstractTest) Test(org.junit.Test)

Example 40 with IgniteAtomicSequence

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

the class GridCachePartitionedAtomicSequenceMultiThreadedTest method testAddAndGet.

/**
 * @throws Exception If failed.
 */
@Test
public void testAddAndGet() throws Exception {
    // Random sequence names.
    String seqName = UUID.randomUUID().toString();
    final IgniteAtomicSequence seq = grid(0).atomicSequence(seqName, 0L, true);
    runSequenceClosure(new GridInUnsafeClosure<IgniteAtomicSequence>() {

        @Override
        public void apply(IgniteAtomicSequence t) {
            t.addAndGet(5);
        }
    }, seq, ITERATION_NUM, THREAD_NUM);
    assertEquals(5 * ITERATION_NUM * THREAD_NUM, seq.get());
}
Also used : IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) IgniteAtomicsAbstractTest(org.apache.ignite.internal.processors.cache.datastructures.IgniteAtomicsAbstractTest) Test(org.junit.Test)

Aggregations

IgniteAtomicSequence (org.apache.ignite.IgniteAtomicSequence)47 Test (org.junit.Test)29 Ignite (org.apache.ignite.Ignite)20 IgniteException (org.apache.ignite.IgniteException)10 IgniteAtomicsAbstractTest (org.apache.ignite.internal.processors.cache.datastructures.IgniteAtomicsAbstractTest)9 AtomicConfiguration (org.apache.ignite.configuration.AtomicConfiguration)6 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)5 ArrayList (java.util.ArrayList)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IOException (java.io.IOException)3 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)3 IgniteCountDownLatch (org.apache.ignite.IgniteCountDownLatch)3 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)3 HashSet (java.util.HashSet)2 Random (java.util.Random)2 UUID (java.util.UUID)2 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)2 Callable (java.util.concurrent.Callable)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IgniteAtomicReference (org.apache.ignite.IgniteAtomicReference)2