/** * Get a unique task id that can be used for new {@link co.cask.coopr.scheduler.task.ClusterTask}s. * * @param jobId Id of the job the task is a part of. * @return Unique task id. */ public TaskId getNewTaskId(JobId jobId) { return new TaskId(jobId, generateId(Type.TASK)); }
private boolean allCreateTasksFailed(ClusterJob job) throws IOException { for (Map.Entry<String, ClusterTask.Status> entry : job.getTaskStatus().entrySet()) { String taskId = entry.getKey(); ClusterTask.Status taskStatus = entry.getValue(); // no task can succeed or be in progress if (taskStatus == ClusterTask.Status.COMPLETE || taskStatus == ClusterTask.Status.IN_PROGRESS) { return false; } if (taskStatus == ClusterTask.Status.FAILED) { // looks up every failed task... not so great. But it should be roughly equal to the # of nodes in the cluster. ClusterTask task = clusterStore.getClusterTask(TaskId.fromString(taskId)); // check it is a create task if (!task.failedBeforeCreate()) { return false; } } } // if we get here, we only have failed, dropped, or not submitted tasks, and all the failed tasks failed before // they could create anything return true; }
@Override public void deleteClusterTask(TaskId taskId) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement("DELETE FROM tasks WHERE task_num=? AND cluster_id=? AND job_num=?"); statement.setLong(1, taskId.getTaskNum()); statement.setLong(2, Long.parseLong(taskId.getClusterId())); statement.setLong(3, taskId.getJobNum()); try { statement.executeUpdate(); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException(e); } }
public ClusterTask(ProvisionerAction taskName, TaskId taskId, String nodeId, String service, ClusterAction clusterAction, String clusterTemplateName, Account account) { this.taskId = taskId.getId(); this.jobId = String.valueOf(taskId.getJobId().getId()); this.clusterId = taskId.getClusterId(); this.taskName = taskName; this.clusterAction = clusterAction; this.nodeId = nodeId; this.service = service; this.attempts = Lists.newArrayList(); //TODO: populate clusterTemplateName and account field for existing tasks: https://issues.cask.co/browse/COOPR-593 this.clusterTemplateName = clusterTemplateName; this.account = account; addAttempt(); }
@Override public void writeClusterTask(ClusterTask clusterTask) throws IOException { TaskId taskId = TaskId.fromString(clusterTask.getTaskId()); long clusterId = Long.parseLong(taskId.getClusterId()); try { Connection conn = dbConnectionPool.getConnection(); try { byte[] taskBytes = dbQueryExecutor.toBytes(clusterTask, ClusterTask.class); DBPut taskPut = new ClusterTaskDBPut(clusterTask, taskBytes, taskId, clusterId); taskPut.executePut(conn); } finally { conn.close(); } } catch (SQLException e) { throw new IOException(e); } }
@Override public PreparedStatement createInsertStatement(Connection conn) throws SQLException { PreparedStatement statement = conn.prepareStatement( "INSERT INTO tasks (task_num, job_num, cluster_id, status, submit_time, task, type, " + "cluster_template_name, user_id, tenant_id)" + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); statement.setLong(1, taskId.getTaskNum()); statement.setLong(2, taskId.getJobNum()); statement.setLong(3, clusterId); statement.setString(4, clusterTask.getStatus().name()); statement.setTimestamp(5, DBHelper.getTimestamp(clusterTask.getSubmitTime())); statement.setBytes(6, taskBytes); statement.setString(7, clusterTask.getTaskName().name()); statement.setString(8, clusterTask.getClusterTemplateName()); statement.setString(9, clusterTask.getAccount().getUserId()); statement.setString(10, clusterTask.getAccount().getTenantId()); return statement; } }
@Override public PreparedStatement createUpdateStatement(Connection conn) throws SQLException { PreparedStatement statement = conn.prepareStatement( "UPDATE tasks SET task=?, status=?, submit_time=?, status_time=?, type=?, " + "cluster_template_name=?, user_id=?, tenant_id=?" + " WHERE task_num=? AND job_num=? AND cluster_id=?"); statement.setBytes(1, dbQueryExecutor.toBytes(clusterTask, ClusterTask.class)); statement.setString(2, clusterTask.getStatus().name()); statement.setTimestamp(3, DBHelper.getTimestamp(clusterTask.getSubmitTime())); statement.setTimestamp(4, DBHelper.getTimestamp(clusterTask.getStatusTime())); statement.setString(5, clusterTask.getTaskName().name()); statement.setString(6, clusterTask.getClusterTemplateName()); statement.setString(7, clusterTask.getAccount().getUserId()); statement.setString(8, clusterTask.getAccount().getTenantId()); statement.setLong(9, taskId.getTaskNum()); statement.setLong(10, taskId.getJobNum()); statement.setLong(11, clusterId); return statement; }
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; }
@Override public ClusterTask getClusterTask(TaskId taskId) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement("SELECT task FROM tasks WHERE task_num=? AND cluster_id=? AND job_num=?"); try { statement.setLong(1, taskId.getTaskNum()); statement.setLong(2, Long.parseLong(taskId.getClusterId())); statement.setLong(3, taskId.getJobNum()); return dbQueryExecutor.getQueryItem(statement, ClusterTask.class); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception getting cluster task {}", taskId, e); throw new IOException("Exception getting cluster task " + taskId, e); } }
/** * Returns a task id from a string, validating that the string is a valid id. The format is clusterid-jobnum-tasknum. * An IllegalArgumentException will be thrown if the string is invalid. * * @param taskIdStr String of the task id to parse. * @return Task id of the string. */ public static TaskId fromString(String taskIdStr) { int index1 = taskIdStr.indexOf("-"); Preconditions.checkArgument(index1 > 0, "invalid task id string " + taskIdStr); String clusterId = taskIdStr.substring(0, index1); int index2 = taskIdStr.indexOf("-", index1 + 1); Preconditions.checkArgument(index2 > 0, "invalid task id string " + taskIdStr); long jobNum = Long.valueOf(taskIdStr.substring(index1 + 1, index2)); Preconditions.checkArgument(taskIdStr.indexOf("-", index2 + 1) < 0, "invalid task id string " + taskIdStr); long taskNum = Long.valueOf(taskIdStr.substring(index2 + 1)); return new TaskId(new JobId(clusterId, jobNum), taskNum); }
ClusterTask task = clusterStore.getClusterTask(TaskId.fromString(taskId));
@Test public void testGetStoreDeleteTask() throws IOException { TaskId id = new TaskId(new JobId("1", 1), 1); ClusterTask task = new ClusterTask(ProvisionerAction.CONFIGURE, id, "node1", "service", ClusterAction.CLUSTER_CREATE, "test", new Account("testUser", "testTenant")); Assert.assertNull(systemView.getClusterTask(id)); systemView.writeClusterTask(task); Assert.assertEquals(task, systemView.getClusterTask(id)); systemView.writeClusterTask(task); Assert.assertEquals(task, systemView.getClusterTask(id)); systemView.deleteClusterTask(id); Assert.assertNull(systemView.getClusterTask(id)); }
private ClusterTask createClusterTask(String name, String taskId, String hostId) { return new ClusterTask( ProvisionerAction.valueOf(name), TaskId.fromString(taskId), hostId, "service", ClusterAction.CLUSTER_CREATE, "test", new Account("testUser", "testTenant")); }
ClusterTask clusterTask = clusterStore.getClusterTask(TaskId.fromString(taskId));
ClusterTask task = clusterStore.getClusterTask(TaskId.fromString(taskId)); job.setTaskStatus(task.getTaskId(), task.getStatus()); LOG.debug("Status of task {} is {}", taskId, task.getStatus());
private TakeTaskRequest getRequest() throws IOException { String tenantId = USER1_ACCOUNT.getTenantId(); ClusterTask clusterTask = new ClusterTask( ProvisionerAction.CREATE, TaskId.fromString("1-1-1"), "node_id", "service", ClusterAction.CLUSTER_CREATE, "test", USER1_ACCOUNT); clusterStore.writeClusterTask(clusterTask); ClusterJob clusterJob = new ClusterJob(JobId.fromString("1-1"), ClusterAction.CLUSTER_CREATE); clusterStore.writeClusterJob(clusterJob); TaskConfig taskConfig = new TaskConfig( NodeProperties.builder().build(), Entities.ProviderExample.JOYENT, ImmutableMap.<String, NodeProperties>of(), new TaskServiceAction("svcA", new ServiceAction("shell", ImmutableMap.<String, String>of())), new JsonObject(), new JsonObject() ); SchedulableTask schedulableTask= new SchedulableTask(clusterTask, taskConfig); provisionerQueues.add(tenantId, new Element(clusterTask.getTaskId(), gson.toJson(schedulableTask))); return new TakeTaskRequest("worker1", PROVISIONER_ID, TENANT_ID); }
@Test public void testTakeTask() throws Exception { String tenantId = USER1_ACCOUNT.getTenantId(); ClusterTask clusterTask = new ClusterTask( ProvisionerAction.CREATE, TaskId.fromString("1-1-1"), "node_id", "service", ClusterAction.CLUSTER_CREATE, "test", USER1_ACCOUNT); clusterStore.writeClusterTask(clusterTask); ClusterJob clusterJob = new ClusterJob(JobId.fromString("1-1"), ClusterAction.CLUSTER_CREATE); clusterStore.writeClusterJob(clusterJob); TaskConfig taskConfig = new TaskConfig( NodeProperties.builder().build(), Entities.ProviderExample.JOYENT, ImmutableMap.<String, NodeProperties>of(), new TaskServiceAction("svcA", new ServiceAction("shell", ImmutableMap.<String, String>of())), new JsonObject(), new JsonObject() ); SchedulableTask schedulableTask= new SchedulableTask(clusterTask, taskConfig); provisionerQueues.add(tenantId, new Element(clusterTask.getTaskId(), gson.toJson(schedulableTask))); TakeTaskRequest takeRequest = new TakeTaskRequest("worker1", PROVISIONER_ID, TENANT_ID); HttpResponse response = doPostInternalAPI("/tasks/take", gson.toJson(takeRequest)); assertResponseStatus(response, HttpResponseStatus.OK); JsonObject responseJson = getResponseJson(response); Assert.assertEquals(clusterTask.getTaskId(), responseJson.get("taskId").getAsString()); }
@Test public void testGetRunningTasks() throws Exception { ClusterTask task1 = new ClusterTask(ProvisionerAction.CREATE, TaskId.fromString("1-1-1"), "node1", "service", ClusterAction.CLUSTER_CREATE, "test", new Account("testUser", "testTenant")); ClusterTask task2 = new ClusterTask(ProvisionerAction.CREATE, TaskId.fromString("1-1-2"), "node2", "service", ClusterAction.CLUSTER_CREATE, "test", new Account("testUser", "testTenant")); ClusterTask task3 = new ClusterTask(ProvisionerAction.CREATE, TaskId.fromString("1-1-3"), "node3", "service", ClusterAction.CLUSTER_CREATE, "test", new Account("testUser", "testTenant")); ClusterTask task4 = new ClusterTask(ProvisionerAction.CREATE, TaskId.fromString("1-1-4"), "node4", "service", ClusterAction.CLUSTER_CREATE, "test", new Account("testUser", "testTenant")); ClusterTask task5 = new ClusterTask(ProvisionerAction.CREATE, TaskId.fromString("1-1-5"), "node5", "service", ClusterAction.CLUSTER_CREATE, "test", new Account("testUser", "testTenant"));
clusterTask = clusterStore.getClusterTask(TaskId.fromString(task.getId())); if (clusterTask != null) { String jobId = clusterTask.getJobId();
ProvisionerAction.CREATE, TaskId.fromString("1-1-1"), node.getId(), "service", ClusterAction.CLUSTER_CREATE, "test", USER1_ACCOUNT); clusterStore.writeClusterTask(clusterTask); TestHelper.finishTask(getBaseUrlInternalAPI(), finishRequest); ClusterTask actualTask = clusterStore.getClusterTask(TaskId.fromString(clusterTask.getTaskId())); Assert.assertEquals(ClusterTask.Status.COMPLETE, actualTask.getStatus());