Search in sources :

Example 11 with UserException

use of com.torodb.core.exceptions.user.UserException in project torodb by torodb.

the class ReplSyncApplier method runProtected.

@Override
protected void runProtected() {
    runThread = Thread.currentThread();
    /*
     * TODO: In general, the replication context can be set as not reaplying. But it is not frequent
     * but possible to stop the replication after some oplog ops have been apply but not marked as
     * executed on the oplog manager. For that reason, all oplog ops betwen the last operation that
     * have been marked as applyed and the current last operation on the remote oplog must be
     * executed as replying operations. As it is not possible to do that yet, we have to always
     * apply operations as replying to be safe.
     */
    ApplierContext applierContext = new ApplierContext.Builder().setReapplying(true).setUpdatesAsUpserts(true).build();
    while (isRunning()) {
        OplogOperation lastOperation = null;
        ExclusiveWriteMongodTransaction trans = connection.openExclusiveWriteTransaction();
        try (ExclusiveWriteMongodTransaction transaction = trans) {
            try {
                for (OplogOperation opToApply : callback.takeOps()) {
                    lastOperation = opToApply;
                    LOGGER.trace("Executing {}", opToApply);
                    try {
                        boolean done = false;
                        while (!done) {
                            try {
                                oplogOpApplier.apply(opToApply, transaction, applierContext);
                                transaction.commit();
                                done = true;
                            } catch (RollbackException ex) {
                                LOGGER.debug("Recived a rollback exception while applying an oplog op", ex);
                            }
                        }
                    } catch (OplogApplyingException ex) {
                        if (!callback.failedToApply(opToApply, ex)) {
                            LOGGER.error(serviceName() + " stopped because one operation " + "cannot be executed", ex);
                            break;
                        }
                    } catch (UserException ex) {
                        if (callback.failedToApply(opToApply, ex)) {
                            LOGGER.error(serviceName() + " stopped because one operation " + "cannot be executed", ex);
                            break;
                        }
                    } catch (Throwable ex) {
                        if (callback.failedToApply(opToApply, ex)) {
                            LOGGER.error(serviceName() + " stopped because " + "an unknown error", ex);
                            break;
                        }
                    }
                    callback.markAsApplied(opToApply);
                }
            } catch (InterruptedException ex) {
                LOGGER.debug("Interrupted applier thread while applying an operator");
            }
        }
        if (lastOperation != null) {
            try (WriteOplogTransaction oplogTransaction = oplogManager.createWriteTransaction()) {
                oplogTransaction.addOperation(lastOperation);
            } catch (OplogManagerPersistException ex) {
                if (callback.failedToApply(lastOperation, ex)) {
                    LOGGER.error(serviceName() + " stopped because " + "the last applied operation couldn't " + "be persisted", ex);
                    break;
                }
            }
        }
    }
}
Also used : OplogApplyingException(com.torodb.mongodb.repl.oplogreplier.OplogOperationApplier.OplogApplyingException) OplogManagerPersistException(com.torodb.mongodb.repl.OplogManager.OplogManagerPersistException) ExclusiveWriteMongodTransaction(com.torodb.mongodb.core.ExclusiveWriteMongodTransaction) UserException(com.torodb.core.exceptions.user.UserException) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) RollbackException(com.torodb.core.transaction.RollbackException) WriteOplogTransaction(com.torodb.mongodb.repl.OplogManager.WriteOplogTransaction)

Example 12 with UserException

use of com.torodb.core.exceptions.user.UserException in project torodb by torodb.

the class SimpleAnalyzedOplogBatchExecutor method visit.

@Override
public OplogOperation visit(CudAnalyzedOplogBatch batch, ApplierContext arg) throws RetrierGiveUpException {
    metrics.getCudBatchSize().update(batch.getOriginalBatch().size());
    try (Context context = metrics.getCudBatchTimer().time()) {
        try {
            execute(batch, arg);
        } catch (UserException | NamespaceJobExecutionException ex) {
            throw new RetrierGiveUpException("Unexpected exception while replying", ex);
        } catch (RollbackException ex) {
            ApplierContext retryingReplingContext = new ApplierContext.Builder().setReapplying(true).setUpdatesAsUpserts(true).build();
            retrier.retry(() -> {
                try {
                    execute(batch, retryingReplingContext);
                    return Empty.getInstance();
                } catch (UserException | NamespaceJobExecutionException ex2) {
                    throw new RetrierAbortException("Unexpected user exception while applying " + "the batch " + batch, ex2);
                }
            }, Hint.CRITICAL, Hint.TIME_SENSIBLE);
        }
    }
    List<OplogOperation> originalBatch = batch.getOriginalBatch();
    return originalBatch.get(originalBatch.size() - 1);
}
Also used : ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Context(com.codahale.metrics.Timer.Context) RetrierAbortException(com.torodb.core.retrier.RetrierAbortException) UserException(com.torodb.core.exceptions.user.UserException) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) RollbackException(com.torodb.core.transaction.RollbackException) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext)

Example 13 with UserException

use of com.torodb.core.exceptions.user.UserException in project torodb by torodb.

the class AkkaDbCloner method insertDocuments.

private int insertDocuments(MongodServer localServer, String toDb, String collection, List<BsonDocument> docsToInsert) throws RollbackException {
    try (WriteMongodTransaction transaction = createWriteMongodTransaction(localServer)) {
        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()) {
            throw new CloningException("Error while inserting a cloned document");
        }
        int insertedDocs = insertResult.getResult().getN();
        if (insertedDocs != docsToInsert.size()) {
            throw new CloningException("Expected to insert " + docsToInsert.size() + " but " + insertResult + " were inserted");
        }
        transaction.commit();
        return insertedDocs;
    } catch (UserException ex) {
        throw new CloningException("Unexpected error while cloning documents", ex);
    }
}
Also used : InsertResult(com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertResult) WriteMongodTransaction(com.torodb.mongodb.core.WriteMongodTransaction) InsertArgument(com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertArgument) Request(com.eightkdata.mongowp.server.api.Request) UserException(com.torodb.core.exceptions.user.UserException) Hint(com.torodb.core.retrier.Retrier.Hint)

Example 14 with UserException

use of com.torodb.core.exceptions.user.UserException in project torodb by torodb.

the class SameThreadInsertPipeline method insert.

@Override
public void insert(Stream<KvDocument> docs) throws RollbackException, UserException {
    D2RTranslationBatchFunction d2rFun = new D2RTranslationBatchFunction(translatorFactory, metaDb, mutableMetaCollection);
    DefaultToBackendFunction r2BackendFun = new DefaultToBackendFunction(jobFactory, metaDb, mutableMetaCollection);
    try {
        Iterators.partition(docs.iterator(), docBatchSize).forEachRemaining(list -> {
            CollectionData collData = d2rFun.apply(list);
            Iterable<BackendTransactionJob> jobs = r2BackendFun.apply(collData);
            jobs.forEach(Unchecked.consumer(job -> job.execute(backendConnection)));
        });
    } catch (UncheckedException ex) {
        Throwable cause = ex.getCause();
        if (cause != null && cause instanceof UserException) {
            throw (UserException) cause;
        }
        throw ex;
    }
}
Also used : CollectionData(com.torodb.core.d2r.CollectionData) Unchecked(org.jooq.lambda.Unchecked) KvDocument(com.torodb.kvdocument.values.KvDocument) UserException(com.torodb.core.exceptions.user.UserException) BackendTransactionJobFactory(com.torodb.core.dsl.backend.BackendTransactionJobFactory) Iterators(com.google.common.collect.Iterators) BackendTransactionJob(com.torodb.core.dsl.backend.BackendTransactionJob) RollbackException(com.torodb.core.transaction.RollbackException) D2RTranslatorFactory(com.torodb.core.d2r.D2RTranslatorFactory) Assisted(com.google.inject.assistedinject.Assisted) Inject(javax.inject.Inject) MutableMetaCollection(com.torodb.core.transaction.metainf.MutableMetaCollection) UncheckedException(org.jooq.lambda.UncheckedException) MetaDatabase(com.torodb.core.transaction.metainf.MetaDatabase) Stream(java.util.stream.Stream) DefaultToBackendFunction(com.torodb.torod.pipeline.DefaultToBackendFunction) WriteBackendTransaction(com.torodb.core.backend.WriteBackendTransaction) Preconditions(com.google.common.base.Preconditions) CollectionData(com.torodb.core.d2r.CollectionData) D2RTranslationBatchFunction(com.torodb.torod.pipeline.D2RTranslationBatchFunction) InsertPipeline(com.torodb.torod.pipeline.InsertPipeline) UncheckedException(org.jooq.lambda.UncheckedException) D2RTranslationBatchFunction(com.torodb.torod.pipeline.D2RTranslationBatchFunction) BackendTransactionJob(com.torodb.core.dsl.backend.BackendTransactionJob) UserException(com.torodb.core.exceptions.user.UserException) DefaultToBackendFunction(com.torodb.torod.pipeline.DefaultToBackendFunction)

Example 15 with UserException

use of com.torodb.core.exceptions.user.UserException in project torodb by torodb.

the class DefaultConsistencyHandler method flushConsistentState.

private Object flushConsistentState(BackendConnection conn) throws RetrierAbortException {
    try (WriteBackendTransaction trans = conn.openSharedWriteTransaction()) {
        trans.writeMetaInfo(CONSISTENCY_KEY, KvBoolean.from(consistent));
        trans.commit();
    } catch (UserException ex) {
        throw new RetrierAbortException(ex);
    }
    return null;
}
Also used : RetrierAbortException(com.torodb.core.retrier.RetrierAbortException) UserException(com.torodb.core.exceptions.user.UserException) WriteBackendTransaction(com.torodb.core.backend.WriteBackendTransaction)

Aggregations

UserException (com.torodb.core.exceptions.user.UserException)15 AttributeReference (com.torodb.core.language.AttributeReference)5 RollbackException (com.torodb.core.transaction.RollbackException)5 MongoException (com.eightkdata.mongowp.exceptions.MongoException)4 Request (com.eightkdata.mongowp.server.api.Request)4 OplogOperation (com.eightkdata.mongowp.server.api.oplog.OplogOperation)4 ObjectKey (com.torodb.core.language.AttributeReference.ObjectKey)4 RetrierAbortException (com.torodb.core.retrier.RetrierAbortException)4 WriteMongodTransaction (com.torodb.mongodb.core.WriteMongodTransaction)4 IndexFieldInfo (com.torodb.torod.IndexFieldInfo)4 OplogManagerPersistException (com.torodb.mongodb.repl.OplogManager.OplogManagerPersistException)3 WriteOplogTransaction (com.torodb.mongodb.repl.OplogManager.WriteOplogTransaction)3 OpTime (com.eightkdata.mongowp.OpTime)2 Status (com.eightkdata.mongowp.Status)2 MongoClient (com.eightkdata.mongowp.client.core.MongoClient)2 MongoConnection (com.eightkdata.mongowp.client.core.MongoConnection)2 UnreachableMongoServerException (com.eightkdata.mongowp.client.core.UnreachableMongoServerException)2 CommandFailed (com.eightkdata.mongowp.exceptions.CommandFailed)2 OplogOperationUnsupported (com.eightkdata.mongowp.exceptions.OplogOperationUnsupported)2 OplogStartMissingException (com.eightkdata.mongowp.exceptions.OplogStartMissingException)2