Java Code Examples for org.apache.ignite.IgniteCache#putAsync()

The following examples show how to use org.apache.ignite.IgniteCache#putAsync() . 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: CacheAsyncOperationsTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param cache Cache.
 */
private void async1(IgniteCache<Integer, Integer> cache) {
    cache.put(1, 1);

    latch = new CountDownLatch(1);

    IgniteFuture<?> fut1 = cache.putAsync(0, 0);

    IgniteFuture<?> fut2 = cache.getAndPutAsync(1, 2);

    IgniteFuture<?> fut3 = cache.getAndPutAsync(1, 3);

    assertFalse(fut1.isDone());
    assertFalse(fut2.isDone());
    assertFalse(fut3.isDone());

    latch.countDown();

    try {
        fut1.get();

        fail();
    }
    catch (CacheException e) {
        log.info("Expected error: " + e);
    }

    assertEquals(1, fut2.get());
    assertEquals(2, fut3.get());

    assertNull(cache.get(0));
    assertEquals((Integer)3, cache.get(1));
}
 
Example 2
Source File: IgniteCacheAtomicProtocolTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception If failed.
 */
private void putBackupFailure1() throws Exception {
    ccfg = cacheConfiguration(1, FULL_SYNC);

    startServers(4);

    Ignite client = startClientGrid(4);

    IgniteCache<Integer, Integer> nearCache = client.cache(TEST_CACHE);

    if (!blockRebalance)
        awaitPartitionMapExchange();

    Ignite srv0 = ignite(0);

    Integer key = primaryKey(srv0.cache(TEST_CACHE));

    Ignite backup = backup(client.affinity(TEST_CACHE), key);

    testSpi(backup).blockMessages(GridDhtAtomicNearResponse.class, client.name());

    log.info("Start put [key=" + key + ']');

    IgniteFuture<?> fut = nearCache.putAsync(key, key);

    U.sleep(500);

    assertFalse(fut.isDone());

    stopGrid(backup.name());

    fut.get();

    checkData(F.asMap(key, key));
}
 
Example 3
Source File: IgniteCacheAtomicProtocolTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testPutPrimarySync() throws Exception {
    startGrids(2);

    Ignite clientNode = startClientGrid(2);

    final IgniteCache<Integer, Integer> nearCache = clientNode.createCache(cacheConfiguration(1, PRIMARY_SYNC));

    awaitPartitionMapExchange();

    Ignite srv0 = grid(0);
    final Ignite srv1 = grid(1);

    final Integer key = primaryKey(srv0.cache(TEST_CACHE));

    testSpi(srv0).blockMessages(GridDhtAtomicSingleUpdateRequest.class, srv1.name());

    IgniteFuture<?> fut = nearCache.putAsync(key, key);

    fut.get(5, TimeUnit.SECONDS);

    assertEquals(key, srv0.cache(TEST_CACHE).get(key));

    assertNull(srv1.cache(TEST_CACHE).localPeek(key));

    testSpi(srv0).stopBlock(true);

    GridTestUtils.waitForCondition(new GridAbsPredicate() {
        @Override public boolean apply() {
            return srv1.cache(TEST_CACHE).localPeek(key) != null;
        }
    }, 5000);

    checkData(F.asMap(key, key));
}
 
Example 4
Source File: IgniteCacheAtomicProtocolTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testPutNearNodeFailure() throws Exception {
    startGrids(2);

    Ignite clientNode = startClientGrid(2);

    final IgniteCache<Integer, Integer> nearCache = clientNode.createCache(cacheConfiguration(1, FULL_SYNC));

    awaitPartitionMapExchange();

    final Ignite srv0 = grid(0);
    final Ignite srv1 = grid(1);

    final Integer key = primaryKey(srv0.cache(TEST_CACHE));

    nearCache.putAsync(key, key);

    testSpi(srv1).blockMessages(GridDhtAtomicNearResponse.class, clientNode.name());

    stopGrid(2);

    GridTestUtils.waitForCondition(new GridAbsPredicate() {
        @Override public boolean apply() {
            return ((IgniteKernal)srv0).context().cache().context().mvcc().atomicFuturesCount() == 0;
        }
    }, 5000);

    assertEquals(0, ((IgniteKernal)srv0).context().cache().context().mvcc().atomicFuturesCount());
    assertEquals(0, ((IgniteKernal)srv1).context().cache().context().mvcc().atomicFuturesCount());

    checkData(F.asMap(key, key));
}
 
Example 5
Source File: IgniteCacheAtomicProtocolTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testPutMissedDhtRequest_UnstableTopology() throws Exception {
    blockRebalance = true;

    ccfg = cacheConfiguration(1, FULL_SYNC);

    startServers(4);

    Ignite client = startClientGrid(4);

    IgniteCache<Integer, Integer> nearCache = client.cache(TEST_CACHE);

    testSpi(ignite(0)).blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {
        @Override public boolean apply(ClusterNode node, Message msg) {
            return msg instanceof GridDhtAtomicAbstractUpdateRequest;
        }
    });

    Integer key = primaryKey(ignite(0).cache(TEST_CACHE));

    log.info("Start put [key=" + key + ']');

    IgniteFuture<?> fut = nearCache.putAsync(key, key);

    U.sleep(500);

    assertFalse(fut.isDone());

    stopGrid(0);

    fut.get();

    checkData(F.asMap(key, key));
}
 
Example 6
Source File: AbstractCacheJtaSelfTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testAsyncOpAwait() throws Exception {
    final IgniteCache<String, Integer> cache = jcache();

    GridTestSafeThreadFactory factory = new GridTestSafeThreadFactory("JtaThread");

    final CountDownLatch latch = new CountDownLatch(1);

    Callable<Object> c = new Callable<Object>() {
        @Override public Object call() throws Exception {
            assertNull(grid(0).transactions().tx());

            UserTransaction jtaTx = jotm.getUserTransaction();

            jtaTx.begin();

            try {
                cache.put("key1", 1);

                cache.putAsync("key", 1);

                assertEquals(grid(0).transactions().tx().state(), ACTIVE);

                latch.countDown();

                info("Before JTA commit.");
            }
            finally {
                jtaTx.commit();
            }

            info("After JTA commit.");

            assertEquals((Integer)1, cache.get("key"));

            return null;
        }
    };

    Thread task = factory.newThread(c);

    try (Transaction tx = ignite(0).transactions().txStart(PESSIMISTIC, READ_COMMITTED)) {
        cache.put("key", 0);

        task.start();

        latch.await();

        while (task.getState() != Thread.State.WAITING)
            factory.checkError();

        info("Before cache TX commit.");

        tx.commit();
    }
}
 
Example 7
Source File: IgniteOnePhaseCommitNearReadersTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @param backups Backups number.
 * @throws Exception If failed.
 */
private void putReaderUpdatePrimaryFails(int backups) throws Exception {
    testSpi = true;

    final int SRVS = 3;

    startGrids(SRVS);

    awaitPartitionMapExchange();

    Ignite srv = ignite(0);

    srv.createCache(cacheConfiguration(backups));

    Ignite client1 = startClientGrid(SRVS);

    IgniteCache<Object, Object> cache1 = client1.createNearCache(DEFAULT_CACHE_NAME,
        new NearCacheConfiguration<>());

    Ignite client2 = startClientGrid(SRVS + 1);

    IgniteCache<Object, Object> cache2 = client2.cache(DEFAULT_CACHE_NAME);

    Integer key = primaryKey(srv.cache(DEFAULT_CACHE_NAME));

    cache1.put(key, 1);

    spi(srv).blockMessages(GridNearTxPrepareResponse.class, client2.name());

    IgniteFuture<?> fut = cache2.putAsync(key, 2);

    U.sleep(1000);

    assertFalse(fut.isDone());

    stopGrid(0);

    fut.get();

    checkCacheData(F.asMap(key, backups == 0 ? null : 2), DEFAULT_CACHE_NAME);

    for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
        for (TransactionIsolation isolation : TransactionIsolation.values()) {
            srv = startGrid(0);

            awaitPartitionMapExchange(true, true, null);

            key = primaryKey(srv.cache(DEFAULT_CACHE_NAME));

            cache1.put(key, 1);

            spi(srv).blockMessages(GridNearTxPrepareResponse.class, client2.name());

            try (Transaction tx = client2.transactions().txStart(concurrency, isolation)) {
                cache2.putAsync(key, 2);

                fut = tx.commitAsync();

                U.sleep(1000);

                assertFalse(fut.isDone());

                stopGrid(0);

                if (backups == 0)
                    fut.get();
                else {
                    try {
                        fut.get();

                        fail();
                    }
                    catch (TransactionRollbackException ignore) {
                        // Expected.
                    }
                }
            }

            checkCacheData(F.asMap(key, backups == 0 ? null : 1), DEFAULT_CACHE_NAME);

            try (Transaction tx = client2.transactions().txStart(concurrency, isolation)) {
                cache2.putAsync(key, 2);

                tx.commit();
            }

            checkCacheData(F.asMap(key, 2), DEFAULT_CACHE_NAME);
        }
    }
}