Search in sources :

Example 6 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 7 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)

Example 8 with DatabaseOperationException

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

the class AwaitCloneAllResourcesFlightStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
    validateRequiredEntries(context.getInputParameters(), ControlledResourceKeys.SOURCE_WORKSPACE_ID, JobMapKeys.REQUEST.getKeyName());
    validateRequiredEntries(context.getWorkingMap(), ControlledResourceKeys.CLONE_ALL_RESOURCES_FLIGHT_ID);
    final var cloneAllResourcesFlightId = context.getWorkingMap().get(ControlledResourceKeys.CLONE_ALL_RESOURCES_FLIGHT_ID, String.class);
    final var destinationWorkspace = context.getInputParameters().get(JobMapKeys.REQUEST.getKeyName(), Workspace.class);
    try {
        // noinspection deprecation
        final FlightState subflightState = context.getStairway().waitForFlight(cloneAllResourcesFlightId, 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()))));
        }
        final FlightMap subflightResultMap = FlightUtils.getResultMapRequired(subflightState);
        // Build the response object from the resource ID to details map. The map won't have been
        // instantiated if there are no resources in the workspace, so just use an empty map in that
        // case.
        final var resourceIdToDetails = Optional.ofNullable(subflightResultMap.get(ControlledResourceKeys.RESOURCE_ID_TO_CLONE_RESULT, new TypeReference<Map<UUID, WsmResourceCloneDetails>>() {
        })).orElse(Collections.emptyMap());
        final var apiClonedWorkspace = new ApiClonedWorkspace();
        apiClonedWorkspace.setDestinationWorkspaceId(destinationWorkspace.getWorkspaceId());
        final var sourceWorkspaceId = context.getInputParameters().get(ControlledResourceKeys.SOURCE_WORKSPACE_ID, UUID.class);
        apiClonedWorkspace.setSourceWorkspaceId(sourceWorkspaceId);
        final List<ApiResourceCloneDetails> resources = resourceIdToDetails.values().stream().map(WsmResourceCloneDetails::toApiModel).collect(Collectors.toList());
        apiClonedWorkspace.setResources(resources);
        // Set overall response for workspace clone flights
        FlightUtils.setResponse(context, apiClonedWorkspace, HttpStatus.OK);
    } 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 : ApiResourceCloneDetails(bio.terra.workspace.generated.model.ApiResourceCloneDetails) FlightWaitTimedOutException(bio.terra.stairway.exception.FlightWaitTimedOutException) FlightState(bio.terra.stairway.FlightState) ApiClonedWorkspace(bio.terra.workspace.generated.model.ApiClonedWorkspace) DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) FlightMap(bio.terra.stairway.FlightMap) StepResult(bio.terra.stairway.StepResult) Map(java.util.Map) FlightMap(bio.terra.stairway.FlightMap)

Example 9 with DatabaseOperationException

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

the class AwaitCloneGcsBucketResourceFlightStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
    // wait for the flight
    try {
        final FlightState subflightState = context.getStairway().waitForFlight(subflightId, FLIGHT_POLL_SECONDS, FLIGHT_POLL_CYCLES);
        final FlightStatus subflightStatus = subflightState.getFlightStatus();
        final WsmCloneResourceResult cloneResult = WorkspaceCloneUtils.flightStatusToCloneResult(subflightStatus, resource);
        final var cloneDetails = new WsmResourceCloneDetails();
        cloneDetails.setResult(cloneResult);
        final FlightMap resultMap = FlightUtils.getResultMapRequired(subflightState);
        final var clonedBucket = resultMap.get(JobMapKeys.RESPONSE.getKeyName(), ApiClonedControlledGcpGcsBucket.class);
        cloneDetails.setStewardshipType(StewardshipType.CONTROLLED);
        cloneDetails.setResourceType(WsmResourceType.CONTROLLED_GCP_GCS_BUCKET);
        cloneDetails.setCloningInstructions(resource.getCloningInstructions());
        cloneDetails.setSourceResourceId(resource.getResourceId());
        cloneDetails.setDestinationResourceId(Optional.ofNullable(clonedBucket).map(ApiClonedControlledGcpGcsBucket::getBucket).map(ApiCreatedControlledGcpGcsBucket::getResourceId).orElse(null));
        cloneDetails.setErrorMessage(FlightUtils.getFlightErrorMessage(subflightState));
        cloneDetails.setName(resource.getName());
        cloneDetails.setDescription(resource.getDescription());
        // add to the map
        final var resourceIdToResult = Optional.ofNullable(context.getWorkingMap().get(ControlledResourceKeys.RESOURCE_ID_TO_CLONE_RESULT, new TypeReference<Map<UUID, WsmResourceCloneDetails>>() {
        })).orElseGet(HashMap::new);
        resourceIdToResult.put(resource.getResourceId(), cloneDetails);
        context.getWorkingMap().put(ControlledResourceKeys.RESOURCE_ID_TO_CLONE_RESULT, resourceIdToResult);
    } catch (DatabaseOperationException | FlightWaitTimedOutException e) {
        // Retry for database issues or expired wait loop
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    validateRequiredEntries(context.getWorkingMap(), ControlledResourceKeys.RESOURCE_ID_TO_CLONE_RESULT);
    return StepResult.getStepResultSuccess();
}
Also used : WsmResourceCloneDetails(bio.terra.workspace.service.workspace.model.WsmResourceCloneDetails) WsmCloneResourceResult(bio.terra.workspace.service.workspace.model.WsmCloneResourceResult) HashMap(java.util.HashMap) FlightWaitTimedOutException(bio.terra.stairway.exception.FlightWaitTimedOutException) ApiCreatedControlledGcpGcsBucket(bio.terra.workspace.generated.model.ApiCreatedControlledGcpGcsBucket) FlightState(bio.terra.stairway.FlightState) DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) FlightStatus(bio.terra.stairway.FlightStatus) FlightMap(bio.terra.stairway.FlightMap) StepResult(bio.terra.stairway.StepResult) HashMap(java.util.HashMap) Map(java.util.Map) FlightMap(bio.terra.stairway.FlightMap)

Example 10 with DatabaseOperationException

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

the class LaunchCreateGcpContextFlightStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
    validateRequiredEntries(context.getInputParameters(), ControlledResourceKeys.SOURCE_WORKSPACE_ID, JobMapKeys.AUTH_USER_INFO.getKeyName(), JobMapKeys.REQUEST.getKeyName());
    validateRequiredEntries(context.getWorkingMap(), ControlledResourceKeys.CREATE_CLOUD_CONTEXT_FLIGHT_ID);
    final var userRequest = context.getInputParameters().get(JobMapKeys.AUTH_USER_INFO.getKeyName(), AuthenticatedUserRequest.class);
    final var destinationWorkspace = context.getInputParameters().get(JobMapKeys.REQUEST.getKeyName(), Workspace.class);
    final var cloudContextJobId = context.getWorkingMap().get(ControlledResourceKeys.CREATE_CLOUD_CONTEXT_FLIGHT_ID, String.class);
    boolean flightAlreadyExists;
    try {
        context.getStairway().getFlightState(cloudContextJobId);
        flightAlreadyExists = true;
    } catch (FlightNotFoundException e) {
        flightAlreadyExists = false;
    } catch (DatabaseOperationException e) {
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    // if we already have a flight, don't launch another one
    if (!flightAlreadyExists) {
        workspaceService.createGcpCloudContext(destinationWorkspace.getWorkspaceId(), cloudContextJobId, userRequest);
    }
    return StepResult.getStepResultSuccess();
}
Also used : FlightNotFoundException(bio.terra.stairway.exception.FlightNotFoundException) DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) StepResult(bio.terra.stairway.StepResult)

Aggregations

DatabaseOperationException (bio.terra.stairway.exception.DatabaseOperationException)21 StepResult (bio.terra.stairway.StepResult)12 FlightMap (bio.terra.stairway.FlightMap)11 FlightState (bio.terra.stairway.FlightState)8 Connection (java.sql.Connection)7 SQLException (java.sql.SQLException)7 FlightWaitTimedOutException (bio.terra.stairway.exception.FlightWaitTimedOutException)6 StairwayExecutionException (bio.terra.stairway.exception.StairwayExecutionException)5 DuplicateFlightIdException (bio.terra.stairway.exception.DuplicateFlightIdException)4 FlightNotFoundException (bio.terra.stairway.exception.FlightNotFoundException)4 Map (java.util.Map)4 WsmCloneResourceResult (bio.terra.workspace.service.workspace.model.WsmCloneResourceResult)3 WsmResourceCloneDetails (bio.terra.workspace.service.workspace.model.WsmResourceCloneDetails)3 ResultSet (java.sql.ResultSet)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