Search in sources :

Example 11 with ReadPreference

use of com.mongodb.ReadPreference in project jackrabbit-oak by apache.

the class ReadPreferenceIT method testMongoReadPreferencesForLocalChanges.

@Test
public void testMongoReadPreferencesForLocalChanges() throws Exception {
    //Change the default
    ReadPreference testPref = ReadPreference.secondary();
    mongoDS.getDBCollection(NODES).getDB().setReadPreference(testPref);
    NodeStore nodeStore = mk.getNodeStore();
    NodeBuilder b1 = nodeStore.getRoot().builder();
    b1.child("x").child("y");
    nodeStore.merge(b1, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    mongoDS.invalidateCache();
    // the local change hasn't been replicated yet, primary must be used
    assertEquals(ReadPreference.primary(), mongoDS.getMongoReadPreference(NODES, null, "/x/y", DocumentReadPreference.PREFER_SECONDARY_IF_OLD_ENOUGH));
    // make the secondary up-to-date
    long now = Revision.getCurrentTimestamp();
    primary.addRevision(now, 0, 1, false);
    primary.addRevision(now, 0, 2, false);
    secondary.addRevision(now, 0, 1, false);
    secondary.addRevision(now, 0, 2, false);
    replica.updateRevisions();
    // local change has been replicated by now, it's fine to use secondary
    for (int i = 0; i < 400; i++) {
        assertEquals(testPref, mongoDS.getMongoReadPreference(NODES, null, "/x/y", DocumentReadPreference.PREFER_SECONDARY_IF_OLD_ENOUGH));
        Thread.sleep(5);
    }
}
Also used : ReadPreference(com.mongodb.ReadPreference) DocumentReadPreference(org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.DocumentReadPreference) NodeStore(org.apache.jackrabbit.oak.spi.state.NodeStore) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Test(org.junit.Test) AbstractMongoConnectionTest(org.apache.jackrabbit.oak.plugins.document.AbstractMongoConnectionTest)

Aggregations

ReadPreference (com.mongodb.ReadPreference)11 BasicDBObject (com.mongodb.BasicDBObject)3 DBObject (com.mongodb.DBObject)3 DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)3 Stopwatch (com.google.common.base.Stopwatch)2 DBCollection (com.mongodb.DBCollection)2 WriteConcern (com.mongodb.WriteConcern)2 AbstractMongoConnectionTest (org.apache.jackrabbit.oak.plugins.document.AbstractMongoConnectionTest)2 NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)2 DocumentReadPreference (org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.DocumentReadPreference)2 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)2 NodeStore (org.apache.jackrabbit.oak.spi.state.NodeStore)2 Test (org.junit.Test)2 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 BulkWriteException (com.mongodb.BulkWriteException)1 DBCursor (com.mongodb.DBCursor)1 MongoClientURI (com.mongodb.MongoClientURI)1 MongoException (com.mongodb.MongoException)1 QueryBuilder (com.mongodb.QueryBuilder)1 ServerAddress (com.mongodb.ServerAddress)1