use of com.mongodb.WriteConcern in project jackrabbit-oak by apache.
the class MongoDocumentStore method setReadWriteMode.
@Override
public void setReadWriteMode(String readWriteMode) {
if (readWriteMode == null || readWriteMode.equals(lastReadWriteMode)) {
return;
}
lastReadWriteMode = readWriteMode;
try {
String rwModeUri = readWriteMode;
if (!readWriteMode.startsWith("mongodb://")) {
rwModeUri = String.format("mongodb://localhost/?%s", readWriteMode);
}
MongoClientURI uri = new MongoClientURI(rwModeUri);
ReadPreference readPref = uri.getOptions().getReadPreference();
if (!readPref.equals(nodes.getReadPreference())) {
nodes = nodes.withReadPreference(readPref);
LOG.info("Using ReadPreference {} ", readPref);
}
WriteConcern writeConcern = uri.getOptions().getWriteConcern();
if (!writeConcern.equals(nodes.getWriteConcern())) {
nodes = nodes.withWriteConcern(writeConcern);
LOG.info("Using WriteConcern " + writeConcern);
}
} catch (Exception e) {
LOG.error("Error setting readWriteMode " + readWriteMode, e);
}
}
use of com.mongodb.WriteConcern in project jackrabbit-oak by apache.
the class MongoCacheConsistencyTest method getFixture.
@Override
public DocumentStoreFixture getFixture() throws Exception {
Fongo fongo = new OakFongo("fongo") {
private String suppressedEx = null;
@Override
protected void afterInsert(WriteResult result) {
maybeThrow();
}
@Override
protected void afterFindAndModify(DBObject result) {
maybeThrow();
}
@Override
protected void afterUpdate(WriteResult result) {
maybeThrow();
}
@Override
protected void afterRemove(WriteResult result) {
maybeThrow();
}
@Override
protected void beforeExecuteBulkWriteOperation(boolean ordered, Boolean bypassDocumentValidation, List<?> writeRequests, WriteConcern aWriteConcern) {
// suppress potentially set exception message because
// fongo bulk writes call other update methods
suppressedEx = exceptionMsg;
exceptionMsg = null;
}
@Override
protected void afterExecuteBulkWriteOperation(BulkWriteResult result) {
exceptionMsg = suppressedEx;
suppressedEx = null;
maybeThrow();
}
private void maybeThrow() {
if (exceptionMsg != null) {
throw new MongoException(exceptionMsg);
}
}
};
DocumentMK.Builder builder = provider.newBuilder().setAsyncDelay(0);
final DocumentStore store = new MongoDocumentStore(fongo.getMongo(), "oak", builder);
return new DocumentStoreFixture() {
@Override
public String getName() {
return "MongoDB";
}
@Override
public DocumentStore createDocumentStore(DocumentMK.Builder builder) {
return store;
}
};
}
use of com.mongodb.WriteConcern in project mongo-java-driver by mongodb.
the class CommitTransactionOperation method getRetryCommandModifier.
@Override
protected Function<BsonDocument, BsonDocument> getRetryCommandModifier() {
return new Function<BsonDocument, BsonDocument>() {
@Override
public BsonDocument apply(final BsonDocument command) {
WriteConcern retryWriteConcern = getWriteConcern().withW("majority");
if (retryWriteConcern.getWTimeout(TimeUnit.MILLISECONDS) == null) {
retryWriteConcern = retryWriteConcern.withWTimeout(10000, TimeUnit.MILLISECONDS);
}
command.put("writeConcern", retryWriteConcern.asDocument());
if (recoveryToken != null) {
command.put("recoveryToken", recoveryToken);
}
return command;
}
};
}
use of com.mongodb.WriteConcern in project mongo-java-driver by mongodb.
the class JsonPoweredCrudTestHelper method getCollection.
private MongoCollection<BsonDocument> getCollection(final BsonDocument collectionOptions) {
MongoCollection<BsonDocument> retVal = baseCollection;
if (collectionOptions.containsKey("readPreference")) {
retVal = retVal.withReadPreference(getReadPreference(collectionOptions));
}
if (collectionOptions.containsKey("writeConcern")) {
WriteConcern writeConcern = getWriteConcern(collectionOptions);
retVal = retVal.withWriteConcern(writeConcern);
}
if (collectionOptions.containsKey("readConcern")) {
ReadConcern readConcern = getReadConcern(collectionOptions);
retVal = retVal.withReadConcern(readConcern);
}
return retVal;
}
use of com.mongodb.WriteConcern in project mongo-java-driver by mongodb.
the class MixedBulkWriteOperation method executeAsync.
public void executeAsync(final AsyncWriteBinding binding, final SingleResultCallback<BulkWriteResult> callback) {
// see the comment in `execute(WriteBinding)` explaining the manual tracking of attempts
RetryState retryState = new RetryState();
BulkWriteTracker.attachNew(retryState, retryWrites);
binding.retain();
AsyncCallbackSupplier<BulkWriteResult> retryingBulkWrite = this.<BulkWriteResult>decorateWriteWithRetries(retryState, funcCallback -> {
logRetryExecute(retryState);
withAsyncSourceAndConnection(binding::getWriteConnectionSource, true, funcCallback, (source, connection, releasingCallback) -> {
ConnectionDescription connectionDescription = connection.getDescription();
int maxWireVersion = connectionDescription.getMaxWireVersion();
// attach `maxWireVersion` ASAP because it is used to check whether we can retry
retryState.attach(AttachmentKeys.maxWireVersion(), maxWireVersion, true);
BulkWriteTracker bulkWriteTracker = retryState.attachment(AttachmentKeys.bulkWriteTracker()).orElseThrow(Assertions::fail);
SessionContext sessionContext = binding.getSessionContext();
WriteConcern writeConcern = getAppliedWriteConcern(sessionContext);
if (!retryState.isFirstAttempt() && !isRetryableWrite(retryWrites, writeConcern, source.getServerDescription(), connectionDescription, sessionContext)) {
Throwable prospectiveFailedResult = retryState.exception().orElse(null);
if (retryState.breakAndCompleteIfRetryAnd(() -> !(prospectiveFailedResult instanceof MongoWriteConcernWithResponseException), releasingCallback)) {
return;
}
bulkWriteTracker.batch().ifPresent(bulkWriteBatch -> {
assertTrue(prospectiveFailedResult instanceof MongoWriteConcernWithResponseException);
bulkWriteBatch.addResult((BsonDocument) ((MongoWriteConcernWithResponseException) prospectiveFailedResult).getResponse());
BulkWriteTracker.attachNext(retryState, bulkWriteBatch);
});
}
if (validateWriteRequestsAndCompleteIfInvalid(connectionDescription, bypassDocumentValidation, writeRequests, writeConcern, releasingCallback)) {
return;
}
if (writeConcern.isAcknowledged() || serverIsAtLeastVersionThreeDotSix(connectionDescription)) {
try {
if (!bulkWriteTracker.batch().isPresent()) {
BulkWriteTracker.attachNew(retryState, BulkWriteBatch.createBulkWriteBatch(namespace, source.getServerDescription(), connectionDescription, ordered, writeConcern, bypassDocumentValidation, retryWrites, writeRequests, sessionContext));
}
} catch (Throwable t) {
releasingCallback.onResult(null, t);
return;
}
logRetryExecute(retryState);
executeBulkWriteBatchAsync(retryState, binding, connection, maxWireVersion, releasingCallback);
} else {
retryState.markAsLastAttempt();
executeLegacyBatchesAsync(binding, connection, releasingCallback);
}
});
}).whenComplete(binding::release);
retryingBulkWrite.get(exceptionTransformingCallback(errorHandlingCallback(callback, LOGGER)));
}
Aggregations