Search in sources :

Example 11 with IgniteAtomicLong

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

the class IgniteAtomicLongApiAbstractSelfTest method testIncrementAndGet.

/**
     * @throws Exception If failed.
     */
public void testIncrementAndGet() 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 + 1 == atomic.incrementAndGet();
    assert curAtomicVal + 1 == atomic.get();
}
Also used : IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) Ignite(org.apache.ignite.Ignite)

Example 12 with IgniteAtomicLong

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

the class IgniteDataStructureUniqueNameTest method testCreateRemove.

/**
     * @throws Exception If failed.
     */
public void testCreateRemove() throws Exception {
    final String name = IgniteUuid.randomUuid().toString();
    final Ignite ignite = ignite(0);
    assertNull(ignite.atomicLong(name, 0, false));
    IgniteAtomicReference<Integer> ref = ignite.atomicReference(name, 0, true);
    assertNotNull(ref);
    assertSame(ref, ignite.atomicReference(name, 0, true));
    GridTestUtils.assertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ignite.atomicLong(name, 0, false);
            return null;
        }
    }, IgniteException.class, null);
    GridTestUtils.assertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ignite.atomicLong(name, 0, true);
            return null;
        }
    }, IgniteException.class, null);
    ref.close();
    IgniteAtomicLong atomicLong = ignite.atomicLong(name, 0, true);
    assertNotNull(atomicLong);
    assertSame(atomicLong, ignite.atomicLong(name, 0, true));
    GridTestUtils.assertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ignite.atomicReference(name, 0, false);
            return null;
        }
    }, IgniteException.class, null);
    GridTestUtils.assertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ignite.queue(name, 0, config(false));
            return null;
        }
    }, IgniteException.class, null);
    GridTestUtils.assertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ignite.queue(name, 0, null);
            return null;
        }
    }, IgniteException.class, null);
    GridTestUtils.assertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ignite.set(name, config(false));
            return null;
        }
    }, IgniteException.class, null);
    GridTestUtils.assertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ignite.set(name, null);
            return null;
        }
    }, IgniteException.class, null);
    atomicLong.close();
    IgniteQueue<Integer> q = ignite.queue(name, 0, config(false));
    assertNotNull(q);
    assertSame(q, ignite.queue(name, 0, config(false)));
    assertSame(q, ignite.queue(name, 0, null));
    q.close();
    assertNull(ignite.set(name, null));
    IgniteSet<Integer> set = ignite.set(name, config(false));
    assertNotNull(set);
    assertSame(set, ignite.set(name, config(false)));
    assertSame(set, ignite.set(name, null));
    GridTestUtils.assertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ignite.atomicReference(name, 0, false);
            return null;
        }
    }, IgniteException.class, null);
    GridTestUtils.assertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ignite.queue(name, 0, config(false));
            return null;
        }
    }, IgniteException.class, null);
    GridTestUtils.assertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            ignite.queue(name, 0, null);
            return null;
        }
    }, IgniteException.class, null);
    set.close();
    ref = ignite.atomicReference(name, 0, true);
    assertNotNull(ref);
    assertSame(ref, ignite.atomicReference(name, 0, true));
}
Also used : IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) Ignite(org.apache.ignite.Ignite) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException)

Example 13 with IgniteAtomicLong

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

the class GridCachePartitionedNodeRestartTxSelfTest method checkAtomic.

/**
     * Test AtomicLong.
     * @param name Name of atomic.
     * @throws Exception If failed.
     */
private void checkAtomic(String name) throws Exception {
    for (int i = INIT_GRID_NUM; i < 20; i++) {
        startGrid(i);
        assert PARTITIONED == grid(i).cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class).getCacheMode();
        IgniteAtomicLong atomic = grid(i).atomicLong(name, 0, true);
        long val = atomic.get();
        assertEquals("Atomic check failed for node: " + i, (long) i, val);
        atomic.incrementAndGet();
        stopGrid(i);
    }
}
Also used : IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong)

Example 14 with IgniteAtomicLong

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

the class IgniteAtomicLongExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws Exception If example execution failed.
     */
public static void main(String[] args) throws Exception {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Atomic long example started.");
        // Make name for atomic long (by which it will be known in the cluster).
        String atomicName = UUID.randomUUID().toString();
        // Initialize atomic long.
        final IgniteAtomicLong atomicLong = ignite.atomicLong(atomicName, 0, true);
        System.out.println();
        System.out.println("Atomic long initial value : " + atomicLong.get() + '.');
        // Try increment atomic long from all nodes.
        // Note that this node is also part of the ignite cluster.
        ignite.compute().broadcast(new IgniteCallable<Object>() {

            @Override
            public Object call() {
                for (int i = 0; i < RETRIES; i++) System.out.println("AtomicLong value has been incremented: " + atomicLong.incrementAndGet());
                return null;
            }
        });
        System.out.println();
        System.out.println("Atomic long value after successful CAS: " + atomicLong.get());
    }
}
Also used : IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) Ignite(org.apache.ignite.Ignite)

Example 15 with IgniteAtomicLong

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

the class MemcacheRestExample method main.

/**
     * @param args Command line arguments.
     * @throws Exception In case of error.
     */
public static void main(String[] args) throws Exception {
    MemcachedClient client = null;
    try (Ignite ignite = Ignition.start(MemcacheRestExampleNodeStartup.configuration())) {
        System.out.println();
        System.out.println(">>> Memcache REST example started.");
        IgniteCache<String, Object> cache = ignite.cache("default");
        client = startMemcachedClient(host, port);
        // Put string value to cache using Memcache binary protocol.
        if (client.add("strKey", 0, "strVal").get())
            System.out.println(">>> Successfully put string value using Memcache client.");
        // Check that string value is actually in cache using traditional
        // Ignite API and Memcache binary protocol.
        System.out.println(">>> Getting value for 'strKey' using Ignite cache API: " + cache.get("strKey"));
        System.out.println(">>> Getting value for 'strKey' using Memcache client: " + client.get("strKey"));
        // Remove string value from cache using Memcache binary protocol.
        if (client.delete("strKey").get())
            System.out.println(">>> Successfully removed string value using Memcache client.");
        // Check that cache is empty.
        System.out.println(">>> Current cache size: " + cache.size() + " (expected: 0).");
        // Put integer value to cache using Memcache binary protocol.
        if (client.add("intKey", 0, 100).get())
            System.out.println(">>> Successfully put integer value using Memcache client.");
        // Check that integer value is actually in cache using traditional
        // Ignite API and Memcache binary protocol.
        System.out.println(">>> Getting value for 'intKey' using Ignite cache API: " + cache.get("intKey"));
        System.out.println(">>> Getting value for 'intKey' using Memcache client: " + client.get("intKey"));
        // Remove string value from cache using Memcache binary protocol.
        if (client.delete("intKey").get())
            System.out.println(">>> Successfully removed integer value using Memcache client.");
        // Check that cache is empty.
        System.out.println(">>> Current cache size: " + cache.size() + " (expected: 0).");
        // Create atomic long and close it after test is done.
        try (IgniteAtomicLong l = ignite.atomicLong("atomicLong", 10, true)) {
            // Increment atomic long by 5 using Memcache client.
            if (client.incr("atomicLong", 5, 0) == 15)
                System.out.println(">>> Successfully incremented atomic long by 5.");
            // Increment atomic long using Ignite API and check that value is correct.
            System.out.println(">>> New atomic long value: " + l.incrementAndGet() + " (expected: 16).");
            // Decrement atomic long by 3 using Memcache client.
            if (client.decr("atomicLong", 3, 0) == 13)
                System.out.println(">>> Successfully decremented atomic long by 3.");
            // Decrement atomic long using Ignite API and check that value is correct.
            System.out.println(">>> New atomic long value: " + l.decrementAndGet() + " (expected: 12).");
        }
    } finally {
        if (client != null)
            client.shutdown();
    }
}
Also used : MemcachedClient(net.spy.memcached.MemcachedClient) 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