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

Last change on this file since 8485 was 8485, checked in by wiktorn, 9 years ago

Addresses: #11548 - do not assume, that we work with HTTP based connection for fetching from remote resources

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