Search in sources :

Example 11 with SingularityRequestDeployState

use of com.hubspot.singularity.SingularityRequestDeployState 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());
}
Also used : DeployManager(com.hubspot.singularity.data.DeployManager) Inject(com.google.inject.Inject) SingularityRequestCleanup(com.hubspot.singularity.SingularityRequestCleanup) RequestManager(com.hubspot.singularity.data.RequestManager) LoggerFactory(org.slf4j.LoggerFactory) SingularityUserSettings(com.hubspot.singularity.SingularityUserSettings) SingularityExpiringScale(com.hubspot.singularity.expiring.SingularityExpiringScale) SingularityBounceRequest(com.hubspot.singularity.api.SingularityBounceRequest) RequestHistoryType(com.hubspot.singularity.SingularityRequestHistory.RequestHistoryType) SingularityExpiringBounce(com.hubspot.singularity.expiring.SingularityExpiringBounce) Optional(com.google.common.base.Optional) Map(java.util.Map) SingularityValidator(com.hubspot.singularity.data.SingularityValidator) TaskManager(com.hubspot.singularity.data.TaskManager) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) UserManager(com.hubspot.singularity.data.UserManager) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SingularityExpiringSkipHealthchecks(com.hubspot.singularity.expiring.SingularityExpiringSkipHealthchecks) UUID(java.util.UUID) SingularityExpiringPause(com.hubspot.singularity.expiring.SingularityExpiringPause) Collectors(java.util.stream.Collectors) RequestState(com.hubspot.singularity.RequestState) PendingType(com.hubspot.singularity.SingularityPendingRequest.PendingType) List(java.util.List) JavaUtils(com.hubspot.mesos.JavaUtils) TaskHistoryHelper(com.hubspot.singularity.data.history.TaskHistoryHelper) RequestCleanupType(com.hubspot.singularity.RequestCleanupType) Singleton(com.google.inject.Singleton) SingularityCreateResult(com.hubspot.singularity.SingularityCreateResult) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityTaskHistoryQuery(com.hubspot.singularity.SingularityTaskHistoryQuery) SingularityRequest(com.hubspot.singularity.SingularityRequest) SingularityTaskHistoryUpdate(com.hubspot.singularity.SingularityTaskHistoryUpdate) SingularityRequestHistory(com.hubspot.singularity.SingularityRequestHistory) SingularityTaskIdHistory(com.hubspot.singularity.SingularityTaskIdHistory) SingularityUser(com.hubspot.singularity.SingularityUser) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) CompletableFuture(java.util.concurrent.CompletableFuture) SingularityTaskIdsByStatus(com.hubspot.singularity.SingularityTaskIdsByStatus) ArrayList(java.util.ArrayList) RequestType(com.hubspot.singularity.RequestType) SingularityDeployHealthHelper(com.hubspot.singularity.scheduler.SingularityDeployHealthHelper) Logger(org.slf4j.Logger) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) SingularityPendingDeploy(com.hubspot.singularity.SingularityPendingDeploy) SingularityMailer(com.hubspot.singularity.smtp.SingularityMailer) SingularityRequestDeployState(com.hubspot.singularity.SingularityRequestDeployState) SingularityRequestParent(com.hubspot.singularity.SingularityRequestParent) Collections(java.util.Collections) Optional(com.google.common.base.Optional) SingularityRequestDeployState(com.hubspot.singularity.SingularityRequestDeployState) SingularityExpiringScale(com.hubspot.singularity.expiring.SingularityExpiringScale) SingularityRequestParent(com.hubspot.singularity.SingularityRequestParent) SingularityUserSettings(com.hubspot.singularity.SingularityUserSettings) CompletableFuture(java.util.concurrent.CompletableFuture) SingularityExpiringSkipHealthchecks(com.hubspot.singularity.expiring.SingularityExpiringSkipHealthchecks) SingularityExpiringPause(com.hubspot.singularity.expiring.SingularityExpiringPause) SingularityExpiringBounce(com.hubspot.singularity.expiring.SingularityExpiringBounce) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SingularityTaskIdsByStatus(com.hubspot.singularity.SingularityTaskIdsByStatus)

Example 12 with SingularityRequestDeployState

use of com.hubspot.singularity.SingularityRequestDeployState 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);
}
Also used : SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityRequestDeployState(com.hubspot.singularity.SingularityRequestDeployState) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ApiResponses(com.wordnik.swagger.annotations.ApiResponses)

Example 13 with SingularityRequestDeployState

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

the class DeployResource method cancelDeploy.

@DELETE
@Path("/deploy/{deployId}/request/{requestId}")
@ApiOperation(value = "Cancel a pending deployment (best effort - the deploy may still succeed or fail)", response = SingularityRequestParent.class)
@ApiResponses({ @ApiResponse(code = 400, message = "Deploy is not in the pending state pending or is not not present") })
public SingularityRequestParent cancelDeploy(@Auth SingularityUser user, @ApiParam(required = true, value = "The Singularity Request Id from which the deployment is removed.") @PathParam("requestId") String requestId, @ApiParam(required = true, value = "The Singularity Deploy Id that should be removed.") @PathParam("deployId") String deployId) {
    SingularityRequestWithState requestWithState = fetchRequestWithState(requestId, user);
    authorizationHelper.checkForAuthorization(requestWithState.getRequest(), user, SingularityAuthorizationScope.WRITE);
    validator.checkActionEnabled(SingularityAction.CANCEL_DEPLOY);
    Optional<SingularityRequestDeployState> deployState = deployManager.getRequestDeployState(requestWithState.getRequest().getId());
    checkBadRequest(deployState.isPresent() && deployState.get().getPendingDeploy().isPresent() && deployState.get().getPendingDeploy().get().getDeployId().equals(deployId), "Request %s does not have a pending deploy %s", requestId, deployId);
    deployManager.createCancelDeployRequest(new SingularityDeployMarker(requestId, deployId, System.currentTimeMillis(), user.getEmail(), Optional.<String>absent()));
    return fillEntireRequest(requestWithState);
}
Also used : SingularityDeployMarker(com.hubspot.singularity.SingularityDeployMarker) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityRequestDeployState(com.hubspot.singularity.SingularityRequestDeployState) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ApiResponses(com.wordnik.swagger.annotations.ApiResponses)

Example 14 with SingularityRequestDeployState

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

the class SingularityStartup method checkActiveRequest.

private void checkActiveRequest(SingularityRequestWithState requestWithState, Map<SingularityDeployKey, SingularityPendingTaskId> deployKeyToPendingTaskId, final long timestamp) {
    final SingularityRequest request = requestWithState.getRequest();
    if (request.getRequestType() == RequestType.ON_DEMAND || request.getRequestType() == RequestType.RUN_ONCE) {
        // There's no situation where we'd want to schedule an On Demand or Run Once request at startup, so don't even bother with them.
        return;
    }
    Optional<SingularityRequestDeployState> requestDeployState = deployManager.getRequestDeployState(request.getId());
    if (!requestDeployState.isPresent() || !requestDeployState.get().getActiveDeploy().isPresent()) {
        LOG.debug("No active deploy for {} - not scheduling on startup", request.getId());
        return;
    }
    final String activeDeployId = requestDeployState.get().getActiveDeploy().get().getDeployId();
    if (request.isScheduled()) {
        SingularityDeployKey deployKey = new SingularityDeployKey(request.getId(), activeDeployId);
        SingularityPendingTaskId pendingTaskId = deployKeyToPendingTaskId.get(deployKey);
        if (pendingTaskId != null && pendingTaskId.getCreatedAt() >= requestWithState.getTimestamp()) {
            LOG.info("Not rescheduling {} because {} is newer than {}", request.getId(), pendingTaskId, requestWithState.getTimestamp());
            return;
        }
    }
    requestManager.addToPendingQueue(new SingularityPendingRequest(request.getId(), activeDeployId, timestamp, Optional.<String>absent(), PendingType.STARTUP, Optional.<Boolean>absent(), Optional.<String>absent()));
}
Also used : SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) SingularityRequest(com.hubspot.singularity.SingularityRequest) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityRequestDeployState(com.hubspot.singularity.SingularityRequestDeployState)

Aggregations

SingularityRequestDeployState (com.hubspot.singularity.SingularityRequestDeployState)14 SingularityRequest (com.hubspot.singularity.SingularityRequest)6 SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)6 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)5 SingularityDeployKey (com.hubspot.singularity.SingularityDeployKey)4 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)4 ArrayList (java.util.ArrayList)4 RequestState (com.hubspot.singularity.RequestState)3 SingularityDeployMarker (com.hubspot.singularity.SingularityDeployMarker)3 SingularityPendingDeploy (com.hubspot.singularity.SingularityPendingDeploy)3 PendingType (com.hubspot.singularity.SingularityPendingRequest.PendingType)3 SingularityPendingTaskId (com.hubspot.singularity.SingularityPendingTaskId)3 SingularityCreateResult (com.hubspot.singularity.SingularityCreateResult)2 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)2 SingularityDeployResult (com.hubspot.singularity.SingularityDeployResult)2 SingularityDeployStatistics (com.hubspot.singularity.SingularityDeployStatistics)2 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)2 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)2 Path (javax.ws.rs.Path)2 Optional (com.google.common.base.Optional)1