Search in sources :

Example 21 with IgniteAtomicSequence

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

the class GridCacheSequenceApiSelfAbstractTest method multiThreadedSequenceIntegrity.

/**
     *  Multi-threaded integrity.
     *
     * @param batchSize Sequence batch size.
     * @param initVal  Sequence initial value.
     * @throws Exception If test fail.
     */
private void multiThreadedSequenceIntegrity(int batchSize, long initVal) throws Exception {
    // Random sequence names.
    String locSeqName = UUID.randomUUID().toString();
    // Sequence.
    final IgniteAtomicSequence locSeq = grid().atomicSequence(locSeqName, initVal, true);
    locSeq.batchSize(batchSize);
    // Result set.
    final Set<Long> resSet = Collections.synchronizedSet(new HashSet<Long>());
    // Get sequence value and try to put it result set.
    for (int i = 0; i < MAX_LOOPS_NUM; i++) {
        Long val = locSeq.getAndIncrement();
        assert !resSet.contains(val) : "Element already in set : " + val;
        resSet.add(val);
        if (i % 100 == 0)
            info("Finished iteration 1: " + i);
    }
    // Work with sequences in many threads.
    multithreaded(new Callable() {

        @Nullable
        @Override
        public Object call() throws Exception {
            // Get sequence value and try to put it result set.
            for (int i = 0; i < MAX_LOOPS_NUM; i++) {
                Long val = locSeq.getAndIncrement();
                assert !resSet.contains(val) : "Element already in set : " + val;
                resSet.add(val);
            }
            return null;
        }
    }, THREAD_NUM);
    // Get sequence value and try to put it result set.
    for (int i = 0; i < MAX_LOOPS_NUM; i++) {
        Long val = locSeq.getAndIncrement();
        assert !resSet.contains(val) : "Element already in set : " + val;
        resSet.add(val);
        if (i % 100 == 0)
            info("Finished iteration 2: " + i);
    }
    assert resSet.size() == MAX_LOOPS_NUM * (THREAD_NUM + 2);
    for (long i = initVal; i < MAX_LOOPS_NUM * (THREAD_NUM + 2) + initVal; i++) {
        assert resSet.contains(i) : "Element is absent in set : " + i;
        if (i % 100 == 0)
            info("Finished iteration 3: " + i);
    }
    removeSequence(locSeqName);
}
Also used : IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) Callable(java.util.concurrent.Callable) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with IgniteAtomicSequence

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

the class GridCacheSequenceApiSelfAbstractTest method sequenceIntegrity.

/**
     *  Sequence integrity.
     *
     * @param batchSize Sequence batch size.
     * @param initVal  Sequence initial value.
     * @throws Exception If test fail.
     */
private void sequenceIntegrity(int batchSize, long initVal) throws Exception {
    // Random sequence names.
    String locSeqName = UUID.randomUUID().toString();
    // Sequence.
    IgniteAtomicSequence locSeq = grid().atomicSequence(locSeqName, initVal, true);
    locSeq.batchSize(batchSize);
    // Result set.
    Collection<Long> resSet = new HashSet<>();
    // Get sequence value and try to put it result set.
    for (int i = 0; i < MAX_LOOPS_NUM; i++) {
        Long val = locSeq.getAndIncrement();
        assert resSet.add(val) : "Element already in set : " + val;
    }
    assert resSet.size() == MAX_LOOPS_NUM;
    for (long i = initVal; i < MAX_LOOPS_NUM + initVal; i++) assert resSet.contains(i) : "Element is absent in set : " + i;
    removeSequence(locSeqName);
}
Also used : IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) HashSet(java.util.HashSet)

Example 23 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 = 9;
    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 latch, grid: " + ignite.name());
                                res = ignite.countDownLatch(name, 0, true, true);
                                break;
                            case 4:
                                log.info("Create atomic reference, grid: " + ignite.name());
                                res = ignite.atomicReference(name, null, true);
                                break;
                            case 5:
                                log.info("Create queue, grid: " + ignite.name());
                                res = ignite.queue(name, 0, config(false));
                                break;
                            case 6:
                                log.info("Create set, grid: " + ignite.name());
                                res = ignite.set(name, config(false));
                                break;
                            case 7:
                                log.info("Create atomic semaphore, grid: " + ignite.name());
                                res = ignite.semaphore(name, 0, false, true);
                                break;
                            case 8:
                                log.info("Create atomic reentrant lock, grid: " + ignite.name());
                                res = ignite.reentrantLock(name, true, true, true);
                                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 24 with IgniteAtomicSequence

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

the class GridCacheAbstractDataStructuresFailoverSelfTest method testAtomicSequenceTopologyChange.

/**
     * @throws Exception If failed.
     */
public void testAtomicSequenceTopologyChange() throws Exception {
    try (IgniteAtomicSequence s = grid(0).atomicSequence(STRUCTURE_NAME, 10, true)) {
        Ignite g = startGrid(NEW_IGNITE_INSTANCE_NAME);
        assertEquals(1010, g.atomicSequence(STRUCTURE_NAME, 10, false).get());
        assertEquals(1020, g.atomicSequence(STRUCTURE_NAME, 10, false).addAndGet(10));
        stopGrid(NEW_IGNITE_INSTANCE_NAME);
    }
}
Also used : IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) Ignite(org.apache.ignite.Ignite)

Example 25 with IgniteAtomicSequence

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

the class GridCacheAbstractDataStructuresFailoverSelfTest method doTestAtomicSequence.

/**
     * Tests atomic sequence.
     *
     * @param topWorker Topology change worker.
     * @throws Exception If failed.
     */
private void doTestAtomicSequence(ConstantTopologyChangeWorker topWorker) throws Exception {
    try (IgniteAtomicSequence s = grid(0).atomicSequence(STRUCTURE_NAME, 1, true)) {
        IgniteInternalFuture<?> fut = topWorker.startChangingTopology(new IgniteClosure<Ignite, Object>() {

            @Override
            public Object apply(Ignite ignite) {
                assertTrue(ignite.atomicSequence(STRUCTURE_NAME, 1, false).get() > 0);
                return null;
            }
        });
        long old = s.get();
        while (!fut.isDone()) {
            assertEquals(old, s.get());
            long val = s.incrementAndGet();
            assertTrue(val > old);
            old = val;
        }
        fut.get();
    }
}
Also used : IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) Ignite(org.apache.ignite.Ignite)

Aggregations

IgniteAtomicSequence (org.apache.ignite.IgniteAtomicSequence)33 Ignite (org.apache.ignite.Ignite)8 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)3 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)3 IgniteCountDownLatch (org.apache.ignite.IgniteCountDownLatch)2 IgniteException (org.apache.ignite.IgniteException)2 Closeable (java.io.Closeable)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 Callable (java.util.concurrent.Callable)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IgniteAtomicReference (org.apache.ignite.IgniteAtomicReference)1 IgniteAtomicStamped (org.apache.ignite.IgniteAtomicStamped)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)1 IgniteLock (org.apache.ignite.IgniteLock)1