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 2014/05/27 17:19:11 UTC

svn commit: r1597804 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/document/rdb/ test/java/org/apache/jackrabbit/oak/plugins/document/

Author: reschke
Date: Tue May 27 15:19:11 2014
New Revision: 1597804

URL: http://svn.apache.org/r1597804
Log:
OAK-1860 - add fwk for concurrent DS tests, improve concurrency handling in RDBDocumentStore

Added:
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMultiDocumentStoreTest.java   (with props)
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MultiDocumentStoreTest.java   (with props)
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractDocumentStoreTest.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentStoreFixture.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java?rev=1597804&r1=1597803&r2=1597804&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java Tue May 27 15:19:11 2014
@@ -474,11 +474,19 @@ public class RDBDocumentStore implements
 
                 int retries = maxRetries;
                 while (!success && retries > 0) {
-                    success = updateDocument(collection, doc, (Long) oldDoc.get(MODCOUNT));
+                    long lastmodcount = (Long) oldDoc.get(MODCOUNT);
+                    success = updateDocument(collection, doc, lastmodcount);
                     if (!success) {
-                        // retry with a fresh document; only use cache when *really* fresh, aka 50ms
                         retries -= 1;
-                        oldDoc = readDocumentCached(collection, update.getId(), 50);
+                        oldDoc = readDocumentCached(collection, update.getId(), Integer.MAX_VALUE);
+                        if (oldDoc != null) {
+                            long newmodcount = (Long) oldDoc.get(MODCOUNT);
+                            if (lastmodcount == newmodcount) {
+                                // cached copy did not change so it probably was updated by
+                                // a different instance, get a fresh one
+                                oldDoc = readDocumentUncached(collection, update.getId());
+                            }
+                        }
                         doc = applyChanges(collection, oldDoc, update, checkConditions);
                         if (doc == null) {
                             return null;

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractDocumentStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractDocumentStoreTest.java?rev=1597804&r1=1597803&r2=1597804&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractDocumentStoreTest.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractDocumentStoreTest.java Tue May 27 15:19:11 2014
@@ -17,11 +17,8 @@
 package org.apache.jackrabbit.oak.plugins.document;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
-import java.util.HashSet;
 import java.util.List;
-import java.util.Set;
 
 import org.junit.After;
 import org.junit.runner.RunWith;
@@ -39,7 +36,7 @@ public abstract class AbstractDocumentSt
     static final Logger LOG = LoggerFactory.getLogger(AbstractDocumentStoreTest.class);
 
     public AbstractDocumentStoreTest(DocumentStoreFixture dsf) {
-        this.ds = dsf.createDocumentStore();
+        this.ds = dsf.createDocumentStore(1);
         this.dsname = dsf.getName();
     }
 

Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMultiDocumentStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMultiDocumentStoreTest.java?rev=1597804&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMultiDocumentStoreTest.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMultiDocumentStoreTest.java Tue May 27 15:19:11 2014
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.plugins.document;
+
+public abstract class AbstractMultiDocumentStoreTest extends AbstractDocumentStoreTest {
+
+    protected DocumentStore ds1, ds2;
+
+    public AbstractMultiDocumentStoreTest(DocumentStoreFixture dsf) {
+        super(dsf);
+        this.ds1 = super.ds;
+        this.ds2 = dsf.createDocumentStore(2);
+    }
+}

Propchange: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMultiDocumentStoreTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentStoreFixture.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentStoreFixture.java?rev=1597804&r1=1597803&r2=1597804&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentStoreFixture.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentStoreFixture.java Tue May 27 15:19:11 2014
@@ -19,6 +19,7 @@ package org.apache.jackrabbit.oak.plugin
 import javax.sql.DataSource;
 
 import com.mongodb.BasicDBObject;
+
 import org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore;
 import org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore;
 import org.apache.jackrabbit.oak.plugins.document.rdb.RDBDataSourceFactory;
@@ -41,7 +42,11 @@ public abstract class DocumentStoreFixtu
 
     public abstract String getName();
 
-    public abstract DocumentStore createDocumentStore();
+    public abstract DocumentStore createDocumentStore(int clusterId);
+
+    public DocumentStore createDocumentStore() {
+        return createDocumentStore(1);
+    }
 
     public boolean isAvailable() {
         return true;
@@ -58,21 +63,21 @@ public abstract class DocumentStoreFixtu
         }
 
         @Override
-        public DocumentStore createDocumentStore() {
+        public DocumentStore createDocumentStore(int clusterId) {
             return new MemoryDocumentStore();
         }
     }
 
     public static class RDBFixture extends DocumentStoreFixture {
 
-        DocumentStore ds;
+        DataSource dataSource;
+        DocumentStore store1, store2;
         String name;
 
         public RDBFixture(String name, String url, String username, String passwd) {
             this.name = name;
             try {
-                DataSource datas = RDBDataSourceFactory.forJdbcUrl(url, username, passwd);
-                this.ds = new RDBDocumentStore(datas, new DocumentMK.Builder());
+                dataSource = RDBDataSourceFactory.forJdbcUrl(url, username, passwd);
             } catch (Exception ex) {
                 LOG.info("Database instance not available at " + url + ", skipping tests...");
             }
@@ -84,13 +89,33 @@ public abstract class DocumentStoreFixtu
         }
 
         @Override
-        public DocumentStore createDocumentStore() {
-            return ds;
+        public DocumentStore createDocumentStore(int clusterId) {
+            if (clusterId == 1) {
+                store1 = new RDBDocumentStore(dataSource, new DocumentMK.Builder().setClusterId(1));
+                return store1;
+            } else if (clusterId == 2) {
+                store2 = new RDBDocumentStore(dataSource, new DocumentMK.Builder().setClusterId(2));
+                return store2;
+            } else {
+                throw new RuntimeException("expect clusterId == 1 or == 2");
+            }
         }
 
         @Override
         public boolean isAvailable() {
-            return this.ds != null;
+            return dataSource != null;
+        }
+
+        @Override
+        public void dispose() {
+            if (this.store1 != null) {
+                this.store1.dispose();
+                this.store1 = null;
+            }
+            if (this.store2 != null) {
+                this.store2.dispose();
+                this.store2 = null;
+            }
         }
     }
 
@@ -112,12 +137,12 @@ public abstract class DocumentStoreFixtu
         }
 
         @Override
-        public DocumentStore createDocumentStore() {
+        public DocumentStore createDocumentStore(int clusterId) {
             try {
                 MongoConnection connection = new MongoConnection(uri);
                 DB db = connection.getDB();
                 MongoUtils.dropCollections(db);
-                return new MongoDocumentStore(db, new DocumentMK.Builder());
+                return new MongoDocumentStore(db, new DocumentMK.Builder().setClusterId(clusterId));
             } catch (Exception e) {
                 throw new RuntimeException(e);
             }

Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MultiDocumentStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MultiDocumentStoreTest.java?rev=1597804&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MultiDocumentStoreTest.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MultiDocumentStoreTest.java Tue May 27 15:19:11 2014
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.plugins.document;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collections;
+
+import org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore;
+import org.junit.Assume;
+import org.junit.Test;
+
+public class MultiDocumentStoreTest extends AbstractMultiDocumentStoreTest {
+
+    public MultiDocumentStoreTest(DocumentStoreFixture dsf) {
+        super(dsf);
+    }
+
+    @Test
+    public void testInterleavedUpdate() {
+        // this does not work with the MemoryDocumentStore
+        Assume.assumeTrue(!(super.ds instanceof MemoryDocumentStore));
+
+        String id = this.getClass().getName() + ".testInterleavedUpdate";
+
+        // remove if present
+        NodeDocument nd = super.ds1.find(Collection.NODES, id);
+        if (nd != null) {
+            super.ds1.remove(Collection.NODES, id);
+        }
+
+        UpdateOp up = new UpdateOp(id, true);
+        up.set("_id", id);
+        up.set("_foo", 0l);
+        assertTrue(super.ds1.create(Collection.NODES, Collections.singletonList(up)));
+
+        long increments = 10;
+
+        for (int i = 0; i < increments; i++) {
+            up = new UpdateOp(id, true);
+            up.set("_id", id);
+            up.increment("_foo", 1l);
+            if (i % 2 == 0) {
+                super.ds1.update(Collection.NODES, Collections.singletonList(id), up);
+            }
+            else {
+                super.ds2.update(Collection.NODES, Collections.singletonList(id), up);
+            }
+        }
+        removeMe.add(id);
+
+        // read uncached
+        nd = super.ds1.find(Collection.NODES, id, 0);
+        assertEquals("_foo should have been incremented 10 times", increments, nd.get("_foo"));
+    }
+}

Propchange: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/MultiDocumentStoreTest.java
------------------------------------------------------------------------------
    svn:eol-style = native