Search in sources :

Example 1 with IgniteAtomicLong

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

the class GridRedisSetCommandHandler method asRestRequest.

/** {@inheritDoc} */
@Override
public GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException {
    assert msg != null;
    if (msg.messageSize() < 3)
        throw new GridRedisGenericException("Wrong number of arguments");
    // check if an atomic long with the key exists (related to incr/decr).
    IgniteAtomicLong l = ctx.grid().atomicLong(msg.key(), 0, false);
    if (l != null) {
        try {
            l.close();
        } catch (IgniteException ignored) {
            U.warn(log, "Failed to remove atomic long for key: " + msg.key());
        }
    }
    GridRestCacheRequest restReq = new GridRestCacheRequest();
    restReq.clientId(msg.clientId());
    restReq.key(msg.key());
    restReq.command(CACHE_PUT);
    restReq.cacheName(CACHE_NAME);
    restReq.value(msg.aux(VAL_POS));
    if (msg.messageSize() >= 4) {
        List<String> params = msg.aux();
        // get rid of SET value.
        params.remove(0);
        if (U.containsStringCollection(params, "nx", true))
            restReq.command(CACHE_PUT_IF_ABSENT);
        else if (U.containsStringCollection(params, "xx", true))
            restReq.command(CACHE_REPLACE);
        setExpire(restReq, params);
    }
    return restReq;
}
Also used : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) GridRedisGenericException(org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException) IgniteException(org.apache.ignite.IgniteException) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong)

Example 2 with IgniteAtomicLong

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

the class GridCacheAbstractDataStructuresFailoverSelfTest method testAtomicLongFailsWhenServersLeft.

/**
     * @throws Exception If failed.
     */
public void testAtomicLongFailsWhenServersLeft() throws Exception {
    client = true;
    Ignite ignite = startGrid(gridCount());
    new Timer().schedule(new TimerTask() {

        @Override
        public void run() {
            for (int i = 0; i < gridCount(); i++) stopGrid(i);
        }
    }, 10_000);
    long stopTime = U.currentTimeMillis() + TEST_TIMEOUT / 2;
    IgniteAtomicLong atomic = ignite.atomicLong(STRUCTURE_NAME, 10, true);
    try {
        while (U.currentTimeMillis() < stopTime) assertEquals(10, atomic.get());
    } catch (IgniteException ignore) {
        // Test that client does not hang.
        return;
    }
    fail();
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) IgniteException(org.apache.ignite.IgniteException) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) Ignite(org.apache.ignite.Ignite)

Example 3 with IgniteAtomicLong

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

the class IgniteCachePutRetryTransactionalSelfTest method testAtomicLongRetries.

/**
     * @throws Exception If failed.
     */
public void testAtomicLongRetries() throws Exception {
    final AtomicBoolean finished = new AtomicBoolean();
    IgniteAtomicLong atomic = ignite(0).atomicLong("TestAtomic", 0, true);
    IgniteInternalFuture<Object> fut = runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finished.get()) {
                stopGrid(3);
                U.sleep(300);
                startGrid(3);
            }
            return null;
        }
    });
    final int keysCnt = 20_000;
    try {
        for (int i = 0; i < keysCnt; i++) atomic.incrementAndGet();
        finished.set(true);
        fut.get();
    } finally {
        finished.set(true);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) EntryProcessorException(javax.cache.processor.EntryProcessorException)

Example 4 with IgniteAtomicLong

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

the class IgniteClientReconnectAtomicsTest method testAtomicLongReconnectRemoved.

/**
     * @throws Exception If failed.
     */
public void testAtomicLongReconnectRemoved() throws Exception {
    Ignite client = grid(serverCount());
    assertTrue(client.cluster().localNode().isClient());
    Ignite srv = clientRouter(client);
    final IgniteAtomicLong clientAtomicLong = client.atomicLong("atomicLongRmv", 0, true);
    assertEquals(0L, clientAtomicLong.getAndAdd(1));
    final IgniteAtomicLong srvAtomicLong = srv.atomicLong("atomicLongRmv", 0, false);
    assertEquals(1L, srvAtomicLong.getAndAdd(1));
    reconnectClientNode(client, srv, new Runnable() {

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

        @Override
        public Object call() throws Exception {
            clientAtomicLong.getAndAdd(1);
            return null;
        }
    }, IllegalStateException.class, null);
    IgniteAtomicLong newClientAtomicLong = client.atomicLong("atomicLongRmv", 0, true);
    assertEquals(0L, newClientAtomicLong.getAndAdd(1));
    IgniteAtomicLong newSrvAtomicLong = srv.atomicLong("atomicLongRmv", 0, false);
    assertEquals(1L, newSrvAtomicLong.getAndAdd(1));
}
Also used : IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) Ignite(org.apache.ignite.Ignite) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException)

Example 5 with IgniteAtomicLong

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

the class IgniteClientReconnectAtomicsTest method testAtomicLongReconnect.

/**
     * @throws Exception If failed.
     */
public void testAtomicLongReconnect() throws Exception {
    Ignite client = grid(serverCount());
    assertTrue(client.cluster().localNode().isClient());
    Ignite srv = clientRouter(client);
    IgniteAtomicLong clientAtomicLong = client.atomicLong("atomicLong", 0, true);
    assertEquals(0L, clientAtomicLong.getAndAdd(1));
    final IgniteAtomicLong srvAtomicLong = srv.atomicLong("atomicLong", 0, false);
    assertEquals(1L, srvAtomicLong.getAndAdd(1));
    reconnectClientNode(client, srv, new Runnable() {

        @Override
        public void run() {
            assertEquals(2L, srvAtomicLong.getAndAdd(1));
        }
    });
    assertEquals(3L, clientAtomicLong.getAndAdd(1));
    assertEquals(4L, srvAtomicLong.getAndAdd(1));
    assertEquals(5L, clientAtomicLong.getAndAdd(1));
}
Also used : IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) Ignite(org.apache.ignite.Ignite)

Aggregations

IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)31 Ignite (org.apache.ignite.Ignite)23 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)6 IgniteException (org.apache.ignite.IgniteException)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 IgniteAtomicSequence (org.apache.ignite.IgniteAtomicSequence)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 EntryProcessorException (javax.cache.processor.EntryProcessorException)2 IgniteCountDownLatch (org.apache.ignite.IgniteCountDownLatch)2 IgniteQueue (org.apache.ignite.IgniteQueue)2 IgniteSet (org.apache.ignite.IgniteSet)2 Transaction (org.apache.ignite.transactions.Transaction)2 Closeable (java.io.Closeable)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 Callable (java.util.concurrent.Callable)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1