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

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

Bing fixes.

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