You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by th...@apache.org on 2015/04/24 16:10:07 UTC

svn commit: r1675865 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/PersistentCache.java test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java

Author: thomasm
Date: Fri Apr 24 14:10:06 2015
New Revision: 1675865

URL: http://svn.apache.org/r1675865
Log:
OAK-2796 Persistent cache: old files not removed sometimes

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/PersistentCache.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/PersistentCache.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/PersistentCache.java?rev=1675865&r1=1675864&r2=1675865&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/PersistentCache.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/PersistentCache.java Fri Apr 24 14:10:06 2015
@@ -65,6 +65,7 @@ public class PersistentCache {
     private long maxBinaryEntry = 1024 * 1024;
     private int autoCompact = 50;
     private boolean appendOnly;
+    private boolean manualCommit;
 
     public PersistentCache(String url) {
         LOG.info("start version 1");
@@ -98,6 +99,8 @@ public class PersistentCache {
                 autoCompact = Integer.parseInt(p.split("=")[1]);
             } else if (p.equals("appendOnly")) {
                 appendOnly = true;
+            } else if (p.equals("manualCommit")) {
+                manualCommit = true;
             }
         }
         this.directory = dir;
@@ -137,7 +140,15 @@ public class PersistentCache {
             }
         }
         while (generations.size() > 2) {
-            generations.remove(generations.last());
+            Integer oldest = generations.first();
+            File oldFile = new File(getFileName(oldest));
+            if (!oldFile.canWrite()) {
+                LOG.info("Ignoring old, read-only generation " + oldFile.getAbsolutePath());
+            } else {
+                LOG.info("Removing old generation " + oldFile.getAbsolutePath());
+                oldFile.delete();
+            }
+            generations.remove(oldest);
         }
         readGeneration = generations.size() > 1 ? generations.first() : -1;
         writeGeneration = generations.size() > 0 ? generations.last() : 0;
@@ -170,6 +181,9 @@ public class PersistentCache {
                     if (compress) {
                         builder.compress();
                     }
+                    if (manualCommit) {
+                        builder.autoCommitDisabled();
+                    }
                     if (fileName != null) {
                         builder.fileName(fileName);
                     }
@@ -203,18 +217,26 @@ public class PersistentCache {
                 if (store == null) {
                     return;
                 }
+                boolean compact = compactOnClose;
                 try {
-                    boolean compact = compactOnClose;
                     if (store.getFileStore().isReadOnly()) {
                         compact = false;
                     }
+                    // clear the interrupted flag, if set
+                    Thread.interrupted();
                     store.close();
-                    if (compact) {
+                } catch (Exception e) {
+                    LOG.debug("Could not close the store", e);
+                    LOG.warn("Could not close the store: " + e);
+                    store.closeImmediately();
+                }
+                if (compact) {
+                    try {
                         MVStoreTool.compact(fileName, true);
+                    } catch (Exception e) {
+                        LOG.debug("Could not compact the store", e);
+                        LOG.warn("Could not compact the store: " + e);
                     }
-                } catch (Exception e) {
-                    LOG.debug("Could not close or compact the store", e);
-                    LOG.warn("Could not close or compact the store: " + e);
                 }
                 store = null;
             }

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java?rev=1675865&r1=1675864&r2=1675865&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java Fri Apr 24 14:10:06 2015
@@ -20,6 +20,7 @@ package org.apache.jackrabbit.oak.plugin
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
@@ -33,6 +34,34 @@ import org.junit.Test;
 public class CacheTest {
 
     @Test
+    public void closeAlways() throws Exception {
+        FileUtils.deleteDirectory(new File("target/cacheTest"));
+        PersistentCache cache = new PersistentCache("target/cacheTest,manualCommit");
+        CacheMap<String, String> map = cache.openMap(0, "test", null);
+        // break the map by calling interrupt
+        Thread.currentThread().interrupt();
+        map.put("hello", "world");
+        cache.close();
+        assertFalse(Thread.interrupted());
+    }
+
+    @Test
+    public void deleteOldAtStartup() throws Exception {
+        FileUtils.deleteDirectory(new File("target/cacheTest"));
+        new File("target/cacheTest").mkdirs();
+        new File("target/cacheTest/cache-0.data").createNewFile();
+        new File("target/cacheTest/cache-1.data").createNewFile();
+        new File("target/cacheTest/cache-2.data").createNewFile();
+        new File("target/cacheTest/cache-3.data").createNewFile();
+        PersistentCache cache = new PersistentCache("target/cacheTest");
+        cache.close();
+        assertFalse(new File("target/cacheTest/cache-0.data").exists());
+        assertFalse(new File("target/cacheTest/cache-1.data").exists());
+        assertTrue(new File("target/cacheTest/cache-2.data").exists());
+        assertTrue(new File("target/cacheTest/cache-3.data").exists());
+    }
+
+    @Test
     public void test() throws Exception {
         FileUtils.deleteDirectory(new File("target/cacheTest"));
         PersistentCache cache = new PersistentCache("target/cacheTest,size=1,-compress");