Search in sources :

Example 1 with RollbackReplicationException

use of com.torodb.mongodb.repl.oplogreplier.RollbackReplicationException in project torodb by torodb.

the class ContinuousOplogFetcher method fetch.

@Override
public OplogBatch fetch() throws StopReplicationException, RollbackReplicationException {
    if (state.isClosed()) {
        return FinishedOplogBatch.getInstance();
    }
    try {
        return retrier.retry(() -> {
            try {
                if (state.isClosed()) {
                    return FinishedOplogBatch.getInstance();
                }
                state.prepareToFetch();
                MongoCursor<OplogOperation> cursor = state.getLastUsedMongoCursor();
                Batch<OplogOperation> batch = cursor.tryFetchBatch();
                if (batch == null || !batch.hasNext()) {
                    Thread.sleep(1000);
                    batch = cursor.tryFetchBatch();
                    if (batch == null || !batch.hasNext()) {
                        return NotReadyForMoreOplogBatch.getInstance();
                    }
                }
                List<OplogOperation> fetchedOps = null;
                long fetchTime = 0;
                /*
           * As we already modify the cursor by fetching the batch, we cannot retry the whole block
           * (as the cursor would be reused and the previous batch will be discarted).
           *
           * Then, if we leave the following try section with an error, we need to discard the
           * cursor, so the next iteration starts from the last batch we returned. On the other
           * hand, if we finished successfully, then we need to update the state.
           */
                boolean successful = false;
                try {
                    fetchedOps = batch.asList();
                    fetchTime = batch.getFetchTime();
                    postBatchChecks(cursor, fetchedOps);
                    OplogBatch result = new NormalOplogBatch(fetchedOps, true);
                    successful = true;
                    return result;
                } finally {
                    if (!successful) {
                        cursor.close();
                    } else {
                        assert fetchedOps != null;
                        assert fetchTime != 0;
                        state.updateState(fetchedOps, fetchTime);
                    }
                }
            } catch (RestartFetchException ex) {
                //lets choose a new reader
                state.discardReader();
                //and then try again
                throw new RollbackException(ex);
            } catch (DeadCursorException ex) {
                //lets retry the whole block with the same reader
                throw new RollbackException(ex);
            } catch (StopReplicationException | RollbackReplicationException ex) {
                //do not try again
                throw new RetrierAbortException(ex);
            } catch (MongoServerException ex) {
                //TODO: Fix this violation on the abstraction!
                LOGGER.debug("Found an unwrapped MongodbServerException");
                //lets choose a new reader
                state.discardReader();
                //rollback and hopefully use another member
                throw new RollbackException(ex);
            } catch (MongoException | MongoRuntimeException ex) {
                LOGGER.warn("Catched an error while reading the remote " + "oplog: {}", ex.getLocalizedMessage());
                //lets choose a new reader
                state.discardReader();
                //rollback and hopefully use another member
                throw new RollbackException(ex);
            }
        }, Hint.CRITICAL, Hint.TIME_SENSIBLE);
    } catch (RetrierGiveUpException ex) {
        this.close();
        throw new StopReplicationException("Stopping replication after several attepts to " + "fetch the remote oplog", ex);
    } catch (RetrierAbortException ex) {
        this.close();
        Throwable cause = ex.getCause();
        if (cause != null) {
            if (cause instanceof StopReplicationException) {
                throw (StopReplicationException) cause;
            }
            if (cause instanceof RollbackReplicationException) {
                throw (RollbackReplicationException) cause;
            }
        }
        throw new StopReplicationException("Stopping replication after a unknown abort " + "exception", ex);
    }
}
Also used : DeadCursorException(com.eightkdata.mongowp.server.api.pojos.MongoCursor.DeadCursorException) MongoException(com.eightkdata.mongowp.exceptions.MongoException) MongoRuntimeException(com.eightkdata.mongowp.server.api.MongoRuntimeException) RollbackReplicationException(com.torodb.mongodb.repl.oplogreplier.RollbackReplicationException) RollbackException(com.torodb.core.transaction.RollbackException) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) NormalOplogBatch(com.torodb.mongodb.repl.oplogreplier.NormalOplogBatch) RetrierAbortException(com.torodb.core.retrier.RetrierAbortException) FinishedOplogBatch(com.torodb.mongodb.repl.oplogreplier.FinishedOplogBatch) OplogBatch(com.torodb.mongodb.repl.oplogreplier.OplogBatch) NotReadyForMoreOplogBatch(com.torodb.mongodb.repl.oplogreplier.NotReadyForMoreOplogBatch) NormalOplogBatch(com.torodb.mongodb.repl.oplogreplier.NormalOplogBatch) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) MongoServerException(com.mongodb.MongoServerException) StopReplicationException(com.torodb.mongodb.repl.oplogreplier.StopReplicationException)

Example 2 with RollbackReplicationException

use of com.torodb.mongodb.repl.oplogreplier.RollbackReplicationException in project torodb by torodb.

the class RecoveryService method applyOplog.

/**
   * Applies all the oplog operations stored on the remote server whose optime is higher than
   * <em>from</em> but lower or equal than <em>to</em>.
   *
   * @param myOplog
   * @param remoteOplog
   * @param to
   * @param from
   */
private void applyOplog(OplogReader remoteOplog, OpTime from, OpTime to) throws TryAgainException, MongoException, FatalErrorException {
    MongoCursor<OplogOperation> oplogCursor = remoteOplog.between(from, true, to, true);
    if (!oplogCursor.hasNext()) {
        throw new OplogStartMissingException(remoteOplog.getSyncSource());
    }
    OplogOperation firstOp = oplogCursor.next();
    if (!firstOp.getOpTime().equals(from)) {
        throw new TryAgainException("Remote oplog does not cointain our last operation");
    }
    OplogFetcher fetcher = new LimitedOplogFetcher(oplogCursor);
    ApplierContext context = new ApplierContext.Builder().setReapplying(true).setUpdatesAsUpserts(true).build();
    try {
        oplogApplier.apply(fetcher, context).waitUntilFinished();
    } catch (StopReplicationException | RollbackReplicationException | CancellationException | UnexpectedOplogApplierException ex) {
        throw new FatalErrorException(ex);
    }
    OpTime lastAppliedOptime;
    try (ReadOplogTransaction oplogTrans = oplogManager.createReadTransaction()) {
        lastAppliedOptime = oplogTrans.getLastAppliedOptime();
    }
    if (!lastAppliedOptime.equals(to)) {
        LOGGER.warn("Unexpected optime for last operation to apply. " + "Expected " + to + ", but " + lastAppliedOptime + " found");
    }
}
Also used : OplogFetcher(com.torodb.mongodb.repl.oplogreplier.fetcher.OplogFetcher) LimitedOplogFetcher(com.torodb.mongodb.repl.oplogreplier.fetcher.LimitedOplogFetcher) ReadOplogTransaction(com.torodb.mongodb.repl.OplogManager.ReadOplogTransaction) OplogStartMissingException(com.eightkdata.mongowp.exceptions.OplogStartMissingException) RollbackReplicationException(com.torodb.mongodb.repl.oplogreplier.RollbackReplicationException) OpTime(com.eightkdata.mongowp.OpTime) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) LimitedOplogFetcher(com.torodb.mongodb.repl.oplogreplier.fetcher.LimitedOplogFetcher) CancellationException(java.util.concurrent.CancellationException) UnexpectedOplogApplierException(com.torodb.mongodb.repl.oplogreplier.OplogApplier.UnexpectedOplogApplierException) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) StopReplicationException(com.torodb.mongodb.repl.oplogreplier.StopReplicationException)

Aggregations

OplogOperation (com.eightkdata.mongowp.server.api.oplog.OplogOperation)2 RollbackReplicationException (com.torodb.mongodb.repl.oplogreplier.RollbackReplicationException)2 StopReplicationException (com.torodb.mongodb.repl.oplogreplier.StopReplicationException)2 OpTime (com.eightkdata.mongowp.OpTime)1 MongoException (com.eightkdata.mongowp.exceptions.MongoException)1 OplogStartMissingException (com.eightkdata.mongowp.exceptions.OplogStartMissingException)1 MongoRuntimeException (com.eightkdata.mongowp.server.api.MongoRuntimeException)1 DeadCursorException (com.eightkdata.mongowp.server.api.pojos.MongoCursor.DeadCursorException)1 MongoServerException (com.mongodb.MongoServerException)1 RetrierAbortException (com.torodb.core.retrier.RetrierAbortException)1 RetrierGiveUpException (com.torodb.core.retrier.RetrierGiveUpException)1 RollbackException (com.torodb.core.transaction.RollbackException)1 ReadOplogTransaction (com.torodb.mongodb.repl.OplogManager.ReadOplogTransaction)1 ApplierContext (com.torodb.mongodb.repl.oplogreplier.ApplierContext)1 FinishedOplogBatch (com.torodb.mongodb.repl.oplogreplier.FinishedOplogBatch)1 NormalOplogBatch (com.torodb.mongodb.repl.oplogreplier.NormalOplogBatch)1 NotReadyForMoreOplogBatch (com.torodb.mongodb.repl.oplogreplier.NotReadyForMoreOplogBatch)1 UnexpectedOplogApplierException (com.torodb.mongodb.repl.oplogreplier.OplogApplier.UnexpectedOplogApplierException)1 OplogBatch (com.torodb.mongodb.repl.oplogreplier.OplogBatch)1 LimitedOplogFetcher (com.torodb.mongodb.repl.oplogreplier.fetcher.LimitedOplogFetcher)1