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");
}
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));
}
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);
}
}
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());
}
}
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);
}
}
Aggregations