/** * @param fut Future to listen. * @return {@code true} If future was not completed and this job should holdCC. */ private boolean callAsync(IgniteFuture fut) { if (fut.isDone()) return false; jobCtx.holdcc(); fut.listen(lsnr); return true; }
/** * @param fut Execution future. * @throws InterruptedException Thrown if wait was interrupted. */ @SuppressWarnings({"UnconditionalWait"}) private void waitForExecution(IgniteFuture fut) throws InterruptedException { long sleep = 250; long threshold = System.currentTimeMillis() + WAIT_TIMEOUT; do synchronized (mux) { mux.wait(sleep); } while (fut != null && !fut.isDone() && !fut.isCancelled() && threshold > System.currentTimeMillis()); assert fut == null || fut.isDone(); }
@Override public void apply(IgniteFuture<Object> fut) { assertTrue(fut.isDone()); try { fut.get(); fail(); } catch (CacheException e) { log.info("Expected error: " + e); futLatch.countDown(); } } });
/** {@inheritDoc} */ @Override protected VisorCacheClearTaskResult run(final VisorCacheClearTaskArg arg) { if (futs == null) futs = new IgniteFuture[3]; if (futs[0] == null || futs[1] == null || futs[2] == null) { String cacheName = arg.getCacheName(); IgniteCache cache = ignite.cache(cacheName); if (cache == null) throw new IllegalStateException("Failed to find cache for name: " + cacheName); if (futs[0] == null) { futs[0] = cache.sizeLongAsync(CachePeekMode.PRIMARY); if (callAsync(futs[0])) return null; } if (futs[1] == null) { futs[1] = cache.clearAsync(); if (callAsync(futs[1])) return null; } if (futs[2] == null) { futs[2] = cache.sizeLongAsync(CachePeekMode.PRIMARY); if (callAsync(futs[2])) return null; } } assert futs[0].isDone() && futs[1].isDone() && futs[2].isDone(); return new VisorCacheClearTaskResult(futs[0].get(), futs[2].get()); }
@Override public boolean apply(Event evt) { if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) { info("Disconnected: " + evt); IgniteFuture<?> fut = client.cluster().clientReconnectFuture(); assertNotNull(fut); assertFalse(fut.isDone()); } else if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) { info("Reconnected: " + evt); reconnectLatch.countDown(); } return true; } }, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
/** * @throws Exception If failed. */ @Test public void testCloseWithCancellation() throws Exception { cnt = 0; startGrids(2); Ignite g1 = grid(1); List<IgniteFuture> futures = new ArrayList<>(); IgniteDataStreamer<Object, Object> dataLdr = g1.dataStreamer(DEFAULT_CACHE_NAME); for (int i = 0; i < 100; i++) futures.add(dataLdr.addData(i, i)); try { dataLdr.close(true); } catch (CacheException e) { // No-op. } for (IgniteFuture fut : futures) assertTrue(fut.isDone()); }
/** * @throws Exception If test failed. */ private void checkActiveFutures() throws Exception { assertEquals(0, compute(prj).activeTaskFutures().size()); cnt.set(0); Collection<IgniteFuture<Object>> futsList = new ArrayList<>(); for (int i = 0; i < 10; i++) { IgniteFuture<Object> fut = compute(prj).callAsync(new TestWaitCallable<>()); assertFalse(fut.isDone()); Map<IgniteUuid, ComputeTaskFuture<Object>> futs = compute(prj).activeTaskFutures(); assertEquals(i + 1, futs.size()); assertTrue(futs.containsKey(((ComputeTaskFuture)fut).getTaskSession().getId())); futsList.add(fut); } synchronized (mux) { cnt.incrementAndGet(); mux.notifyAll(); } for (IgniteFuture<Object> fut : futsList) fut.get(); assertEquals(0, compute(prj).activeTaskFutures().size()); }
/** * @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)); } /**
/** * @throws Exception If failed. */ @Test public void testChain() throws Exception { GridFutureAdapter<String> fut0 = new GridFutureAdapter<>(); IgniteFutureImpl<String> fut = createFuture(fut0); IgniteFuture<Integer> chained = fut.chain(new C1<IgniteFuture<String>, Integer>() { @Override public Integer apply(IgniteFuture<String> fut) { return Integer.valueOf(fut.get()); } }); assertFalse(chained.isDone()); U.sleep(100); final AtomicInteger lsnrCnt = new AtomicInteger(); chained.listen(new CI1<IgniteFuture<Integer>>() { @Override public void apply(IgniteFuture<Integer> fut) { assertEquals(10, (int)fut.get()); lsnrCnt.incrementAndGet(); } }); fut0.onDone("10"); assertTrue(chained.isDone()); assertEquals(10, (int)chained.get()); assertEquals(1, lsnrCnt.get()); assertTrue(fut.isDone()); assertEquals("10", fut.get()); }
/** * @param err Err. */ private void checkChainedOnErrorFinishedFuture(final Exception err) throws Exception { IgniteFutureImpl<String> fut = createFuture(new GridFinishedFuture<String>(err)); // Chain callback will be invoked in specific executor. IgniteFuture<Integer> chained1 = fut.chainAsync(new C1<IgniteFuture<String>, Integer>() { @Override public Integer apply(IgniteFuture<String> fut) { assertEquals(CUSTOM_THREAD_NAME, Thread.currentThread().getName()); try { fut.get(); fail(); } catch (IgniteException e) { assertExpectedException(e, err); throw e; } return -1; } }, customExec); assertExceptionThrown(err, chained1); assertExceptionThrown(err, fut); assertTrue(chained1.isDone()); assertTrue(fut.isDone()); }
/** * @throws Exception If failed. */ private void putAllBackupFailure1() throws Exception { ccfg = cacheConfiguration(1, FULL_SYNC); startServers(4); client = true; Ignite client = startGrid(4); IgniteCache<Integer, Integer> nearCache = client.cache(TEST_CACHE); if (!blockRebalance) awaitPartitionMapExchange(); Ignite srv0 = ignite(0); List<Integer> keys = primaryKeys(srv0.cache(TEST_CACHE), 3); Ignite backup = backup(client.affinity(TEST_CACHE), keys.get(0)); testSpi(backup).blockMessages(GridDhtAtomicNearResponse.class, client.name()); Map<Integer, Integer> map = new HashMap<>(); for (Integer key : keys) map.put(key, key); log.info("Start put [map=" + map + ']'); IgniteFuture<?> fut = nearCache.putAllAsync(map); U.sleep(500); assertFalse(fut.isDone()); stopGrid(backup.name()); fut.get(); checkData(map); }
assertFalse(fut.isDone());
assertFalse(fut.isDone());
/** * @throws Exception If failed. */ @Test public void testPutMissedDhtRequest_UnstableTopology() throws Exception { blockRebalance = true; ccfg = cacheConfiguration(1, FULL_SYNC); startServers(4); client = true; Ignite client = startGrid(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)); }
/** * @throws Exception If failed. */ private void putBackupFailure1() throws Exception { ccfg = cacheConfiguration(1, FULL_SYNC); startServers(4); client = true; Ignite client = startGrid(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)); }