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();
}
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();
}
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));
}
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());
}
}
}
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);
}
Aggregations