jackrabbit-oak-commits mailing list archives

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

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

Fix logic related to determining the parentId when query is being performed as fromKey are
not valid id in some cases

Modified:
    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/util/Utils.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReadPreferenceIT.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/util/UtilsTest.java

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=1594166&r1=1594165&r2=1594166&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:18:51 2014
@@ -326,7 +326,7 @@ public class MongoDocumentStore implemen
         DBCollection dbCollection = getDBCollection(collection);
         long start = start();
         try {
-            ReadPreference readPreference = getMongoReadPreference(collection, key, docReadPref);
+            ReadPreference readPreference = getMongoReadPreference(collection, Utils.getParentId(key),
docReadPref);
             DBObject obj = dbCollection.findOne(getByKeyQuery(key).get(), null, null, readPreference);
 
             if (obj == null
@@ -383,7 +383,8 @@ public class MongoDocumentStore implemen
         long start = start();
         try {
             DBCursor cursor = dbCollection.find(query).sort(BY_ID_ASC);
-            cursor.setReadPreference(getMongoReadPreference(collection, fromKey, getDefaultReadPreference(collection)));
+            String parentId = Utils.getParentIdFromLowerLimit(fromKey);
+            cursor.setReadPreference(getMongoReadPreference(collection, parentId, getDefaultReadPreference(collection)));
 
             List<T> list;
             try {
@@ -679,7 +680,7 @@ public class MongoDocumentStore implemen
     }
 
     <T extends Document> ReadPreference getMongoReadPreference(Collection<T>
collection,
-                                                               String key,
+                                                               String parentId,
                                                                DocumentReadPreference preference)
{
         switch(preference){
             case PRIMARY:
@@ -697,7 +698,6 @@ public class MongoDocumentStore implemen
                 //we can still read from secondary
                 //TODO REVIEW Would that be safe
                 ReadPreference readPreference = ReadPreference.primaryPreferred();
-                String parentId = Utils.getParentId(key);
                 if (parentId != null) {
                     long replicationSafeLimit = getTime() - maxReplicationLagMillis;
                     NodeDocument cachedDoc = (NodeDocument) getIfCached(collection, parentId);

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java?rev=1594166&r1=1594165&r2=1594166&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java
Tue May 13 09:18:51 2014
@@ -60,14 +60,14 @@ public class Utils {
      * possibly be too large to be used for the primary key for the document
      * store.
      */
-    private static final int PATH_SHORT = Integer.getInteger("oak.pathShort", 165);
+    static final int PATH_SHORT = Integer.getInteger("oak.pathShort", 165);
 
     /**
      * The maximum length of the parent path, in bytes. If the parent path is
      * longer, then the id of a document is no longer the path, but the hash of
      * the parent, and then the node name.
      */
-    private static final int PATH_LONG = Integer.getInteger("oak.pathLong", 350);
+    static final int PATH_LONG = Integer.getInteger("oak.pathLong", 350);
 
     /**
      * The maximum size a node name, in bytes. This is only a problem for long path.
@@ -379,6 +379,23 @@ public class Utils {
     }
 
     /**
+     * Returns parentId extracted from the fromKey. fromKey is usually constructed
+     * using Utils#getKeyLowerLimit
+     *
+     * @param fromKey key used as start key in queries
+     * @return parentId if possible.
+     */
+    @CheckForNull
+    public static String getParentIdFromLowerLimit(String fromKey){
+        //If key just ends with slash 2:/foo/ then append a fake
+        //name to create a proper id
+        if(fromKey.endsWith("/")){
+            fromKey = fromKey + "a";
+        }
+        return getParentId(fromKey);
+    }
+
+    /**
      * Returns <code>true</code> if a revision tagged with the given revision
      * should be considered committed, <code>false</code> otherwise. Committed
      * revisions have a tag, which equals 'c' or starts with 'c-'.

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=1594166&r1=1594165&r2=1594166&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:18:51 2014
@@ -135,11 +135,12 @@ public class ReadPreferenceIT {
         nodeStore.merge(b1, EmptyHook.INSTANCE, CommitInfo.EMPTY);
 
         String id = Utils.getIdFromPath("/x/y");
+        String parentId = Utils.getParentId(id);
         mongoDS.invalidateCache(NODES,id);
 
         //For modifiedTime < replicationLag primary should be preferred
         assertEquals(ReadPreference.primaryPreferred(),
-                mongoDS.getMongoReadPreference(NODES,id, DocumentReadPreference.PREFER_SECONDARY_IF_OLD_ENOUGH));
+                mongoDS.getMongoReadPreference(NODES,parentId, DocumentReadPreference.PREFER_SECONDARY_IF_OLD_ENOUGH));
 
         //Going into future to make parent /x old enough
         clock.waitUntil(Revision.getCurrentTimestamp() + replicationLag);
@@ -147,6 +148,6 @@ public class ReadPreferenceIT {
 
         //For old modified nodes secondaries should be preferred
         assertEquals(testPref,
-                mongoDS.getMongoReadPreference(NODES,id, DocumentReadPreference.PREFER_SECONDARY_IF_OLD_ENOUGH));
+                mongoDS.getMongoReadPreference(NODES,parentId, DocumentReadPreference.PREFER_SECONDARY_IF_OLD_ENOUGH));
     }
 }

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/util/UtilsTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/util/UtilsTest.java?rev=1594166&r1=1594165&r2=1594166&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/util/UtilsTest.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/util/UtilsTest.java
Tue May 13 09:18:51 2014
@@ -16,11 +16,15 @@
  */
 package org.apache.jackrabbit.oak.plugins.document.util;
 
+import com.google.common.base.Strings;
+import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.plugins.document.Revision;
 import org.junit.Ignore;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 /**
  * Tests for {@link Utils}.
@@ -38,6 +42,23 @@ public class UtilsTest {
                 Utils.getPreviousIdFor("/a/b/c/d/e/f/g/h/i/j/k/l/m", r, 3));
     }
 
+    @Test
+    public void getParentIdFromLowerLimit() throws Exception{
+        assertEquals("1:/foo",Utils.getParentIdFromLowerLimit(Utils.getKeyLowerLimit("/foo")));
+        assertEquals("1:/foo",Utils.getParentIdFromLowerLimit("2:/foo/bar"));
+    }
+
+    @Test
+    public void getParentId() throws Exception{
+        String longPath = PathUtils.concat("/"+Strings.repeat("p", Utils.PATH_LONG + 1),
"foo");
+        assertTrue(Utils.isLongPath(longPath));
+
+        assertNull(Utils.getParentId(Utils.getIdFromPath(longPath)));
+
+        assertNull(Utils.getParentId(Utils.getIdFromPath("/")));
+        assertEquals("1:/foo",Utils.getParentId("2:/foo/bar"));
+    }
+
     @Ignore("Performance test")
     @Test
     public void performance_getPreviousIdFor() {



Mime
View raw message