Java Code Examples for org.apache.ignite.Ignite#atomicLong()

The following examples show how to use org.apache.ignite.Ignite#atomicLong() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GridCacheMultiNodeDataStructureTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @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());
}
 
Example 2
Source File: GridCacheDataStructuresLoadTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
@Override public void applyx(Ignite ignite) {
    IgniteAtomicLong al = ignite.atomicLong(TEST_LONG_NAME, 0, true);

    for (int i = 0; i < operationsPerTx; i++) {
        al.addAndGet(RAND.nextInt(MAX_INT));

        long cnt = writes.incrementAndGet();

        if (cnt % WRITE_LOG_MOD == 0)
            info("Performed " + cnt + " writes.");
    }
}
 
Example 3
Source File: GridCacheDataStructuresLoadTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
@Override public void applyx(Ignite ignite) {
    IgniteAtomicLong al = ignite.atomicLong(TEST_LONG_NAME, 0, true);

    for (int i = 0; i < operationsPerTx; i++) {
        al.get();

        long cnt = reads.incrementAndGet();

        if (cnt % READ_LOG_MOD == 0)
            info("Performed " + cnt + " reads.");
    }
}
 
Example 4
Source File: IgniteClientReconnectAtomicsWithLostPartitionsTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Tests atomic long operation provided by the the given {@code clo}.
 *
 * @param atomicName Name of atomic long.
 * @param clo Closure that represents an operation.
 * @throws Exception If failed.
 */
private void testAtomicLongReconnectClusterRestart(
    String atomicName,
    final IgniteInClosure<IgniteAtomicLong> clo
) throws Exception {
    Ignite client = grid(serverCount());

    assertTrue(client.cluster().localNode().isClient());

    final IgniteAtomicLong atomic = client.atomicLong(atomicName, 1L, true);

    assertNotNull(atomic);

    assertEquals("Unexpected initial value.", 1L, atomic.get());

    // Restart the cluster without waiting for rebalancing.
    // It should lead to data loss because there are no backups in the atomic configuration.
    for (int i = 0; i < serverCount(); ++i) {
        grid(i).close();

        startGrid(i);
    }

    GridTestUtils.assertThrows(
        log,
        () -> {
            clo.apply(atomic);

            return null;
        },
        IgniteException.class,
        "Failed to find atomic long: " + atomicName);

    assertTrue("Atomic long instance should be removed.", atomic.removed());

    IgniteAtomicLong recreatedAtomicLong = client.atomicLong(atomicName, 100L, true);

    assertEquals("Unexpected initial value.", 100L, recreatedAtomicLong.get());
}
 
Example 5
Source File: IgnitePersistentStoreDataStructuresTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testAtomic() throws Exception {
    Ignite ignite = startGrids(4);

    ignite.active(true);

    IgniteAtomicLong atomicLong = ignite.atomicLong("testLong", 0, true);

    for (int i = 0; i < 100; i++)
        atomicLong.incrementAndGet();

    stopAllGrids();

    ignite = startGrids(4);

    ignite.active(true);

    atomicLong = ignite.atomicLong("testLong", 0, false);

    for (int i = 100; i != 0; )
        assertEquals(i--, atomicLong.getAndDecrement());
}
 
Example 6
Source File: IgniteChangeGlobalStateDataStructureTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testDeActivateAndActivateAtomicLong() throws Exception {
    String lName = "myLong";

    Ignite ig1 = primary(0);
    Ignite ig2 = primary(1);
    Ignite ig3 = primary(2);

    IgniteAtomicLong lexp = ig1.atomicLong(lName, 100, true);

    lexp.incrementAndGet();
    lexp.incrementAndGet();
    lexp.incrementAndGet();

    assertTrue(ig1.active());
    assertTrue(ig2.active());
    assertTrue(ig3.active());

    ig2.active(false);

    IgniteEx ex1 = (IgniteEx)ig1;
    IgniteEx ex2 = (IgniteEx)ig2;
    IgniteEx ex3 = (IgniteEx)ig3;

    GridCacheProcessor cache1 = ex1.context().cache();
    GridCacheProcessor cache2 = ex2.context().cache();
    GridCacheProcessor cache3 = ex3.context().cache();

    assertTrue(F.isEmpty(cache1.jcaches()));
    assertTrue(F.isEmpty(cache2.jcaches()));
    assertTrue(F.isEmpty(cache3.jcaches()));

    assertTrue(!ig1.active());
    assertTrue(!ig2.active());
    assertTrue(!ig3.active());

    ig3.active(true);

    assertTrue(ig1.active());
    assertTrue(ig2.active());
    assertTrue(ig3.active());

    IgniteAtomicLong lact1 = ig1.atomicLong(lName, 0, false);
    IgniteAtomicLong lact2 = ig2.atomicLong(lName, 0, false);
    IgniteAtomicLong lact3 = ig3.atomicLong(lName, 0, false);

    assertEquals(103, lact1.get());
    assertEquals(103, lact2.get());
    assertEquals(103, lact3.get());

    lact1.incrementAndGet();

    assertEquals(104, lact1.get());
    assertEquals(104, lact2.get());
    assertEquals(104, lact3.get());

    lact2.incrementAndGet();

    assertEquals(105, lact1.get());
    assertEquals(105, lact2.get());
    assertEquals(105, lact3.get());

    lact3.incrementAndGet();

    assertEquals(106, lact3.get());
    assertEquals(106, lact3.get());
    assertEquals(106, lact3.get());
}
 
Example 7
Source File: IgniteAtomicLongApiAbstractSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Implementation of ignite data structures internally uses special system caches, need make sure that
 * transaction on these system caches do not intersect with transactions started by user.
 *
 * @throws Exception If failed.
 */
@Test
public void testIsolation() throws Exception {
    Ignite ignite = grid(0);

    IgniteCache<Object, Object> cache = ignite.cache(TRANSACTIONAL_CACHE_NAME);

    IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true);

    long curAtomicVal = atomic.get();

    try (Transaction tx = ignite.transactions().txStart()) {
        atomic.getAndIncrement();

        cache.put(1, 1);

        tx.rollback();
    }

    assertEquals(0, cache.size());
    assertEquals(curAtomicVal + 1, atomic.get());
}
 
Example 8
Source File: IgniteAtomicLongApiAbstractSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that basic API works correctly when there are multiple structures in multiple groups.
 *
 * @throws Exception If failed.
 */
@Test
public void testMultipleStructuresInDifferentGroups() throws Exception {
    Ignite ignite = grid(0);

    AtomicConfiguration cfg = new AtomicConfiguration().setGroupName("grp1");

    IgniteAtomicLong atomic1 = ignite.atomicLong("atomic1", 1, true);
    IgniteAtomicLong atomic2 = ignite.atomicLong("atomic2", 2, true);
    IgniteAtomicLong atomic3 = ignite.atomicLong("atomic3", cfg, 3, true);
    IgniteAtomicLong atomic4 = ignite.atomicLong("atomic4", cfg, 4, true);

    assertNull(ignite.atomicLong("atomic1", cfg, 1, false));
    assertNull(ignite.atomicLong("atomic2", cfg, 2, false));
    assertNull(ignite.atomicLong("atomic3", 3, false));
    assertNull(ignite.atomicLong("atomic4", 4, false));

    assertTrue(atomic1.compareAndSet(1, 11));
    assertTrue(atomic2.compareAndSet(2, 12));
    assertTrue(atomic3.compareAndSet(3, 13));
    assertTrue(atomic4.compareAndSet(4, 14));

    assertFalse(atomic1.compareAndSet(1, 0));
    assertFalse(atomic2.compareAndSet(2, 0));
    assertFalse(atomic3.compareAndSet(3, 0));
    assertFalse(atomic4.compareAndSet(4, 0));

    atomic2.close();
    atomic4.close();

    assertTrue(atomic2.removed());
    assertTrue(atomic4.removed());

    assertNull(ignite.atomicLong("atomic2", 2, false));
    assertNull(ignite.atomicLong("atomic4", cfg, 4, false));

    assertFalse(atomic1.removed());
    assertFalse(atomic3.removed());

    assertNotNull(ignite.atomicLong("atomic1", 1, false));
    assertNotNull(ignite.atomicLong("atomic3", cfg, 3, false));
}
 
Example 9
Source File: IgniteClientReconnectAtomicsTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testAtomicsReconnectClusterRestart() throws Exception {
    Ignite client = grid(serverCount());

    assertTrue(client.cluster().localNode().isClient());

    final IgniteAtomicLong atomicLong = client.atomicLong("atomicLong", 1L, true);
    final IgniteAtomicReference<Integer> atomicRef = client.atomicReference("atomicRef", 1, true);
    final IgniteAtomicStamped<Integer, Integer> atomicStamped = client.atomicStamped("atomicStamped", 1, 1, true);
    final IgniteCountDownLatch latch = client.countDownLatch("latch", 1, true, true);
    final IgniteAtomicSequence seq = client.atomicSequence("seq", 1L, true);

    Ignite srv = grid(0);

    reconnectServersRestart(log, client, Collections.singleton(srv), new Callable<Collection<Ignite>>() {
        @Override public Collection<Ignite> call() throws Exception {
            return Collections.singleton((Ignite)startGrid(0));
        }
    });

    GridTestUtils.assertThrows(log, new Callable<Void>() {
        @Override public Void call() throws Exception {
            atomicStamped.compareAndSet(1, 1, 2, 2);

            return null;
        }
    }, IllegalStateException.class, null);

    GridTestUtils.assertThrows(log, new Callable<Void>() {
        @Override public Void call() throws Exception {
            atomicRef.compareAndSet(1, 2);

            return null;
        }
    }, IllegalStateException.class, null);

    GridTestUtils.assertThrows(log, new Callable<Void>() {
        @Override public Void call() throws Exception {
            atomicLong.incrementAndGet();

            return null;
        }
    }, IllegalStateException.class, null);

    GridTestUtils.assertThrows(log, new Callable<Void>() {
        @Override public Void call() throws Exception {
            seq.getAndAdd(1L);

            return null;
        }
    }, IllegalStateException.class, null);
}
 
Example 10
Source File: IgniteClientReconnectAtomicsTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testAtomicLongReconnectInProgress() throws Exception {
    Ignite client = grid(serverCount());

    assertTrue(client.cluster().localNode().isClient());

    Ignite srv = ignite(0);

    BlockTcpCommunicationSpi commSpi = commSpi(srv);

    final IgniteAtomicLong clientAtomicLong = client.atomicLong("atomicLongInProggress", 0, true);

    final IgniteAtomicLong srvAtomicLong = srv.atomicLong("atomicLongInProggress", 0, false);

    commSpi.blockMessage(GridNearTxPrepareResponse.class);

    final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {
        @Override public Object call() throws Exception {
            try {
                clientAtomicLong.getAndAdd(1);
            }
            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);

    commSpi.unblockMessage();

    reconnectClientNode(client, srv, null);

    assertTrue((Boolean)fut.get(2, TimeUnit.SECONDS));

    // Check that after reconnect working.
    assertEquals(2, clientAtomicLong.addAndGet(1));
    assertEquals(3, srvAtomicLong.addAndGet(1));

    clientAtomicLong.close();
}
 
Example 11
Source File: IgniteClientDataStructuresAbstractTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @param creator Creator node.
 * @param other Other node.
 * @throws Exception If failed.
 */
private void testAtomicLong(Ignite creator, Ignite other) throws Exception {
    assertNull(creator.atomicLong("long1", 1L, false));
    assertNull(other.atomicLong("long1", 1L, false));

    try (IgniteAtomicLong cntr = creator.atomicLong("long1", 1L, true)) {
        assertNotNull(cntr);

        assertEquals(1L, cntr.get());

        assertEquals(1L, cntr.getAndAdd(1));

        assertEquals(2L, cntr.get());

        IgniteAtomicLong cntr0 = other.atomicLong("long1", 1L, false);

        assertNotNull(cntr0);

        assertEquals(2L, cntr0.get());

        assertEquals(3L, cntr0.incrementAndGet());

        assertEquals(3L, cntr.get());
    }

    assertAtomicLongClosedCorrect(creator.atomicLong("long1", 1L, false));
    assertAtomicLongClosedCorrect(other.atomicLong("long1", 1L, false));
}
 
Example 12
Source File: IgniteClientReconnectAtomicsTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testAtomicLongReconnect() throws Exception {
    Ignite client = grid(serverCount());

    assertTrue(client.cluster().localNode().isClient());

    Ignite srv = ignite(0);

    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));
}
 
Example 13
Source File: IgniteClientReconnectAtomicsTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testAtomicLongReconnectRemoved() throws Exception {
    Ignite client = grid(serverCount());

    assertTrue(client.cluster().localNode().isClient());

    Ignite srv = ignite(0);

    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));
}