Search in sources :

Example 16 with MongoException

use of com.eightkdata.mongowp.exceptions.MongoException in project torodb by torodb.

the class AkkaDbCloner method cloneDatabase.

@Override
public void cloneDatabase(String dstDb, MongoClient remoteClient, MongodServer localServer, CloneOptions opts) throws CloningException, NotMasterException, MongoException {
    Preconditions.checkState(isRunning(), "This db cloner is not running");
    if (!remoteClient.isRemote() && opts.getDbToClone().equals(dstDb)) {
        LOGGER.warn("Trying to clone a database to itself! Ignoring it");
        return;
    }
    String fromDb = opts.getDbToClone();
    CursorResult<Entry> listCollections;
    try (MongoConnection remoteConnection = remoteClient.openConnection()) {
        listCollections = ListCollectionsRequester.getListCollections(remoteConnection, fromDb, null);
    } catch (MongoException ex) {
        throw new CloningException("It was impossible to get information from the remote server", ex);
    }
    if (!opts.getWritePermissionSupplier().get()) {
        throw new NotMasterException("Destiny database cannot be written");
    }
    List<Entry> collsToClone = getCollsToClone(listCollections, fromDb, opts);
    if (!opts.getWritePermissionSupplier().get()) {
        throw new NotMasterException("Destiny database cannot be written " + "after get collections info");
    }
    try {
        for (Entry entry : collsToClone) {
            prepareCollection(localServer, dstDb, entry);
        }
    } catch (RollbackException ex) {
        throw new AssertionError("Unexpected rollback exception", ex);
    }
    Materializer materializer = ActorMaterializer.create(getActorSystem());
    try (MongoConnection remoteConnection = remoteClient.openConnection()) {
        if (opts.isCloneData()) {
            for (Entry entry : collsToClone) {
                LOGGER.info("Cloning collection data {}.{} into {}.{}", fromDb, entry.getCollectionName(), dstDb, entry.getCollectionName());
                try {
                    cloneCollection(localServer, remoteConnection, dstDb, opts, materializer, entry);
                } catch (CompletionException completionException) {
                    Throwable cause = completionException.getCause();
                    if (cause instanceof RollbackException) {
                        throw (RollbackException) cause;
                    }
                    throw completionException;
                }
            }
        }
        if (opts.isCloneIndexes()) {
            for (Entry entry : collsToClone) {
                LOGGER.info("Cloning collection indexes {}.{} into {}.{}", fromDb, entry.getCollectionName(), dstDb, entry.getCollectionName());
                try {
                    cloneIndex(localServer, dstDb, dstDb, remoteConnection, opts, entry.getCollectionName(), entry.getCollectionName());
                } catch (CompletionException completionException) {
                    Throwable cause = completionException.getCause();
                    if (cause instanceof RollbackException) {
                        throw (RollbackException) cause;
                    }
                    throw completionException;
                }
            }
        }
    }
}
Also used : MongoException(com.eightkdata.mongowp.exceptions.MongoException) RollbackException(com.torodb.core.transaction.RollbackException) Entry(com.torodb.mongodb.commands.signatures.admin.ListCollectionsCommand.ListCollectionsResult.Entry) CompletionException(java.util.concurrent.CompletionException) NotMasterException(com.eightkdata.mongowp.exceptions.NotMasterException) MongoConnection(com.eightkdata.mongowp.client.core.MongoConnection) ActorMaterializer(akka.stream.ActorMaterializer) Materializer(akka.stream.Materializer)

Example 17 with MongoException

use of com.eightkdata.mongowp.exceptions.MongoException in project torodb by torodb.

the class TorodbSafeRequestProcessor method query.

@Override
public ReplyMessage query(MongodConnection connection, Request req, int requestId, QueryRequest queryRequest) throws MongoException {
    FindArgument findArg = new FindArgument.Builder().setCollection(queryRequest.getCollection()).setFilter(queryRequest.getQuery() != null ? queryRequest.getQuery() : DefaultBsonValues.EMPTY_DOC).build();
    Status<FindResult> status = execute(req, FindCommand.INSTANCE, findArg, connection);
    if (!status.isOk()) {
        throw new MongoException(status.getErrorCode(), status.getErrorMsg());
    }
    FindResult result = status.getResult();
    assert result != null;
    return new ReplyMessage(EmptyBsonContext.getInstance(), requestId, false, false, false, false, result.getCursor().getCursorId(), queryRequest.getNumberToSkip(), IterableDocumentProvider.of(Lists.newArrayList(result.getCursor().getFirstBatch())));
}
Also used : FindArgument(com.torodb.mongodb.commands.signatures.general.FindCommand.FindArgument) MongoException(com.eightkdata.mongowp.exceptions.MongoException) ReplyMessage(com.eightkdata.mongowp.messages.response.ReplyMessage) FindResult(com.torodb.mongodb.commands.signatures.general.FindCommand.FindResult)

Example 18 with MongoException

use of com.eightkdata.mongowp.exceptions.MongoException in project torodb by torodb.

the class ReplSetHeartbeatReplyMarshaller method checkCommandError.

private static void checkCommandError(BsonDocument bson, String setName) throws MongoException {
    if (setName == null && !BsonReaderTool.isPseudoTrue(bson, OK_FIELD)) {
        String errMsg = BsonReaderTool.getString(bson, ERR_MSG_FIELD, "");
        assert errMsg != null;
        Entry<?> errorCodeEntry = BsonReaderTool.getEntry(bson, ERROR_CODE_FIELD, null);
        if (errorCodeEntry != null) {
            if (!errorCodeEntry.getValue().isNumber()) {
                throw new BadValueException(ERROR_CODE_FIELD + " is " + "not a number");
            }
            ErrorCode errorCode = ErrorCode.fromErrorCode(errorCodeEntry.getValue().asNumber().intValue());
            throw new MongoException(errMsg, errorCode);
        }
        throw new UnknownErrorException(errMsg);
    }
}
Also used : BadValueException(com.eightkdata.mongowp.exceptions.BadValueException) MongoException(com.eightkdata.mongowp.exceptions.MongoException) UnknownErrorException(com.eightkdata.mongowp.exceptions.UnknownErrorException) ErrorCode(com.eightkdata.mongowp.ErrorCode)

Aggregations

MongoException (com.eightkdata.mongowp.exceptions.MongoException)18 OplogOperation (com.eightkdata.mongowp.server.api.oplog.OplogOperation)6 Request (com.eightkdata.mongowp.server.api.Request)4 UserException (com.torodb.core.exceptions.user.UserException)4 RetrierAbortException (com.torodb.core.retrier.RetrierAbortException)4 RetrierGiveUpException (com.torodb.core.retrier.RetrierGiveUpException)4 RollbackException (com.torodb.core.transaction.RollbackException)4 OpTime (com.eightkdata.mongowp.OpTime)3 Status (com.eightkdata.mongowp.Status)3 BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)3 MongoConnection (com.eightkdata.mongowp.client.core.MongoConnection)3 UnreachableMongoServerException (com.eightkdata.mongowp.client.core.UnreachableMongoServerException)3 BsonDocumentBuilder (com.eightkdata.mongowp.utils.BsonDocumentBuilder)3 HostAndPort (com.google.common.net.HostAndPort)3 WriteMongodTransaction (com.torodb.mongodb.core.WriteMongodTransaction)3 ErrorCode (com.eightkdata.mongowp.ErrorCode)2 DefaultBsonValues (com.eightkdata.mongowp.bson.utils.DefaultBsonValues)2 MongoClient (com.eightkdata.mongowp.client.core.MongoClient)2 NotMasterException (com.eightkdata.mongowp.exceptions.NotMasterException)2 OplogOperationUnsupported (com.eightkdata.mongowp.exceptions.OplogOperationUnsupported)2