Search in sources :

Example 1 with ReadConcernLevel

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

the class MongoDocumentNodeStoreBuilderBase method setMongoDB.

private T setMongoDB(@Nonnull MongoClient client, @Nonnull MongoDatabase db, MongoStatus status, int blobCacheSizeMB) {
    if (!MongoConnection.isSufficientWriteConcern(client, db.getWriteConcern())) {
        LOG.warn("Insufficient write concern: " + db.getWriteConcern() + " At least " + MongoConnection.getDefaultWriteConcern(client) + " is recommended.");
    }
    if (status.isMajorityReadConcernSupported() && !status.isMajorityReadConcernEnabled()) {
        LOG.warn("The read concern should be enabled on mongod using --enableMajorityReadConcern");
    } else if (status.isMajorityReadConcernSupported() && !MongoConnection.isSufficientReadConcern(client, db.getReadConcern())) {
        ReadConcernLevel currentLevel = readConcernLevel(db.getReadConcern());
        ReadConcernLevel recommendedLevel = readConcernLevel(MongoConnection.getDefaultReadConcern(client, db));
        if (currentLevel == null) {
            LOG.warn("Read concern hasn't been set. At least " + recommendedLevel + " is recommended.");
        } else {
            LOG.warn("Insufficient read concern: " + currentLevel + ". At least " + recommendedLevel + " is recommended.");
        }
    }
    this.mongoStatus = status;
    this.documentStoreSupplier = memoize(() -> new MongoDocumentStore(client, db.getName(), MongoDocumentNodeStoreBuilderBase.this));
    if (this.blobStore == null) {
        GarbageCollectableBlobStore s = new MongoBlobStore(db, blobCacheSizeMB * 1024 * 1024L);
        setGCBlobStore(s);
    }
    return thisBuilder();
}
Also used : GarbageCollectableBlobStore(org.apache.jackrabbit.oak.spi.blob.GarbageCollectableBlobStore) ReadConcernLevel(com.mongodb.ReadConcernLevel)

Example 2 with ReadConcernLevel

use of com.mongodb.ReadConcernLevel in project mongo-java-driver by mongodb.

the class ReadConcernHelper method getReadConcernDocument.

public static BsonDocument getReadConcernDocument(final SessionContext sessionContext, final int maxWireVersion) {
    notNull("sessionContext", sessionContext);
    BsonDocument readConcernDocument = new BsonDocument();
    ReadConcernLevel level = sessionContext.getReadConcern().getLevel();
    if (level != null) {
        readConcernDocument.append("level", new BsonString(level.getValue()));
    }
    assertFalse(sessionContext.isSnapshot() && sessionContext.isCausallyConsistent());
    if (sessionContext.isSnapshot() && maxWireVersion < FIVE_DOT_ZERO_WIRE_VERSION) {
        throw new MongoClientException("Snapshot reads require MongoDB 5.0 or later");
    }
    if (shouldAddAfterClusterTime(sessionContext)) {
        readConcernDocument.append("afterClusterTime", sessionContext.getOperationTime());
    } else if (shouldAddAtClusterTime(sessionContext)) {
        readConcernDocument.append("atClusterTime", sessionContext.getSnapshotTimestamp());
    }
    return readConcernDocument;
}
Also used : BsonDocument(org.bson.BsonDocument) MongoClientException(com.mongodb.MongoClientException) BsonString(org.bson.BsonString) ReadConcernLevel(com.mongodb.ReadConcernLevel)

Example 3 with ReadConcernLevel

use of com.mongodb.ReadConcernLevel in project mongo-java-driver by mongodb.

the class Entities method initClient.

private void initClient(final BsonDocument entity, final String id, final Function<MongoClientSettings, MongoClient> mongoClientSupplier, final boolean waitForPoolAsyncWorkManagerStart) {
    if (!SUPPORTED_CLIENT_ENTITY_OPTIONS.containsAll(entity.keySet())) {
        throw new UnsupportedOperationException("Client entity contains unsupported options: " + entity.keySet() + ". Supported options are " + SUPPORTED_CLIENT_ENTITY_OPTIONS);
    }
    MongoClientSettings.Builder clientSettingsBuilder;
    if (entity.getBoolean("useMultipleMongoses", BsonBoolean.FALSE).getValue() && (isSharded() || isLoadBalanced())) {
        assumeTrue("Multiple mongos connection string not available for sharded cluster", !isSharded() || getMultiMongosConnectionString() != null);
        assumeTrue("Multiple mongos connection string not available for load-balanced cluster", !isLoadBalanced() || getMultiMongosConnectionString() != null);
        clientSettingsBuilder = getMultiMongosMongoClientSettingsBuilder();
    } else {
        clientSettingsBuilder = getMongoClientSettingsBuilder();
    }
    if (entity.containsKey("observeEvents")) {
        List<String> ignoreCommandMonitoringEvents = entity.getArray("ignoreCommandMonitoringEvents", new BsonArray()).stream().map(type -> type.asString().getValue()).collect(Collectors.toList());
        ignoreCommandMonitoringEvents.add("configureFailPoint");
        TestCommandListener testCommandListener = new TestCommandListener(entity.getArray("observeEvents").stream().map(type -> type.asString().getValue()).collect(Collectors.toList()), ignoreCommandMonitoringEvents, entity.getBoolean("observeSensitiveCommands", BsonBoolean.FALSE).getValue());
        clientSettingsBuilder.addCommandListener(testCommandListener);
        putEntity(id + "-command-listener", testCommandListener, clientCommandListeners);
        TestConnectionPoolListener testConnectionPoolListener = new TestConnectionPoolListener(entity.getArray("observeEvents").stream().map(type -> type.asString().getValue()).collect(Collectors.toList()));
        clientSettingsBuilder.applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(testConnectionPoolListener));
        putEntity(id + "-connection-pool-listener", testConnectionPoolListener, clientConnectionPoolListeners);
    } else {
        // Regardless of whether events are observed, we still need to track some info about the pool in order to implement
        // the assertNumberConnectionsCheckedOut operation
        TestConnectionPoolListener testConnectionPoolListener = new TestConnectionPoolListener();
        clientSettingsBuilder.applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(testConnectionPoolListener));
        putEntity(id + "-connection-pool-listener", testConnectionPoolListener, clientConnectionPoolListeners);
    }
    if (entity.containsKey("storeEventsAsEntities")) {
        BsonArray storeEventsAsEntitiesArray = entity.getArray("storeEventsAsEntities");
        for (BsonValue eventValue : storeEventsAsEntitiesArray) {
            BsonDocument eventDocument = eventValue.asDocument();
            String key = eventDocument.getString("id").getValue();
            BsonArray eventList = eventDocument.getArray("events");
            List<BsonDocument> eventDocumentList = synchronizedList(new ArrayList<>());
            putEntity(key, eventDocumentList, eventsMap);
            if (eventList.stream().map(value -> value.asString().getValue()).anyMatch(value -> value.startsWith("Command"))) {
                clientSettingsBuilder.addCommandListener(new EntityCommandListener(eventList.stream().map(value -> value.asString().getValue()).collect(Collectors.toSet()), eventDocumentList));
            }
            if (eventList.stream().map(value -> value.asString().getValue()).anyMatch(value -> value.startsWith("Pool") || value.startsWith("Connection"))) {
                clientSettingsBuilder.applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(new EntityConnectionPoolListener(eventList.stream().map(value -> value.asString().getValue()).collect(Collectors.toSet()), eventDocumentList)));
            }
        }
    }
    clientSettingsBuilder.applyToServerSettings(builder -> {
        builder.heartbeatFrequency(50, TimeUnit.MILLISECONDS);
        builder.minHeartbeatFrequency(50, TimeUnit.MILLISECONDS);
    });
    if (entity.containsKey("uriOptions")) {
        entity.getDocument("uriOptions").forEach((key, value) -> {
            switch(key) {
                case "retryReads":
                    clientSettingsBuilder.retryReads(value.asBoolean().getValue());
                    break;
                case "retryWrites":
                    clientSettingsBuilder.retryWrites(value.asBoolean().getValue());
                    break;
                case "readConcernLevel":
                    clientSettingsBuilder.readConcern(new ReadConcern(ReadConcernLevel.fromString(value.asString().getValue())));
                    break;
                case "w":
                    clientSettingsBuilder.writeConcern(new WriteConcern(value.asInt32().intValue()));
                    break;
                case "maxPoolSize":
                    clientSettingsBuilder.applyToConnectionPoolSettings(builder -> builder.maxSize(value.asNumber().intValue()));
                    break;
                case "waitQueueTimeoutMS":
                    clientSettingsBuilder.applyToConnectionPoolSettings(builder -> builder.maxWaitTime(value.asNumber().longValue(), TimeUnit.MILLISECONDS));
                    break;
                case "loadBalanced":
                    if (value.asBoolean().getValue()) {
                        clientSettingsBuilder.applyToClusterSettings(builder -> builder.mode(ClusterConnectionMode.LOAD_BALANCED));
                    }
                    break;
                case "appname":
                    clientSettingsBuilder.applicationName(value.asString().getValue());
                    break;
                default:
                    throw new UnsupportedOperationException("Unsupported uri option: " + key);
            }
        });
    }
    if (entity.containsKey("serverApi")) {
        BsonDocument serverApiDocument = entity.getDocument("serverApi");
        String apiVersion = serverApiDocument.getString("version").getValue();
        ServerApi.Builder serverApiBuilder = ServerApi.builder().version(ServerApiVersion.findByValue(apiVersion));
        if (serverApiDocument.containsKey("deprecationErrors")) {
            serverApiBuilder.deprecationErrors(serverApiDocument.getBoolean("deprecationErrors").getValue());
        }
        if (serverApiDocument.containsKey("strict")) {
            serverApiBuilder.strict(serverApiDocument.getBoolean("strict").getValue());
        }
        clientSettingsBuilder.serverApi(serverApiBuilder.build());
    }
    putEntity(id, mongoClientSupplier.apply(clientSettingsBuilder.build()), clients);
    if (waitForPoolAsyncWorkManagerStart) {
        waitForPoolAsyncWorkManagerStart();
    }
}
Also used : GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) MongoDatabase(com.mongodb.client.MongoDatabase) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) ConnectionPoolCreatedEvent(com.mongodb.event.ConnectionPoolCreatedEvent) BsonValue(org.bson.BsonValue) CommandListener(com.mongodb.event.CommandListener) ClusterFixture.isSharded(com.mongodb.ClusterFixture.isSharded) Fixture.getMongoClientSettingsBuilder(com.mongodb.client.Fixture.getMongoClientSettingsBuilder) Arrays.asList(java.util.Arrays.asList) CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) BsonArray(org.bson.BsonArray) Map(java.util.Map) ClusterFixture.getMultiMongosConnectionString(com.mongodb.ClusterFixture.getMultiMongosConnectionString) ReadConcern(com.mongodb.ReadConcern) ConnectionCheckedOutEvent(com.mongodb.event.ConnectionCheckedOutEvent) CommandEvent(com.mongodb.event.CommandEvent) UnifiedCrudHelper.asReadPreference(com.mongodb.client.unified.UnifiedCrudHelper.asReadPreference) Collections.synchronizedList(java.util.Collections.synchronizedList) Set(java.util.Set) ConnectionPoolClearedEvent(com.mongodb.event.ConnectionPoolClearedEvent) Collectors(java.util.stream.Collectors) List(java.util.List) EventMatcher.getReasonString(com.mongodb.client.unified.EventMatcher.getReasonString) TestConnectionPoolListener(com.mongodb.internal.connection.TestConnectionPoolListener) Assume.assumeTrue(org.junit.Assume.assumeTrue) MongoClientSettings(com.mongodb.MongoClientSettings) NotNull(org.jetbrains.annotations.NotNull) ConnectionClosedEvent(com.mongodb.event.ConnectionClosedEvent) ConnectionCheckedInEvent(com.mongodb.event.ConnectionCheckedInEvent) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) ServerApi(com.mongodb.ServerApi) ClusterFixture.isLoadBalanced(com.mongodb.ClusterFixture.isLoadBalanced) BsonBoolean(org.bson.BsonBoolean) HashMap(java.util.HashMap) AbstractConnectionPoolTest.waitForPoolAsyncWorkManagerStart(com.mongodb.internal.connection.AbstractConnectionPoolTest.waitForPoolAsyncWorkManagerStart) BsonString(org.bson.BsonString) Function(java.util.function.Function) Fixture.getMultiMongosMongoClientSettingsBuilder(com.mongodb.client.Fixture.getMultiMongosMongoClientSettingsBuilder) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) TransactionOptions(com.mongodb.TransactionOptions) HashSet(java.util.HashSet) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) MongoCursor(com.mongodb.client.MongoCursor) ClusterConnectionMode(com.mongodb.connection.ClusterConnectionMode) BsonInt64(org.bson.BsonInt64) ConnectionCreatedEvent(com.mongodb.event.ConnectionCreatedEvent) ConnectionReadyEvent(com.mongodb.event.ConnectionReadyEvent) ConnectionPoolListener(com.mongodb.event.ConnectionPoolListener) ServerApiVersion(com.mongodb.ServerApiVersion) ServerId(com.mongodb.connection.ServerId) UnifiedCrudHelper.asReadConcern(com.mongodb.client.unified.UnifiedCrudHelper.asReadConcern) BsonDouble(org.bson.BsonDouble) CommandFailedEvent(com.mongodb.event.CommandFailedEvent) TimeUnit(java.util.concurrent.TimeUnit) ConnectionPoolClosedEvent(com.mongodb.event.ConnectionPoolClosedEvent) ConnectionPoolReadyEvent(com.mongodb.event.ConnectionPoolReadyEvent) ClientSessionOptions(com.mongodb.ClientSessionOptions) ReadConcernLevel(com.mongodb.ReadConcernLevel) ConnectionCheckOutStartedEvent(com.mongodb.event.ConnectionCheckOutStartedEvent) UnifiedCrudHelper.asWriteConcern(com.mongodb.client.unified.UnifiedCrudHelper.asWriteConcern) ClientSession(com.mongodb.client.ClientSession) BsonInt32(org.bson.BsonInt32) WriteConcern(com.mongodb.WriteConcern) ConnectionId(com.mongodb.connection.ConnectionId) ConnectionCheckOutFailedEvent(com.mongodb.event.ConnectionCheckOutFailedEvent) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) TestConnectionPoolListener(com.mongodb.internal.connection.TestConnectionPoolListener) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) MongoClientSettings(com.mongodb.MongoClientSettings) ClusterFixture.getMultiMongosConnectionString(com.mongodb.ClusterFixture.getMultiMongosConnectionString) EventMatcher.getReasonString(com.mongodb.client.unified.EventMatcher.getReasonString) BsonString(org.bson.BsonString) ReadConcern(com.mongodb.ReadConcern) UnifiedCrudHelper.asReadConcern(com.mongodb.client.unified.UnifiedCrudHelper.asReadConcern) BsonDocument(org.bson.BsonDocument) UnifiedCrudHelper.asWriteConcern(com.mongodb.client.unified.UnifiedCrudHelper.asWriteConcern) WriteConcern(com.mongodb.WriteConcern) BsonArray(org.bson.BsonArray) ServerApi(com.mongodb.ServerApi) BsonValue(org.bson.BsonValue)

Aggregations

ReadConcernLevel (com.mongodb.ReadConcernLevel)3 BsonDocument (org.bson.BsonDocument)2 BsonString (org.bson.BsonString)2 ClientSessionOptions (com.mongodb.ClientSessionOptions)1 ClusterFixture.getMultiMongosConnectionString (com.mongodb.ClusterFixture.getMultiMongosConnectionString)1 ClusterFixture.isLoadBalanced (com.mongodb.ClusterFixture.isLoadBalanced)1 ClusterFixture.isSharded (com.mongodb.ClusterFixture.isSharded)1 MongoClientException (com.mongodb.MongoClientException)1 MongoClientSettings (com.mongodb.MongoClientSettings)1 ReadConcern (com.mongodb.ReadConcern)1 ServerApi (com.mongodb.ServerApi)1 ServerApiVersion (com.mongodb.ServerApiVersion)1 TransactionOptions (com.mongodb.TransactionOptions)1 WriteConcern (com.mongodb.WriteConcern)1 ClientSession (com.mongodb.client.ClientSession)1 Fixture.getMongoClientSettingsBuilder (com.mongodb.client.Fixture.getMongoClientSettingsBuilder)1 Fixture.getMultiMongosMongoClientSettingsBuilder (com.mongodb.client.Fixture.getMultiMongosMongoClientSettingsBuilder)1 MongoClient (com.mongodb.client.MongoClient)1 MongoCollection (com.mongodb.client.MongoCollection)1 MongoCursor (com.mongodb.client.MongoCursor)1