@Override protected PreparedStatement getSetClusterStatement( Connection conn, long id, Cluster cluster, byte[] clusterBytes) throws SQLException { PreparedStatement statement = conn.prepareStatement( "UPDATE clusters SET cluster=?, owner_id=?, tenant_id=?, status=?, expire_time=?, latest_job_num=? WHERE id=?"); statement.setBytes(1, clusterBytes); statement.setString(2, cluster.getAccount().getUserId()); statement.setString(3, cluster.getAccount().getTenantId()); statement.setString(4, cluster.getStatus().name()); statement.setTimestamp(5, DBHelper.getTimestamp(cluster.getExpireTime())); statement.setLong(6, JobId.fromString(cluster.getLatestJobId()).getJobNum()); // where clause statement.setLong(7, id); return statement; }
@Override public void writeClusterJob(ClusterJob clusterJob) throws IOException { JobId jobId = JobId.fromString(clusterJob.getJobId()); long clusterId = Long.parseLong(jobId.getClusterId()); try { Connection conn = dbConnectionPool.getConnection(); try { byte[] jobBytes = dbQueryExecutor.toBytes(clusterJob, ClusterJob.class); DBPut jobPut = new ClusterJobDBPut(clusterJob, jobBytes, jobId, clusterId); jobPut.executePut(conn); } finally { conn.close(); } } catch (SQLException e) { throw new IOException(e); } }
private PreparedStatement getInsertClusterStatement( Connection conn, long id, Cluster cluster, byte[] clusterBytes) throws SQLException { PreparedStatement statement = conn.prepareStatement( "INSERT INTO clusters (cluster, owner_id, tenant_id, status, expire_time," + " create_time, name, id, latest_job_num) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); statement.setBytes(1, clusterBytes); statement.setString(2, cluster.getAccount().getUserId()); statement.setString(3, cluster.getAccount().getTenantId()); statement.setString(4, cluster.getStatus().name()); statement.setTimestamp(5, DBHelper.getTimestamp(cluster.getExpireTime())); statement.setTimestamp(6, DBHelper.getTimestamp(cluster.getCreateTime())); statement.setString(7, cluster.getName()); statement.setLong(8, id); String latestJobStr = cluster.getLatestJobId(); long latestJobNum = latestJobStr == null ? 0 : JobId.fromString(latestJobStr).getJobNum(); statement.setLong(9, latestJobNum); return statement; }
JobId jobId = JobId.fromString(planId); ClusterJob clusterJob = clusterStore.getClusterJob(jobId);
for (int i = retryActionIndex; i < currentActionIndex; ++i) { ProvisionerAction action = taskOrder.get(i); TaskId retryTaskId = idService.getNewTaskId(JobId.fromString(task.getJobId())); ClusterTask retry = new ClusterTask(action, retryTaskId, task.getNodeId(), task.getService(), task.getClusterAction(), task.getClusterTemplateName(), task.getAccount());
TaskId taskId = idService.getNewTaskId(JobId.fromString(job.getJobId())); ClusterTask task = new ClusterTask(action, taskId, taskNode.getHostId(), taskNode.getService(), clusterAction, cluster.getClusterTemplate().getName(), cluster.getAccount());
ClusterJob job = clusterStore.getClusterJob(JobId.fromString(cluster.getLatestJobId())); if (job == null) { responder.sendError(HttpResponseStatus.NOT_FOUND,
ClusterJob clusterJob = clusterStore.getClusterJob(JobId.fromString(cluster.getLatestJobId())); LOG.debug("latest job info: {}", clusterJob);
/** * Get a specific cluster visible to the user. * * @param request Request for a cluster. * @param responder Responder for sending the response. * @param clusterId Id of the cluster to get. */ @GET @Path("/{cluster-id}") public void getCluster(HttpRequest request, HttpResponder responder, @PathParam("cluster-id") String clusterId) { Account account = getAndAuthenticateAccount(request, responder); if (account == null) { return; } try { ClusterStoreView view = clusterStoreService.getView(account); Cluster cluster = view.getCluster(clusterId); if (cluster == null) { responder.sendError(HttpResponseStatus.NOT_FOUND, "cluster " + clusterId + " not found."); return; } Set<Node> clusterNodes = view.getClusterNodes(clusterId); ClusterJob clusterJob = clusterStore.getClusterJob(JobId.fromString(cluster.getLatestJobId())); ClusterDetails clusterDetails = new ClusterDetails(cluster, clusterNodes, clusterJob); responder.sendJson(HttpResponseStatus.OK, clusterDetails, ClusterDetails.class, gson); } catch (IOException e) { responder.sendError(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Exception getting cluster " + clusterId); } }
/** * Get the rollback task that should run if the given task fails. * * @param task Task that needs to get rolled back. * @return Cluster task that will roll back the given failed task. */ private ClusterTask getRollbackTask(ClusterTask task) { ProvisionerAction rollback = actions.getRollbackActions().get(task.getTaskName()); if (rollback == null) { return null; } TaskId rollbackTaskId = idService.getNewTaskId(JobId.fromString(task.getJobId())); ClusterTask rollbackTask = new ClusterTask(rollback, rollbackTaskId, task.getNodeId(), task.getService(), task.getClusterAction(), task.getClusterTemplateName(), task.getAccount()); return rollbackTask; }
ClusterJob clusterJob = clusterStore.getClusterJob(JobId.fromString(cluster.getLatestJobId())); LOG.debug("latest job info: {}", clusterJob);
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()); }
ClusterJob clusterJob = clusterStore.getClusterJob(JobId.fromString(cluster.getLatestJobId())); clusterJob = clusterStore.getClusterJob(JobId.fromString(cluster.getLatestJobId()));
ClusterJob delJob1 = clusterStore.getClusterJob(JobId.fromString(actualCluster1.getLatestJobId())); Assert.assertEquals(ClusterAction.CLUSTER_DELETE, delJob1.getClusterAction()); ClusterJob delJob2 = clusterStore.getClusterJob(JobId.fromString(actualCluster3.getLatestJobId())); Assert.assertEquals(ClusterAction.CLUSTER_DELETE, delJob2.getClusterAction());
@Test public void testNoEdgeNodesInDag() { Service s1 = Service.builder().setName("s1").build(); Node node1 = new Node("node1", "1", ImmutableSet.<Service>of(s1), NodeProperties.builder().setHostname("host1").addIPAddress("access_v4", "ip1").build()); Node node2 = new Node("node2", "1", ImmutableSet.<Service>of(s1), NodeProperties.builder().setHostname("host2").addIPAddress("access_v4", "ip2").build()); Set<Node> clusterNodes = ImmutableSet.of(node1, node2); TaskDag expected = new TaskDag(); TaskNode taskNode1 = new TaskNode(node1.getId(), ProvisionerAction.DELETE.name(), ""); TaskNode taskNode2 = new TaskNode(node2.getId(), ProvisionerAction.DELETE.name(), ""); expected.addTaskNode(taskNode1); expected.addTaskNode(taskNode2); ClusterJob job = new ClusterJob(JobId.fromString("123-001"), ClusterAction.CLUSTER_DELETE); JobPlanner planner = new JobPlanner(job, clusterNodes); TaskDag actual = planner.createTaskDag(); Assert.assertEquals(expected, actual); List<Set<TaskNode>> linearizedTasks = actual.linearize(); Assert.assertEquals(1, linearizedTasks.size()); Assert.assertEquals(2, linearizedTasks.get(0).size()); Assert.assertTrue(linearizedTasks.get(0).contains(taskNode1)); Assert.assertTrue(linearizedTasks.get(0).contains(taskNode2)); }
@Test public void testGetClusterJobs() throws Exception { Cluster cluster = createClusterObj("1"); ClusterStoreView user1view = clusterStoreService.getView(cluster.getAccount()); user1view.writeCluster(cluster); Set<ClusterJob> jobs = Sets.newHashSet(); for (int i = 0; i < 10; i++) { JobId jobId = new JobId(cluster.getId(), i); ClusterJob job = new ClusterJob(jobId, ClusterAction.RESTART_SERVICES); systemView.writeClusterJob(job); jobs.add(job); } // this job shouldn't get fetched ClusterJob randomJob = new ClusterJob(JobId.fromString("2123-0214"), ClusterAction.CLUSTER_CONFIGURE); systemView.writeClusterJob(randomJob); // shouldn't be able to get since the cluster isn't owned by this user Assert.assertTrue(clusterStoreService.getView(tenant1_user2).getClusterJobs(cluster.getId(), -1).isEmpty()); // check we can get all the jobs Assert.assertEquals(Sets.newHashSet(user1view.getClusterJobs(cluster.getId(), -1)), jobs); Assert.assertEquals(Sets.newHashSet(systemView.getClusterJobs(cluster.getId(), -1)), jobs); // check the limit List<ClusterJob> fetchedJobs = user1view.getClusterJobs(cluster.getId(), 5); Assert.assertEquals(5, fetchedJobs.size()); Assert.assertTrue(jobs.containsAll(fetchedJobs)); fetchedJobs = systemView.getClusterJobs(cluster.getId(), 5); Assert.assertEquals(5, fetchedJobs.size()); Assert.assertTrue(jobs.containsAll(fetchedJobs)); }
clusterStore.writeClusterTask(clusterTask); ClusterJob clusterJob = new ClusterJob(JobId.fromString("1-1"), ClusterAction.CLUSTER_CREATE); clusterStore.writeClusterJob(clusterJob);
"test", cluster.getAccount()); clusterStore.writeClusterTask(clusterTask); ClusterJob clusterJob = new ClusterJob(JobId.fromString("1-1"), ClusterAction.CLUSTER_CREATE); clusterStore.writeClusterJob(clusterJob); TaskConfig taskConfig = new TaskConfig(
new TaskNode(node2.getId(), ProvisionerAction.CONFIGURE.name(), s3.getName())); ClusterJob job = new ClusterJob(JobId.fromString("123-001"), ClusterAction.CLUSTER_CONFIGURE); JobPlanner planner = new JobPlanner(job, clusterNodes); Assert.assertEquals(expected, planner.createTaskDag());