Return-Path: X-Original-To: apmail-jackrabbit-oak-commits-archive@minotaur.apache.org Delivered-To: apmail-jackrabbit-oak-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0EED311CC6 for ; Wed, 14 May 2014 04:05:57 +0000 (UTC) Received: (qmail 19803 invoked by uid 500); 13 May 2014 09:19:17 -0000 Delivered-To: apmail-jackrabbit-oak-commits-archive@jackrabbit.apache.org Received: (qmail 19771 invoked by uid 500); 13 May 2014 09:19:16 -0000 Mailing-List: contact oak-commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: oak-dev@jackrabbit.apache.org Delivered-To: mailing list oak-commits@jackrabbit.apache.org Received: (qmail 19763 invoked by uid 99); 13 May 2014 09:19:16 -0000 Received: from Unknown (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 13 May 2014 09:19:16 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 13 May 2014 09:19:16 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 17DD52388860; Tue, 13 May 2014 09:18:52 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: oak-commits@jackrabbit.apache.org From: chetanm@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140513091852.17DD52388860@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 list; try { @@ -679,7 +680,7 @@ public class MongoDocumentStore implemen } ReadPreference getMongoReadPreference(Collection 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 true if a revision tagged with the given revision * should be considered committed, false 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() {