Java Code Examples for org.apache.brooklyn.api.mgmt.Task#cancel()
The following examples show how to use
org.apache.brooklyn.api.mgmt.Task#cancel() .
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: SshMachineLocation.java From brooklyn-server with Apache License 2.0 | 6 votes |
@Override public void close() throws IOException { LoadingCache<Map<String, ?>, Pool<SshTool>> sshPoolCacheOrNullRef = sshPoolCacheOrNull; Task<?> cleanupTaskRef = cleanupTask; if (sshPoolCacheOrNullRef != null) { if (LOG.isDebugEnabled()) { LOG.debug("{} invalidating all entries in ssh pool cache. Final stats: {}", this, sshPoolCacheOrNullRef.stats()); } sshPoolCacheOrNullRef.invalidateAll(); } if (cleanupTaskRef != null) { cleanupTaskRef.cancel(false); } cleanupTask = null; sshPoolCacheOrNull = null; }
Example 2
Source File: BrooklynDslDeferredSupplier.java From brooklyn-server with Apache License 2.0 | 5 votes |
@Override public final T get() { if (log.isTraceEnabled()) log.trace("Queuing task to resolve {}, called by {}", dsl, Tasks.current()); ExecutionContext exec = BrooklynTaskTags.getCurrentExecutionContext(); if (exec == null) { throw new IllegalStateException("No execution context available to resolve " + dsl); } Task<T> task = newTask(); T result; try { result = exec.submit(task).get(); } catch (InterruptedException | ExecutionException e) { Task<?> currentTask = Tasks.current(); if (currentTask != null && currentTask.isCancelled()) { task.cancel(true); } throw Exceptions.propagate(e); } if (log.isTraceEnabled()) { // https://issues.apache.org/jira/browse/BROOKLYN-269 // We must not log sensitve data, such as from $brooklyn:external, or if the value // is to be used as a password etc. Unfortunately we don't know the context, so can't // use Sanitizer.sanitize. log.trace("Resolved "+dsl); } return result; }
Example 3
Source File: DynamicClusterTest.java From brooklyn-server with Apache License 2.0 | 4 votes |
@Override protected <T> Task<?> newThrottledEffectorTask(Entity target, Effector<T> effector, Map<?, ?> arguments, boolean isPrivileged) { Task<?> unsubmitted = super.newThrottledEffectorTask(target, effector, arguments, isPrivileged); unsubmitted.cancel(true); return unsubmitted; }
Example 4
Source File: DynamicSequentialTaskTest.java From brooklyn-server with Apache License 2.0 | 4 votes |
@Test public void testCancelled() throws Exception { Task<List<?>> t = Tasks.sequential( sayTask("1"), sayTask("2a", Duration.THIRTY_SECONDS, "2b"), sayTask("3")); ec.submit(t); // wait for 2 to start, saying "2a", and the first interruptible block is when it waits for its 30s waitForMessages(Predicates.compose(MathPredicates.greaterThanOrEqual(2), CollectionFunctionals.sizeFunction()), TIMEOUT); Assert.assertEquals(messages, Arrays.asList("1", "2a")); // now cancel, and make sure we get the right behaviour t.cancel(true); Assert.assertTrue(t.isDone()); // 2 should get cancelled, and invoke the cancellation semaphore, but not say 2b // 3 should get cancelled and not run at all // cancel(..) currently cancels everything in the tree in the calling thread // so we could even assert task3.isCancelled() now // but not sure we will guarantee that for subtasks, so weaker assertion // that it is eventually cancelled, and that it for sure never starts // message list is still 1, 2a Assert.assertEquals(messages, Arrays.asList("1", "2a")); // And 2 when cancelled should release the semaphore log.info("testCancelled waiting on semaphore; permits left is "+cancellations.availablePermits()); Assert.assertTrue(cancellations.tryAcquire(10, TimeUnit.SECONDS)); log.info("testCancelled acquired semaphore; permits left is "+cancellations.availablePermits()); Iterator<Task<?>> ci = ((HasTaskChildren)t).getChildren().iterator(); // 1 completed fine Assert.assertEquals(ci.next().get(), "1"); // 2 is cancelled -- cancelled flag should always be set *before* the interrupt sent // (and that released the semaphore above, even if thread is not completed, so this should be set) Task<?> task2 = ci.next(); Assert.assertTrue(task2.isBegun()); Assert.assertTrue(task2.isDone()); Assert.assertTrue(task2.isCancelled()); Task<?> task3 = ci.next(); // 3 is being cancelled in the thread which cancelled 2, and should either be // *before* 2 was cancelled or *not run* because the parent was cancelled // so we shouldn't need to wait ... but if we did: // Asserts.eventually(Suppliers.ofInstance(task3), TaskPredicates.isDone()); Assert.assertTrue(task3.isDone()); Assert.assertTrue(task3.isCancelled()); Assert.assertFalse(task3.isBegun()); // messages unchanged Assert.assertEquals(messages, Arrays.asList("1", "2a")); // no further mutexes should be available (ie 3 should not run) // TODO for some reason this was observed to fail on the jenkins box (2016-01) // but i can't see why; have added logging in case it happens again Assert.assertEquals(cancellations.availablePermits(), 0); }
Example 5
Source File: DynamicSequentialTaskTest.java From brooklyn-server with Apache License 2.0 | 4 votes |
public void doTestCancellationModeAndSubmitted( boolean isSubtaskTransient, Object cancellationMode, boolean expectedTaskInterrupted, boolean expectedSubtaskCancelled ) throws Exception { tearDown(); setUp(); final Task<String> t1 = sayTask("1-wait", Duration.minutes(10), "1-done"); if (isSubtaskTransient) { BrooklynTaskTags.addTagDynamically(t1, BrooklynTaskTags.TRANSIENT_TASK_TAG); } final Task<List<?>> t = Tasks.parallel( submitting(t1), sayTask("2-wait", Duration.minutes(10), "2-done")); ec.submit(t); waitForMessages(Predicates.compose(MathPredicates.greaterThanOrEqual(2), CollectionFunctionals.sizeFunction()), TIMEOUT); Asserts.assertEquals(MutableSet.copyOf(messages), MutableSet.of("1-wait", "2-wait")); if (cancellationMode==null) { ((TaskInternal<?>)t).cancel(); } else if (cancellationMode instanceof Boolean) { t.cancel((Boolean)cancellationMode); } else if (cancellationMode instanceof TaskCancellationMode) { ((TaskInternal<?>)t).cancel((TaskCancellationMode)cancellationMode); } else { throw new IllegalStateException("Invalid cancellationMode: "+cancellationMode); } // the cancelled task always reports cancelled and done right away Assert.assertEquals(t.isDone(), true); Assert.assertEquals(t.isCancelled(), true); if (expectedTaskInterrupted) { // end time might not be set for another fraction of a second, when task truly ended, but should be set soon if task was interrupted Asserts.eventually( () -> t.getEndTimeUtc(), MathPredicates.greaterThanOrEqual(0), null, null, null, t ); } else { // if interrupts not allowed, time should not be set until long delay expires Assert.assertTrue(t.getEndTimeUtc() < 0, "Wrong end time: "+t.getEndTimeUtc()); } if (expectedSubtaskCancelled) { Asserts.eventually(Suppliers.ofInstance(t1), TaskPredicates.isDone()); Assert.assertTrue(t1.isCancelled()); Asserts.eventually( () -> t1.getEndTimeUtc(), MathPredicates.greaterThanOrEqual(0), null, null, null, t ); } else { Time.sleep(TINY_TIME); Assert.assertFalse(t1.isCancelled()); Assert.assertFalse(t1.isDone()); } }
Example 6
Source File: BasicTasksFutureTest.java From brooklyn-server with Apache License 2.0 | 4 votes |
public void doTestCancelTriggersListenableFuture(Duration delayBeforeSubmit, Duration delayBeforeCancel, boolean dynamic) throws Exception { Task<String> t = waitForSemaphore(Duration.TEN_SECONDS, true, "x", dynamic); addFutureListener(t, "before"); Stopwatch watch = Stopwatch.createStarted(); if (delayBeforeSubmit!=null) { new Thread(() -> { Time.sleep(delayBeforeSubmit); ec.submit(t); }).start(); } else { ec.submit(t); } addFutureListener(t, "during"); log.info("test cancelling "+t+" ("+t.getClass()+") after "+delayBeforeCancel+ ", submit delay "+delayBeforeSubmit); // NB: three different code paths (callers to this method) for notifying futures // depending whether task is started before, at, or after the cancel if (delayBeforeCancel!=null) Time.sleep(delayBeforeCancel); synchronized (data) { t.cancel(true); assertSoonGetsData("before"); assertSoonGetsData("during"); addFutureListener(t, "after"); Assert.assertNull(data.get("after")); assertSoonGetsData("after"); } Assert.assertTrue(t.isDone()); Assert.assertTrue(t.isCancelled()); try { t.get(); Assert.fail("should have thrown CancellationException"); } catch (CancellationException e) { /* expected */ } Assert.assertTrue(watch.elapsed(TimeUnit.MILLISECONDS) < Duration.FIVE_SECONDS.toMilliseconds(), Time.makeTimeStringRounded(watch.elapsed(TimeUnit.MILLISECONDS))+" is too long; should have cancelled very quickly"); if (started.tryAcquire()) // if the task is begun, this should get released Assert.assertTrue(cancelledWhileSleeping.tryAcquire(5, TimeUnit.SECONDS)); }