/** * Runs given code in multiple threads. Returns future that ends upon * threads completion. If any thread failed, exception will be thrown * out of this method. * * @param r Runnable. * @param threadNum Thread number. * @param threadName Thread name. * @throws Exception If failed. * @return Future. */ protected IgniteInternalFuture<?> multithreadedAsync(Runnable r, int threadNum, String threadName) throws Exception { return GridTestUtils.runMultiThreadedAsync(r, threadNum, threadName); }
/** * Runs given code in multiple threads and asynchronously waits for all threads to complete. * If any thread failed, exception will be thrown out of this method. * * @param c Callable. * @param threadNum Thread number. * @param threadName Thread name. * @throws Exception If failed. * @return Future. */ protected IgniteInternalFuture<?> multithreadedAsync(Callable<?> c, int threadNum, String threadName) throws Exception { return GridTestUtils.runMultiThreadedAsync(c, threadNum, threadName); }
/** * @param cnt Threads count. * @return Future. */ private IgniteInternalFuture<?> startJobThreads(int cnt) { final CyclicBarrier b = new CyclicBarrier(cnt); return GridTestUtils.runMultiThreadedAsync(new Callable<Void>() { @Override public Void call() throws Exception { int idx = b.await(); Ignite node = ignite(idx % 3); IgniteCompute comp = node.compute(node.cluster().forRemotes()); comp.run(new TestJob()); return null; } }, cnt, "job-thread"); }
/** * @param nodes Number of nodes. * @param incMap Increments map. * @param fut Future to wait for. * @return Future. */ private IgniteInternalFuture<?> updateFuture(final int nodes, final Map<Integer, AtomicInteger> incMap, final IgniteInternalFuture<?> fut) { final AtomicInteger threadIdx = new AtomicInteger(0); return GridTestUtils.runMultiThreadedAsync(new Callable<Void>() { @Override public Void call() throws Exception { int node = threadIdx.incrementAndGet() % nodes; Ignite ignite = grid(node); Thread.currentThread().setName("update-" + ignite.name()); IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME); while (!fut.isDone()) incrementTx(ignite, cache, incMap); for (int i = 0; i < 50; i++) incrementTx(ignite, cache, incMap); return null; } }, nodes * 3, "update-thread"); }
/** * Runs runnable object in specified number of threads. * * @param run Target runnable. * @param threadNum Number of threads. * @param threadName Thread name. * @return Future for the run. Future returns execution time in milliseconds. */ public static IgniteInternalFuture<Long> runMultiThreadedAsync(Runnable run, int threadNum, String threadName) { return runMultiThreadedAsync(makeCallable(run, null), threadNum, threadName); }
@Nullable @Override public Object call() throws Exception { // Test latch in multiple threads on each node. IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync( new Callable<Object>() { @Nullable @Override public Object call() throws Exception { IgniteCountDownLatch latch = ignite.countDownLatch("latch", 2, false, true); assert latch != null && latch.count() == 2; log.info("Thread is going to wait on latch: " + Thread.currentThread().getName()); assert latch.await(1, MINUTES); log.info("Thread is again runnable: " + Thread.currentThread().getName()); return null; } }, 5, "test-thread" ); fut.get(); return null; } });
@Nullable @Override public Object call() throws Exception { // Test reentrant lock in multiple threads on each node. IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync( new Callable<Object>() { @Nullable @Override public Object call() throws Exception { IgniteLock lock = ignite.reentrantLock("lock", true, fair, true); assert lock != null; log.info("Thread is going to wait on reentrant lock: " + Thread.currentThread().getName()); assert lock.tryLock(1, MINUTES); log.info("Thread is again runnable: " + Thread.currentThread().getName()); lock.unlock(); return null; } }, 5, "test-thread" ); fut.get(); return null; } });
@Nullable @Override public Object call() throws Exception { // Test semaphore in multiple threads on each node. IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync( new Callable<Object>() { @Nullable @Override public Object call() throws Exception { IgniteSemaphore semaphore = ignite.semaphore("semaphore", -2, true, true); assert semaphore != null && semaphore.availablePermits() == -2; log.info("Thread is going to wait on semaphore: " + Thread.currentThread().getName() + ", node = " + ignite.cluster().localNode() + ", sem = " + semaphore); assert semaphore.tryAcquire(1, 1, MINUTES); log.info("Thread is again runnable: " + Thread.currentThread().getName() + ", node = " + ignite.cluster().localNode() + ", sem = " + semaphore); semaphore.release(); return null; } }, 5, "test-thread" ); fut.get(); return null; } });
/** */ private void doCachePuts(IgniteEx ig, long millis) throws IgniteCheckedException { IgniteCache<Integer, byte[]> cache = ig.getOrCreateCache(DEFAULT_CACHE_NAME); AtomicBoolean stop = new AtomicBoolean(); IgniteInternalFuture<Long> putFut = GridTestUtils.runMultiThreadedAsync(() -> { ThreadLocalRandom rnd = ThreadLocalRandom.current(); while (!stop.get()) cache.put(rnd.nextInt(), "Ignite".getBytes()); }, 4, "cache-filler"); U.sleep(millis); stop.set(true); putFut.get(); } }
/** * Fills Server Thread Pool with <code>qryCnt</code> queries. Given queries will wait for * <code>suspendQryLatch</code> to be released. * * @param statements Statements. * @param qryCnt Number of queries to execute. * @return <code>IgniteInternalFuture</code> in order to check whether exception was thrown or not. */ private IgniteInternalFuture<Long> fillServerThreadPool(List<Statement> statements, int qryCnt) { AtomicInteger idx = new AtomicInteger(0); return GridTestUtils.runMultiThreadedAsync(() -> { try { statements.get(idx.getAndIncrement()).executeQuery( "select * from Integer where awaitQuerySuspensionLatch();"); } catch (SQLException e) { log.error("Unexpected exception.", e); fail("Unexpected exception"); } }, qryCnt, "ThreadName"); }
/** * */ @Test public void testTransactionsBeginCommitRollbackAutocommit() throws IgniteCheckedException { GridTestUtils.runMultiThreadedAsync(new Runnable() { @Override public void run() { try { try (Connection c = c(true, NestedTxMode.ERROR)) { try (Statement s = c.createStatement()) { s.execute("BEGIN"); s.execute("COMMIT"); s.execute("BEGIN"); s.execute("ROLLBACK"); } } } catch (SQLException e) { throw new AssertionError(e); } } }, 8, "jdbc-transactions").get(); }
/** * */ @Test public void testTransactionsBeginCommitRollbackAutocommit() throws IgniteCheckedException { GridTestUtils.runMultiThreadedAsync(new Runnable() { @Override public void run() { try { try (Connection c = c(true, NestedTxMode.ERROR)) { try (Statement s = c.createStatement()) { s.execute("BEGIN"); s.execute("COMMIT"); s.execute("BEGIN"); s.execute("ROLLBACK"); } } } catch (SQLException e) { throw new AssertionError(e); } } }, 8, "jdbc-transactions").get(); }
/** * @throws Exception If failed. */ @Test public void testStartStopSameCacheMultinode() throws Exception { final AtomicInteger idx = new AtomicInteger(); IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() { @Override public Object call() throws Exception { int node = idx.getAndIncrement(); Ignite ignite = ignite(node); Thread.currentThread().setName("start-stop-" + ignite.name()); CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME); ccfg.setName("testStartStop"); for (int i = 0; i < 1000; i++) { log.info("Start cache: " + i); try (IgniteCache<Object, Object> cache = ignite.getOrCreateCache(ccfg)) { // No-op. } log.info("Stopped cache: " + i); } return null; } }, nodeCount(), "start-stop-cache"); fut.get(); }
/** * @throws Exception If failed. */ @Test public void testEntrySetIteratorMultithreaded() throws Exception { for (int i = 0; i < gridCount(); i++) jcache(i).removeAll(); final IgniteInternalFuture<?> putFut = GridTestUtils.runMultiThreadedAsync(new CAX() { @Override public void applyx() { for (int i = 0; i < entryCount(); i++) jcache().put(KEY_PREFIX + i, i); } }, 1, "put-thread"); GridTestUtils.runMultiThreaded(new CA() { @Override public void apply() { while (!putFut.isDone()) { for (Cache.Entry<String, Integer> entry : jcache()) { assert entry != null; assert entry.getKey() != null; assert entry.getKey().contains(KEY_PREFIX); } } } }, 3, "iterator-thread"); }
/** * @throws Exception If failed. */ @Test public void testCacheIteratorMultithreaded() throws Exception { for (int i = 0; i < gridCount(); i++) jcache(i).removeAll(); final IgniteInternalFuture<?> putFut = GridTestUtils.runMultiThreadedAsync(new CAX() { @Override public void applyx() { for (int i = 0; i < entryCount(); i++) jcache().put(KEY_PREFIX + i, i); } }, 1, "put-thread"); GridTestUtils.runMultiThreaded(new CA() { @Override public void apply() { while (!putFut.isDone()) { for (Cache.Entry<String, Integer> entry : jcache()) { assert entry != null; assert entry.getKey() != null; assert entry.getKey().contains(KEY_PREFIX); } } } }, 3, "iterator-thread"); }
/** * @throws Exception If any error occurs. */ @Test public void testNonSharedIpFinder() throws Exception { try { GridTestUtils.runMultiThreadedAsync(new Callable<Object>() { @Override public Object call() throws Exception { Thread.sleep(4000); return startGrid("NonSharedIpFinder-2"); } }, 1, "grid-starter"); // This node should wait until any node "from ipFinder" appears, see log messages. Ignite g = startGrid("NonSharedIpFinder-1"); assert g.cluster().localNode().order() == 2; } finally { stopAllGrids(); } }
/** * @throws Exception If failed. */ @Test public void testMergeExchangeCoordinatorChange4() throws Exception { testSpi = true; final int srvs = 4; Ignite srv0 = startGrids(srvs); mergeExchangeWaitVersion(srv0, 6); final AtomicInteger idx = new AtomicInteger(srvs); CountDownLatch latch = blockExchangeFinish(srv0, 5, F.asList(1, 2, 3, 4), F.asList(5)); IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() { @Override public Void call() throws Exception { startGrid(idx.getAndIncrement()); return null; } }, 2, "start-node"); if (latch != null && !latch.await(WAIT_SECONDS, TimeUnit.SECONDS)) fail("Failed to wait for expected messages."); stopGrid(getTestIgniteInstanceName(0), true, false); fut.get(); checkCaches(); }
/** * @throws Exception If failed. */ @Test public void testMergeServersJoin1() throws Exception { IgniteEx srv0 = startGrid(0); mergeExchangeWaitVersion(srv0, 3); final AtomicInteger idx = new AtomicInteger(1); IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() { @Override public Void call() throws Exception { startGrid(idx.getAndIncrement()); return null; } }, 2, "start-node"); fut.get(); checkCaches(); checkExchanges(srv0, 1, 3); checkExchanges(ignite(1), 3); checkExchanges(ignite(2), 3); }
/** * @throws Exception If failed. */ @Test public void testMergeServerJoin1ClientsInTopology() throws Exception { IgniteEx srv0 = startGrid(0); client.set(true); startGrid(1); client.set(true); startGrid(2); mergeExchangeWaitVersion(srv0, 5); final AtomicInteger idx = new AtomicInteger(3); IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() { @Override public Void call() throws Exception { startGrid(idx.getAndIncrement()); return null; } }, 2, "start-node"); fut.get(); checkCaches(); checkExchanges(srv0, 1, 2, 3, 5); checkExchanges(ignite(1), 2, 3, 5); checkExchanges(ignite(2), 3, 5); checkExchanges(ignite(3), 5); checkExchanges(ignite(4), 5); }
/** * @throws Exception If failed. */ @Test public void testNearOnlyPutMultithreaded() throws Exception { final Ignite ignite1 = ignite(1); assertTrue(ignite1.configuration().isClientMode()); ignite1.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<>()); final Integer key = 1; final AtomicInteger idx = new AtomicInteger(); IgniteCache<Integer, Integer> cache0 = ignite(0).cache(DEFAULT_CACHE_NAME); IgniteCache<Integer, Integer> cache1 = ignite1.cache(DEFAULT_CACHE_NAME); for (int i = 0; i < 5; i++) { log.info("Iteration: " + i); GridTestUtils.runMultiThreadedAsync(new Callable<Object>() { @Override public Object call() throws Exception { int val = idx.getAndIncrement(); IgniteCache<Integer, Integer> cache = ignite1.cache(DEFAULT_CACHE_NAME); for (int i = 0; i < 100; i++) cache.put(key, val); return null; } }, 5, "put-thread").get(); assertEquals(cache0.localPeek(key), cache1.localPeek(key)); } }