Search in sources :

Example 1 with SingularityTaskHistory

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

the class TaskManager method getTaskHistory.

public Optional<SingularityTaskHistory> getTaskHistory(SingularityTaskId taskId) {
    final Optional<SingularityTask> task = getTaskCheckCache(taskId, true);
    if (!task.isPresent()) {
        return Optional.absent();
    }
    List<SingularityTaskHistoryUpdate> taskUpdates = getTaskHistoryUpdates(taskId);
    Optional<String> directory = getDirectory(taskId);
    Optional<String> containerId = getContainerId(taskId);
    List<SingularityTaskHealthcheckResult> healthchecks = getHealthcheckResults(taskId);
    List<SingularityLoadBalancerUpdate> loadBalancerUpdates = Lists.newArrayListWithCapacity(2);
    checkLoadBalancerHistory(loadBalancerUpdates, taskId, LoadBalancerRequestType.ADD);
    checkLoadBalancerHistory(loadBalancerUpdates, taskId, LoadBalancerRequestType.REMOVE);
    List<SingularityTaskShellCommandHistory> shellCommandHistory = getTaskShellCommandHistory(taskId);
    List<SingularityTaskMetadata> taskMetadata = getTaskMetadata(taskId);
    return Optional.of(new SingularityTaskHistory(taskUpdates, directory, containerId, healthchecks, task.get(), loadBalancerUpdates, shellCommandHistory, taskMetadata));
}
Also used : SingularityLoadBalancerUpdate(com.hubspot.singularity.SingularityLoadBalancerUpdate) SingularityTaskShellCommandHistory(com.hubspot.singularity.SingularityTaskShellCommandHistory) SingularityTaskHealthcheckResult(com.hubspot.singularity.SingularityTaskHealthcheckResult) SingularityTask(com.hubspot.singularity.SingularityTask) SingularityTaskMetadata(com.hubspot.singularity.SingularityTaskMetadata) SingularityTaskHistoryUpdate(com.hubspot.singularity.SingularityTaskHistoryUpdate) SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory)

Example 2 with SingularityTaskHistory

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

the class SingularityMailPoller method checkToSendTaskFinishedMail.

private void checkToSendTaskFinishedMail(SingularityTaskId taskId) {
    Optional<SingularityRequestWithState> requestWithState = requestManager.getRequest(taskId.getRequestId());
    Optional<SingularityTaskHistory> taskHistory = taskManager.getTaskHistory(taskId);
    ShouldSendMailState shouldSendState = shouldSendTaskFinishedMail(taskId, requestWithState, taskHistory);
    if (shouldSendState == ShouldSendMailState.WAIT) {
        return;
    }
    try {
        mailer.sendTaskCompletedMail(taskHistory.get(), requestWithState.get().getRequest());
    } catch (Throwable t) {
        LOG.error("While trying to send task completed mail for {}", taskId, t);
    } finally {
        SingularityDeleteResult result = taskManager.deleteFinishedTaskMailQueue(taskId);
        LOG.debug("Task {} mail sent with status {} (delete result {})", taskId, shouldSendState, result);
    }
}
Also used : SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) SingularityDeleteResult(com.hubspot.singularity.SingularityDeleteResult)

Example 3 with SingularityTaskHistory

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

the class S3LogResource method getS3PrefixesForTask.

// Generation of prefixes
private Collection<String> getS3PrefixesForTask(S3Configuration s3Configuration, SingularityTaskId taskId, Optional<Long> startArg, Optional<Long> endArg, String group, SingularityUser user) {
    Optional<SingularityTaskHistory> history = getTaskHistory(taskId, user);
    long start = taskId.getStartedAt();
    if (startArg.isPresent()) {
        start = Math.max(startArg.get(), start);
    }
    long end = start + s3Configuration.getMissingTaskDefaultS3SearchPeriodMillis();
    if (history.isPresent()) {
        SimplifiedTaskState taskState = SingularityTaskHistoryUpdate.getCurrentState(history.get().getTaskUpdates());
        if (taskState == SimplifiedTaskState.DONE) {
            end = Iterables.getLast(history.get().getTaskUpdates()).getTimestamp();
        } else {
            end = System.currentTimeMillis();
        }
    }
    if (endArg.isPresent()) {
        end = Math.min(endArg.get(), end);
    }
    Optional<String> tag = Optional.absent();
    if (history.isPresent() && history.get().getTask().getTaskRequest().getDeploy().getExecutorData().isPresent()) {
        tag = history.get().getTask().getTaskRequest().getDeploy().getExecutorData().get().getLoggingTag();
    }
    Collection<String> prefixes = SingularityS3FormatHelper.getS3KeyPrefixes(s3Configuration.getS3KeyFormat(), taskId, tag, start, end, group);
    for (SingularityS3UploaderFile additionalFile : s3Configuration.getS3UploaderAdditionalFiles()) {
        if (additionalFile.getS3UploaderKeyPattern().isPresent() && !additionalFile.getS3UploaderKeyPattern().get().equals(s3Configuration.getS3KeyFormat())) {
            prefixes.addAll(SingularityS3FormatHelper.getS3KeyPrefixes(additionalFile.getS3UploaderKeyPattern().get(), taskId, tag, start, end, group));
        }
    }
    LOG.trace("Task {} got S3 prefixes {} for start {}, end {}, tag {}", taskId, prefixes, start, end, tag);
    return prefixes;
}
Also used : SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) SimplifiedTaskState(com.hubspot.singularity.SingularityTaskHistoryUpdate.SimplifiedTaskState) SingularityS3UploaderFile(com.hubspot.singularity.SingularityS3UploaderFile)

Example 4 with SingularityTaskHistory

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

the class S3LogResource method getRequestGroupForTask.

// Finding request group
private Optional<String> getRequestGroupForTask(final SingularityTaskId taskId, SingularityUser user) {
    Optional<SingularityTaskHistory> maybeTaskHistory = getTaskHistory(taskId, user);
    if (maybeTaskHistory.isPresent()) {
        SingularityRequest request = maybeTaskHistory.get().getTask().getTaskRequest().getRequest();
        authorizationHelper.checkForAuthorization(request, user, SingularityAuthorizationScope.READ);
        return request.getGroup();
    } else {
        return getRequestGroup(taskId.getRequestId(), user);
    }
}
Also used : SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) SingularityRequest(com.hubspot.singularity.SingularityRequest)

Example 5 with SingularityTaskHistory

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

the class SandboxResource method read.

@GET
@Path("/{taskId}/read")
@ApiOperation("Retrieve part of the contents of a file in a specific task's sandbox.")
public MesosFileChunkObject read(@Auth SingularityUser user, @ApiParam("The task ID of the sandbox to read from") @PathParam("taskId") String taskId, @ApiParam("The path to the file to be read") @QueryParam("path") String path, @ApiParam("Optional string to grep for") @QueryParam("grep") Optional<String> grep, @ApiParam("Byte offset to start reading from") @QueryParam("offset") Optional<Long> offset, @ApiParam("Maximum number of bytes to read") @QueryParam("length") Optional<Long> length) {
    authorizationHelper.checkForAuthorizationByTaskId(taskId, user, SingularityAuthorizationScope.READ);
    final SingularityTaskHistory history = checkHistory(taskId, user);
    checkBadRequest(!Strings.isNullOrEmpty(path), "Must specify 'path'");
    final String slaveHostname = history.getTask().getHostname();
    final String fullPath = new File(history.getDirectory().get(), path).toString();
    try {
        final Optional<MesosFileChunkObject> maybeChunk = sandboxManager.read(slaveHostname, fullPath, offset, length);
        checkNotFound(maybeChunk.isPresent(), "File %s does not exist for task ID %s", fullPath, taskId);
        if (grep.isPresent() && !Strings.isNullOrEmpty(grep.get())) {
            final Pattern grepPattern = Pattern.compile(grep.get());
            final StringBuilder strBuilder = new StringBuilder(maybeChunk.get().getData().length());
            for (String line : Splitter.on("\n").split(maybeChunk.get().getData())) {
                if (grepPattern.matcher(line).find()) {
                    strBuilder.append(line);
                    strBuilder.append("\n");
                }
            }
            return new MesosFileChunkObject(strBuilder.toString(), maybeChunk.get().getOffset(), Optional.of(maybeChunk.get().getOffset() + maybeChunk.get().getData().length()));
        }
        return maybeChunk.get();
    } catch (SlaveNotFoundException snfe) {
        throw notFound("Slave @ %s was not found, it is probably offline", slaveHostname);
    }
}
Also used : Pattern(java.util.regex.Pattern) SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) MesosFileChunkObject(com.hubspot.mesos.json.MesosFileChunkObject) 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