Search in sources :

Example 1 with FlightState

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

the class IngestDriverStep method getLoadCandidates.

private LoadCandidates getLoadCandidates(FlightContext context, UUID loadId, int concurrentLoads) throws DatabaseOperationException, InterruptedException {
    // We start by getting the database view of the state of loads.
    // For the running loads, we ask Stairway what the actual state is.
    // If they have completed, we mark them as such.
    // We then update the failure count and runnings loads list in the
    // LoadCandidates so it correctly reflects the running state
    // right now (more or less).
    LoadCandidates candidates = loadService.findCandidates(loadId, concurrentLoads);
    logger.debug("Candidates from db: failedLoads={}  runningLoads={}  candidateFiles={}", candidates.getFailedLoads(), candidates.getRunningLoads().size(), candidates.getCandidateFiles().size());
    int failureCount = candidates.getFailedLoads();
    List<LoadFile> realRunningLoads = new LinkedList<>();
    for (LoadFile loadFile : candidates.getRunningLoads()) {
        FlightState flightState = context.getStairway().getFlightState(loadFile.getFlightId());
        switch(flightState.getFlightStatus()) {
            case RUNNING:
            case WAITING:
            case READY:
            case QUEUED:
                realRunningLoads.add(loadFile);
                break;
            case ERROR:
            case FATAL:
                {
                    String error = "unknown error";
                    if (flightState.getException().isPresent()) {
                        error = flightState.getException().get().toString();
                    }
                    loadService.setLoadFileFailed(loadId, loadFile.getTargetPath(), error);
                    failureCount++;
                    break;
                }
            case SUCCESS:
                {
                    FlightMap resultMap = flightState.getResultMap().orElse(null);
                    if (resultMap == null) {
                        throw new FileSystemCorruptException("no result map in flight state");
                    }
                    String fileId = resultMap.get(FileMapKeys.FILE_ID, String.class);
                    FSFileInfo fileInfo = resultMap.get(FileMapKeys.FILE_INFO, FSFileInfo.class);
                    loadService.setLoadFileSucceeded(loadId, loadFile.getTargetPath(), fileId, fileInfo);
                    break;
                }
        }
    }
    candidates.failedLoads(failureCount).runningLoads(realRunningLoads);
    logger.debug("Candidates resolved: failedLoads={}  runningLoads={}  candidateFiles={}", candidates.getFailedLoads(), candidates.getRunningLoads().size(), candidates.getCandidateFiles().size());
    return candidates;
}
Also used : FlightState(bio.terra.stairway.FlightState) FSFileInfo(bio.terra.service.filedata.FSFileInfo) LoadCandidates(bio.terra.service.load.LoadCandidates) FileSystemCorruptException(bio.terra.service.filedata.exception.FileSystemCorruptException) LoadFile(bio.terra.service.load.LoadFile) FlightMap(bio.terra.stairway.FlightMap) LinkedList(java.util.LinkedList)

Example 2 with FlightState

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

the class JobService method retrieveJobResultWorker.

private <T> JobResultWithStatus<T> retrieveJobResultWorker(String jobId, Class<T> resultClass) throws StairwayException, InterruptedException {
    FlightState flightState = stairway.getFlightState(jobId);
    FlightMap resultMap = flightState.getResultMap().orElse(null);
    if (resultMap == null) {
        throw new InvalidResultStateException("No result map returned from flight");
    }
    switch(flightState.getFlightStatus()) {
        case FATAL:
        case ERROR:
            if (flightState.getException().isPresent()) {
                Exception exception = flightState.getException().get();
                if (exception instanceof RuntimeException) {
                    throw (RuntimeException) exception;
                } else {
                    throw new JobResponseException("wrap non-runtime exception", exception);
                }
            }
            throw new InvalidResultStateException("Failed operation with no exception reported");
        case SUCCESS:
            HttpStatus statusCode = resultMap.get(JobMapKeys.STATUS_CODE.getKeyName(), HttpStatus.class);
            if (statusCode == null) {
                statusCode = HttpStatus.OK;
            }
            return new JobResultWithStatus<T>().statusCode(statusCode).result(resultMap.get(JobMapKeys.RESPONSE.getKeyName(), resultClass));
        case RUNNING:
            throw new JobNotCompleteException("Attempt to retrieve job result before job is complete; job id: " + flightState.getFlightId());
        default:
            throw new InvalidResultStateException("Impossible case reached");
    }
}
Also used : FlightState(bio.terra.stairway.FlightState) InvalidResultStateException(bio.terra.service.job.exception.InvalidResultStateException) HttpStatus(org.springframework.http.HttpStatus) FlightMap(bio.terra.stairway.FlightMap) JobNotCompleteException(bio.terra.service.job.exception.JobNotCompleteException) JobResponseException(bio.terra.service.job.exception.JobResponseException) JobServiceStartupException(bio.terra.service.job.exception.JobServiceStartupException) JobServiceShutdownException(bio.terra.service.job.exception.JobServiceShutdownException) JobNotCompleteException(bio.terra.service.job.exception.JobNotCompleteException) JobUnauthorizedException(bio.terra.service.job.exception.JobUnauthorizedException) InvalidResultStateException(bio.terra.service.job.exception.InvalidResultStateException) StairwayException(bio.terra.stairway.exception.StairwayException) FlightNotFoundException(bio.terra.stairway.exception.FlightNotFoundException) DatabaseOperationException(bio.terra.stairway.exception.DatabaseOperationException) JobNotFoundException(bio.terra.service.job.exception.JobNotFoundException) JobResponseException(bio.terra.service.job.exception.JobResponseException) InternalStairwayException(bio.terra.service.job.exception.InternalStairwayException)

Example 3 with FlightState

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

the class FlightStates method makeFlightCompletedState.

public static FlightState makeFlightCompletedState() {
    DatasetSummaryModel req = buildMinimalDatasetSummary();
    FlightMap resultMap = new FlightMap();
    resultMap.put(JobMapKeys.RESPONSE.getKeyName(), req);
    resultMap.put(JobMapKeys.STATUS_CODE.getKeyName(), HttpStatus.I_AM_A_TEAPOT);
    resultMap.put(JobMapKeys.DESCRIPTION.getKeyName(), req.getDescription());
    FlightState flightState = new FlightState();
    flightState.setFlightId(testFlightId);
    flightState.setFlightStatus(FlightStatus.SUCCESS);
    flightState.setSubmitted(submittedTime);
    flightState.setInputParameters(resultMap);
    flightState.setResultMap(resultMap);
    flightState.setCompleted(completedTime);
    return flightState;
}
Also used : FlightState(bio.terra.stairway.FlightState) DatasetSummaryModel(bio.terra.model.DatasetSummaryModel) FlightMap(bio.terra.stairway.FlightMap)

Example 4 with FlightState

use of bio.terra.stairway.FlightState 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 5 with FlightState

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

the class CreateGcpContextFlightV2Test method successCreatesProjectAndContext.

@Test
@DisabledIfEnvironmentVariable(named = "TEST_ENV", matches = BUFFER_SERVICE_DISABLED_ENVS_REG_EX)
void successCreatesProjectAndContext() throws Exception {
    UUID workspaceId = createWorkspace(spendUtils.defaultSpendId());
    AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
    assertTrue(testUtils.getAuthorizedGcpCloudContext(workspaceId, userRequest).isEmpty());
    // Retry steps once to validate idempotency.
    Map<String, StepStatus> retrySteps = getStepNameToStepStatusMap();
    FlightDebugInfo debugInfo = FlightDebugInfo.newBuilder().doStepFailures(retrySteps).build();
    FlightState flightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateGcpContextFlightV2.class, createInputParameters(workspaceId, userRequest), STAIRWAY_FLIGHT_TIMEOUT, debugInfo);
    assertEquals(FlightStatus.SUCCESS, flightState.getFlightStatus());
    String projectId = flightState.getResultMap().get().get(WorkspaceFlightMapKeys.GCP_PROJECT_ID, String.class);
    assertTrue(testUtils.getAuthorizedGcpCloudContext(workspaceId, userRequest).isPresent());
    String contextProjectId = workspaceService.getAuthorizedRequiredGcpProject(workspaceId, userRequest);
    assertEquals(projectId, contextProjectId);
    // Verify that the policies were properly stored
    Optional<GcpCloudContext> optionalCloudContext = testUtils.getAuthorizedGcpCloudContext(workspaceId, userRequest);
    assertTrue(optionalCloudContext.isPresent(), "has cloud context");
    GcpCloudContext cloudContext = optionalCloudContext.get();
    assertTrue(cloudContext.getSamPolicyOwner().isPresent(), "has owner policy");
    assertTrue(cloudContext.getSamPolicyWriter().isPresent(), "has writer policy");
    assertTrue(cloudContext.getSamPolicyReader().isPresent(), "has reader policy");
    assertTrue(cloudContext.getSamPolicyApplication().isPresent(), "has application policy");
    Project project = crl.getCloudResourceManagerCow().projects().get(projectId).execute();
    assertEquals(projectId, project.getProjectId());
    assertEquals("billingAccounts/" + spendUtils.defaultBillingAccountId(), crl.getCloudBillingClientCow().getProjectBillingInfo("projects/" + projectId).getBillingAccountName());
    assertRolesExist(project);
    assertPolicyGroupsSynced(workspaceId, project);
}
Also used : FlightState(bio.terra.stairway.FlightState) Project(com.google.api.services.cloudresourcemanager.v3.model.Project) FlightDebugInfo(bio.terra.stairway.FlightDebugInfo) AuthenticatedUserRequest(bio.terra.workspace.service.iam.AuthenticatedUserRequest) StepStatus(bio.terra.stairway.StepStatus) UUID(java.util.UUID) GcpCloudContext(bio.terra.workspace.service.workspace.model.GcpCloudContext) Test(org.junit.jupiter.api.Test) BaseConnectedTest(bio.terra.workspace.common.BaseConnectedTest) DisabledIfEnvironmentVariable(org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)

Aggregations

FlightState (bio.terra.stairway.FlightState)49 AuthenticatedUserRequest (bio.terra.workspace.service.iam.AuthenticatedUserRequest)23 UUID (java.util.UUID)23 Test (org.junit.jupiter.api.Test)23 FlightMap (bio.terra.stairway.FlightMap)20 BaseConnectedTest (bio.terra.workspace.common.BaseConnectedTest)16 DisabledIfEnvironmentVariable (org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)15 FlightDebugInfo (bio.terra.stairway.FlightDebugInfo)11 DatabaseOperationException (bio.terra.stairway.exception.DatabaseOperationException)11 StepStatus (bio.terra.stairway.StepStatus)7 StairwayException (bio.terra.stairway.exception.StairwayException)7 BaseAzureTest (bio.terra.workspace.common.BaseAzureTest)7 InternalStairwayException (bio.terra.workspace.service.job.exception.InternalStairwayException)7 StepResult (bio.terra.stairway.StepResult)6 FlightWaitTimedOutException (bio.terra.stairway.exception.FlightWaitTimedOutException)6 HashMap (java.util.HashMap)6 FlightNotFoundException (bio.terra.stairway.exception.FlightNotFoundException)5 ControlledResource (bio.terra.workspace.service.resource.controlled.model.ControlledResource)5 ManagementException (com.azure.core.management.exception.ManagementException)5 Project (com.google.api.services.cloudresourcemanager.v3.model.Project)5