Author: reschke
Date: Tue Mar  5 16:45:19 2019
New Revision: 1854862

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

Modified:
    jackrabbit/oak/branches/1.10/   (props changed)
    
jackrabbit/oak/branches/1.10/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheMap.java
    
jackrabbit/oak/branches/1.10/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java

Propchange: jackrabbit/oak/branches/1.10/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Tue Mar  5 16:45:19 2019
@@ -1,3 +1,3 @@
 /jackrabbit/oak/branches/1.0:1665962
-/jackrabbit/oak/trunk:1850874,1850882,1851236,1851253,1851451,1851533-1851535,1851619,1852052,1852084,1852120,1852451,1852492-1852493,1852528,1852582,1852584,1852601,1852920,1853141,1853229,1853393,1853429,1853433,1853441,1853866,1853868,1853870,1853893,1853969,1853997,1854034,1854044,1854058,1854113,1854373,1854377,1854380,1854385,1854401,1854403,1854461-1854462,1854466,1854468,1854515,1854533
+/jackrabbit/oak/trunk:1850874,1850882,1851236,1851253,1851451,1851533-1851535,1851619,1852052,1852084,1852120,1852451,1852492-1852493,1852528,1852582,1852584,1852601,1852920,1853141,1853229,1853393,1853429,1853433,1853441,1853866,1853868,1853870,1853893,1853969,1853997,1854034,1854044,1854058,1854113,1854373,1854377,1854380,1854385,1854401,1854403,1854455,1854461-1854462,1854466,1854468,1854515,1854533,1854701
 /jackrabbit/trunk:1345480

Modified: 
jackrabbit/oak/branches/1.10/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheMap.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.10/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheMap.java?rev=1854862&r1=1854861&r2=1854862&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.10/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheMap.java
 (original)
+++ 
jackrabbit/oak/branches/1.10/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheMap.java
 Tue Mar  5 16:45:19 2019
@@ -25,14 +25,19 @@ 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
  */
 public class CacheMap<K, V> {
-    
+
     static final Logger LOG = LoggerFactory.getLogger(CacheMap.class);
-    
+
     private final MapFactory factory;
     private final String name;
     private final MVMap.Builder<K, V> builder;
@@ -40,14 +45,21 @@ public class CacheMap<K, V> {
     private volatile Map<K, V> map;
     private volatile boolean closed;
 
-    
+
     public CacheMap(MapFactory factory, String name, Builder<K, V> builder) {
         this.factory = factory;
         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) {
         if (i > 10) {
             LOG.warn("Too many re-opens; disabling this cache map", e);
@@ -64,7 +76,7 @@ public class CacheMap<K, V> {
         }
         openMap();
     }
-    
+
     public V put(K key, V value) {
         for (int i = 0;; i++) {
             if (closed) {
@@ -77,7 +89,7 @@ public class CacheMap<K, V> {
             }
         }
     }
-    
+
     public V get(Object key) {
         for (int i = 0;; i++) {
             if (closed) {
@@ -90,7 +102,7 @@ public class CacheMap<K, V> {
             }
         }
     }
-    
+
     public boolean containsKey(Object key) {
         for (int i = 0;; i++) {
             if (closed) {
@@ -101,9 +113,9 @@ public class CacheMap<K, V> {
             } catch (Exception e) {
                 reopen(i, e);
             }
-        }        
+        }
     }
-    
+
     public V remove(Object key) {
         for (int i = 0;; i++) {
             if (closed) {
@@ -114,7 +126,7 @@ public class CacheMap<K, V> {
             } catch (Exception e) {
                 reopen(i, e);
             }
-        }        
+        }
     }
 
     public void clear() {
@@ -128,9 +140,9 @@ public class CacheMap<K, V> {
             } catch (Exception e) {
                 reopen(i, e);
             }
-        }        
+        }
     }
-    
+
     void openMap() {
         openCount = factory.reopenStoreIfNeeded(openCount);
         Map<K, V> m2 = factory.openMap(name, builder);
@@ -138,5 +150,4 @@ public class CacheMap<K, V> {
             map = m2;
         }
     }
-    
 }

Modified: 
jackrabbit/oak/branches/1.10/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.10/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java?rev=1854862&r1=1854861&r2=1854862&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.10/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java
 (original)
+++ 
jackrabbit/oak/branches/1.10/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheTest.java
 Tue Mar  5 16:45:19 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"));


Reply via email to