Index: oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java =================================================================== --- oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java (revision 1857344) +++ oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java (working copy) @@ -261,6 +261,23 @@ 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 @@ 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 @@ 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 @@ 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() { Index: oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java =================================================================== --- oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java (revision 1857323) +++ oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTest.java (working copy) @@ -92,6 +92,26 @@ 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); Index: . =================================================================== --- . (revision 1857323) +++ . (working copy) Property changes on: . ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /jackrabbit/oak/trunk:r1857104