Modified: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/MongoBlobStoreTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/MongoBlobStoreTest.java?rev=1832110&r1=1832109&r2=1832110&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/MongoBlobStoreTest.java (original) +++ jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/MongoBlobStoreTest.java Wed May 23 15:56:20 2018 @@ -16,6 +16,9 @@ */ package org.apache.jackrabbit.oak.plugins.document.blob; +import com.mongodb.ReadPreference; +import com.mongodb.client.MongoDatabase; + import org.apache.jackrabbit.oak.plugins.document.MongoUtils; import org.apache.jackrabbit.oak.plugins.document.mongo.MongoBlobStore; import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection; @@ -48,7 +51,10 @@ public class MongoBlobStoreTest extends mongoConnection = MongoUtils.getConnection(); MongoUtils.dropCollections(mongoConnection.getDatabase()); - MongoBlobStore blobStore = new MongoBlobStore(mongoConnection.getDatabase()); + // Some tests assume read from the primary + MongoDatabase db = mongoConnection.getDatabase() + .withReadPreference(ReadPreference.primary()); + MongoBlobStore blobStore = new MongoBlobStore(db); blobStore.setBlockSize(128); blobStore.setBlockSizeMin(48); this.store = blobStore;
Modified: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/MongoCloudBlobGCTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/MongoCloudBlobGCTest.java?rev=1832110&r1=1832109&r2=1832110&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/MongoCloudBlobGCTest.java (original) +++ jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/cloud/MongoCloudBlobGCTest.java Wed May 23 15:56:20 2018 @@ -19,7 +19,7 @@ package org.apache.jackrabbit.oak.plugin import org.apache.jackrabbit.oak.plugins.blob.cloud.CloudBlobStore; import org.apache.jackrabbit.oak.plugins.document.DocumentMK; import org.apache.jackrabbit.oak.plugins.document.MongoBlobGCTest; -import org.apache.jackrabbit.oak.plugins.document.MongoUtils; +import org.apache.jackrabbit.oak.spi.blob.BlobStore; import org.junit.After; import org.junit.Assume; import org.junit.Before; @@ -30,8 +30,11 @@ import org.junit.BeforeClass; * */ public class MongoCloudBlobGCTest extends MongoBlobGCTest { + + private BlobStore blobStore; + @BeforeClass - public static void setUpBeforeClass() throws Exception { + public static void setUpBeforeClass() { try { Assume.assumeNotNull(CloudStoreUtils.getBlobStore()); } catch (Exception e) { @@ -40,20 +43,17 @@ public class MongoCloudBlobGCTest extend } @Before - @Override - public void setUpConnection() throws Exception { - mongoConnection = connectionFactory.getConnection(); - MongoUtils.dropCollections(mongoConnection.getDBName()); - mk = new DocumentMK.Builder() - .setMongoDB(mongoConnection.getMongoClient(), mongoConnection.getDBName()) - .setBlobStore(CloudStoreUtils.getBlobStore()).open(); + public void setUpBlobStore() throws Exception { + blobStore = CloudStoreUtils.getBlobStore(); } @After - @Override - public void tearDownConnection() throws Exception { + public void deleteBucket() { ((CloudBlobStore) mk.getNodeStore().getBlobStore()).deleteBucket(); - mk.dispose(); - MongoUtils.dropCollections(connectionFactory.getConnection().getDB()); + } + + @Override + protected DocumentMK.Builder addToBuilder(DocumentMK.Builder mk) { + return super.addToBuilder(mk).setBlobStore(blobStore); } } Modified: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/MongoDataStoreBlobGCTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/MongoDataStoreBlobGCTest.java?rev=1832110&r1=1832109&r2=1832110&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/MongoDataStoreBlobGCTest.java (original) +++ jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/ds/MongoDataStoreBlobGCTest.java Wed May 23 15:56:20 2018 @@ -45,22 +45,16 @@ public class MongoDataStoreBlobGCTest ex } } + @Override + protected DocumentMK.Builder addToBuilder(DocumentMK.Builder mk) { + return super.addToBuilder(mk).setBlobStore(blobStore); + } + @Before @Override public void setUpConnection() throws Exception { startDate = new Date(); - mongoConnection = connectionFactory.getConnection(); - MongoUtils.dropCollections(mongoConnection.getDBName()); blobStore = DataStoreUtils.getBlobStore(folder.newFolder()); - mk = new DocumentMK.Builder().clock(getTestClock()) - .setMongoDB(mongoConnection.getMongoClient(), mongoConnection.getDBName()) - .setBlobStore(blobStore).open(); - } - - @After - @Override - public void tearDownConnection() throws Exception { - mk.dispose(); - MongoUtils.dropCollections(connectionFactory.getConnection().getDB()); + super.setUpConnection(); } } Modified: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheInvalidationIT.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheInvalidationIT.java?rev=1832110&r1=1832109&r2=1832110&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheInvalidationIT.java (original) +++ jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/CacheInvalidationIT.java Wed May 23 15:56:20 2018 @@ -19,6 +19,8 @@ package org.apache.jackrabbit.oak.plugins.document.mongo; +import com.mongodb.ReadPreference; + import org.apache.jackrabbit.oak.api.CommitFailedException; import org.apache.jackrabbit.oak.commons.PathUtils; import org.apache.jackrabbit.oak.plugins.document.AbstractMongoConnectionTest; @@ -195,7 +197,7 @@ public class CacheInvalidationIT extends private DocumentNodeStore createNS(int clusterId) throws Exception { MongoConnection mc = connectionFactory.getConnection(); - return new DocumentMK.Builder() + DocumentNodeStore ns = new DocumentMK.Builder() .setMongoDB(mc.getMongoClient(), mc.getDBName()) .setClusterId(clusterId) //Set delay to 0 so that effect of changes are immediately reflected @@ -203,6 +205,10 @@ public class CacheInvalidationIT extends .setBundlingDisabled(true) .setLeaseCheck(false) .getNodeStore(); + // enforce primary read preference, otherwise test fails on a replica + // set with a read preference configured to secondary. + MongoTestUtils.setReadPreference(ns, ReadPreference.primary()); + return ns; } } Modified: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/JournalIT.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/JournalIT.java?rev=1832110&r1=1832109&r2=1832110&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/JournalIT.java (original) +++ jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/JournalIT.java Wed May 23 15:56:20 2018 @@ -19,6 +19,8 @@ package org.apache.jackrabbit.oak.plugin import java.util.List; import java.util.Set; +import com.mongodb.ReadPreference; + import org.apache.jackrabbit.oak.cache.CacheStats; import org.apache.jackrabbit.oak.plugins.document.AbstractJournalTest; import org.apache.jackrabbit.oak.plugins.document.DocumentMK; @@ -210,8 +212,13 @@ public class JournalIT extends AbstractJ protected DocumentMK createMK(int clusterId, int asyncDelay) { MongoConnection c = connectionFactory.getConnection(); builder = newDocumentMKBuilder(); - return register(builder.setMongoDB(c.getMongoClient(), c.getDBName()) - .setClusterId(clusterId).setAsyncDelay(asyncDelay).setBundlingDisabled(true).open()); + DocumentMK mk = builder.setMongoDB(c.getMongoClient(), c.getDBName()) + .setClusterId(clusterId).setAsyncDelay(asyncDelay) + .setBundlingDisabled(true).open(); + // enforce primary read preference, otherwise test fails on a replica + // set with a read preference configured to secondary. + MongoTestUtils.setReadPreference(mk.getDocumentStore(), ReadPreference.primary()); + return register(mk); } private static long getCacheElementCount(DocumentStore ds) { Modified: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentNodeStoreBuilderTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentNodeStoreBuilderTest.java?rev=1832110&r1=1832109&r2=1832110&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentNodeStoreBuilderTest.java (original) +++ jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentNodeStoreBuilderTest.java Wed May 23 15:56:20 2018 @@ -18,6 +18,7 @@ package org.apache.jackrabbit.oak.plugin import org.junit.Test; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class MongoDocumentNodeStoreBuilderTest { @@ -27,4 +28,10 @@ public class MongoDocumentNodeStoreBuild MongoDocumentNodeStoreBuilder builder = new MongoDocumentNodeStoreBuilder(); assertTrue(builder.isSocketKeepAlive()); } + + @Test + public void clientSessionDisabled() { + MongoDocumentNodeStoreBuilder builder = new MongoDocumentNodeStoreBuilder(); + assertFalse(builder.isClientSessionDisabled()); + } } Added: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestUtils.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestUtils.java?rev=1832110&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestUtils.java (added) +++ jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestUtils.java Wed May 23 15:56:20 2018 @@ -0,0 +1,35 @@ +/* + * 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.mongo; + +import com.mongodb.ReadPreference; + +import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore; +import org.apache.jackrabbit.oak.plugins.document.DocumentStore; + +public final class MongoTestUtils { + + public static void setReadPreference(DocumentNodeStore ns, + ReadPreference preference) { + setReadPreference(ns.getDocumentStore(), preference); + } + + public static void setReadPreference(DocumentStore store, + ReadPreference preference) { + store.setReadWriteMode("readPreference=" + preference.getName()); + } +} Propchange: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestUtils.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReadPreferenceIT.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReadPreferenceIT.java?rev=1832110&r1=1832109&r2=1832110&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReadPreferenceIT.java (original) +++ jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReadPreferenceIT.java Wed May 23 15:56:20 2018 @@ -70,6 +70,7 @@ public class ReadPreferenceIT extends Ab mk = new DocumentMK.Builder() .clock(clock) .setClusterId(1) + .setClientSessionDisabled(true) .setMongoDB(mongoConnection.getMongoClient(), mongoConnection.getDBName()) .setLeaseCheck(false) .open(); @@ -80,6 +81,7 @@ public class ReadPreferenceIT extends Ab mk2 = new DocumentMK.Builder() .clock(clock) .setClusterId(2) + .setClientSessionDisabled(true) .setMongoDB(mongoConnection2.getMongoClient(), mongoConnection2.getDBName()) .setLeaseCheck(false) .open(); @@ -127,6 +129,9 @@ public class ReadPreferenceIT extends Ab @Test public void testMongoReadPreferencesDefault() throws Exception{ + // start with read preference set to primary + mongoDS.setReadWriteMode(rwMode(ReadPreference.primary())); + assertEquals(ReadPreference.primary(), mongoDS.getMongoReadPreference(NODES,"foo", null, DocumentReadPreference.PRIMARY)); @@ -224,6 +229,7 @@ public class ReadPreferenceIT extends Ab @Test public void testReadWriteMode() throws Exception{ + mongoDS.setReadWriteMode(rwMode(ReadPreference.primary())); assertEquals(ReadPreference.primary(), mongoDS.getConfiguredReadPreference(NODES)); mongoDS.setReadWriteMode("readPreference=secondary&w=2&safe=true&j=true"); Added: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/replica/ReplicaSetStatusTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/replica/ReplicaSetStatusTest.java?rev=1832110&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/replica/ReplicaSetStatusTest.java (added) +++ jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/replica/ReplicaSetStatusTest.java Wed May 23 15:56:20 2018 @@ -0,0 +1,109 @@ +/* + * 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.mongo.replica; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import com.mongodb.ServerAddress; +import com.mongodb.connection.ClusterId; +import com.mongodb.connection.ConnectionDescription; +import com.mongodb.connection.ServerId; +import com.mongodb.event.ServerHeartbeatSucceededEvent; + +import org.bson.BsonArray; +import org.bson.BsonDateTime; +import org.bson.BsonDocument; +import org.bson.BsonString; +import org.bson.BsonValue; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class ReplicaSetStatusTest { + + private List<ServerAddress> hosts = Arrays.asList( + new ServerAddress("localhost", 27017), + new ServerAddress("localhost", 27018), + new ServerAddress("localhost", 27019) + ); + + private List<BsonValue> hostValues = hosts.stream() + .map(sa -> new BsonString(sa.toString())) + .collect(Collectors.toList()); + + private List<ConnectionDescription> connections = Arrays.asList( + new ConnectionDescription(new ServerId(new ClusterId(), hosts.get(0))), + new ConnectionDescription(new ServerId(new ClusterId(), hosts.get(1))), + new ConnectionDescription(new ServerId(new ClusterId(), hosts.get(2))) + ); + + @Test + public void estimateLag() { + ReplicaSetStatus status = new ReplicaSetStatus(); + assertEquals(ReplicaSetStatus.UNKNOWN_LAG, status.getLagEstimate()); + status.serverHeartbeatSucceeded(newEvent(0, 0, 1000)); + assertEquals(ReplicaSetStatus.UNKNOWN_LAG, status.getLagEstimate()); + status.serverHeartbeatSucceeded(newEvent(1, 0, 800)); + assertEquals(ReplicaSetStatus.UNKNOWN_LAG, status.getLagEstimate()); + status.serverHeartbeatSucceeded(newEvent(2, 50, 1000)); + // lastWrite difference: 200 + // localTime difference: 50 + // lag estimate: max(0, 200 - 50) = 150 + // avg lag estimate: avg(150, 0, 0) = 50 + assertEquals(50, status.getLagEstimate()); + + status.serverHeartbeatSucceeded(newEvent(0, 5000, 4800)); + // lastWrite difference: 4000 + // localTime difference: 5000 + // lag estimate: max(0, 4000 - 5000) = 0 + // avg lag estimate: avg(0, 150, 0) = 50 + assertEquals(50, status.getLagEstimate()); + + status.serverHeartbeatSucceeded(newEvent(1, 5050, 2000)); + // lastWrite difference: 3800 + // localTime difference: 5000 + // lag estimate: max(0, 3800 - 5000) = 0 + // avg lag estimate: avg(0, 0, 150) = 50 + assertEquals(50, status.getLagEstimate()); + + status.serverHeartbeatSucceeded(newEvent(2, 5150, 5000)); + // lastWrite difference: 3000 + // localTime difference: 150 + // lag estimate: max(0, 3000 - 150) = 2850 + // avg lag estimate: avg(2850, 0, 0) = 950 + assertEquals(950, status.getLagEstimate()); + + status.serverHeartbeatSucceeded(newEvent(0, 10010, 9000)); + // lastWrite difference: 7000 + // localTime difference: 4960 + // lag estimate: max(0, 7000 - 4960) = 2040 + // avg lag estimate: avg(2040, 2850, 0) = 1630 + assertEquals(1630, status.getLagEstimate()); + + } + + private ServerHeartbeatSucceededEvent newEvent(int connectionIndex, long localTime, long lastWriteDate) { + ConnectionDescription description = connections.get(connectionIndex); + BsonDocument reply = new BsonDocument("localTime", new BsonDateTime(localTime)); + reply.put("hosts", new BsonArray(hostValues)); + BsonDocument lastWrite = new BsonDocument("lastWriteDate", new BsonDateTime(lastWriteDate)); + reply.put("lastWrite", lastWrite); + return new ServerHeartbeatSucceededEvent(description.getConnectionId(), reply, 0); + } +} Propchange: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/replica/ReplicaSetStatusTest.java ------------------------------------------------------------------------------ svn:eol-style = native
