private JsonObject formatJobPlan(ClusterJob job) throws IOException { JsonObject jobJson = new JsonObject(); jobJson.addProperty("id", job.getJobId()); jobJson.addProperty("clusterId", job.getClusterId()); jobJson.addProperty("action", job.getClusterAction().name()); jobJson.addProperty("currentStage", job.getCurrentStageNumber()); JsonArray stagesJson = new JsonArray(); for (Set<String> stage : job.getStagedTasks()) { JsonArray stageJson = new JsonArray(); for (String taskId : stage) { ClusterTask task = clusterStore.getClusterTask(TaskId.fromString(taskId)); JsonObject taskJson = new JsonObject(); taskJson.addProperty("id", task.getTaskId()); taskJson.addProperty("taskName", task.getTaskName().name()); taskJson.addProperty("nodeId", task.getNodeId()); taskJson.addProperty("service", task.getService()); stageJson.add(taskJson); } stagesJson.add(stageJson); } jobJson.add("stages", stagesJson); return jobJson; }
/** * Sets the status of the given job to {@link ClusterJob.Status#FAILED} and the status of the cluster to the default * failure status as given in {@link co.cask.coopr.scheduler.ClusterAction#getFailureStatus()}. * * @param job Job to fail. * @param cluster Cluster to set the status for. * @throws IOException */ public void failJobAndSetClusterStatus(ClusterJob job, Cluster cluster) throws IOException, IllegalAccessException { failJobAndSetClusterStatus(job, cluster, job.getClusterAction().getFailureStatus(), null); }
private Set<ClusterAction> parseActionsString(String actionsStr) { if (actionsStr == null) { return ImmutableSet.of(); } Iterator<String> actionIter = Splitter.on(',').split(actionsStr).iterator(); Set<ClusterAction> actions = Sets.newHashSet(); while (actionIter.hasNext()) { String actionStr = actionIter.next(); try { ClusterAction action = ClusterAction.valueOf(actionStr.toUpperCase()); actions.add(action); } catch (IllegalArgumentException e) { LOG.warn("Unknown cluster action " + actionStr + ". Hooks will not be executed for that action"); } } return actions; }
long jobNum = 1; for (ClusterAction action : ClusterAction.values()) { if (action == ClusterAction.CLUSTER_DELETE) { continue;
clusterQueues.add(queueName, new Element(cluster.getId(), ClusterAction.CLUSTER_CREATE.name()));
Cluster cluster = clusterStore.getCluster(clusterElement.getId()); ClusterJob job = clusterStore.getClusterJob(JobId.fromString(cluster.getLatestJobId())); ClusterAction clusterAction = ClusterAction.valueOf(clusterElement.getValue()); LOG.debug("Got cluster {} with action {}", cluster.getName(), clusterAction); try {
clusterQueues.add(account.getTenantId(), new Element(clusterId, action.name())); } finally { lock.unlock();
clusterQueues.add(queueName, new Element(cluster.getId(), ClusterAction.ADD_SERVICES.name())); return "Solved";
clusterQueues.add(account.getTenantId(), new Element(clusterId, action.name())); } finally { lock.unlock();
clusterQueues.add(account.getTenantId(), new Element(clusterId, ClusterAction.CLUSTER_DELETE.name())); } finally { lock.unlock();
assertResponseStatus(doPostExternalAPI("/clusters/123/services" + entry.getKey(), "", USER1_HEADERS), HttpResponseStatus.OK); Assert.assertEquals(entry.getValue().name(), clusterQueues.take(cluster.getAccount().getTenantId(), "0").getValue()); cluster.setStatus(Cluster.Status.ACTIVE);
@Test public void testOnlyCorrectClustersAreCleaned() throws Exception { long now = System.currentTimeMillis(); for (int i = 0; i < 20; i++) { createCluster(String.valueOf(i), now - 1000, now - 100, Cluster.Status.ACTIVE); } String queueName = account.getTenantId(); ClusterCleanup clusterCleanup = new ClusterCleanup(clusterStore, clusterService, nodeService, taskService, jobQueues, provisionerQueues, -10, 3, 7); Assert.assertEquals(0, Iterators.size(clusterQueues.getQueued(queueName))); clusterCleanup.run(); // clusters 3, 10, and 17 should have been scheduled for deletion Assert.assertEquals(3, Iterators.size(clusterQueues.getQueued(queueName))); Element e1 = clusterQueues.take(queueName, "consumer1"); Element e2 = clusterQueues.take(queueName, "consumer1"); Element e3 = clusterQueues.take(queueName, "consumer1"); Assert.assertEquals(ImmutableSet.of("3", "10", "17"), ImmutableSet.of(e1.getId(), e2.getId(), e3.getId())); Assert.assertEquals(ClusterAction.CLUSTER_DELETE.name(), e1.getValue()); Assert.assertEquals(ClusterAction.CLUSTER_DELETE.name(), e2.getValue()); Assert.assertEquals(ClusterAction.CLUSTER_DELETE.name(), e3.getValue()); }
@Test(timeout = 20000) public void testFalseOnStartStopsJob() throws Exception { String tenantId = "q"; ClusterScheduler clusterScheduler = injector.getInstance(ClusterScheduler.class); clusterQueues.add(tenantId, new Element(cluster.getId(), ClusterAction.CLUSTER_CREATE.name())); clusterScheduler.run(); CallbackScheduler callbackScheduler = injector.getInstance(CallbackScheduler.class); // should be no job in the queue until the start callback runs Assert.assertEquals(0, jobQueues.size(tenantId)); // tell mock callback to return false for onStart callback mockClusterCallback.setReturnOnStart(false); // wait for start callback to finish waitForCallback(callbackScheduler); Assert.assertEquals(CallbackData.Type.START, mockClusterCallback.getReceivedCallbacks().get(0).getType()); // wait for fail callback to finish if (mockClusterCallback.getReceivedCallbacks().size() < 2) { waitForCallback(callbackScheduler); } Assert.assertEquals(CallbackData.Type.FAILURE, mockClusterCallback.getReceivedCallbacks().get(1).getType()); // there also should not be any jobs in the queue Assert.assertEquals(0, jobQueues.size(tenantId)); }
private void testCallbacks(boolean failJob) throws Exception { ClusterScheduler clusterScheduler = injector.getInstance(ClusterScheduler.class); String tenantId = cluster.getAccount().getTenantId(); clusterQueues.add(tenantId, new Element(cluster.getId(), ClusterAction.CLUSTER_CREATE.name())); clusterScheduler.run(); CallbackScheduler callbackScheduler = injector.getInstance(CallbackScheduler.class); // should be no job in the queue until the start callback runs Assert.assertEquals(0, jobQueues.size(tenantId)); waitForCallback(callbackScheduler); Assert.assertEquals(CallbackData.Type.START, mockClusterCallback.getReceivedCallbacks().get(0).getType()); JobScheduler jobScheduler = injector.getInstance(JobScheduler.class); jobScheduler.run(); // take tasks until there are no more TakeTaskRequest takeRequest = new TakeTaskRequest("consumer1", PROVISIONER_ID, tenantId); SchedulableTask task = TestHelper.takeTask(getInternalServerUrl(), takeRequest); while (task != null) { FinishTaskRequest finishRequest = new FinishTaskRequest("consumer1", PROVISIONER_ID, tenantId, task.getTaskId(), null, null, failJob ? 1 : 0, null, null, null); TestHelper.finishTask(getInternalServerUrl(), finishRequest); jobScheduler.run(); jobScheduler.run(); task = TestHelper.takeTask(getInternalServerUrl(), takeRequest); } jobScheduler.run(); waitForCallback(callbackScheduler); // at this point, the failure callback should have run Assert.assertEquals(failJob ? CallbackData.Type.FAILURE : CallbackData.Type.SUCCESS, mockClusterCallback.getReceivedCallbacks().get(1).getType()); }
CallbackScheduler callbackScheduler = injector.getInstance(CallbackScheduler.class); clusterQueues.add(tenantId, new Element(cluster.getId(), ClusterAction.CLUSTER_CREATE.name())); clusterScheduler.run(); waitForCallback(callbackScheduler);
CallbackScheduler callbackScheduler = injector.getInstance(CallbackScheduler.class); clusterQueues.add(tenantId, new Element(cluster.getId(), ClusterAction.CLUSTER_CREATE.name())); clusterScheduler.run();
@Test public void testTriggers() { HttpPostClusterCallback callback = new HttpPostClusterCallback(); String base = "http://" + host + ":" + port; conf = Configuration.create(); conf.set(Constants.HttpCallback.START_URL, base + "/start/endpoint"); conf.set(Constants.HttpCallback.START_TRIGGERS, ClusterAction.CLUSTER_CONFIGURE.name()); callback.initialize(conf); // should not get triggered ClusterJob job = new ClusterJob(new JobId(cluster.getId(), 1), ClusterAction.CLUSTER_CREATE); CallbackData data = new CallbackData(CallbackData.Type.START, cluster, job); CallbackContext context = new CallbackContext(clusterStoreService, userStore, cluster.getAccount()); callback.onStart(data, context); Assert.assertEquals(0, handler.getStartCount()); // should get triggered job = new ClusterJob(new JobId(cluster.getId(), 1), ClusterAction.CLUSTER_CONFIGURE); data = new CallbackData(CallbackData.Type.START, cluster, job); callback.onStart(data, context); Assert.assertEquals(1, handler.getStartCount()); }