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

The following examples show how to use org.apache.ignite.Ignite#atomicReference() . 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) {
    IgniteAtomicReference<Integer> ar = ignite.atomicReference(TEST_REF_NAME,
        null, true);

    for (int i = 0; i < operationsPerTx; i++) {
        ar.set(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) {
    IgniteAtomicReference<Integer> ar = ignite.atomicReference(TEST_REF_NAME, null,
        true);

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

        long cnt = reads.incrementAndGet();

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

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

    final IgniteAtomicReference atomic = client.atomicReference(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 reference with given name: " + atomicName);

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

    IgniteAtomicReference recreatedAtomic = client.atomicReference(atomicName, 100L, true);

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

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

    Ignite srv = ignite(0);

    IgniteAtomicReference<String> clientAtomicRef = client.atomicReference("atomicRef", "1st value", true);

    assertEquals("1st value", clientAtomicRef.get());
    assertTrue(clientAtomicRef.compareAndSet("1st value", "2st value"));
    assertEquals("2st value", clientAtomicRef.get());

    final IgniteAtomicReference<String> srvAtomicRef = srv.atomicReference("atomicRef", "1st value", false);

    assertEquals("2st value", srvAtomicRef.get());
    assertTrue(srvAtomicRef.compareAndSet("2st value", "3st value"));
    assertEquals("3st value", srvAtomicRef.get());

    reconnectClientNode(client, srv, new Runnable() {
        @Override public void run() {
            assertEquals("3st value", srvAtomicRef.get());
            assertTrue(srvAtomicRef.compareAndSet("3st value", "4st value"));
            assertEquals("4st value", srvAtomicRef.get());
        }
    });

    assertEquals("4st value", clientAtomicRef.get());
    assertTrue(clientAtomicRef.compareAndSet("4st value", "5st value"));
    assertEquals("5st value", clientAtomicRef.get());

    assertEquals("5st value", srvAtomicRef.get());
    assertTrue(srvAtomicRef.compareAndSet("5st value", "6st value"));
    assertEquals("6st value", srvAtomicRef.get());

    srvAtomicRef.close();
}
 
Example 5
Source File: GridCacheAtomicReferenceApiSelfAbstractTest.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);

    IgniteAtomicReference<String> ref1 = ignite.atomicReference("ref1", "a", true);
    IgniteAtomicReference<String> ref2 = ignite.atomicReference("ref2", "b", true);
    IgniteAtomicReference<String> ref3 = ignite.atomicReference("ref3", "c", true);

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

    IgniteAtomicReference<String> ref4 = ignite.atomicReference("ref4", cfg, "d", true);
    IgniteAtomicReference<String> ref5 = ignite.atomicReference("ref5", cfg, "e", true);
    IgniteAtomicReference<String> ref6 = ignite.atomicReference("ref6", cfg, "f", true);

    assertNull(ignite.atomicReference("ref4", "a", false));
    assertNull(ignite.atomicReference("ref5", "a", false));
    assertNull(ignite.atomicReference("ref6", "a", false));

    assertNull(ignite.atomicReference("ref1", cfg, "a", false));
    assertNull(ignite.atomicReference("ref2", cfg, "a", false));
    assertNull(ignite.atomicReference("ref3", cfg, "a", false));

    assertTrue(ref1.compareAndSet("a", "A"));
    assertTrue(ref2.compareAndSet("b", "B"));
    assertTrue(ref3.compareAndSet("c", "C"));
    assertTrue(ref4.compareAndSet("d", "D"));
    assertTrue(ref5.compareAndSet("e", "E"));
    assertTrue(ref6.compareAndSet("f", "F"));

    assertFalse(ref1.compareAndSet("a", "Z"));
    assertFalse(ref2.compareAndSet("b", "Z"));
    assertFalse(ref3.compareAndSet("c", "Z"));
    assertFalse(ref4.compareAndSet("d", "Z"));
    assertFalse(ref5.compareAndSet("e", "Z"));
    assertFalse(ref6.compareAndSet("f", "Z"));

    assertEquals("A", ref1.get());
    assertEquals("B", ref2.get());
    assertEquals("C", ref3.get());
    assertEquals("D", ref4.get());
    assertEquals("E", ref5.get());
    assertEquals("F", ref6.get());

    ref2.close();
    ref5.close();

    assertTrue(ref2.removed());
    assertTrue(ref5.removed());

    assertNull(ignite.atomicReference("ref2", "b", false));
    assertNull(ignite.atomicReference("ref5", cfg, "e", false));

    assertFalse(ref1.removed());
    assertFalse(ref3.removed());
    assertFalse(ref4.removed());
    assertFalse(ref6.removed());

    assertNotNull(ignite.atomicReference("ref1", "a", false));
    assertNotNull(ignite.atomicReference("ref3", "c", false));
    assertNotNull(ignite.atomicReference("ref4", cfg, "d", false));
    assertNotNull(ignite.atomicReference("ref6", cfg, "f", false));
}
 
Example 6
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 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 testAtomicReferenceReconnectRemoved() throws Exception {
    Ignite client = grid(serverCount());

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

    Ignite srv = ignite(0);

    final IgniteAtomicReference<String> clientAtomicRef =
        client.atomicReference("atomicRefRemoved", "1st value", true);

    assertEquals("1st value", clientAtomicRef.get());
    assertTrue(clientAtomicRef.compareAndSet("1st value", "2st value"));
    assertEquals("2st value", clientAtomicRef.get());

    final IgniteAtomicReference<String> srvAtomicRef = srv.atomicReference("atomicRefRemoved", "1st value", false);

    assertEquals("2st value", srvAtomicRef.get());
    assertTrue(srvAtomicRef.compareAndSet("2st value", "3st value"));
    assertEquals("3st value", srvAtomicRef.get());

    reconnectClientNode(client, srv, new Runnable() {
        @Override public void run() {
            srvAtomicRef.close();
        }
    });

    GridTestUtils.assertThrows(log, new Callable<Object>() {
        @Override public Object call() throws Exception {
            clientAtomicRef.compareAndSet("3st value", "4st value");

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

    IgniteAtomicReference<String> newClientAtomicRef =
        client.atomicReference("atomicRefRemoved", "1st value", true);

    IgniteAtomicReference<String> newSrvAtomicRef = srv.atomicReference("atomicRefRemoved", "1st value", false);

    assertEquals("1st value", newClientAtomicRef.get());
    assertTrue(newClientAtomicRef.compareAndSet("1st value", "2st value"));
    assertEquals("2st value", newClientAtomicRef.get());

    assertEquals("2st value", newSrvAtomicRef.get());
    assertTrue(newSrvAtomicRef.compareAndSet("2st value", "3st value"));
    assertEquals("3st value", newSrvAtomicRef.get());

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

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

    Ignite srv = ignite(0);

    final IgniteAtomicReference<String> clientAtomicRef =
        client.atomicReference("atomicRefInProg", "1st value", true);

    assertEquals("1st value", clientAtomicRef.get());
    assertTrue(clientAtomicRef.compareAndSet("1st value", "2st value"));
    assertEquals("2st value", clientAtomicRef.get());

    IgniteAtomicReference<String> srvAtomicRef = srv.atomicReference("atomicRefInProg", "1st value", false);

    assertEquals("2st value", srvAtomicRef.get());
    assertTrue(srvAtomicRef.compareAndSet("2st value", "3st value"));
    assertEquals("3st value", srvAtomicRef.get());

    BlockTcpCommunicationSpi servCommSpi = commSpi(srv);

    servCommSpi.blockMessage(GridNearTxPrepareResponse.class);

    final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {
        @Override public Object call() throws Exception {
            try {
                clientAtomicRef.compareAndSet("3st value", "4st value");
            }
            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("4st value", clientAtomicRef.get());

    assertEquals("4st value", srvAtomicRef.get());
    assertTrue(srvAtomicRef.compareAndSet("4st value", "5st value"));
    assertEquals("5st value", srvAtomicRef.get());

    srvAtomicRef.close();
}
 
Example 9
Source File: GridCacheAtomicReferenceApiSelfAbstractTest.java    From ignite with Apache License 2.0 2 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);

    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 initValue = "qazwsx";

        IgniteAtomicReference<String> atomicReference = ignite.atomicReference(atomicName, initValue, true);

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

            assertEquals(initValue, atomicReference.get());

            assertTrue(atomicReference.compareAndSet(initValue, "aaa"));

            assertEquals("aaa", atomicReference.get());

            tx.rollback();

            assertEquals(0, cache.size());
        }

        assertTrue(atomicReference.compareAndSet("aaa", null));

        assertNull(atomicReference.get());

        atomicReference.close();

        assertTrue(atomicReference.removed());
    }
    finally {
        ignite.destroyCache(cfg.getName());
    }
}