Search in sources :

Example 6 with SingularityTaskHistory

use of com.hubspot.singularity.SingularityTaskHistory in project Singularity by HubSpot.

the class TaskTrackerResource method getTaskStateByRunId.

@GET
@Path("/run/{requestId}/{runId}")
@ApiOperation(value = "Get the current state of a task by taskId whether it is pending, active, or inactive")
@ApiResponses({ @ApiResponse(code = 404, message = "Task with this runId does not exist") })
public Optional<SingularityTaskState> getTaskStateByRunId(@Auth SingularityUser user, @PathParam("requestId") String requestId, @PathParam("runId") String runId) {
    authorizationHelper.checkForAuthorizationByRequestId(requestId, user, SingularityAuthorizationScope.READ);
    // Check if it's active or inactive
    Optional<SingularityTaskId> maybeTaskId = taskManager.getTaskByRunId(requestId, runId);
    if (maybeTaskId.isPresent()) {
        Optional<SingularityTaskState> maybeTaskState = getTaskStateFromId(maybeTaskId.get());
        if (maybeTaskState.isPresent()) {
            return maybeTaskState;
        }
    } else {
        Optional<SingularityTaskHistory> maybeTaskHistory = historyManager.getTaskHistoryByRunId(requestId, runId);
        if (maybeTaskHistory.isPresent()) {
            return Optional.of(SingularityTaskState.fromTaskHistory(maybeTaskHistory.get()));
        }
    }
    // Check if it's pending
    for (SingularityPendingTask pendingTask : taskManager.getPendingTasksForRequest(requestId)) {
        if (pendingTask.getRunId().isPresent() && pendingTask.getRunId().get().equals(runId)) {
            return Optional.of(new SingularityTaskState(Optional.absent(), pendingTask.getPendingTaskId(), pendingTask.getRunId(), Optional.absent(), Collections.emptyList(), true));
        }
    }
    for (SingularityPendingRequest pendingRequest : requestManager.getPendingRequests()) {
        if (pendingRequest.getRequestId().equals(requestId) && pendingRequest.getRunId().isPresent() && pendingRequest.getRunId().get().equals(runId)) {
            return Optional.of(new SingularityTaskState(Optional.absent(), Optional.absent(), pendingRequest.getRunId(), Optional.absent(), Collections.emptyList(), true));
        }
    }
    return Optional.absent();
}
Also used : SingularityTaskState(com.hubspot.singularity.SingularityTaskState) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask) SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ApiResponses(com.wordnik.swagger.annotations.ApiResponses)

Example 7 with SingularityTaskHistory

use of com.hubspot.singularity.SingularityTaskHistory in project Singularity by HubSpot.

the class SingularityExecutorCleanup method clean.

public SingularityExecutorCleanupStatistics clean() {
    final SingularityExecutorCleanupStatisticsBuilder statisticsBldr = new SingularityExecutorCleanupStatisticsBuilder();
    final Path directory = Paths.get(executorConfiguration.getGlobalTaskDefinitionDirectory());
    Set<String> runningTaskIds = null;
    try {
        runningTaskIds = getRunningTaskIds();
    } catch (Exception e) {
        LOG.error("While fetching running tasks from singularity", e);
        exceptionNotifier.notify(String.format("Error fetching running tasks (%s)", e.getMessage()), e, Collections.<String, String>emptyMap());
        statisticsBldr.setErrorMessage(e.getMessage());
        return statisticsBldr.build();
    }
    LOG.info("Found {} running tasks from Mesos", runningTaskIds);
    statisticsBldr.setMesosRunningTasks(runningTaskIds.size());
    if (runningTaskIds.isEmpty()) {
        if (!isDecommissioned()) {
            if (cleanupConfiguration.isSafeModeWontRunWithNoTasks()) {
                final String errorMessage = "Running in safe mode and found 0 running tasks - aborting cleanup";
                LOG.error(errorMessage);
                statisticsBldr.setErrorMessage(errorMessage);
                return statisticsBldr.build();
            } else {
                LOG.warn("Found 0 running tasks - proceeding with cleanup as we are not in safe mode");
            }
        } else {
            if (!cleanupConfiguration.isCleanTasksWhenDecommissioned()) {
                return statisticsBldr.build();
            }
        }
    }
    if (cleanupConfiguration.isRunDockerCleanup()) {
        cleanDocker(runningTaskIds);
    }
    for (Path file : JavaUtils.iterable(directory)) {
        if (!Objects.toString(file.getFileName()).endsWith(executorConfiguration.getGlobalTaskDefinitionSuffix())) {
            LOG.debug("Ignoring file {} that doesn't have suffix {}", file, executorConfiguration.getGlobalTaskDefinitionSuffix());
            statisticsBldr.incrInvalidTasks();
            continue;
        }
        statisticsBldr.incrTotalTaskFiles();
        try {
            Optional<SingularityExecutorTaskDefinition> maybeTaskDefinition = jsonObjectFileHelper.read(file, LOG, SingularityExecutorTaskDefinition.class);
            if (!maybeTaskDefinition.isPresent()) {
                statisticsBldr.incrInvalidTasks();
                continue;
            }
            SingularityExecutorTaskDefinition taskDefinition = withDefaults(maybeTaskDefinition.get());
            final String taskId = taskDefinition.getTaskId();
            LOG.info("{} - Starting possible cleanup", taskId);
            if (runningTaskIds.contains(taskId) || executorStillRunning(taskDefinition)) {
                statisticsBldr.incrRunningTasksIgnored();
                continue;
            }
            Optional<SingularityTaskHistory> taskHistory = null;
            try {
                taskHistory = singularityClient.getHistoryForTask(taskId);
            } catch (SingularityClientException sce) {
                LOG.error("{} - Failed fetching history", taskId, sce);
                exceptionNotifier.notify(String.format("Error fetching history (%s)", sce.getMessage()), sce, ImmutableMap.<String, String>of("taskId", taskId));
                statisticsBldr.incrErrorTasks();
                continue;
            }
            TaskCleanupResult result = cleanTask(taskDefinition, taskHistory);
            LOG.info("{} - {}", taskId, result);
            switch(result) {
                case ERROR:
                    statisticsBldr.incrErrorTasks();
                    break;
                case SUCCESS:
                    statisticsBldr.incrSuccessfullyCleanedTasks();
                    break;
                case WAITING:
                    statisticsBldr.incrWaitingTasks();
                    break;
                default:
                    break;
            }
        } catch (IOException ioe) {
            LOG.error("Couldn't read file {}", file, ioe);
            exceptionNotifier.notify(String.format("Error reading file (%s)", ioe.getMessage()), ioe, ImmutableMap.of("file", file.toString()));
            statisticsBldr.incrIoErrorTasks();
        }
    }
    return statisticsBldr.build();
}
Also used : Path(java.nio.file.Path) SingularityClientException(com.hubspot.singularity.client.SingularityClientException) TaskCleanupResult(com.hubspot.singularity.executor.task.TaskCleanupResult) SingularityExecutorTaskDefinition(com.hubspot.singularity.executor.task.SingularityExecutorTaskDefinition) SingularityExecutorCleanupStatisticsBuilder(com.hubspot.singularity.executor.SingularityExecutorCleanupStatistics.SingularityExecutorCleanupStatisticsBuilder) SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) IOException(java.io.IOException) ProcessFailedException(com.hubspot.singularity.runner.base.shared.ProcessFailedException) SingularityClientException(com.hubspot.singularity.client.SingularityClientException) IOException(java.io.IOException)

Example 8 with SingularityTaskHistory

use of com.hubspot.singularity.SingularityTaskHistory in project Singularity by HubSpot.

the class SandboxResource method checkHistory.

private SingularityTaskHistory checkHistory(String taskId, SingularityUser user) {
    final SingularityTaskId taskIdObj = getTaskIdObject(taskId);
    final SingularityTaskHistory taskHistory = getTaskHistoryRequired(taskIdObj, user);
    if (!taskHistory.getDirectory().isPresent()) {
        logSupport.checkDirectoryAndContainerId(taskIdObj);
        throw badRequest("Task %s does not have a directory yet - check again soon (enqueued request to refetch)", taskId);
    }
    return taskHistory;
}
Also used : SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Example 9 with SingularityTaskHistory

use of com.hubspot.singularity.SingularityTaskHistory in project Singularity by HubSpot.

the class SandboxResource method browse.

@GET
@Path("/{taskId}/browse")
@ApiOperation("Retrieve information about a specific task's sandbox.")
public SingularitySandbox browse(@Auth SingularityUser user, @ApiParam("The task ID to browse") @PathParam("taskId") String taskId, @ApiParam("The path to browse from") @QueryParam("path") String path) {
    authorizationHelper.checkForAuthorizationByTaskId(taskId, user, SingularityAuthorizationScope.READ);
    // Remove all trailing slashes from the path
    if (path != null) {
        path = path.replaceAll("\\/+$", "");
    }
    final String currentDirectory = getCurrentDirectory(taskId, path);
    final SingularityTaskHistory history = checkHistory(taskId, user);
    final String slaveHostname = history.getTask().getHostname();
    final String pathToRoot = history.getDirectory().get();
    final String fullPath = new File(pathToRoot, currentDirectory).toString();
    final int substringTruncationLength = currentDirectory.length() == 0 ? pathToRoot.length() + 1 : pathToRoot.length() + currentDirectory.length() + 2;
    try {
        Collection<MesosFileObject> mesosFiles = sandboxManager.browse(slaveHostname, fullPath);
        List<SingularitySandboxFile> sandboxFiles = Lists.newArrayList(Iterables.transform(mesosFiles, new Function<MesosFileObject, SingularitySandboxFile>() {

            @Override
            public SingularitySandboxFile apply(MesosFileObject input) {
                return new SingularitySandboxFile(input.getPath().substring(substringTruncationLength), input.getMtime(), input.getSize(), input.getMode());
            }
        }));
        return new SingularitySandbox(sandboxFiles, pathToRoot, currentDirectory, slaveHostname);
    } catch (SlaveNotFoundException snfe) {
        throw notFound("Slave @ %s was not found, it is probably offline", slaveHostname);
    }
}
Also used : SingularitySandbox(com.hubspot.singularity.SingularitySandbox) SingularitySandboxFile(com.hubspot.singularity.SingularitySandboxFile) Function(com.google.common.base.Function) MesosFileObject(com.hubspot.mesos.json.MesosFileObject) SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) File(java.io.File) SingularitySandboxFile(com.hubspot.singularity.SingularitySandboxFile) SlaveNotFoundException(com.hubspot.singularity.data.SandboxManager.SlaveNotFoundException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(com.wordnik.swagger.annotations.ApiOperation)

Aggregations

SingularityTaskHistory (com.hubspot.singularity.SingularityTaskHistory)9 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 SingularitySandboxFile (com.hubspot.singularity.SingularitySandboxFile)2 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)2 SlaveNotFoundException (com.hubspot.singularity.data.SandboxManager.SlaveNotFoundException)2 File (java.io.File)2 Function (com.google.common.base.Function)1 MesosFileChunkObject (com.hubspot.mesos.json.MesosFileChunkObject)1 MesosFileObject (com.hubspot.mesos.json.MesosFileObject)1 SingularityDeleteResult (com.hubspot.singularity.SingularityDeleteResult)1 SingularityLoadBalancerUpdate (com.hubspot.singularity.SingularityLoadBalancerUpdate)1 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)1 SingularityPendingTask (com.hubspot.singularity.SingularityPendingTask)1 SingularityRequest (com.hubspot.singularity.SingularityRequest)1 SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)1 SingularityS3UploaderFile (com.hubspot.singularity.SingularityS3UploaderFile)1 SingularitySandbox (com.hubspot.singularity.SingularitySandbox)1 SingularityTask (com.hubspot.singularity.SingularityTask)1