/** * Cancel a random task when required. */ private void performCancel() { iteration++; if (iteration % cancelRate == 0) { // Here we should have mostly running futures so just pick one. ComputeTaskFuture<Integer> futToCancel = futures.get( new Random().nextInt(futures.size()) ); try { futToCancel.cancel(); ignite.log().info("Task canceled: " + futToCancel.getTaskSession().getId()); } catch (IgniteException e) { ignite.log().warning(">>> Future cancellation failed: " + futToCancel.getTaskSession().getId(), e); } } } }
Iterable<UUID> nodeIds = (Iterable<UUID>)fut.getTaskSession().getAttribute("nodes");
fut.getTaskSession().setAttribute("attr1", 1); fut.getTaskSession().setAttribute("attr2", 2);
/** * Finds completed tasks in the queue and removes them. */ private void checkCompletion() { for (Iterator<ComputeTaskFuture<Integer>> iter = futures.iterator(); iter.hasNext();) { ComputeTaskFuture<Integer> fut = iter.next(); if (fut.isDone()) { try { Integer res = fut.get(); assert res == params.getJobsCount() : "Task returned wrong result [taskIs=" + fut.getTaskSession().getId() + ", result=" + res + "]"; ignite.log().info(">>> Task completed successfully. Task id: " + fut.getTaskSession().getId()); } catch (IgniteFutureCancelledException ignored) { ignite.log().info(">>> Task cancelled: " + fut.getTaskSession().getId()); } catch (IgniteException e) { ignite.log().warning( ">>> Get operation for completed task failed: " + fut.getTaskSession().getId(), e); } finally { iter.remove(); } } } }
/** * @throws Exception If failed. */ @Test public void testAsynchronousTimeout() throws Exception { Ignite ignite = G.ignite(getTestIgniteInstanceName()); ignite.compute().localDeployTask(GridTaskTimeoutTestTask.class, GridTaskTimeoutTestTask.class.getClassLoader()); ComputeTaskFuture<?> fut = executeAsync(ignite.compute().withTimeout(TIMEOUT), GridTaskTimeoutTestTask.class.getName(), null); // Allow timed out events to be executed. Thread.sleep(TIMEOUT + 500); checkTimedOutEvents(fut.getTaskSession().getId()); }
/** * @param num Number. * @throws InterruptedException if failed. * @throws IgniteCheckedException if failed. */ private void checkTask(int num) throws InterruptedException, IgniteCheckedException { Ignite ignite = G.ignite(getTestIgniteInstanceName()); ComputeTaskFuture<?> fut = executeAsync(ignite.compute(), GridTaskSessionTestTask.class.getName(), num); assert fut != null; try { // Wait until task receive results from jobs. boolean await = startSignal[num].await(WAIT_TIME, TimeUnit.MILLISECONDS); assert await : "Jobs did not executed."; String val = fut.getTaskSession().waitForAttribute("testName", WAIT_TIME); info("Received attribute 'testName': " + val); // Signal task to finish work. stopSignal[num].countDown(); assert "testVal".equals(val) : "Invalid attribute value: " + val; Object res = fut.get(); assert (Integer)res == SPLIT_COUNT : "Invalid result [num=" + num + ", fut=" + fut + ']'; } finally { // We must wait for the jobs to be sure that they have completed // their execution since they use static variable (shared for the tests). fut.get(); } }
/** * @param num Number. * @throws InterruptedException if failed. */ private void checkTask(int num) throws InterruptedException { Ignite ignite = G.ignite(getTestIgniteInstanceName()); ComputeTaskFuture<?> fut = ignite.compute().executeAsync(GridTaskSessionTestTask.class.getName(), num); assert fut != null; try { // Wait until task receive results from jobs. boolean await = startSignal[num].await(WAIT_TIME, TimeUnit.MILLISECONDS); assert await : "Jobs did not executed."; String val = fut.getTaskSession().waitForAttribute("testName", 100000); info("Received attribute 'testName': " + val); // Signal jobs to finish work. stopSignal[num].countDown(); assert "testVal".equals(val) : "Invalid attribute value: " + val; Object res = fut.get(); assert (Integer)res == SPLIT_COUNT : "Invalid result [num=" + num + ", fut=" + fut + ']'; } finally { // We must wait for the jobs to be sure that they have completed // their execution since they use static variable (shared for the tests). fut.get(); } }
@Override public Object call() throws Exception { ComputeTaskFuture f = null; try { for (int i = 0; i < EXEC_CNT; i++) assertEquals(Boolean.TRUE, (f = executeAsync(ignite.compute().withName("task-name"), SessionLoadTestTask.class, ignite.cluster().nodes().size() * 2)).get(20000)); } catch (Exception e) { U.error(log, "Task failed: " + f != null ? f.getTaskSession().getId() : "N/A", e); throw e; } finally { info("Thread finished."); } return null; } }, THREAD_CNT, "grid-load-test-thread");
Collection<ComputeJobSibling> jobSiblings = fut.getTaskSession().getJobSiblings();
/** * @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()); }
/** * @throws Exception If failed. */ @Test public void testSetAttribute() throws Exception { Ignite ignite = G.ignite(getTestIgniteInstanceName()); ignite.compute().localDeployTask(GridTaskSessionTestTask.class, GridTaskSessionTestTask.class.getClassLoader()); for (int i = 0; i < 1; i++) { refreshInitialData(); ComputeTaskFuture<?> fut = executeAsync(ignite.compute(), GridTaskSessionTestTask.class.getName(), null); assert fut != null; try { // Wait until jobs begin execution. boolean await = startSignal.await(WAIT_TIME, TimeUnit.MILLISECONDS); assert await : "Jobs did not start."; fut.getTaskSession().addAttributeListener(lsnr, true); info("Setting attribute 'testName'."); fut.getTaskSession().setAttribute("testName", "testVal"); Object res = fut.get(); assert (Integer)res == SPLIT_COUNT : "Invalid result [i=" + i + ", fut=" + fut + ']'; assert !lsnr.getAttributes().isEmpty() : "No attributes found."; } finally { // We must wait for the jobs to be sure that they have completed // their execution since they use static variable (shared for the tests). fut.get(); } } }
/** * @throws Exception If failed. */ @Test public void testSynchronousTimeout() throws Exception { Ignite ignite = G.ignite(getTestIgniteInstanceName()); ignite.compute().localDeployTask(GridTaskTimeoutTestTask.class, GridTaskTimeoutTestTask.class.getClassLoader()); ComputeTaskFuture<?> fut = executeAsync(ignite.compute().withTimeout(TIMEOUT), GridTaskTimeoutTestTask.class.getName(), null); try { fut.get(); assert false : "ComputeTaskTimeoutException was not thrown (synchronous apply)"; } catch (ComputeTaskTimeoutException e) { info("Received expected timeout exception (synchronous apply): " + e); } Thread.sleep(TIMEOUT + 500); checkTimedOutEvents(fut.getTaskSession().getId()); }
/** * @throws Exception If failed. */ @Test public void testResultFailed() throws Exception { Ignite ignite = G.ignite(getTestIgniteInstanceName()); ignite.compute().localDeployTask(GridTaskFailedTestTask.class, GridTaskFailedTestTask.class.getClassLoader()); ComputeTaskFuture<?> fut = executeAsync(ignite.compute(), GridTaskFailedTestTask.class.getName(), FailType.RESULT); try { fut.get(); assert false; } catch (IgniteException e) { info("Got expected grid exception: " + e); } IgniteUuid sesId = fut.getTaskSession().getId(); // Query for correct events. List<Event> evts = ignite.events().remoteQuery(new TaskFailedEventFilter(sesId), 0); assert evts.size() == 1; info("Task failed event: " + evts.get(0)); }
/** * @throws Exception If failed. */ @Test public void testReduceFailed() throws Exception { Ignite ignite = G.ignite(getTestIgniteInstanceName()); ignite.compute().localDeployTask(GridTaskFailedTestTask.class, GridTaskFailedTestTask.class.getClassLoader()); ComputeTaskFuture<?> fut = executeAsync(ignite.compute(), GridTaskFailedTestTask.class.getName(), FailType.RESULT); try { fut.get(); assert false; } catch (IgniteException e) { info("Got expected grid exception: " + e); } IgniteUuid sesId = fut.getTaskSession().getId(); // Query for correct events. List<Event> evts = ignite.events().remoteQuery(new TaskFailedEventFilter(sesId), 0); assert evts.size() == 1; info("Task failed event: " + evts.get(0)); }
fut.getTaskSession().saveCheckpoint("future:session:key", "future:session:testval"); fut.getTaskSession().saveCheckpoint("future:global:key", "future:global:testval", ComputeTaskSessionScope.GLOBAL_SCOPE, 0);
/** * @throws Exception If failed. */ @Test public void testExecuteFailed() throws Exception { Ignite ignite = G.ignite(getTestIgniteInstanceName()); ignite.compute().localDeployTask(GridTaskFailedTestTask.class, GridTaskFailedTestTask.class.getClassLoader()); ComputeTaskFuture<?> fut = executeAsync(ignite.compute(), GridTaskFailedTestTask.class.getName(), FailType.EXECUTE); try { fut.get(); assert false; } catch (IgniteException e) { info("Got expected grid exception: " + e); } IgniteUuid sesId = fut.getTaskSession().getId(); // Query for correct events. List<Event> evts = ignite.events().remoteQuery(new TaskFailedEventFilter(sesId), 0); info("Job failed event: " + evts.get(0)); assert evts.size() == 1; }
/** * @throws Exception If failed. */ @Test public void testMapFailed() throws Exception { Ignite ignite = G.ignite(getTestIgniteInstanceName()); ignite.compute().localDeployTask(GridTaskFailedTestTask.class, GridTaskFailedTestTask.class.getClassLoader()); ComputeTaskFuture<?> fut = executeAsync(ignite.compute(), GridTaskFailedTestTask.class.getName(), FailType.MAP); try { fut.get(); assert false; } catch (IgniteException e) { info("Got expected grid exception: " + e); } IgniteUuid sesId = fut.getTaskSession().getId(); // Query for correct events. List<Event> evts = ignite.events().remoteQuery(new TaskFailedEventFilter(sesId), 0); assert evts.size() == 1; info("Task failed event: " + evts.get(0)); }
/** * @param num Number. * @throws InterruptedException if failed. * @throws IgniteCheckedException if failed. */ private void checkTask(int num) throws InterruptedException, IgniteCheckedException { Ignite ignite = G.ignite(getTestIgniteInstanceName()); ComputeTaskFuture<?> fut = executeAsync(ignite.compute(), GridTaskSessionTestTask.class, num); assert fut != null; try { // Wait until jobs begin execution. boolean await = startSignal[num].await(WAIT_TIME, TimeUnit.MILLISECONDS); assert await : "Jobs did not start."; info("Set attribute 'testName'."); fut.getTaskSession().setAttribute("testName", "testVal"); Object res = fut.get(); assert (Integer)res == SPLIT_COUNT : "Invalid result [num=" + num + ", fut=" + fut + ']'; } finally { // We must wait for the jobs to be sure that they have completed // their execution since they use static variable (shared for the tests). fut.get(); } }
/** * @throws Exception If failed. */ @Test public void testJobSetAttribute() throws Exception { try { Ignite ignite1 = startGrid(1); Ignite ignite2 = startGrid(2); ignite1.compute().localDeployTask(SessionTestTask.class, SessionTestTask.class.getClassLoader()); for (int i = 0; i < TESTS_COUNT; i++) { ComputeTaskFuture<?> fut = ignite1.compute().withTimeout(100000).executeAsync( SessionTestTask.class.getName(), ignite2.cluster().localNode().id()); fut.getTaskSession().setAttribute(TEST_ATTR_KEY, SETS_ATTR_COUNT); Integer res = (Integer)fut.get(); assert res != null && res.equals(SETS_ATTR_COUNT) : "Unexpected result [res=" + res + ", expected=" + SETS_ATTR_COUNT + ']'; info("Session attribute value was correct for test [res=" + res + ", expected=" + SETS_ATTR_COUNT + ']'); } } finally { stopAllGrids(false); } }
fut.getTaskSession().mapFuture().get(); ComputeTaskSession ses = fut.getTaskSession();