Search in sources :

Example 1 with IgniteAtomicStamped

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

the class IgniteClientReconnectAtomicsTest method testAtomicStampedReconnect.

/**
     * @throws Exception If failed.
     */
public void testAtomicStampedReconnect() throws Exception {
    Ignite client = grid(serverCount());
    assertTrue(client.cluster().localNode().isClient());
    Ignite srv = clientRouter(client);
    IgniteAtomicStamped clientAtomicStamped = client.atomicStamped("atomicStamped", 0, 0, true);
    assertEquals(true, clientAtomicStamped.compareAndSet(0, 1, 0, 1));
    assertEquals(1, clientAtomicStamped.value());
    assertEquals(1, clientAtomicStamped.stamp());
    final IgniteAtomicStamped srvAtomicStamped = srv.atomicStamped("atomicStamped", 0, 0, false);
    assertEquals(true, srvAtomicStamped.compareAndSet(1, 2, 1, 2));
    assertEquals(2, srvAtomicStamped.value());
    assertEquals(2, srvAtomicStamped.stamp());
    reconnectClientNode(client, srv, new Runnable() {

        @Override
        public void run() {
            assertEquals(true, srvAtomicStamped.compareAndSet(2, 3, 2, 3));
            assertEquals(3, srvAtomicStamped.value());
            assertEquals(3, srvAtomicStamped.stamp());
        }
    });
    assertEquals(true, clientAtomicStamped.compareAndSet(3, 4, 3, 4));
    assertEquals(4, clientAtomicStamped.value());
    assertEquals(4, clientAtomicStamped.stamp());
    assertEquals(true, srvAtomicStamped.compareAndSet(4, 5, 4, 5));
    assertEquals(5, srvAtomicStamped.value());
    assertEquals(5, srvAtomicStamped.stamp());
    srvAtomicStamped.close();
}
Also used : IgniteAtomicStamped(org.apache.ignite.IgniteAtomicStamped) Ignite(org.apache.ignite.Ignite)

Example 2 with IgniteAtomicStamped

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

the class IgniteClientReconnectAtomicsTest method testAtomicStampedReconnectInProgress.

/**
     * @throws Exception If failed.
     */
public void testAtomicStampedReconnectInProgress() throws Exception {
    Ignite client = grid(serverCount());
    assertTrue(client.cluster().localNode().isClient());
    Ignite srv = clientRouter(client);
    final IgniteAtomicStamped clientAtomicStamped = client.atomicStamped("atomicStampedInProgress", 0, 0, true);
    assertEquals(true, clientAtomicStamped.compareAndSet(0, 1, 0, 1));
    assertEquals(1, clientAtomicStamped.value());
    assertEquals(1, clientAtomicStamped.stamp());
    IgniteAtomicStamped srvAtomicStamped = srv.atomicStamped("atomicStampedInProgress", 0, 0, false);
    assertEquals(true, srvAtomicStamped.compareAndSet(1, 2, 1, 2));
    assertEquals(2, srvAtomicStamped.value());
    assertEquals(2, srvAtomicStamped.stamp());
    BlockTcpCommunicationSpi servCommSpi = commSpi(srv);
    servCommSpi.blockMessage(GridNearTxPrepareResponse.class);
    final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                clientAtomicStamped.compareAndSet(2, 3, 2, 3);
            } catch (IgniteClientDisconnectedException e) {
                checkAndWait(e);
                return true;
            }
            return false;
        }
    });
    // Check that client waiting operation.
    GridTestUtils.assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            return fut.get(200);
        }
    }, IgniteFutureTimeoutCheckedException.class, null);
    assertNotDone(fut);
    servCommSpi.unblockMessage();
    reconnectClientNode(client, srv, null);
    assertTrue((Boolean) fut.get(2, TimeUnit.SECONDS));
    // Check that after reconnect working.
    assertEquals(false, clientAtomicStamped.compareAndSet(2, 3, 2, 3));
    assertEquals(3, clientAtomicStamped.value());
    assertEquals(3, clientAtomicStamped.stamp());
    assertEquals(true, srvAtomicStamped.compareAndSet(3, 4, 3, 4));
    assertEquals(4, srvAtomicStamped.value());
    assertEquals(4, srvAtomicStamped.stamp());
    srvAtomicStamped.close();
}
Also used : IgniteAtomicStamped(org.apache.ignite.IgniteAtomicStamped) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) Ignite(org.apache.ignite.Ignite) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException)

Example 3 with IgniteAtomicStamped

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

the class IgniteClientReconnectAtomicsTest method testAtomicStampedReconnectRemoved.

/**
     * @throws Exception If failed.
     */
public void testAtomicStampedReconnectRemoved() throws Exception {
    Ignite client = grid(serverCount());
    assertTrue(client.cluster().localNode().isClient());
    Ignite srv = clientRouter(client);
    final IgniteAtomicStamped clientAtomicStamped = client.atomicStamped("atomicStampedRemoved", 0, 0, true);
    assertEquals(true, clientAtomicStamped.compareAndSet(0, 1, 0, 1));
    assertEquals(1, clientAtomicStamped.value());
    assertEquals(1, clientAtomicStamped.stamp());
    final IgniteAtomicStamped srvAtomicStamped = srv.atomicStamped("atomicStampedRemoved", 0, 0, false);
    assertEquals(true, srvAtomicStamped.compareAndSet(1, 2, 1, 2));
    assertEquals(2, srvAtomicStamped.value());
    assertEquals(2, srvAtomicStamped.stamp());
    reconnectClientNode(client, srv, new Runnable() {

        @Override
        public void run() {
            srvAtomicStamped.close();
        }
    });
    GridTestUtils.assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            clientAtomicStamped.compareAndSet(2, 3, 2, 3);
            return null;
        }
    }, IllegalStateException.class, null);
    IgniteAtomicStamped newClientAtomicStamped = client.atomicStamped("atomicStampedRemoved", 0, 0, true);
    assertEquals(true, newClientAtomicStamped.compareAndSet(0, 1, 0, 1));
    assertEquals(1, newClientAtomicStamped.value());
    assertEquals(1, newClientAtomicStamped.stamp());
    IgniteAtomicStamped newSrvAtomicStamped = srv.atomicStamped("atomicStampedRemoved", 0, 0, false);
    assertEquals(true, newSrvAtomicStamped.compareAndSet(1, 2, 1, 2));
    assertEquals(2, newSrvAtomicStamped.value());
    assertEquals(2, newSrvAtomicStamped.stamp());
    newClientAtomicStamped.close();
}
Also used : IgniteAtomicStamped(org.apache.ignite.IgniteAtomicStamped) Ignite(org.apache.ignite.Ignite) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException)

Example 4 with IgniteAtomicStamped

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

the class GridCacheAtomicReferenceMultiNodeAbstractTest method testAtomicStamped.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
public void testAtomicStamped() throws Exception {
    // Get random name of stamped.
    final String stampedName = UUID.randomUUID().toString();
    // Get random value of atomic stamped.
    final String val = UUID.randomUUID().toString();
    // Get random value of atomic stamped.
    final String stamp = UUID.randomUUID().toString();
    // Get random new value of atomic stamped.
    final String newVal = UUID.randomUUID().toString();
    // Get random new stamp of atomic stamped.
    final String newStamp = UUID.randomUUID().toString();
    // Initialize atomicStamped in cache.
    IgniteAtomicStamped<String, String> stamped = grid(0).atomicStamped(stampedName, val, stamp, true);
    final Ignite ignite = grid(0);
    // Execute task on all grid nodes.
    ignite.compute().call(new IgniteCallable<String>() {

        @Override
        public String call() {
            IgniteAtomicStamped<String, String> stamped = ignite.atomicStamped(stampedName, val, stamp, true);
            assertEquals(val, stamped.value());
            assertEquals(stamp, stamped.stamp());
            return stamped.value();
        }
    });
    stamped.compareAndSet("WRONG EXPECTED VALUE", newVal, "WRONG EXPECTED STAMP", newStamp);
    // Execute task on all grid nodes.
    ignite.compute().call(new IgniteCallable<String>() {

        @Override
        public String call() {
            IgniteAtomicStamped<String, String> stamped = ignite.atomicStamped(stampedName, val, stamp, true);
            assertEquals(val, stamped.value());
            assertEquals(stamp, stamped.stamp());
            return stamped.value();
        }
    });
    stamped.compareAndSet(val, newVal, stamp, newStamp);
    // Execute task on all grid nodes.
    ignite.compute().call(new IgniteCallable<String>() {

        @Override
        public String call() {
            IgniteAtomicStamped<String, String> stamped = ignite.atomicStamped(stampedName, val, stamp, true);
            assertEquals(newVal, stamped.value());
            assertEquals(newStamp, stamped.stamp());
            return stamped.value();
        }
    });
}
Also used : IgniteAtomicStamped(org.apache.ignite.IgniteAtomicStamped) Ignite(org.apache.ignite.Ignite)

Example 5 with IgniteAtomicStamped

use of org.apache.ignite.IgniteAtomicStamped 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)

Aggregations

Ignite (org.apache.ignite.Ignite)6 IgniteAtomicStamped (org.apache.ignite.IgniteAtomicStamped)6 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)2 Closeable (java.io.Closeable)1 ArrayList (java.util.ArrayList)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)1 IgniteAtomicReference (org.apache.ignite.IgniteAtomicReference)1 IgniteAtomicSequence (org.apache.ignite.IgniteAtomicSequence)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