Search in sources :

Example 51 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class UnifiedTest method addInitialData.

private void addInitialData() {
    for (BsonValue cur : initialData.getValues()) {
        BsonDocument curDataSet = cur.asDocument();
        CollectionHelper<BsonDocument> helper = new CollectionHelper<>(new BsonDocumentCodec(), new MongoNamespace(curDataSet.getString("databaseName").getValue(), curDataSet.getString("collectionName").getValue()));
        helper.create(WriteConcern.MAJORITY);
        BsonArray documentsArray = curDataSet.getArray("documents", new BsonArray());
        if (!documentsArray.isEmpty()) {
            helper.insertDocuments(documentsArray.stream().map(BsonValue::asDocument).collect(toList()), WriteConcern.MAJORITY);
        }
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) CollectionHelper(com.mongodb.client.test.CollectionHelper) MongoNamespace(com.mongodb.MongoNamespace) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BsonValue(org.bson.BsonValue)

Example 52 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class WorkloadExecutor method main.

public static void main(String[] args) throws IOException {
    if (args.length != 2) {
        System.out.println("Usage: AstrolabeTestRunner <path to workload spec JSON file> <path to results directory>");
        System.exit(1);
    }
    String pathToWorkloadFile = args[0];
    String pathToResultsDirectory = args[1];
    LOGGER.info("Max memory (GB): " + (Runtime.getRuntime().maxMemory() / 1_073_741_824.0));
    LOGGER.info("Path to workload file: '" + pathToWorkloadFile + "'");
    LOGGER.info("Path to results directory: '" + pathToResultsDirectory + "'");
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        LOGGER.info("Running shutdown hook");
        terminateLoop = true;
        try {
            if (!terminationLatch.await(1, TimeUnit.MINUTES)) {
                LOGGER.warn("Terminating after waiting for 1 minute for results to be written");
            } else {
                LOGGER.info("Terminating.");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }));
    BsonDocument fileDocument;
    try (FileReader reader = new FileReader(pathToWorkloadFile)) {
        fileDocument = new BsonDocumentCodec().decode(new JsonReader(reader), DecoderContext.builder().build());
    }
    LOGGER.info("Executing workload: " + fileDocument.toJson(JsonWriterSettings.builder().indent(true).build()));
    BsonArray testArray = fileDocument.getArray("tests");
    if (testArray.size() != 1) {
        throw new IllegalArgumentException("Expected exactly one test");
    }
    BsonDocument testDocument = testArray.get(0).asDocument();
    UnifiedTest unifiedTest = new UnifiedSyncTest(fileDocument.getString("schemaVersion").getValue(), fileDocument.getArray("runOnRequirements", null), fileDocument.getArray("createEntities", new BsonArray()), fileDocument.getArray("initialData", new BsonArray()), testDocument) {

        @Override
        protected boolean terminateLoop() {
            return terminateLoop;
        }
    };
    try {
        unifiedTest.setUp();
        unifiedTest.shouldPassAllOutcomes();
        Entities entities = unifiedTest.getEntities();
        long iterationCount = -1;
        if (entities.hasIterationCount("iterations")) {
            iterationCount = entities.getIterationCount("iterations");
        }
        long successCount = -1;
        if (entities.hasSuccessCount("successes")) {
            successCount = entities.getSuccessCount("successes");
        }
        BsonArray errorDocuments = null;
        long errorCount = 0;
        if (entities.hasErrorDocuments("errors")) {
            errorDocuments = entities.getErrorDocuments("errors");
            errorCount = errorDocuments.size();
        }
        BsonArray failureDocuments = null;
        long failureCount = 0;
        if (entities.hasFailureDocuments("failures")) {
            failureDocuments = entities.getFailureDocuments("failures");
            failureCount = failureDocuments.size();
        }
        BsonArray eventDocuments = new BsonArray();
        if (entities.hasEvents("events")) {
            eventDocuments = new BsonArray(entities.getEvents("events"));
        }
        BsonDocument eventsDocument = new BsonDocument().append("errors", errorDocuments == null ? new BsonArray() : errorDocuments).append("failures", failureDocuments == null ? new BsonArray() : failureDocuments).append("events", eventDocuments);
        BsonDocument resultsDocument = new BsonDocument().append("numErrors", new BsonInt64(errorCount)).append("numFailures", new BsonInt64(failureCount)).append("numSuccesses", new BsonInt64(successCount)).append("numIterations", new BsonInt64(iterationCount));
        writeFile(eventsDocument, Paths.get(pathToResultsDirectory, "events.json"));
        writeFile(resultsDocument, Paths.get(pathToResultsDirectory, "results.json"));
    } finally {
        unifiedTest.cleanUp();
        terminationLatch.countDown();
    }
}
Also used : UnifiedSyncTest(com.mongodb.client.unified.UnifiedSyncTest) Entities(com.mongodb.client.unified.Entities) BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) JsonReader(org.bson.json.JsonReader) FileReader(java.io.FileReader) UnifiedTest(com.mongodb.client.unified.UnifiedTest) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 53 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class CommandOperationHelper method executeCommandAsync.

/* Async Write Binding Helpers */
static <T> void executeCommandAsync(final AsyncWriteBinding binding, final String database, final BsonDocument command, final AsyncConnection connection, final CommandWriteTransformerAsync<BsonDocument, T> transformer, final SingleResultCallback<T> callback) {
    notNull("binding", binding);
    SingleResultCallback<T> addingRetryableLabelCallback = addingRetryableLabelCallback(callback, connection.getDescription().getMaxWireVersion());
    connection.commandAsync(database, command, new NoOpFieldNameValidator(), primary(), new BsonDocumentCodec(), binding.getSessionContext(), binding.getServerApi(), binding.getRequestContext(), transformingWriteCallback(transformer, connection, addingRetryableLabelCallback));
}
Also used : NoOpFieldNameValidator(com.mongodb.internal.validator.NoOpFieldNameValidator) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 54 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class ListCollectionsOperation method execute.

@Override
public BatchCursor<T> execute(final ReadBinding binding) {
    RetryState retryState = initialRetryState(retryReads);
    Supplier<BatchCursor<T>> read = decorateReadWithRetries(retryState, () -> {
        logRetryExecute(retryState);
        return withSourceAndConnection(binding::getReadConnectionSource, false, (source, connection) -> {
            retryState.breakAndThrowIfRetryAnd(() -> !canRetryRead(source.getServerDescription(), connection.getDescription(), binding.getSessionContext()));
            if (serverIsAtLeastVersionThreeDotZero(connection.getDescription())) {
                try {
                    return createReadCommandAndExecute(retryState, binding, source, databaseName, getCommandCreator(), createCommandDecoder(), commandTransformer(), connection);
                } catch (MongoCommandException e) {
                    return rethrowIfNotNamespaceError(e, createEmptyBatchCursor(createNamespace(), decoder, source.getServerDescription().getAddress(), batchSize));
                }
            } else {
                retryState.markAsLastAttempt();
                return new ProjectingBatchCursor(new QueryBatchCursor<>(connection.query(getNamespace(), asQueryDocument(connection.getDescription(), binding.getReadPreference()), null, 0, 0, batchSize, binding.getReadPreference().isSecondaryOk(), false, false, false, false, false, new BsonDocumentCodec(), binding.getRequestContext()), 0, batchSize, new BsonDocumentCodec(), source));
            }
        });
    });
    return read.get();
}
Also used : OperationHelper.cursorDocumentToAsyncBatchCursor(com.mongodb.internal.operation.OperationHelper.cursorDocumentToAsyncBatchCursor) OperationHelper.createEmptyAsyncBatchCursor(com.mongodb.internal.operation.OperationHelper.createEmptyAsyncBatchCursor) OperationHelper.cursorDocumentToBatchCursor(com.mongodb.internal.operation.OperationHelper.cursorDocumentToBatchCursor) OperationHelper.createEmptyBatchCursor(com.mongodb.internal.operation.OperationHelper.createEmptyBatchCursor) AsyncBatchCursor(com.mongodb.internal.async.AsyncBatchCursor) MongoCommandException(com.mongodb.MongoCommandException) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) CommandOperationHelper.initialRetryState(com.mongodb.internal.operation.CommandOperationHelper.initialRetryState) RetryState(com.mongodb.internal.async.function.RetryState)

Example 55 with BsonDocumentCodec

use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.

the class ListCollectionsOperation method executeAsync.

@Override
public void executeAsync(final AsyncReadBinding binding, final SingleResultCallback<AsyncBatchCursor<T>> callback) {
    RetryState retryState = initialRetryState(retryReads);
    binding.retain();
    AsyncCallbackSupplier<AsyncBatchCursor<T>> asyncRead = CommandOperationHelper.<AsyncBatchCursor<T>>decorateReadWithRetries(retryState, funcCallback -> {
        logRetryExecute(retryState);
        withAsyncSourceAndConnection(binding::getReadConnectionSource, false, funcCallback, (source, connection, releasingCallback) -> {
            if (retryState.breakAndCompleteIfRetryAnd(() -> !canRetryRead(source.getServerDescription(), connection.getDescription(), binding.getSessionContext()), releasingCallback)) {
                return;
            }
            if (serverIsAtLeastVersionThreeDotZero(connection.getDescription())) {
                createReadCommandAndExecuteAsync(retryState, binding, source, databaseName, getCommandCreator(), createCommandDecoder(), asyncTransformer(), connection, (result, t) -> {
                    if (t != null && !isNamespaceError(t)) {
                        releasingCallback.onResult(null, t);
                    } else {
                        releasingCallback.onResult(result != null ? result : emptyAsyncCursor(source), null);
                    }
                });
            } else {
                retryState.markAsLastAttempt();
                connection.queryAsync(getNamespace(), asQueryDocument(connection.getDescription(), binding.getReadPreference()), null, 0, 0, batchSize, binding.getReadPreference().isSecondaryOk(), false, false, false, false, false, new BsonDocumentCodec(), binding.getRequestContext(), new SingleResultCallback<QueryResult<BsonDocument>>() {

                    @Override
                    public void onResult(final QueryResult<BsonDocument> result, final Throwable t) {
                        if (t != null) {
                            releasingCallback.onResult(null, t);
                        } else {
                            releasingCallback.onResult(new ProjectingAsyncBatchCursor(new AsyncQueryBatchCursor<BsonDocument>(result, 0, batchSize, 0, new BsonDocumentCodec(), source, connection)), null);
                        }
                    }
                });
            }
        });
    }).whenComplete(binding::release);
    asyncRead.get(errorHandlingCallback(callback, LOGGER));
}
Also used : AsyncReadBinding(com.mongodb.internal.binding.AsyncReadBinding) ServerDescription(com.mongodb.connection.ServerDescription) CommandOperationHelper.initialRetryState(com.mongodb.internal.operation.CommandOperationHelper.initialRetryState) ConnectionDescription(com.mongodb.connection.ConnectionDescription) Arrays.asList(java.util.Arrays.asList) BsonArray(org.bson.BsonArray) CursorHelper.getCursorDocumentFromBatchSize(com.mongodb.internal.operation.CursorHelper.getCursorDocumentFromBatchSize) OperationHelper.canRetryRead(com.mongodb.internal.operation.OperationHelper.canRetryRead) QueryResult(com.mongodb.internal.connection.QueryResult) OperationHelper.withSourceAndConnection(com.mongodb.internal.operation.OperationHelper.withSourceAndConnection) ConnectionSource(com.mongodb.internal.binding.ConnectionSource) ReadBinding(com.mongodb.internal.binding.ReadBinding) AsyncCallbackSupplier(com.mongodb.internal.async.function.AsyncCallbackSupplier) OperationHelper.cursorDocumentToAsyncBatchCursor(com.mongodb.internal.operation.OperationHelper.cursorDocumentToAsyncBatchCursor) CommandOperationHelper.rethrowIfNotNamespaceError(com.mongodb.internal.operation.CommandOperationHelper.rethrowIfNotNamespaceError) String.format(java.lang.String.format) SingleResultCallback(com.mongodb.internal.async.SingleResultCallback) Decoder(org.bson.codecs.Decoder) CommandReadTransformerAsync(com.mongodb.internal.operation.CommandOperationHelper.CommandReadTransformerAsync) RetryState(com.mongodb.internal.async.function.RetryState) List(java.util.List) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) BsonDocumentReader(org.bson.BsonDocumentReader) Codec(org.bson.codecs.Codec) ReadPreference.primary(com.mongodb.ReadPreference.primary) OperationHelper.createEmptyAsyncBatchCursor(com.mongodb.internal.operation.OperationHelper.createEmptyAsyncBatchCursor) ReadPreference(com.mongodb.ReadPreference) CommandOperationHelper.logRetryExecute(com.mongodb.internal.operation.CommandOperationHelper.logRetryExecute) CommandCreator(com.mongodb.internal.operation.CommandOperationHelper.CommandCreator) OperationHelper.cursorDocumentToBatchCursor(com.mongodb.internal.operation.OperationHelper.cursorDocumentToBatchCursor) BsonBoolean(org.bson.BsonBoolean) MongoCommandException(com.mongodb.MongoCommandException) ServerCursor(com.mongodb.ServerCursor) ErrorHandlingResultCallback.errorHandlingCallback(com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback) CommandReadTransformer(com.mongodb.internal.operation.CommandOperationHelper.CommandReadTransformer) BsonString(org.bson.BsonString) CommandOperationHelper.createReadCommandAndExecuteAsync(com.mongodb.internal.operation.CommandOperationHelper.createReadCommandAndExecuteAsync) Supplier(java.util.function.Supplier) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) LOGGER(com.mongodb.internal.operation.OperationHelper.LOGGER) Connection(com.mongodb.internal.connection.Connection) SHARD_ROUTER(com.mongodb.connection.ServerType.SHARD_ROUTER) BsonInt64(org.bson.BsonInt64) AsyncConnectionSource(com.mongodb.internal.binding.AsyncConnectionSource) DecoderContext(org.bson.codecs.DecoderContext) CommandOperationHelper.isNamespaceError(com.mongodb.internal.operation.CommandOperationHelper.isNamespaceError) ServerAddress(com.mongodb.ServerAddress) OperationHelper.createEmptyBatchCursor(com.mongodb.internal.operation.OperationHelper.createEmptyBatchCursor) MongoNamespace(com.mongodb.MongoNamespace) CommandOperationHelper.createReadCommandAndExecute(com.mongodb.internal.operation.CommandOperationHelper.createReadCommandAndExecute) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) CommandOperationHelper.decorateReadWithRetries(com.mongodb.internal.operation.CommandOperationHelper.decorateReadWithRetries) TimeUnit(java.util.concurrent.TimeUnit) AsyncBatchCursor(com.mongodb.internal.async.AsyncBatchCursor) OperationHelper.withAsyncSourceAndConnection(com.mongodb.internal.operation.OperationHelper.withAsyncSourceAndConnection) BsonInt32(org.bson.BsonInt32) AsyncConnection(com.mongodb.internal.connection.AsyncConnection) ServerVersionHelper.serverIsAtLeastVersionThreeDotZero(com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotZero) BsonRegularExpression(org.bson.BsonRegularExpression) OperationHelper.cursorDocumentToAsyncBatchCursor(com.mongodb.internal.operation.OperationHelper.cursorDocumentToAsyncBatchCursor) OperationHelper.createEmptyAsyncBatchCursor(com.mongodb.internal.operation.OperationHelper.createEmptyAsyncBatchCursor) AsyncBatchCursor(com.mongodb.internal.async.AsyncBatchCursor) SingleResultCallback(com.mongodb.internal.async.SingleResultCallback) QueryResult(com.mongodb.internal.connection.QueryResult) BsonDocument(org.bson.BsonDocument) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) CommandOperationHelper.initialRetryState(com.mongodb.internal.operation.CommandOperationHelper.initialRetryState) RetryState(com.mongodb.internal.async.function.RetryState)

Aggregations

BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)56 BsonDocument (org.bson.BsonDocument)29 MongoNamespace (com.mongodb.MongoNamespace)9 BsonBinaryReader (org.bson.BsonBinaryReader)8 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)8 BsonBinaryWriter (org.bson.BsonBinaryWriter)5 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)5 JsonReader (org.bson.json.JsonReader)5 Test (org.junit.Test)5 CollectionHelper (com.mongodb.client.test.CollectionHelper)4 NoOpFieldNameValidator (com.mongodb.internal.validator.NoOpFieldNameValidator)4 StringWriter (java.io.StringWriter)4 BsonArray (org.bson.BsonArray)4 BsonString (org.bson.BsonString)4 JsonWriter (org.bson.json.JsonWriter)4 ByteBuffer (java.nio.ByteBuffer)3 BsonDocumentWriter (org.bson.BsonDocumentWriter)3 Before (org.junit.Before)3 MongoClientSettings (com.mongodb.MongoClientSettings)2 MongoCommandException (com.mongodb.MongoCommandException)2