Search in sources :

Example 31 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project ditto by eclipse.

the class MongoSearchUpdaterFlowTest method testThroughput.

@Test
@SuppressWarnings("unchecked")
public void testThroughput() throws Throwable {
    new TestKit(actorSystem) {

        {
            // GIVEN: Persistence has high latency
            final var latency = Duration.ofSeconds(1);
            final MongoDatabase db = Mockito.mock(MongoDatabase.class);
            final MongoCollection<Document> collection = Mockito.mock(MongoCollection.class);
            Mockito.when(db.getCollection(any(), any(Class.class))).thenReturn(collection);
            doAnswer(inv -> {
                final var size = inv.<List<?>>getArgument(0).size();
                final BulkWriteResult result = BulkWriteResult.acknowledged(0, size, 0, size, List.of(), List.of());
                return Source.single(result).delay(latency, DelayOverflowStrategy.backpressure()).runWith(Sink.asPublisher(AsPublisher.WITHOUT_FANOUT), actorSystem);
            }).when(collection).bulkWrite(any(), any(BulkWriteOptions.class));
            final MongoSearchUpdaterFlow flow = MongoSearchUpdaterFlow.of(db, DefaultPersistenceStreamConfig.of(ConfigFactory.empty()), SearchUpdateMapper.get(actorSystem));
            // WHEN: 25 updates go through 32 parallel streams
            final int numberOfChanges = 25;
            final CountDownLatch latch = new CountDownLatch(numberOfChanges);
            final Sink<Source<AbstractWriteModel, NotUsed>, CompletionStage<Done>> sink = flow.start(false, 32, 1).map(writeResultAndErrors -> {
                latch.countDown();
                return writeResultAndErrors;
            }).toMat(Sink.ignore(), Keep.right());
            final Metadata metadata = Metadata.of(ThingId.of("thing:id"), 1L, PolicyId.of("policy:id"), 1L, null);
            final AbstractWriteModel abstractWriteModel = ThingWriteModel.of(metadata, new BsonDocument());
            final Thread testRunnerThread = Thread.currentThread();
            final AtomicReference<Throwable> errorBox = new AtomicReference<>();
            Source.repeat(Source.single(abstractWriteModel)).take(numberOfChanges).runWith(Objects.requireNonNull(sink), actorSystem).exceptionally(error -> {
                errorBox.set(error);
                testRunnerThread.interrupt();
                return null;
            });
            try {
                final var result = latch.await(20L, TimeUnit.SECONDS);
                assertThat(latch.getCount()).isZero();
                assertThat(result).isTrue();
            } catch (final InterruptedException e) {
                if (errorBox.get() != null) {
                    throw errorBox.get();
                } else {
                    throw e;
                }
            }
        }
    };
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Document(org.bson.Document) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Source(akka.stream.javadsl.Source) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) SystemMaterializer(akka.stream.SystemMaterializer) Materializer(akka.stream.Materializer) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) WriteModel(com.mongodb.client.model.WriteModel) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) BsonDocument(org.bson.BsonDocument) AsPublisher(akka.stream.javadsl.AsPublisher) Keep(akka.stream.javadsl.Keep) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractWriteModel(org.eclipse.ditto.thingsearch.service.persistence.write.model.AbstractWriteModel) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Duration(java.time.Duration) After(org.junit.After) ConfigFactory(com.typesafe.config.ConfigFactory) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) ThingWriteModel(org.eclipse.ditto.thingsearch.service.persistence.write.model.ThingWriteModel) RestartSink(akka.stream.javadsl.RestartSink) Done(akka.Done) MongoException(com.mongodb.MongoException) Publisher(org.reactivestreams.Publisher) PolicyId(org.eclipse.ditto.policies.model.PolicyId) Sink(akka.stream.javadsl.Sink) Test(org.junit.Test) DelayOverflowStrategy(akka.stream.DelayOverflowStrategy) UUID(java.util.UUID) ThingId(org.eclipse.ditto.things.model.ThingId) OverflowStrategy(akka.stream.OverflowStrategy) TestKit(akka.testkit.javadsl.TestKit) Metadata(org.eclipse.ditto.thingsearch.service.persistence.write.model.Metadata) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultPersistenceStreamConfig(org.eclipse.ditto.thingsearch.service.common.config.DefaultPersistenceStreamConfig) Mockito(org.mockito.Mockito) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) NotUsed(akka.NotUsed) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) ActorSystem(akka.actor.ActorSystem) AbstractWriteModel(org.eclipse.ditto.thingsearch.service.persistence.write.model.AbstractWriteModel) Metadata(org.eclipse.ditto.thingsearch.service.persistence.write.model.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestKit(akka.testkit.javadsl.TestKit) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) CountDownLatch(java.util.concurrent.CountDownLatch) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) Source(akka.stream.javadsl.Source) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) BsonDocument(org.bson.BsonDocument) CompletionStage(java.util.concurrent.CompletionStage) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Test(org.junit.Test)

Example 32 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project ditto by eclipse.

the class MongoClientWrapperTest method assertWithExpected.

private static void assertWithExpected(final DittoMongoClient mongoClient, final boolean sslEnabled, final boolean withCredentials) {
    final MongoClientSettings mongoClientSettings = mongoClient.getClientSettings();
    assertThat(mongoClientSettings.getClusterSettings().getHosts()).isEqualTo(Collections.singletonList(new ServerAddress(KNOWN_SERVER_ADDRESS)));
    @Nullable final MongoCredential expectedCredential = withCredentials ? MongoCredential.createCredential(KNOWN_USER, KNOWN_DB_NAME, KNOWN_PASSWORD.toCharArray()) : null;
    assertThat(mongoClientSettings.getCredential()).isEqualTo(expectedCredential);
    assertThat(mongoClientSettings.getSslSettings().isEnabled()).isEqualTo(sslEnabled);
    final MongoDatabase mongoDatabase = mongoClient.getDefaultDatabase();
    assertThat(mongoDatabase).isNotNull();
    assertThat(mongoDatabase.getName()).isEqualTo(KNOWN_DB_NAME);
}
Also used : MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress) MongoClientSettings(com.mongodb.MongoClientSettings) Nullable(javax.annotation.Nullable) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase)

Example 33 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project ditto by eclipse.

the class ConnectionPersistenceOperationsActor method props.

/**
 * Create Props of this actor.
 *
 * @param pubSubMediator Akka pub-sub mediator.
 * @param mongoDbConfig the MongoDB configuration settings.
 * @param config configuration with info about event journal, snapshot store and database.
 * @param persistenceOperationsConfig the persistence operations configuration settings.
 * @return a Props object.
 */
public static Props props(final ActorRef pubSubMediator, final MongoDbConfig mongoDbConfig, final Config config, final PersistenceOperationsConfig persistenceOperationsConfig) {
    return Props.create(ConnectionPersistenceOperationsActor.class, () -> {
        final MongoEventSourceSettings eventSourceSettings = MongoEventSourceSettings.fromConfig(config, ConnectionPersistenceActor.PERSISTENCE_ID_PREFIX, false, ConnectionPersistenceActor.JOURNAL_PLUGIN_ID, ConnectionPersistenceActor.SNAPSHOT_PLUGIN_ID);
        final MongoClientWrapper mongoClient = MongoClientWrapper.newInstance(mongoDbConfig);
        final MongoDatabase db = mongoClient.getDefaultDatabase();
        final EntityPersistenceOperations entitiesOps = MongoEntitiesPersistenceOperations.of(db, eventSourceSettings);
        return new ConnectionPersistenceOperationsActor(pubSubMediator, entitiesOps, mongoClient, persistenceOperationsConfig);
    });
}
Also used : EntityPersistenceOperations(org.eclipse.ditto.internal.utils.persistence.operations.EntityPersistenceOperations) MongoEventSourceSettings(org.eclipse.ditto.internal.utils.persistence.mongo.ops.eventsource.MongoEventSourceSettings) MongoClientWrapper(org.eclipse.ditto.internal.utils.persistence.mongo.MongoClientWrapper) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase)

Example 34 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project helidon by oracle.

the class MongoDbCommandExecutor method callStatement.

private static Flow.Publisher<DbRow> callStatement(MongoDbStatement dbStatement, MongoDbStatement.MongoStatement mongoStmt, CompletableFuture<Void> statementFuture, CompletableFuture<Long> commandFuture) {
    MongoDatabase db = dbStatement.db();
    Document command = mongoStmt.getQuery();
    LOGGER.fine(() -> String.format("Command: %s", command.toString()));
    Publisher<Document> publisher = dbStatement.noTx() ? db.runCommand(command) : db.runCommand(dbStatement.txManager().tx(), command);
    return new CommandRows(publisher, dbStatement, statementFuture, commandFuture).publisher();
}
Also used : Document(org.bson.Document) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase)

Example 35 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project helidon by oracle.

the class MongoDbClientTest method testUnwrapExecutorClass.

@Test
void testUnwrapExecutorClass() {
    MongoDbClient dbClient = new MongoDbClient(new MongoDbClientProviderBuilder(), CLIENT, DB);
    dbClient.execute(exec -> {
        Single<MongoDatabase> future = exec.unwrap(MongoDatabase.class);
        MongoDatabase connection = future.await();
        assertThat(connection, notNullValue());
        return exec.query("{\"operation\": \"command\", \"query\": { ping: 1 }}");
    });
}
Also used : MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Test(org.junit.jupiter.api.Test)

Aggregations

MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)38 Document (org.bson.Document)15 MongoClient (com.mongodb.reactivestreams.client.MongoClient)9 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)8 DeleteResult (com.mongodb.client.result.DeleteResult)6 List (java.util.List)6 Map (java.util.Map)6 HashMap (java.util.HashMap)5 BsonDocument (org.bson.BsonDocument)5 MongoClientSettings (com.mongodb.MongoClientSettings)4 MongoException (com.mongodb.MongoException)4 InsertOneResult (com.mongodb.client.result.InsertOneResult)4 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)4 Publisher (org.reactivestreams.Publisher)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 WriteConcern (com.mongodb.WriteConcern)3 DeleteOneModel (com.mongodb.client.model.DeleteOneModel)3 FindPublisher (com.mongodb.reactivestreams.client.FindPublisher)3 MongoClients (com.mongodb.reactivestreams.client.MongoClients)3