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);
}
}
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");
}
}
Aggregations