Search in sources :

Example 1 with SingularityRequestWithState

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

the class SingularityWebCache method cacheRequests.

public void cacheRequests(List<SingularityRequestWithState> requests) {
    requestsMissMeter.mark();
    Map<String, SingularityRequestWithState> newRequests = new HashMap<>(requests.size());
    for (SingularityRequestWithState request : requests) {
        newRequests.put(request.getRequest().getId(), request);
    }
    cachedRequests = newRequests;
    lastRequestsCache = System.currentTimeMillis();
}
Also used : HashMap(java.util.HashMap) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState)

Example 2 with SingularityRequestWithState

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

the class StateManager method getNumTasks.

private Map<String, Long> getNumTasks(List<SingularityRequestWithState> requests) {
    final CounterMap<String> numTasks = new CounterMap<>(requests.size());
    for (SingularityTaskId taskId : taskManager.getActiveTaskIds()) {
        numTasks.incr(taskId.getRequestId());
    }
    for (SingularityPendingTaskId pendingTaskId : taskManager.getPendingTaskIds()) {
        numTasks.incr(pendingTaskId.getRequestId());
    }
    for (SingularityTaskId cleaningTaskId : taskManager.getCleanupTaskIds()) {
        Optional<SingularityRequestWithState> request = requestManager.getRequest(cleaningTaskId.getRequestId());
        if (request.isPresent() && request.get().getRequest().isScheduled()) {
            continue;
        }
        numTasks.decr(cleaningTaskId.getRequestId());
    }
    return numTasks.toCountMap();
}
Also used : CounterMap(com.hubspot.mesos.CounterMap) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Example 3 with SingularityRequestWithState

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

the class ScheduleMigration method applyMigration.

@Override
public void applyMigration() {
    LOG.info("Starting migration to fix certain CRON schedules");
    final long start = System.currentTimeMillis();
    int num = 0;
    for (SingularityRequestWithState requestWithState : requestManager.getRequests()) {
        if (requestWithState.getRequest().isScheduled()) {
            Optional<String> schedule = requestWithState.getRequest().getSchedule();
            Optional<String> quartzSchedule = requestWithState.getRequest().getQuartzSchedule();
            Optional<ScheduleType> scheduleType = requestWithState.getRequest().getScheduleType();
            if (scheduleType.isPresent() && scheduleType.get() != ScheduleType.CRON) {
                LOG.info("Skipping {}, it had schedule type: {}", requestWithState.getRequest().getId(), scheduleType.get());
                continue;
            }
            if (quartzSchedule.isPresent() && schedule.isPresent() && quartzSchedule.get().equals(schedule.get())) {
                LOG.info("Skipping {}, assuming it was quartz - it had quartz schedule == schedule {}", requestWithState.getRequest().getId(), schedule.get());
                continue;
            }
            if (!schedule.isPresent()) {
                LOG.info("Skipping {}, it had no schedule", requestWithState.getRequest().getId());
                continue;
            }
            String actualSchedule = schedule.get();
            String newQuartzSchedule = null;
            try {
                newQuartzSchedule = validator.getQuartzScheduleFromCronSchedule(actualSchedule);
            } catch (WebApplicationException e) {
                LOG.error("Failed to convert {} ({}) due to {}", requestWithState.getRequest().getId(), actualSchedule, e.getResponse().getEntity());
                continue;
            }
            if (quartzSchedule.isPresent() && quartzSchedule.get().equals(newQuartzSchedule)) {
                LOG.info("Skipping {}, migration had no effect {}", requestWithState.getRequest().getId(), newQuartzSchedule);
                continue;
            }
            SingularityRequest newRequest = requestWithState.getRequest().toBuilder().setQuartzSchedule(Optional.of(newQuartzSchedule)).build();
            try {
                LOG.info("Saving new schedule (quartz {} - from {}) for {}", newQuartzSchedule, actualSchedule, newRequest.getId());
                curator.setData().forPath("/requests/all/" + newRequest.getId(), requestTranscoder.toBytes(new SingularityRequestWithState(newRequest, requestWithState.getState(), requestWithState.getTimestamp())));
                num++;
            } catch (Throwable t) {
                LOG.error("Failed to write {}", newRequest.getId(), t);
                throw Throwables.propagate(t);
            }
        }
    }
    LOG.info("Applied {} in {}", num, JavaUtils.duration(start));
}
Also used : ScheduleType(com.hubspot.singularity.ScheduleType) WebApplicationException(javax.ws.rs.WebApplicationException) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityRequest(com.hubspot.singularity.SingularityRequest)

Example 4 with SingularityRequestWithState

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

the class SingularityAuthorizationHelper method checkForAuthorizationByTaskId.

public void checkForAuthorizationByTaskId(String taskId, SingularityUser user, SingularityAuthorizationScope scope) {
    if (authEnabled) {
        checkForbidden(user.isAuthenticated(), "Not Authenticated!");
        try {
            final SingularityTaskId taskIdObj = SingularityTaskId.valueOf(taskId);
            final Optional<SingularityRequestWithState> maybeRequest = requestManager.getRequest(taskIdObj.getRequestId());
            if (maybeRequest.isPresent()) {
                checkForAuthorization(maybeRequest.get().getRequest(), user, scope);
            }
        } catch (InvalidSingularityTaskIdException e) {
            badRequest(e.getMessage());
        }
    }
}
Also used : InvalidSingularityTaskIdException(com.hubspot.singularity.InvalidSingularityTaskIdException) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Example 5 with SingularityRequestWithState

use of com.hubspot.singularity.SingularityRequestWithState 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);
}
Also used : SingularityCreateResult(com.hubspot.singularity.SingularityCreateResult) ExtendedTaskState(com.hubspot.singularity.ExtendedTaskState) SingularityTask(com.hubspot.singularity.SingularityTask) SingularityTaskHistoryUpdate(com.hubspot.singularity.SingularityTaskHistoryUpdate) SingularityPendingDeploy(com.hubspot.singularity.SingularityPendingDeploy) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityTaskStatusHolder(com.hubspot.singularity.SingularityTaskStatusHolder)

Aggregations

SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)37 SingularityRequest (com.hubspot.singularity.SingularityRequest)16 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)11 SingularityPendingDeploy (com.hubspot.singularity.SingularityPendingDeploy)9 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)7 SingularityRequestDeployState (com.hubspot.singularity.SingularityRequestDeployState)7 ArrayList (java.util.ArrayList)7 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)6 SingularityCreateResult (com.hubspot.singularity.SingularityCreateResult)5 SingularityDeployKey (com.hubspot.singularity.SingularityDeployKey)5 SingularityPendingTaskId (com.hubspot.singularity.SingularityPendingTaskId)5 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)5 Path (javax.ws.rs.Path)5 RequestState (com.hubspot.singularity.RequestState)4 SingularityDeployMarker (com.hubspot.singularity.SingularityDeployMarker)4 SingularityRequestCleanup (com.hubspot.singularity.SingularityRequestCleanup)4 SingularityRequestHistory (com.hubspot.singularity.SingularityRequestHistory)4 SingularityBounceRequest (com.hubspot.singularity.api.SingularityBounceRequest)4 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)4 List (java.util.List)4