diff --git a/oak-benchmarks/pom.xml b/oak-benchmarks/pom.xml index 59958a3d3c0..d3982f3efcb 100644 --- a/oak-benchmarks/pom.xml +++ b/oak-benchmarks/pom.xml @@ -206,7 +206,7 @@ org.mongodb - mongo-java-driver + mongodb-driver-sync com.h2database diff --git a/oak-examples/standalone/pom.xml b/oak-examples/standalone/pom.xml index 04580efadd1..7cc9ec8e135 100644 --- a/oak-examples/standalone/pom.xml +++ b/oak-examples/standalone/pom.xml @@ -100,8 +100,7 @@ org.mongodb - mongo-java-driver - ${mongo.driver.version} + mongodb-driver-sync true diff --git a/oak-it/pom.xml b/oak-it/pom.xml index 1d296e13819..008d4cb596a 100644 --- a/oak-it/pom.xml +++ b/oak-it/pom.xml @@ -186,7 +186,7 @@ org.mongodb - mongo-java-driver + mongodb-driver-sync true test diff --git a/oak-jcr/pom.xml b/oak-jcr/pom.xml index 6eb3b6bc438..192346a4aa0 100644 --- a/oak-jcr/pom.xml +++ b/oak-jcr/pom.xml @@ -389,7 +389,7 @@ org.mongodb - mongo-java-driver + mongodb-driver-sync test diff --git a/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/cluster/NonLocalObservationIT.java b/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/cluster/NonLocalObservationIT.java index 4c18043c99d..6557e1c746e 100644 --- a/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/cluster/NonLocalObservationIT.java +++ b/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/cluster/NonLocalObservationIT.java @@ -48,7 +48,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; /** * Test for external events from another cluster node. @@ -108,7 +108,7 @@ public void dispose(NodeStore nodeStore) { nodeStores.remove(nodeStore); if (nodeStores.size() == 0) { try (MongoClient c = createClient()) { - c.dropDatabase(dbName); + c.getDatabase(dbName).drop(); } catch (Exception e) { log.error("dispose: Can't close Mongo", e); } diff --git a/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/RandomOpCompare.java b/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/RandomOpCompare.java index 656964ce522..6628c11a21e 100644 --- a/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/RandomOpCompare.java +++ b/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/RandomOpCompare.java @@ -38,7 +38,6 @@ import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection; import org.apache.jackrabbit.oak.spi.state.NodeStore; -import com.mongodb.DB; /** * A randomized test that writes to two repositories (using different storage diff --git a/oak-lucene/pom.xml b/oak-lucene/pom.xml index d3684328c59..758e2f7edd4 100644 --- a/oak-lucene/pom.xml +++ b/oak-lucene/pom.xml @@ -281,7 +281,7 @@ org.mongodb - mongo-java-driver + mongodb-driver-sync test diff --git a/oak-parent/pom.xml b/oak-parent/pom.xml index d86b32c6d3b..c868055d355 100644 --- a/oak-parent/pom.xml +++ b/oak-parent/pom.xml @@ -58,7 +58,7 @@ SegmentMK 4.7.2 8.11.1 - 3.12.14 + 5.2.0 1.7.36 1.7.36 1.2.13 @@ -558,9 +558,9 @@ org.mongodb - mongo-java-driver + mongodb-driver-sync ${mongo.driver.version} - + org.easymock easymock diff --git a/oak-pojosr/pom.xml b/oak-pojosr/pom.xml index 2b1c01a8f83..f7a7dd50fdc 100644 --- a/oak-pojosr/pom.xml +++ b/oak-pojosr/pom.xml @@ -121,7 +121,7 @@ org.mongodb - mongo-java-driver + mongodb-driver-sync true diff --git a/oak-run-commons/pom.xml b/oak-run-commons/pom.xml index d46e5ea5d63..f4961f635e0 100644 --- a/oak-run-commons/pom.xml +++ b/oak-run-commons/pom.xml @@ -117,7 +117,7 @@ org.mongodb - mongo-java-driver + mongodb-driver-sync commons-io diff --git a/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/fixture/CompositeStoreFixture.java b/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/fixture/CompositeStoreFixture.java index 4cfc278b98a..851fbfe67ac 100644 --- a/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/fixture/CompositeStoreFixture.java +++ b/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/fixture/CompositeStoreFixture.java @@ -43,7 +43,7 @@ import java.util.ArrayList; import java.util.List; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; import static java.util.Arrays.asList; import static org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentNodeStoreBuilder.newMongoDocumentNodeStoreBuilder; @@ -109,7 +109,7 @@ static OakFixture newCompositeMongoFixture(String name, String uri, boolean drop boolean throttlingEnabled) { return new CompositeStoreFixture(name) { - private String database = new MongoClientURI(uri).getDatabase(); + private String database = new ConnectionString(uri).getDatabase(); private DocumentNodeStore ns; @Override diff --git a/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/DocumentStoreIndexerBase.java b/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/DocumentStoreIndexerBase.java index 1a25acc7c1d..7e941f50961 100644 --- a/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/DocumentStoreIndexerBase.java +++ b/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/DocumentStoreIndexerBase.java @@ -20,7 +20,8 @@ package org.apache.jackrabbit.oak.index.indexer.document; import com.codahale.metrics.MetricRegistry; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; +import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.apache.jackrabbit.guava.common.base.Stopwatch; import org.apache.jackrabbit.guava.common.io.Closer; @@ -61,6 +62,7 @@ import org.apache.jackrabbit.oak.spi.state.NodeStateUtils; import org.apache.jackrabbit.oak.spi.state.NodeStore; import org.apache.jackrabbit.oak.stats.StatisticsProvider; +import org.bson.Document; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -483,8 +485,8 @@ private MongoDocumentStore getMongoDocumentStore() { return requireNonNull(indexHelper.getService(MongoDocumentStore.class)); } - private MongoClientURI getMongoClientURI() { - return requireNonNull(indexHelper.getService(MongoClientURI.class)); + private ConnectionString getMongoClientURI() { + return requireNonNull(indexHelper.getService(ConnectionString.class)); } private MongoDatabase getMongoDatabase() { @@ -508,7 +510,8 @@ private void configureEstimators(IndexingProgressReporter progressReporter) { private long getEstimatedDocumentCount() { MongoConnection mongoConnection = indexHelper.getService(MongoConnection.class); if (mongoConnection != null) { - return mongoConnection.getDatabase().getCollection("nodes").count(); + MongoCollection nodesCollection = mongoConnection.getDatabase().getCollection("nodes"); + return nodesCollection.countDocuments(); } return 0; } diff --git a/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/FlatFileNodeStoreBuilder.java b/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/FlatFileNodeStoreBuilder.java index 60cfc3037c6..1d85447ac8a 100644 --- a/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/FlatFileNodeStoreBuilder.java +++ b/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/FlatFileNodeStoreBuilder.java @@ -19,7 +19,7 @@ package org.apache.jackrabbit.oak.index.indexer.document.flatfile; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; import com.mongodb.client.MongoDatabase; import org.apache.commons.lang3.StringUtils; import org.apache.jackrabbit.guava.common.collect.Iterables; @@ -112,7 +112,7 @@ public class FlatFileNodeStoreBuilder { private String checkpoint; private StatisticsProvider statisticsProvider = StatisticsProvider.NOOP; private IndexingReporter indexingReporter = IndexingReporter.NOOP; - private MongoClientURI mongoClientURI; + private ConnectionString mongoClientURI; private boolean withAheadOfTimeBlobDownloading = false; public enum SortStrategyType { @@ -191,7 +191,7 @@ public FlatFileNodeStoreBuilder withCheckpoint(String checkpoint) { return this; } - public FlatFileNodeStoreBuilder withMongoClientURI(MongoClientURI mongoClientURI) { + public FlatFileNodeStoreBuilder withMongoClientURI(ConnectionString mongoClientURI) { this.mongoClientURI = mongoClientURI; return this; } diff --git a/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedMongoDownloadTask.java b/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedMongoDownloadTask.java index 2c7c78cd75c..10e61148af2 100644 --- a/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedMongoDownloadTask.java +++ b/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedMongoDownloadTask.java @@ -20,7 +20,6 @@ import com.mongodb.ConnectionString; import com.mongodb.MongoClientSettings; -import com.mongodb.MongoClientURI; import com.mongodb.MongoException; import com.mongodb.MongoIncompatibleDriverException; import com.mongodb.MongoInterruptedException; @@ -204,7 +203,7 @@ public String toString() { static final String THREAD_NAME_PREFIX = "mongo-dump"; - private final MongoClientURI mongoClientURI; + private final ConnectionString mongoClientURI; private final MongoDocumentStore docStore; private final int maxBatchSizeBytes; private final int maxBatchNumberOfDocuments; @@ -229,7 +228,7 @@ public String toString() { private final DownloadStageStatistics downloadStageStatistics = new DownloadStageStatistics(); private Instant lastDelayedEnqueueWarningMessageLoggedTimestamp = Instant.now(); - public PipelinedMongoDownloadTask(MongoClientURI mongoClientURI, + public PipelinedMongoDownloadTask(ConnectionString mongoClientURI, MongoDocumentStore docStore, int maxBatchSizeBytes, int maxBatchNumberOfDocuments, @@ -240,7 +239,7 @@ public PipelinedMongoDownloadTask(MongoClientURI mongoClientURI, this(mongoClientURI, docStore, maxBatchSizeBytes, maxBatchNumberOfDocuments, queue, pathFilters, statisticsProvider, reporter, new ThreadFactoryBuilder().setDaemon(true).build()); } - public PipelinedMongoDownloadTask(MongoClientURI mongoClientURI, + public PipelinedMongoDownloadTask(ConnectionString mongoClientURI, MongoDocumentStore docStore, int maxBatchSizeBytes, int maxBatchNumberOfDocuments, @@ -343,7 +342,7 @@ public Result call() throws Exception { ); MongoClientSettings.Builder settingsBuilder = MongoClientSettings.builder() - .applyConnectionString(new ConnectionString(mongoClientURI.getURI())) + .applyConnectionString(mongoClientURI) .readPreference(ReadPreference.secondaryPreferred()); if (parallelDump && parallelDumpSecondariesOnly) { // Set a custom server selector that is able to distribute the two connections between the two secondaries. diff --git a/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedStrategy.java b/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedStrategy.java index a3417a769bf..b212ca1f7cf 100644 --- a/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedStrategy.java +++ b/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedStrategy.java @@ -18,7 +18,7 @@ */ package org.apache.jackrabbit.oak.index.indexer.document.flatfile.pipelined; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; import org.apache.commons.io.FileUtils; import org.apache.jackrabbit.guava.common.base.Preconditions; import org.apache.jackrabbit.guava.common.base.Stopwatch; @@ -179,7 +179,7 @@ private static void prettyPrintTransformStatisticsHistograms(TransformStageStati } private final MongoDocumentStore docStore; - private final MongoClientURI mongoClientURI; + private final ConnectionString mongoClientURI; private final DocumentNodeStore documentNodeStore; private final RevisionVector rootRevision; private final BlobStore blobStore; @@ -204,7 +204,7 @@ private static void prettyPrintTransformStatisticsHistograms(TransformStageStati * @param statisticsProvider Used to collect statistics about the indexing process. * @param indexingReporter Used to collect diagnostics, metrics and statistics and report them at the end of the indexing process. */ - public PipelinedStrategy(MongoClientURI mongoClientURI, + public PipelinedStrategy(ConnectionString mongoClientURI, MongoDocumentStore documentStore, DocumentNodeStore documentNodeStore, RevisionVector rootRevision, diff --git a/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedTreeStoreStrategy.java b/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedTreeStoreStrategy.java index b5cb0371136..642c50a41fe 100644 --- a/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedTreeStoreStrategy.java +++ b/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedTreeStoreStrategy.java @@ -18,7 +18,7 @@ */ package org.apache.jackrabbit.oak.index.indexer.document.flatfile.pipelined; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; import org.apache.commons.io.FileUtils; import org.apache.jackrabbit.guava.common.base.Preconditions; import org.apache.jackrabbit.guava.common.base.Stopwatch; @@ -166,7 +166,7 @@ private static void prettyPrintTransformStatisticsHistograms(TransformStageStati } private final MongoDocumentStore docStore; - private final MongoClientURI mongoClientURI; + private final ConnectionString mongoClientURI; private final DocumentNodeStore documentNodeStore; private final RevisionVector rootRevision; private final BlobStore blobStore; @@ -190,7 +190,7 @@ private static void prettyPrintTransformStatisticsHistograms(TransformStageStati * @param statisticsProvider Used to collect statistics about the indexing process. * @param indexingReporter Used to collect diagnostics, metrics and statistics and report them at the end of the indexing process. */ - public PipelinedTreeStoreStrategy(MongoClientURI mongoClientURI, + public PipelinedTreeStoreStrategy(ConnectionString mongoClientURI, MongoDocumentStore documentStore, DocumentNodeStore documentNodeStore, RevisionVector rootRevision, diff --git a/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/run/cli/DocumentFixtureProvider.java b/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/run/cli/DocumentFixtureProvider.java index 8535682ddf5..a4f46e9dc36 100644 --- a/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/run/cli/DocumentFixtureProvider.java +++ b/oak-run-commons/src/main/java/org/apache/jackrabbit/oak/run/cli/DocumentFixtureProvider.java @@ -22,9 +22,9 @@ import javax.sql.DataSource; +import com.mongodb.ConnectionString; import com.mongodb.client.MongoDatabase; import org.apache.jackrabbit.guava.common.io.Closer; -import com.mongodb.MongoClientURI; import org.apache.commons.io.FileUtils; import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore; import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStoreBuilder; @@ -101,14 +101,14 @@ static DocumentNodeStore configureDocumentMk(Options options, DocumentNodeStore dns; if (commonOpts.isMongo()) { - MongoClientURI uri = new MongoClientURI(commonOpts.getStoreArg()); + ConnectionString uri = new ConnectionString(commonOpts.getStoreArg()); if (uri.getDatabase() == null) { System.err.println("Database missing in MongoDB URI: " - + uri.getURI()); + + uri); System.exit(1); } - MongoConnection mongo = new MongoConnection(uri.getURI()); - wb.register(MongoClientURI.class, uri, emptyMap()); + MongoConnection mongo = new MongoConnection(uri.getConnectionString()); + wb.register(ConnectionString.class, uri, emptyMap()); wb.register(MongoConnection.class, mongo, emptyMap()); wb.register(MongoDatabase.class, mongo.getDatabase(), emptyMap()); closer.register(mongo::close); diff --git a/oak-run-commons/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/MongoTestBackend.java b/oak-run-commons/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/MongoTestBackend.java index 243939a9c07..a446a7617b6 100644 --- a/oak-run-commons/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/MongoTestBackend.java +++ b/oak-run-commons/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/MongoTestBackend.java @@ -18,7 +18,7 @@ */ package org.apache.jackrabbit.oak.index.indexer.document.flatfile.pipelined; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; import com.mongodb.client.MongoDatabase; import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore; import org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore; @@ -27,12 +27,12 @@ import java.io.IOException; class MongoTestBackend implements Closeable { - final MongoClientURI mongoClientURI; + final ConnectionString mongoClientURI; final MongoDocumentStore mongoDocumentStore; final DocumentNodeStore documentNodeStore; final MongoDatabase mongoDatabase; - public MongoTestBackend(MongoClientURI mongoClientURI, MongoDocumentStore mongoDocumentStore, DocumentNodeStore documentNodeStore, MongoDatabase mongoDatabase) { + public MongoTestBackend(ConnectionString mongoClientURI, MongoDocumentStore mongoDocumentStore, DocumentNodeStore documentNodeStore, MongoDatabase mongoDatabase) { this.mongoClientURI = mongoClientURI; this.mongoDocumentStore = mongoDocumentStore; this.documentNodeStore = documentNodeStore; diff --git a/oak-run-commons/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelineITUtil.java b/oak-run-commons/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelineITUtil.java index b5543a30fa6..020880f8b9e 100644 --- a/oak-run-commons/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelineITUtil.java +++ b/oak-run-commons/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelineITUtil.java @@ -18,7 +18,7 @@ */ package org.apache.jackrabbit.oak.index.indexer.document.flatfile.pipelined; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; import org.apache.jackrabbit.oak.api.CommitFailedException; import org.apache.jackrabbit.oak.commons.Compression; import org.apache.jackrabbit.oak.plugins.document.DocumentMK; @@ -179,7 +179,7 @@ static MongoTestBackend createNodeStore(boolean readOnly, MongoConnectionFactory } static MongoTestBackend createNodeStore(boolean readOnly, String mongoUri, DocumentMKBuilderProvider builderProvider) { - MongoClientURI mongoClientUri = new MongoClientURI(mongoUri); + ConnectionString mongoClientUri = new ConnectionString(mongoUri); DocumentMK.Builder builder = builderProvider.newBuilder(); builder.setMongoDB(mongoUri, "oak", 0); if (readOnly) { diff --git a/oak-run-commons/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedMongoDownloadTaskTest.java b/oak-run-commons/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedMongoDownloadTaskTest.java index 7f4eb4a9476..36b74d2e7f8 100644 --- a/oak-run-commons/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedMongoDownloadTaskTest.java +++ b/oak-run-commons/src/test/java/org/apache/jackrabbit/oak/index/indexer/document/flatfile/pipelined/PipelinedMongoDownloadTaskTest.java @@ -18,7 +18,7 @@ */ package org.apache.jackrabbit.oak.index.indexer.document.flatfile.pipelined; -import com.mongodb.MongoClient; +import com.mongodb.MongoClientSettings; import com.mongodb.client.model.Filters; import org.apache.jackrabbit.oak.index.indexer.document.flatfile.pipelined.MongoRegexPathFilterFactory.MongoFilterPaths; import org.apache.jackrabbit.oak.plugins.document.NodeDocument; @@ -336,8 +336,8 @@ private void assertBsonEquals(Bson actual, Bson expected) { throw new AssertionError("One of the bson is null. Actual: " + actual + ", expected: " + expected); } assertEquals( - actual.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry()), - expected.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry()) + actual.toBsonDocument(BsonDocument.class, MongoClientSettings.getDefaultCodecRegistry()), + expected.toBsonDocument(BsonDocument.class, MongoClientSettings.getDefaultCodecRegistry()) ); } diff --git a/oak-run/pom.xml b/oak-run/pom.xml index ff542d2e3d6..784f6511acf 100644 --- a/oak-run/pom.xml +++ b/oak-run/pom.xml @@ -296,7 +296,7 @@ org.mongodb - mongo-java-driver + mongodb-driver-sync org.mapdb diff --git a/oak-run/src/main/java/org/apache/jackrabbit/oak/run/DataStoreCheckCommand.java b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/DataStoreCheckCommand.java index facb06396c7..a6ddb3d32d2 100644 --- a/oak-run/src/main/java/org/apache/jackrabbit/oak/run/DataStoreCheckCommand.java +++ b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/DataStoreCheckCommand.java @@ -50,9 +50,9 @@ import org.apache.jackrabbit.guava.common.io.Closeables; import org.apache.jackrabbit.guava.common.io.Closer; import org.apache.jackrabbit.guava.common.io.Files; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientURI; -import com.mongodb.MongoURI; +import com.mongodb.ConnectionString; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; import joptsimple.ArgumentAcceptingOptionSpec; import joptsimple.OptionParser; import joptsimple.OptionSet; @@ -185,9 +185,9 @@ static int checkDataStore(String... args) { NodeStore nodeStore = null; if (options.has(store)) { String source = options.valueOf(store); - if (source.startsWith(MongoURI.MONGODB_PREFIX)) { - MongoClientURI uri = new MongoClientURI(source); - MongoClient client = new MongoClient(uri); + if (source.startsWith("mongodb://")) { + ConnectionString uri = new ConnectionString(source); + MongoClient client = MongoClients.create(uri); DocumentNodeStore docNodeStore = newMongoDocumentNodeStoreBuilder().setMongoDB(client, uri.getDatabase()).build(); closer.register(Utils.asCloseable(docNodeStore)); diff --git a/oak-run/src/main/java/org/apache/jackrabbit/oak/run/UnlockUpgradeCommand.java b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/UnlockUpgradeCommand.java index a266a84fd78..71766b23141 100644 --- a/oak-run/src/main/java/org/apache/jackrabbit/oak/run/UnlockUpgradeCommand.java +++ b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/UnlockUpgradeCommand.java @@ -22,8 +22,7 @@ import javax.sql.DataSource; -import com.mongodb.MongoClientURI; - +import com.mongodb.ConnectionString; import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStoreBuilder; import org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentNodeStoreBuilder; import org.apache.jackrabbit.oak.run.commons.Command; @@ -38,7 +37,6 @@ import joptsimple.OptionSet; import joptsimple.OptionSpec; -import static com.mongodb.MongoURI.MONGODB_PREFIX; import static java.util.Arrays.asList; import static org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore.VERSION; @@ -74,12 +72,12 @@ public void execute(String... args) throws Exception { DocumentStore store = null; try { String uri = nonOptions.get(0); - if (uri.startsWith(MONGODB_PREFIX)) { - MongoClientURI clientURI = new MongoClientURI(uri); + if (uri.startsWith("mongodb://")) { + ConnectionString clientURI = new ConnectionString(uri); if (clientURI.getDatabase() == null) { - System.err.println("Database missing in MongoDB URI: " + clientURI.getURI()); + System.err.println("Database missing in MongoDB URI: " + clientURI); } else { - MongoConnection mongo = new MongoConnection(clientURI.getURI()); + MongoConnection mongo = new MongoConnection(uri); store = new MongoDocumentStore( mongo.getMongoClient(), mongo.getDatabase(), new MongoDocumentNodeStoreBuilder()); diff --git a/oak-run/src/main/java/org/apache/jackrabbit/oak/run/Utils.java b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/Utils.java index f202486bc9e..284e36c42e6 100644 --- a/oak-run/src/main/java/org/apache/jackrabbit/oak/run/Utils.java +++ b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/Utils.java @@ -17,7 +17,6 @@ package org.apache.jackrabbit.oak.run; -import static com.mongodb.MongoURI.MONGODB_PREFIX; import static java.util.Arrays.asList; import static java.util.Objects.isNull; import static java.util.Optional.empty; @@ -67,12 +66,10 @@ import org.apache.jackrabbit.oak.spi.blob.GarbageCollectableBlobStore; import org.apache.jackrabbit.oak.spi.state.NodeStore; import org.jetbrains.annotations.Nullable; - +import com.mongodb.ConnectionString; import org.apache.jackrabbit.guava.common.collect.Maps; import org.apache.jackrabbit.guava.common.io.Closer; import org.apache.jackrabbit.guava.common.io.Files; -import com.mongodb.MongoClientURI; -import com.mongodb.MongoURI; import joptsimple.ArgumentAcceptingOptionSpec; import joptsimple.OptionParser; @@ -184,7 +181,7 @@ public static NodeStore bootstrapNodeStore(NodeStoreOptions options, Closer clos System.exit(1); } - if (src.startsWith(MongoURI.MONGODB_PREFIX) || src.startsWith("jdbc")) { + if (src.startsWith("mongodb://") || src.startsWith("jdbc")) { DocumentNodeStoreBuilder builder = createDocumentMKBuilder(options, closer); if (builder != null) { if (readOnlyMode) { @@ -218,7 +215,7 @@ static DocumentNodeStoreBuilder createDocumentMKBuilder(String[] args, Closer static Optional getMongoConnection(final NodeStoreOptions options, final Closer closer) { String src = options.getStoreArg(); - if (isNull(src) || !src.startsWith(MONGODB_PREFIX)) { + if (isNull(src) || !src.startsWith("mongodb://")) { return empty(); } @@ -235,7 +232,7 @@ static DocumentNodeStoreBuilder createDocumentMKBuilder(NodeStoreOptions opti System.exit(1); } DocumentNodeStoreBuilder builder; - if (src.startsWith(MONGODB_PREFIX)) { + if (src.startsWith("mongodb://")) { MongoConnection mongo = getMongoConnection(closer, src); builder = newMongoDocumentNodeStoreBuilder().setMongoDB(mongo.getMongoClient(), mongo.getDBName()); } else if (src.startsWith("jdbc")) { @@ -262,12 +259,12 @@ static DocumentNodeStoreBuilder createDocumentMKBuilder(NodeStoreOptions opti } private static MongoConnection getMongoConnection(Closer closer, String src) { - MongoClientURI uri = new MongoClientURI(src); + ConnectionString uri = new ConnectionString(src); if (uri.getDatabase() == null) { - System.err.println("Database missing in MongoDB URI: " + uri.getURI()); + System.err.println("Database missing in MongoDB URI: " + uri); System.exit(1); } - MongoConnection mongo = new MongoConnection(uri.getURI()); + MongoConnection mongo = new MongoConnection(src); closer.register(asCloseable(mongo)); return mongo; } diff --git a/oak-run/src/test/java/org/apache/jackrabbit/oak/index/DocumentStoreIndexerIT.java b/oak-run/src/test/java/org/apache/jackrabbit/oak/index/DocumentStoreIndexerIT.java index 317c5bca738..8fea020b2a3 100644 --- a/oak-run/src/test/java/org/apache/jackrabbit/oak/index/DocumentStoreIndexerIT.java +++ b/oak-run/src/test/java/org/apache/jackrabbit/oak/index/DocumentStoreIndexerIT.java @@ -20,7 +20,7 @@ package org.apache.jackrabbit.oak.index; import com.codahale.metrics.Counter; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; import com.mongodb.client.MongoDatabase; import org.apache.jackrabbit.guava.common.collect.Iterators; import org.apache.jackrabbit.oak.InitialContent; @@ -319,7 +319,7 @@ public void bundling() throws Exception { Whiteboard wb = new DefaultWhiteboard(); MongoDocumentStore ds = (MongoDocumentStore) docBuilder.getDocumentStore(); Registration r1 = wb.register(MongoDocumentStore.class, ds, emptyMap()); - wb.register(MongoClientURI.class, c1.getMongoURI(), emptyMap()); + wb.register(ConnectionString.class, c1.getMongoURI(), emptyMap()); wb.register(StatisticsProvider.class, StatisticsProvider.NOOP, emptyMap()); wb.register(IndexingReporter.class, IndexingReporter.NOOP, emptyMap()); Registration c1Registration = wb.register(MongoDatabase.class, c1.getDatabase(), emptyMap()); @@ -408,7 +408,7 @@ public void metrics() throws Exception { wb.register(StatisticsProvider.class, metricsStatisticsProvider, emptyMap()); wb.register(IndexingReporter.class, new ConsoleIndexingReporter(), emptyMap()); Registration c1Registration = wb.register(MongoDatabase.class, mongoConnection.getDatabase(), emptyMap()); - wb.register(MongoClientURI.class, mongoConnection.getMongoURI(), emptyMap()); + wb.register(ConnectionString.class, mongoConnection.getMongoURI(), emptyMap()); configureIndex(store); diff --git a/oak-run/src/test/java/org/apache/jackrabbit/oak/index/IncrementalStoreTest.java b/oak-run/src/test/java/org/apache/jackrabbit/oak/index/IncrementalStoreTest.java index 7dbe79b31f5..2345cad292f 100644 --- a/oak-run/src/test/java/org/apache/jackrabbit/oak/index/IncrementalStoreTest.java +++ b/oak-run/src/test/java/org/apache/jackrabbit/oak/index/IncrementalStoreTest.java @@ -20,7 +20,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; import com.mongodb.client.MongoDatabase; import org.apache.commons.collections4.set.ListOrderedSet; import org.apache.commons.io.IOUtils; @@ -723,13 +723,13 @@ private Backend createNodeStore(boolean readOnly) { static class Backend { - private final MongoClientURI mongoURI; + private final ConnectionString mongoURI; final MongoDocumentStore mongoDocumentStore; final DocumentNodeStore documentNodeStore; final MongoDatabase mongoDatabase; final BlobStore blobStore; - public Backend(MongoClientURI mongoURI, MongoDocumentStore mongoDocumentStore, DocumentNodeStore documentNodeStore, MongoDatabase mongoDatabase, BlobStore blobStore) { + public Backend(ConnectionString mongoURI, MongoDocumentStore mongoDocumentStore, DocumentNodeStore documentNodeStore, MongoDatabase mongoDatabase, BlobStore blobStore) { this.mongoURI = mongoURI; this.mongoDocumentStore = mongoDocumentStore; this.documentNodeStore = documentNodeStore; diff --git a/oak-store-composite/pom.xml b/oak-store-composite/pom.xml index f05dda244f8..195404bfcf6 100644 --- a/oak-store-composite/pom.xml +++ b/oak-store-composite/pom.xml @@ -212,7 +212,7 @@ org.mongodb - mongo-java-driver + mongodb-driver-sync true test diff --git a/oak-store-document/pom.xml b/oak-store-document/pom.xml index 8b299b2ee7c..41686c0d3e4 100644 --- a/oak-store-document/pom.xml +++ b/oak-store-document/pom.xml @@ -43,8 +43,8 @@ - com.mongodb*;version="[3.8, 4)";resolution:=optional, - org.bson*;version="[3.8, 4)";resolution:=optional, + com.mongodb*;version="[5.0, 5.2)";resolution:=optional, + org.bson*;version="[5.0, 5.2)";resolution:=optional, * @@ -161,7 +161,7 @@ org.mongodb - mongo-java-driver + mongodb-driver-sync true diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java index 115951f080e..1349fb8e17d 100644 --- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java +++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java @@ -53,7 +53,8 @@ import org.apache.jackrabbit.guava.common.base.Strings; import org.apache.jackrabbit.guava.common.io.Closer; import org.apache.jackrabbit.guava.common.util.concurrent.UncheckedExecutionException; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings; import org.apache.commons.io.FilenameUtils; import org.apache.jackrabbit.commons.SimpleValueFactory; @@ -320,7 +321,11 @@ private void registerNodeStore() throws IOException { String db = config.db(); boolean soKeepAlive = config.socketKeepAlive(); - MongoClientURI mongoURI = new MongoClientURI(uri); + ConnectionString mongoURI = new ConnectionString(uri); + MongoClientSettings settings = MongoClientSettings.builder() + .applyConnectionString(mongoURI) + .build(); + String persistentCache = resolvePath(config.persistentCache(), DEFAULT_PERSISTENT_CACHE); String journalCache = resolvePath(config.journalCache(), DEFAULT_JOURNAL_CACHE); @@ -328,12 +333,12 @@ private void registerNodeStore() throws IOException { // Take care around not logging the uri directly as it // might contain passwords log.info("Starting DocumentNodeStore with host={}, db={}, cache size (MB)={}, persistentCache={}, " + - "journalCache={}, blobCacheSize (MB)={}, maxReplicationLagInSecs={}, " + - "clusterIdReuseDelayAfterRecoveryMillis={}, recoveryDelayMillis={}", + "journalCache={}, blobCacheSize (MB)={}, maxReplicationLagInSecs={}, " + + "clusterIdReuseDelayAfterRecoveryMillis={}, recoveryDelayMillis={}", mongoURI.getHosts(), db, config.cache(), persistentCache, journalCache, config.blobCacheSize(), config.maxReplicationLagInSecs(), config.clusterIdReuseDelayAfterRecoveryMillis(), config.recoveryDelayMillis()); - log.info("Mongo Connection details {}", MongoConnection.toString(mongoURI.getOptions())); + log.info("Mongo Connection details {}", MongoConnection.toString(settings)); } MongoDocumentNodeStoreBuilder builder = newMongoDocumentNodeStoreBuilder(); @@ -478,35 +483,36 @@ private void configureBuilder(DocumentNodeStoreBuilder builder) { String persistentCache = resolvePath(config.persistentCache(), DEFAULT_PERSISTENT_CACHE); String journalCache = resolvePath(config.journalCache(), DEFAULT_JOURNAL_CACHE); final Tracker leaseFailureHandlerTracker = whiteboard.track(LeaseFailureHandler.class); - builder.setStatisticsProvider(statisticsProvider). - setExecutor(executor). - memoryCacheSize(config.cache() * MB). - memoryCacheDistribution( + builder.setStatisticsProvider(statisticsProvider) + .setExecutor(executor) + .memoryCacheSize(config.cache() * MB) + .memoryCacheDistribution( config.nodeCachePercentage(), config.prevDocCachePercentage(), config.childrenCachePercentage(), - config.diffCachePercentage()). - setCacheSegmentCount(config.cacheSegmentCount()). - setCacheStackMoveDistance(config.cacheStackMoveDistance()). - setBundlingDisabled(config.bundlingDisabled()). - setJournalPropertyHandlerFactory(journalPropertyHandlerFactory). - setLeaseCheckMode(ClusterNodeInfo.DEFAULT_LEASE_CHECK_DISABLED ? LeaseCheckMode.DISABLED : LeaseCheckMode.valueOf(config.leaseCheckMode())). - setPrefetchFeature(prefetchFeature). - setDocStoreThrottlingFeature(docStoreThrottlingFeature). - setNoChildOrderCleanupFeature(noChildOrderCleanupFeature). - setCancelInvalidationFeature(cancelInvalidationFeature). - setDocStoreFullGCFeature(docStoreFullGCFeature). - setDocStoreEmbeddedVerificationFeature(docStoreEmbeddedVerificationFeature). - setThrottlingEnabled(config.throttlingEnabled()). - setFullGCEnabled(config.fullGCEnabled()). - setFullGCIncludePaths(config.fullGCIncludePaths()). - setFullGCExcludePaths(config.fullGCExcludePaths()). - setEmbeddedVerificationEnabled(config.embeddedVerificationEnabled()). - setFullGCMode(config.fullGCMode()). - setSuspendTimeoutMillis(config.suspendTimeoutMillis()). - setClusterIdReuseDelayAfterRecovery(config.clusterIdReuseDelayAfterRecoveryMillis()). - setRecoveryDelayMillis(config.recoveryDelayMillis()). - setLeaseFailureHandler(new LeaseFailureHandler() { + config.diffCachePercentage()) + .setCacheSegmentCount(config.cacheSegmentCount()) + .setCacheStackMoveDistance(config.cacheStackMoveDistance()) + .setBundlingDisabled(config.bundlingDisabled()) + .setJournalPropertyHandlerFactory(journalPropertyHandlerFactory) + .setLeaseCheckMode( + ClusterNodeInfo.DEFAULT_LEASE_CHECK_DISABLED ? LeaseCheckMode.DISABLED : LeaseCheckMode.valueOf(config.leaseCheckMode())) + .setPrefetchFeature(prefetchFeature) + .setDocStoreThrottlingFeature(docStoreThrottlingFeature) + .setNoChildOrderCleanupFeature(noChildOrderCleanupFeature) + .setCancelInvalidationFeature(cancelInvalidationFeature) + .setDocStoreFullGCFeature(docStoreFullGCFeature) + .setDocStoreEmbeddedVerificationFeature(docStoreEmbeddedVerificationFeature) + .setThrottlingEnabled(config.throttlingEnabled()) + .setFullGCEnabled(config.fullGCEnabled()) + .setFullGCIncludePaths(config.fullGCIncludePaths()) + .setFullGCExcludePaths(config.fullGCExcludePaths()) + .setEmbeddedVerificationEnabled(config.embeddedVerificationEnabled()) + .setFullGCMode(config.fullGCMode()) + .setSuspendTimeoutMillis(config.suspendTimeoutMillis()) + .setClusterIdReuseDelayAfterRecovery(config.clusterIdReuseDelayAfterRecoveryMillis()) + .setRecoveryDelayMillis(config.recoveryDelayMillis()) + .setLeaseFailureHandler(new LeaseFailureHandler() { private final LeaseFailureHandler defaultLeaseFailureHandler = createDefaultLeaseFailureHandler(); @@ -535,11 +541,11 @@ public void handleLeaseFailure() { } } } - }). - setPrefetchExternalChanges(config.prefetchExternalChanges()). - setUpdateLimit(config.updateLimit()). - setJournalGCMaxAge(config.journalGCMaxAge()). - setNodeCachePathPredicate(createCachePredicate()); + }) + .setPrefetchExternalChanges(config.prefetchExternalChanges()) + .setUpdateLimit(config.updateLimit()) + .setJournalGCMaxAge(config.journalGCMaxAge()) + .setNodeCachePathPredicate(createCachePredicate()); if (!Strings.isNullOrEmpty(persistentCache)) { builder.setPersistentCache(persistentCache); diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoBlobStore.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoBlobStore.java index c391c371114..4b0b5e304f2 100644 --- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoBlobStore.java +++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoBlobStore.java @@ -39,7 +39,7 @@ import org.apache.jackrabbit.guava.common.collect.AbstractIterator; import com.mongodb.BasicDBObject; -import com.mongodb.MongoClient; +import com.mongodb.MongoClientSettings; import com.mongodb.MongoException; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCursor; @@ -68,7 +68,7 @@ public class MongoBlobStore extends CachingBlobStore { private static final CodecRegistry CODEC_REGISTRY = fromRegistries( fromCodecs(new MongoBlobCodec()), - MongoClient.getDefaultCodecRegistry() + MongoClientSettings.getDefaultCodecRegistry() ); private final ReadPreference defaultReadPreference; diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBConnection.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBConnection.java index 4ef0151f52d..6dfdda9f3d7 100644 --- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBConnection.java +++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBConnection.java @@ -17,14 +17,16 @@ package org.apache.jackrabbit.oak.plugins.document.mongo; import com.mongodb.BasicDBObject; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientOptions; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings.Builder; import com.mongodb.ReadConcernLevel; import com.mongodb.client.ClientSession; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; +import java.util.concurrent.TimeUnit; import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; @@ -34,8 +36,7 @@ import static org.apache.jackrabbit.oak.plugins.document.util.MongoConnection.readConcernLevel; /** - * Simple struct that contains {@code MongoClient}, {@code MongoDatabase} and - * {@code MongoStatus}. + * Simple struct that contains {@code MongoClient}, {@code MongoDatabase} and {@code MongoStatus}. */ final class MongoDBConnection { @@ -64,17 +65,25 @@ static MongoDBConnection newMongoDBConnection(@NotNull String uri, int socketTimeout, boolean socketKeepAlive) { CompositeServerMonitorListener serverMonitorListener = new CompositeServerMonitorListener(); - MongoClientOptions.Builder options = MongoConnection.getDefaultBuilder(); - options.addServerMonitorListener(serverMonitorListener); - options.socketKeepAlive(socketKeepAlive); - if (socketTimeout > 0) { - options.socketTimeout(socketTimeout); - } - MongoClient client = new MongoClient(new MongoClientURI(uri, options)); + + ConnectionString connectionString = new ConnectionString(uri); + Builder options = MongoConnection.getDefaultBuilder(); + options.applyConnectionString(connectionString); + options.applyToServerSettings(builder -> builder.addServerMonitorListener(serverMonitorListener)); + options.applyToSocketSettings(builder -> { + // It's not possible anymore setting keepalive, it was deprecated since at least 3.6.0 version + // builder.keepAlive(socketKeepAlive); + if (socketTimeout > 0) { + builder.readTimeout(socketTimeout, TimeUnit.MILLISECONDS); + } + }); + + MongoClient client = MongoClients.create(options.build()); + MongoStatus status = new MongoStatus(client, name); serverMonitorListener.addListener(status); MongoDatabase db = client.getDatabase(name); - if (!MongoConnection.hasWriteConcern(uri)) { + if (MongoConnection.hasMongoDbDefaultWriteConcern(uri)) { db = db.withWriteConcern(MongoConnection.getDefaultWriteConcern(client)); } if (status.isMajorityReadConcernSupported() diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentNodeStoreBuilderBase.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentNodeStoreBuilderBase.java index 46f6f7decdf..24ea29c3797 100644 --- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentNodeStoreBuilderBase.java +++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentNodeStoreBuilderBase.java @@ -18,7 +18,7 @@ import java.util.concurrent.TimeUnit; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; import org.apache.jackrabbit.oak.plugins.blob.ReferencedBlob; import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore; diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java index baaaf8b9aef..11bc8e83833 100644 --- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java +++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java @@ -37,6 +37,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.locks.Lock; +import java.util.function.Consumer; import java.util.stream.StreamSupport; import org.apache.jackrabbit.guava.common.base.Stopwatch; @@ -45,20 +46,9 @@ import org.apache.jackrabbit.guava.common.collect.Iterables; import org.apache.jackrabbit.guava.common.collect.Iterators; import org.apache.jackrabbit.guava.common.collect.Lists; +import org.apache.jackrabbit.guava.common.collect.Maps; import org.apache.jackrabbit.guava.common.io.Closeables; import org.apache.jackrabbit.guava.common.util.concurrent.AtomicDouble; -import com.mongodb.Block; -import com.mongodb.DBObject; -import com.mongodb.MongoBulkWriteException; -import com.mongodb.MongoWriteException; -import com.mongodb.MongoCommandException; -import com.mongodb.WriteError; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientURI; -import com.mongodb.ReadPreference; - -import com.mongodb.client.model.CreateCollectionOptions; - import org.apache.jackrabbit.guava.common.util.concurrent.UncheckedExecutionException; import org.apache.jackrabbit.oak.cache.CacheStats; import org.apache.jackrabbit.oak.cache.CacheValue; @@ -93,20 +83,28 @@ import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - -import org.apache.jackrabbit.guava.common.collect.Maps; import com.mongodb.BasicDBObject; +import com.mongodb.ConnectionString; +import com.mongodb.DBObject; +import com.mongodb.MongoBulkWriteException; +import com.mongodb.MongoClientSettings; +import com.mongodb.MongoCommandException; import com.mongodb.MongoException; +import com.mongodb.MongoWriteException; +import com.mongodb.ReadPreference; import com.mongodb.WriteConcern; +import com.mongodb.WriteError; import com.mongodb.bulk.BulkWriteError; import com.mongodb.bulk.BulkWriteResult; import com.mongodb.bulk.BulkWriteUpsert; import com.mongodb.client.ClientSession; import com.mongodb.client.FindIterable; +import com.mongodb.client.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.BulkWriteOptions; +import com.mongodb.client.model.CreateCollectionOptions; import com.mongodb.client.model.Filters; import com.mongodb.client.model.FindOneAndUpdateOptions; import com.mongodb.client.model.ReturnDocument; @@ -689,7 +687,7 @@ protected T findUncached(Collection collection, String k ReadPreference readPreference = getMongoReadPreference(collection, null, docReadPref); MongoCollection dbCollection = getDBCollection(collection, readPreference); - if(readPreference.isSlaveOk()){ + if (readPreference.isSecondaryOk()) { LOG.trace("Routing call to secondary for fetching [{}]", key); isSlaveOk = true; } @@ -835,7 +833,7 @@ && canUseModifiedTimeIdx(startValue)) { ReadPreference readPreference = getMongoReadPreference(collection, parentId, getDefaultReadPreference(collection)); - if(readPreference.isSlaveOk()){ + if (readPreference.isSecondaryOk()) { isSlaveOk = true; LOG.trace("Routing call to secondary for fetching children from [{}] to [{}]", fromKey, toKey); } @@ -1539,12 +1537,7 @@ private Map findDocuments(Collection collecti conditions.add(getByKeyQuery(key)); } MongoCollection dbCollection; - if (secondariesWithinAcceptableLag()) { - dbCollection = getDBCollection(collection); - } else { - lagTooHigh(); - dbCollection = getDBCollection(collection).withReadPreference(ReadPreference.primary()); - } + dbCollection = getDBCollection(collection); execute(session -> { FindIterable cursor; if (session != null) { @@ -1574,12 +1567,7 @@ private Map findDocumentsOneByOne(Collection Bson condition = getByKeyQuery(key); MongoCollection dbCollection; - if (secondariesWithinAcceptableLag()) { - dbCollection = getDBCollection(collection); - } else { - lagTooHigh(); - dbCollection = getDBCollection(collection).withReadPreference(ReadPreference.primary()); - } + dbCollection = getDBCollection(collection); execute(session -> { FindIterable cursor; if (session != null) { @@ -1774,7 +1762,7 @@ public void prefetch(Collection collection, ReadPreference readPreference = getMongoReadPreference(collection, null, getDefaultReadPreference(collection)); MongoCollection dbCollection = getDBCollection(collection, readPreference); - if (readPreference.isSlaveOk()) { + if (readPreference.isSecondaryOk()) { LOG.trace("Routing call to secondary for prefetching [{}]", keys); } @@ -1855,8 +1843,9 @@ private Map getModStamps(Iterable keys) Map modCounts = Maps.newHashMap(); nodes.withReadPreference(ReadPreference.primary()) - .find(Filters.in(Document.ID, keys)).projection(fields) - .forEach((Block) obj -> { + .find(Filters.in(Document.ID, keys)) + .projection(fields) + .forEach((Consumer) obj -> { String id = (String) obj.get(Document.ID); Long modCount = Utils.asLong((Number) obj.get(Document.MOD_COUNT)); if (modCount == null) { @@ -1902,10 +1891,10 @@ ReadPreference getMongoReadPreference(@NotNull Collection ReadPreference getMongoReadPreference(@NotNull Collection T withTransaction(@NotNull TransactionBody transactionBody) { @@ -179,5 +175,43 @@ public void close() { clock.advanceSessionAndClock(session); session.close(); } + + @Override + public Object getTransactionContext() { + return session.getTransactionContext(); + } + + @Override + public void setTransactionContext(ServerAddress address, Object transactionContext) { + session.setTransactionContext(address, transactionContext); + + } + + @Override + public void clearTransactionContext() { + session.clearTransactionContext(); + + } + + @Override + public void setSnapshotTimestamp(BsonTimestamp snapshotTimestamp) { + session.setSnapshotTimestamp(snapshotTimestamp); + + } + + @Override + public BsonTimestamp getSnapshotTimestamp() { + return session.getSnapshotTimestamp(); + } + + @Override + public TimeoutContext getTimeoutContext() { + return session.getTimeoutContext(); + } + + @Override + public void notifyOperationInitiated(Object operation) { + session.notifyOperationInitiated(operation); + } } } diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoStatus.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoStatus.java index 5ed5b7d5f2d..6d355bdc3d4 100644 --- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoStatus.java +++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoStatus.java @@ -20,7 +20,7 @@ import org.apache.jackrabbit.guava.common.collect.Maps; import com.mongodb.BasicDBObject; import com.mongodb.ClientSessionOptions; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; import com.mongodb.MongoClientException; import com.mongodb.MongoCommandException; import com.mongodb.MongoQueryException; diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoVersionGCSupport.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoVersionGCSupport.java index 6d741bf578a..69395abc3aa 100644 --- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoVersionGCSupport.java +++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoVersionGCSupport.java @@ -18,20 +18,21 @@ */ package org.apache.jackrabbit.oak.plugins.document.mongo; +import static com.mongodb.client.model.Filters.and; import static com.mongodb.client.model.Filters.eq; import static com.mongodb.client.model.Filters.exists; import static com.mongodb.client.model.Filters.gt; +import static com.mongodb.client.model.Filters.lt; import static com.mongodb.client.model.Filters.or; import static com.mongodb.client.model.Projections.include; + +import static java.util.Collections.emptyList; import static com.mongodb.client.model.Sorts.ascending; import static java.util.Optional.empty; import static java.util.Optional.ofNullable; import static org.apache.jackrabbit.guava.common.collect.Iterables.concat; import static org.apache.jackrabbit.guava.common.collect.Iterables.filter; import static org.apache.jackrabbit.guava.common.collect.Iterables.transform; -import static com.mongodb.client.model.Filters.and; -import static com.mongodb.client.model.Filters.lt; -import static java.util.Collections.emptyList; import static org.apache.jackrabbit.oak.plugins.document.Collection.NODES; import static org.apache.jackrabbit.oak.plugins.document.Document.ID; import static org.apache.jackrabbit.oak.plugins.document.NodeDocument.DELETED_ONCE; @@ -51,9 +52,8 @@ import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; - -import com.mongodb.client.MongoCursor; - +import org.apache.jackrabbit.guava.common.base.Joiner; +import org.apache.jackrabbit.guava.common.collect.Lists; import org.apache.jackrabbit.oak.commons.json.JsopBuilder; import org.apache.jackrabbit.oak.plugins.document.Document; import org.apache.jackrabbit.oak.plugins.document.NodeDocument; @@ -72,14 +72,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.apache.jackrabbit.guava.common.base.Joiner; - -import org.apache.jackrabbit.guava.common.collect.Lists; import com.mongodb.BasicDBObject; -import com.mongodb.Block; import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoCursor; import com.mongodb.client.model.Filters; +import com.mongodb.client.model.Sorts; /** * Mongo specific version of VersionGCSupport which uses mongo queries @@ -259,6 +257,7 @@ public Iterable getModifiedDocs(final long fromModified, final lon and(gt(MODIFIED_IN_SECS, getModifiedInSecs(fromModified)), lt(MODIFIED_IN_SECS, getModifiedInSecs(toModified))))); // first sort by _modified and then by _id + final Bson sort = ascending(MODIFIED_IN_SECS, ID); logQueryExplain("fullGC query explain details, hint : {} - explain : {}", query, modifiedIdHint); @@ -345,19 +344,17 @@ protected Iterable identifyGarbage(final Set gcTypes public long getOldestDeletedOnceTimestamp(Clock clock, long precisionMs) { LOG.debug("getOldestDeletedOnceTimestamp() <- start"); Bson query = Filters.eq(DELETED_ONCE, Boolean.TRUE); + Bson sort = ascending(MODIFIED_IN_SECS); List result = new ArrayList<>(1); getNodeCollection().find(query).sort(sort).limit(1).forEach( - new Block() { - @Override - public void apply(BasicDBObject document) { + document -> { NodeDocument doc = store.convertFromDBObject(NODES, document); long modifiedMs = doc.getModified() * TimeUnit.SECONDS.toMillis(1); if (LOG.isDebugEnabled()) { LOG.debug("getOldestDeletedOnceTimestamp() -> {}", Utils.timestampToString(modifiedMs)); } result.add(modifiedMs); - } }); if (result.isEmpty()) { LOG.debug("getOldestDeletedOnceTimestamp() -> none found, return current time"); @@ -374,6 +371,7 @@ public void apply(BasicDBObject document) { */ @Override public Optional getOldestModifiedDoc(final Clock clock) { + // we need to add query condition to ignore `previous` documents which doesn't have this field final Bson query = exists(MODIFIED_IN_SECS); // sort by MODIFIED_IN_SECS first, ID otherwise @@ -460,7 +458,7 @@ private void logSplitDocIdsTobeDeleted(Bson query) { getNodeCollection() .withReadPreference(store.getConfiguredReadPreference(NODES)) .find(query).projection(keys) - .forEach((Block) doc -> ids.add(getID(doc))); + .forEach(doc -> ids.add(getID(doc))); StringBuilder sb = new StringBuilder("Split documents with following ids were deleted as part of GC \n"); Joiner.on(System.getProperty("line.separator")).appendTo(sb, ids); diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReplicaSetStatus.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReplicaSetStatus.java index 41a45911b04..452c6c059ff 100644 --- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReplicaSetStatus.java +++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReplicaSetStatus.java @@ -30,7 +30,7 @@ import com.mongodb.ServerAddress; import com.mongodb.event.ServerHeartbeatSucceededEvent; -import com.mongodb.event.ServerMonitorListenerAdapter; +import com.mongodb.event.ServerMonitorListener; import org.bson.BsonArray; import org.bson.BsonDocument; @@ -47,7 +47,7 @@ * operations shouldn't be sent to a secondary when it lags too much behind, * otherwise the read operation will block until it was able to catch up. */ -public class ReplicaSetStatus extends ServerMonitorListenerAdapter { +public class ReplicaSetStatus implements ServerMonitorListener { private static final Logger LOG = LoggerFactory.getLogger(ReplicaSetStatus.class); diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/RevisionEntry.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/RevisionEntry.java index bdc6fcc0440..2c7d777d0a7 100644 --- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/RevisionEntry.java +++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/RevisionEntry.java @@ -27,7 +27,6 @@ import com.mongodb.BasicDBObject; import com.mongodb.DBObject; -import com.mongodb.util.JSON; import static java.util.Objects.requireNonNull; @@ -86,11 +85,6 @@ public Object removeField(String key) { throw new UnsupportedOperationException(); } - @Override - public boolean containsKey(String s) { - return containsField(s); - } - @Override public boolean containsField(String s) { return revision.toString().equals(s); diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/MongoConnection.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/MongoConnection.java index 8a9b7c8b06f..da145439da2 100644 --- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/MongoConnection.java +++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/MongoConnection.java @@ -21,15 +21,21 @@ import java.util.concurrent.TimeUnit; import org.apache.jackrabbit.guava.common.base.MoreObjects; -import org.apache.jackrabbit.guava.common.collect.ImmutableSet; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientOptions; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings; +import com.mongodb.MongoClientSettings.Builder; import com.mongodb.MongoException; import com.mongodb.ReadConcern; import com.mongodb.ReadConcernLevel; import com.mongodb.WriteConcern; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; import com.mongodb.client.MongoDatabase; +import com.mongodb.connection.ClusterDescription; +import com.mongodb.connection.ClusterType; +import com.mongodb.connection.ConnectionPoolSettings; +import com.mongodb.connection.ServerSettings; +import com.mongodb.connection.SocketSettings; import static java.util.Objects.requireNonNull; @@ -42,9 +48,12 @@ public class MongoConnection { private static final int DEFAULT_MAX_WAIT_TIME = (int) TimeUnit.MINUTES.toMillis(1); private static final int DEFAULT_HEARTBEAT_FREQUENCY_MS = (int) TimeUnit.SECONDS.toMillis(5); + + // TODO why using a non-sense "w"? values for w should those listed in the head comment of WriteConcern... private static final WriteConcern WC_UNKNOWN = new WriteConcern("unknown"); + private static final Set REPLICA_RC = Set.of(ReadConcernLevel.MAJORITY, ReadConcernLevel.LINEARIZABLE); - private final MongoClientURI mongoURI; + private final ConnectionString mongoURI; private final MongoClient mongo; /** @@ -66,10 +75,13 @@ public MongoConnection(String uri) throws MongoException { * @param builder the client option defaults. * @throws MongoException if there are failures */ - public MongoConnection(String uri, MongoClientOptions.Builder builder) + public MongoConnection(String uri, MongoClientSettings.Builder builder) throws MongoException { - mongoURI = new MongoClientURI(uri, builder); - mongo = new MongoClient(mongoURI); + + mongoURI = new ConnectionString(uri); + builder.applyConnectionString(mongoURI); + MongoClientSettings settings = builder.build(); + mongo = MongoClients.create(settings); } /** @@ -92,15 +104,19 @@ public MongoConnection(String host, int port, String database) * @param client the already connected client. */ public MongoConnection(String uri, MongoClient client) { - mongoURI = new MongoClientURI(uri, MongoConnection.getDefaultBuilder()); - mongo = client; + + Builder defaultBuilder = MongoConnection.getDefaultBuilder(); + mongoURI = new ConnectionString(uri); + defaultBuilder.applyConnectionString(mongoURI); + MongoClientSettings settings = defaultBuilder.build(); + mongo = MongoClients.create(settings); } /** * * @return the {@link MongoClientURI} for this connection */ - public MongoClientURI getMongoURI() { + public ConnectionString getMongoURI() { return mongoURI; } @@ -151,26 +167,33 @@ public void close() { * * @return builder with default options set */ - public static MongoClientOptions.Builder getDefaultBuilder() { - return new MongoClientOptions.Builder() - .description("MongoConnection for Oak DocumentMK") - .maxWaitTime(DEFAULT_MAX_WAIT_TIME) - .heartbeatFrequency(DEFAULT_HEARTBEAT_FREQUENCY_MS) - .threadsAllowedToBlockForConnectionMultiplier(100); + public static MongoClientSettings.Builder getDefaultBuilder() { + return MongoClientSettings.builder() + .applicationName("MongoConnection for Oak DocumentMK") + .applyToConnectionPoolSettings(builder -> builder + .maxWaitTime(DEFAULT_MAX_WAIT_TIME, java.util.concurrent.TimeUnit.MILLISECONDS)) + .applyToServerSettings(builder -> builder + .heartbeatFrequency(DEFAULT_HEARTBEAT_FREQUENCY_MS, java.util.concurrent.TimeUnit.MILLISECONDS)) + .applyToConnectionPoolSettings(builder -> builder + .maxSize(100)); } - public static String toString(MongoClientOptions opts) { - return MoreObjects.toStringHelper(opts) - .add("connectionsPerHost", opts.getConnectionsPerHost()) - .add("connectTimeout", opts.getConnectTimeout()) - .add("socketTimeout", opts.getSocketTimeout()) - .add("socketKeepAlive", opts.isSocketKeepAlive()) - .add("maxWaitTime", opts.getMaxWaitTime()) - .add("heartbeatFrequency", opts.getHeartbeatFrequency()) - .add("threadsAllowedToBlockForConnectionMultiplier", - opts.getThreadsAllowedToBlockForConnectionMultiplier()) - .add("readPreference", opts.getReadPreference().getName()) - .add("writeConcern", opts.getWriteConcern()) + public static String toString(MongoClientSettings settings) { + ConnectionPoolSettings poolSettings = settings.getConnectionPoolSettings(); + SocketSettings socketSettings = settings.getSocketSettings(); + ServerSettings serverSettings = settings.getServerSettings(); + + return MoreObjects.toStringHelper(settings) + .add("connectionsPerHost", poolSettings.getMaxSize()) + .add("connectTimeout", socketSettings.getConnectTimeout(java.util.concurrent.TimeUnit.MILLISECONDS)) + .add("socketTimeout", socketSettings.getReadTimeout(java.util.concurrent.TimeUnit.MILLISECONDS)) + .add("socketKeepAlive", "Unavailable in MongoClientSettings") + .add("maxWaitTime", poolSettings.getMaxWaitTime(java.util.concurrent.TimeUnit.MILLISECONDS)) + .add("heartbeatFrequency", serverSettings.getHeartbeatFrequency(java.util.concurrent.TimeUnit.MILLISECONDS)) + .add("threadsAllowedToBlockForConnectionMultiplier", "Handled via maxSize in connection pool") + .add("readPreference", settings.getReadPreference() + .getName()) + .add("writeConcern", settings.getWriteConcern()) .toString(); } @@ -180,12 +203,14 @@ public static String toString(MongoClientOptions opts) { * @return {@code true} if the URI has a write concern set, {@code false} * otherwise. */ - public static boolean hasWriteConcern(@NotNull String uri) { - MongoClientOptions.Builder builder = MongoClientOptions.builder(); - builder.writeConcern(WC_UNKNOWN); - WriteConcern wc = new MongoClientURI(requireNonNull(uri), builder) - .getOptions().getWriteConcern(); - return !WC_UNKNOWN.equals(wc); + public static boolean hasMongoDbDefaultWriteConcern(@NotNull String uri) { + ConnectionString connectionString = new ConnectionString(requireNonNull(uri)); + MongoClientSettings.Builder builder = MongoClientSettings.builder() + .applyConnectionString(connectionString); + MongoClientSettings settings = builder.build(); + + // ACKNOWLEDGE is the default of MongoClientSettings.Builder while the default of ConnectionString would be UNACKNOWLEDGED + return WriteConcern.ACKNOWLEDGED.equals(settings.getWriteConcern()); } /** @@ -195,9 +220,12 @@ public static boolean hasWriteConcern(@NotNull String uri) { * otherwise. */ public static boolean hasReadConcern(@NotNull String uri) { - ReadConcern rc = new MongoClientURI(requireNonNull(uri)) - .getOptions().getReadConcern(); - return readConcernLevel(rc) != null; + ConnectionString connectionString = new ConnectionString(requireNonNull(uri)); + MongoClientSettings.Builder builder = MongoClientSettings.builder() + .applyConnectionString(connectionString); + MongoClientSettings settings = builder.build(); + + return readConcernLevel(settings.getReadConcern()) != null; } /** @@ -211,13 +239,13 @@ public static boolean hasReadConcern(@NotNull String uri) { * @return the default write concern to use for Oak. */ public static WriteConcern getDefaultWriteConcern(@NotNull MongoClient client) { - WriteConcern w; - if (client.getReplicaSetStatus() != null) { - w = WriteConcern.MAJORITY; + ClusterDescription clusterDescription = client.getClusterDescription(); + + if (clusterDescription.getType() == ClusterType.REPLICA_SET) { + return WriteConcern.MAJORITY; } else { - w = WriteConcern.ACKNOWLEDGED; + return WriteConcern.ACKNOWLEDGED; } - return w; } /** @@ -233,7 +261,10 @@ public static WriteConcern getDefaultWriteConcern(@NotNull MongoClient client) { public static ReadConcern getDefaultReadConcern(@NotNull MongoClient client, @NotNull MongoDatabase db) { ReadConcern r; - if (requireNonNull(client).getReplicaSetStatus() != null && isMajorityWriteConcern(db)) { + + ClusterDescription clusterDescription = requireNonNull(client).getClusterDescription(); + + if (clusterDescription.getType() == ClusterType.REPLICA_SET && isMajorityWriteConcern(db)) { r = ReadConcern.MAJORITY; } else { r = ReadConcern.LOCAL; @@ -276,7 +307,9 @@ public static boolean isSufficientWriteConcern(@NotNull MongoClient client, throw new IllegalArgumentException( "Unknown write concern: " + wc); } - if (client.getReplicaSetStatus() != null) { + + ClusterDescription clusterDescription = client.getClusterDescription(); + if (clusterDescription.getType() == ClusterType.REPLICA_SET) { return w >= 2; } else { return w >= 1; @@ -295,7 +328,8 @@ public static boolean isSufficientWriteConcern(@NotNull MongoClient client, public static boolean isSufficientReadConcern(@NotNull MongoClient client, @NotNull ReadConcern rc) { ReadConcernLevel r = readConcernLevel(requireNonNull(rc)); - if (client.getReplicaSetStatus() == null) { + ClusterDescription clusterDescription = client.getClusterDescription(); + if (clusterDescription.getType() != ClusterType.REPLICA_SET) { return true; } else { return Objects.nonNull(r) && REPLICA_RC.contains(r); diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/fixture/DocumentMongoFixture.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/fixture/DocumentMongoFixture.java index e87f76ffb04..9446666d86f 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/fixture/DocumentMongoFixture.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/fixture/DocumentMongoFixture.java @@ -34,8 +34,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoDatabase; public class DocumentMongoFixture extends NodeStoreFixture { @@ -83,11 +85,11 @@ public NodeStore createNodeStore() { } protected MongoClient createClient() { - return new MongoClient(new MongoClientURI(uri)); + return MongoClients.create(uri); } protected String getDBName(String suffix) { - String dbName = new MongoClientURI(uri).getDatabase(); + String dbName = new ConnectionString(uri).getDatabase(); return dbName + "-" + suffix; } @@ -110,7 +112,8 @@ public void dispose(NodeStore nodeStore) { String suffix = suffixes.remove(nodeStore); if (suffix != null) { try (MongoClient client = createClient()) { - client.dropDatabase(getDBName(suffix)); + MongoDatabase database = client.getDatabase(getDBName(suffix)); + database.drop(); } catch (Exception e) { log.error("Can't close Mongo", e); } @@ -121,4 +124,4 @@ public void dispose(NodeStore nodeStore) { public String toString() { return "DocumentNodeStore[Mongo] on " + this.uri; } -} \ No newline at end of file +} diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMongoConnectionTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMongoConnectionTest.java index 0a0b45a06ab..6b360915b45 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMongoConnectionTest.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/AbstractMongoConnectionTest.java @@ -16,7 +16,7 @@ */ package org.apache.jackrabbit.oak.plugins.document; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection; import org.apache.jackrabbit.oak.stats.Clock; diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/BlobThroughPutTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/BlobThroughPutTest.java index cac59c145cd..ff32f035e26 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/BlobThroughPutTest.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/BlobThroughPutTest.java @@ -30,9 +30,10 @@ import org.apache.commons.collections4.bidimap.DualHashBidiMap; import org.apache.jackrabbit.guava.common.io.ByteStreams; import com.mongodb.BasicDBObject; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; import com.mongodb.WriteConcern; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Filters; @@ -60,13 +61,11 @@ public class BlobThroughPutTest { static { Map bimap = new DualHashBidiMap(); - bimap.put(WriteConcern.FSYNC_SAFE,"FSYNC_SAFE"); - bimap.put(WriteConcern.JOURNAL_SAFE,"JOURNAL_SAFE"); + bimap.put(WriteConcern.JOURNALED,"JOURNALED"); // bimap.put(WriteConcern.MAJORITY,"MAJORITY"); bimap.put(WriteConcern.UNACKNOWLEDGED,"UNACKNOWLEDGED"); - bimap.put(WriteConcern.NORMAL,"NORMAL"); // bimap.put(WriteConcern.REPLICAS_SAFE,"REPLICAS_SAFE"); - bimap.put(WriteConcern.SAFE,"SAFE"); + bimap.put(WriteConcern.ACKNOWLEDGED,"ACKNOWLEDGED"); namedConcerns = Collections.unmodifiableMap(bimap); } @@ -76,8 +75,11 @@ public class BlobThroughPutTest { @Ignore @Test public void performBenchMark() throws InterruptedException { - MongoClient local = new MongoClient(new MongoClientURI(localServer)); - MongoClient remote = new MongoClient(new MongoClientURI(remoteServer)); + ConnectionString localServerConnectionString = new ConnectionString(localServer); + MongoClient local = MongoClients.create(localServerConnectionString); + + ConnectionString remoteServerConnectionString = new ConnectionString(remoteServer); + MongoClient remote = MongoClients.create(remoteServerConnectionString); run(local, false, false); run(local, true, false); @@ -90,7 +92,8 @@ public void performBenchMark() throws InterruptedException { @Ignore @Test public void performBenchMark_WriteConcern() throws InterruptedException { - MongoClient mongo = new MongoClient(new MongoClientURI(remoteServer)); + ConnectionString remoteServerConnectionString = new ConnectionString(remoteServer); + MongoClient mongo = MongoClients.create(remoteServerConnectionString); final MongoDatabase db = mongo.getDatabase(TEST_DB1); final MongoCollection nodes = db.getCollection("nodes", BasicDBObject.class); final MongoCollection blobs = db.getCollection("blobs", BasicDBObject.class); @@ -128,7 +131,7 @@ private void run(MongoClient mongo, boolean useSameDB, boolean remote) throws In for (int writers : WRITERS) { prepareDB(nodes, blobs); final Benchmark b = new Benchmark(nodes, blobs); - Result r = b.run(readers, writers, remote, WriteConcern.SAFE); + Result r = b.run(readers, writers, remote, WriteConcern.ACKNOWLEDGED); results.add(r); } } diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/CacheConsistencyTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/CacheConsistencyTest.java index 30ae23a7029..04ed05d4974 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/CacheConsistencyTest.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/CacheConsistencyTest.java @@ -30,7 +30,7 @@ import org.apache.jackrabbit.guava.common.collect.Maps; import com.mongodb.DBObject; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import static org.apache.jackrabbit.oak.plugins.document.Collection.NODES; diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/ClusterInfoTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/ClusterInfoTest.java index 9d9f50a85ed..3e2bbd11324 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/ClusterInfoTest.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/ClusterInfoTest.java @@ -89,11 +89,11 @@ public void readWriteMode() throws InterruptedException { assertEquals(WriteConcern.MAJORITY, mem.getWriteConcern()); op = new UpdateOp(list.get(0).getId(), false); - op.set("readWriteMode", "read:nearest, write:fsynced"); + op.set("readWriteMode", "read:nearest, write:w2"); mem.findAndUpdate(Collection.CLUSTER_NODES, op); ns1.renewClusterIdLease(); assertEquals(ReadPreference.nearest(), mem.getReadPreference()); - assertEquals(WriteConcern.FSYNCED, mem.getWriteConcern()); + assertEquals(WriteConcern.W2, mem.getWriteConcern()); ns1.dispose(); ns2.dispose(); diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/ConcurrentPrefetchAndUpdateIT.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/ConcurrentPrefetchAndUpdateIT.java index 9645ec0c014..19732901439 100755 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/ConcurrentPrefetchAndUpdateIT.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/ConcurrentPrefetchAndUpdateIT.java @@ -24,7 +24,7 @@ import org.apache.jackrabbit.guava.common.collect.Lists; import com.mongodb.DBObject; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore; diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKBuilderTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKBuilderTest.java index 7cf6d9a9fd8..dc0b1a6402e 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKBuilderTest.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentMKBuilderTest.java @@ -17,7 +17,7 @@ package org.apache.jackrabbit.oak.plugins.document; import org.apache.jackrabbit.guava.common.collect.Iterables; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; import org.apache.jackrabbit.oak.cache.CacheStats; import org.junit.Test; diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreServiceTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreServiceTest.java index c0fb2299efa..689c91c1b7c 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreServiceTest.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreServiceTest.java @@ -23,11 +23,9 @@ import java.util.function.Supplier; import org.apache.jackrabbit.guava.common.collect.Maps; -import com.mongodb.MongoClient; import org.apache.commons.io.FilenameUtils; import org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore; -import org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStoreTestHelper; import org.apache.jackrabbit.oak.plugins.document.spi.JournalPropertyService; import org.apache.jackrabbit.oak.plugins.document.spi.lease.LeaseFailureHandler; import org.apache.jackrabbit.oak.spi.state.NodeStore; @@ -150,18 +148,6 @@ public void setUpdateLimit() throws Exception { assertEquals(17, store.getUpdateLimit()); } - @Test - public void keepAlive() throws Exception { - Map config = newConfig(repoHome); - config.put(DocumentNodeStoreServiceConfiguration.PROP_SO_KEEP_ALIVE, true); - MockOsgi.setConfigForPid(context.bundleContext(), PID, config); - MockOsgi.activate(service, context.bundleContext()); - DocumentNodeStore store = context.getService(DocumentNodeStore.class); - MongoDocumentStore mds = getMongoDocumentStore(store); - MongoClient client = MongoDocumentStoreTestHelper.getClient(mds); - assertTrue(client.getMongoClientOptions().isSocketKeepAlive()); - } - @Test public void continuousRGCDefault() throws Exception { Map config = newConfig(repoHome); @@ -217,8 +203,6 @@ public void preset() throws Exception { DocumentNodeStore store = context.getService(DocumentNodeStore.class); MongoDocumentStore mds = getMongoDocumentStore(store); assertNotNull(mds); - MongoClient client = MongoDocumentStoreTestHelper.getClient(mds); - assertTrue(client.getMongoClientOptions().isSocketKeepAlive()); } @Test @@ -233,11 +217,6 @@ public void presetOverride() throws Exception { MockOsgi.setConfigForPid(context.bundleContext(), PID, config); MockOsgi.activate(service, context.bundleContext()); - - DocumentNodeStore store = context.getService(DocumentNodeStore.class); - MongoDocumentStore mds = getMongoDocumentStore(store); - MongoClient client = MongoDocumentStoreTestHelper.getClient(mds); - assertFalse(client.getMongoClientOptions().isSocketKeepAlive()); } @Test diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoDbTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoDbTest.java index fbe09d223fe..a706b052380 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoDbTest.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoDbTest.java @@ -25,6 +25,7 @@ import org.junit.Test; import com.mongodb.BasicDBObject; +import com.mongodb.ExplainVerbosity; import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; @@ -199,9 +200,9 @@ public void updateDocument() { c.close(); } - private static BasicDBObject explain(MongoCollection collection, + private static org.bson.Document explain(MongoCollection collection, Bson query) { - return collection.find(query).modifiers(new BasicDBObject("$explain", true)).first(); + return collection.find(query).explain(ExplainVerbosity.QUERY_PLANNER); } private static void log(String msg) { diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoUtils.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoUtils.java index a1a6e206209..757b629b8fc 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoUtils.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoUtils.java @@ -29,8 +29,7 @@ import org.slf4j.LoggerFactory; import com.mongodb.BasicDBObject; -import com.mongodb.DB; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; import com.mongodb.client.MongoDatabase; /** @@ -107,16 +106,15 @@ public static MongoConnection getConnection() { * @return the connection or null */ public static MongoConnection getConnection(String dbName) { - MongoClientURI clientURI; + ConnectionString connectionString; try { - clientURI = new MongoClientURI(URL); + connectionString = new ConnectionString(URL); } catch (IllegalArgumentException e) { - // configured URL is invalid return null; } StringBuilder uri = new StringBuilder("mongodb://"); String separator = ""; - for (String host : clientURI.getHosts()) { + for (String host : connectionString.getHosts()) { uri.append(separator); separator = ","; uri.append(host); @@ -144,20 +142,6 @@ public static void dropCollections(String dbName) { } } - /** - * Drop all user defined collections. System collections are not dropped. - * - * @param db the connection - * @deprecated use {@link #dropCollections(MongoDatabase)} instead. - */ - public static void dropCollections(DB db) { - for (String name : db.getCollectionNames()) { - if (!name.startsWith("system.")) { - db.getCollection(name).drop(); - } - } - } - /** * Drop all user defined collections. System collections are not dropped. * diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/AcquireRecoveryLockTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/AcquireRecoveryLockTest.java index db79a5dc0d7..5b975e07d75 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/AcquireRecoveryLockTest.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/AcquireRecoveryLockTest.java @@ -18,7 +18,7 @@ import java.util.List; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; import org.apache.jackrabbit.oak.plugins.document.AbstractMongoConnectionTest; import org.apache.jackrabbit.oak.plugins.document.ClusterNodeInfo; diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ClusterConflictTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ClusterConflictTest.java index 182c2fa6e29..5fb6af32322 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ClusterConflictTest.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ClusterConflictTest.java @@ -16,7 +16,8 @@ */ package org.apache.jackrabbit.oak.plugins.document.mongo; -import com.mongodb.MongoClient; + +import com.mongodb.client.MongoClient; import org.apache.jackrabbit.oak.api.CommitFailedException; import org.apache.jackrabbit.oak.commons.PathUtils; diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoConnectionTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoConnectionTest.java index e5a51920456..150c4cfe67c 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoConnectionTest.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoConnectionTest.java @@ -16,20 +16,18 @@ */ package org.apache.jackrabbit.oak.plugins.document.mongo; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientOptions; import com.mongodb.ReadConcern; -import com.mongodb.ReplicaSetStatus; import com.mongodb.WriteConcern; +import com.mongodb.client.MongoClient; +import com.mongodb.connection.ClusterDescription; +import com.mongodb.connection.ClusterType; -import org.apache.jackrabbit.oak.plugins.document.MongoUtils; import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -37,8 +35,8 @@ public class MongoConnectionTest { @Test public void hasWriteConcern() throws Exception { - assertFalse(MongoConnection.hasWriteConcern("mongodb://localhost:27017/foo")); - assertTrue(MongoConnection.hasWriteConcern("mongodb://localhost:27017/foo?w=1")); + assertTrue(MongoConnection.hasMongoDbDefaultWriteConcern("mongodb://localhost:27017/foo")); + assertFalse(MongoConnection.hasMongoDbDefaultWriteConcern("mongodb://localhost:27017/foo?w=1")); } @Test @@ -52,25 +50,13 @@ public void sufficientWriteConcern() throws Exception { sufficientWriteConcernReplicaSet(WriteConcern.ACKNOWLEDGED, false); sufficientWriteConcernReplicaSet(WriteConcern.JOURNALED, false); sufficientWriteConcernReplicaSet(WriteConcern.MAJORITY, true); - sufficientWriteConcernReplicaSet(WriteConcern.FSYNC_SAFE, false); - sufficientWriteConcernReplicaSet(WriteConcern.FSYNCED, false); - sufficientWriteConcernReplicaSet(WriteConcern.JOURNAL_SAFE, false); - sufficientWriteConcernReplicaSet(WriteConcern.NORMAL, false); - sufficientWriteConcernReplicaSet(WriteConcern.REPLICA_ACKNOWLEDGED, true); - sufficientWriteConcernReplicaSet(WriteConcern.REPLICAS_SAFE, true); - sufficientWriteConcernReplicaSet(WriteConcern.SAFE, false); + sufficientWriteConcernReplicaSet(WriteConcern.W2, true); sufficientWriteConcernReplicaSet(WriteConcern.UNACKNOWLEDGED, false); sufficientWriteConcernSingleNode(WriteConcern.ACKNOWLEDGED, true); sufficientWriteConcernSingleNode(WriteConcern.JOURNALED, true); sufficientWriteConcernSingleNode(WriteConcern.MAJORITY, true); - sufficientWriteConcernSingleNode(WriteConcern.FSYNC_SAFE, true); - sufficientWriteConcernSingleNode(WriteConcern.FSYNCED, true); - sufficientWriteConcernSingleNode(WriteConcern.JOURNAL_SAFE, true); - sufficientWriteConcernSingleNode(WriteConcern.NORMAL, false); - sufficientWriteConcernSingleNode(WriteConcern.REPLICA_ACKNOWLEDGED, true); - sufficientWriteConcernSingleNode(WriteConcern.REPLICAS_SAFE, true); - sufficientWriteConcernSingleNode(WriteConcern.SAFE, true); + sufficientWriteConcernReplicaSet(WriteConcern.W2, true); sufficientWriteConcernSingleNode(WriteConcern.UNACKNOWLEDGED, false); } @@ -85,26 +71,6 @@ public void sufficientReadConcern() throws Exception { sufficientReadConcernSingleNode(ReadConcern.MAJORITY, true); } - @Test - public void socketKeepAlive() throws Exception { - assumeTrue(MongoUtils.isAvailable()); - MongoClientOptions.Builder options = MongoConnection.getDefaultBuilder(); - options.socketKeepAlive(false); - MongoConnection c = new MongoConnection(MongoUtils.URL, options); - try { - assertFalse(c.getMongoClient().getMongoClientOptions().isSocketKeepAlive()); - } finally { - c.close(); - } - // default is with keep-alive (starting with 3.6 driver) - c = new MongoConnection(MongoUtils.URL); - try { - assertTrue(c.getMongoClient().getMongoClientOptions().isSocketKeepAlive()); - } finally { - c.close(); - } - } - private void sufficientWriteConcernReplicaSet(WriteConcern w, boolean sufficient) { sufficientWriteConcern(w, true, sufficient); @@ -139,14 +105,15 @@ private void sufficientReadConcern(ReadConcern r, } private MongoClient mockMongoClient(boolean replicaSet) { - ReplicaSetStatus status; + ClusterDescription description = mock(ClusterDescription.class); if (replicaSet) { - status = mock(ReplicaSetStatus.class); + when(description.getType()).thenReturn(ClusterType.REPLICA_SET); } else { - status = null; + when(description.getType()).thenReturn(ClusterType.STANDALONE); } MongoClient client = mock(MongoClient.class); - when(client.getReplicaSetStatus()).thenReturn(status); + when(client.getClusterDescription()).thenReturn(description); + return client; } } diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBConfigTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBConfigTest.java index de6646d98f0..0b5ae991f08 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBConfigTest.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBConfigTest.java @@ -17,7 +17,7 @@ package org.apache.jackrabbit.oak.plugins.document.mongo; import com.mongodb.BasicDBObject; -import com.mongodb.MongoClient; +import com.mongodb.MongoClientSettings; import org.apache.jackrabbit.oak.plugins.document.mongo.MongoDBConfig.CollectionCompressor; import org.bson.BsonDocument; import org.bson.conversions.Bson; @@ -25,21 +25,19 @@ import java.util.Collections; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static org.apache.jackrabbit.oak.plugins.document.mongo.MongoDBConfig.getCollectionStorageOptions; import static org.apache.jackrabbit.oak.plugins.document.mongo.MongoDBConfig.COLLECTION_COMPRESSION_TYPE; import static org.apache.jackrabbit.oak.plugins.document.mongo.MongoDBConfig.STORAGE_CONFIG; import static org.apache.jackrabbit.oak.plugins.document.mongo.MongoDBConfig.STORAGE_ENGINE; - +import static org.apache.jackrabbit.oak.plugins.document.mongo.MongoDBConfig.getCollectionStorageOptions; public class MongoDBConfigTest { @Test public void defaultCollectionStorageOptions() { Bson bson = getCollectionStorageOptions(Collections.emptyMap()); - BsonDocument bsonDocument = bson.toBsonDocument(BasicDBObject.class, MongoClient.getDefaultCodecRegistry()); + BsonDocument bsonDocument = bson.toBsonDocument(BasicDBObject.class, MongoClientSettings.getDefaultCodecRegistry()); String configuredCompressor = bsonDocument.getDocument(STORAGE_ENGINE).getString(STORAGE_CONFIG).getValue(); assertTrue(configuredCompressor.indexOf(CollectionCompressor.SNAPPY.getName()) > 0); @@ -54,7 +52,7 @@ public void invalidCollectionStorageOptions() { @Test public void overrideDefaultCollectionStorageOptions() { Bson bson = getCollectionStorageOptions(Collections.singletonMap(COLLECTION_COMPRESSION_TYPE, "zstd")); - BsonDocument bsonDocument = bson.toBsonDocument(BasicDBObject.class, MongoClient.getDefaultCodecRegistry()); + BsonDocument bsonDocument = bson.toBsonDocument(BasicDBObject.class, MongoClientSettings.getDefaultCodecRegistry()); String configuredCompressor = bsonDocument.getDocument(STORAGE_ENGINE).getString(STORAGE_CONFIG).getValue(); assertTrue(configuredCompressor.indexOf(CollectionCompressor.ZSTD.getName()) > 0); diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBExceptionTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBExceptionTest.java index 5dbe01d66b6..52754f6b2bd 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBExceptionTest.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDBExceptionTest.java @@ -1,21 +1,26 @@ /* - * 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 + * 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 + * 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. + * 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 static java.util.Collections.singletonList; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.apache.jackrabbit.oak.commons.junit.LogCustomizer; import org.apache.jackrabbit.oak.plugins.document.Collection; import org.apache.jackrabbit.oak.plugins.document.DocumentMK; import org.apache.jackrabbit.oak.plugins.document.DocumentStoreException; @@ -25,24 +30,12 @@ import org.apache.jackrabbit.oak.plugins.document.Revision; import org.apache.jackrabbit.oak.plugins.document.UpdateOp; import org.apache.jackrabbit.oak.plugins.document.util.Utils; -import org.apache.jackrabbit.oak.commons.junit.LogCustomizer; import org.junit.After; import org.junit.Assume; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static java.util.Collections.singletonList; -import static org.hamcrest.Matchers.containsString; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.junit.Assert.assertFalse; - public class MongoDBExceptionTest { private MongoDocumentStore store; @@ -83,9 +76,11 @@ public void idInExceptionMessage() { store.findAndUpdate(Collection.NODES, op); fail("DocumentStoreException expected"); } catch (DocumentStoreException e) { - assertTrue(e.getMessage().contains(exceptionMsg)); + assertTrue(e.getMessage() + .contains(exceptionMsg)); assertTrue("Exception message does not contain id: '" + e.getMessage() + "'", - e.getMessage().contains(id)); + e.getMessage() + .contains(id)); } exceptionMsg = "createOrUpdate failed"; @@ -94,9 +89,11 @@ public void idInExceptionMessage() { store.createOrUpdate(Collection.NODES, op); fail("DocumentStoreException expected"); } catch (DocumentStoreException e) { - assertTrue(e.getMessage().contains(exceptionMsg)); + assertTrue(e.getMessage() + .contains(exceptionMsg)); assertTrue("Exception message does not contain id: '" + e.getMessage() + "'", - e.getMessage().contains(id)); + e.getMessage() + .contains(id)); } exceptionMsg = "createOrUpdate (multiple) failed"; @@ -105,9 +102,11 @@ public void idInExceptionMessage() { store.createOrUpdate(Collection.NODES, singletonList(op)); fail("DocumentStoreException expected"); } catch (DocumentStoreException e) { - assertTrue(e.getMessage().contains(exceptionMsg)); + assertTrue(e.getMessage() + .contains(exceptionMsg)); assertTrue("Exception message does not contain id: '" + e.getMessage() + "'", - e.getMessage().contains(id)); + e.getMessage() + .contains(id)); } exceptionMsg = "find failed"; @@ -118,7 +117,8 @@ public void idInExceptionMessage() { } catch (DocumentStoreException e) { assertThat(e.getMessage(), containsString(exceptionMsg)); assertTrue("Exception message does not contain id: '" + e.getMessage() + "'", - e.getMessage().contains(id)); + e.getMessage() + .contains(id)); } Path foo = Path.fromString("/foo"); @@ -132,15 +132,18 @@ public void idInExceptionMessage() { } catch (DocumentStoreException e) { assertThat(e.getMessage(), containsString(exceptionMsg)); assertTrue("Exception message does not contain id: '" + e.getMessage() + "'", - e.getMessage().contains(fromKey)); + e.getMessage() + .contains(fromKey)); assertTrue("Exception message does not contain id: '" + e.getMessage() + "'", - e.getMessage().contains(toKey)); + e.getMessage() + .contains(toKey)); } } @Test public void createOrUpdate16MBDoc() { - LogCustomizer customizer = LogCustomizer.forLogger(MongoDocumentStore.class.getName()).create(); + LogCustomizer customizer = LogCustomizer.forLogger(MongoDocumentStore.class.getName()) + .create(); customizer.starting(); String id = "/foo"; UpdateOp updateOp = new UpdateOp(id, true); @@ -151,7 +154,8 @@ public void createOrUpdate16MBDoc() { fail("DocumentStoreException expected"); } catch (DocumentStoreException e) { assertThat(e.getMessage(), containsString(exceptionMsg)); - String log = customizer.getLogs().toString(); + String log = customizer.getLogs() + .toString(); assertTrue("Message doesn't contain the id", log.contains(id)); } customizer.finished(); @@ -179,7 +183,8 @@ public void update16MBDoc() { public void multiCreateOrUpdate16MBDoc() { List updateOps = new ArrayList<>(); - LogCustomizer customizer = LogCustomizer.forLogger(MongoDocumentStore.class.getName()).create(); + LogCustomizer customizer = LogCustomizer.forLogger(MongoDocumentStore.class.getName()) + .create(); customizer.starting(); String id1 = "/test"; String id2 = "/foo"; @@ -203,7 +208,8 @@ public void multiCreateOrUpdate16MBDoc() { fail("DocumentStoreException expected"); } catch (DocumentStoreException e) { assertThat(e.getMessage(), containsString(exceptionMsg)); - String log = customizer.getLogs().toString(); + String log = customizer.getLogs() + .toString(); assertTrue("Message doesn't contain the id", log.contains(id1)); } customizer.finished(); @@ -213,7 +219,8 @@ public void multiCreateOrUpdate16MBDoc() { public void create16MBDoc() { List updateOps = new ArrayList<>(); - LogCustomizer customizer = LogCustomizer.forLogger(MongoDocumentStore.class.getName()).create(); + LogCustomizer customizer = LogCustomizer.forLogger(MongoDocumentStore.class.getName()) + .create(); customizer.starting(); String id1 = "/test"; String id2 = "/foo"; @@ -227,7 +234,8 @@ public void create16MBDoc() { updateOps.add(op1); updateOps.add(op2); assertFalse(store.create(Collection.NODES, updateOps)); - String log = customizer.getLogs().toString(); + String log = customizer.getLogs() + .toString(); assertTrue("Message doesn't contain the id", log.contains(id2)); } @@ -245,7 +253,7 @@ public void findAndUpdate16MBDoc() throws Exception { } catch (DocumentStoreException e) { assertThat(e.getMessage(), containsString(exceptionMsg)); assertThat(e.getMessage(), containsString(id)); - } + } } private void setExceptionMsg() { @@ -263,11 +271,11 @@ private UpdateOp create1MBProp(UpdateOp op) { private UpdateOp create16MBProp(UpdateOp op) { // create a 1 MB property String content = create1MBContent(); - - //create 16MB property + + // create 16MB property for (int i = 0; i < 16; i++) { - op.set("property"+ i, content); + op.set("property" + i, content); } return op; } diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTestHelper.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTestHelper.java index 11e547e4644..bedf0bad776 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTestHelper.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreTestHelper.java @@ -16,7 +16,7 @@ */ package org.apache.jackrabbit.oak.plugins.document.mongo; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; public final class MongoDocumentStoreTestHelper { diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoStatusTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoStatusTest.java index dc1a90e4e66..0d9bdfd1d52 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoStatusTest.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoStatusTest.java @@ -1,30 +1,24 @@ /* - * 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 + * 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 + * 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. + * 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 static org.apache.jackrabbit.oak.plugins.document.MongoUtils.isAvailable; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.junit.Assume.assumeTrue; +import java.util.Iterator; import java.util.concurrent.atomic.AtomicReference; - -import com.mongodb.BasicDBObject; -import com.mongodb.MongoCommandException; -import com.mongodb.ReadPreference; -import com.mongodb.ServerAddress; -import com.mongodb.client.ClientSession; -import com.mongodb.client.MongoDatabase; - import org.apache.jackrabbit.oak.plugins.document.MongoConnectionFactory; import org.apache.jackrabbit.oak.plugins.document.MongoUtils; import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection; @@ -39,13 +33,15 @@ import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; - -import static org.apache.jackrabbit.oak.plugins.document.MongoUtils.isAvailable; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.junit.Assume.assumeTrue; +import com.mongodb.BasicDBObject; +import com.mongodb.MongoClientSettings; +import com.mongodb.MongoCommandException; +import com.mongodb.ReadPreference; +import com.mongodb.ServerAddress; +import com.mongodb.client.ClientSession; +import com.mongodb.client.MongoDatabase; +import com.mongodb.connection.ClusterDescription; +import com.mongodb.connection.ServerDescription; public class MongoStatusTest { @@ -103,12 +99,13 @@ public void testReadConcern() { @Test public void testGetVersion() { - assertTrue(status.getVersion().matches("^\\d+\\.\\d+\\.\\d+$")); + assertTrue(status.getVersion() + .matches("^\\d+\\.\\d+\\.\\d+$")); } @Test public void testCheckVersionValid() { - for (String v : new String[] { "2.6.0", "2.7.0", "3.0.0"}) { + for (String v : new String[] {"2.6.0", "2.7.0", "3.0.0"}) { status.setVersion(v); status.checkVersion(); } @@ -116,7 +113,7 @@ public void testCheckVersionValid() { @Test public void testCheckVersionInvalid() { - for (String v : new String[] { "1.0.0", "2.0.0", "2.5.0"}) { + for (String v : new String[] {"1.0.0", "2.0.0", "2.5.0"}) { status.setVersion(v); try { status.checkVersion(); @@ -144,54 +141,54 @@ public void unauthorized() { @Override public @NotNull Document runCommand(@NotNull Bson command, - @NotNull ReadPreference readPreference) { + @NotNull ReadPreference readPreference) { unauthorizedIfServerStatus(command); return super.runCommand(command, readPreference); } @Override public @NotNull TResult runCommand(@NotNull Bson command, - @NotNull Class tResultClass) { + @NotNull Class tResultClass) { unauthorizedIfServerStatus(command); return super.runCommand(command, tResultClass); } @Override public @NotNull TResult runCommand(@NotNull Bson command, - @NotNull ReadPreference readPreference, - @NotNull Class tResultClass) { + @NotNull ReadPreference readPreference, + @NotNull Class tResultClass) { unauthorizedIfServerStatus(command); return super.runCommand(command, readPreference, tResultClass); } @Override public @NotNull Document runCommand(@NotNull ClientSession clientSession, - @NotNull Bson command) { + @NotNull Bson command) { unauthorizedIfServerStatus(command); return super.runCommand(clientSession, command); } @Override public @NotNull Document runCommand(@NotNull ClientSession clientSession, - @NotNull Bson command, - @NotNull ReadPreference readPreference) { + @NotNull Bson command, + @NotNull ReadPreference readPreference) { unauthorizedIfServerStatus(command); return super.runCommand(clientSession, command, readPreference); } @Override public @NotNull TResult runCommand(@NotNull ClientSession clientSession, - @NotNull Bson command, - @NotNull Class tResultClass) { + @NotNull Bson command, + @NotNull Class tResultClass) { unauthorizedIfServerStatus(command); return super.runCommand(clientSession, command, tResultClass); } @Override public @NotNull TResult runCommand(@NotNull ClientSession clientSession, - @NotNull Bson command, - @NotNull ReadPreference readPreference, - @NotNull Class tResultClass) { + @NotNull Bson command, + @NotNull ReadPreference readPreference, + @NotNull Class tResultClass) { unauthorizedIfServerStatus(command); return super.runCommand(clientSession, command, readPreference, tResultClass); } @@ -199,12 +196,22 @@ public void unauthorized() { } private void unauthorizedIfServerStatus(Bson command) { - if (command.toBsonDocument(BasicDBObject.class, getDefaultCodecRegistry()).containsKey("serverStatus")) { + if (command.toBsonDocument(BasicDBObject.class, MongoClientSettings.getDefaultCodecRegistry()) + .containsKey("serverStatus")) { BsonDocument response = new BsonDocument("ok", new BsonDouble(0.0)); response.put("errmsg", new BsonString("command serverStatus requires authentication")); response.put("code", new BsonInt32(13)); response.put("codeName", new BsonString("Unauthorized")); - ServerAddress address = getAddress(); + + ServerAddress address = null; + ClusterDescription clusterDescription = getClusterDescription(); + for (Iterator iterator = clusterDescription.getServerDescriptions().iterator(); iterator.hasNext();) { + ServerDescription serverDescription = iterator.next(); + + address = serverDescription.getAddress(); + break; + } + if (address == null) { // OAK-8459: use dummy/default address instead address = new ServerAddress(); diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestClient.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestClient.java index 28bc39e1c31..2f36610f0b2 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestClient.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestClient.java @@ -16,28 +16,45 @@ */ package org.apache.jackrabbit.oak.plugins.document.mongo; +import java.util.List; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; - -import com.mongodb.MongoClient; -import com.mongodb.MongoClientURI; -import com.mongodb.client.MongoDatabase; - +import org.bson.Document; +import org.bson.codecs.configuration.CodecRegistry; +import org.bson.conversions.Bson; import org.jetbrains.annotations.NotNull; +import com.mongodb.ClientSessionOptions; +import com.mongodb.ConnectionString; +import com.mongodb.ReadConcern; +import com.mongodb.ReadPreference; +import com.mongodb.WriteConcern; +import com.mongodb.client.ChangeStreamIterable; +import com.mongodb.client.ClientSession; +import com.mongodb.client.ListDatabasesIterable; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCluster; +import com.mongodb.client.MongoDatabase; +import com.mongodb.client.MongoIterable; +import com.mongodb.connection.ClusterDescription; -class MongoTestClient extends MongoClient { +class MongoTestClient implements MongoClient { private AtomicReference beforeQueryException = new AtomicReference<>(); private AtomicReference beforeUpdateException = new AtomicReference<>(); private AtomicReference afterUpdateException = new AtomicReference<>(); + + private MongoClient delegate; MongoTestClient(String uri) { - super(new MongoClientURI(uri)); + ConnectionString connectionString = new ConnectionString(uri); + delegate = MongoClients.create(connectionString); } @NotNull @Override public MongoDatabase getDatabase(String databaseName) { - return new MongoTestDatabase(super.getDatabase(databaseName), + return new MongoTestDatabase(delegate.getDatabase(databaseName), beforeQueryException, beforeUpdateException, afterUpdateException); } @@ -52,4 +69,145 @@ void setExceptionBeforeUpdate(String msg) { void setExceptionAfterUpdate(String msg) { afterUpdateException.set(msg); } + + @Override + public CodecRegistry getCodecRegistry() { + return delegate.getCodecRegistry(); + } + + @Override + public ReadPreference getReadPreference() { + return delegate.getReadPreference(); + } + + @Override + public WriteConcern getWriteConcern() { + return delegate.getWriteConcern(); + } + + @Override + public ReadConcern getReadConcern() { + return delegate.getReadConcern(); + } + + @Override + public Long getTimeout(TimeUnit timeUnit) { + return delegate.getTimeout(timeUnit); + } + + @Override + public MongoCluster withCodecRegistry(CodecRegistry codecRegistry) { + return delegate.withCodecRegistry(codecRegistry); + } + + @Override + public MongoCluster withReadPreference(ReadPreference readPreference) { + return delegate.withReadPreference(readPreference); + } + + @Override + public MongoCluster withWriteConcern(WriteConcern writeConcern) { + return delegate.withWriteConcern(writeConcern); + } + + @Override + public MongoCluster withReadConcern(ReadConcern readConcern) { + return delegate.withReadConcern(readConcern); + } + + @Override + public MongoCluster withTimeout(long timeout, TimeUnit timeUnit) { + return delegate.withTimeout(timeout, timeUnit); + } + + @Override + public ClientSession startSession() { + return delegate.startSession(); + } + + @Override + public ClientSession startSession(ClientSessionOptions options) { + return delegate.startSession(options); + } + + @Override + public MongoIterable listDatabaseNames() { + return delegate.listDatabaseNames(); + } + + @Override + public MongoIterable listDatabaseNames(ClientSession clientSession) { + return delegate.listDatabaseNames(clientSession); + } + + @Override + public ListDatabasesIterable listDatabases() { + return delegate.listDatabases(); + } + + @Override + public ListDatabasesIterable listDatabases(ClientSession clientSession) { + return delegate.listDatabases(clientSession); + } + + @Override + public ListDatabasesIterable listDatabases(Class resultClass) { + return delegate.listDatabases(resultClass); + } + + @Override + public ListDatabasesIterable listDatabases(ClientSession clientSession, Class resultClass) { + return delegate.listDatabases(clientSession, resultClass); + } + + @Override + public ChangeStreamIterable watch() { + return delegate.watch(); + } + + @Override + public ChangeStreamIterable watch(Class resultClass) { + return delegate.watch(resultClass); + } + + @Override + public ChangeStreamIterable watch(List pipeline) { + return delegate.watch(pipeline); + } + + @Override + public ChangeStreamIterable watch(List pipeline, Class resultClass) { + return delegate.watch(pipeline, resultClass); + } + + @Override + public ChangeStreamIterable watch(ClientSession clientSession) { + return delegate.watch(clientSession); + } + + @Override + public ChangeStreamIterable watch(ClientSession clientSession, Class resultClass) { + return delegate.watch(clientSession, resultClass); + } + + @Override + public ChangeStreamIterable watch(ClientSession clientSession, List pipeline) { + return delegate.watch(clientSession, pipeline); + } + + @Override + public ChangeStreamIterable watch(ClientSession clientSession, List pipeline, Class resultClass) { + return delegate.watch(clientSession, pipeline, resultClass); + } + + @Override + public void close() { + delegate.close(); + + } + + @Override + public ClusterDescription getClusterDescription() { + return delegate.getClusterDescription(); + } } diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestCollection.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestCollection.java index 051f99d49db..7cf70a14a94 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestCollection.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestCollection.java @@ -17,8 +17,13 @@ package org.apache.jackrabbit.oak.plugins.document.mongo; import java.util.List; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; - +import org.bson.Document; +import org.bson.codecs.configuration.CodecRegistry; +import org.bson.conversions.Bson; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import com.mongodb.MongoException; import com.mongodb.MongoNamespace; import com.mongodb.ReadConcern; @@ -31,12 +36,14 @@ import com.mongodb.client.DistinctIterable; import com.mongodb.client.FindIterable; import com.mongodb.client.ListIndexesIterable; +import com.mongodb.client.ListSearchIndexesIterable; import com.mongodb.client.MapReduceIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.model.BulkWriteOptions; import com.mongodb.client.model.CountOptions; import com.mongodb.client.model.CreateIndexOptions; import com.mongodb.client.model.DeleteOptions; +import com.mongodb.client.model.DropCollectionOptions; import com.mongodb.client.model.DropIndexOptions; import com.mongodb.client.model.EstimatedDocumentCountOptions; import com.mongodb.client.model.FindOneAndDeleteOptions; @@ -48,17 +55,14 @@ import com.mongodb.client.model.InsertOneOptions; import com.mongodb.client.model.RenameCollectionOptions; import com.mongodb.client.model.ReplaceOptions; +import com.mongodb.client.model.SearchIndexModel; import com.mongodb.client.model.UpdateOptions; import com.mongodb.client.model.WriteModel; import com.mongodb.client.result.DeleteResult; +import com.mongodb.client.result.InsertManyResult; +import com.mongodb.client.result.InsertOneResult; import com.mongodb.client.result.UpdateResult; -import org.bson.Document; -import org.bson.codecs.configuration.CodecRegistry; -import org.bson.conversions.Bson; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - public class MongoTestCollection implements MongoCollection { private final MongoCollection collection; @@ -143,44 +147,6 @@ public MongoCollection withReadConcern(@NotNull ReadConcern readConce return new MongoTestCollection<>(collection.withReadConcern(readConcern), beforeQueryException, beforeUpdateException, afterUpdateException); } - @Override - @Deprecated - public long count() { - return collection.count(); - } - - @Override - @Deprecated - public long count(@NotNull Bson filter) { - return collection.count(filter); - } - - @Override - @Deprecated - public long count(@NotNull Bson filter, @NotNull CountOptions options) { - return collection.count(filter, options); - } - - @Override - @Deprecated - public long count(@NotNull ClientSession clientSession) { - return collection.count(clientSession); - } - - @Override - @Deprecated - public long count(@NotNull ClientSession clientSession, @NotNull Bson filter) { - return collection.count(clientSession, filter); - } - - @Override - @Deprecated - public long count(@NotNull ClientSession clientSession, - @NotNull Bson filter, - @NotNull CountOptions options) { - return collection.count(clientSession, filter, options); - } - @Override public long countDocuments() { return collection.countDocuments(); @@ -472,65 +438,73 @@ public BulkWriteResult bulkWrite(@NotNull ClientSession clientSession, } @Override - public void insertOne(@NotNull TDocument tDocument) { + public InsertOneResult insertOne(@NotNull TDocument tDocument) { maybeThrowExceptionBeforeUpdate(); - collection.insertOne(tDocument); + InsertOneResult insertOne = collection.insertOne(tDocument); maybeThrowExceptionAfterUpdate(); + return insertOne; } @Override - public void insertOne(@NotNull TDocument tDocument, @NotNull InsertOneOptions options) { + public InsertOneResult insertOne(@NotNull TDocument tDocument, @NotNull InsertOneOptions options) { maybeThrowExceptionBeforeUpdate(); - collection.insertOne(tDocument, options); + InsertOneResult insertOne = collection.insertOne(tDocument, options); maybeThrowExceptionAfterUpdate(); + return insertOne; } @Override - public void insertOne(@NotNull ClientSession clientSession, @NotNull TDocument tDocument) { + public InsertOneResult insertOne(@NotNull ClientSession clientSession, @NotNull TDocument tDocument) { maybeThrowExceptionBeforeUpdate(); - collection.insertOne(clientSession, tDocument); + InsertOneResult insertOne = collection.insertOne(clientSession, tDocument); maybeThrowExceptionAfterUpdate(); + return insertOne; } @Override - public void insertOne(@NotNull ClientSession clientSession, + public InsertOneResult insertOne(@NotNull ClientSession clientSession, @NotNull TDocument tDocument, @NotNull InsertOneOptions options) { maybeThrowExceptionBeforeUpdate(); - collection.insertOne(clientSession, tDocument, options); + InsertOneResult insertOne = collection.insertOne(clientSession, tDocument, options); maybeThrowExceptionAfterUpdate(); + return insertOne; } @Override - public void insertMany(@NotNull List tDocuments) { + public InsertManyResult insertMany(@NotNull List tDocuments) { maybeThrowExceptionBeforeUpdate(); - collection.insertMany(tDocuments); + InsertManyResult insertMany = collection.insertMany(tDocuments); maybeThrowExceptionAfterUpdate(); + return insertMany; } @Override - public void insertMany(@NotNull List tDocuments, + public InsertManyResult insertMany(@NotNull List tDocuments, @NotNull InsertManyOptions options) { maybeThrowExceptionBeforeUpdate(); - collection.insertMany(tDocuments, options); + InsertManyResult insertMany = collection.insertMany(tDocuments, options); maybeThrowExceptionAfterUpdate(); + return insertMany; } @Override - public void insertMany(@NotNull ClientSession clientSession, + public InsertManyResult insertMany(@NotNull ClientSession clientSession, @NotNull List tDocuments) { maybeThrowExceptionBeforeUpdate(); - collection.insertMany(clientSession, tDocuments); + InsertManyResult insertMany = collection.insertMany(clientSession, tDocuments); maybeThrowExceptionAfterUpdate(); + return insertMany; } @Override - public void insertMany(@NotNull ClientSession clientSession, + public InsertManyResult insertMany(@NotNull ClientSession clientSession, @NotNull List tDocuments, @NotNull InsertManyOptions options) { maybeThrowExceptionBeforeUpdate(); - collection.insertMany(clientSession, tDocuments, options); + InsertManyResult insertMany = collection.insertMany(clientSession, tDocuments, options); maybeThrowExceptionAfterUpdate(); + return insertMany; } @NotNull @@ -618,18 +592,6 @@ public UpdateResult replaceOne(@NotNull Bson filter, @NotNull TDocument replacem return result; } - @NotNull - @Override - @Deprecated - public UpdateResult replaceOne(@NotNull Bson filter, - @NotNull TDocument replacement, - @NotNull UpdateOptions updateOptions) { - maybeThrowExceptionBeforeUpdate(); - UpdateResult result = collection.replaceOne(filter, replacement, updateOptions); - maybeThrowExceptionAfterUpdate(); - return result; - } - @NotNull @Override public UpdateResult replaceOne(@NotNull ClientSession clientSession, @@ -641,19 +603,6 @@ public UpdateResult replaceOne(@NotNull ClientSession clientSession, return result; } - @NotNull - @Override - @Deprecated - public UpdateResult replaceOne(@NotNull ClientSession clientSession, - @NotNull Bson filter, - @NotNull TDocument replacement, - @NotNull UpdateOptions updateOptions) { - maybeThrowExceptionBeforeUpdate(); - UpdateResult result = collection.replaceOne(clientSession, filter, replacement, updateOptions); - maybeThrowExceptionAfterUpdate(); - return result; - } - @NotNull @Override public UpdateResult replaceOne(@NotNull Bson filter, @@ -1194,6 +1143,61 @@ public void renameCollection(@NotNull ClientSession clientSession, @NotNull RenameCollectionOptions renameCollectionOptions) { collection.renameCollection(clientSession, newCollectionNamespace, renameCollectionOptions); } + + @Override + public Long getTimeout(TimeUnit timeUnit) { + return collection.getTimeout(timeUnit); + } + + @Override + public MongoCollection withTimeout(long timeout, TimeUnit timeUnit) { + return collection.withTimeout(timeout, timeUnit); + } + + @Override + public void drop(DropCollectionOptions dropCollectionOptions) { + collection.drop(dropCollectionOptions); + } + + @Override + public void drop(ClientSession clientSession, DropCollectionOptions dropCollectionOptions) { + collection.drop(clientSession, dropCollectionOptions); + } + + @Override + public String createSearchIndex(String indexName, Bson definition) { + return collection.createSearchIndex(indexName, definition); + } + + @Override + public String createSearchIndex(Bson definition) { + return collection.createSearchIndex(definition); + } + + @Override + public List createSearchIndexes(List searchIndexModels) { + return collection.createSearchIndexes(searchIndexModels); + } + + @Override + public void updateSearchIndex(String indexName, Bson definition) { + collection.updateSearchIndex(indexName, definition); + } + + @Override + public void dropSearchIndex(String indexName) { + collection.dropSearchIndex(indexName); + } + + @Override + public ListSearchIndexesIterable listSearchIndexes() { + return collection.listSearchIndexes(); + } + + @Override + public ListSearchIndexesIterable listSearchIndexes(Class resultClass) { + return collection.listSearchIndexes(resultClass); + } private void maybeThrowExceptionBeforeQuery() { String msg = beforeQueryException.get(); diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestDatabase.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestDatabase.java index 27d89cffc9f..db3c231b2f7 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestDatabase.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoTestDatabase.java @@ -17,26 +17,25 @@ package org.apache.jackrabbit.oak.plugins.document.mongo; import java.util.List; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; - +import org.bson.Document; +import org.bson.codecs.configuration.CodecRegistry; +import org.bson.conversions.Bson; +import org.jetbrains.annotations.NotNull; import com.mongodb.ReadConcern; import com.mongodb.ReadPreference; import com.mongodb.WriteConcern; import com.mongodb.client.AggregateIterable; import com.mongodb.client.ChangeStreamIterable; import com.mongodb.client.ClientSession; +import com.mongodb.client.ListCollectionNamesIterable; import com.mongodb.client.ListCollectionsIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; -import com.mongodb.client.MongoIterable; import com.mongodb.client.model.CreateCollectionOptions; import com.mongodb.client.model.CreateViewOptions; -import org.bson.Document; -import org.bson.codecs.configuration.CodecRegistry; -import org.bson.conversions.Bson; -import org.jetbrains.annotations.NotNull; - public class MongoTestDatabase implements MongoDatabase { private final MongoDatabase db; @@ -194,7 +193,7 @@ public void drop(@NotNull ClientSession clientSession) { @NotNull @Override - public MongoIterable listCollectionNames() { + public ListCollectionNamesIterable listCollectionNames() { return db.listCollectionNames(); } @@ -212,7 +211,7 @@ public ListCollectionsIterable listCollections(@NotNull Class @NotNull @Override - public MongoIterable listCollectionNames(@NotNull ClientSession clientSession) { + public ListCollectionNamesIterable listCollectionNames(@NotNull ClientSession clientSession) { return db.listCollectionNames(clientSession); } @@ -366,4 +365,16 @@ public AggregateIterable aggregate(@NotNull ClientSession cli @NotNull Class tResultClass) { return db.aggregate(clientSession, pipeline, tResultClass); } + + @Override + public Long getTimeout(TimeUnit timeUnit) { + // TODO Auto-generated method stub + return null; + } + + @Override + public MongoDatabase withTimeout(long timeout, TimeUnit timeUnit) { + // TODO Auto-generated method stub + return null; + } } diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtilsTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtilsTest.java index 5d24e77d3dc..55520e77866 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtilsTest.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoUtilsTest.java @@ -16,21 +16,19 @@ */ package org.apache.jackrabbit.oak.plugins.document.mongo; +import static org.apache.jackrabbit.oak.plugins.document.DocumentStoreException.Type.GENERIC; +import static org.apache.jackrabbit.oak.plugins.document.DocumentStoreException.Type.TRANSIENT; +import static org.apache.jackrabbit.oak.plugins.document.MongoUtils.isAvailable; +import static org.apache.jackrabbit.oak.plugins.document.mongo.MongoUtils.getDocumentStoreExceptionTypeFor; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; +import static org.mockito.Mockito.mock; import java.io.IOException; import java.util.ArrayList; import java.util.List; - -import com.mongodb.BasicDBObject; -import com.mongodb.DuplicateKeyException; -import com.mongodb.MongoClient; -import com.mongodb.MongoCommandException; -import com.mongodb.MongoException; -import com.mongodb.MongoSocketException; -import com.mongodb.ServerAddress; -import com.mongodb.WriteConcernException; -import com.mongodb.client.ClientSession; -import com.mongodb.client.MongoCollection; - import org.apache.jackrabbit.oak.plugins.document.Collection; import org.apache.jackrabbit.oak.plugins.document.MongoConnectionFactory; import org.apache.jackrabbit.oak.plugins.document.util.MongoConnection; @@ -41,17 +39,16 @@ import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; - -import static org.apache.jackrabbit.oak.plugins.document.DocumentStoreException.Type.GENERIC; -import static org.apache.jackrabbit.oak.plugins.document.DocumentStoreException.Type.TRANSIENT; -import static org.apache.jackrabbit.oak.plugins.document.MongoUtils.isAvailable; -import static org.apache.jackrabbit.oak.plugins.document.mongo.MongoUtils.getDocumentStoreExceptionTypeFor; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeTrue; -import static org.mockito.Mockito.mock; +import com.mongodb.BasicDBObject; +import com.mongodb.DuplicateKeyException; +import com.mongodb.MongoCommandException; +import com.mongodb.MongoException; +import com.mongodb.MongoSocketException; +import com.mongodb.ServerAddress; +import com.mongodb.WriteConcernException; +import com.mongodb.client.ClientSession; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoCollection; public class MongoUtilsTest { diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongodProcessFactory.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongodProcessFactory.java index 7ddcd850df3..de930362b5e 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongodProcessFactory.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongodProcessFactory.java @@ -18,6 +18,8 @@ */ package org.apache.jackrabbit.oak.plugins.document.mongo; +import static org.apache.jackrabbit.oak.plugins.document.mongo.MongodProcess.join; +import static org.junit.Assert.assertTrue; import java.io.IOException; import java.net.InetAddress; import java.util.ArrayList; @@ -25,15 +27,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; - import org.apache.jackrabbit.guava.common.base.Joiner; -import com.mongodb.MongoClient; - import org.bson.Document; import org.junit.rules.ExternalResource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; import de.flapdoodle.embed.mongo.Command; import de.flapdoodle.embed.mongo.MongodStarter; import de.flapdoodle.embed.mongo.config.Defaults; @@ -43,9 +43,6 @@ import de.flapdoodle.embed.process.io.progress.Slf4jProgressListener; import de.flapdoodle.embed.process.runtime.Network; -import static org.apache.jackrabbit.oak.plugins.document.mongo.MongodProcess.join; -import static org.junit.Assert.assertTrue; - /** * External resource for mongod processes. */ @@ -145,7 +142,7 @@ private void initRS(String rs, int[] ports) { } Document config = new Document("_id", rs); config.append("members", members); - try (MongoClient c = new MongoClient(localhost(), ports[0])) { + try (MongoClient c = MongoClients.create("mongodb://" + localhost() + ":" + ports[0])) { c.getDatabase("admin").runCommand( new Document("replSetInitiate", config)); } diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReplicaSetResilienceIT.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReplicaSetResilienceIT.java index 77d949da489..b76f777b377 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReplicaSetResilienceIT.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReplicaSetResilienceIT.java @@ -18,6 +18,11 @@ */ package org.apache.jackrabbit.oak.plugins.document.mongo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeThat; import java.io.IOException; import java.io.UncheckedIOException; import java.util.ArrayList; @@ -31,12 +36,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; - import org.apache.jackrabbit.guava.common.base.Stopwatch; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientOptions; -import com.mongodb.ServerAddress; - import org.apache.jackrabbit.oak.plugins.document.DocumentMKBuilderProvider; import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore; import org.apache.jackrabbit.oak.plugins.document.MongoUtils; @@ -49,12 +49,13 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeThat; +import com.mongodb.MongoClientSettings; +import com.mongodb.ServerAddress; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.connection.ClusterDescription; +import com.mongodb.connection.ServerDescription; +import com.mongodb.connection.ServerType; /** * A long running resilience IT. The test sets up a three node replica set and @@ -215,11 +216,29 @@ private void stopPrimary() { for (MongodProcess p : executables.values()) { seeds.add(p.getAddress()); } - try (MongoClient c = new MongoClient(seeds, - new MongoClientOptions.Builder().requiredReplicaSetName("rs").build())) { + + String replicaSetName = "rs"; + + MongoClientSettings settings = MongoClientSettings.builder() + .applyToClusterSettings(builder -> + builder.hosts(seeds).requiredReplicaSetName(replicaSetName) + ) + .build(); + + try (MongoClient c = MongoClients.create(settings)) { ServerAddress address = null; for (int i = 0; i < 5; i++) { - address = c.getReplicaSetStatus().getMaster(); + ClusterDescription clusterDescription = c.getClusterDescription(); + + for (Iterator iterator = clusterDescription.getServerDescriptions().iterator(); iterator.hasNext();) { + ServerDescription sd = iterator.next(); + + if (ServerType.REPLICA_SET_PRIMARY.equals(sd.getType())) { + address = sd.getAddress(); + break; + } + } + if (address == null) { LOG.info("Primary unavailable. Waiting one second..."); try { diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReplicaSetStatusTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReplicaSetStatusTest.java index c334a1055b5..41c89ea7263 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReplicaSetStatusTest.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/ReplicaSetStatusTest.java @@ -104,6 +104,6 @@ private ServerHeartbeatSucceededEvent newEvent(int connectionIndex, long localTi 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); + return new ServerHeartbeatSucceededEvent(description.getConnectionId(), reply, 0, false); } } diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/RetryReadIT.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/RetryReadIT.java index a77ca245e0d..029547d4aff 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/RetryReadIT.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/mongo/RetryReadIT.java @@ -30,8 +30,8 @@ import org.jetbrains.annotations.NotNull; import org.junit.Test; -import com.mongodb.MongoClient; import com.mongodb.MongoException; +import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import static org.apache.jackrabbit.oak.plugins.document.Collection.NODES; diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/prefetch/CountingMongoDatabase.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/prefetch/CountingMongoDatabase.java index e1e68b49353..170934943cb 100644 --- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/prefetch/CountingMongoDatabase.java +++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/prefetch/CountingMongoDatabase.java @@ -19,6 +19,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import org.bson.Document; @@ -31,10 +32,10 @@ import com.mongodb.client.AggregateIterable; import com.mongodb.client.ChangeStreamIterable; import com.mongodb.client.ClientSession; +import com.mongodb.client.ListCollectionNamesIterable; import com.mongodb.client.ListCollectionsIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; -import com.mongodb.client.MongoIterable; import com.mongodb.client.model.CreateCollectionOptions; import com.mongodb.client.model.CreateViewOptions; @@ -180,7 +181,7 @@ public void drop(ClientSession clientSession) { } @Override - public MongoIterable listCollectionNames() { + public ListCollectionNamesIterable listCollectionNames() { return delegate.listCollectionNames(); } @@ -199,7 +200,7 @@ public ListCollectionsIterable listCollections( } @Override - public MongoIterable listCollectionNames(ClientSession clientSession) { + public ListCollectionNamesIterable listCollectionNames(ClientSession clientSession) { return delegate.listCollectionNames(clientSession); } @@ -351,4 +352,14 @@ public AggregateIterable aggregate(ClientSession clientSessio } + @Override + public Long getTimeout(TimeUnit timeUnit) { + return delegate.getTimeout(timeUnit); + } + + @Override + public MongoDatabase withTimeout(long timeout, TimeUnit timeUnit) { + return delegate.withTimeout(timeout, timeUnit); + } + } diff --git a/oak-upgrade/pom.xml b/oak-upgrade/pom.xml index 4fc33e044d4..63fd382b609 100644 --- a/oak-upgrade/pom.xml +++ b/oak-upgrade/pom.xml @@ -169,7 +169,7 @@ org.mongodb - mongo-java-driver + mongodb-driver-sync org.apache.tomcat diff --git a/oak-upgrade/src/main/java/org/apache/jackrabbit/oak/upgrade/cli/node/MongoFactory.java b/oak-upgrade/src/main/java/org/apache/jackrabbit/oak/upgrade/cli/node/MongoFactory.java index cee39f4f507..cd5ca42db3d 100644 --- a/oak-upgrade/src/main/java/org/apache/jackrabbit/oak/upgrade/cli/node/MongoFactory.java +++ b/oak-upgrade/src/main/java/org/apache/jackrabbit/oak/upgrade/cli/node/MongoFactory.java @@ -16,15 +16,16 @@ */ package org.apache.jackrabbit.oak.upgrade.cli.node; +import org.apache.jackrabbit.guava.common.io.Closer; import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore; import org.apache.jackrabbit.oak.plugins.document.mongo.MongoBlobStore; import org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentNodeStoreBuilder; import org.apache.jackrabbit.oak.spi.blob.BlobStore; import org.apache.jackrabbit.oak.spi.state.NodeStore; -import org.apache.jackrabbit.guava.common.io.Closer; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; import java.io.IOException; @@ -32,14 +33,14 @@ public class MongoFactory extends DocumentFactory { - private final MongoClientURI uri; + private final ConnectionString uri; private final int cacheSize; private final boolean readOnly; public MongoFactory(String repoDesc, int cacheSize, boolean readOnly) { - this.uri = new MongoClientURI(repoDesc); + this.uri = new ConnectionString(repoDesc); this.cacheSize = cacheSize; this.readOnly = readOnly; } @@ -65,7 +66,7 @@ public NodeStore create(BlobStore blobStore, Closer closer) throws IOException { } private MongoClient createClient(Closer closer) { - MongoClient client = new MongoClient(uri); + MongoClient client = MongoClients.create(uri); closer.register(client::close); return client; } diff --git a/oak-upgrade/src/test/java/org/apache/jackrabbit/oak/upgrade/cli/container/MongoNodeStoreContainer.java b/oak-upgrade/src/test/java/org/apache/jackrabbit/oak/upgrade/cli/container/MongoNodeStoreContainer.java index 2475e6516bc..f6fb1493714 100644 --- a/oak-upgrade/src/test/java/org/apache/jackrabbit/oak/upgrade/cli/container/MongoNodeStoreContainer.java +++ b/oak-upgrade/src/test/java/org/apache/jackrabbit/oak/upgrade/cli/container/MongoNodeStoreContainer.java @@ -17,18 +17,20 @@ package org.apache.jackrabbit.oak.upgrade.cli.container; import java.io.IOException; +import java.util.Iterator; import java.util.concurrent.atomic.AtomicInteger; +import org.apache.jackrabbit.guava.common.io.Closer; import org.apache.jackrabbit.oak.spi.state.NodeStore; import org.apache.jackrabbit.oak.upgrade.cli.node.MongoFactory; import org.junit.Assume; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.apache.jackrabbit.guava.common.io.Closer; -import com.mongodb.Mongo; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientURI; +import com.mongodb.ConnectionString; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoIterable; public class MongoNodeStoreContainer implements NodeStoreContainer { @@ -71,11 +73,16 @@ public static boolean isMongoAvailable() { } private static boolean testMongoAvailability() { - Mongo mongo = null; + MongoClient mongo = null; try { - MongoClientURI uri = new MongoClientURI(MONGO_URI + "?connectTimeoutMS=3000"); - mongo = new MongoClient(uri); - mongo.getDatabaseNames(); + ConnectionString uri = new ConnectionString(MONGO_URI + "?connectTimeoutMS=3000"); + mongo = MongoClients.create(uri); + MongoIterable listDatabaseNames = mongo.listDatabaseNames(); + + // To do real call to mongo and so test its availability we need to call iterator on just returned result + for (Iterator iterator = listDatabaseNames.iterator(); iterator.hasNext();) { + // do nothing... + } return true; } catch (Exception e) { return false; @@ -103,9 +110,9 @@ public void close() { @Override public void clean() throws IOException { - MongoClientURI uri = new MongoClientURI(mongoUri); - MongoClient client = new MongoClient(uri); - client.dropDatabase(uri.getDatabase()); + ConnectionString uri = new ConnectionString(mongoUri); + MongoClient client = MongoClients.create(uri); + client.getDatabase(uri.getDatabase()).drop(); blob.clean(); }