use of com.hubspot.singularity.SingularityRequest in project Singularity by HubSpot.
the class TaskRequestManager method getTaskRequests.
public List<SingularityTaskRequest> getTaskRequests(List<SingularityPendingTask> tasks) {
final Multimap<String, SingularityPendingTask> requestIdToPendingTaskId = ArrayListMultimap.create(tasks.size(), 1);
for (SingularityPendingTask task : tasks) {
requestIdToPendingTaskId.put(task.getPendingTaskId().getRequestId(), task);
}
final List<SingularityRequestWithState> matchingRequests = requestManager.getRequests(requestIdToPendingTaskId.keySet());
final Map<SingularityPendingTask, SingularityDeployKey> deployKeys = SingularityDeployKey.fromPendingTasks(requestIdToPendingTaskId.values());
final Map<SingularityDeployKey, SingularityDeploy> matchingDeploys = deployManager.getDeploysForKeys(Sets.newHashSet(deployKeys.values()));
final List<SingularityTaskRequest> taskRequests = Lists.newArrayListWithCapacity(matchingRequests.size());
for (SingularityRequestWithState request : matchingRequests) {
Optional<SingularityPendingDeploy> maybePendingDeploy = deployManager.getPendingDeploy(request.getRequest().getId());
for (SingularityPendingTask task : requestIdToPendingTaskId.get(request.getRequest().getId())) {
SingularityDeploy foundDeploy = matchingDeploys.get(deployKeys.get(task));
if (foundDeploy == null) {
LOG.warn("Couldn't find a matching deploy for pending task {}", task);
continue;
}
if (!request.getState().isRunnable()) {
LOG.warn("Request was in state {} for pending task {}", request.getState(), task);
continue;
}
Optional<SingularityRequest> updatedRequest = maybePendingDeploy.isPresent() && maybePendingDeploy.get().getDeployMarker().getDeployId().equals(task.getPendingTaskId().getDeployId()) ? maybePendingDeploy.get().getUpdatedRequest() : Optional.<SingularityRequest>absent();
taskRequests.add(new SingularityTaskRequest(updatedRequest.or(request.getRequest()), foundDeploy, task));
}
}
return taskRequests;
}
use of com.hubspot.singularity.SingularityRequest in project Singularity by HubSpot.
the class RequestResource method scale.
public SingularityRequestParent scale(String requestId, SingularityScaleRequest scaleRequest, SingularityUser user) {
SingularityRequestWithState oldRequestWithState = fetchRequestWithState(requestId, user);
SingularityRequest oldRequest = oldRequestWithState.getRequest();
authorizationHelper.checkForAuthorization(oldRequest, user, SingularityAuthorizationScope.WRITE);
validator.checkActionEnabled(SingularityAction.SCALE_REQUEST);
SingularityRequest newRequest = oldRequest.toBuilder().setInstances(scaleRequest.getInstances()).build();
validator.checkScale(newRequest, Optional.<Integer>absent());
checkBadRequest(oldRequest.getInstancesSafe() != newRequest.getInstancesSafe(), "Scale request has no affect on the # of instances (%s)", newRequest.getInstancesSafe());
String scaleMessage = String.format("Scaling from %d -> %d", oldRequest.getInstancesSafe(), newRequest.getInstancesSafe());
if (scaleRequest.getMessage().isPresent()) {
scaleMessage = String.format("%s -- %s", scaleRequest.getMessage().get(), scaleMessage);
} else {
scaleMessage = String.format("%s", scaleMessage);
}
if (scaleRequest.getBounce().or(newRequest.getBounceAfterScale().or(false))) {
validator.checkActionEnabled(SingularityAction.BOUNCE_REQUEST);
checkBadRequest(newRequest.isLongRunning(), "Can not bounce a %s request (%s)", newRequest.getRequestType(), newRequest);
checkConflict(oldRequestWithState.getState() != RequestState.PAUSED, "Request %s is paused. Unable to bounce (it must be manually unpaused first)", newRequest.getId());
checkConflict(!requestManager.cleanupRequestExists(newRequest.getId(), RequestCleanupType.BOUNCE), "Request %s is already bouncing cannot bounce again", newRequest.getId());
final boolean isIncrementalBounce = scaleRequest.getIncremental().or(true);
validator.checkResourcesForBounce(newRequest, isIncrementalBounce);
validator.checkRequestForPriorityFreeze(newRequest);
SingularityBounceRequest bounceRequest = new SingularityBounceRequest(Optional.of(isIncrementalBounce), scaleRequest.getSkipHealthchecks(), Optional.<Long>absent(), Optional.of(UUID.randomUUID().toString()), Optional.<String>absent(), Optional.<SingularityShellCommand>absent());
submitRequest(newRequest, Optional.of(oldRequestWithState), Optional.of(RequestHistoryType.SCALED), scaleRequest.getSkipHealthchecks(), Optional.of(scaleMessage), Optional.of(bounceRequest), user);
} else {
submitRequest(newRequest, Optional.of(oldRequestWithState), Optional.of(RequestHistoryType.SCALED), scaleRequest.getSkipHealthchecks(), Optional.of(scaleMessage), Optional.absent(), user);
}
if (scaleRequest.getDurationMillis().isPresent()) {
requestManager.saveExpiringObject(new SingularityExpiringScale(requestId, user.getEmail(), System.currentTimeMillis(), scaleRequest, oldRequest.getInstances(), scaleRequest.getActionId().or(UUID.randomUUID().toString()), scaleRequest.getBounce()));
} else {
requestManager.deleteExpiringObject(SingularityExpiringScale.class, requestId);
}
if (!scaleRequest.getSkipEmailNotification().isPresent() || !scaleRequest.getSkipEmailNotification().get()) {
mailer.sendRequestScaledMail(newRequest, Optional.of(scaleRequest), oldRequest.getInstances(), user.getEmail());
}
return fillEntireRequest(fetchRequestWithState(requestId, user));
}
use of com.hubspot.singularity.SingularityRequest in project Singularity by HubSpot.
the class RequestResource method updateAuthorizedGroups.
public SingularityRequestParent updateAuthorizedGroups(SingularityUser user, String requestId, SingularityUpdateGroupsRequest updateGroupsRequest) {
SingularityRequestWithState oldRequestWithState = fetchRequestWithState(requestId, user);
authorizationHelper.checkForAuthorization(oldRequestWithState.getRequest(), user, SingularityAuthorizationScope.WRITE);
SingularityRequest newRequest = oldRequestWithState.getRequest().toBuilder().setGroup(updateGroupsRequest.getGroup()).setReadWriteGroups(Optional.of(updateGroupsRequest.getReadWriteGroups())).setReadOnlyGroups(Optional.of(updateGroupsRequest.getReadOnlyGroups())).build();
submitRequest(newRequest, Optional.of(oldRequestWithState), Optional.of(RequestHistoryType.UPDATED), Optional.absent(), updateGroupsRequest.getMessage(), Optional.absent(), user);
return fillEntireRequest(fetchRequestWithState(requestId, user));
}
use of com.hubspot.singularity.SingularityRequest in project Singularity by HubSpot.
the class RequestResource method deleteRequest.
public SingularityRequest deleteRequest(String requestId, Optional<SingularityDeleteRequestRequest> deleteRequest, SingularityUser user) {
SingularityRequest request = fetchRequestWithState(requestId, user).getRequest();
authorizationHelper.checkForAuthorization(request, user, SingularityAuthorizationScope.WRITE);
validator.checkActionEnabled(SingularityAction.REMOVE_REQUEST);
Optional<String> message = Optional.absent();
Optional<String> actionId = Optional.absent();
Optional<Boolean> deleteFromLoadBalancer = Optional.absent();
if (deleteRequest.isPresent()) {
actionId = deleteRequest.get().getActionId();
message = deleteRequest.get().getMessage();
deleteFromLoadBalancer = deleteRequest.get().getDeleteFromLoadBalancer();
}
requestManager.startDeletingRequest(request, deleteFromLoadBalancer, user.getEmail(), actionId, message);
mailer.sendRequestRemovedMail(request, user.getEmail(), message);
return request;
}
use of com.hubspot.singularity.SingularityRequest in project Singularity by HubSpot.
the class RequestResource method skipHealthchecks.
public SingularityRequestParent skipHealthchecks(String requestId, SingularitySkipHealthchecksRequest skipHealthchecksRequest, SingularityUser user) {
SingularityRequestWithState oldRequestWithState = fetchRequestWithState(requestId, user);
SingularityRequest oldRequest = oldRequestWithState.getRequest();
SingularityRequest newRequest = oldRequest.toBuilder().setSkipHealthchecks(skipHealthchecksRequest.getSkipHealthchecks()).build();
submitRequest(newRequest, Optional.of(oldRequestWithState), Optional.absent(), Optional.absent(), skipHealthchecksRequest.getMessage(), Optional.absent(), user);
if (skipHealthchecksRequest.getDurationMillis().isPresent()) {
requestManager.saveExpiringObject(new SingularityExpiringSkipHealthchecks(requestId, user.getEmail(), System.currentTimeMillis(), skipHealthchecksRequest, oldRequest.getSkipHealthchecks(), skipHealthchecksRequest.getActionId().or(UUID.randomUUID().toString())));
}
return fillEntireRequest(fetchRequestWithState(requestId, user));
}
Aggregations