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

The following examples show how to use org.apache.ignite.Ignite#atomicStamped() . 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: GridCacheDataStructuresLoadTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
@Override public void applyx(Ignite ignite) {
    IgniteAtomicStamped<Integer, Integer> as = ignite.atomicStamped(TEST_STAMP_NAME,
        0, 0, true);

    for (int i = 0; i < operationsPerTx; i++) {
        as.set(RAND.nextInt(MAX_INT), RAND.nextInt(MAX_INT));

        long cnt = writes.incrementAndGet();

        if (cnt % WRITE_LOG_MOD == 0)
            info("Performed " + cnt + " writes.");
    }
}
 
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) {
    IgniteAtomicStamped<Integer, Integer> as = ignite.atomicStamped(TEST_STAMP_NAME,
        0, 0, true);

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

        long cnt = reads.incrementAndGet();

        if (cnt % READ_LOG_MOD == 0)
            info("Performed " + cnt + " reads.");
    }
}
 
Example 3
Source File: IgniteClientReconnectAtomicsTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testAtomicStampedReconnect() throws Exception {
    Ignite client = grid(serverCount());

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

    Ignite srv = ignite(0);

    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();
}
 
Example 4
Source File: IgniteClientReconnectAtomicsWithLostPartitionsTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Tests atomic stamped operation provided by the the given {@code clo}.
 *
 * @param atomicName Name of atomic.
 * @param op Closure that represents an operation.
 * @throws Exception If failed.
 */
private void testAtomicStampedReconnectClusterRestart(
    String atomicName,
    final IgniteInClosure<IgniteAtomicStamped> op
) throws Exception {
    Ignite client = grid(serverCount());

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

    String initVal = "qwerty";
    String initStamp = "asdfgh";

    final IgniteAtomicStamped<String, String> atomic = client.atomicStamped(atomicName, initVal, initStamp, true);

    assertNotNull(atomic);

    assertEquals(initVal, atomic.value());
    assertEquals(initStamp, atomic.stamp());
    assertEquals(initVal, atomic.get().get1());
    assertEquals(initStamp, atomic.get().get2());

    // 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,
        () -> {
            op.apply(atomic);

            return null;
        },
        IgniteException.class,
        "Failed to find atomic stamped with given name: " + atomicName);

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

    IgniteAtomicStamped<String, String> recreatedAtomic = client.atomicStamped(atomicName, initVal, initStamp, true);

    assertNotNull(recreatedAtomic);

    assertEquals(initVal, recreatedAtomic.value());
    assertEquals(initStamp, recreatedAtomic.stamp());
    assertEquals(initVal, recreatedAtomic.get().get1());
    assertEquals(initStamp, recreatedAtomic.get().get2());
}
 
Example 5
Source File: GridCacheAtomicStampedApiSelfAbstractTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testIsolation() throws Exception {
    Ignite ignite = grid(0);

    CacheConfiguration cfg = new CacheConfiguration(DEFAULT_CACHE_NAME);

    cfg.setName("MyCache");
    cfg.setAtomicityMode(TRANSACTIONAL);
    cfg.setWriteSynchronizationMode(FULL_SYNC);

    IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(cfg);

    try {
        String atomicName = UUID.randomUUID().toString();

        String initVal = "qwerty";
        String initStamp = "asdf";

        IgniteAtomicStamped<String, String> atomicStamped = ignite.atomicStamped(atomicName,
            initVal,
            initStamp,
            true);

        try (Transaction tx = ignite.transactions().txStart()) {
            cache.put(1,1);

            assertEquals(initVal, atomicStamped.value());
            assertEquals(initStamp, atomicStamped.stamp());
            assertEquals(initVal, atomicStamped.get().get1());
            assertEquals(initStamp, atomicStamped.get().get2());

            assertTrue(atomicStamped.compareAndSet(initVal, "b", initStamp, "d"));

            tx.rollback();
        }

        assertEquals(0, cache.size());

        assertEquals("b", atomicStamped.value());
        assertEquals("d", atomicStamped.stamp());

        atomicStamped.close();

        assertTrue(atomicStamped.removed());
    }
    finally {
        ignite.destroyCache(cfg.getName());
    }
}
 
Example 6
Source File: GridCacheAtomicStampedApiSelfAbstractTest.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");

    IgniteAtomicStamped<String, Integer> atomic1 = ignite.atomicStamped("atomic1", "a", 1, true);
    IgniteAtomicStamped<String, Integer> atomic2 = ignite.atomicStamped("atomic2", "b", 2, true);
    IgniteAtomicStamped<String, Integer> atomic3 = ignite.atomicStamped("atomic3", cfg, "c", 3, true);
    IgniteAtomicStamped<String, Integer> atomic4 = ignite.atomicStamped("atomic4", cfg, "d", 4, true);

    assertNull(ignite.atomicStamped("atomic1", cfg, "a", 1, false));
    assertNull(ignite.atomicStamped("atomic2", cfg, "a", 1, false));
    assertNull(ignite.atomicStamped("atomic3", "a", 1, false));
    assertNull(ignite.atomicStamped("atomic4", "a", 1, false));

    assertTrue(atomic1.compareAndSet("a", "A", 1, 11));
    assertTrue(atomic2.compareAndSet("b", "B", 2, 12));
    assertTrue(atomic3.compareAndSet("c", "C", 3, 13));
    assertTrue(atomic4.compareAndSet("d", "D", 4, 14));

    assertFalse(atomic1.compareAndSet("a", "Z", 1, 0));
    assertFalse(atomic1.compareAndSet("b", "Z", 2, 0));
    assertFalse(atomic1.compareAndSet("c", "Z", 3, 0));
    assertFalse(atomic1.compareAndSet("d", "Z", 4, 0));

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

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

    assertNull(ignite.atomicStamped("atomic2", "b", 2, false));
    assertNull(ignite.atomicStamped("atomic4", cfg, "d", 4, false));

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

    assertNotNull(ignite.atomicStamped("atomic1", "a", 1, false));
    assertNotNull(ignite.atomicStamped("atomic3", cfg, "c", 3, false));
}
 
Example 7
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 8
Source File: IgniteClientReconnectAtomicsTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testAtomicStampedReconnectRemoved() throws Exception {
    Ignite client = grid(serverCount());

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

    Ignite srv = ignite(0);

    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();
}
 
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 testAtomicStampedReconnectInProgress() throws Exception {
    Ignite client = grid(serverCount());

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

    Ignite srv = ignite(0);

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