Add an interface for the stale search deleter

This commit is contained in:
James Agnew 2017-03-01 19:50:17 -05:00
parent 01d102accc
commit bcff22c769
6 changed files with 40 additions and 23 deletions

View File

@ -38,7 +38,8 @@ import org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import ca.uhn.fhir.jpa.search.DatabaseBackedPagingProvider;
import ca.uhn.fhir.jpa.search.StaleSearchDeletingSvc;
import ca.uhn.fhir.jpa.search.IStaleSearchDeletingSvc;
import ca.uhn.fhir.jpa.search.StaleSearchDeletingSvcImpl;
@Configuration
@EnableScheduling
@ -59,13 +60,13 @@ public class BaseConfig implements SchedulingConfigurer {
@Bean(autowire = Autowire.BY_TYPE)
public DatabaseBackedPagingProvider databaseBackedPagingProvider() {
DatabaseBackedPagingProvider retVal = new DatabaseBackedPagingProvider(10);
DatabaseBackedPagingProvider retVal = new DatabaseBackedPagingProvider();
return retVal;
}
@Bean(autowire=Autowire.BY_TYPE)
public StaleSearchDeletingSvc staleSearchDeletingSvc() {
return new StaleSearchDeletingSvc();
public IStaleSearchDeletingSvc staleSearchDeletingSvc() {
return new StaleSearchDeletingSvcImpl();
}
@Bean()

View File

@ -0,0 +1,9 @@
package ca.uhn.fhir.jpa.search;
public interface IStaleSearchDeletingSvc {
void pollForStaleSearchesAndDeleteThem();
void schedulePollForStaleSearches();
}

View File

@ -42,8 +42,8 @@ import ca.uhn.fhir.jpa.entity.Search;
/**
* Deletes old searches
*/
public class StaleSearchDeletingSvc {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(StaleSearchDeletingSvc.class);
public class StaleSearchDeletingSvcImpl implements IStaleSearchDeletingSvc {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(StaleSearchDeletingSvcImpl.class);
@Autowired
private ISearchDao mySearchDao;
@ -62,15 +62,23 @@ public class StaleSearchDeletingSvc {
@Scheduled(fixedDelay = 10 * DateUtils.MILLIS_PER_SECOND)
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public synchronized void pollForStaleSearches() {
if (myDaoConfig.isExpireSearchResults()) {
Date cutoff = new Date(System.currentTimeMillis() - myDaoConfig.getExpireSearchResultsAfterMillis());
ourLog.debug("Searching for searches which are before {}", cutoff);
Collection<Search> toDelete = mySearchDao.findWhereCreatedBefore(cutoff);
if (toDelete.isEmpty()) {
return;
@Override
public synchronized void schedulePollForStaleSearches() {
if (!myDaoConfig.isSchedulingDisabled()) {
if (myDaoConfig.isExpireSearchResults()) {
pollForStaleSearchesAndDeleteThem();
}
}
}
@Override
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void pollForStaleSearchesAndDeleteThem() {
Date cutoff = new Date(System.currentTimeMillis() - myDaoConfig.getExpireSearchResultsAfterMillis());
ourLog.debug("Searching for searches which are before {}", cutoff);
Collection<Search> toDelete = mySearchDao.findWhereCreatedBefore(cutoff);
if (!toDelete.isEmpty()) {
for (final Search next : toDelete) {
deleteSearch(next);

View File

@ -36,7 +36,7 @@ import ca.uhn.fhir.jpa.dao.dstu2.FhirResourceDaoDstu2SearchNoFtTest;
import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamString;
import ca.uhn.fhir.jpa.entity.ResourceTable;
import ca.uhn.fhir.jpa.provider.dstu3.JpaSystemProviderDstu3;
import ca.uhn.fhir.jpa.search.StaleSearchDeletingSvc;
import ca.uhn.fhir.jpa.search.IStaleSearchDeletingSvc;
import ca.uhn.fhir.jpa.term.IHapiTerminologySvc;
import ca.uhn.fhir.jpa.validation.JpaValidationSupportChainDstu3;
import ca.uhn.fhir.parser.IParser;
@ -162,7 +162,7 @@ public abstract class BaseJpaDstu3Test extends BaseJpaTest {
@Autowired
protected IFulltextSearchSvc mySearchDao;
@Autowired
protected StaleSearchDeletingSvc myStaleSearchDeletingSvc;
protected IStaleSearchDeletingSvc myStaleSearchDeletingSvc;
@Autowired
@Qualifier("myStructureDefinitionDaoDstu3")
protected IFhirResourceDao<StructureDefinition> myStructureDefinitionDao;

View File

@ -49,7 +49,7 @@ import ca.uhn.fhir.jpa.dao.DaoConfig;
import ca.uhn.fhir.jpa.dao.SearchParameterMap;
import ca.uhn.fhir.jpa.dao.SearchParameterMap.EverythingModeEnum;
import ca.uhn.fhir.jpa.entity.*;
import ca.uhn.fhir.jpa.search.StaleSearchDeletingSvc;
import ca.uhn.fhir.jpa.search.IStaleSearchDeletingSvc;
import ca.uhn.fhir.model.api.IQueryParameterType;
import ca.uhn.fhir.model.api.Include;
import ca.uhn.fhir.model.dstu.valueset.QuantityCompararatorEnum;
@ -1510,7 +1510,7 @@ public class FhirResourceDaoDstu3SearchNoFtTest extends BaseJpaDstu3Test {
}
@Autowired
protected StaleSearchDeletingSvc myStaleSearchDeletingSvc;
protected IStaleSearchDeletingSvc myStaleSearchDeletingSvc;
@Test
@ -1539,7 +1539,7 @@ public class FhirResourceDaoDstu3SearchNoFtTest extends BaseJpaDstu3Test {
assertThat(toUnqualifiedVersionlessIds(bundleProvider), containsInAnyOrder(pid1, pid2));
myDaoConfig.setExpireSearchResults(false);
myStaleSearchDeletingSvc.pollForStaleSearches();
myStaleSearchDeletingSvc.pollForStaleSearchesAndDeleteThem();
Thread.sleep(1500);
assertThat(toUnqualifiedVersionlessIds(bundleProvider), (containsInAnyOrder(pid1, pid2)));
@ -1572,7 +1572,7 @@ public class FhirResourceDaoDstu3SearchNoFtTest extends BaseJpaDstu3Test {
Thread.sleep(1500);
myDaoConfig.setExpireSearchResultsAfterMillis(500);
myStaleSearchDeletingSvc.pollForStaleSearches();
myStaleSearchDeletingSvc.pollForStaleSearchesAndDeleteThem();
assertThat(toUnqualifiedVersionlessIds(bundleProvider), not(containsInAnyOrder(pid1, pid2)));
}

View File

@ -17,7 +17,6 @@ import org.junit.Test;
import ca.uhn.fhir.rest.gclient.IClientExecutable;
import ca.uhn.fhir.rest.gclient.IQuery;
import ca.uhn.fhir.rest.server.exceptions.ResourceGoneException;
import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
import ca.uhn.fhir.util.TestUtil;
public class StaleSearchDeletingSvcDstu3Test extends BaseResourceProviderDstu3Test {
@ -69,13 +68,13 @@ public class StaleSearchDeletingSvcDstu3Test extends BaseResourceProviderDstu3Te
Bundle resp2 = ourClient.search().byUrl(nextLinkUrl).returnBundle(Bundle.class).execute();
ourLog.info(myFhirCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(resp2));
myStaleSearchDeletingSvc.pollForStaleSearches();
myStaleSearchDeletingSvc.pollForStaleSearchesAndDeleteThem();
ourClient.search().byUrl(nextLinkUrl).returnBundle(Bundle.class).execute();
Thread.sleep(20);
myDaoConfig.setExpireSearchResultsAfterMillis(10);
myStaleSearchDeletingSvc.pollForStaleSearches();
myStaleSearchDeletingSvc.pollForStaleSearchesAndDeleteThem();
try {
ourClient.search().byUrl(nextLinkUrl).returnBundle(Bundle.class).execute();