jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chet...@apache.org
Subject svn commit: r1594167 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/document/mongo/ test/java/org/apache/jackrabbit/oak/plugins/document/mongo/
Date Tue, 13 May 2014 09:19:22 GMT
Author: chetanm
Date: Tue May 13 09:19:21 2014
New Revision: 1594167

URL: http://svn.apache.org/r1594167
Log:
OAK-1645 - Route find queries to Mongo secondary in MongoDocumentStore

Integrate with changes done in OAK-759. The readWriteMode is now parsed as MongoUri option.
The default value read is now read per collection. So if custom readWrite mode is specified
then that value be used otherwise global DB level value would be used

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoBlobReferenceIterator.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoVersionGCSupport.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReadPreferenceIT.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoBlobReferenceIterator.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoBlobReferenceIterator.java?rev=1594167&r1=1594166&r2=1594167&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoBlobReferenceIterator.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoBlobReferenceIterator.java
Tue May 13 09:19:21 2014
@@ -28,7 +28,6 @@ import com.mongodb.DBCollection;
 import com.mongodb.DBCursor;
 import com.mongodb.DBObject;
 import com.mongodb.QueryBuilder;
-import com.mongodb.ReadPreference;
 import org.apache.jackrabbit.oak.api.Blob;
 import org.apache.jackrabbit.oak.plugins.document.BlobCollector;
 import org.apache.jackrabbit.oak.plugins.document.Collection;
@@ -78,7 +77,8 @@ public class MongoBlobReferenceIterator 
                     .is(NodeDocument.HAS_BINARY_VAL)
                     .get();
             //TODO It currently prefers secondary. Would that be Ok?
-            cursor = getNodeCollection().find(query).setReadPreference(documentStore.getDefaultReadPreference());
+            cursor = getNodeCollection().find(query)
+                    .setReadPreference(documentStore.getConfiguredReadPreference(Collection.NODES));
         }
     }
 

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java?rev=1594167&r1=1594166&r2=1594167&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
Tue May 13 09:19:21 2014
@@ -34,9 +34,8 @@ import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
-import com.google.common.base.Splitter;
-
 import com.google.common.collect.Lists;
+import com.mongodb.MongoClientURI;
 import com.mongodb.ReadPreference;
 import org.apache.jackrabbit.mk.api.MicroKernelException;
 import org.apache.jackrabbit.oak.cache.CacheStats;
@@ -123,14 +122,11 @@ public class MongoDocumentStore implemen
 
     private Clock clock = Clock.SIMPLE;
 
-    private final DB db;
-
     private final long maxReplicationLagMillis;
 
     private String lastReadWriteMode;
 
     public MongoDocumentStore(DB db, DocumentMK.Builder builder) {
-        this.db = db;
         nodes = db.getCollection(
                 Collection.NODES.toString());
         clusterNodes = db.getCollection(
@@ -194,9 +190,7 @@ public class MongoDocumentStore implemen
                 .recordStats()
                 .build();
 
-        Cache<CacheValue, NodeDocument> cache =
-                new NodeDocOffHeapCache(primaryCache, listener, builder, this);
-        return cache;
+        return new NodeDocOffHeapCache(primaryCache, listener, builder, this);
     }
 
     private static long start() {
@@ -688,7 +682,7 @@ public class MongoDocumentStore implemen
             case PREFER_PRIMARY :
                 return ReadPreference.primaryPreferred();
             case PREFER_SECONDARY :
-                return getDefaultReadPreference();
+                return getConfiguredReadPreference(collection);
             case PREFER_SECONDARY_IF_OLD_ENOUGH:
                 if(collection != Collection.NODES){
                     return ReadPreference.primary();
@@ -705,7 +699,7 @@ public class MongoDocumentStore implemen
 
                         //If parent has been modified loooong time back then there children
                         //would also have not be modified. In that case we can read from
secondary
-                        readPreference = getDefaultReadPreference();
+                        readPreference = getConfiguredReadPreference(collection);
                     }
                 }
                 return readPreference;
@@ -721,8 +715,8 @@ public class MongoDocumentStore implemen
      *
      * @return db level ReadPreference
      */
-    ReadPreference getDefaultReadPreference(){
-        return db.getReadPreference();
+    ReadPreference getConfiguredReadPreference(Collection collection){
+        return getDBCollection(collection).getReadPreference();
     }
 
     @CheckForNull
@@ -1020,22 +1014,22 @@ public class MongoDocumentStore implemen
         }
         lastReadWriteMode = readWriteMode;
         try {
-            Map<String, String> map = Splitter.on(", ").withKeyValueSeparator(":").split(readWriteMode);
-            String read = map.get("read");
-            if (read != null) {
-                ReadPreference readPref = ReadPreference.valueOf(read);
-                if (!readPref.equals(nodes.getReadPreference())) {
-                    nodes.setReadPreference(readPref);
-                    LOG.info("Using ReadPreference " + readPref);
-                }
-            }
-            String write = map.get("write");
-            if (write != null) {
-                WriteConcern writeConcern = WriteConcern.valueOf(write);
-                if (!writeConcern.equals(nodes.getWriteConcern())) {
-                    nodes.setWriteConcern(writeConcern);
-                    LOG.info("Using WriteConcern " + writeConcern);
-                }
+            String rwModeUri = readWriteMode;
+            if(!readWriteMode.startsWith("mongodb://")){
+                rwModeUri = String.format("mongodb://localhost/?%s", readWriteMode);
+            }
+            MongoClientURI uri = new MongoClientURI(rwModeUri);
+            ReadPreference readPref = uri.getOptions().getReadPreference();
+
+            if (!readPref.equals(nodes.getReadPreference())) {
+                nodes.setReadPreference(readPref);
+                LOG.info("Using ReadPreference {} ",readPref);
+            }
+
+            WriteConcern writeConcern = uri.getOptions().getWriteConcern();
+            if (!writeConcern.equals(nodes.getWriteConcern())) {
+                nodes.setWriteConcern(writeConcern);
+                LOG.info("Using WriteConcern " + writeConcern);
             }
         } catch (Exception e) {
             LOG.error("Error setting readWriteMode " + readWriteMode, e);

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoVersionGCSupport.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoVersionGCSupport.java?rev=1594167&r1=1594166&r2=1594167&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoVersionGCSupport.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoVersionGCSupport.java
Tue May 13 09:19:21 2014
@@ -117,7 +117,7 @@ public class MongoVersionGCSupport exten
         final BasicDBObject keys = new BasicDBObject(Document.ID, 1);
         List<String> ids;
         DBCursor cursor = getNodeCollection().find(query, keys)
-                .setReadPreference(store.getDefaultReadPreference());
+                .setReadPreference(store.getConfiguredReadPreference(Collection.NODES));
         try {
              ids = ImmutableList.copyOf(Iterables.transform(cursor, new Function<DBObject,
String>() {
                  @Override

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReadPreferenceIT.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReadPreferenceIT.java?rev=1594167&r1=1594166&r2=1594167&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReadPreferenceIT.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReadPreferenceIT.java
Tue May 13 09:19:21 2014
@@ -43,6 +43,7 @@ import static org.apache.jackrabbit.oak.
 import static org.apache.jackrabbit.oak.plugins.document.Collection.SETTINGS;
 import static org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.DocumentReadPreference;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 public class ReadPreferenceIT {
 
@@ -148,6 +149,19 @@ public class ReadPreferenceIT {
 
         //For old modified nodes secondaries should be preferred
         assertEquals(testPref,
-                mongoDS.getMongoReadPreference(NODES,parentId, DocumentReadPreference.PREFER_SECONDARY_IF_OLD_ENOUGH));
+                mongoDS.getMongoReadPreference(NODES, parentId, DocumentReadPreference.PREFER_SECONDARY_IF_OLD_ENOUGH));
+    }
+
+    @Test
+    public void testReadWriteMode() throws Exception{
+        assertEquals(ReadPreference.primary(), mongoDS.getConfiguredReadPreference(NODES));
+
+        mongoDS.setReadWriteMode("readPreference=secondary&w=2&safe=true&j=true");
+
+        assertEquals(ReadPreference.secondary(), mongoDS.getDBCollection(NODES).getReadPreference());
+        assertEquals(2, mongoDS.getDBCollection(NODES).getWriteConcern().getW());
+        assertTrue(mongoDS.getDBCollection(NODES).getWriteConcern().getJ());
+
+        assertEquals(ReadPreference.secondary(), mongoDS.getConfiguredReadPreference(NODES));
     }
 }



Mime
View raw message