Search in sources :

Example 1 with DefaultBsonValues.newLong

use of com.eightkdata.mongowp.bson.utils.DefaultBsonValues.newLong 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);
    }
}
Also used : DeleteArgument(com.torodb.mongodb.commands.signatures.general.DeleteCommand.DeleteArgument) InsertResult(com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertResult) WriteMongodTransaction(com.torodb.mongodb.core.WriteMongodTransaction) MongoException(com.eightkdata.mongowp.exceptions.MongoException) Request(com.eightkdata.mongowp.server.api.Request) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) DeleteStatement(com.torodb.mongodb.commands.signatures.general.DeleteCommand.DeleteStatement) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder) RetrierAbortException(com.torodb.core.retrier.RetrierAbortException) InsertArgument(com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertArgument) DefaultBsonValues.newLong(com.eightkdata.mongowp.bson.utils.DefaultBsonValues.newLong) UserException(com.torodb.core.exceptions.user.UserException) Locked(com.torodb.mongodb.annotations.Locked)

Aggregations

DefaultBsonValues.newLong (com.eightkdata.mongowp.bson.utils.DefaultBsonValues.newLong)1 MongoException (com.eightkdata.mongowp.exceptions.MongoException)1 Request (com.eightkdata.mongowp.server.api.Request)1 BsonDocumentBuilder (com.eightkdata.mongowp.utils.BsonDocumentBuilder)1 UserException (com.torodb.core.exceptions.user.UserException)1 RetrierAbortException (com.torodb.core.retrier.RetrierAbortException)1 RetrierGiveUpException (com.torodb.core.retrier.RetrierGiveUpException)1 Locked (com.torodb.mongodb.annotations.Locked)1 DeleteArgument (com.torodb.mongodb.commands.signatures.general.DeleteCommand.DeleteArgument)1 DeleteStatement (com.torodb.mongodb.commands.signatures.general.DeleteCommand.DeleteStatement)1 InsertArgument (com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertArgument)1 InsertResult (com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertResult)1 WriteMongodTransaction (com.torodb.mongodb.core.WriteMongodTransaction)1