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

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

see #13416 - add parameter validation do detect NPE origin

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