Search in sources :

Example 1 with RequiredTransaction

use of com.torodb.mongodb.commands.TorodbCommandsLibrary.RequiredTransaction 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)

Aggregations

Timer (com.codahale.metrics.Timer)1 Status (com.eightkdata.mongowp.Status)1 RetrierGiveUpException (com.torodb.core.retrier.RetrierGiveUpException)1 RequiredTransaction (com.torodb.mongodb.commands.TorodbCommandsLibrary.RequiredTransaction)1 ExclusiveWriteMongodTransaction (com.torodb.mongodb.core.ExclusiveWriteMongodTransaction)1 ReadOnlyMongodTransaction (com.torodb.mongodb.core.ReadOnlyMongodTransaction)1 WriteMongodTransaction (com.torodb.mongodb.core.WriteMongodTransaction)1