/** only works with at least one tag; returns empty if no tags */ @Override public Set<Task<?>> getTasksWithAllTags(Iterable<?> tags) { //NB: for this method retrieval for multiple tags could be made (much) more efficient (if/when it is used with multiple tags!) //by first looking for the least-used tag, getting those tasks, and then for each of those tasks //checking whether it contains the other tags (looking for second-least used, then third-least used, etc) Set<Task<?>> result = new LinkedHashSet<Task<?>>(); boolean first = true; Iterator<?> ti = tags.iterator(); while (ti.hasNext()) { Object tag = ti.next(); if (first) { first = false; result.addAll(getTasksWithTag(tag)); } else { result.retainAll(getTasksWithTag(tag)); } } return Collections.unmodifiableSet(result); }
protected void expireTransientTasks() { Set<Task<?>> transientTasks = executionManager.getTasksWithTag(BrooklynTaskTags.TRANSIENT_TASK_TAG); for (Task<?> t: transientTasks) { if (!t.isDone()) continue; executionManager.deleteTask(t); } }
@Test public void testRetrievingTasksWithMultipleTags() throws Exception { Task<?> t = new BasicTask<Void>(newNoop()); em.submit(MutableMap.of("tags", ImmutableList.of("A", "B")), t); t.get(); assertEquals(em.getTasksWithTag("A"), ImmutableList.of(t)); assertEquals(em.getTasksWithTag("B"), ImmutableList.of(t)); assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A")), ImmutableList.of(t)); assertEquals(em.getTasksWithAnyTag(ImmutableList.of("B")), ImmutableList.of(t)); assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A", "B")), ImmutableList.of(t)); assertEquals(em.getTasksWithAllTags(ImmutableList.of("A", "B")), ImmutableList.of(t)); assertEquals(em.getTasksWithAllTags(ImmutableList.of("A")), ImmutableList.of(t)); assertEquals(em.getTasksWithAllTags(ImmutableList.of("B")), ImmutableList.of(t)); }
@Test public void testRetrievingTasksWithTagsReturnsExpectedTask() throws Exception { Task<?> t = new BasicTask<Void>(newNoop()); em.submit(MutableMap.of("tag", "A"), t); t.get(); assertEquals(em.getTasksWithTag("A"), ImmutableList.of(t)); assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A")), ImmutableList.of(t)); assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A", "B")), ImmutableList.of(t)); assertEquals(em.getTasksWithAllTags(ImmutableList.of("A")), ImmutableList.of(t)); }
@Test public void testRetrievedTasksIncludesTasksInProgress() throws Exception { final CountDownLatch runningLatch = new CountDownLatch(1); final CountDownLatch finishLatch = new CountDownLatch(1); Task<Void> t = new BasicTask<Void>(new Callable<Void>() { @Override public Void call() throws Exception { runningLatch.countDown(); finishLatch.await(); return null; }}); em.submit(MutableMap.of("tags", ImmutableList.of("A")), t); try { runningLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS); assertEquals(em.getTasksWithTag("A"), ImmutableList.of(t)); } finally { finishLatch.countDown(); } }
@Test public void testRetrievingTasksWithTagsExcludesNonMatchingTasks() throws Exception { Task<?> t = new BasicTask<Void>(newNoop()); em.submit(MutableMap.of("tag", "A"), t); t.get(); assertEquals(em.getTasksWithTag("B"), ImmutableSet.of()); assertEquals(em.getTasksWithAnyTag(ImmutableList.of("B")), ImmutableSet.of()); assertEquals(em.getTasksWithAllTags(ImmutableList.of("A", "B")), ImmutableSet.of()); }
BasicTask<?> tb = (BasicTask<?>) em.getTasksWithTag("B").iterator().next(); assertEquals( 46, tb.get() ); assertEquals( t, em.getTasksWithTag("A").iterator().next() ); assertNull( t.getSubmittedByTask() );
@Test public void runMultipleBasicTasksMultipleTags() throws Exception { data.put(1, 1); Collection<Task<Integer>> tasks = Lists.newArrayList(); tasks.add(em.submit(MutableMap.of("tag", "A"), new BasicTask<Integer>(newIncrementCallable(1)))); tasks.add(em.submit(MutableMap.of("tags", ImmutableList.of("A","B")), new BasicTask<Integer>(newIncrementCallable(1)))); tasks.add(em.submit(MutableMap.of("tags", ImmutableList.of("B","C")), new BasicTask<Integer>(newIncrementCallable(1)))); tasks.add(em.submit(MutableMap.of("tags", ImmutableList.of("D")), new BasicTask<Integer>(newIncrementCallable(1)))); int total = 0; for (Task<Integer> t : tasks) { log.debug("BasicTask {}, has {}", t, t.get()); total += t.get(); } assertEquals(10, total); //now that all have completed: assertEquals(data.get(1), 5); assertEquals(em.getTasksWithTag("A").size(), 2); assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A")).size(), 2); assertEquals(em.getTasksWithAllTags(ImmutableList.of("A")).size(), 2); assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A", "B")).size(), 3); assertEquals(em.getTasksWithAllTags(ImmutableList.of("A", "B")).size(), 1); assertEquals(em.getTasksWithAllTags(ImmutableList.of("B", "C")).size(), 1); assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A", "D")).size(), 3); }
@Test public void runMultipleBasicTasks() throws Exception { data.put(1, 1); BasicExecutionManager em = new BasicExecutionManager("mycontext"); for (int i = 0; i < 2; i++) { em.submit(MutableMap.of("tag", "A"), new BasicTask<Integer>(newIncrementCallable(1))); em.submit(MutableMap.of("tag", "B"), new BasicTask<Integer>(newIncrementCallable((1)))); } int total = 0; for (Object tag : em.getTaskTags()) { log.debug("tag {}", tag); for (Task<?> task : em.getTasksWithTag(tag)) { log.debug("BasicTask {}, has {}", task, task.get()); total += (Integer)task.get(); } } assertEquals(10, total); //now that all have completed: assertEquals(5, data.get(1)); }