Author: reschke
Date: Mon Apr 15 09:42:25 2019
New Revision: 1857555

URL: http://svn.apache.org/viewvc?rev=1857555&view=rev
Log:
OAK-8200: MongoDocumentStore in ReadOnly mode should never modify persistence 
(ported to 1.8)

Modified:
    
jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
    
jackrabbit/oak/branches/1.8/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java

Modified: 
jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java?rev=1857555&r1=1857554&r2=1857555&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
 (original)
+++ 
jackrabbit/oak/branches/1.8/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
 Mon Apr 15 09:42:25 2019
@@ -261,6 +261,23 @@ public class MongoDocumentStore implemen
             replicaInfoThread.start();
         }
 
+        if (!readOnly) {
+            ensureIndexes(mongoStatus);
+        }
+
+        this.nodeLocks = new StripedNodeDocumentLocks();
+        this.nodesCache = builder.buildNodeDocumentCache(this, nodeLocks);
+
+        LOG.info("Connected to MongoDB {} with maxReplicationLagMillis {}, " +
+                "maxDeltaForModTimeIdxSecs {}, disableIndexHint {}, " +
+                "{}, serverStatus {}",
+                mongoStatus.getVersion(), maxReplicationLagMillis, 
maxDeltaForModTimeIdxSecs,
+                disableIndexHint, db.getWriteConcern(),
+                mongoStatus.getServerDetails());
+
+    }
+
+    private void ensureIndexes(@NotNull MongoStatus mongoStatus) {
         // indexes:
         // the _id field is the primary key, so we don't need to define it
 
@@ -273,11 +290,9 @@ public class MongoDocumentStore implemen
                     new boolean[]{true, true}, false, false);
         } else if (!hasIndex(nodes, NodeDocument.MODIFIED_IN_SECS, 
Document.ID)) {
             hasModifiedIdCompoundIndex = false;
-            if (!builder.getReadOnlyMode()) {
-                LOG.warn("Detected an upgrade from Oak version <= 1.2. For 
optimal " +
-                        "performance it is recommended to create a compound 
index " +
-                        "for the 'nodes' collection on {_modified:1, _id:1}.");
-            }
+            LOG.warn("Detected an upgrade from Oak version <= 1.2. For optimal 
" +
+                    "performance it is recommended to create a compound index 
" +
+                    "for the 'nodes' collection on {_modified:1, _id:1}.");
         }
 
         // index on the _bin flag to faster access nodes with binaries for GC
@@ -293,14 +308,12 @@ public class MongoDocumentStore implemen
                 createIndex(nodes, NodeDocument.DELETED_ONCE, true, false, 
true);
             }
         } else if (!hasIndex(nodes, DELETED_ONCE, MODIFIED_IN_SECS)) {
-            if (!builder.getReadOnlyMode()) {
-                LOG.warn("Detected an upgrade from Oak version <= 1.6. For 
optimal " +
-                        "Revision GC performance it is recommended to create a 
" +
-                        "partial index for the 'nodes' collection on " +
-                        "{_deletedOnce:1, _modified:1} with a 
partialFilterExpression " +
-                        "{_deletedOnce:true}. Partial indexes require MongoDB 
3.2 " +
-                        "or higher.");
-            }
+            LOG.warn("Detected an upgrade from Oak version <= 1.6. For optimal 
" +
+                    "Revision GC performance it is recommended to create a " +
+                    "partial index for the 'nodes' collection on " +
+                    "{_deletedOnce:1, _modified:1} with a 
partialFilterExpression " +
+                    "{_deletedOnce:true}. Partial indexes require MongoDB 3.2 
" +
+                    "or higher.");
         }
 
         // compound index on _sdType and _sdMaxRevTime
@@ -310,26 +323,14 @@ public class MongoDocumentStore implemen
             createIndex(nodes, new String[]{SD_TYPE, SD_MAX_REV_TIME_IN_SECS},
                     new boolean[]{true, true}, false, true);
         } else if (!hasIndex(nodes, SD_TYPE, SD_MAX_REV_TIME_IN_SECS)) {
-            if (!builder.getReadOnlyMode()) {
-                LOG.warn("Detected an upgrade from Oak version <= 1.6. For 
optimal " +
-                        "Revision GC performance it is recommended to create a 
" +
-                        "sparse compound index for the 'nodes' collection on " 
+
-                        "{_sdType:1, _sdMaxRevTime:1}.");
-            }
+            LOG.warn("Detected an upgrade from Oak version <= 1.6. For optimal 
" +
+                    "Revision GC performance it is recommended to create a " +
+                    "sparse compound index for the 'nodes' collection on " +
+                    "{_sdType:1, _sdMaxRevTime:1}.");
         }
 
         // index on _modified for journal entries
         createIndex(journal, JournalEntry.MODIFIED, true, false, false);
-
-        this.nodeLocks = new StripedNodeDocumentLocks();
-        this.nodesCache = builder.buildNodeDocumentCache(this, nodeLocks);
-
-        LOG.info("Connected to MongoDB {} with maxReplicationLagMillis {}, " +
-                "maxDeltaForModTimeIdxSecs {}, disableIndexHint {}, " +
-                "{}, serverStatus {}",
-                mongoStatus.getVersion(), maxReplicationLagMillis, 
maxDeltaForModTimeIdxSecs,
-                disableIndexHint, db.getWriteConcern(),
-                mongoStatus.getServerDetails());
     }
 
     public boolean isReadOnly() {

Modified: 
jackrabbit/oak/branches/1.8/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java?rev=1857555&r1=1857554&r2=1857555&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.8/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java
 (original)
+++ 
jackrabbit/oak/branches/1.8/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java
 Mon Apr 15 09:42:25 2019
@@ -92,6 +92,26 @@ public class MongoDocumentStoreTest exte
         assertThat(info.keySet(), hasItem("settings.count"));
     }
 
+    @Test
+    public void readOnly() throws Exception {
+        // setup must have created nodes collection with index on _bin
+        assertTrue(hasIndex(store.getDBCollection(Collection.NODES), 
NodeDocument.HAS_BINARY_FLAG));
+        mk.dispose();
+        // remove the indexes
+        mongoConnection = connectionFactory.getConnection();
+        assertNotNull(mongoConnection);
+        DocumentMK.Builder builder = new DocumentMK.Builder();
+        store = new TestStore(mongoConnection.getDB(), builder);
+        store.getDBCollection(Collection.NODES).dropIndexes();
+        // must be gone now
+        assertFalse(hasIndex(store.getDBCollection(Collection.NODES), 
NodeDocument.HAS_BINARY_FLAG));
+
+        // start a new read-only DocumentNodeStore
+        mk = newBuilder(mongoConnection.getDB()).setReadOnlyMode().open();
+        // must still not exist when started in read-only mode
+        assertFalse(hasIndex(store.getDBCollection(Collection.NODES), 
NodeDocument.HAS_BINARY_FLAG));
+    }
+
     static final class TestStore extends MongoDocumentStore {
         TestStore(DB db, DocumentMK.Builder builder) {
             super(db, builder);


Reply via email to