@BeforeMethod(alwaysRun=true) public void setUp() throws Exception { em = new BasicExecutionManager("mycontext"); }
public void shutdownNow() { shutdownNow(null); }
public String getUsageString() { return makeBasicUsageString()+"; "+ // ignore storage // "storage: " + storage.getStorageMetrics() + "; " + "tasks: " + executionManager.getNumActiveTasks()+" active, "+ executionManager.getNumIncompleteTasks()+" unfinished; "+ executionManager.getNumInMemoryTasks()+" remembered, "+ executionManager.getTotalTasksSubmitted()+" total submitted)"; }
protected void expireTransientTasks() { Set<Task<?>> transientTasks = executionManager.getTasksWithTag(BrooklynTaskTags.TRANSIENT_TASK_TAG); for (Task<?> t: transientTasks) { if (!t.isDone()) continue; executionManager.deleteTask(t); } }
@BeforeMethod public void setUp() { em = new BasicExecutionManager("mycontextid"); em.setTaskSchedulerForTag("category1", SingleThreadedScheduler.class); }
@Test public void testScheduledTaskResubmittedIfExceptionThrownAndCancelOnExceptionFalse() { BasicExecutionManager m = new BasicExecutionManager("mycontextid"); final AtomicInteger calls = new AtomicInteger(0); ScheduledTask t = new ScheduledTask(MutableMap.of("period", Duration.ONE_MILLISECOND, "maxIterations", 5, "cancelOnException", false), new Callable<Task<?>>() { @Override public Task<?> call() throws Exception { return new BasicTask<>(new Callable<Integer>() { @Override public Integer call() { calls.incrementAndGet(); throw new RuntimeException("boo"); }}); }}); m.submit(t); t.blockUntilEnded(); assertEquals(calls.get(), 5, "Expected task to be resubmitted despite throwing an exception"); }
@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(groups = "Integration") @Override public void testGetMachineDetails() throws Exception { BasicExecutionManager execManager = new BasicExecutionManager("mycontextid"); BasicExecutionContext execContext = new BasicExecutionContext(execManager); try { MachineDetails details = execContext.submit(new Callable<MachineDetails>() { @Override public MachineDetails call() { return host.getMachineDetails(); }}).get(); LOG.info("machineDetails="+details); assertNotNull(details); } finally { execManager.shutdownNow(); } }
@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)); }
@Override public Boolean call() throws Exception { origManagementContext.getGarbageCollector().gcIteration(); List<Task<?>> tasksAfter = ((BasicExecutionManager)origManagementContext.getExecutionManager()).getAllTasks(); LOG.info("tasks after disabling HA, "+tasksAfter.size()+": "+tasksAfter); return tasksAfter.isEmpty(); } }).runRequiringTrue();
@Override public Boolean call() throws Exception { ((LocalManagementContext)mgmt).getGarbageCollector().gcIteration(); long taskCountAfterAtOld = ((BasicExecutionManager)mgmt.getExecutionManager()).getNumIncompleteTasks(); List<Task<?>> tasks = ((BasicExecutionManager)mgmt.getExecutionManager()).getAllTasks(); int unendedTasks = 0, extraAllowedMax = 0; for (Task<?> t: tasks) { if (!t.isDone()) { if (skipKnownBackgroundTasks) { if (t.toString().indexOf("ssh-location cache cleaner")>=0) { extraAllowedMax++; } } unendedTasks++; } } LOG.info("Count of incomplete tasks now "+taskCountAfterAtOld+", "+unendedTasks+" unended" + (extraAllowedMax>0 ? " ("+extraAllowedMax+" allowed)" : "") + "; tasks remembered are: "+ tasks); return taskCountAfterAtOld<=allowedMax+extraAllowedMax; } }).runRequiringTrue();
@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(); } }
protected int expireSubTasksWhoseSubmitterIsExpired() { // ideally we wouldn't have this; see comments on CHECK_SUBTASK_SUBMITTERS if (!brooklynProperties.getConfig(CHECK_SUBTASK_SUBMITTERS)) return 0; Collection<Task<?>> allTasks = executionManager.allTasksLive(); Collection<Task<?>> tasksToDelete = MutableList.of(); try { for (Task<?> task: allTasks) { if (!task.isDone()) continue; Task<?> submitter = task.getSubmittedByTask(); // if we've leaked, ie a subtask which is not a child task, // and the submitter is GC'd, then delete this also if (submitter!=null && submitter.isDone() && executionManager.getTask(submitter.getId())==null) { tasksToDelete.add(task); } } } catch (ConcurrentModificationException e) { // delete what we've found so far LOG.debug("Got CME inspecting aged tasks, with "+tasksToDelete.size()+" found for deletion: "+e); } for (Task<?> task: tasksToDelete) { executionManager.deleteTask(task); } return tasksToDelete.size(); }
protected int expireIfOverCapacityGlobally() { Collection<Task<?>> tasksLive = executionManager.allTasksLive(); if (tasksLive.size() <= brooklynProperties.getConfig(MAX_TASKS_GLOBAL)) return 0; tasksLive = MutableList.copyOf(tasksLive); } catch (ConcurrentModificationException e) { tasksLive = executionManager.getTasksWithAllTags(MutableList.of()); executionManager.deleteTask( tasks.get(numDeleted++) ); LOG.debug("brooklyn-gc deleted "+numDeleted+" tasks as was over global limit, now have "+executionManager.allTasksLive().size()); return numDeleted;
@Test public void testRetrievingTasksWithAllTagsWhenFirstNotMatched() throws Exception { Task<?> t = new BasicTask<Void>(newNoop()); em.submit(MutableMap.of("tags", ImmutableList.of("A")), t); t.get(); assertEquals(em.getTasksWithAllTags(ImmutableList.of("not_there","A")), ImmutableSet.of()); }
private void refreshSensors() { ManagementContext managementContext = getManagementContext(); BasicExecutionManager execManager = (BasicExecutionManager) (managementContext != null ? managementContext.getExecutionManager() : null); LocalSubscriptionManager subsManager = (LocalSubscriptionManager) (managementContext != null ? managementContext.getSubscriptionManager() : null); if (managementContext != null) { sensors().set(TOTAL_EFFECTORS_INVOKED, ((ManagementContextInternal)managementContext).getTotalEffectorInvocations()); } if (execManager != null) { sensors().set(TOTAL_TASKS_SUBMITTED, execManager.getTotalTasksSubmitted()); sensors().set(NUM_INCOMPLETE_TASKS, execManager.getNumIncompleteTasks()); sensors().set(NUM_ACTIVE_TASKS, execManager.getNumActiveTasks()); } if (subsManager != null) { sensors().set(TOTAL_EVENTS_PUBLISHED, subsManager.getTotalEventsPublished()); sensors().set(TOTAL_EVENTS_DELIVERED, subsManager.getTotalEventsDelivered()); sensors().set(NUM_SUBSCRIPTIONS, subsManager.getNumSubscriptions()); } } }
public AsyncCollectionChangeAdapter(ExecutionManager executor, CollectionChangeListener<Item> delegate) { this.executor = checkNotNull(executor, "executor"); this.delegate = checkNotNull(delegate, "delegate"); ((BasicExecutionManager) executor).setTaskSchedulerForTag(delegate, SingleThreadedScheduler.class); }
public void setTaskSchedulerForTag(Object tag, Class<? extends TaskScheduler> scheduler) { synchronized (schedulerByTag) { TaskScheduler old = getTaskSchedulerForTag(tag); if (old!=null) { if (scheduler.isAssignableFrom(old.getClass())) { /* already have such an instance */ return; } //might support multiple in future... throw new IllegalStateException("Not allowed to set multiple TaskSchedulers on ExecutionManager tag (tag "+tag+", has "+old+", setting new "+scheduler+")"); } try { TaskScheduler schedulerI = scheduler.newInstance(); // allow scheduler to have a nice name, for logging etc if (schedulerI instanceof CanSetName) ((CanSetName)schedulerI).setName(""+tag); setTaskSchedulerForTag(tag, schedulerI); } catch (InstantiationException e) { throw Exceptions.propagate(e); } catch (IllegalAccessException e) { throw Exceptions.propagate(e); } } }
Set<Object> taskTags = executionManager.getTaskTags(); if (isTagIgnoredForGc(tag)) continue; Set<Task<?>> tasksWithTag = executionManager.tasksWithTagLiveOrNull(tag); if (tasksWithTag==null) continue; AtomicInteger overA = null;