View Javadoc
1   package ca.uhn.fhir.jpa.search;
2   
3   /*-
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import ca.uhn.fhir.jpa.dao.DaoConfig;
24  import ca.uhn.fhir.jpa.dao.data.ISearchDao;
25  import ca.uhn.fhir.jpa.dao.data.ISearchIncludeDao;
26  import ca.uhn.fhir.jpa.dao.data.ISearchResultDao;
27  import ca.uhn.fhir.jpa.entity.Search;
28  import com.google.common.annotations.VisibleForTesting;
29  import org.apache.commons.lang3.time.DateUtils;
30  import org.hl7.fhir.dstu3.model.InstantType;
31  import org.springframework.beans.factory.annotation.Autowired;
32  import org.springframework.data.domain.PageRequest;
33  import org.springframework.data.domain.Slice;
34  import org.springframework.scheduling.annotation.Scheduled;
35  import org.springframework.transaction.PlatformTransactionManager;
36  import org.springframework.transaction.TransactionStatus;
37  import org.springframework.transaction.annotation.Propagation;
38  import org.springframework.transaction.annotation.Transactional;
39  import org.springframework.transaction.support.TransactionCallback;
40  import org.springframework.transaction.support.TransactionCallbackWithoutResult;
41  import org.springframework.transaction.support.TransactionTemplate;
42  
43  import java.util.Date;
44  
45  /**
46   * Deletes old searches
47   */
48  public class StaleSearchDeletingSvcImpl implements IStaleSearchDeletingSvc {
49  	public static final long DEFAULT_CUTOFF_SLACK = 10 * DateUtils.MILLIS_PER_SECOND;
50  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(StaleSearchDeletingSvcImpl.class);
51  	private static Long ourNowForUnitTests;
52  	/*
53  	 * We give a bit of extra leeway just to avoid race conditions where a query result
54  	 * is being reused (because a new client request came in with the same params) right before
55  	 * the result is to be deleted
56  	 */
57  	private long myCutoffSlack = DEFAULT_CUTOFF_SLACK;
58  	@Autowired
59  	private DaoConfig myDaoConfig;
60  	@Autowired
61  	private ISearchDao mySearchDao;
62  	@Autowired
63  	private ISearchIncludeDao mySearchIncludeDao;
64  	@Autowired
65  	private ISearchResultDao mySearchResultDao;
66  	@Autowired
67  	private PlatformTransactionManager myTransactionManager;
68  
69  	private void deleteSearch(final Long theSearchPid) {
70  		mySearchDao.findById(theSearchPid).ifPresent(searchToDelete -> {
71  			ourLog.info("Deleting search {}/{} - Created[{}] -- Last returned[{}]", searchToDelete.getId(), searchToDelete.getUuid(), new InstantType(searchToDelete.getCreated()), new InstantType(searchToDelete.getSearchLastReturned()));
72  			mySearchIncludeDao.deleteForSearch(searchToDelete.getId());
73  			mySearchResultDao.deleteForSearch(searchToDelete.getId());
74  			mySearchDao.delete(searchToDelete);
75  		});
76  	}
77  
78  	@Override
79  	@Transactional(propagation = Propagation.NEVER)
80  	public void pollForStaleSearchesAndDeleteThem() {
81  		if (!myDaoConfig.isExpireSearchResults()) {
82  			return;
83  		}
84  
85  		long cutoffMillis = myDaoConfig.getExpireSearchResultsAfterMillis();
86  		if (myDaoConfig.getReuseCachedSearchResultsForMillis() != null) {
87  			cutoffMillis = Math.max(cutoffMillis, myDaoConfig.getReuseCachedSearchResultsForMillis());
88  		}
89  		final Date cutoff = new Date((now() - cutoffMillis) - myCutoffSlack);
90  
91  		if (ourNowForUnitTests != null) {
92  			ourLog.info("Searching for searches which are before {} - now is {}", new InstantType(cutoff), new InstantType(new Date(now())));
93  		}
94  
95  		ourLog.debug("Searching for searches which are before {}", cutoff);
96  
97  		TransactionTemplate tt = new TransactionTemplate(myTransactionManager);
98  		final Slice<Long> toDelete = tt.execute(new TransactionCallback<Slice<Long>>() {
99  			@Override
100 			public Slice<Long> doInTransaction(TransactionStatus theStatus) {
101 				return mySearchDao.findWhereLastReturnedBefore(cutoff, new PageRequest(0, 1000));
102 			}
103 		});
104 
105 		for (final Long nextSearchToDelete : toDelete) {
106 			ourLog.debug("Deleting search with PID {}", nextSearchToDelete);
107 			tt.execute(new TransactionCallbackWithoutResult() {
108 				@Override
109 				protected void doInTransactionWithoutResult(TransactionStatus status) {
110 					deleteSearch(nextSearchToDelete);
111 				}
112 			});
113 		}
114 
115 		int count = toDelete.getContent().size();
116 		if (count > 0) {
117 			long total = tt.execute(new TransactionCallback<Long>() {
118 				@Override
119 				public Long doInTransaction(TransactionStatus theStatus) {
120 					return mySearchDao.count();
121 				}
122 			});
123 			ourLog.info("Deleted {} searches, {} remaining", count, total);
124 		}
125 
126 	}
127 
128 	@Scheduled(fixedDelay = DEFAULT_CUTOFF_SLACK)
129 	@Transactional(propagation = Propagation.NEVER)
130 	@Override
131 	public synchronized void schedulePollForStaleSearches() {
132 		if (!myDaoConfig.isSchedulingDisabled()) {
133 			pollForStaleSearchesAndDeleteThem();
134 		}
135 	}
136 
137 	@VisibleForTesting
138 	public void setCutoffSlackForUnitTest(long theCutoffSlack) {
139 		myCutoffSlack = theCutoffSlack;
140 	}
141 
142 	private static long now() {
143 		if (ourNowForUnitTests != null) {
144 			return ourNowForUnitTests;
145 		}
146 		return System.currentTimeMillis();
147 	}
148 
149 	/**
150 	 * This is for unit tests only, do not call otherwise
151 	 */
152 	@VisibleForTesting
153 	public static void setNowForUnitTests(Long theNowForUnitTests) {
154 		ourNowForUnitTests = theNowForUnitTests;
155 	}
156 
157 }