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

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

Better error reporting in JCS, checkstyle fixes

File size: 20.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.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 = Long.MAX_VALUE; // unlimited
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) {
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 return;
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 ensureCacheElement();
176 if (!force && cacheElement != null && isCacheElementValid() && isObjectLoadable()) {
177 // we got something in cache, and it's valid, so lets return it
178 log.log(Level.FINE, "JCS - Returning object from cache: {0}", getCacheKey());
179 finishLoading(LoadResult.SUCCESS);
180 return;
181 }
182 // object not in cache, so submit work to separate thread
183 downloadJobExecutor.execute(this);
184 }
185 }
186
187 /**
188 * This method is run when job has finished
189 */
190 protected void executionFinished() {
191 if (finishTask != null) {
192 finishTask.run();
193 }
194 }
195
196 /**
197 *
198 * @return checks if object from cache has sufficient data to be returned
199 */
200 protected boolean isObjectLoadable() {
201 if (cacheData == null) {
202 return false;
203 }
204 byte[] content = cacheData.getContent();
205 return content != null && content.length > 0;
206 }
207
208 /**
209 * Simple implementation. All errors should be cached as empty. Though some JDK (JDK8 on Windows for example)
210 * doesn't return 4xx error codes, instead they do throw an FileNotFoundException or IOException
211 *
212 * @return true if we should put empty object into cache, regardless of what remote resource has returned
213 */
214 protected boolean cacheAsEmpty() {
215 return attributes.getResponseCode() < 500;
216 }
217
218 /**
219 * @return key under which discovered server settings will be kept
220 */
221 protected String getServerKey() {
222 return getUrl().getHost();
223 }
224
225 @Override
226 public void run() {
227 final Thread currentThread = Thread.currentThread();
228 final String oldName = currentThread.getName();
229 currentThread.setName("JCS Downloading: " + getUrl());
230 try {
231 // try to load object from remote resource
232 if (loadObject()) {
233 finishLoading(LoadResult.SUCCESS);
234 } else {
235 // if loading failed - check if we can return stale entry
236 if (isObjectLoadable()) {
237 // try to get stale entry in cache
238 finishLoading(LoadResult.SUCCESS);
239 log.log(Level.FINE, "JCS - found stale object in cache: {0}", getUrl());
240 } else {
241 // failed completely
242 finishLoading(LoadResult.FAILURE);
243 }
244 }
245 } finally {
246 executionFinished();
247 currentThread.setName(oldName);
248 }
249 }
250
251 private void finishLoading(LoadResult result) {
252 Set<ICachedLoaderListener> listeners = null;
253 synchronized (inProgress) {
254 listeners = inProgress.remove(getUrl().toString());
255 }
256 if (listeners == null) {
257 log.log(Level.WARNING, "Listener not found for URL: {0}. Listener not notified!", getUrl());
258 return;
259 }
260 for (ICachedLoaderListener l: listeners) {
261 l.loadingFinished(cacheData, attributes, result);
262 }
263 }
264
265 private boolean isCacheElementValid() {
266 long expires = attributes.getExpirationTime();
267
268 // check by expire date set by server
269 if (expires != 0L) {
270 // put a limit to the expire time (some servers send a value
271 // that is too large)
272 expires = Math.min(expires, attributes.getCreateTime() + EXPIRE_TIME_SERVER_LIMIT);
273 if (now > expires) {
274 log.log(Level.FINE, "JCS - Object {0} has expired -> valid to {1}, now is: {2}",
275 new Object[]{getUrl(), Long.toString(expires), Long.toString(now)});
276 return false;
277 }
278 } else {
279 // check by file modification date
280 if (now - attributes.getLastModification() > DEFAULT_EXPIRE_TIME) {
281 log.log(Level.FINE, "JCS - Object has expired, maximum file age reached {0}", getUrl());
282 return false;
283 }
284 }
285 return true;
286 }
287
288 /**
289 * @return true if object was successfully downloaded, false, if there was a loading failure
290 */
291
292 private boolean loadObject() {
293 if (attributes == null) {
294 attributes = new CacheEntryAttributes();
295 }
296 try {
297 // if we have object in cache, and host doesn't support If-Modified-Since nor If-None-Match
298 // then just use HEAD request and check returned values
299 if (isObjectLoadable() &&
300 Boolean.TRUE.equals(useHead.get(getServerKey())) &&
301 isCacheValidUsingHead()) {
302 log.log(Level.FINE, "JCS - cache entry verified using HEAD request: {0}", getUrl());
303 return true;
304 }
305
306 HttpURLConnection urlConn = getURLConnection();
307
308 if (isObjectLoadable() &&
309 (now - attributes.getLastModification()) <= ABSOLUTE_EXPIRE_TIME_LIMIT) {
310 urlConn.setIfModifiedSince(attributes.getLastModification());
311 }
312 if (isObjectLoadable() && attributes.getEtag() != null) {
313 urlConn.addRequestProperty("If-None-Match", attributes.getEtag());
314 }
315 if (urlConn.getResponseCode() == 304) {
316 // If isModifiedSince or If-None-Match has been set
317 // and the server answers with a HTTP 304 = "Not Modified"
318 log.log(Level.FINE, "JCS - IfModifiedSince/Etag test: local version is up to date: {0}", getUrl());
319 return true;
320 } else if (isObjectLoadable() // we have an object in cache, but we haven't received 304 resposne code
321 && (
322 (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getRequestProperty("ETag"))) ||
323 attributes.getLastModification() == urlConn.getLastModified())
324 ) {
325 // we sent ETag or If-Modified-Since, but didn't get 304 response code
326 // for further requests - use HEAD
327 String serverKey = getServerKey();
328 log.log(Level.INFO, "JCS - Host: {0} found not to return 304 codes for If-Modifed-Since or If-None-Match headers",
329 serverKey);
330 useHead.put(serverKey, Boolean.TRUE);
331 }
332
333
334 attributes = parseHeaders(urlConn);
335
336 for (int i = 0; i < 5; ++i) {
337 if (urlConn.getResponseCode() == 503) {
338 Thread.sleep(5000+(new Random()).nextInt(5000));
339 continue;
340 }
341
342 attributes.setResponseCode(urlConn.getResponseCode());
343 byte[] raw;
344 if (urlConn.getResponseCode() == 200) {
345 raw = Utils.readBytesFromStream(urlConn.getInputStream());
346 } else {
347 raw = new byte[]{};
348 }
349
350 if (isResponseLoadable(urlConn.getHeaderFields(), urlConn.getResponseCode(), raw)) {
351 // we need to check cacheEmpty, so for cases, when data is returned, but we want to store
352 // as empty (eg. empty tile images) to save some space
353 cacheData = createCacheEntry(raw);
354 cache.put(getCacheKey(), cacheData, attributes);
355 log.log(Level.FINE, "JCS - downloaded key: {0}, length: {1}, url: {2}",
356 new Object[] {getCacheKey(), raw.length, getUrl()});
357 return true;
358 } else if (cacheAsEmpty()) {
359 cacheData = createCacheEntry(new byte[]{});
360 cache.put(getCacheKey(), cacheData, attributes);
361 log.log(Level.FINE, "JCS - Caching empty object {0}", getUrl());
362 return true;
363 } else {
364 log.log(Level.FINE, "JCS - failure during load - reponse is not loadable nor cached as empty");
365 return false;
366 }
367 }
368 } catch (FileNotFoundException e) {
369 log.log(Level.FINE, "JCS - Caching empty object as server returned 404 for: {0}", getUrl());
370 attributes.setResponseCode(404);
371 boolean doCache = isResponseLoadable(null, 404, null) || cacheAsEmpty();
372 if (doCache) {
373 cacheData = createCacheEntry(new byte[]{});
374 cache.put(getCacheKey(), cacheData, attributes);
375 }
376 return doCache;
377 } catch (IOException e) {
378 log.log(Level.FINE, "JCS - IOExecption during communication with server for: {0}", getUrl());
379
380 attributes.setResponseCode(499); // set dummy error code
381 boolean doCache = isResponseLoadable(null, 499, null) || cacheAsEmpty(); //generic 499 error code returned
382 if (doCache) {
383 cacheData = createCacheEntry(new byte[]{});
384 cache.put(getCacheKey(), createCacheEntry(new byte[]{}), attributes);
385 }
386 return doCache;
387 } catch (Exception e) {
388 log.log(Level.WARNING, "JCS - Exception during download {0}", getUrl());
389 Main.warn(e);
390 }
391 log.log(Level.WARNING, "JCS - Silent failure during download: {0}", getUrl());
392 return false;
393
394 }
395
396 /**
397 * Check if the object is loadable. This means, if the data will be parsed, and if this response
398 * will finish as successful retrieve.
399 *
400 * This simple implementation doesn't load empty response, nor client (4xx) and server (5xx) errors
401 *
402 * @param headerFields headers sent by server
403 * @param responseCode http status code
404 * @param raw data read from server
405 * @return true if object should be cached and returned to listener
406 */
407 protected boolean isResponseLoadable(Map<String, List<String>> headerFields, int responseCode, byte[] raw) {
408 if (raw == null || raw.length == 0 || responseCode >= 400) {
409 return false;
410 }
411 return true;
412 }
413
414 protected abstract V createCacheEntry(byte[] content);
415
416 protected CacheEntryAttributes parseHeaders(URLConnection urlConn) {
417 CacheEntryAttributes ret = new CacheEntryAttributes();
418
419 Long lng = urlConn.getExpiration();
420 if (lng.equals(0L)) {
421 try {
422 String str = urlConn.getHeaderField("Cache-Control");
423 if (str != null) {
424 for (String token: str.split(",")) {
425 if (token.startsWith("max-age=")) {
426 lng = Long.parseLong(token.substring(8)) * 1000 +
427 System.currentTimeMillis();
428 }
429 }
430 }
431 } catch (NumberFormatException e) {
432 // ignore malformed Cache-Control headers
433 if (Main.isTraceEnabled()) {
434 Main.trace(e.getMessage());
435 }
436 }
437 }
438
439 ret.setExpirationTime(lng);
440 ret.setLastModification(now);
441 ret.setEtag(urlConn.getHeaderField("ETag"));
442 return ret;
443 }
444
445 private HttpURLConnection getURLConnection() throws IOException {
446 HttpURLConnection urlConn = (HttpURLConnection) getUrl().openConnection();
447 urlConn.setRequestProperty("Accept", "text/html, image/png, image/jpeg, image/gif, */*");
448 urlConn.setReadTimeout(readTimeout); // 30 seconds read timeout
449 urlConn.setConnectTimeout(connectTimeout);
450 if (headers != null) {
451 for (Map.Entry<String, String> e: headers.entrySet()) {
452 urlConn.setRequestProperty(e.getKey(), e.getValue());
453 }
454 }
455 if (force) {
456 urlConn.setUseCaches(false);
457 }
458 return urlConn;
459 }
460
461 private boolean isCacheValidUsingHead() throws IOException {
462 HttpURLConnection urlConn = getURLConnection();
463 urlConn.setRequestMethod("HEAD");
464 long lastModified = urlConn.getLastModified();
465 return (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getRequestProperty("ETag"))) ||
466 (lastModified != 0 && lastModified <= attributes.getLastModification());
467 }
468
469 /**
470 * TODO: move to JobFactory
471 * cancels all outstanding tasks in the queue.
472 */
473 public void cancelOutstandingTasks() {
474 for (Runnable r: downloadJobExecutor.getQueue()) {
475 if (downloadJobExecutor.remove(r) && r instanceof JCSCachedTileLoaderJob) {
476 ((JCSCachedTileLoaderJob<?, ?>) r).handleJobCancellation();
477 }
478 }
479 }
480
481 /**
482 * Sets a job, that will be run, when job will finish execution
483 * @param runnable that will be executed
484 */
485 public void setFinishedTask(Runnable runnable) {
486 this.finishTask = runnable;
487
488 }
489
490 /**
491 * Marks this job as canceled
492 */
493 public void handleJobCancellation() {
494 finishLoading(LoadResult.CANCELED);
495 }
496}
Note: See TracBrowser for help on using the repository browser.