Search in sources :

Example 16 with IgniteAtomicLong

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

the class GridCacheMultiNodeDataStructureTest method sample.

/**
     *
     * @param g Grid.
     * @param cacheName Cache name.
     */
private static void sample(Ignite g, String cacheName) {
    IgniteAtomicLong atomicLong = g.atomicLong("keygen", 0, true);
    IgniteAtomicSequence seq = g.atomicSequence("keygen", 0, true);
    seq.incrementAndGet();
    seq.incrementAndGet();
    seq.incrementAndGet();
    seq.incrementAndGet();
    atomicLong.incrementAndGet();
    atomicLong.incrementAndGet();
    atomicLong.incrementAndGet();
    X.println(cacheName + ": Seq: " + seq.get() + " atomicLong " + atomicLong.get());
}
Also used : IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence)

Example 17 with IgniteAtomicLong

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

the class GridRedisGetCommandHandler method makeResponse.

/** {@inheritDoc} */
@Override
public ByteBuffer makeResponse(final GridRestResponse restRes, List<String> params) {
    if (restRes.getResponse() == null) {
        // check if an atomic long with the key exists (related to incr/decr).
        IgniteAtomicLong l = ctx.grid().atomicLong(params.get(0), 0, false);
        long val;
        try {
            val = l.get();
        } catch (Exception ignored) {
            return GridRedisProtocolParser.nil();
        }
        return GridRedisProtocolParser.toBulkString(val);
    }
    if (restRes.getResponse() instanceof String)
        return GridRedisProtocolParser.toBulkString(restRes.getResponse());
    else
        return GridRedisProtocolParser.toTypeError("Operation against a key holding the wrong kind of value");
}
Also used : IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 18 with IgniteAtomicLong

use of org.apache.ignite.IgniteAtomicLong 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 19 with IgniteAtomicLong

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

the class IgniteAtomicLongApiAbstractSelfTest method testAddAndGet.

/**
     * @throws Exception If failed.
     */
public void testAddAndGet() throws Exception {
    info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']');
    Ignite ignite = grid(0);
    IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true);
    long delta = RND.nextLong();
    long curAtomicVal = atomic.get();
    assert curAtomicVal + delta == atomic.addAndGet(delta);
    assert curAtomicVal + delta == atomic.get();
}
Also used : IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) Ignite(org.apache.ignite.Ignite)

Example 20 with IgniteAtomicLong

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

the class IgniteAtomicLongApiAbstractSelfTest method testGetAndDecrement.

/**
     * @throws Exception If failed.
     */
public void testGetAndDecrement() throws Exception {
    info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']');
    Ignite ignite = grid(0);
    IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true);
    long curAtomicVal = atomic.get();
    assert curAtomicVal == atomic.getAndDecrement();
    assert curAtomicVal - 1 == atomic.get();
}
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