source: josm/trunk/src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java@ 13825

Last change on this file since 13825 was 13825, checked in by Don-vip, 6 years ago

SonarQube - squid:S3878 - Arrays should not be created for varargs parameters

  • Property svn:eol-style set to native
File size: 22.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.cache;
3
4import java.io.FileNotFoundException;
5import java.io.IOException;
6import java.net.HttpURLConnection;
7import java.net.URL;
8import java.security.SecureRandom;
9import java.util.HashSet;
10import java.util.List;
11import java.util.Map;
12import java.util.Set;
13import java.util.concurrent.ConcurrentHashMap;
14import java.util.concurrent.ConcurrentMap;
15import java.util.concurrent.LinkedBlockingDeque;
16import java.util.concurrent.ThreadPoolExecutor;
17import java.util.concurrent.TimeUnit;
18import java.util.regex.Matcher;
19
20import org.apache.commons.jcs.access.behavior.ICacheAccess;
21import org.apache.commons.jcs.engine.behavior.ICacheElement;
22import org.openstreetmap.josm.data.cache.ICachedLoaderListener.LoadResult;
23import org.openstreetmap.josm.data.imagery.TileJobOptions;
24import org.openstreetmap.josm.data.preferences.IntegerProperty;
25import org.openstreetmap.josm.tools.CheckParameterUtil;
26import org.openstreetmap.josm.tools.HttpClient;
27import org.openstreetmap.josm.tools.Logging;
28import org.openstreetmap.josm.tools.Utils;
29
30/**
31 * Generic loader for HTTP based tiles. Uses custom attribute, to check, if entry has expired
32 * according to HTTP headers sent with tile. If so, it tries to verify using Etags
33 * or If-Modified-Since / Last-Modified.
34 *
35 * If the tile is not valid, it will try to download it from remote service and put it
36 * to cache. If remote server will fail it will try to use stale entry.
37 *
38 * This class will keep only one Job running for specified tile. All others will just finish, but
39 * listeners will be gathered and notified, once download job will be finished
40 *
41 * @author Wiktor Niesiobędzki
42 * @param <K> cache entry key type
43 * @param <V> cache value type
44 * @since 8168
45 */
46public abstract class JCSCachedTileLoaderJob<K, V extends CacheEntry> implements ICachedLoaderJob<K> {
47 protected static final long DEFAULT_EXPIRE_TIME = TimeUnit.DAYS.toMillis(7);
48 // Limit for the max-age value send by the server.
49 protected static final long EXPIRE_TIME_SERVER_LIMIT = TimeUnit.DAYS.toMillis(28);
50 // Absolute expire time limit. Cached tiles that are older will not be used,
51 // even if the refresh from the server fails.
52 protected static final long ABSOLUTE_EXPIRE_TIME_LIMIT = TimeUnit.DAYS.toMillis(365);
53
54 /**
55 * maximum download threads that will be started
56 */
57 public static final IntegerProperty THREAD_LIMIT = new IntegerProperty("cache.jcs.max_threads", 10);
58
59 /*
60 * ThreadPoolExecutor starts new threads, until THREAD_LIMIT is reached. Then it puts tasks into LinkedBlockingDeque.
61 *
62 * The queue works FIFO, so one needs to take care about ordering of the entries submitted
63 *
64 * There is no point in canceling tasks, that are already taken by worker threads (if we made so much effort, we can at least cache
65 * the response, so later it could be used). We could actually cancel what is in LIFOQueue, but this is a tradeoff between simplicity
66 * and performance (we do want to have something to offer to worker threads before tasks will be resubmitted by class consumer)
67 */
68
69 private static final ThreadPoolExecutor DEFAULT_DOWNLOAD_JOB_DISPATCHER = new ThreadPoolExecutor(
70 1, // we have a small queue, so threads will be quickly started (threads are started only, when queue is full)
71 THREAD_LIMIT.get(), // do not this number of threads
72 30, // keepalive for thread
73 TimeUnit.SECONDS,
74 // make queue of LIFO type - so recently requested tiles will be loaded first (assuming that these are which user is waiting to see)
75 new LinkedBlockingDeque<Runnable>(),
76 Utils.newThreadFactory("JCS-downloader-%d", Thread.NORM_PRIORITY)
77 );
78
79 private static final ConcurrentMap<String, Set<ICachedLoaderListener>> inProgress = new ConcurrentHashMap<>();
80 private static final ConcurrentMap<String, Boolean> useHead = new ConcurrentHashMap<>();
81
82 protected final long now; // when the job started
83
84 private final ICacheAccess<K, V> cache;
85 private ICacheElement<K, V> cacheElement;
86 protected V cacheData;
87 protected CacheEntryAttributes attributes;
88
89 // HTTP connection parameters
90 private final int connectTimeout;
91 private final int readTimeout;
92 private final Map<String, String> headers;
93 private final ThreadPoolExecutor downloadJobExecutor;
94 private Runnable finishTask;
95 private boolean force;
96 private long minimumExpiryTime;
97
98 /**
99 * @param cache cache instance that we will work on
100 * @param options options of the request
101 * @param downloadJobExecutor that will be executing the jobs
102 */
103 public JCSCachedTileLoaderJob(ICacheAccess<K, V> cache,
104 TileJobOptions options,
105 ThreadPoolExecutor downloadJobExecutor) {
106 CheckParameterUtil.ensureParameterNotNull(cache, "cache");
107 this.cache = cache;
108 this.now = System.currentTimeMillis();
109 this.connectTimeout = options.getConnectionTimeout();
110 this.readTimeout = options.getReadTimeout();
111 this.headers = options.getHeaders();
112 this.downloadJobExecutor = downloadJobExecutor;
113 this.minimumExpiryTime = TimeUnit.SECONDS.toMillis(options.getMinimumExpiryTime());
114 }
115
116 /**
117 * @param cache cache instance that we will work on
118 * @param options of the request
119 */
120 public JCSCachedTileLoaderJob(ICacheAccess<K, V> cache,
121 TileJobOptions options) {
122 this(cache, options, DEFAULT_DOWNLOAD_JOB_DISPATCHER);
123 }
124
125 private void ensureCacheElement() {
126 if (cacheElement == null && getCacheKey() != null) {
127 cacheElement = cache.getCacheElement(getCacheKey());
128 if (cacheElement != null) {
129 attributes = (CacheEntryAttributes) cacheElement.getElementAttributes();
130 cacheData = cacheElement.getVal();
131 }
132 }
133 }
134
135 @Override
136 public V get() {
137 ensureCacheElement();
138 return cacheData;
139 }
140
141 @Override
142 public void submit(ICachedLoaderListener listener, boolean force) throws IOException {
143 this.force = force;
144 boolean first = false;
145 URL url = getUrl();
146 String deduplicationKey = null;
147 if (url != null) {
148 // url might be null, for example when Bing Attribution is not loaded yet
149 deduplicationKey = url.toString();
150 }
151 if (deduplicationKey == null) {
152 Logging.warn("No url returned for: {0}, skipping", getCacheKey());
153 throw new IllegalArgumentException("No url returned");
154 }
155 synchronized (inProgress) {
156 Set<ICachedLoaderListener> newListeners = inProgress.get(deduplicationKey);
157 if (newListeners == null) {
158 newListeners = new HashSet<>();
159 inProgress.put(deduplicationKey, newListeners);
160 first = true;
161 }
162 newListeners.add(listener);
163 }
164
165 if (first || force) {
166 // submit all jobs to separate thread, so calling thread is not blocked with IO when loading from disk
167 Logging.debug("JCS - Submitting job for execution for url: {0}", getUrlNoException());
168 downloadJobExecutor.execute(this);
169 }
170 }
171
172 /**
173 * This method is run when job has finished
174 */
175 protected void executionFinished() {
176 if (finishTask != null) {
177 finishTask.run();
178 }
179 }
180
181 /**
182 *
183 * @return checks if object from cache has sufficient data to be returned
184 */
185 protected boolean isObjectLoadable() {
186 if (cacheData == null) {
187 return false;
188 }
189 return cacheData.getContent().length > 0;
190 }
191
192 /**
193 * Simple implementation. All errors should be cached as empty. Though some JDK (JDK8 on Windows for example)
194 * doesn't return 4xx error codes, instead they do throw an FileNotFoundException or IOException
195 *
196 * @return true if we should put empty object into cache, regardless of what remote resource has returned
197 */
198 protected boolean cacheAsEmpty() {
199 return attributes.getResponseCode() < 500;
200 }
201
202 /**
203 * @return key under which discovered server settings will be kept
204 */
205 protected String getServerKey() {
206 try {
207 return getUrl().getHost();
208 } catch (IOException e) {
209 Logging.trace(e);
210 return null;
211 }
212 }
213
214 @Override
215 public void run() {
216 final Thread currentThread = Thread.currentThread();
217 final String oldName = currentThread.getName();
218 currentThread.setName("JCS Downloading: " + getUrlNoException());
219 Logging.debug("JCS - starting fetch of url: {0} ", getUrlNoException());
220 ensureCacheElement();
221 try {
222 // try to fetch from cache
223 if (!force && cacheElement != null && isCacheElementValid() && isObjectLoadable()) {
224 // we got something in cache, and it's valid, so lets return it
225 Logging.debug("JCS - Returning object from cache: {0}", getCacheKey());
226 finishLoading(LoadResult.SUCCESS);
227 return;
228 }
229
230 // try to load object from remote resource
231 if (loadObject()) {
232 finishLoading(LoadResult.SUCCESS);
233 } else {
234 // if loading failed - check if we can return stale entry
235 if (isObjectLoadable()) {
236 // try to get stale entry in cache
237 finishLoading(LoadResult.SUCCESS);
238 Logging.debug("JCS - found stale object in cache: {0}", getUrlNoException());
239 } else {
240 // failed completely
241 finishLoading(LoadResult.FAILURE);
242 }
243 }
244 } finally {
245 executionFinished();
246 currentThread.setName(oldName);
247 }
248 }
249
250 private void finishLoading(LoadResult result) {
251 Set<ICachedLoaderListener> listeners;
252 synchronized (inProgress) {
253 try {
254 listeners = inProgress.remove(getUrl().toString());
255 } catch (IOException e) {
256 listeners = null;
257 Logging.trace(e);
258 }
259 }
260 if (listeners == null) {
261 Logging.warn("Listener not found for URL: {0}. Listener not notified!", getUrlNoException());
262 return;
263 }
264 for (ICachedLoaderListener l: listeners) {
265 l.loadingFinished(cacheData, attributes, result);
266 }
267 }
268
269 protected boolean isCacheElementValid() {
270 long expires = attributes.getExpirationTime();
271
272 // check by expire date set by server
273 if (expires != 0L) {
274 // put a limit to the expire time (some servers send a value
275 // that is too large)
276 expires = Math.min(expires, attributes.getCreateTime() + Math.max(EXPIRE_TIME_SERVER_LIMIT, minimumExpiryTime));
277 if (now > expires) {
278 Logging.debug("JCS - Object {0} has expired -> valid to {1}, now is: {2}",
279 getUrlNoException(), Long.toString(expires), Long.toString(now));
280 return false;
281 }
282 } else if (attributes.getLastModification() > 0 &&
283 now - attributes.getLastModification() > Math.max(DEFAULT_EXPIRE_TIME, minimumExpiryTime)) {
284 // check by file modification date
285 Logging.debug("JCS - Object has expired, maximum file age reached {0}", getUrlNoException());
286 return false;
287 } else if (now - attributes.getCreateTime() > Math.max(DEFAULT_EXPIRE_TIME, minimumExpiryTime)) {
288 Logging.debug("JCS - Object has expired, maximum time since object creation reached {0}", getUrlNoException());
289 return false;
290 }
291 return true;
292 }
293
294 /**
295 * @return true if object was successfully downloaded, false, if there was a loading failure
296 */
297 private boolean loadObject() {
298 if (attributes == null) {
299 attributes = new CacheEntryAttributes();
300 }
301 try {
302 // if we have object in cache, and host doesn't support If-Modified-Since nor If-None-Match
303 // then just use HEAD request and check returned values
304 if (isObjectLoadable() &&
305 Boolean.TRUE.equals(useHead.get(getServerKey())) &&
306 isCacheValidUsingHead()) {
307 Logging.debug("JCS - cache entry verified using HEAD request: {0}", getUrl());
308 return true;
309 }
310
311 Logging.debug("JCS - starting HttpClient GET request for URL: {0}", getUrl());
312 final HttpClient request = getRequest("GET", true);
313
314 if (isObjectLoadable() &&
315 (now - attributes.getLastModification()) <= ABSOLUTE_EXPIRE_TIME_LIMIT) {
316 request.setIfModifiedSince(attributes.getLastModification());
317 }
318 if (isObjectLoadable() && attributes.getEtag() != null) {
319 request.setHeader("If-None-Match", attributes.getEtag());
320 }
321
322 final HttpClient.Response urlConn = request.connect();
323
324 if (urlConn.getResponseCode() == 304) {
325 // If isModifiedSince or If-None-Match has been set
326 // and the server answers with a HTTP 304 = "Not Modified"
327 Logging.debug("JCS - If-Modified-Since/ETag test: local version is up to date: {0}", getUrl());
328 // update cache attributes
329 attributes = parseHeaders(urlConn);
330 cache.put(getCacheKey(), cacheData, attributes);
331 return true;
332 } else if (isObjectLoadable() // we have an object in cache, but we haven't received 304 response code
333 && (
334 (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getHeaderField("ETag"))) ||
335 attributes.getLastModification() == urlConn.getLastModified())
336 ) {
337 // we sent ETag or If-Modified-Since, but didn't get 304 response code
338 // for further requests - use HEAD
339 String serverKey = getServerKey();
340 Logging.info("JCS - Host: {0} found not to return 304 codes for If-Modified-Since or If-None-Match headers",
341 serverKey);
342 useHead.put(serverKey, Boolean.TRUE);
343 }
344
345 attributes = parseHeaders(urlConn);
346
347 for (int i = 0; i < 5; ++i) {
348 if (urlConn.getResponseCode() == HttpURLConnection.HTTP_UNAVAILABLE) {
349 Thread.sleep(5000L+new SecureRandom().nextInt(5000));
350 continue;
351 }
352
353 attributes.setResponseCode(urlConn.getResponseCode());
354 byte[] raw;
355 if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
356 raw = Utils.readBytesFromStream(urlConn.getContent());
357 } else {
358 raw = new byte[]{};
359 try {
360 String data = urlConn.fetchContent();
361 if (!data.isEmpty()) {
362 Matcher m = HttpClient.getTomcatErrorMatcher(data);
363 if (m.matches()) {
364 attributes.setErrorMessage(m.group(1).replace("'", "''"));
365 }
366 }
367 } catch (IOException e) {
368 Logging.warn(e);
369 }
370 }
371
372 if (isResponseLoadable(urlConn.getHeaderFields(), urlConn.getResponseCode(), raw)) {
373 // we need to check cacheEmpty, so for cases, when data is returned, but we want to store
374 // as empty (eg. empty tile images) to save some space
375 cacheData = createCacheEntry(raw);
376 cache.put(getCacheKey(), cacheData, attributes);
377 Logging.debug("JCS - downloaded key: {0}, length: {1}, url: {2}",
378 getCacheKey(), raw.length, getUrl());
379 return true;
380 } else if (cacheAsEmpty()) {
381 cacheData = createCacheEntry(new byte[]{});
382 cache.put(getCacheKey(), cacheData, attributes);
383 Logging.debug("JCS - Caching empty object {0}", getUrl());
384 return true;
385 } else {
386 Logging.debug("JCS - failure during load - reponse is not loadable nor cached as empty");
387 return false;
388 }
389 }
390 } catch (FileNotFoundException e) {
391 Logging.debug("JCS - Caching empty object as server returned 404 for: {0}", getUrlNoException());
392 attributes.setResponseCode(404);
393 attributes.setError(e);
394 boolean doCache = isResponseLoadable(null, 404, null) || cacheAsEmpty();
395 if (doCache) {
396 cacheData = createCacheEntry(new byte[]{});
397 cache.put(getCacheKey(), cacheData, attributes);
398 }
399 return doCache;
400 } catch (IOException e) {
401 Logging.debug("JCS - IOException during communication with server for: {0}", getUrlNoException());
402 if (isObjectLoadable()) {
403 return true;
404 } else {
405 attributes.setError(e);
406 attributes.setResponseCode(599); // set dummy error code, greater than 500 so it will be not cached
407 return false;
408 }
409
410 } catch (InterruptedException e) {
411 attributes.setError(e);
412 Logging.logWithStackTrace(Logging.LEVEL_WARN, e, "JCS - Exception during download {0}", getUrlNoException());
413 Thread.currentThread().interrupt();
414 }
415 Logging.warn("JCS - Silent failure during download: {0}", getUrlNoException());
416 return false;
417 }
418
419 /**
420 * Check if the object is loadable. This means, if the data will be parsed, and if this response
421 * will finish as successful retrieve.
422 *
423 * This simple implementation doesn't load empty response, nor client (4xx) and server (5xx) errors
424 *
425 * @param headerFields headers sent by server
426 * @param responseCode http status code
427 * @param raw data read from server
428 * @return true if object should be cached and returned to listener
429 */
430 protected boolean isResponseLoadable(Map<String, List<String>> headerFields, int responseCode, byte[] raw) {
431 return raw != null && raw.length != 0 && responseCode < 400;
432 }
433
434 protected abstract V createCacheEntry(byte[] content);
435
436 protected CacheEntryAttributes parseHeaders(HttpClient.Response urlConn) {
437 CacheEntryAttributes ret = new CacheEntryAttributes();
438
439 /*
440 * according to https://www.ietf.org/rfc/rfc2616.txt Cache-Control takes precedence over max-age
441 * max-age is for private caches, s-max-age is for shared caches. We take any value that is larger
442 */
443 Long expiration = 0L;
444 String cacheControl = urlConn.getHeaderField("Cache-Control");
445 if (cacheControl != null) {
446 for (String token: cacheControl.split(",")) {
447 try {
448 if (token.startsWith("max-age=")) {
449 expiration = Math.max(expiration,
450 TimeUnit.SECONDS.toMillis(Long.parseLong(token.substring("max-age=".length())))
451 + System.currentTimeMillis()
452 );
453 }
454 if (token.startsWith("s-max-age=")) {
455 expiration = Math.max(expiration,
456 TimeUnit.SECONDS.toMillis(Long.parseLong(token.substring("s-max-age=".length())))
457 + System.currentTimeMillis()
458 );
459 }
460 } catch (NumberFormatException e) {
461 // ignore malformed Cache-Control headers
462 Logging.trace(e);
463 }
464 }
465 }
466
467 if (expiration.equals(0L)) {
468 expiration = urlConn.getExpiration();
469 }
470
471 // if nothing is found - set default
472 if (expiration.equals(0L)) {
473 expiration = System.currentTimeMillis() + DEFAULT_EXPIRE_TIME;
474 }
475
476 ret.setExpirationTime(Math.max(minimumExpiryTime + System.currentTimeMillis(), expiration));
477 ret.setLastModification(now);
478 ret.setEtag(urlConn.getHeaderField("ETag"));
479
480 return ret;
481 }
482
483 private HttpClient getRequest(String requestMethod, boolean noCache) throws IOException {
484 final HttpClient urlConn = HttpClient.create(getUrl(), requestMethod);
485 urlConn.setAccept("text/html, image/png, image/jpeg, image/gif, */*");
486 urlConn.setReadTimeout(readTimeout); // 30 seconds read timeout
487 urlConn.setConnectTimeout(connectTimeout);
488 if (headers != null) {
489 urlConn.setHeaders(headers);
490 }
491
492 if (force || noCache) {
493 urlConn.useCache(false);
494 }
495 return urlConn;
496 }
497
498 private boolean isCacheValidUsingHead() throws IOException {
499 final HttpClient.Response urlConn = getRequest("HEAD", false).connect();
500 long lastModified = urlConn.getLastModified();
501 boolean ret = (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getHeaderField("ETag"))) ||
502 (lastModified != 0 && lastModified <= attributes.getLastModification());
503 if (ret) {
504 // update attributes
505 attributes = parseHeaders(urlConn);
506 cache.put(getCacheKey(), cacheData, attributes);
507 }
508 return ret;
509 }
510
511 /**
512 * TODO: move to JobFactory
513 * cancels all outstanding tasks in the queue.
514 */
515 public void cancelOutstandingTasks() {
516 for (Runnable r: downloadJobExecutor.getQueue()) {
517 if (downloadJobExecutor.remove(r) && r instanceof JCSCachedTileLoaderJob) {
518 ((JCSCachedTileLoaderJob<?, ?>) r).handleJobCancellation();
519 }
520 }
521 }
522
523 /**
524 * Sets a job, that will be run, when job will finish execution
525 * @param runnable that will be executed
526 */
527 public void setFinishedTask(Runnable runnable) {
528 this.finishTask = runnable;
529
530 }
531
532 /**
533 * Marks this job as canceled
534 */
535 public void handleJobCancellation() {
536 finishLoading(LoadResult.CANCELED);
537 }
538
539 private URL getUrlNoException() {
540 try {
541 return getUrl();
542 } catch (IOException e) {
543 return null;
544 }
545 }
546}
Note: See TracBrowser for help on using the repository browser.