Checks whether or not it's possible to re-serve this query from disk. @param query The query to serve. @param end_time The end time on the query (32-bit unsigned int, seconds). @param max_age The maximum time (in seconds) we wanna allow clients to cache the result in case of a cache hit. @param base
(final HttpQuery query,
final long end_time,
final int max_age,
final String basepath)
| 449 | * the query needs to be processed). |
| 450 | */ |
| 451 | private boolean isDiskCacheHit(final HttpQuery query, |
| 452 | final long end_time, |
| 453 | final int max_age, |
| 454 | final String basepath) throws IOException { |
| 455 | final String cachepath = basepath + (query.hasQueryStringParam("ascii") |
| 456 | ? ".txt" : ".png"); |
| 457 | final File cachedfile = new File(cachepath); |
| 458 | if (cachedfile.exists()) { |
| 459 | final long bytes = cachedfile.length(); |
| 460 | if (bytes < 21) { // Minimum possible size for a PNG: 21 bytes. |
| 461 | // For .txt files, <21 bytes is almost impossible. |
| 462 | logWarn(query, "Cached " + cachepath + " is too small (" |
| 463 | + bytes + " bytes) to be valid. Ignoring it."); |
| 464 | return false; |
| 465 | } |
| 466 | if (staleCacheFile(query, end_time, max_age, cachedfile)) { |
| 467 | return false; |
| 468 | } |
| 469 | if (query.hasQueryStringParam("json")) { |
| 470 | HashMap<String, Object> map = loadCachedJson(query, end_time, |
| 471 | max_age, basepath); |
| 472 | if (map == null) { |
| 473 | map = new HashMap<String, Object>(); |
| 474 | } |
| 475 | map.put("timing", query.processingTimeMillis()); |
| 476 | map.put("cachehit", "disk"); |
| 477 | query.sendReply(JSON.serializeToBytes(map)); |
| 478 | } else if (query.hasQueryStringParam("png") |
| 479 | || query.hasQueryStringParam("ascii")) { |
| 480 | query.sendFile(cachepath, max_age); |
| 481 | } else { |
| 482 | query.sendReply(HttpQuery.makePage("TSDB Query", "Your graph is ready", |
| 483 | "<img src=\"" + query.request().getUri() + "&png\"/><br/>" |
| 484 | + "<small>(served from disk cache)</small>")); |
| 485 | } |
| 486 | graphs_diskcache_hit.incrementAndGet(); |
| 487 | return true; |
| 488 | } |
| 489 | // We didn't find an image. Do a negative cache check. If we've seen |
| 490 | // this query before but there was no result, we at least wrote the JSON. |
| 491 | final HashMap<String, Object> map = loadCachedJson(query, end_time, |
| 492 | max_age, basepath); |
| 493 | // If we don't have a JSON file it's a complete cache miss. If we have |
| 494 | // one, and it says 0 data points were plotted, it's a negative cache hit. |
| 495 | if (map == null || !map.containsKey("plotted") || |
| 496 | ((Integer)map.get("plotted")) == 0) { |
| 497 | return false; |
| 498 | } |
| 499 | if (query.hasQueryStringParam("json")) { |
| 500 | map.put("timing", query.processingTimeMillis()); |
| 501 | map.put("cachehit", "disk"); |
| 502 | query.sendReply(JSON.serializeToBytes(map)); |
| 503 | } else if (query.hasQueryStringParam("png")) { |
| 504 | query.sendReply(" "); // Send back an empty response... |
| 505 | } else { |
| 506 | query.sendReply(HttpQuery.makePage("TSDB Query", "No results", |
| 507 | "Sorry, your query didn't return anything.<br/>" |
| 508 | + "<small>(served from disk cache)</small>")); |
no test coverage detected