Search in sources :

Example 1 with DatabaseOperationException

use of bio.terra.stairway.exception.DatabaseOperationException in project jade-data-repo by DataBiosphere.

the class IngestDriverStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException {
    // Gather inputs
    FlightMap workingMap = context.getWorkingMap();
    String loadIdString = workingMap.get(LoadMapKeys.LOAD_ID, String.class);
    UUID loadId = UUID.fromString(loadIdString);
    GoogleBucketResource bucketResource = workingMap.get(FileMapKeys.BUCKET_INFO, GoogleBucketResource.class);
    try {
        // Check for launch orphans - these are loads in the RUNNING state that never
        // got recorded by stairway.
        checkForOrphans(context, loadId);
        // Load Loop
        while (true) {
            int podCount = kubeService.getActivePodCount();
            int concurrentFiles = configurationService.getParameterValue(ConfigEnum.LOAD_CONCURRENT_FILES);
            int scaledConcurrentFiles = podCount * concurrentFiles;
            // Get the state of active and failed loads
            LoadCandidates candidates = getLoadCandidates(context, loadId, scaledConcurrentFiles);
            int currentRunning = candidates.getRunningLoads().size();
            int candidateCount = candidates.getCandidateFiles().size();
            if (currentRunning == 0 && candidateCount == 0) {
                // Nothing doing and nothing to do
                break;
            }
            // Test for exceeding max failed loads; if so, wait for all RUNNINGs to finish
            if (candidates.getFailedLoads() > maxFailedFileLoads) {
                waitForAll(context, loadId, scaledConcurrentFiles);
                break;
            }
            // Launch new loads
            if (currentRunning < scaledConcurrentFiles) {
                // Compute how many loads to launch
                int launchCount = scaledConcurrentFiles - currentRunning;
                if (candidateCount < launchCount) {
                    launchCount = candidateCount;
                }
                launchLoads(context, launchCount, candidates.getCandidateFiles(), profileId, loadId, bucketResource);
                currentRunning += launchCount;
            }
            // Wait until some loads complete
            waitForAny(context, loadId, scaledConcurrentFiles, currentRunning);
        }
    } catch (DatabaseOperationException | StairwayExecutionException ex) {
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, ex);
    }
    return StepResult.getStepResultSuccess();
}
Also used : GoogleBucketResource(bio.terra.service.resourcemanagement.google.GoogleBucketResource) DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) LoadCandidates(bio.terra.service.load.LoadCandidates) StairwayExecutionException(bio.terra.stairway.exception.StairwayExecutionException) FlightMap(bio.terra.stairway.FlightMap) UUID(java.util.UUID) StepResult(bio.terra.stairway.StepResult)

Example 2 with DatabaseOperationException

use of bio.terra.stairway.exception.DatabaseOperationException in project terra-workspace-manager by DataBiosphere.

the class JobService method verifyUserAccess.

/**
 * Ensure the user in the user request has permission to read the workspace associated with the
 * Job ID. Throws ForbiddenException if not.
 *
 * @param jobId - ID of running job
 * @param userRequest - original user request
 */
private void verifyUserAccess(String jobId, AuthenticatedUserRequest userRequest) {
    try {
        FlightState flightState = stairwayComponent.get().getFlightState(jobId);
        FlightMap inputParameters = flightState.getInputParameters();
        UUID workspaceId = inputParameters.get(WorkspaceFlightMapKeys.WORKSPACE_ID, UUID.class);
        flightBeanBag.getWorkspaceService().validateWorkspaceAndAction(userRequest, workspaceId, SamWorkspaceAction.READ);
    } catch (DatabaseOperationException | InterruptedException ex) {
        throw new InternalStairwayException("Stairway exception looking up the job", ex);
    } catch (FlightNotFoundException ex) {
        throw new JobNotFoundException("Job not found", ex);
    }
}
Also used : FlightState(bio.terra.stairway.FlightState) FlightNotFoundException(bio.terra.stairway.exception.FlightNotFoundException) DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) JobNotFoundException(bio.terra.workspace.service.job.exception.JobNotFoundException) FlightMap(bio.terra.stairway.FlightMap) UUID(java.util.UUID) InternalStairwayException(bio.terra.workspace.service.job.exception.InternalStairwayException)

Example 3 with DatabaseOperationException

use of bio.terra.stairway.exception.DatabaseOperationException in project terra-workspace-manager by DataBiosphere.

the class AwaitCreateGcpContextFlightStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
    FlightUtils.validateRequiredEntries(context.getWorkingMap(), ControlledResourceKeys.CREATE_CLOUD_CONTEXT_FLIGHT_ID);
    final var jobId = context.getWorkingMap().get(ControlledResourceKeys.CREATE_CLOUD_CONTEXT_FLIGHT_ID, String.class);
    try {
        final FlightState subflightState = context.getStairway().waitForFlight(jobId, FLIGHT_POLL_SECONDS, FLIGHT_POLL_CYCLES);
        if (FlightStatus.SUCCESS != subflightState.getFlightStatus()) {
            // no point in retrying the await step
            return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, subflightState.getException().orElseGet(() -> new RuntimeException(String.format("Subflight had unexpected status %s. No exception for subflight found.", subflightState.getFlightStatus()))));
        }
    } catch (DatabaseOperationException | FlightWaitTimedOutException e) {
        // Retry for database issues or expired wait loop
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : FlightState(bio.terra.stairway.FlightState) DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) StepResult(bio.terra.stairway.StepResult) FlightWaitTimedOutException(bio.terra.stairway.exception.FlightWaitTimedOutException)

Example 4 with DatabaseOperationException

use of bio.terra.stairway.exception.DatabaseOperationException in project terra-workspace-manager by DataBiosphere.

the class LaunchCloneGcsBucketResourceFlightStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
    validateRequiredEntries(context.getInputParameters(), JobMapKeys.AUTH_USER_INFO.getKeyName(), ControlledResourceKeys.DESTINATION_WORKSPACE_ID);
    @Nullable final var location = context.getInputParameters().get(ControlledResourceKeys.LOCATION, String.class);
    final var userRequest = context.getInputParameters().get(JobMapKeys.AUTH_USER_INFO.getKeyName(), AuthenticatedUserRequest.class);
    final var destinationWorkspaceId = context.getInputParameters().get(ControlledResourceKeys.DESTINATION_WORKSPACE_ID, UUID.class);
    // Gather input parameters for flight. See
    // bio.terra.workspace.service.resource.controlled.ControlledResourceService#cloneGcsBucket.
    final FlightMap subflightInputParameters = new FlightMap();
    subflightInputParameters.put(ControlledResourceKeys.LOCATION, location);
    subflightInputParameters.put(ControlledResourceKeys.DESTINATION_WORKSPACE_ID, destinationWorkspaceId);
    subflightInputParameters.put(JobMapKeys.AUTH_USER_INFO.getKeyName(), userRequest);
    subflightInputParameters.put(ResourceKeys.RESOURCE, resource);
    subflightInputParameters.put(ControlledResourceKeys.CLONING_INSTRUCTIONS, resource.getCloningInstructions());
    // submit flight
    try {
        context.getStairway().submit(subflightId, CloneControlledGcsBucketResourceFlight.class, subflightInputParameters);
    } catch (DuplicateFlightIdException unused) {
        return StepResult.getStepResultSuccess();
    } catch (DatabaseOperationException | StairwayExecutionException e) {
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) StairwayExecutionException(bio.terra.stairway.exception.StairwayExecutionException) FlightMap(bio.terra.stairway.FlightMap) StepResult(bio.terra.stairway.StepResult) Nullable(javax.annotation.Nullable) DuplicateFlightIdException(bio.terra.stairway.exception.DuplicateFlightIdException)

Example 5 with DatabaseOperationException

use of bio.terra.stairway.exception.DatabaseOperationException in project terra-workspace-manager by DataBiosphere.

the class LaunchCreateReferenceResourceFlightStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
    if (CloningInstructions.COPY_REFERENCE != resource.getCloningInstructions()) {
        // Nothing to do -- don't launch flight
        return StepResult.getStepResultSuccess();
    }
    FlightUtils.validateRequiredEntries(context.getInputParameters(), ControlledResourceKeys.DESTINATION_WORKSPACE_ID, JobMapKeys.AUTH_USER_INFO.getKeyName());
    final var destinationWorkspaceId = context.getInputParameters().get(ControlledResourceKeys.DESTINATION_WORKSPACE_ID, UUID.class);
    final var userRequest = context.getInputParameters().get(JobMapKeys.AUTH_USER_INFO.getKeyName(), AuthenticatedUserRequest.class);
    final String description = String.format("Clone of Referenced Resource %s", resource.getResourceId());
    final ReferencedResource destinationResource = WorkspaceCloneUtils.buildDestinationReferencedResource(resource, destinationWorkspaceId, null, description);
    // put the destination resource in the map, because it's not communicated
    // from the flight as the response (and we need the workspace ID)
    context.getWorkingMap().put(ControlledResourceKeys.DESTINATION_REFERENCED_RESOURCE, destinationResource);
    final FlightMap subflightInputParameters = new FlightMap();
    subflightInputParameters.put(ResourceKeys.RESOURCE, destinationResource);
    subflightInputParameters.put(JobMapKeys.AUTH_USER_INFO.getKeyName(), userRequest);
    subflightInputParameters.put(WorkspaceFlightMapKeys.ResourceKeys.RESOURCE_TYPE, resource.getResourceType().name());
    try {
        context.getStairway().submit(subflightId, CreateReferenceResourceFlight.class, subflightInputParameters);
    } catch (DatabaseOperationException | StairwayExecutionException e) {
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    } catch (DuplicateFlightIdException unused) {
        // already submitted the flight - treat as success
        return StepResult.getStepResultSuccess();
    }
    FlightUtils.validateRequiredEntries(context.getWorkingMap(), ControlledResourceKeys.DESTINATION_REFERENCED_RESOURCE);
    return StepResult.getStepResultSuccess();
}
Also used : ReferencedResource(bio.terra.workspace.service.resource.referenced.cloud.gcp.ReferencedResource) DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) StairwayExecutionException(bio.terra.stairway.exception.StairwayExecutionException) FlightMap(bio.terra.stairway.FlightMap) StepResult(bio.terra.stairway.StepResult) DuplicateFlightIdException(bio.terra.stairway.exception.DuplicateFlightIdException)

Aggregations

DatabaseOperationException (bio.terra.stairway.exception.DatabaseOperationException)14 StepResult (bio.terra.stairway.StepResult)12 FlightMap (bio.terra.stairway.FlightMap)11 FlightState (bio.terra.stairway.FlightState)8 FlightWaitTimedOutException (bio.terra.stairway.exception.FlightWaitTimedOutException)6 StairwayExecutionException (bio.terra.stairway.exception.StairwayExecutionException)5 DuplicateFlightIdException (bio.terra.stairway.exception.DuplicateFlightIdException)4 Map (java.util.Map)4 FlightNotFoundException (bio.terra.stairway.exception.FlightNotFoundException)3 WsmCloneResourceResult (bio.terra.workspace.service.workspace.model.WsmCloneResourceResult)3 WsmResourceCloneDetails (bio.terra.workspace.service.workspace.model.WsmResourceCloneDetails)3 HashMap (java.util.HashMap)3 UUID (java.util.UUID)2 Nullable (javax.annotation.Nullable)2 InternalStairwayException (bio.terra.service.job.exception.InternalStairwayException)1 JobNotFoundException (bio.terra.service.job.exception.JobNotFoundException)1 JobServiceShutdownException (bio.terra.service.job.exception.JobServiceShutdownException)1 JobUnauthorizedException (bio.terra.service.job.exception.JobUnauthorizedException)1 LoadCandidates (bio.terra.service.load.LoadCandidates)1 GoogleBucketResource (bio.terra.service.resourcemanagement.google.GoogleBucketResource)1