Search in sources :

Example 1 with BsonDocumentBuilder

use of com.eightkdata.mongowp.utils.BsonDocumentBuilder 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)

Example 2 with BsonDocumentBuilder

use of com.eightkdata.mongowp.utils.BsonDocumentBuilder in project torodb by torodb.

the class OplogTestParser method getOps.

private static List<OplogOperation> getOps(BsonDocument doc) {
    BsonValue<?> oplogValue = doc.get("oplog");
    if (oplogValue == null) {
        throw new AssertionError("Does not contain oplog");
    }
    AtomicInteger tsFactory = new AtomicInteger();
    AtomicInteger tFactory = new AtomicInteger();
    BsonInt32 twoInt32 = DefaultBsonValues.newInt(2);
    return oplogValue.asArray().asList().stream().map(BsonValue::asDocument).map(child -> {
        BsonDocumentBuilder builder = new BsonDocumentBuilder(child);
        if (child.get("ts") == null) {
            builder.appendUnsafe("ts", DefaultBsonValues.newTimestamp(tsFactory.incrementAndGet(), tFactory.incrementAndGet()));
        }
        if (child.get("h") == null) {
            builder.appendUnsafe("h", DefaultBsonValues.INT32_ONE);
        }
        if (child.get("v") == null) {
            builder.appendUnsafe("v", twoInt32);
        }
        return builder.build();
    }).map(child -> {
        try {
            return OplogOperationParser.fromBson(child);
        } catch (MongoException ex) {
            throw new AssertionError("Invalid oplog operation", ex);
        }
    }).collect(Collectors.toList());
}
Also used : java.util(java.util) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) Truth(com.google.common.truth.Truth) KvValue(com.torodb.kvdocument.values.KvValue) BsonValue(com.eightkdata.mongowp.bson.BsonValue) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) MongoWpConverter(com.torodb.kvdocument.conversion.mongowp.MongoWpConverter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TorodTransaction(com.torodb.torod.TorodTransaction) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder) MongoException(com.eightkdata.mongowp.exceptions.MongoException) DefaultBsonValues(com.eightkdata.mongowp.bson.utils.DefaultBsonValues) MongoBsonTranslator(com.eightkdata.mongowp.bson.org.bson.utils.MongoBsonTranslator) OplogOperationParser(com.torodb.mongodb.commands.pojos.OplogOperationParser) Charsets(com.google.common.base.Charsets) Files(java.nio.file.Files) BsonInt32(com.eightkdata.mongowp.bson.BsonInt32) KvDocument(com.torodb.kvdocument.values.KvDocument) Collectors(java.util.stream.Collectors) WriteMongodTransaction(com.torodb.mongodb.core.WriteMongodTransaction) Stream(java.util.stream.Stream) java.io(java.io) Paths(java.nio.file.Paths) ReadOnlyMongodTransaction(com.torodb.mongodb.core.ReadOnlyMongodTransaction) Assert(org.junit.Assert) BsonInt32(com.eightkdata.mongowp.bson.BsonInt32) MongoException(com.eightkdata.mongowp.exceptions.MongoException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder)

Example 3 with BsonDocumentBuilder

use of com.eightkdata.mongowp.utils.BsonDocumentBuilder in project torodb by torodb.

the class CollectionOptions method marshall.

public BsonDocument marshall() {
    BsonDocumentBuilder builder = new BsonDocumentBuilder();
    marshall(builder);
    return builder.build();
}
Also used : BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder)

Example 4 with BsonDocumentBuilder

use of com.eightkdata.mongowp.utils.BsonDocumentBuilder in project torodb by torodb.

the class HeartbeatInfo method marshall.

public BsonDocument marshall() {
    BsonDocumentBuilder builder = new BsonDocumentBuilder();
    builder.append(SET_NAME_FIELD_NAME, setName);
    builder.append(PROTOCOL_VERSION_FIELD_NAME, protocolVersion);
    builder.append(CONFIG_VERSION_FIELD_NAME, configVersion);
    if (senderHost != null) {
        builder.append(SENDER_HOST_FIELD_NAME, senderHost.toString());
    } else {
        builder.append(SENDER_HOST_FIELD_NAME, "");
    }
    if (senderId != null) {
        builder.append(SENDER_ID_FIELD_NAME, senderId);
    }
    if (checkEmpty != null) {
        builder.append(CHECK_EMPTY_FIELD_NAME, checkEmpty);
    }
    return builder.build();
}
Also used : BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder)

Example 5 with BsonDocumentBuilder

use of com.eightkdata.mongowp.utils.BsonDocumentBuilder in project torodb by torodb.

the class WriteConcernError method marshall.

public BsonValue<?> marshall() {
    BsonDocumentBuilder bsonWriteConcernError = new BsonDocumentBuilder();
    bsonWriteConcernError.append(CODE_FIELD, code);
    bsonWriteConcernError.append(ERR_MSG_FIELD, errMsg);
    return bsonWriteConcernError.build();
}
Also used : BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder)

Aggregations

BsonDocumentBuilder (com.eightkdata.mongowp.utils.BsonDocumentBuilder)13 BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)3 MongoException (com.eightkdata.mongowp.exceptions.MongoException)2 BsonArrayBuilder (com.eightkdata.mongowp.utils.BsonArrayBuilder)2 WriteMongodTransaction (com.torodb.mongodb.core.WriteMongodTransaction)2 Map (java.util.Map)2 BsonInt32 (com.eightkdata.mongowp.bson.BsonInt32)1 BsonValue (com.eightkdata.mongowp.bson.BsonValue)1 MongoBsonTranslator (com.eightkdata.mongowp.bson.org.bson.utils.MongoBsonTranslator)1 DefaultBsonValues (com.eightkdata.mongowp.bson.utils.DefaultBsonValues)1 DefaultBsonValues.newLong (com.eightkdata.mongowp.bson.utils.DefaultBsonValues.newLong)1 BadValueException (com.eightkdata.mongowp.exceptions.BadValueException)1 NoSuchKeyException (com.eightkdata.mongowp.exceptions.NoSuchKeyException)1 Request (com.eightkdata.mongowp.server.api.Request)1 OplogOperation (com.eightkdata.mongowp.server.api.oplog.OplogOperation)1 Charsets (com.google.common.base.Charsets)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Truth (com.google.common.truth.Truth)1 UserException (com.torodb.core.exceptions.user.UserException)1 RetrierAbortException (com.torodb.core.retrier.RetrierAbortException)1