Author: thomasm
Date: Fri May 22 11:44:38 2015
New Revision: 1681077

URL: http://svn.apache.org/r1681077
Log:
OAK-2837 Persistent cache: avoid repeated log message after closing

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

Modified: 
jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/PersistentCache.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/PersistentCache.java?rev=1681077&r1=1681076&r2=1681077&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/PersistentCache.java
 (original)
+++ 
jackrabbit/oak/branches/1.2/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/PersistentCache.java
 Fri May 22 11:44:38 2015
@@ -66,6 +66,8 @@ public class PersistentCache {
     private int autoCompact = 50;
     private boolean appendOnly;
     private boolean manualCommit;
+    
+    private int exceptionCount;
 
     public PersistentCache(String url) {
         LOG.info("start version 1");
@@ -201,6 +203,7 @@ public class PersistentCache {
                     builder.backgroundExceptionHandler(new 
Thread.UncaughtExceptionHandler() {
                         @Override
                         public void uncaughtException(Thread t, Throwable e) {
+                            exceptionCount++;
                             LOG.debug("Error in the background thread of the 
persistent cache", e);
                             LOG.warn("Error in the background thread of the 
persistent cache: " + e);
                         }
@@ -210,6 +213,7 @@ public class PersistentCache {
                         store.setReuseSpace(false);
                     }
                 } catch (Exception e) {
+                    exceptionCount++;
                     LOG.warn("Could not open the store " + fileName, e);
                 }
             }
@@ -228,6 +232,7 @@ public class PersistentCache {
                     Thread.interrupted();
                     store.close();
                 } catch (Exception e) {
+                    exceptionCount++;
                     LOG.debug("Could not close the store", e);
                     LOG.warn("Could not close the store: " + e);
                     store.closeImmediately();
@@ -236,6 +241,7 @@ public class PersistentCache {
                     try {
                         MVStoreTool.compact(fileName, true);
                     } catch (Exception e) {
+                        exceptionCount++;
                         LOG.debug("Could not compact the store", e);
                         LOG.warn("Could not compact the store: " + e);
                     }
@@ -251,6 +257,7 @@ public class PersistentCache {
                     }
                     return store.openMap(name, builder);
                 } catch (Exception e) {
+                    exceptionCount++;
                     LOG.warn("Could not open the map", e);
                     return null;
                 }
@@ -259,12 +266,16 @@ public class PersistentCache {
             @Override
             long getFileSize() {
                 try {
+                    if (store == null) {
+                        return 0;
+                    }
                     FileStore fs = store.getFileStore();
                     if (fs == null) {
                         return 0;
                     }
                     return fs.size();
                 } catch (Exception e) {
+                    exceptionCount++;
                     LOG.warn("Could not retrieve the map size", e);
                     return 0;
                 }
@@ -342,6 +353,7 @@ public class PersistentCache {
         } else if (generation == writeGeneration) {
             s = writeStore;
         } else {
+            exceptionCount++;
             throw new IllegalArgumentException("Unknown generation: " + 
generation);
         }
         return new CacheMap<K, V>(s, name, builder);
@@ -396,6 +408,10 @@ public class PersistentCache {
     public int getOpenCount() {
         return writeStore.getOpenCount();
     }
+    
+    public int getExceptionCount() {
+        return exceptionCount;
+    }
 
 
     interface GenerationCache {

Modified: 
jackrabbit/oak/branches/1.2/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.2/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java?rev=1681077&r1=1681076&r2=1681077&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.2/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java
 (original)
+++ 
jackrabbit/oak/branches/1.2/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java
 Fri May 22 11:44:38 2015
@@ -24,9 +24,17 @@ import static org.junit.Assert.assertTru
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.FileOutputStream;
 import java.util.Random;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.jackrabbit.oak.cache.CacheLIRS;
+import org.apache.jackrabbit.oak.plugins.document.util.StringValue;
+
+import com.google.common.cache.Cache;
+
+import org.apache.jackrabbit.oak.plugins.document.PathRev;
+import org.apache.jackrabbit.oak.plugins.document.Revision;
 import org.apache.jackrabbit.oak.spi.blob.BlobStore;
 import org.apache.jackrabbit.oak.spi.blob.MemoryBlobStore;
 import org.junit.Test;
@@ -34,6 +42,33 @@ import org.junit.Test;
 public class CacheTest {
 
     @Test
+    public void recoverIfCorrupt() throws Exception {
+        FileUtils.deleteDirectory(new File("target/cacheTest"));
+        new File("target/cacheTest").mkdirs();
+        FileOutputStream out = new 
FileOutputStream("target/cacheTest/cache-0.data");
+        out.write("corrupt".getBytes());
+        out.close();
+        PersistentCache pCache = new PersistentCache("target/cacheTest");
+        CacheLIRS<PathRev, StringValue> cache = new CacheLIRS.Builder().
+                maximumSize(1).build();
+        Cache<PathRev, StringValue> map = pCache.wrap(null,  null,  cache, 
CacheType.DIFF);
+        String largeString = new String(new char[1024 * 1024]);
+        for (int counter = 0; counter < 10; counter++) {
+            long end = System.currentTimeMillis() + 100;
+            while (System.currentTimeMillis() < end) {
+                Thread.yield();
+            }
+            for (int i = 0; i < 100; i++) {
+                PathRev k = new PathRev("/" + counter, new Revision(0, 0, i));
+                map.getIfPresent(k);
+                map.put(k, new StringValue(largeString));
+            }
+        }
+        assertTrue("Exceptions: " + pCache.getExceptionCount(), 
+                pCache.getExceptionCount() < 100);
+    }
+    
+    @Test
     public void closeAlways() throws Exception {
         FileUtils.deleteDirectory(new File("target/cacheTest"));
         PersistentCache cache = new 
PersistentCache("target/cacheTest,manualCommit");


Reply via email to