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

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

sonar - squid:S1166 - Exception handlers should preserve the original exceptions

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