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 re...@apache.org on 2019/03/03 13:01:42 UTC

svn commit: r1854701 - in /jackrabbit/oak/trunk/oak-store-document/src: main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheMap.java test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java

Author: reschke
Date: Sun Mar  3 13:01:42 2019
New Revision: 1854701

URL: http://svn.apache.org/viewvc?rev=1854701&view=rev
Log:
OAK-8051: PersistentCache: error during open can lead to incomplete initialization and subsequent NPEs

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

Modified: jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheMap.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheMap.java?rev=1854701&r1=1854700&r2=1854701&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheMap.java (original)
+++ jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheMap.java Sun Mar  3 13:01:42 2019
@@ -25,7 +25,12 @@ import org.slf4j.LoggerFactory;
 
 /**
  * A cache map. This map supports re-opening the store if this is needed.
- *
+ * <p>
+ * Note that a failure to open the underlying store will be handled gracefully,
+ * in that the {@code CacheMap} can be constructed, but will not actually cache
+ * anything. The same is true for the case where the underlying store starts to
+ * fail and can not be re-opened.
+ * 
  * @param <K> the key type
  * @param <V> the value type
  */
@@ -46,6 +51,13 @@ public class CacheMap<K, V> {
         this.name = name;
         this.builder = builder;
         openMap();
+        // OAK-8051: if opening failed, immediately try to re-open,
+        // until either the the map is closed, or open
+        for (int i = 0; map == null && !closed; i++) {
+            if (map == null) {
+                reopen(i, null);
+            }
+        }
     }
 
     private void reopen(int i, Exception e) {

Modified: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java?rev=1854701&r1=1854700&r2=1854701&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java (original)
+++ jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java Sun Mar  3 13:01:42 2019
@@ -26,43 +26,58 @@ import java.io.File;
 import java.io.FileOutputStream;
 
 import com.google.common.cache.Cache;
+
 import org.apache.commons.io.FileUtils;
 import org.apache.jackrabbit.oak.cache.CacheLIRS;
+import org.apache.jackrabbit.oak.commons.junit.LogCustomizer;
 import org.apache.jackrabbit.oak.plugins.document.PathRev;
 import org.apache.jackrabbit.oak.plugins.document.Revision;
 import org.apache.jackrabbit.oak.plugins.document.RevisionVector;
 import org.apache.jackrabbit.oak.plugins.document.util.StringValue;
 import org.junit.Test;
+import org.slf4j.event.Level;
 
 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<PathRev, StringValue>().
-                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 RevisionVector(new Revision(0, 0, i)));
-                map.getIfPresent(k);
-                map.put(k, new StringValue(largeString));
+        String expectedWarning = "Too many re-opens";
+        LogCustomizer lc = LogCustomizer.forLogger(CacheMap.class).enable(Level.WARN).contains(expectedWarning).create();
+
+        try {
+            lc.starting();
+
+            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<PathRev, StringValue>().
+                    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 RevisionVector(new Revision(0, 0, i)));
+                    map.getIfPresent(k);
+                    map.put(k, new StringValue(largeString));
+                }
             }
+            assertTrue("Exceptions: " + pCache.getExceptionCount(), 
+                    pCache.getExceptionCount() < 100);
+
+            assertTrue("WARN level log should contain one entry containing '" + expectedWarning + "'", lc.getLogs().size() == 1);
+        }
+        finally {
+            lc.finished();
         }
-        assertTrue("Exceptions: " + pCache.getExceptionCount(), 
-                pCache.getExceptionCount() < 100);
     }
-    
+
     @Test
     public void closeAlways() throws Exception {
         FileUtils.deleteDirectory(new File("target/cacheTest"));