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  		Search searchToDelete = mySearchDao.findOne(theSearchPid);
71  		if (searchToDelete != null) {
72  			ourLog.info("Deleting search {}/{} - Created[{}] -- Last returned[{}]", searchToDelete.getId(), searchToDelete.getUuid(), new InstantType(searchToDelete.getCreated()), new InstantType(searchToDelete.getSearchLastReturned()));
73  			mySearchIncludeDao.deleteForSearch(searchToDelete.getId());
74  			mySearchResultDao.deleteForSearch(searchToDelete.getId());
75  			mySearchDao.delete(searchToDelete);
76  		}
77  	}
78  
79  	@Override
80  	@Transactional(propagation = Propagation.NEVER)
81  	public void pollForStaleSearchesAndDeleteThem() {
82  		if (!myDaoConfig.isExpireSearchResults()) {
83  			return;
84  		}
85  
86  		long cutoffMillis = myDaoConfig.getExpireSearchResultsAfterMillis();
87  		if (myDaoConfig.getReuseCachedSearchResultsForMillis() != null) {
88  			cutoffMillis = Math.max(cutoffMillis, myDaoConfig.getReuseCachedSearchResultsForMillis());
89  		}
90  		final Date cutoff = new Date((now() - cutoffMillis) - myCutoffSlack);
91  
92  		if (ourNowForUnitTests != null) {
93  			ourLog.info("Searching for searches which are before {} - now is {}", new InstantType(cutoff), new InstantType(new Date(now())));
94  		}
95  
96  		ourLog.debug("Searching for searches which are before {}", cutoff);
97  
98  		TransactionTemplate tt = new TransactionTemplate(myTransactionManager);
99  		final Slice<Long> toDelete = tt.execute(new TransactionCallback<Slice<Long>>() {
100 			@Override
101 			public Slice<Long> doInTransaction(TransactionStatus theStatus) {
102 				return mySearchDao.findWhereLastReturnedBefore(cutoff, new PageRequest(0, 1000));
103 			}
104 		});
105 
106 		for (final Long nextSearchToDelete : toDelete) {
107 			ourLog.debug("Deleting search with PID {}", nextSearchToDelete);
108 			tt.execute(new TransactionCallbackWithoutResult() {
109 				@Override
110 				protected void doInTransactionWithoutResult(TransactionStatus status) {
111 					deleteSearch(nextSearchToDelete);
112 				}
113 			});
114 		}
115 
116 		int count = toDelete.getContent().size();
117 		if (count > 0) {
118 			long total = tt.execute(new TransactionCallback<Long>() {
119 				@Override
120 				public Long doInTransaction(TransactionStatus theStatus) {
121 					return mySearchDao.count();
122 				}
123 			});
124 			ourLog.info("Deleted {} searches, {} remaining", count, total);
125 		}
126 
127 	}
128 
129 	@Scheduled(fixedDelay = DEFAULT_CUTOFF_SLACK)
130 	@Transactional(propagation = Propagation.NEVER)
131 	@Override
132 	public synchronized void schedulePollForStaleSearches() {
133 		if (!myDaoConfig.isSchedulingDisabled()) {
134 			pollForStaleSearchesAndDeleteThem();
135 		}
136 	}
137 
138 	@VisibleForTesting
139 	public void setCutoffSlackForUnitTest(long theCutoffSlack) {
140 		myCutoffSlack = theCutoffSlack;
141 	}
142 
143 	private static long now() {
144 		if (ourNowForUnitTests != null) {
145 			return ourNowForUnitTests;
146 		}
147 		return System.currentTimeMillis();
148 	}
149 
150 	/**
151 	 * This is for unit tests only, do not call otherwise
152 	 */
153 	@VisibleForTesting
154 	public static void setNowForUnitTests(Long theNowForUnitTests) {
155 		ourNowForUnitTests = theNowForUnitTests;
156 	}
157 
158 }