@Override public boolean apply(SingularityTaskIdHistory input) { final SingularityTaskId taskId = input.getTaskId(); if (runId.isPresent() && !runId.get().equals(input.getRunId().or(""))) { return false; if (!input.getLastTaskState().isPresent()) { return false; if (lastTaskStatus.get() != input.getLastTaskState().get()) { return false; if (updatedAfter.isPresent() && updatedAfter.get() >= input.getUpdatedAt()) { return false; if (updatedBefore.isPresent() && updatedBefore.get() <= input.getUpdatedAt()) { return false;
@Override public SingularityTaskIdHistory map(int index, ResultSet r, StatementContext ctx) throws SQLException { try { final SingularityTaskId taskId = singularityTaskIdTranscoder.fromString(r.getString("taskId")); final String lastTaskStatus = r.getString("lastTaskStatus"); Optional<ExtendedTaskState> lastTaskState = Optional.absent(); if (lastTaskStatus != null) { try { lastTaskState = Optional.of(ExtendedTaskState.valueOf(lastTaskStatus)); } catch (IllegalArgumentException e) { LOG.warn("Found invalid taskState {} in DB for task {}", lastTaskState, taskId, e); } } return new SingularityTaskIdHistory(taskId, r.getTimestamp("updatedAt").getTime(), lastTaskState, Optional.fromNullable(r.getString("runId"))); } catch (SingularityTranscoderException e) { throw new ResultSetException("Could not deserialize database result", e, ctx); } } }
@Override public void saveTaskHistory(SingularityTaskHistory taskHistory) { if (history.getTaskHistoryForTask(taskHistory.getTask().getTaskId().getId()) != null) { if (LOG.isTraceEnabled()) { LOG.trace("saveTaskHistory -- existing taskHistory {}", taskHistory); } return; } SingularityTaskIdHistory taskIdHistory = SingularityTaskIdHistory.fromTaskIdAndTaskAndUpdates(taskHistory.getTask().getTaskId(), taskHistory.getTask(), taskHistory.getTaskUpdates()); String lastTaskStatus = null; if (taskIdHistory.getLastTaskState().isPresent()) { lastTaskStatus = taskIdHistory.getLastTaskState().get().name(); } if (LOG.isTraceEnabled()) { LOG.trace("saveTaskHistory -- will insert taskHistory {}", taskHistory); } history.insertTaskHistory(taskIdHistory.getTaskId().getRequestId(), taskIdHistory.getTaskId().getId(), taskHistoryTranscoder.toBytes(taskHistory), new Date(taskIdHistory.getUpdatedAt()), lastTaskStatus, taskHistory.getTask().getTaskRequest().getPendingTask().getRunId().orNull(), taskIdHistory.getTaskId().getDeployId(), taskIdHistory.getTaskId().getHost(), new Date(taskIdHistory.getTaskId().getStartedAt())); }
@Override public int compareTo(SingularityTaskIdHistory o) { return ComparisonChain.start() .compare(o.getUpdatedAt(), updatedAt) .compare(taskId.getId(), o.getTaskId().getId()) .result(); }
public Optional<SingularityTaskIdHistory> getByRunId(String requestId, String runId) { for (SingularityTaskIdHistory history : getFromZk(Collections.singletonList(requestId))) { if (history.getRunId().isPresent() && history.getRunId().get().equals(runId)) { return Optional.of(history); } } Optional<SingularityTaskHistory> history = historyManager.getTaskHistoryByRunId(requestId, runId); if (history.isPresent()) { return Optional.of(SingularityTaskIdHistory.fromTaskIdAndTaskAndUpdates(history.get().getTask().getTaskId(), history.get().getTask(), history.get().getTaskUpdates())); } return Optional.absent(); }
@Override public int compare(SingularityTaskIdHistory o1, SingularityTaskIdHistory o2) { ComparisonChain chain = ComparisonChain.start(); if (localOrderDirection == OrderDirection.ASC) { chain = chain.compare(o1.getTaskId().getStartedAt(), o2.getTaskId().getStartedAt()); } else { chain = chain.compare(o2.getTaskId().getStartedAt(), o1.getTaskId().getStartedAt()); } return chain.compare(o1.getTaskId().getRequestId(), o2.getTaskId().getRequestId()).result(); }
@Test public void testRunId() { initScheduledRequest(); initFirstDeploy(); String runId = "my-run-id"; SingularityPendingRequestParent parent = requestResource.scheduleImmediately(singularityUser, requestId, new SingularityRunNowRequestBuilder().setRunId(runId).build()); Assert.assertEquals(runId, parent.getPendingRequest().getRunId().get()); resourceOffers(); Assert.assertEquals(runId, taskManager.getActiveTasks().get(0).getTaskRequest().getPendingTask().getRunId().get()); SingularityTaskId taskId = taskManager.getActiveTaskIds().get(0); statusUpdate(taskManager.getTask(taskId).get(), TaskState.TASK_FINISHED); configuration.setTaskPersistAfterStartupBufferMillis(0); taskMetadataConfiguration.setTaskPersistAfterFinishBufferMillis(0); taskHistoryPersister.runActionOnPoll(); Assert.assertEquals(runId, historyManager.getTaskHistory(taskId.getId()).get().getTask().getTaskRequest().getPendingTask().getRunId().get()); Assert.assertEquals(runId, getTaskHistoryForRequest(requestId, 0, 10).get(0).getRunId().get()); parent = requestResource.scheduleImmediately(singularityUser, requestId, ((SingularityRunNowRequest) null)); Assert.assertTrue(parent.getPendingRequest().getRunId().isPresent()); }
private long getLastActionTimeForRequest(SingularityRequest request, Optional<SingularityRequestHistory> lastHistory, Optional<SingularityRequestDeployState> deployState, Optional<SingularityTaskIdHistory> mostRecentTask) { long lastUpdate = 0; if (lastHistory.isPresent()) { lastUpdate = lastHistory.get().getCreatedAt(); } if (deployState.isPresent()) { if (deployState.get().getActiveDeploy().isPresent()) { lastUpdate = Math.max(lastUpdate, deployState.get().getActiveDeploy().get().getTimestamp()); } if (deployState.get().getPendingDeploy().isPresent()) { lastUpdate = Math.max(lastUpdate, deployState.get().getPendingDeploy().get().getTimestamp()); } } // Only consider most recent task time for non-long-running if (mostRecentTask.isPresent() && !request.getRequestType().isLongRunning()) { lastUpdate = Math.max(lastUpdate, mostRecentTask.get().getUpdatedAt()); } return lastUpdate; } }
public List<SingularityTaskIdHistory> getTaskHistoriesFor(TaskManager taskManager, Collection<SingularityTaskId> taskIds) { Map<SingularityTaskId, SingularityTask> tasks = taskManager.getTasks(taskIds); Map<SingularityTaskId, List<SingularityTaskHistoryUpdate>> map = taskManager.getTaskHistoryUpdates(taskIds); List<SingularityTaskIdHistory> histories = Lists.newArrayListWithCapacity(taskIds.size()); for (SingularityTaskId taskId : taskIds) { List<SingularityTaskHistoryUpdate> historyUpdates = map.get(taskId); SingularityTask task = tasks.get(taskId); if (task != null) { histories.add(SingularityTaskIdHistory.fromTaskIdAndTaskAndUpdates(taskId, task, historyUpdates)); } } Collections.sort(histories); return histories; }
@GET @Path("/request/{requestId}/command-line-args") @Operation(summary = "Get a list of recently used command line args for an on-demand or scheduled request") public Set<List<String>> getRecentCommandLineArgs( @Parameter(hidden = true) @Auth SingularityUser user, @Parameter(required = true, description = "Request ID to look up") @PathParam("requestId") String requestId, @Parameter(description = "Max number of recent args to return") @QueryParam("count") Optional<Integer> count) { authorizationHelper.checkForAuthorizationByRequestId(requestId, user, SingularityAuthorizationScope.READ); final int argCount = count.or(DEFAULT_ARGS_HISTORY_COUNT); List<SingularityTaskIdHistory> historiesToCheck = taskHistoryHelper.getBlendedHistory(new SingularityTaskHistoryQuery( Optional.of(requestId), Optional.<String>absent(), Optional.<String>absent(), Optional.<String>absent(), Optional.<ExtendedTaskState>absent(), Optional.<Long>absent(), Optional.<Long>absent(), Optional.<Long>absent(), Optional.<Long>absent(), Optional.<OrderDirection>absent()), 0, argCount); Collections.sort(historiesToCheck); Set<List<String>> args = new HashSet<>(); for (SingularityTaskIdHistory taskIdHistory : historiesToCheck) { Optional<SingularityTask> maybeTask = taskHistoryHelper.getTask(taskIdHistory.getTaskId()); if (maybeTask.isPresent() && maybeTask.get().getTaskRequest().getPendingTask().getCmdLineArgsList().isPresent()) { List<String> taskArgs = maybeTask.get().getTaskRequest().getPendingTask().getCmdLineArgsList().get(); if (!taskArgs.isEmpty()) { args.add(maybeTask.get().getTaskRequest().getPendingTask().getCmdLineArgsList().get()); } } } return args; }
public Optional<SingularityTaskIdHistory> getByRunId(String requestId, String runId) { for (SingularityTaskIdHistory history : getFromZk(Collections.singletonList(requestId))) { if (history.getRunId().isPresent() && history.getRunId().get().equals(runId)) { return Optional.of(history); } } Optional<SingularityTaskHistory> history = historyManager.getTaskHistoryByRunId(requestId, runId); if (history.isPresent()) { return Optional.of(SingularityTaskIdHistory.fromTaskIdAndTaskAndUpdates(history.get().getTask().getTaskId(), history.get().getTask(), history.get().getTaskUpdates())); } return Optional.absent(); }
@Test public void testRunId() { initScheduledRequest(); initFirstDeploy(); String runId = "my-run-id"; SingularityPendingRequestParent parent = requestResource.scheduleImmediately(singularityUser, requestId, new SingularityRunNowRequestBuilder().setRunId(runId).build()); Assert.assertEquals(runId, parent.getPendingRequest().getRunId().get()); resourceOffers(); Assert.assertEquals(runId, taskManager.getActiveTasks().get(0).getTaskRequest().getPendingTask().getRunId().get()); SingularityTaskId taskId = taskManager.getActiveTaskIds().get(0); statusUpdate(taskManager.getTask(taskId).get(), TaskState.TASK_FINISHED); configuration.setTaskPersistAfterStartupBufferMillis(0); taskMetadataConfiguration.setTaskPersistAfterFinishBufferMillis(0); taskHistoryPersister.runActionOnPoll(); Assert.assertEquals(runId, historyManager.getTaskHistory(taskId.getId()).get().getTask().getTaskRequest().getPendingTask().getRunId().get()); Assert.assertEquals(runId, getTaskHistoryForRequest(requestId, 0, 10).get(0).getRunId().get()); parent = requestResource.scheduleImmediately(singularityUser, requestId, ((SingularityRunNowRequest) null)); Assert.assertTrue(parent.getPendingRequest().getRunId().isPresent()); }
private long getLastActionTimeForRequest(SingularityRequest request, Optional<SingularityRequestHistory> lastHistory, Optional<SingularityRequestDeployState> deployState, Optional<SingularityTaskIdHistory> mostRecentTask) { long lastUpdate = 0; if (lastHistory.isPresent()) { lastUpdate = lastHistory.get().getCreatedAt(); } if (deployState.isPresent()) { if (deployState.get().getActiveDeploy().isPresent()) { lastUpdate = Math.max(lastUpdate, deployState.get().getActiveDeploy().get().getTimestamp()); } if (deployState.get().getPendingDeploy().isPresent()) { lastUpdate = Math.max(lastUpdate, deployState.get().getPendingDeploy().get().getTimestamp()); } } // Only consider most recent task time for non-long-running if (mostRecentTask.isPresent() && !request.getRequestType().isLongRunning()) { lastUpdate = Math.max(lastUpdate, mostRecentTask.get().getUpdatedAt()); } return lastUpdate; } }
public List<SingularityTaskIdHistory> getTaskHistoriesFor(TaskManager taskManager, Collection<SingularityTaskId> taskIds) { Map<SingularityTaskId, SingularityTask> tasks = taskManager.getTasks(taskIds); Map<SingularityTaskId, List<SingularityTaskHistoryUpdate>> map = taskManager.getTaskHistoryUpdates(taskIds); List<SingularityTaskIdHistory> histories = Lists.newArrayListWithCapacity(taskIds.size()); for (SingularityTaskId taskId : taskIds) { List<SingularityTaskHistoryUpdate> historyUpdates = map.get(taskId); SingularityTask task = tasks.get(taskId); if (task != null) { histories.add(SingularityTaskIdHistory.fromTaskIdAndTaskAndUpdates(taskId, task, historyUpdates)); } } Collections.sort(histories); return histories; }
@Override public void saveTaskHistory(SingularityTaskHistory taskHistory) { if (history.getTaskHistoryForTask(taskHistory.getTask().getTaskId().getId()) != null) { if (LOG.isTraceEnabled()) { LOG.trace("saveTaskHistory -- existing taskHistory {}", taskHistory); } return; } SingularityTaskIdHistory taskIdHistory = SingularityTaskIdHistory.fromTaskIdAndTaskAndUpdates(taskHistory.getTask().getTaskId(), taskHistory.getTask(), taskHistory.getTaskUpdates()); String lastTaskStatus = null; if (taskIdHistory.getLastTaskState().isPresent()) { lastTaskStatus = taskIdHistory.getLastTaskState().get().name(); } if (LOG.isTraceEnabled()) { LOG.trace("saveTaskHistory -- will insert taskHistory {}", taskHistory); } history.insertTaskHistory(taskIdHistory.getTaskId().getRequestId(), taskIdHistory.getTaskId().getId(), taskHistoryTranscoder.toBytes(taskHistory), new Date(taskIdHistory.getUpdatedAt()), lastTaskStatus, taskHistory.getTask().getTaskRequest().getPendingTask().getRunId().orNull(), taskIdHistory.getTaskId().getDeployId(), taskIdHistory.getTaskId().getHost(), new Date(taskIdHistory.getTaskId().getStartedAt())); }
private void match(List<SingularityTaskIdHistory> history, int num, SingularityTask... tasks) { Assert.assertEquals(num, history.size()); for (int i = 0; i < tasks.length; i++) { SingularityTaskIdHistory idHistory = history.get(i); SingularityTask task = tasks[i]; Assert.assertEquals(task.getTaskId(), idHistory.getTaskId()); } }
public Optional<SingularityTaskIdHistory> getMostRecentTask(SingularityRequest request) { List<SingularityTaskId> activeTaskIds = taskManager.getActiveTaskIdsForRequest(request.getId()); if (!activeTaskIds.isEmpty()) { SingularityTaskId lastTaskId = activeTaskIds.get(0); List<SingularityTaskHistoryUpdate> historyUpdates = taskManager.getTaskHistoryUpdates(lastTaskId); if (!historyUpdates.isEmpty()) { SingularityTaskHistoryUpdate lastUpdate = historyUpdates.get(historyUpdates.size() - 1); return Optional.of(new SingularityTaskIdHistory( lastTaskId, lastUpdate.getTimestamp(), Optional.of(lastUpdate.getTaskState()), Optional.absent() // runId not currently provided here, grabbing the full task data for this is a more expensive call )); } } List<SingularityTaskIdHistory> maybeRecentTasks = taskHistoryHelper.getBlendedHistory(new SingularityTaskHistoryQuery(request.getId()), 0 , 1); if (!maybeRecentTasks.isEmpty()) { return Optional.of(maybeRecentTasks.get(0)); } return Optional.absent(); }
@GET @Path("/request/{requestId}/command-line-args") @Operation(summary = "Get a list of recently used command line args for an on-demand or scheduled request") public Set<List<String>> getRecentCommandLineArgs( @Parameter(hidden = true) @Auth SingularityUser user, @Parameter(required = true, description = "Request ID to look up") @PathParam("requestId") String requestId, @Parameter(description = "Max number of recent args to return") @QueryParam("count") Optional<Integer> count) { authorizationHelper.checkForAuthorizationByRequestId(requestId, user, SingularityAuthorizationScope.READ); final int argCount = count.or(DEFAULT_ARGS_HISTORY_COUNT); List<SingularityTaskIdHistory> historiesToCheck = taskHistoryHelper.getBlendedHistory(new SingularityTaskHistoryQuery( Optional.of(requestId), Optional.<String>absent(), Optional.<String>absent(), Optional.<String>absent(), Optional.<ExtendedTaskState>absent(), Optional.<Long>absent(), Optional.<Long>absent(), Optional.<Long>absent(), Optional.<Long>absent(), Optional.<OrderDirection>absent()), 0, argCount); Collections.sort(historiesToCheck); Set<List<String>> args = new HashSet<>(); for (SingularityTaskIdHistory taskIdHistory : historiesToCheck) { Optional<SingularityTask> maybeTask = taskHistoryHelper.getTask(taskIdHistory.getTaskId()); if (maybeTask.isPresent() && maybeTask.get().getTaskRequest().getPendingTask().getCmdLineArgsList().isPresent()) { List<String> taskArgs = maybeTask.get().getTaskRequest().getPendingTask().getCmdLineArgsList().get(); if (!taskArgs.isEmpty()) { args.add(maybeTask.get().getTaskRequest().getPendingTask().getCmdLineArgsList().get()); } } } return args; }
public static SingularityTaskIdHistory fromTaskIdAndTaskAndUpdates(SingularityTaskId taskId, SingularityTask task, List<SingularityTaskHistoryUpdate> updates) { ExtendedTaskState lastTaskState = null; long updatedAt = taskId.getStartedAt(); if (updates != null && !updates.isEmpty()) { SingularityTaskHistoryUpdate lastUpdate = Collections.max(updates); lastTaskState = lastUpdate.getTaskState(); updatedAt = lastUpdate.getTimestamp(); } return new SingularityTaskIdHistory(taskId, updatedAt, Optional.fromNullable(lastTaskState), task.getTaskRequest().getPendingTask().getRunId()); }
private void match(List<SingularityTaskIdHistory> history, int num, SingularityTask... tasks) { Assert.assertEquals(num, history.size()); for (int i = 0; i < tasks.length; i++) { SingularityTaskIdHistory idHistory = history.get(i); SingularityTask task = tasks[i]; Assert.assertEquals(task.getTaskId(), idHistory.getTaskId()); } }