/** * Runs runnable task asyncronously. * * @param task Runnable. * @return Future with task result. */ public static IgniteInternalFuture runAsync(final Runnable task) { return runAsync(task,"async-runnable-runner"); }
/** * Runs callable task asyncronously. * * @param task Callable. * @return Future with task result. */ public static <T> IgniteInternalFuture<T> runAsync(final Callable<T> task) { return runAsync(task, "async-callable-runner"); }
@Override public void apply(IgniteFuture<String> fut) { GridTestUtils.runAsync(new Callable<Object>() { @Override public Object call() throws Exception { cache.deregisterCacheEntryListener(cfg); return null; } }); } });
/** */ protected IgniteInternalFuture createAndRunConcurrentAction(final AtomicBoolean finished, final long endTime) { return GridTestUtils.runAsync(new Callable<Object>() { @Override public Object call() throws Exception { Thread.currentThread().setName("restart-thread"); while (!finished.get() && System.currentTimeMillis() < endTime) { startGrid(NODE_CNT); U.sleep(500); stopGrid(NODE_CNT); } return null; } }); }
/** {@inheritDoc} */ @Override protected IgniteInternalFuture createAndRunConcurrentAction(final AtomicBoolean finished, final long endTime) { return GridTestUtils.runAsync(new Callable() { @Override public Object call() throws Exception { Thread.currentThread().setName("restart-thread"); while (!finished.get() && System.currentTimeMillis() < endTime) { ThreadLocalRandom tlr = ThreadLocalRandom.current(); int idx = tlr.nextInt(1, GRIDS_COUNT + 1); log.info("Stopping node " + idx); stopGrid(idx); U.sleep(tlr.nextInt(500, 700)); log.info("Restarting node " + idx); startGrid(idx); } return null; } }); }
/** * Create IGFS file asynchronously. * * @param path Path. * @param writeStartLatch Write start latch. * @return Future. */ private IgniteInternalFuture<Void> createFileAsync(final IgfsPath path, final CountDownLatch writeStartLatch) { return GridTestUtils.runAsync(new Callable<Void>() { @Override public Void call() throws Exception { IgniteFileSystem igfs = attacker.fileSystem(IGFS_NAME); try (IgfsOutputStream out = igfs.create(path, true)) { writeStartLatch.await(); out.write(new byte[1024]); out.flush(); } return null; } }); }
/** * Starts grid asynchronously and returns just before grid starting. * Avoids blocking on PME. * * @param idx Test grid index. * @throws Exception If fails. */ private void startGridAsync(int idx) throws Exception { GridTestUtils.runAsync(new Runnable() { @Override public void run() { try { startGrid(idx); } catch (Exception e) { // no-op. } } }); }
/** * @param stopTime Stop time. * @param stop Stop flag. * @return Future. */ private IgniteInternalFuture<?> startRestartZkServers(final long stopTime, final AtomicBoolean stop) { return GridTestUtils.runAsync(new Callable<Void>() { @Override public Void call() throws Exception { ThreadLocalRandom rnd = ThreadLocalRandom.current(); while (!stop.get() && System.currentTimeMillis() < stopTime) { U.sleep(rnd.nextLong(2500)); int idx = rnd.nextInt(ZK_SRVS); log.info("Restart ZK server: " + idx); zkCluster.getServers().get(idx).restart(); waitForZkClusterReady(zkCluster); } return null; } }, "zk-restart-thread"); }
/** * Runs runnable task asyncronously. * * @param task Runnable. * @return Future with task result. */ public static IgniteInternalFuture runAsync(final Runnable task, String threadName) { return runAsync(() -> { task.run(); return null; }, threadName); }
/** * Stop the node asynchronously. * * @param node Ignite instance. * @return Latch to signal when the node is stopped completely. */ private static CountDownLatch stopNodeAsync(final Ignite node) { final CountDownLatch latch = new CountDownLatch(1); GridTestUtils.runAsync(new Runnable() { @Override public void run() { try { node.close(); } finally { latch.countDown(); } } }); return latch; }
@Override public boolean apply(Event evt) { if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) { info("Disconnected: " + evt); assertEquals(1, reconnectLatch.getCount()); for (T2<Callable, C1<Object, Boolean>> op : ops) futs.add(GridTestUtils.runAsync(op.get1())); disconnectLatch.countDown(); } else if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) { info("Reconnected: " + evt); reconnectLatch.countDown(); } return true; } }, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
@Override public Void apply(Ignite ignite) { final IgniteLock l = ignite.reentrantLock(STRUCTURE_NAME, failoverSafe, fair, false); final AtomicBoolean done = new AtomicBoolean(false); GridTestUtils.runAsync(new Callable<Void>() { @Override public Void call() throws Exception { try{ l.lock(); } finally { done.set(true); } return null; } }, "lock-thread"); // Wait until l.lock() has been called. while(!l.hasQueuedThreads() && !done.get()){ // No-op. } return null; } });
/** * Start 1, 2 or 3 random nodes simultaneously. * * @param limit Cluster size limit. * @param rnd Randmo generator. * @param grids Collection with indexes of running nodes. * @throws Exception If failed. */ private void startRandomNodesMultithreaded(int limit, Random rnd, Collection<Integer> grids) throws Exception { int cnt = rnd.nextInt(Math.min(limit - grids.size(), 3)) + 1; for (int i = 1; i <= cnt; i++) { int gridIdx = nextAvailableIdx(grids, limit, rnd); if (i == cnt) startGrid(gridIdx); else GridTestUtils.runAsync(() -> startGrid(gridIdx)); grids.add(gridIdx); } }
/** */ private IgniteInternalFuture loopServerRestarts() { return GridTestUtils.runAsync(() -> { try { for (int i = 0; i < RESTARTS; ++i) { stopGrid(1); U.sleep(500); startGrid(1); U.sleep(500); } } catch (Exception e) { e.printStackTrace(System.err); fail("Unexpected exception on server restart: " + e.getMessage()); } }); } }
/** */ private void checkPeekDoesNotSeeActiveVersions() throws Exception { Integer pk = primaryKey(cache); cache.put(pk, 1); CountDownLatch writeCompleted = new CountDownLatch(1); CountDownLatch checkCompleted = new CountDownLatch(1); IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(() -> { try (Transaction tx = grid(0).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) { cache.put(pk, 2); writeCompleted.countDown(); checkCompleted.await(); tx.commit(); } return null; }); writeCompleted.await(); assertEquals(1, cache.localPeek(pk)); checkCompleted.countDown(); fut.get(); }
/** * @param timeMillis Loading time in milliseconds. */ public IgniteInternalFuture<?> loadByTime(int timeMillis) { GridCompoundFuture<?, ?> loadFut = new GridCompoundFuture(); for (CacheLoader cacheLoader : cacheLoaders) { long endTime = U.currentTimeMillis() + timeMillis; cacheLoader.stopPredicate = it -> U.currentTimeMillis() >= endTime; loadFut.add(GridTestUtils.runAsync(cacheLoader)); } loadFut.markInitialized(); return loadFut; }
/** * Loads cache using closure and asserts cache size. * * @param f cache loading closure * @throws Exception if failed */ protected void loadCache(IgniteInClosure<Ignite> f) throws Exception { Ignite g0 = startGrid(0); IgniteInternalFuture fut = GridTestUtils.runAsync(new Callable<Ignite>() { @Override public Ignite call() throws Exception { return startGridsMultiThreaded(1, GRIDS_CNT - 1); } }); try { f.apply(g0); } finally { fut.get(); } assertCacheSize(KEYS_CNT); }
/** * @throws Exception If failed. */ @Test public void testDeployService3() throws Exception { IgniteInternalFuture fut = GridTestUtils.runAsync(new Callable<Object>() { @Override public Object call() throws Exception { helper.clientModeThreadLocal(true); startGrid(0); return null; } }, "start-node"); helper.clientModeThreadLocal(false); startGrid(1); fut.get(); grid(0).services(grid(0).cluster()).deployNodeSingleton("test", new GridCacheAbstractFullApiSelfTest.DummyServiceImpl()); }
/** * @throws Exception Exception. */ private void checkLock(final boolean fair) throws Exception { // Check only 'false' cases here. Successful lock is tested over the grid. final IgniteLock lock = createReentrantLock("acquire", false, fair); lock.lock(); IgniteInternalFuture fut = GridTestUtils.runAsync(new Callable<Void>() { @Override public Void call() throws Exception { assertNotNull(lock); assert !lock.tryLock(); assert !lock.tryLock(10, MICROSECONDS); return null; } }); fut.get(); lock.unlock(); removeReentrantLock("acquire", fair); }
/** * Check tracking running queries for DDL. * * @throws Exception Exception in case of failure. */ public void testQueryDDL(String sql) throws Exception { newBarrier(2); IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME); SqlFieldsQuery qry = new SqlFieldsQuery(sql); IgniteInternalFuture<List<List<?>>> fut = GridTestUtils.runAsync(() -> cache.query(qry).getAll()); assertWaitingOnBarrier(); Collection<GridRunningQueryInfo> runningQueries = ignite.context().query().runningQueries(-1); assertEquals(1, runningQueries.size()); assertNoRunningQueries(ignite); runningQueries.forEach((info) -> Assert.assertEquals(qry.getSql(), info.query())); awaitTimeouted(); awaitTimeouted(); fut.get(TIMEOUT_IN_MS); }