public Optional<String> getInUseDeployId(String requestId) { Optional<SingularityRequestDeployState> deployState = getRequestDeployState(requestId); if (!deployState.isPresent() || !deployState.get().getActiveDeploy().isPresent() && !deployState.get().getPendingDeploy().isPresent()) { return Optional.absent(); } return Optional.of(deployState.get().getActiveDeploy().or(deployState.get().getPendingDeploy()).get().getDeployId()); }
private List<String> getUnderProvisionedRequestIds(Set<String> possiblyUnderProvisionedRequestIds) { final List<String> underProvisionedRequestIds = new ArrayList<>(possiblyUnderProvisionedRequestIds.size()); if (!possiblyUnderProvisionedRequestIds.isEmpty()) { Map<String, SingularityRequestDeployState> deployStates = deployManager.getRequestDeployStatesByRequestIds(possiblyUnderProvisionedRequestIds); for (SingularityRequestDeployState deployState : deployStates.values()) { if (deployState.getActiveDeploy().isPresent() || deployState.getPendingDeploy().isPresent()) { underProvisionedRequestIds.add(deployState.getRequestId()); } } } return underProvisionedRequestIds; }
private boolean userAssociatedWithDeploy(Optional<SingularityRequestDeployState> deployState, SingularityUser user) { return deployState.isPresent() && (deployState.get().getPendingDeploy().isPresent() && userMatches(deployState.get().getPendingDeploy().get().getUser(), user) || deployState.get().getActiveDeploy().isPresent() && userMatches(deployState.get().getActiveDeploy().get().getUser(), user)); }
private boolean shouldTransferDeploy(SingularityRequestDeployState deployState, SingularityDeployKey deployKey) { if (deployState == null) { LOG.warn("Missing request deploy state for deployKey {}", deployKey); return true; } if (deployState.getActiveDeploy().isPresent() && deployState.getActiveDeploy().get().getDeployId().equals(deployKey.getDeployId())) { return false; } if (deployState.getPendingDeploy().isPresent() && deployState.getPendingDeploy().get().getDeployId().equals(deployKey.getDeployId())) { return false; } return true; }
private boolean isDeployInUse(Optional<SingularityRequestDeployState> requestDeployState, String deployId, boolean mustMatchActiveDeploy) { if (!requestDeployState.isPresent()) { return false; } if (matchesDeployMarker(requestDeployState.get().getActiveDeploy(), deployId)) { return true; } if (mustMatchActiveDeploy) { return false; } return matchesDeployMarker(requestDeployState.get().getPendingDeploy(), deployId); }
private boolean saveNewDeployState(SingularityDeployMarker pendingDeployMarker, Optional<SingularityDeployMarker> newActiveDeploy) { Optional<SingularityRequestDeployState> deployState = deployManager.getRequestDeployState(pendingDeployMarker.getRequestId()); if (!deployState.isPresent()) { LOG.error("Expected deploy state for deploy marker: {} but didn't find it", pendingDeployMarker); return false; } deployManager.saveNewRequestDeployState(new SingularityRequestDeployState(deployState.get().getRequestId(), newActiveDeploy.or(deployState.get().getActiveDeploy()), Optional.absent())); return true; }
private SingularityRequestDeployHolder getDeployHolder(String requestId) { Optional<SingularityRequestDeployState> requestDeployState = deployManager.getRequestDeployState(requestId); Optional<SingularityDeploy> activeDeploy = Optional.absent(); Optional<SingularityDeploy> pendingDeploy = Optional.absent(); if (requestDeployState.isPresent()) { if (requestDeployState.get().getActiveDeploy().isPresent()) { activeDeploy = deployManager.getDeploy(requestId, requestDeployState.get().getActiveDeploy().get().getDeployId()); } if (requestDeployState.get().getPendingDeploy().isPresent()) { pendingDeploy = deployManager.getDeploy(requestId, requestDeployState.get().getPendingDeploy().get().getDeployId()); } } return new SingularityRequestDeployHolder(activeDeploy, pendingDeploy); }
private long getLastActionTimeForRequest(SingularityRequest request, Optional<SingularityRequestHistory> lastHistory, Optional<SingularityRequestDeployState> deployState, Optional<SingularityTaskIdHistory> mostRecentTask) { long lastUpdate = 0; if (lastHistory.isPresent()) { lastUpdate = lastHistory.get().getCreatedAt(); } if (deployState.isPresent()) { if (deployState.get().getActiveDeploy().isPresent()) { lastUpdate = Math.max(lastUpdate, deployState.get().getActiveDeploy().get().getTimestamp()); } if (deployState.get().getPendingDeploy().isPresent()) { lastUpdate = Math.max(lastUpdate, deployState.get().getPendingDeploy().get().getTimestamp()); } } // Only consider most recent task time for non-long-running if (mostRecentTask.isPresent() && !request.getRequestType().isLongRunning()) { lastUpdate = Math.max(lastUpdate, mostRecentTask.get().getUpdatedAt()); } return lastUpdate; } }
private boolean shouldExitCooldown(SingularityRequestWithState cooldownRequest) { Optional<SingularityRequestDeployState> maybeDeployState = deployManager.getRequestDeployState(cooldownRequest.getRequest().getId()); if (!maybeDeployState.isPresent() || !maybeDeployState.get().getActiveDeploy().isPresent()) { LOG.trace("{} had no deployState / activeDeploy {}, exiting cooldown", cooldownRequest.getRequest().getId(), maybeDeployState); return true; } Optional<SingularityDeployStatistics> maybeDeployStatistics = deployManager.getDeployStatistics(cooldownRequest.getRequest().getId(), maybeDeployState.get().getActiveDeploy().get().getDeployId()); if (!maybeDeployStatistics.isPresent()) { LOG.trace("{} had no deploy statistics, exiting cooldown", new SingularityDeployKey(cooldownRequest.getRequest().getId(), maybeDeployState.get().getActiveDeploy().get().getDeployId())); return true; } Optional<Long> lastFinishAt = maybeDeployStatistics.get().getLastFinishAt(); if (!lastFinishAt.isPresent()) { LOG.trace("{} had no last finish, exiting cooldown", new SingularityDeployKey(cooldownRequest.getRequest().getId(), maybeDeployState.get().getActiveDeploy().get().getDeployId())); return true; } if (cooldown.hasCooldownExpired(cooldownRequest.getRequest(), maybeDeployStatistics.get(), Optional.<Integer> absent(), Optional.<Long> absent())) { return true; } return false; }
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) { return; // 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. } 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())); }
public SingularityCreateResult saveDeploy(SingularityRequest request, SingularityDeployMarker deployMarker, SingularityDeploy deploy) { final SingularityCreateResult deploySaveResult = create(getDeployDataPath(deploy.getRequestId(), deploy.getId()), deploy, deployTranscoder); if (deploySaveResult == SingularityCreateResult.EXISTED) { LOG.info("Deploy object for {} already existed (new marker: {})", deploy, deployMarker); } singularityEventListener.deployHistoryEvent(new SingularityDeployUpdate(deployMarker, Optional.of(deploy), DeployEventType.STARTING, Optional.<SingularityDeployResult>absent())); create(getDeployMarkerPath(deploy.getRequestId(), deploy.getId()), deployMarker, deployMarkerTranscoder); final Optional<SingularityRequestDeployState> currentState = getRequestDeployState(deploy.getRequestId()); Optional<SingularityDeployMarker> activeDeploy = Optional.absent(); Optional<SingularityDeployMarker> pendingDeploy = Optional.absent(); if (request.isDeployable()) { if (currentState.isPresent()) { activeDeploy = currentState.get().getActiveDeploy(); } pendingDeploy = Optional.of(deployMarker); } else { activeDeploy = Optional.of(deployMarker); } final SingularityRequestDeployState newState = new SingularityRequestDeployState(deploy.getRequestId(), activeDeploy, pendingDeploy); return saveNewRequestDeployState(newState); }
protected SingularityRequestParent fillEntireRequest(SingularityRequestWithState requestWithState, Optional<SingularityRequest> newRequestData) { final String requestId = requestWithState.getRequest().getId(); final Optional<SingularityRequestDeployState> requestDeployState = deployManager.getRequestDeployState(requestId); Optional<SingularityDeploy> activeDeploy = Optional.absent(); Optional<SingularityDeploy> pendingDeploy = Optional.absent(); if (requestDeployState.isPresent()) { activeDeploy = fillDeploy(requestDeployState.get().getActiveDeploy()); pendingDeploy = fillDeploy(requestDeployState.get().getPendingDeploy()); } Optional<SingularityPendingDeploy> pendingDeployState = deployManager.getPendingDeploy(requestId); return new SingularityRequestParent(newRequestData.or(requestWithState.getRequest()), requestWithState.getState(), requestDeployState, activeDeploy, pendingDeploy, pendingDeployState, requestManager.getExpiringBounce(requestId), requestManager.getExpiringPause(requestId), requestManager.getExpiringScale(requestId), requestManager.getExpiringSkipHealthchecks(requestId), requestHelper.getTaskIdsByStatusForRequest(requestId), requestHistoryHelper.getLastHistory(requestId), requestHelper.getMostRecentTask(requestWithState.getRequest())); }
lastActionTime = deployState.get().getPendingDeploy().get().getTimestamp(); if (deployState.get().getActiveDeploy().isPresent()) { lastActionTime = deployState.get().getActiveDeploy().get().getTimestamp();
SingularityPendingRequest pendingRequest = new SingularityPendingRequest(request.getId(), requestDeployState.get().getActiveDeploy().get().getDeployId(), System.currentTimeMillis(), Optional.absent(), pendingType, cmdLineArgsList, Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), resources, Collections.emptyList(), Optional.absent(), Collections.emptyMap(), Collections.emptyList(), Optional.absent());
private boolean matchesDeploy(SingularityRequestDeployState requestDeployState, SingularityTaskRequest taskRequest) { if (requestDeployState == null) { return false; } return matchesDeployMarker(requestDeployState.getActiveDeploy(), taskRequest.getDeploy().getId()) || matchesDeployMarker(requestDeployState.getPendingDeploy(), taskRequest.getDeploy().getId()); }
if (maybeDeployState.isPresent() && maybeDeployState.get().getActiveDeploy().isPresent()) { List<SingularityTaskId> remainingActiveTasks = new ArrayList<>(); taskManager.getActiveTaskIdsForDeploy(request.getId(), maybeDeployState.get().getActiveDeploy().get().getDeployId()).forEach((taskId) -> { if (taskId.getInstanceNo() > newInstances) { taskManager.createTaskCleanup(new SingularityTaskCleanup(
if (!deployState.isPresent() || !deployState.get().getActiveDeploy().isPresent()) { LOG.debug("Killing a task {} immediately because there is no active deploy state {}", taskCleanup, deployState); return true; final String activeDeployId = deployState.get().getActiveDeploy().get().getDeployId(); final String matchingTasksDeployId = taskCleanup.getCleanupType() == TaskCleanupType.INCREMENTAL_DEPLOY_CANCELLED || taskCleanup.getCleanupType() == TaskCleanupType.INCREMENTAL_DEPLOY_FAILED ? activeDeployId : taskCleanup.getTaskId().getDeployId();
@Test public void testRunOnceRunOnlyOnce() { SingularityRequestBuilder bldr = new SingularityRequestBuilder(requestId, RequestType.RUN_ONCE); request = bldr.build(); saveRequest(request); deployResource.deploy(new SingularityDeployRequest(new SingularityDeployBuilder(requestId, "d1").setCommand(Optional.of("cmd")).build(), Optional.absent(), Optional.absent()), singularityUser); scheduler.drainPendingQueue(); deployChecker.checkDeploys(); resourceOffers(); Assert.assertTrue(deployManager.getRequestDeployState(requestId).get().getActiveDeploy().isPresent()); Assert.assertTrue(!deployManager.getRequestDeployState(requestId).get().getPendingDeploy().isPresent()); Assert.assertEquals(1, taskManager.getActiveTaskIds().size()); statusUpdate(taskManager.getActiveTasks().get(0), TaskState.TASK_LOST); resourceOffers(); Assert.assertTrue(taskManager.getActiveTaskIds().isEmpty()); deployResource.deploy(new SingularityDeployRequest(new SingularityDeployBuilder(requestId, "d2").setCommand(Optional.of("cmd")).build(), Optional.absent(), Optional.absent()), singularityUser); scheduler.drainPendingQueue(); deployChecker.checkDeploys(); resourceOffers(); Assert.assertTrue(deployManager.getRequestDeployState(requestId).get().getActiveDeploy().isPresent()); Assert.assertTrue(!deployManager.getRequestDeployState(requestId).get().getPendingDeploy().isPresent()); Assert.assertEquals(1, taskManager.getActiveTaskIds().size()); statusUpdate(taskManager.getActiveTasks().get(0), TaskState.TASK_FINISHED); resourceOffers(); Assert.assertTrue(taskManager.getActiveTaskIds().isEmpty()); }
Optional<SingularityRequestDeployState> maybeRequestDeployState = deployManager.getRequestDeployState(request.getId()); if (maybeRequestDeployState.isPresent() && maybeRequestDeployState.get().getActiveDeploy().isPresent() && !(requestWithState.getState() == RequestState.PAUSED || requestWithState.getState() == RequestState.DEPLOYING_TO_UNPAUSE)) { requestManager.addToPendingQueue(new SingularityPendingRequest( request.getId(), maybeRequestDeployState.get().getActiveDeploy().get().getDeployId(), deployResult.getTimestamp(), pendingDeploy.getDeployMarker().getUser(),
public Optional<String> getInUseDeployId(String requestId) { Optional<SingularityRequestDeployState> deployState = getRequestDeployState(requestId); if (!deployState.isPresent() || !deployState.get().getActiveDeploy().isPresent() && !deployState.get().getPendingDeploy().isPresent()) { return Optional.absent(); } return Optional.of(deployState.get().getActiveDeploy().or(deployState.get().getPendingDeploy()).get().getDeployId()); }