Search in sources :

Example 1 with IgniteAtomicReference

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

the class GridCacheAtomicReferenceMultiNodeAbstractTest method testAtomicReference.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
public void testAtomicReference() throws Exception {
    // Get random name of reference.
    final String refName = UUID.randomUUID().toString();
    // Get random value of atomic reference.
    final String val = UUID.randomUUID().toString();
    // Get random new value of atomic reference.
    final String newVal = UUID.randomUUID().toString();
    // Initialize atomicReference in cache.
    IgniteAtomicReference<String> ref = grid(0).atomicReference(refName, val, true);
    final Ignite ignite = grid(0);
    // Execute task on all grid nodes.
    ignite.compute().call(new IgniteCallable<Object>() {

        @Override
        public String call() {
            IgniteAtomicReference<String> ref = ignite.atomicReference(refName, val, true);
            assertEquals(val, ref.get());
            return ref.get();
        }
    });
    ref.compareAndSet("WRONG EXPECTED VALUE", newVal);
    // Execute task on all grid nodes.
    ignite.compute().call(new IgniteCallable<String>() {

        @Override
        public String call() {
            IgniteAtomicReference<String> ref = ignite.atomicReference(refName, val, true);
            assertEquals(val, ref.get());
            return ref.get();
        }
    });
    ref.compareAndSet(val, newVal);
    // Execute task on all grid nodes.
    ignite.compute().call(new IgniteCallable<String>() {

        @Override
        public String call() {
            IgniteAtomicReference<String> ref = ignite.atomicReference(refName, val, true);
            assertEquals(newVal, ref.get());
            return ref.get();
        }
    });
}
Also used : IgniteAtomicReference(org.apache.ignite.IgniteAtomicReference) Ignite(org.apache.ignite.Ignite)

Example 2 with IgniteAtomicReference

use of org.apache.ignite.IgniteAtomicReference 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 3 with IgniteAtomicReference

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

the class GridCacheAbstractDataStructuresFailoverSelfTest method testAtomicReferenceTopologyChange.

/**
     * @throws Exception If failed.
     */
public void testAtomicReferenceTopologyChange() throws Exception {
    try (IgniteAtomicReference atomic = grid(0).atomicReference(STRUCTURE_NAME, 10, true)) {
        Ignite g = startGrid(NEW_IGNITE_INSTANCE_NAME);
        assertEquals((Integer) 10, g.atomicReference(STRUCTURE_NAME, 10, false).get());
        g.atomicReference(STRUCTURE_NAME, 10, false).set(20);
        stopGrid(NEW_IGNITE_INSTANCE_NAME);
        assertEquals((Integer) 20, grid(0).atomicReference(STRUCTURE_NAME, 10, true).get());
    }
}
Also used : IgniteAtomicReference(org.apache.ignite.IgniteAtomicReference) Ignite(org.apache.ignite.Ignite)

Aggregations

Ignite (org.apache.ignite.Ignite)3 IgniteAtomicReference (org.apache.ignite.IgniteAtomicReference)3 Closeable (java.io.Closeable)1 ArrayList (java.util.ArrayList)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)1 IgniteAtomicSequence (org.apache.ignite.IgniteAtomicSequence)1 IgniteAtomicStamped (org.apache.ignite.IgniteAtomicStamped)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteCountDownLatch (org.apache.ignite.IgniteCountDownLatch)1 IgniteException (org.apache.ignite.IgniteException)1 IgniteLock (org.apache.ignite.IgniteLock)1 IgniteQueue (org.apache.ignite.IgniteQueue)1 IgniteSemaphore (org.apache.ignite.IgniteSemaphore)1 IgniteSet (org.apache.ignite.IgniteSet)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1