Search in sources :

Example 1 with ReadOnlyMongodTransaction

use of com.torodb.mongodb.core.ReadOnlyMongodTransaction 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");
        }
    }
}
Also used : Status(com.eightkdata.mongowp.Status) ExclusiveWriteMongodTransaction(com.torodb.mongodb.core.ExclusiveWriteMongodTransaction) WriteMongodTransaction(com.torodb.mongodb.core.WriteMongodTransaction) Timer(com.codahale.metrics.Timer) RequiredTransaction(com.torodb.mongodb.commands.TorodbCommandsLibrary.RequiredTransaction) ReadOnlyMongodTransaction(com.torodb.mongodb.core.ReadOnlyMongodTransaction) ExclusiveWriteMongodTransaction(com.torodb.mongodb.core.ExclusiveWriteMongodTransaction) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException)

Example 2 with ReadOnlyMongodTransaction

use of com.torodb.mongodb.core.ReadOnlyMongodTransaction in project torodb by torodb.

the class OplogManager method loadState.

@Locked(exclusive = true)
private void loadState() throws OplogManagerPersistException {
    try {
        retrier.retry(() -> {
            try (ReadOnlyMongodTransaction transaction = connection.openReadOnlyTransaction()) {
                Status<FindResult> status = transaction.execute(new Request(OPLOG_DB, null, true, null), FindCommand.INSTANCE, new FindArgument.Builder().setCollection(OPLOG_COL).setSlaveOk(true).build());
                if (!status.isOk()) {
                    throw new RetrierAbortException(new MongoException(status));
                }
                Iterator<BsonDocument> batch = status.getResult().getCursor().getFirstBatch();
                if (!batch.hasNext()) {
                    lastAppliedHash = 0;
                    lastAppliedOpTime = OpTime.EPOCH;
                } else {
                    BsonDocument doc = batch.next();
                    BsonDocument subDoc = BsonReaderTool.getDocument(doc, KEY);
                    lastAppliedHash = BsonReaderTool.getLong(subDoc, "hash");
                    long optimeAsLong = BsonReaderTool.getLong(subDoc, "optime_i");
                    BsonDateTime optimeAsDateTime = DefaultBsonValues.newDateTime(optimeAsLong);
                    lastAppliedOpTime = new OpTime(TimestampToDateTime.toTimestamp(optimeAsDateTime, DefaultBsonValues::newTimestamp), BsonReaderTool.getLong(subDoc, "optime_t"));
                }
                notifyLastAppliedOpTimeChange();
                return Empty.getInstance();
            }
        }, Hint.INFREQUENT_ROLLBACK);
    } catch (RetrierGiveUpException ex) {
        throw new OplogManagerPersistException(ex);
    }
}
Also used : MongoException(com.eightkdata.mongowp.exceptions.MongoException) ReadOnlyMongodTransaction(com.torodb.mongodb.core.ReadOnlyMongodTransaction) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder) Request(com.eightkdata.mongowp.server.api.Request) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) OpTime(com.eightkdata.mongowp.OpTime) DefaultBsonValues(com.eightkdata.mongowp.bson.utils.DefaultBsonValues) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) RetrierAbortException(com.torodb.core.retrier.RetrierAbortException) BsonDateTime(com.eightkdata.mongowp.bson.BsonDateTime) FindResult(com.torodb.mongodb.commands.signatures.general.FindCommand.FindResult) Locked(com.torodb.mongodb.annotations.Locked)

Example 3 with ReadOnlyMongodTransaction

use of com.torodb.mongodb.core.ReadOnlyMongodTransaction in project torodb by torodb.

the class BDDOplogTest method execute.

@Override
public void execute(OplogTestContext context) throws Exception {
    MongodServer server = context.getMongodServer();
    try (MongodConnection conn = server.openConnection()) {
        try (WriteMongodTransaction trans = conn.openWriteTransaction(true)) {
            given(trans);
            trans.commit();
        }
    }
    context.apply(streamOplog(), getApplierContext());
    try (MongodConnection conn = server.openConnection()) {
        try (ReadOnlyMongodTransaction trans = conn.openReadOnlyTransaction()) {
            then(trans);
        }
    }
}
Also used : WriteMongodTransaction(com.torodb.mongodb.core.WriteMongodTransaction) ReadOnlyMongodTransaction(com.torodb.mongodb.core.ReadOnlyMongodTransaction) MongodConnection(com.torodb.mongodb.core.MongodConnection) MongodServer(com.torodb.mongodb.core.MongodServer)

Aggregations

ReadOnlyMongodTransaction (com.torodb.mongodb.core.ReadOnlyMongodTransaction)3 RetrierGiveUpException (com.torodb.core.retrier.RetrierGiveUpException)2 WriteMongodTransaction (com.torodb.mongodb.core.WriteMongodTransaction)2 Timer (com.codahale.metrics.Timer)1 OpTime (com.eightkdata.mongowp.OpTime)1 Status (com.eightkdata.mongowp.Status)1 BsonDateTime (com.eightkdata.mongowp.bson.BsonDateTime)1 BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)1 DefaultBsonValues (com.eightkdata.mongowp.bson.utils.DefaultBsonValues)1 MongoException (com.eightkdata.mongowp.exceptions.MongoException)1 Request (com.eightkdata.mongowp.server.api.Request)1 BsonDocumentBuilder (com.eightkdata.mongowp.utils.BsonDocumentBuilder)1 RetrierAbortException (com.torodb.core.retrier.RetrierAbortException)1 Locked (com.torodb.mongodb.annotations.Locked)1 RequiredTransaction (com.torodb.mongodb.commands.TorodbCommandsLibrary.RequiredTransaction)1 FindResult (com.torodb.mongodb.commands.signatures.general.FindCommand.FindResult)1 ExclusiveWriteMongodTransaction (com.torodb.mongodb.core.ExclusiveWriteMongodTransaction)1 MongodConnection (com.torodb.mongodb.core.MongodConnection)1 MongodServer (com.torodb.mongodb.core.MongodServer)1