public static void checkBadRequest(boolean condition, String message, Object... args) { if (!condition) { badRequest(message, args); } }
private Integer getLimitCount(Integer countParam) { if (countParam == null) { return 100; } checkBadRequest(countParam >= 0, "count param must be non-zero"); if (countParam > 1000) { return 1000; } return countParam; }
public void validateExpiringMachineStateChange(Optional<SingularityMachineChangeRequest> maybeChangeRequest, MachineState currentState, Optional<SingularityExpiringMachineState> currentExpiringObject) { if (!maybeChangeRequest.isPresent() || !maybeChangeRequest.get().getDurationMillis().isPresent()) { return; } SingularityMachineChangeRequest changeRequest = maybeChangeRequest.get(); checkBadRequest(changeRequest.getRevertToState().isPresent(), "Must include a machine state to revert to for an expiring machine state change"); MachineState newState = changeRequest.getRevertToState().get(); checkConflict(!currentExpiringObject.isPresent(), "A current expiring object already exists, delete it first"); checkBadRequest(!(newState == MachineState.STARTING_DECOMMISSION && currentState.isDecommissioning()), "Cannot start decommission when it has already been started"); checkBadRequest(!(((newState == MachineState.DECOMMISSIONING) || (newState == MachineState.DECOMMISSIONED)) && (currentState == MachineState.FROZEN)), "Cannot transition from FROZEN to DECOMMISSIONING or DECOMMISSIONED"); checkBadRequest(!(((newState == MachineState.DECOMMISSIONING) || (newState == MachineState.DECOMMISSIONED)) && (currentState == MachineState.ACTIVE)), "Cannot transition from ACTIVE to DECOMMISSIONING or DECOMMISSIONED"); checkBadRequest(!(newState == MachineState.FROZEN && currentState.isDecommissioning()), "Cannot transition from a decommissioning state to FROZEN"); List<MachineState> systemOnlyStateTransitions = ImmutableList.of(MachineState.DEAD, MachineState.MISSING_ON_STARTUP, MachineState.DECOMMISSIONING); checkBadRequest(!systemOnlyStateTransitions.contains(newState), "States {} are reserved for system usage, you cannot manually transition to {}", systemOnlyStateTransitions, newState); checkBadRequest(!(newState == MachineState.DECOMMISSIONED && !changeRequest.isKillTasksOnDecommissionTimeout()), "Must specify that all tasks on slave get killed if transitioning to DECOMMISSIONED state"); }
String newDayOfWeekValue = null; checkBadRequest(dayOfWeekValue >= 0 && dayOfWeekValue <= 7, "Schedule %s is invalid, day of week (%s) is not 0-7", schedule, dayOfWeekValue); break; default: badRequest("Schedule %s is invalid, day of week (%s) is not 0-7", schedule, dayOfWeekValue); break;
checkBadRequest(!Strings.isNullOrEmpty(path), "Must specify 'path'"); final Optional<MesosFileChunkObject> maybeChunk = sandboxManager.read(slaveHostname, fullPath, offset, length); checkNotFound(maybeChunk.isPresent(), "File %s does not exist for task ID %s", fullPath, taskId); throw notFound("Slave @ %s was not found, it is probably offline", slaveHostname);
if (request.isScheduled()) { pendingType = PendingType.IMMEDIATE; checkConflict(activeTasks.isEmpty(), "Cannot request immediate run of a scheduled job which is currently running (%s)", activeTasks); } else if (request.isOneOff()) { pendingType = PendingType.ONEOFF; if (request.getInstances().isPresent()) { checkRateLimited( activeTasks.size() + pendingTasks.size() < request.getInstances().get(), "No more than %s tasks allowed to run concurrently for request %s (%s active, %s pending)", throw badRequest("Can not request an immediate run of a non-scheduled / always running request (%s)", request); throw badRequest("Task launch delay can be at most %d days from now.", maxRunNowTaskLaunchDelay);
private void submitRequest(SingularityRequest request, Optional<SingularityRequestWithState> oldRequestWithState, Optional<RequestHistoryType> historyType, Optional<Boolean> skipHealthchecks, Optional<String> message, Optional<SingularityBounceRequest> maybeBounceRequest, SingularityUser user) { checkNotNullBadRequest(request.getId(), "Request must have an id"); checkConflict(!requestManager.cleanupRequestExists(request.getId()), "Request %s is currently cleaning. Try again after a few moments", request.getId()); checkConflict(!(maybePendingDeploy.isPresent() && maybePendingDeploy.get().getUpdatedRequest().isPresent()), "Request %s has a pending deploy that may change the request data. Try again when the deploy has finished", request.getId()); checkBadRequest(validator.isSpreadAllSlavesEnabled(), "You must enabled spread to all slaves in order to use the SPREAD_ALL_SLAVES request type"); int currentActiveSlaveCount = slaveManager.getNumObjectsAtState(MachineState.ACTIVE); request = request.toBuilder().setInstances(Optional.of(currentActiveSlaveCount)).build();
public void checkAdminAuthorization(SingularityUser user) { if (authEnabled) { checkForbidden(user.isAuthenticated(), "Not Authenticated!"); if (!adminGroups.isEmpty()) { checkForbidden(groupsIntersect(user.getGroups(), adminGroups), "%s must be part of one or more admin groups: %s", user.getId(), JavaUtils.COMMA_JOINER.join(adminGroups)); } } }
private void checkS3() { checkNotFound(s3Services.isS3ConfigPresent(), "S3 configuration was absent"); checkNotFound(configuration.isPresent(), "S3 configuration was absent"); }
validator.checkActionEnabled(SingularityAction.ADD_METADATA); WebExceptions.checkBadRequest(taskMetadataRequest.getTitle().length() < taskMetadataConfiguration.getMaxMetadataTitleLength(), "Task metadata title too long, must be less than %s bytes", taskMetadataConfiguration.getMaxMetadataTitleLength()); WebExceptions.checkBadRequest(!taskMetadataRequest.getMessage().isPresent() || messageLength < taskMetadataConfiguration.getMaxMetadataMessageLength(), "Task metadata message too long, must be less than %s bytes", taskMetadataConfiguration.getMaxMetadataMessageLength()); WebExceptions.checkBadRequest(taskMetadataConfiguration.getAllowedMetadataTypes().get().contains(taskMetadataRequest.getType()), "%s is not one of the allowed metadata types %s", taskMetadataRequest.getType(), taskMetadataConfiguration.getAllowedMetadataTypes().get()); WebExceptions.checkNotFound(taskManager.taskExistsInZk(taskIdObj), "Task %s not found in ZooKeeper (can not save metadata to tasks which have been persisted", taskIdObj); WebExceptions.checkConflict(result == SingularityCreateResult.CREATED, "Task metadata conficted with existing metadata for %s at %s", taskMetadata.getType(), taskMetadata.getTimestamp());
private String getAndCheckDeployId(String requestId) { Optional<String> maybeDeployId = deployManager.getInUseDeployId(requestId); checkConflict(maybeDeployId.isPresent(), "Can not schedule/bounce a request (%s) with no deploy", requestId); return maybeDeployId.get(); }
public SingularityRequestParent deploy(SingularityDeployRequest deployRequest, SingularityUser user) { validator.checkActionEnabled(SingularityAction.DEPLOY); SingularityDeploy deploy = deployRequest.getDeploy(); checkNotNullBadRequest(deploy, "DeployRequest must have a deploy object"); final String requestId = checkNotNullBadRequest(deploy.getRequestId(), "DeployRequest must have a non-null requestId"); checkConflict(requestWithState.getState() != RequestState.PAUSED, "Request %s is paused. Unable to deploy (it must be manually unpaused first)", requestWithState.getRequest().getId()); checkConflict(!deployAlreadyInProgress, "Pending deploy already in progress for %s - cancel it or wait for it to complete (%s)", requestId, deployManager.getPendingDeploy(requestId).orNull());
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()); } } }
public Optional<SingularityPendingTask> deleteScheduledTask(String taskId, SingularityUser user) { Optional<SingularityPendingTask> maybePendingTask = taskManager.getPendingTask(getPendingTaskIdFromStr(taskId)); if (maybePendingTask.isPresent()) { SingularityPendingTaskId pendingTaskId = maybePendingTask.get().getPendingTaskId(); Optional<SingularityRequestWithState> maybeRequest = requestManager.getRequest(pendingTaskId.getRequestId()); checkNotFound(maybeRequest.isPresent(), "Couldn't find: " + taskId); SingularityRequest request = maybeRequest.get().getRequest(); authorizationHelper.checkForAuthorizationByRequestId(request.getId(), user, SingularityAuthorizationScope.WRITE); checkBadRequest(request.getRequestType() == RequestType.ON_DEMAND, "Only ON_DEMAND tasks may be deleted."); taskManager.markPendingTaskForDeletion(pendingTaskId); } return maybePendingTask; }
public static void checkNotFound(boolean condition, String message, Object... args) { if (!condition) { notFound(message, args); } }
public static void checkConflict(boolean condition, String message, Object... args) { if (!condition) { conflict(message, args); } }
public static void checkForbidden(boolean condition, String message, Object... args) { if (!condition) { forbidden(message, args); } }
checkBadRequest(!Strings.isNullOrEmpty(path), "Must specify 'path'"); final Optional<MesosFileChunkObject> maybeChunk = sandboxManager.read(slaveHostname, fullPath, offset, length); checkNotFound(maybeChunk.isPresent(), "File %s does not exist for task ID %s", fullPath, taskId); throw notFound("Slave @ %s was not found, it is probably offline", slaveHostname);
if (request.isScheduled()) { pendingType = PendingType.IMMEDIATE; checkConflict(activeTasks.isEmpty(), "Cannot request immediate run of a scheduled job which is currently running (%s)", activeTasks); } else if (request.isOneOff()) { pendingType = PendingType.ONEOFF; if (request.getInstances().isPresent()) { checkRateLimited( activeTasks.size() + pendingTasks.size() < request.getInstances().get(), "No more than %s tasks allowed to run concurrently for request %s (%s active, %s pending)", throw badRequest("Can not request an immediate run of a non-scheduled / always running request (%s)", request); throw badRequest("Task launch delay can be at most %d days from now.", maxRunNowTaskLaunchDelay);
private void submitRequest(SingularityRequest request, Optional<SingularityRequestWithState> oldRequestWithState, Optional<RequestHistoryType> historyType, Optional<Boolean> skipHealthchecks, Optional<String> message, Optional<SingularityBounceRequest> maybeBounceRequest, SingularityUser user) { checkNotNullBadRequest(request.getId(), "Request must have an id"); checkConflict(!requestManager.cleanupRequestExists(request.getId()), "Request %s is currently cleaning. Try again after a few moments", request.getId()); checkConflict(!(maybePendingDeploy.isPresent() && maybePendingDeploy.get().getUpdatedRequest().isPresent()), "Request %s has a pending deploy that may change the request data. Try again when the deploy has finished", request.getId()); checkBadRequest(validator.isSpreadAllSlavesEnabled(), "You must enabled spread to all slaves in order to use the SPREAD_ALL_SLAVES request type"); int currentActiveSlaveCount = slaveManager.getNumObjectsAtState(MachineState.ACTIVE); request = request.toBuilder().setInstances(Optional.of(currentActiveSlaveCount)).build();