use of com.hubspot.singularity.SingularityRequestWithState 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));
}
use of com.hubspot.singularity.SingularityRequestWithState in project Singularity by HubSpot.
the class RequestResource method checkAuthForGroupsUpdate.
@POST
@Path("/request/{requestId}/groups/auth-check")
@ApiOperation(value = "Check authorization for updating the group, readOnlyGroups, and readWriteGroups for a SingularityReques, without commiting the change")
@ApiResponses({ @ApiResponse(code = 200, message = "User is authorized to make these changes"), @ApiResponse(code = 401, message = "User is not authorized to make these updates") })
public Response checkAuthForGroupsUpdate(@Auth SingularityUser user, @PathParam("requestId") String requestId, @ApiParam("Updated groups") SingularityUpdateGroupsRequest updateGroupsRequest) {
Optional<SingularityRequestWithState> maybeOldRequestWithState = requestManager.getRequest(requestId, false);
if (!maybeOldRequestWithState.isPresent()) {
authorizationHelper.checkForAuthorization(user, Sets.union(updateGroupsRequest.getGroup().asSet(), updateGroupsRequest.getReadWriteGroups()), updateGroupsRequest.getReadOnlyGroups(), SingularityAuthorizationScope.WRITE, Optional.absent());
return Response.ok().build();
}
SingularityRequestWithState oldRequestWithState = maybeOldRequestWithState.get();
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();
authorizationHelper.checkForAuthorizedChanges(newRequest, oldRequestWithState.getRequest(), user);
return Response.ok().build();
}
use of com.hubspot.singularity.SingularityRequestWithState in project Singularity by HubSpot.
the class RequestHelper method getTaskIdsByStatusForRequest.
private Optional<SingularityTaskIdsByStatus> getTaskIdsByStatusForRequest(SingularityRequestWithState requestWithState) {
String requestId = requestWithState.getRequest().getId();
Optional<SingularityPendingDeploy> pendingDeploy = deployManager.getPendingDeploy(requestId);
List<SingularityTaskId> cleaningTaskIds = taskManager.getCleanupTaskIds().stream().filter((t) -> t.getRequestId().equals(requestId)).collect(Collectors.toList());
List<SingularityPendingTaskId> pendingTaskIds = taskManager.getPendingTaskIdsForRequest(requestId);
List<SingularityTaskId> activeTaskIds = taskManager.getActiveTaskIdsForRequest(requestId);
activeTaskIds.removeAll(cleaningTaskIds);
List<SingularityTaskId> healthyTaskIds = new ArrayList<>();
List<SingularityTaskId> notYetHealthyTaskIds = new ArrayList<>();
Map<String, List<SingularityTaskId>> taskIdsByDeployId = activeTaskIds.stream().collect(Collectors.groupingBy(SingularityTaskId::getDeployId));
for (Map.Entry<String, List<SingularityTaskId>> entry : taskIdsByDeployId.entrySet()) {
Optional<SingularityDeploy> deploy = deployManager.getDeploy(requestId, entry.getKey());
List<SingularityTaskId> healthyTasksIdsForDeploy = deployHealthHelper.getHealthyTasks(requestWithState.getRequest(), deploy, entry.getValue(), pendingDeploy.isPresent() && pendingDeploy.get().getDeployMarker().getDeployId().equals(entry.getKey()));
for (SingularityTaskId taskId : entry.getValue()) {
if (healthyTasksIdsForDeploy.contains(taskId)) {
healthyTaskIds.add(taskId);
} else {
notYetHealthyTaskIds.add(taskId);
}
}
}
return Optional.of(new SingularityTaskIdsByStatus(healthyTaskIds, notYetHealthyTaskIds, pendingTaskIds, cleaningTaskIds));
}
use of com.hubspot.singularity.SingularityRequestWithState in project Singularity by HubSpot.
the class RequestHelper method fillDataForRequestsAndFilter.
public List<SingularityRequestParent> fillDataForRequestsAndFilter(List<SingularityRequestWithState> requests, SingularityUser user, boolean filterRelevantForUser, boolean includeFullRequestData, Optional<Integer> limit, List<RequestType> requestTypeFilters) {
final Map<String, Optional<SingularityTaskIdHistory>> mostRecentTasks = new ConcurrentHashMap<>();
final Map<String, SingularityRequestDeployState> deployStates = deployManager.getRequestDeployStatesByRequestIds(requests.stream().map((r) -> r.getRequest().getId()).collect(Collectors.toList()));
final Map<String, Optional<SingularityRequestHistory>> requestIdToLastHistory;
if (includeFullRequestData) {
requestIdToLastHistory = requests.parallelStream().collect(Collectors.toMap((r) -> r.getRequest().getId(), (r) -> getMostRecentHistoryFromZk(r.getRequest().getId())));
} else {
requestIdToLastHistory = Collections.emptyMap();
}
Optional<SingularityUserSettings> maybeUserSettings = userManager.getUserSettings(user.getId());
return requests.parallelStream().filter((request) -> {
if (!requestTypeFilters.isEmpty() && !requestTypeFilters.contains(request.getRequest().getRequestType())) {
return false;
}
if (!filterRelevantForUser || user.equals(SingularityUser.DEFAULT_USER)) {
return true;
}
String requestId = request.getRequest().getId();
if (maybeUserSettings.isPresent() && maybeUserSettings.get().getStarredRequestIds().contains(requestId)) {
// This is a starred request for the user
return true;
}
if (request.getRequest().getGroup().isPresent() && user.getGroups().contains(request.getRequest().getGroup().get())) {
// The user is in the group for this request
return true;
}
if (includeFullRequestData) {
if (userModifiedRequestLast(requestIdToLastHistory.getOrDefault(requestId, Optional.absent()), user)) {
return true;
}
}
return userAssociatedWithDeploy(Optional.fromNullable(deployStates.get(requestId)), user);
}).map((request) -> {
Long lastActionTime = null;
if (includeFullRequestData) {
lastActionTime = getLastActionTimeForRequest(request.getRequest(), requestIdToLastHistory.getOrDefault(request.getRequest().getId(), Optional.absent()), Optional.fromNullable(deployStates.get(request.getRequest().getId())), mostRecentTasks.computeIfAbsent(request.getRequest().getId(), (id) -> getMostRecentTask(request.getRequest())));
} else {
// To save on zk calls, if not returning all data, use the most recent deploy timestamps
Optional<SingularityRequestDeployState> deployState = Optional.fromNullable(deployStates.get(request.getRequest().getId()));
if (deployState.isPresent()) {
if (deployState.get().getPendingDeploy().isPresent()) {
lastActionTime = deployState.get().getPendingDeploy().get().getTimestamp();
}
if (deployState.get().getActiveDeploy().isPresent()) {
lastActionTime = deployState.get().getActiveDeploy().get().getTimestamp();
}
}
if (lastActionTime == null) {
lastActionTime = 0L;
}
}
return new RequestParentWithLastActionTime(request, lastActionTime, maybeUserSettings.isPresent() && maybeUserSettings.get().getStarredRequestIds().contains(request.getRequest().getId()));
}).sorted().limit(limit.or(requests.size())).map((parentWithActionTime) -> {
SingularityRequestWithState requestWithState = parentWithActionTime.getRequestWithState();
if (includeFullRequestData) {
CompletableFuture<Optional<SingularityTaskIdsByStatus>> maybeTaskIdsByStatus = CompletableFuture.supplyAsync(() -> getTaskIdsByStatusForRequest(requestWithState)).exceptionally((throwable) -> Optional.absent());
CompletableFuture<Optional<SingularityExpiringBounce>> maybeExpiringBounce = CompletableFuture.supplyAsync(() -> requestManager.getExpiringBounce(requestWithState.getRequest().getId())).exceptionally((throwable) -> Optional.absent());
CompletableFuture<Optional<SingularityExpiringPause>> maybeExpiringPause = CompletableFuture.supplyAsync(() -> requestManager.getExpiringPause(requestWithState.getRequest().getId())).exceptionally((throwable) -> Optional.absent());
CompletableFuture<Optional<SingularityExpiringScale>> maybeExpiringScale = CompletableFuture.supplyAsync(() -> requestManager.getExpiringScale(requestWithState.getRequest().getId())).exceptionally((throwable) -> Optional.absent());
CompletableFuture<Optional<SingularityExpiringSkipHealthchecks>> maybeExpiringSkipHealthchecks = CompletableFuture.supplyAsync(() -> requestManager.getExpiringSkipHealthchecks(requestWithState.getRequest().getId())).exceptionally((throwable) -> Optional.absent());
return new SingularityRequestParent(requestWithState.getRequest(), requestWithState.getState(), Optional.fromNullable(deployStates.get(requestWithState.getRequest().getId())), // full deploy data not provided
Optional.absent(), // full deploy data not provided
Optional.absent(), // full deploy data not provided
Optional.absent(), maybeExpiringBounce.join(), maybeExpiringPause.join(), maybeExpiringScale.join(), maybeExpiringSkipHealthchecks.join(), maybeTaskIdsByStatus.join(), requestIdToLastHistory.getOrDefault(requestWithState.getRequest().getId(), Optional.absent()), mostRecentTasks.computeIfAbsent(requestWithState.getRequest().getId(), (id) -> getMostRecentTask(requestWithState.getRequest())));
} else {
return new SingularityRequestParent(requestWithState.getRequest(), requestWithState.getState(), Optional.fromNullable(deployStates.get(requestWithState.getRequest().getId())), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent());
}
}).collect(Collectors.toList());
}
use of com.hubspot.singularity.SingularityRequestWithState in project Singularity by HubSpot.
the class DeployResource method updatePendingDeploy.
@POST
@Path("/update")
@ApiOperation(value = "Update the target active instance count for a pending deploy", response = SingularityRequestParent.class)
@ApiResponses({ @ApiResponse(code = 400, message = "Deploy is not in the pending state pending or is not not present") })
public SingularityRequestParent updatePendingDeploy(@Auth SingularityUser user, @ApiParam(required = true) SingularityUpdatePendingDeployRequest updateRequest) {
SingularityRequestWithState requestWithState = fetchRequestWithState(updateRequest.getRequestId(), user);
authorizationHelper.checkForAuthorization(requestWithState.getRequest(), user, SingularityAuthorizationScope.WRITE);
Optional<SingularityRequestDeployState> deployState = deployManager.getRequestDeployState(requestWithState.getRequest().getId());
checkBadRequest(deployState.isPresent() && deployState.get().getPendingDeploy().isPresent() && deployState.get().getPendingDeploy().get().getDeployId().equals(updateRequest.getDeployId()), "Request %s does not have a pending deploy %s", updateRequest.getRequestId(), updateRequest.getDeployId());
checkBadRequest(updateRequest.getTargetActiveInstances() > 0 && updateRequest.getTargetActiveInstances() <= requestWithState.getRequest().getInstancesSafe(), "Cannot update pending deploy to have more instances (%s) than instances set for request (%s), or less than 1 instance", updateRequest.getTargetActiveInstances(), requestWithState.getRequest().getInstancesSafe());
deployManager.createUpdatePendingDeployRequest(updateRequest);
return fillEntireRequest(requestWithState);
}
Aggregations