Search in sources :

Example 1 with BsonDocument

use of com.eightkdata.mongowp.bson.BsonDocument in project torodb by torodb.

the class AbstractMongoOplogReader method queryGte.

@Override
public MongoCursor<OplogOperation> queryGte(OpTime lastFetchedOpTime) throws MongoException {
    BsonDocument query = DefaultBsonValues.newDocument("ts", DefaultBsonValues.newDocument("$gte", lastFetchedOpTime.getTimestamp()));
    EnumSet<QueryOption> flags = EnumSet.of(QueryOption.AWAIT_DATA, QueryOption.TAILABLE_CURSOR);
    return query(query, flags, NATURAL_ORDER_SORT);
}
Also used : BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) QueryOption(com.eightkdata.mongowp.messages.request.QueryMessage.QueryOption)

Example 2 with BsonDocument

use of com.eightkdata.mongowp.bson.BsonDocument in project torodb by torodb.

the class AbstractMongoOplogReader method getFirstOrLastOp.

private OplogOperation getFirstOrLastOp(boolean first) throws OplogStartMissingException, OplogOperationUnsupported, MongoException {
    Preconditions.checkState(!isClosed(), "You have to connect this client before");
    BsonDocument query = DefaultBsonValues.EMPTY_DOC;
    BsonDocument orderBy = first ? NATURAL_ORDER_SORT : INVERSE_ORDER_SORT;
    EnumSet<QueryOption> flags = EnumSet.of(QueryOption.SLAVE_OK);
    BsonDocument doc;
    MongoConnection connection = consumeConnection();
    try {
        MongoCursor<BsonDocument> cursor = connection.query(DATABASE, COLLECTION, query, 0, 1, new QueryOptions(flags), orderBy, null);
        try {
            Batch<BsonDocument> batch = cursor.fetchBatch();
            try {
                if (!batch.hasNext()) {
                    throw new OplogStartMissingException(getSyncSource());
                }
                doc = batch.next();
            } finally {
                batch.close();
            }
        } finally {
            cursor.close();
        }
        try {
            return OplogOperationParser.fromBson(doc);
        } catch (BadValueException | TypesMismatchException | NoSuchKeyException ex) {
            throw new OplogOperationUnsupported(doc, ex);
        }
    } finally {
        releaseConnection(connection);
    }
}
Also used : BadValueException(com.eightkdata.mongowp.exceptions.BadValueException) OplogOperationUnsupported(com.eightkdata.mongowp.exceptions.OplogOperationUnsupported) QueryOption(com.eightkdata.mongowp.messages.request.QueryMessage.QueryOption) OplogStartMissingException(com.eightkdata.mongowp.exceptions.OplogStartMissingException) QueryOptions(com.eightkdata.mongowp.messages.request.QueryMessage.QueryOptions) NoSuchKeyException(com.eightkdata.mongowp.exceptions.NoSuchKeyException) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) TypesMismatchException(com.eightkdata.mongowp.exceptions.TypesMismatchException) MongoConnection(com.eightkdata.mongowp.client.core.MongoConnection)

Example 3 with BsonDocument

use of com.eightkdata.mongowp.bson.BsonDocument in project torodb by torodb.

the class TransactionalDbCloner method cloneCollection.

private void cloneCollection(String toDb, MongoConnection remoteConnection, WriteMongodTransaction transaction, CloneOptions opts, String collection, CollectionOptions collOptions) throws MongoException, CloningException {
    String fromDb = opts.getDbToClone();
    LOGGER.info("Cloning {}.{} into {}.{}", fromDb, collection, toDb, collection);
    //TODO: enable exhaust?
    EnumSet<QueryOption> queryFlags = EnumSet.of(QueryOption.NO_CURSOR_TIMEOUT);
    if (opts.isSlaveOk()) {
        queryFlags.add(QueryOption.SLAVE_OK);
    }
    MongoCursor<BsonDocument> cursor = remoteConnection.query(opts.getDbToClone(), collection, null, 0, 0, new QueryOptions(queryFlags), null, null);
    while (!cursor.hasNext()) {
        List<? extends BsonDocument> docsToInsert = cursor.fetchBatch().asList();
        Status<InsertResult> insertResult = transaction.execute(new Request(toDb, null, true, null), InsertCommand.INSTANCE, new InsertArgument.Builder(collection).addDocuments(docsToInsert).setWriteConcern(WriteConcern.fsync()).setOrdered(true).build());
        if (!insertResult.isOk() || insertResult.getResult().getN() != docsToInsert.size()) {
            throw new CloningException("Error while inserting a cloned document");
        }
    }
}
Also used : InsertResult(com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertResult) Request(com.eightkdata.mongowp.server.api.Request) QueryOption(com.eightkdata.mongowp.messages.request.QueryMessage.QueryOption) QueryOptions(com.eightkdata.mongowp.messages.request.QueryMessage.QueryOptions) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) InsertArgument(com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertArgument)

Example 4 with BsonDocument

use of com.eightkdata.mongowp.bson.BsonDocument in project torodb by torodb.

the class OplogOperationApplier method insertDocument.

private void insertDocument(InsertOplogOperation op, ExclusiveWriteMongodTransaction trans) throws MongoException {
    BsonDocument docToInsert = op.getDocToInsert();
    //TODO: Inserts must be executed as upserts to be idempotent
    //TODO: This implementation works iff this connection is the only one that is writing
    BsonDocument query;
    if (!DefaultIdUtils.containsDefaultId(docToInsert)) {
        //as we dont have _id, we need the whole document to be sure selector is correct
        query = docToInsert;
    } else {
        query = newDocument(DefaultIdUtils.DEFAULT_ID_KEY, DefaultIdUtils.getDefaultId(docToInsert));
    }
    while (true) {
        executeTorodCommand(op.getDatabase(), DeleteCommand.INSTANCE, new DeleteCommand.DeleteArgument(op.getCollection(), Collections.singletonList(new DeleteCommand.DeleteStatement(query, true)), true, null), trans);
        executeTorodCommand(op.getDatabase(), InsertCommand.INSTANCE, new InsertCommand.InsertArgument(op.getCollection(), Collections.singletonList(docToInsert), WriteConcern.fsync(), true, null), trans);
        break;
    }
}
Also used : DeleteCommand(com.torodb.mongodb.commands.signatures.general.DeleteCommand) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) DeleteArgument(com.torodb.mongodb.commands.signatures.general.DeleteCommand.DeleteArgument) InsertCommand(com.torodb.mongodb.commands.signatures.general.InsertCommand) DeleteStatement(com.torodb.mongodb.commands.signatures.general.DeleteCommand.DeleteStatement)

Example 5 with BsonDocument

use of com.eightkdata.mongowp.bson.BsonDocument 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)

Aggregations

BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)22 NoSuchKeyException (com.eightkdata.mongowp.exceptions.NoSuchKeyException)6 TypesMismatchException (com.eightkdata.mongowp.exceptions.TypesMismatchException)6 BsonDocumentBuilder (com.eightkdata.mongowp.utils.BsonDocumentBuilder)6 BadValueException (com.eightkdata.mongowp.exceptions.BadValueException)5 BsonValue (com.eightkdata.mongowp.bson.BsonValue)4 Request (com.eightkdata.mongowp.server.api.Request)4 Status (com.eightkdata.mongowp.Status)3 CommandFailed (com.eightkdata.mongowp.exceptions.CommandFailed)3 MongoException (com.eightkdata.mongowp.exceptions.MongoException)3 QueryOption (com.eightkdata.mongowp.messages.request.QueryMessage.QueryOption)3 QueryOptions (com.eightkdata.mongowp.messages.request.QueryMessage.QueryOptions)3 Command (com.eightkdata.mongowp.server.api.Command)3 KvDocument (com.torodb.kvdocument.values.KvDocument)3 KvValue (com.torodb.kvdocument.values.KvValue)3 ErrorCode (com.eightkdata.mongowp.ErrorCode)2 OpTime (com.eightkdata.mongowp.OpTime)2 BsonArray (com.eightkdata.mongowp.bson.BsonArray)2 DefaultBsonValues (com.eightkdata.mongowp.bson.utils.DefaultBsonValues)2 MongoConnection (com.eightkdata.mongowp.client.core.MongoConnection)2