use of com.torodb.mongodb.core.WriteMongodTransaction in project torodb by torodb.
the class AkkaDbCloner method cloneIndex.
private void cloneIndex(MongodServer localServer, String fromDb, String dstDb, MongoConnection remoteConnection, CloneOptions opts, String fromCol, String toCol) throws CloningException {
WriteMongodTransaction transaction = createWriteMongodTransaction(localServer);
try {
try {
List<IndexOptions> indexesToClone = getIndexesToClone(Lists.newArrayList(ListIndexesRequester.getListCollections(remoteConnection, dstDb, fromCol).getFirstBatch()), dstDb, toCol, fromDb, fromCol, opts);
if (indexesToClone.isEmpty()) {
return;
}
Status<CreateIndexesResult> status = transaction.execute(new Request(dstDb, null, true, null), CreateIndexesCommand.INSTANCE, new CreateIndexesArgument(fromCol, indexesToClone));
if (!status.isOk()) {
throw new CloningException("Error while cloning indexes: " + status.getErrorMsg());
}
transaction.commit();
} catch (UserException | MongoException ex) {
throw new CloningException("Unexpected error while cloning indexes", ex);
}
} finally {
transaction.close();
}
}
use of com.torodb.mongodb.core.WriteMongodTransaction in project torodb by torodb.
the class AkkaDbCloner method prepareCollection.
private void prepareCollection(MongodServer localServer, String dstDb, Entry colEntry) throws RetrierAbortException {
try {
retrier.retry(() -> {
try (WriteMongodTransaction transaction = createWriteMongodTransaction(localServer)) {
dropCollection(transaction, dstDb, colEntry.getCollectionName());
createCollection(transaction, dstDb, colEntry.getCollectionName(), colEntry.getCollectionOptions());
transaction.commit();
return null;
} catch (UserException ex) {
throw new RetrierAbortException("An unexpected user exception was catched", ex);
}
});
} catch (RetrierGiveUpException ex) {
throw new CloningException(ex);
}
}
use of com.torodb.mongodb.core.WriteMongodTransaction in project torodb by torodb.
the class TorodbSafeRequestProcessor method execute.
@Override
public <A, R> Status<R> execute(Request req, Command<? super A, ? super R> command, A arg, MongodConnection connection) {
mongodMetrics.getCommands().mark();
Timer timer = mongodMetrics.getTimer(command);
try (Timer.Context ctx = timer.time()) {
Callable<Status<R>> callable;
RequiredTransaction commandType = commandsLibrary.getCommandType(command);
switch(commandType) {
case NO_TRANSACTION:
callable = () -> {
return connection.getCommandsExecutor().execute(req, command, arg, connection);
};
break;
case READ_TRANSACTION:
callable = () -> {
try (ReadOnlyMongodTransaction trans = connection.openReadOnlyTransaction()) {
return trans.execute(req, command, arg);
}
};
break;
case WRITE_TRANSACTION:
callable = () -> {
try (WriteMongodTransaction trans = connection.openWriteTransaction(true)) {
Status<R> result = trans.execute(req, command, arg);
if (result.isOk()) {
trans.commit();
}
return result;
}
};
break;
case EXCLUSIVE_WRITE_TRANSACTION:
callable = () -> {
try (ExclusiveWriteMongodTransaction trans = connection.openExclusiveWriteTransaction(true)) {
Status<R> result = trans.execute(req, command, arg);
if (result.isOk()) {
trans.commit();
}
return result;
}
};
break;
default:
throw new AssertionError("Unexpected command type" + commandType);
}
try {
return retrier.retry(callable);
} catch (RetrierGiveUpException ex) {
return Status.from(ErrorCode.CONFLICTING_OPERATION_IN_PROGRESS, "It was impossible to execute " + command.getCommandName() + " after several attempts");
}
}
}
use of com.torodb.mongodb.core.WriteMongodTransaction in project torodb by torodb.
the class SimpleAnalyzedOplogBatchExecutor method execute.
private void execute(NamespaceJob job, ApplierContext applierContext, MongodConnection connection, boolean optimisticDeleteAndCreate) throws RollbackException, UserException, NamespaceJobExecutionException, UniqueIndexViolationException {
try (WriteMongodTransaction mongoTransaction = connection.openWriteTransaction()) {
namespaceJobExecutor.apply(job, mongoTransaction, applierContext, optimisticDeleteAndCreate);
mongoTransaction.commit();
}
}
use of com.torodb.mongodb.core.WriteMongodTransaction in project torodb by torodb.
the class OplogManager method storeState.
@Locked(exclusive = true)
private void storeState(long hash, OpTime opTime) throws OplogManagerPersistException {
Preconditions.checkState(isRunning(), "The service is not running");
try {
retrier.retry(() -> {
try (WriteMongodTransaction transaction = connection.openWriteTransaction()) {
Status<Long> deleteResult = transaction.execute(new Request(OPLOG_DB, null, true, null), DeleteCommand.INSTANCE, new DeleteArgument.Builder(OPLOG_COL).addStatement(new DeleteStatement(DOC_QUERY, false)).build());
if (!deleteResult.isOk()) {
throw new RetrierAbortException(new MongoException(deleteResult));
}
//TODO: This should be stored as timestamp once TORODB-189 is resolved
long optimeAsLong = opTime.toOldBson().getMillisFromUnix();
Status<InsertResult> insertResult = transaction.execute(new Request(OPLOG_DB, null, true, null), InsertCommand.INSTANCE, new InsertArgument.Builder(OPLOG_COL).addDocument(new BsonDocumentBuilder().appendUnsafe(KEY, new BsonDocumentBuilder().appendUnsafe("hash", newLong(hash)).appendUnsafe("optime_i", DefaultBsonValues.newLong(optimeAsLong)).appendUnsafe("optime_t", newLong(opTime.getTerm())).build()).build()).build());
if (insertResult.isOk() && insertResult.getResult().getN() != 1) {
throw new RetrierAbortException(new MongoException(ErrorCode.OPERATION_FAILED, "More than one element inserted"));
}
if (!insertResult.isOk()) {
throw new RetrierAbortException(new MongoException(insertResult));
}
transaction.commit();
return Empty.getInstance();
} catch (UserException ex) {
throw new RetrierAbortException(ex);
}
}, Hint.INFREQUENT_ROLLBACK);
} catch (RetrierGiveUpException ex) {
throw new OplogManagerPersistException(ex);
}
}
Aggregations