Search in sources :

Example 1 with Request

use of com.eightkdata.mongowp.server.api.Request in project torodb by torodb.

the class AkkaDbCloner method cloneIndex.

private void cloneIndex(MongodServer localServer, String fromDb, String dstDb, MongoConnection remoteConnection, CloneOptions opts, String fromCol, String toCol) throws CloningException {
    WriteMongodTransaction transaction = createWriteMongodTransaction(localServer);
    try {
        try {
            List<IndexOptions> indexesToClone = getIndexesToClone(Lists.newArrayList(ListIndexesRequester.getListCollections(remoteConnection, dstDb, fromCol).getFirstBatch()), dstDb, toCol, fromDb, fromCol, opts);
            if (indexesToClone.isEmpty()) {
                return;
            }
            Status<CreateIndexesResult> status = transaction.execute(new Request(dstDb, null, true, null), CreateIndexesCommand.INSTANCE, new CreateIndexesArgument(fromCol, indexesToClone));
            if (!status.isOk()) {
                throw new CloningException("Error while cloning indexes: " + status.getErrorMsg());
            }
            transaction.commit();
        } catch (UserException | MongoException ex) {
            throw new CloningException("Unexpected error while cloning indexes", ex);
        }
    } finally {
        transaction.close();
    }
}
Also used : CreateIndexesResult(com.torodb.mongodb.commands.signatures.admin.CreateIndexesCommand.CreateIndexesResult) WriteMongodTransaction(com.torodb.mongodb.core.WriteMongodTransaction) MongoException(com.eightkdata.mongowp.exceptions.MongoException) CreateIndexesArgument(com.torodb.mongodb.commands.signatures.admin.CreateIndexesCommand.CreateIndexesArgument) IndexOptions(com.torodb.mongodb.commands.pojos.index.IndexOptions) Request(com.eightkdata.mongowp.server.api.Request) UserException(com.torodb.core.exceptions.user.UserException)

Example 2 with Request

use of com.eightkdata.mongowp.server.api.Request in project torodb by torodb.

the class TransactionalDbCloner method cloneIndex.

private void cloneIndex(String dstDb, MongoConnection remoteConnection, WriteMongodTransaction transaction, CloneOptions opts, String fromCol, CollectionOptions collOptions) throws CloningException {
    try {
        String fromDb = opts.getDbToClone();
        HostAndPort remoteAddress = remoteConnection.getClientOwner().getAddress();
        String remoteAddressString = remoteAddress != null ? remoteAddress.toString() : "local";
        LOGGER.info("copying indexes from {}.{} on {} to {}.{} on local server", fromDb, fromCol, remoteAddressString, dstDb, fromCol);
        Status<?> status;
        List<IndexOptions> indexes = Lists.newArrayList(ListIndexesRequester.getListCollections(remoteConnection, dstDb, fromCol).getFirstBatch());
        if (indexes.isEmpty()) {
            return;
        }
        status = transaction.execute(new Request(dstDb, null, true, null), CreateIndexesCommand.INSTANCE, new CreateIndexesArgument(fromCol, indexes));
        if (!status.isOk()) {
            throw new CloningException("Error while trying to fetch indexes from remote: " + status);
        }
    } catch (MongoException ex) {
        throw new CloningException("Error while trying to fetch indexes from remote", ex);
    }
}
Also used : HostAndPort(com.google.common.net.HostAndPort) MongoException(com.eightkdata.mongowp.exceptions.MongoException) CreateIndexesArgument(com.torodb.mongodb.commands.signatures.admin.CreateIndexesCommand.CreateIndexesArgument) IndexOptions(com.torodb.mongodb.commands.pojos.index.IndexOptions) Request(com.eightkdata.mongowp.server.api.Request)

Example 3 with Request

use of com.eightkdata.mongowp.server.api.Request 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 Request

use of com.eightkdata.mongowp.server.api.Request in project torodb by torodb.

the class OplogOperationApplier method executeReplCommand.

private <A, R> Status<R> executeReplCommand(String db, Command<? super A, ? super R> command, A arg, ExclusiveWriteTorodTransaction trans) {
    Request req = new Request(db, null, true, null);
    Status<R> result = executor.execute(req, command, arg, trans);
    return result;
}
Also used : Request(com.eightkdata.mongowp.server.api.Request)

Example 5 with Request

use of com.eightkdata.mongowp.server.api.Request 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

Request (com.eightkdata.mongowp.server.api.Request)12 WriteMongodTransaction (com.torodb.mongodb.core.WriteMongodTransaction)5 Status (com.eightkdata.mongowp.Status)4 BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)4 MongoException (com.eightkdata.mongowp.exceptions.MongoException)4 Command (com.eightkdata.mongowp.server.api.Command)4 UserException (com.torodb.core.exceptions.user.UserException)4 AttributeReference (com.torodb.core.language.AttributeReference)4 IndexOptions (com.torodb.mongodb.commands.pojos.index.IndexOptions)4 List (java.util.List)4 ErrorCode (com.eightkdata.mongowp.ErrorCode)3 InsertArgument (com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertArgument)3 InsertResult (com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertResult)3 Constants (com.torodb.mongodb.language.Constants)3 IndexFieldInfo (com.torodb.torod.IndexFieldInfo)3 Arrays (java.util.Arrays)3 CommandFailed (com.eightkdata.mongowp.exceptions.CommandFailed)2 BsonDocumentBuilder (com.eightkdata.mongowp.utils.BsonDocumentBuilder)2 Cursor (com.torodb.core.cursors.Cursor)2 Builder (com.torodb.core.language.AttributeReference.Builder)2