[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] [metrics-web/master] Don't attempt to delete old graphs from the cache.



Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Wed, 6 Oct 2010 14:31:17 +0200
Subject: Don't attempt to delete old graphs from the cache.
Commit: 0f6c66eb90076165b2a9583423dfd33feb5376af

Tomcat shouldn't have permissions to delete old graph files from disk.
Instead, if a cached graph gets too old, we ask Rserve to overwrite it
with a new graph.

We could delete old graphs using a shell script, but it takes a long time
to fill a disk with graph files of about 25 KiB.
---
 etc/ernie.properties                              |    6 -
 src/org/torproject/ernie/web/GraphController.java |  104 +--------------------
 2 files changed, 5 insertions(+), 105 deletions(-)

diff --git a/etc/ernie.properties b/etc/ernie.properties
index 1f8cf92..3b303e0 100644
--- a/etc/ernie.properties
+++ b/etc/ernie.properties
@@ -7,12 +7,6 @@ rserve.host=localhost
 # The Rserve port
 rserve.port=6311
 
-# How many graphs to keep cached at most
-max.cache.size=5000
-
-# How many graphs to keep cached at least
-min.cache.size=2500
-
 # Maximum age in seconds of a graph in the cache
 max.cache.age=21600
 
diff --git a/src/org/torproject/ernie/web/GraphController.java b/src/org/torproject/ernie/web/GraphController.java
index 7d30b4d..32b7f4f 100644
--- a/src/org/torproject/ernie/web/GraphController.java
+++ b/src/org/torproject/ernie/web/GraphController.java
@@ -20,43 +20,28 @@ public class GraphController {
 
   /* Some parameters for our cache of graph images. */
   private String cachedGraphsDirectory;
-  private int maxCacheSize;
-  private int minCacheSize;
   private long maxCacheAge;
-  private int currentCacheSize;
-  private long oldestGraph;
 
   protected GraphController ()  {
 
     /* Read properties from property file. */
     ErnieProperties props = new ErnieProperties();
     this.cachedGraphsDirectory = props.getProperty("cached.graphs.dir");
-    this.maxCacheSize = props.getInt("max.cache.size");
-    this.minCacheSize = props.getInt("min.cache.size");
     this.maxCacheAge = (long) props.getInt("max.cache.age");
     this.rserveHost = props.getProperty("rserve.host");
     this.rservePort = props.getInt("rserve.port");
-
-    /* Clean up cache on startup. */
-    this.cleanUpCache();
   }
 
   /* Generate a graph using the given R query that has a placeholder for
    * the absolute path to the image to be created. */
   public byte[] generateGraph(String rQuery, String imageFilename) {
 
-    /* Check if we need to clean up the cache first, or we might give
-     * someone an old grpah. */
-    if (this.currentCacheSize > this.maxCacheSize ||
-        (this.currentCacheSize > 0 && System.currentTimeMillis()
-        - this.oldestGraph > this.maxCacheAge * 1000L)) {
-      this.cleanUpCache();
-    }
-
     /* See if we need to generate this graph. */
     File imageFile = new File(this.cachedGraphsDirectory + "/"
         + imageFilename);
-    if (!imageFile.exists()) {
+    long now = System.currentTimeMillis();
+    if (!imageFile.exists() || imageFile.lastModified() < now
+        - this.maxCacheAge * 1000L) {
 
       /* We do. Update the R query to contain the absolute path to the file
        * to be generated, create a connection to Rserve, run the R query,
@@ -71,12 +56,10 @@ public class GraphController {
       }
 
       /* Check that we really just generated the file */
-      if (!imageFile.exists()) {
+      if (!imageFile.exists() || imageFile.lastModified() < now
+          - this.maxCacheAge * 1000L) {
         return null;
       }
-
-      /* Update our graph counter. */
-      this.currentCacheSize++;
     }
 
     /* Read the image from disk and write it to a byte array. */
@@ -98,82 +81,5 @@ public class GraphController {
     /* Return the graph bytes. */
     return result;
   }
-
-  /* Clean up graph cache by removing all graphs older than maxCacheAge
-   * and then the oldest graphs until we have minCacheSize graphs left.
-   * Also update currentCacheSize and oldestGraph. */
-  public void cleanUpCache() {
-BufferedWriter out = null;
-try {
-out = new BufferedWriter(new FileWriter("/tmp/graphcache.log"));
-out.write("cleaning up cache\n");
-out.flush();
-} catch (IOException e) {}
-    /* Check if the cache is empty first. */
-    File[] filesInCache = new File(this.cachedGraphsDirectory).
-        listFiles();
-    if (filesInCache.length == 0) {
-      this.currentCacheSize = 0;
-      this.oldestGraph = System.currentTimeMillis();
-try {
-if (out != null) {
-out.write("cache is empty. exiting\n");
-out.close();
-}
-} catch (IOException e) {}
-      return;
-    }
-
-    /* Sort graphs in cache by the time they were last modified. */
-    List<File> graphsByLastModified = new LinkedList<File>(
-        Arrays.asList(filesInCache));
-    Collections.sort(graphsByLastModified, new Comparator<File>() {
-      public int compare(File a, File b) {
-        return a.lastModified() < b.lastModified() ? -1 :
-            a.lastModified() > b.lastModified() ? 1 : 0;
-      }
-    });
-
-    /* Delete the graphs that are either older than maxCacheAge and then
-     * as many graphs as necessary to shrink to minCacheSize graphs. */
-    long cutOffTime = System.currentTimeMillis()
-        - this.maxCacheAge * 1000L;
-try {
-if (out != null) {
-out.write("cut off time is " + cutOffTime + "\n");
-out.flush();
-}
-} catch (IOException e) {}
-    while (!graphsByLastModified.isEmpty()) {
-      File oldestGraphInList = graphsByLastModified.remove(0);
-      if (oldestGraphInList.lastModified() >= cutOffTime &&
-          graphsByLastModified.size() < this.minCacheSize) {
-        break;
-      }
-      boolean deleted = oldestGraphInList.delete();
-try {
-if (out != null) {
-out.write("deleting " + oldestGraphInList.getName() + " was " + (deleted ? "" : "NOT") + " successful.\n");
-out.flush();
-}
-} catch (IOException e) {}
-    }
-
-    /* Update currentCacheSize and oldestGraph that we need to decide when
-     * we should next clean up the graph cache. */
-    this.currentCacheSize = graphsByLastModified.size();
-    if (!graphsByLastModified.isEmpty()) {
-      this.oldestGraph = graphsByLastModified.get(0).lastModified();
-    } else {
-      this.oldestGraph = System.currentTimeMillis();
-    }
-try {
-if (out != null) {
-out.write("now we have " + this.currentCacheSize + " graphs in our cache, the oldest one being from " + this.oldestGraph + "\n");
-out.close();
-}
-} catch (IOException e) {}
-
-  }
 }
 
-- 
1.7.1