Search in sources :

Example 1 with FlightStatus

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

the class JobServiceShutdownTest method testShutdown.

@Test
public void testShutdown() throws Exception {
    // Segregated into its own test, since the Stairway object is not usable after it completes
    // scenario:
    // - start a thread that creates one flight every X second; keeping track of the flights
    // - each flight sleeps Y secs and then returns; we want to get a backlog of flights in the queue
    // when thread catches JobServiceShutdown exception, it stops launching and returns its flight list
    // - sleep Z seconds to let the system fill up
    // - call shutdown
    // - validate that flights are either SUCCESS or READY
    // Test Control Parameters
    final int flightPerSecond = 1;
    final int flightWaitSeconds = 30;
    int maxStairwayThreads = appConfig.getMaxStairwayThreads();
    logger.info("maxStairwayThreads: " + maxStairwayThreads);
    JobTestShutdownFlightLauncher launcher = new JobTestShutdownFlightLauncher(flightPerSecond, flightWaitSeconds, testUser, jobService);
    Thread launchThread = new Thread(launcher);
    launchThread.start();
    // Build a backlog in the queue
    TimeUnit.SECONDS.sleep(2 * maxStairwayThreads);
    jobService.shutdown();
    launchThread.join();
    List<String> flightIdList = launcher.getFlightIdList();
    Stairway stairway = jobService.getStairway();
    for (String flightId : flightIdList) {
        FlightState state = stairway.getFlightState(flightId);
        FlightStatus status = state.getFlightStatus();
        logger.info("Flightid: " + flightId + "; status: " + status);
        assertTrue(status == FlightStatus.SUCCESS || status == FlightStatus.READY);
    }
}
Also used : FlightState(bio.terra.stairway.FlightState) Stairway(bio.terra.stairway.Stairway) FlightStatus(bio.terra.stairway.FlightStatus) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with FlightStatus

use of bio.terra.stairway.FlightStatus 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 3 with FlightStatus

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

the class JobService method mapFlightStateToApiJobReport.

public ApiJobReport mapFlightStateToApiJobReport(FlightState flightState) {
    FlightMap inputParameters = flightState.getInputParameters();
    String description = inputParameters.get(JobMapKeys.DESCRIPTION.getKeyName(), String.class);
    FlightStatus flightStatus = flightState.getFlightStatus();
    String submittedDate = flightState.getSubmitted().toString();
    ApiJobReport.StatusEnum jobStatus = getJobStatus(flightStatus);
    String completedDate = null;
    HttpStatus statusCode = HttpStatus.ACCEPTED;
    if (jobStatus != StatusEnum.RUNNING) {
        // If the job is completed, the JobReport should include a result code indicating success or
        // failure. For failed jobs, this code is the error code. For successful jobs, this is the
        // code specified by the flight if present, or a default of 200 if not.
        completedDate = flightState.getCompleted().get().toString();
        switch(jobStatus) {
            case FAILED:
                int errorCode = flightState.getException().map(e -> ErrorReportUtils.buildApiErrorReport(e).getStatusCode()).orElseThrow(() -> new InvalidResultStateException("Flight failed with no exception reported"));
                statusCode = HttpStatus.valueOf(errorCode);
                break;
            case SUCCEEDED:
                FlightMap resultMap = getResultMap(flightState);
                statusCode = resultMap.get(JobMapKeys.STATUS_CODE.getKeyName(), HttpStatus.class);
                if (statusCode == null) {
                    statusCode = HttpStatus.OK;
                }
                break;
            default:
                throw new IllegalStateException("Cannot get status code of flight in unknown state " + jobStatus);
        }
    }
    ApiJobReport jobReport = new ApiJobReport().id(flightState.getFlightId()).description(description).status(jobStatus).statusCode(statusCode.value()).submitted(submittedDate).completed(completedDate).resultURL(resultUrlFromFlightState(flightState));
    return jobReport;
}
Also used : ApiJobReport(bio.terra.workspace.generated.model.ApiJobReport) ScheduledFuture(java.util.concurrent.ScheduledFuture) JobNotCompleteException(bio.terra.workspace.service.job.exception.JobNotCompleteException) StatusEnum(bio.terra.workspace.generated.model.ApiJobReport.StatusEnum) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ApiJobReport(bio.terra.workspace.generated.model.ApiJobReport) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) FlightState(bio.terra.stairway.FlightState) Path(java.nio.file.Path) InvalidResultStateException(bio.terra.workspace.service.job.exception.InvalidResultStateException) UUID(java.util.UUID) StairwayException(bio.terra.stairway.exception.StairwayException) SamWorkspaceAction(bio.terra.workspace.service.iam.model.SamConstants.SamWorkspaceAction) Executors(java.util.concurrent.Executors) Flight(bio.terra.stairway.Flight) FlightNotFoundException(bio.terra.stairway.exception.FlightNotFoundException) List(java.util.List) DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) FlightFilter(bio.terra.stairway.FlightFilter) ErrorReportUtils(bio.terra.workspace.common.utils.ErrorReportUtils) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) DuplicateFlightIdException(bio.terra.stairway.exception.DuplicateFlightIdException) JobConfiguration(bio.terra.workspace.app.configuration.external.JobConfiguration) JobNotFoundException(bio.terra.workspace.service.job.exception.JobNotFoundException) Callable(java.util.concurrent.Callable) StairwayComponent(bio.terra.common.stairway.StairwayComponent) Stairway(bio.terra.stairway.Stairway) ArrayList(java.util.ArrayList) ApiErrorReport(bio.terra.workspace.generated.model.ApiErrorReport) WorkspaceFlightMapKeys(bio.terra.workspace.service.workspace.flight.WorkspaceFlightMapKeys) JobResponseException(bio.terra.workspace.service.job.exception.JobResponseException) FlightFilterOp(bio.terra.stairway.FlightFilterOp) Traced(io.opencensus.contrib.spring.aop.Traced) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MdcHook(bio.terra.workspace.common.utils.MdcHook) FlightDebugInfo(bio.terra.stairway.FlightDebugInfo) FlightBeanBag(bio.terra.workspace.common.utils.FlightBeanBag) InternalStairwayException(bio.terra.workspace.service.job.exception.InternalStairwayException) Logger(org.slf4j.Logger) DuplicateJobIdException(bio.terra.workspace.service.job.exception.DuplicateJobIdException) StairwayDatabaseConfiguration(bio.terra.workspace.app.configuration.external.StairwayDatabaseConfiguration) FlightMap(bio.terra.stairway.FlightMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TracingHook(bio.terra.common.stairway.TracingHook) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpStatus(org.springframework.http.HttpStatus) Component(org.springframework.stereotype.Component) FlightStatus(bio.terra.stairway.FlightStatus) IngressConfiguration(bio.terra.workspace.app.configuration.external.IngressConfiguration) VisibleForTesting(com.google.common.annotations.VisibleForTesting) InvalidResultStateException(bio.terra.workspace.service.job.exception.InvalidResultStateException) StatusEnum(bio.terra.workspace.generated.model.ApiJobReport.StatusEnum) FlightStatus(bio.terra.stairway.FlightStatus) HttpStatus(org.springframework.http.HttpStatus) FlightMap(bio.terra.stairway.FlightMap)

Example 4 with FlightStatus

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

the class JobService method mapFlightStateToJobModel.

public JobModel mapFlightStateToJobModel(FlightState flightState) {
    FlightMap inputParameters = flightState.getInputParameters();
    String description = inputParameters.get(JobMapKeys.DESCRIPTION.getKeyName(), String.class);
    FlightStatus flightStatus = flightState.getFlightStatus();
    String submittedDate = flightState.getSubmitted().toString();
    JobModel.JobStatusEnum jobStatus = getJobStatus(flightStatus);
    String completedDate = null;
    HttpStatus statusCode = HttpStatus.ACCEPTED;
    if (flightState.getCompleted().isPresent()) {
        FlightMap resultMap = getResultMap(flightState);
        // The STATUS_CODE return only needs to be used to return alternate success responses.
        // If it is not present, then we set it to the default OK status.
        statusCode = resultMap.get(JobMapKeys.STATUS_CODE.getKeyName(), HttpStatus.class);
        if (statusCode == null) {
            statusCode = HttpStatus.OK;
        }
        completedDate = flightState.getCompleted().get().toString();
    }
    JobModel jobModel = new JobModel().id(flightState.getFlightId()).description(description).jobStatus(jobStatus).statusCode(statusCode.value()).submitted(submittedDate).completed(completedDate);
    return jobModel;
}
Also used : FlightStatus(bio.terra.stairway.FlightStatus) HttpStatus(org.springframework.http.HttpStatus) FlightMap(bio.terra.stairway.FlightMap) JobModel(bio.terra.model.JobModel)

Aggregations

FlightStatus (bio.terra.stairway.FlightStatus)4 FlightMap (bio.terra.stairway.FlightMap)3 FlightState (bio.terra.stairway.FlightState)3 Stairway (bio.terra.stairway.Stairway)2 DatabaseOperationException (bio.terra.stairway.exception.DatabaseOperationException)2 HttpStatus (org.springframework.http.HttpStatus)2 StairwayComponent (bio.terra.common.stairway.StairwayComponent)1 TracingHook (bio.terra.common.stairway.TracingHook)1 JobModel (bio.terra.model.JobModel)1 Flight (bio.terra.stairway.Flight)1 FlightDebugInfo (bio.terra.stairway.FlightDebugInfo)1 FlightFilter (bio.terra.stairway.FlightFilter)1 FlightFilterOp (bio.terra.stairway.FlightFilterOp)1 StepResult (bio.terra.stairway.StepResult)1 DuplicateFlightIdException (bio.terra.stairway.exception.DuplicateFlightIdException)1 FlightNotFoundException (bio.terra.stairway.exception.FlightNotFoundException)1 FlightWaitTimedOutException (bio.terra.stairway.exception.FlightWaitTimedOutException)1 StairwayException (bio.terra.stairway.exception.StairwayException)1 IngressConfiguration (bio.terra.workspace.app.configuration.external.IngressConfiguration)1 JobConfiguration (bio.terra.workspace.app.configuration.external.JobConfiguration)1