use of com.hubspot.singularity.SingularityTask in project Singularity by HubSpot.
the class SingularityMesosStatusUpdateHandler method unsafeProcessStatusUpdate.
private void unsafeProcessStatusUpdate(Protos.TaskStatus status, SingularityTaskId taskIdObj) {
final String taskId = status.getTaskId().getValue();
long timestamp = System.currentTimeMillis();
if (status.hasTimestamp()) {
timestamp = (long) (status.getTimestamp() * 1000);
}
long now = System.currentTimeMillis();
long delta = now - timestamp;
LOG.debug("Update: task {} is now {} ({}) at {} (delta: {})", taskId, status.getState(), status.getMessage(), timestamp, JavaUtils.durationFromMillis(delta));
statusUpdateDeltas.put(now, delta);
final SingularityTaskStatusHolder newTaskStatusHolder = new SingularityTaskStatusHolder(taskIdObj, Optional.of(mesosProtosUtils.taskStatusFromProtos(status)), System.currentTimeMillis(), serverId, Optional.<String>absent());
final Optional<SingularityTaskStatusHolder> previousTaskStatusHolder = taskManager.getLastActiveTaskStatus(taskIdObj);
final ExtendedTaskState taskState = MesosUtils.fromTaskState(status.getState());
if (isDuplicateOrIgnorableStatusUpdate(previousTaskStatusHolder, newTaskStatusHolder)) {
LOG.trace("Ignoring status update {} to {}", taskState, taskIdObj);
saveNewTaskStatusHolder(taskIdObj, newTaskStatusHolder, taskState);
return;
}
if (status.getState() == TaskState.TASK_LOST) {
lostTasksMeter.mark();
if (configuration.getDisasterDetection().isEnabled()) {
taskLostReasons.add(status.getReason());
}
}
final Optional<SingularityTask> task = taskManager.getTask(taskIdObj);
final boolean isActiveTask = taskManager.isActiveTask(taskId);
if (isActiveTask && !taskState.isDone()) {
if (task.isPresent()) {
final Optional<SingularityPendingDeploy> pendingDeploy = deployManager.getPendingDeploy(taskIdObj.getRequestId());
Optional<SingularityRequestWithState> requestWithState = Optional.absent();
if (taskState == ExtendedTaskState.TASK_RUNNING) {
requestWithState = requestManager.getRequest(taskIdObj.getRequestId());
healthchecker.enqueueHealthcheck(task.get(), pendingDeploy, requestWithState);
}
if (!pendingDeploy.isPresent() || !pendingDeploy.get().getDeployMarker().getDeployId().equals(taskIdObj.getDeployId())) {
if (!requestWithState.isPresent()) {
requestWithState = requestManager.getRequest(taskIdObj.getRequestId());
}
newTaskChecker.enqueueNewTaskCheck(task.get(), requestWithState, healthchecker);
}
} else {
final String message = String.format("Task %s is active but is missing task data", taskId);
exceptionNotifier.notify(message);
LOG.error(message);
}
}
final Optional<String> statusMessage = getStatusMessage(status, task);
final SingularityTaskHistoryUpdate taskUpdate = new SingularityTaskHistoryUpdate(taskIdObj, timestamp, taskState, statusMessage, status.hasReason() ? Optional.of(status.getReason().name()) : Optional.<String>absent());
final SingularityCreateResult taskHistoryUpdateCreateResult = taskManager.saveTaskHistoryUpdate(taskUpdate);
logSupport.checkDirectoryAndContainerId(taskIdObj);
if (taskState.isDone()) {
healthchecker.cancelHealthcheck(taskId);
newTaskChecker.cancelNewTaskCheck(taskId);
taskManager.deleteKilledRecord(taskIdObj);
slaveAndRackManager.checkStateAfterFinishedTask(taskIdObj, status.getAgentId().getValue(), leaderCache);
scheduler.handleCompletedTask(task, taskIdObj, isActiveTask, timestamp, taskState, taskHistoryUpdateCreateResult, status);
}
saveNewTaskStatusHolder(taskIdObj, newTaskStatusHolder, taskState);
}
use of com.hubspot.singularity.SingularityTask in project Singularity by HubSpot.
the class SingularityMesosTaskBuilder method buildTask.
public SingularityMesosTaskHolder buildTask(SingularityOfferHolder offerHolder, List<Resource> availableResources, SingularityTaskRequest taskRequest, Resources desiredTaskResources, Resources desiredExecutorResources) {
final String sanitizedRackId = offerHolder.getSanitizedRackId();
final String sanitizedHost = offerHolder.getSanitizedHost();
final SingularityTaskId taskId = new SingularityTaskId(taskRequest.getPendingTask().getPendingTaskId().getRequestId(), taskRequest.getDeploy().getId(), System.currentTimeMillis(), taskRequest.getPendingTask().getPendingTaskId().getInstanceNo(), sanitizedHost, sanitizedRackId);
final TaskInfo.Builder bldr = TaskInfo.newBuilder().setTaskId(TaskID.newBuilder().setValue(taskId.toString()));
Optional<long[]> ports = Optional.absent();
Optional<Resource> portsResource = Optional.absent();
final Optional<SingularityContainerInfo> containerInfo = taskRequest.getDeploy().getContainerInfo();
if (desiredTaskResources.getNumPorts() > 0 || hasLiteralPortMapping(containerInfo)) {
List<Long> requestedPorts = new ArrayList<>();
if (hasLiteralPortMapping(containerInfo)) {
requestedPorts.addAll(containerInfo.get().getDocker().get().getLiteralHostPorts());
}
portsResource = Optional.of(MesosUtils.getPortsResource(desiredTaskResources.getNumPorts(), availableResources, requestedPorts));
ports = Optional.of(MesosUtils.getPorts(portsResource.get(), desiredTaskResources.getNumPorts()));
}
if (containerInfo.isPresent()) {
prepareContainerInfo(offerHolder, taskId, bldr, containerInfo.get(), ports);
}
if (taskRequest.getDeploy().getCustomExecutorCmd().isPresent()) {
prepareCustomExecutor(bldr, taskId, taskRequest, offerHolder, ports, desiredExecutorResources);
} else {
prepareCommand(bldr, taskId, taskRequest, offerHolder, ports);
}
if (portsResource.isPresent()) {
bldr.addResources(portsResource.get());
}
Optional<String> requiredRole = taskRequest.getRequest().getRequiredRole();
bldr.addResources(MesosUtils.getCpuResource(desiredTaskResources.getCpus(), requiredRole));
bldr.addResources(MesosUtils.getMemoryResource(desiredTaskResources.getMemoryMb(), requiredRole));
bldr.addResources(MesosUtils.getDiskResource(desiredTaskResources.getDiskMb(), requiredRole));
bldr.setAgentId(offerHolder.getOffers().get(0).getAgentId());
bldr.setName(taskRequest.getRequest().getId());
final Builder labelsBuilder = Labels.newBuilder();
// apply request-specific labels, if any
if (taskRequest.getDeploy().getMesosLabels().isPresent() && !taskRequest.getDeploy().getMesosLabels().get().isEmpty()) {
for (SingularityMesosTaskLabel label : taskRequest.getDeploy().getMesosLabels().get()) {
org.apache.mesos.v1.Protos.Label.Builder labelBuilder = Label.newBuilder();
labelBuilder.setKey(label.getKey());
if ((label.getValue().isPresent())) {
labelBuilder.setValue(label.getValue().get());
}
labelsBuilder.addLabels(labelBuilder.build());
}
}
// apply task-specific labels, if any
final int taskInstanceNo = taskRequest.getPendingTask().getPendingTaskId().getInstanceNo();
if (taskRequest.getDeploy().getMesosTaskLabels().isPresent() && taskRequest.getDeploy().getMesosTaskLabels().get().containsKey(taskInstanceNo) && !taskRequest.getDeploy().getMesosTaskLabels().get().get(taskInstanceNo).isEmpty()) {
for (SingularityMesosTaskLabel label : taskRequest.getDeploy().getMesosTaskLabels().get().get(taskInstanceNo)) {
org.apache.mesos.v1.Protos.Label.Builder labelBuilder = Label.newBuilder();
labelBuilder.setKey(label.getKey());
if ((label.getValue().isPresent())) {
labelBuilder.setValue(label.getValue().get());
}
labelsBuilder.addLabels(labelBuilder.build());
}
}
bldr.setLabels(labelsBuilder);
TaskInfo task = bldr.build();
return new SingularityMesosTaskHolder(new SingularityTask(taskRequest, taskId, offerHolder.getOffers().stream().map((o) -> mesosProtosUtils.offerFromProtos(o)).collect(Collectors.toList()), mesosProtosUtils.taskFromProtos(task), Optional.of(offerHolder.getRackId())), task);
}
use of com.hubspot.singularity.SingularityTask in project Singularity by HubSpot.
the class TaskResource method getTaskStatistics.
@GET
@Path("/task/{taskId}/statistics")
@ApiOperation("Retrieve statistics about a specific active task.")
public MesosTaskStatisticsObject getTaskStatistics(@Auth SingularityUser user, @PathParam("taskId") String taskId) {
SingularityTask task = checkActiveTask(taskId, SingularityAuthorizationScope.READ, user);
String executorIdToMatch = null;
if (task.getMesosTask().hasExecutor()) {
executorIdToMatch = task.getMesosTask().getExecutor().getExecutorId().getValue();
} else {
executorIdToMatch = taskId;
}
for (MesosTaskMonitorObject taskMonitor : mesosClient.getSlaveResourceUsage(task.getHostname())) {
if (taskMonitor.getExecutorId().equals(executorIdToMatch)) {
return taskMonitor.getStatistics();
}
}
throw notFound("Couldn't find executor %s for %s on slave %s", executorIdToMatch, taskId, task.getHostname());
}
use of com.hubspot.singularity.SingularityTask in project Singularity by HubSpot.
the class TaskResource method checkActiveTask.
private SingularityTask checkActiveTask(String taskId, SingularityAuthorizationScope scope, SingularityUser user) {
SingularityTaskId taskIdObj = getTaskIdFromStr(taskId);
Optional<SingularityTask> task = taskManager.getTask(taskIdObj);
checkNotFound(task.isPresent() && taskManager.isActiveTask(taskId), "No active task with id %s", taskId);
if (task.isPresent()) {
authorizationHelper.checkForAuthorizationByRequestId(task.get().getTaskId().getRequestId(), user, scope);
}
return task.get();
}
use of com.hubspot.singularity.SingularityTask in project Singularity by HubSpot.
the class SingularityExecutorCleanup method getRunningTaskIds.
private Set<String> getRunningTaskIds() {
final String slaveId = mesosClient.getSlaveState(mesosClient.getSlaveUri(hostname)).getId();
final Collection<SingularityTask> activeTasks = singularityClient.getActiveTasksOnSlave(slaveId);
final Set<String> runningTaskIds = Sets.newHashSet();
for (SingularityTask task : activeTasks) {
runningTaskIds.add(task.getTaskId().getId());
}
return runningTaskIds;
}
Aggregations