Search in sources :

Example 6 with CoverageMapping

use of gov.cms.ab2d.coverage.model.CoverageMapping in project ab2d by CMSgov.

the class EndToEndBfdTests method getCoverage.

/**
 * Load the coverage data for all enabled contracts. To do this we:
 *
 * 1. Discover all the coverage periods for the contracts
 * 2. Queue all the stale coverage periods to searches
 * 3. For each search, start it
 * 4. While the searches are not complete, call monitorMappingJobs which takes the results of the searches and adds
 *    them to the queue to save. This would be done by a quartz job normally
 * 5. While the searches have not been saved, call insertJobResults which takes the results of the save queue and *
 *    adds saves the data. This would be done by a quartz job normally
 *
 *  These operations are done sequentially and it's fine for a small amount of data. This is not appropriate
 *  for several or large contracts
 *
 * @throws InterruptedException if there is an issue with threads being interrupted
 */
private void getCoverage() throws InterruptedException {
    long startTime = System.currentTimeMillis();
    coverageDriver.discoverCoveragePeriods();
    coverageDriver.queueStaleCoveragePeriods();
    Optional<CoverageSearch> search = ((CoverageDriverImpl) coverageDriver).getNextSearch();
    while (search.isPresent()) {
        Optional<CoverageMapping> maybeSearch = coverageService.startSearch(search.get(), "starting a job");
        if (maybeSearch.isEmpty()) {
            continue;
        }
        CoverageMapping mapping = maybeSearch.get();
        if (!coverageProcessor.startJob(mapping)) {
            coverageService.cancelSearch(mapping.getPeriodId(), "failed to start job");
            coverageProcessor.queueMapping(mapping, false);
        }
        search = ((CoverageDriverImpl) coverageDriver).getNextSearch();
    }
    // Wait for all the searches to be done
    while (numberOfInProgressMappings() > 0) {
        System.out.println("\n************** " + numberOfInProgressMappings() + " num searches still to do\n");
        ((CoverageProcessorImpl) coverageProcessor).monitorMappingJobs();
        Thread.sleep(1000);
    }
    // Wait for all the inserts to be done
    while (numberCoverageInsertion() > 0) {
        System.out.println("\n************** " + numberCoverageInsertion() + " num inserts still to do\n");
        ((CoverageProcessorImpl) coverageProcessor).insertJobResults();
        Thread.sleep(1000);
    }
    long endTime = System.currentTimeMillis();
    long timeToProcess = endTime - startTime;
    System.out.println("\n*************** It took " + ((double) timeToProcess) / 1000 + " seconds to load coverage data\n");
}
Also used : CoverageMapping(gov.cms.ab2d.coverage.model.CoverageMapping) ContractToContractCoverageMapping(gov.cms.ab2d.worker.config.ContractToContractCoverageMapping) CoverageSearch(gov.cms.ab2d.coverage.model.CoverageSearch) CoverageDriverImpl(gov.cms.ab2d.worker.processor.coverage.CoverageDriverImpl) CoverageProcessorImpl(gov.cms.ab2d.worker.processor.coverage.CoverageProcessorImpl)

Example 7 with CoverageMapping

use of gov.cms.ab2d.coverage.model.CoverageMapping in project ab2d by CMSgov.

the class CoverageServiceImpl method startSearch.

@Override
public Optional<CoverageMapping> startSearch(CoverageSearch submittedSearch, String description) {
    if (submittedSearch == null) {
        return Optional.empty();
    }
    CoveragePeriod period = submittedSearch.getPeriod();
    CoverageSearchEvent coverageSearchEvent = updateStatus(period, description, CoverageJobStatus.IN_PROGRESS);
    return Optional.of(new CoverageMapping(coverageSearchEvent, submittedSearch));
}
Also used : CoverageMapping(gov.cms.ab2d.coverage.model.CoverageMapping) CoveragePeriod(gov.cms.ab2d.coverage.model.CoveragePeriod) CoverageSearchEvent(gov.cms.ab2d.coverage.model.CoverageSearchEvent)

Example 8 with CoverageMapping

use of gov.cms.ab2d.coverage.model.CoverageMapping in project ab2d by CMSgov.

the class CoverageDriverImpl method loadMappingJob.

/**
 * Queues coverage mapping jobs to run on this machine. Coverage mapping jobs are split
 * between workers so this worker will get a job to start in a thread safe manner.
 *
 * Method checks that worker node is running properly and not too busy before attempting to find a search
 * and start that search.
 *
 * If a coverage job fails to start it is immediately cancelled and queued again silently.
 */
@Scheduled(cron = "${coverage.update.load.schedule}")
public void loadMappingJob() {
    if (propertiesService.isInMaintenanceMode()) {
        log.info("waiting to execute queued coverage searches because api is in maintenance mode");
        return;
    }
    if (coverageProcessor.isProcessorBusy()) {
        log.info("not starting any new coverage mapping jobs because service is full.");
        return;
    }
    Optional<CoverageSearch> search = getNextSearch();
    if (search.isEmpty()) {
        return;
    }
    Optional<CoverageMapping> maybeSearch = coverageService.startSearch(search.get(), "starting a job");
    if (maybeSearch.isEmpty()) {
        return;
    }
    CoverageMapping mapping = maybeSearch.get();
    log.info("found a search in queue for contract {} during {}-{}, attempting to search", mapping.getContractNumber(), mapping.getPeriod().getMonth(), mapping.getPeriod().getYear());
    /*
         * Start a job, if starting a job fails immediately cancel the job and queue the search again.
         */
    if (!coverageProcessor.startJob(mapping)) {
        coverageService.cancelSearch(mapping.getPeriodId(), "failed to start job");
        coverageProcessor.queueMapping(mapping, false);
    }
}
Also used : ContractToContractCoverageMapping(gov.cms.ab2d.worker.config.ContractToContractCoverageMapping) CoverageMapping(gov.cms.ab2d.coverage.model.CoverageMapping) CoverageSearch(gov.cms.ab2d.coverage.model.CoverageSearch) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 9 with CoverageMapping

use of gov.cms.ab2d.coverage.model.CoverageMapping in project ab2d by CMSgov.

the class CoverageProcessorImpl method insertJobResults.

/**
 * Insert results of one coverage search at a time into the database unless the processor has shut down.
 *
 * The {@link #attemptCoverageInsertion(CoverageMapping)} method will handle failure to insertion by resubmitting
 * quietly.
 *
 * Only inserts results of coverage mapping jobs run on the current application, not jobs running on other machines
 */
@Scheduled(fixedDelay = ONE_SECOND, initialDelayString = "${coverage.update.initial.delay}")
public void insertJobResults() {
    try {
        CoverageMapping result = coverageInsertionQueue.poll(SIXTY_SECONDS, TimeUnit.MILLISECONDS);
        if (result == null) {
            return;
        }
        String contractNumber = result.getContractNumber();
        int month = result.getPeriod().getMonth();
        int year = result.getPeriod().getYear();
        log.info("attempting to insert coverage for {}-{}-{}", contractNumber, month, year);
        if (!inShutdown.get()) {
            log.info("inserting coverage mapping for contract {} during {}-{}", contractNumber, month, year);
            attemptCoverageInsertion(result);
        } else {
            String message = String.format("shutting down before inserting results for" + " contract %s during %d-%d, will re-attempt", contractNumber, month, year);
            log.info(message);
            resubmitMapping(result, message, "shutting down coverage processor before beneficiary data can be inserted into database", true);
        }
    } catch (InterruptedException ie) {
        // NOSONAR
        log.info("polling for data to insert failed due to interruption {}", ie.getMessage());
    }
}
Also used : CoverageMapping(gov.cms.ab2d.coverage.model.CoverageMapping) ContractToContractCoverageMapping(gov.cms.ab2d.worker.config.ContractToContractCoverageMapping) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 10 with CoverageMapping

use of gov.cms.ab2d.coverage.model.CoverageMapping in project ab2d by CMSgov.

the class CoverageDriverUnitTest method loadMappingFailsQuietly.

@DisplayName("When loading a mapping job exit early if conditions not met")
@Test
void loadMappingFailsQuietly() {
    CoverageDriverImpl driver = spy(new CoverageDriverImpl(null, null, coverageService, propertiesService, coverageProcessor, lockWrapper, null));
    doReturn(true).when(propertiesService).isInMaintenanceMode();
    try {
        driver.loadMappingJob();
    } catch (Exception exception) {
        fail("maintenance mode should cause job to fail quietly", exception);
    }
    doReturn(false).when(propertiesService).isInMaintenanceMode();
    doReturn(true).when(coverageProcessor).isProcessorBusy();
    try {
        driver.loadMappingJob();
    } catch (Exception exception) {
        fail("coverage processor busy should cause job to fail quietly", exception);
    }
    doReturn(false).when(coverageProcessor).isProcessorBusy();
    doReturn(tryLockFalse).when(lockWrapper).getCoverageLock();
    try {
        driver.loadMappingJob();
    } catch (Exception exception) {
        fail("no search found should fail quietly", exception);
    }
    ContractDTO contract = new ContractDTO("contractNum", null, null, null);
    CoveragePeriod coveragePeriod = new CoveragePeriod();
    coveragePeriod.setId(100);
    coveragePeriod.setMonth(1);
    coveragePeriod.setYear(2021);
    coveragePeriod.setContractNumber(contract.getContractNumber());
    CoverageSearchEvent event = new CoverageSearchEvent();
    event.setCoveragePeriod(coveragePeriod);
    CoverageSearch search = new CoverageSearch();
    CoverageMapping mapping = new CoverageMapping(event, search);
    doReturn(Optional.of(search)).when(driver).getNextSearch();
    doReturn(Optional.empty()).when(coverageService).startSearch(any(), anyString());
    try {
        driver.loadMappingJob();
    } catch (Exception exception) {
        fail("coverage service not starting search should not fail", exception);
    }
    doReturn(Optional.of(mapping)).when(coverageService).startSearch(any(), anyString());
    doReturn(false).when(coverageProcessor).startJob(any());
    doReturn(event).when(coverageService).cancelSearch(anyInt(), anyString());
    doNothing().when(coverageProcessor).queueMapping(any(), anyBoolean());
    try {
        driver.loadMappingJob();
    } catch (Exception exception) {
        fail("coverage processor failing to start jobs should fail quietly", exception);
    }
}
Also used : ContractDTO(gov.cms.ab2d.common.dto.ContractDTO) CoverageMapping(gov.cms.ab2d.coverage.model.CoverageMapping) ContractToContractCoverageMapping(gov.cms.ab2d.worker.config.ContractToContractCoverageMapping) CoveragePeriod(gov.cms.ab2d.coverage.model.CoveragePeriod) CoverageSearch(gov.cms.ab2d.coverage.model.CoverageSearch) CoverageSearchEvent(gov.cms.ab2d.coverage.model.CoverageSearchEvent) EntityNotFoundException(javax.persistence.EntityNotFoundException) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

CoverageMapping (gov.cms.ab2d.coverage.model.CoverageMapping)16 ContractToContractCoverageMapping (gov.cms.ab2d.worker.config.ContractToContractCoverageMapping)14 CoverageSearch (gov.cms.ab2d.coverage.model.CoverageSearch)13 CoveragePeriod (gov.cms.ab2d.coverage.model.CoveragePeriod)12 CoverageSearchEvent (gov.cms.ab2d.coverage.model.CoverageSearchEvent)12 Test (org.junit.jupiter.api.Test)11 ContractDTO (gov.cms.ab2d.common.dto.ContractDTO)10 DisplayName (org.junit.jupiter.api.DisplayName)10 Identifiers (gov.cms.ab2d.coverage.model.Identifiers)4 BundleUtils.createPatient (gov.cms.ab2d.worker.processor.BundleUtils.createPatient)3 ContractForCoverageDTO (gov.cms.ab2d.coverage.model.ContractForCoverageDTO)2 Patient (org.hl7.fhir.dstu3.model.Patient)2 Scheduled (org.springframework.scheduling.annotation.Scheduled)2 AB2D_EPOCH (gov.cms.ab2d.common.util.DateUtil.AB2D_EPOCH)1 CoverageCount (gov.cms.ab2d.coverage.model.CoverageCount)1 CoverageDelta (gov.cms.ab2d.coverage.model.CoverageDelta)1 CoverageJobStatus (gov.cms.ab2d.coverage.model.CoverageJobStatus)1 CoveragePagingRequest (gov.cms.ab2d.coverage.model.CoveragePagingRequest)1 CoveragePagingResult (gov.cms.ab2d.coverage.model.CoveragePagingResult)1 CoverageSearchDiff (gov.cms.ab2d.coverage.model.CoverageSearchDiff)1