Author: reschke
Date: Thu Dec 28 12:16:08 2017
New Revision: 1819421

URL: http://svn.apache.org/viewvc?rev=1819421&view=rev
Log:
OAK-7101: Stale documents in RDBDocumentStore cache

(Test and fix by Marcel Reutegger - thanks!)

Modified:
    
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
    
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBCacheConsistency2Test.java

Modified: 
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java?rev=1819421&r1=1819420&r2=1819421&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
 Thu Dec 28 12:16:08 2017
@@ -493,61 +493,64 @@ public class RDBDocumentStore implements
                 missingDocs.add(op.getId());
             }
         }
-        for (T doc : readDocumentsUncached(collection, missingDocs).values()) {
-            oldDocs.put(doc.getId(), doc);
-            if (collection == Collection.NODES) {
-                nodesCache.putIfAbsent((NodeDocument) doc);
-            }
-        }
+        oldDocs.putAll(readDocumentsUncached(collection, missingDocs));
 
-        List<T> docsToUpdate = new ArrayList<T>(updates.size());
-        Set<String> keysToUpdate = new HashSet<String>();
-        for (UpdateOp update : updates) {
-            String id = update.getId();
-            T modifiedDoc = collection.newDocument(this);
-            if (oldDocs.containsKey(id)) {
-                oldDocs.get(id).deepCopy(modifiedDoc);
-            }
-            UpdateUtils.applyChanges(modifiedDoc, update);
-            docsToUpdate.add(modifiedDoc);
-            keysToUpdate.add(id);
+        CacheChangesTracker tracker = null;
+        if (collection == Collection.NODES) {
+            tracker = nodesCache.registerTracker(Sets.union(oldDocs.keySet(), 
missingDocs));
         }
 
-        Connection connection = null;
-        RDBTableMetaData tmd = getTable(collection);
         try {
-            connection = this.ch.getRWConnection();
-            Set<String> successfulUpdates = db.update(connection, tmd, 
docsToUpdate, upsert);
-            connection.commit();
-
-            Set<String> failedUpdates = Sets.difference(keysToUpdate, 
successfulUpdates);
-            oldDocs.keySet().removeAll(failedUpdates);
-
-            if (collection == Collection.NODES) {
-                for (T doc : docsToUpdate) {
-                    String id = doc.getId();
-                    if (successfulUpdates.contains(id)) {
-                        if (oldDocs.containsKey(id)) {
-                            nodesCache.replaceCachedDocument((NodeDocument) 
oldDocs.get(id), (NodeDocument) doc);
-                        } else {
-                            nodesCache.putIfAbsent((NodeDocument) doc);
+            List<T> docsToUpdate = new ArrayList<T>(updates.size());
+            Set<String> keysToUpdate = new HashSet<String>();
+            for (UpdateOp update : updates) {
+                String id = update.getId();
+                T modifiedDoc = collection.newDocument(this);
+                if (oldDocs.containsKey(id)) {
+                    oldDocs.get(id).deepCopy(modifiedDoc);
+                }
+                UpdateUtils.applyChanges(modifiedDoc, update);
+                docsToUpdate.add(modifiedDoc);
+                keysToUpdate.add(id);
+            }
+
+            Connection connection = null;
+            RDBTableMetaData tmd = getTable(collection);
+            try {
+                connection = this.ch.getRWConnection();
+                Set<String> successfulUpdates = db.update(connection, tmd, 
docsToUpdate, upsert);
+                connection.commit();
+
+                Set<String> failedUpdates = Sets.difference(keysToUpdate, 
successfulUpdates);
+                oldDocs.keySet().removeAll(failedUpdates);
+
+                if (collection == Collection.NODES) {
+                    List<NodeDocument> docsToCache = new ArrayList<>();
+                    for (T doc : docsToUpdate) {
+                        if (successfulUpdates.contains(doc.getId())) {
+                            docsToCache.add((NodeDocument) doc);
                         }
                     }
+                    nodesCache.putNonConflictingDocs(tracker, docsToCache);
                 }
-            }
 
-            Map<UpdateOp, T> result = new HashMap<UpdateOp, T>();
-            for (UpdateOp op : updates) {
-                if (successfulUpdates.contains(op.getId())) {
-                    result.put(op, oldDocs.get(op.getId()));
+                Map<UpdateOp, T> result = new HashMap<UpdateOp, T>();
+                for (UpdateOp op : updates) {
+                    if (successfulUpdates.contains(op.getId())) {
+                        result.put(op, oldDocs.get(op.getId()));
+                    }
                 }
+                return result;
+            } catch (SQLException ex) {
+                this.ch.rollbackConnection(connection);
+                throw handleException("update failed for: " + keysToUpdate, 
ex, collection, keysToUpdate);
+            } finally {
+                this.ch.closeConnection(connection);
             }
-            return result;
-        } catch (SQLException ex) {
-            this.ch.rollbackConnection(connection);
-            throw handleException("update failed for: " + keysToUpdate, ex, 
collection, keysToUpdate);
         } finally {
-            this.ch.closeConnection(connection);
+            if (tracker != null) {
+                tracker.close();
+            }
         }
     }
 

Modified: 
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBCacheConsistency2Test.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBCacheConsistency2Test.java?rev=1819421&r1=1819420&r2=1819421&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBCacheConsistency2Test.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBCacheConsistency2Test.java
 Thu Dec 28 12:16:08 2017
@@ -16,13 +16,17 @@
  */
 package org.apache.jackrabbit.oak.plugins.document.rdb;
 
+import static org.apache.jackrabbit.oak.plugins.document.Collection.NODES;
+import static 
org.apache.jackrabbit.oak.plugins.document.util.Utils.getIdFromPath;
+import static 
org.apache.jackrabbit.oak.plugins.document.util.Utils.getKeyLowerLimit;
+import static 
org.apache.jackrabbit.oak.plugins.document.util.Utils.getKeyUpperLimit;
+import static org.junit.Assert.assertEquals;
+
 import java.util.List;
 import java.util.concurrent.atomic.AtomicLong;
 
 import javax.sql.DataSource;
 
-import com.google.common.collect.Lists;
-
 import org.apache.jackrabbit.oak.plugins.document.AbstractRDBConnectionTest;
 import org.apache.jackrabbit.oak.plugins.document.DocumentMK;
 import org.apache.jackrabbit.oak.plugins.document.DocumentStore;
@@ -31,16 +35,10 @@ import org.apache.jackrabbit.oak.plugins
 import org.apache.jackrabbit.oak.plugins.document.UpdateOp;
 import org.apache.jackrabbit.oak.plugins.document.util.Utils;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 
-import static org.apache.jackrabbit.oak.plugins.document.Collection.NODES;
-import static 
org.apache.jackrabbit.oak.plugins.document.util.Utils.getIdFromPath;
-import static 
org.apache.jackrabbit.oak.plugins.document.util.Utils.getKeyLowerLimit;
-import static 
org.apache.jackrabbit.oak.plugins.document.util.Utils.getKeyUpperLimit;
-import static org.junit.Assert.assertEquals;
+import com.google.common.collect.Lists;
 
-@Ignore("OAK-7101")
 public class RDBCacheConsistency2Test extends AbstractRDBConnectionTest {
 
     private static final long CACHE_SIZE = 128 * 1024;


Reply via email to