use of com.torodb.core.retrier.RetrierAbortException 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.core.retrier.RetrierAbortException in project torodb by torodb.
the class AkkaDbCloner method prepareCollection.
private void prepareCollection(MongodServer localServer, String dstDb, Entry colEntry) throws RetrierAbortException {
try {
retrier.retry(() -> {
try (WriteMongodTransaction transaction = createWriteMongodTransaction(localServer)) {
dropCollection(transaction, dstDb, colEntry.getCollectionName());
createCollection(transaction, dstDb, colEntry.getCollectionName(), colEntry.getCollectionOptions());
transaction.commit();
return null;
} catch (UserException ex) {
throw new RetrierAbortException("An unexpected user exception was catched", ex);
}
});
} catch (RetrierGiveUpException ex) {
throw new CloningException(ex);
}
}
use of com.torodb.core.retrier.RetrierAbortException 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);
}
}
use of com.torodb.core.retrier.RetrierAbortException in project torodb by torodb.
the class OplogManager method loadState.
@Locked(exclusive = true)
private void loadState() throws OplogManagerPersistException {
try {
retrier.retry(() -> {
try (ReadOnlyMongodTransaction transaction = connection.openReadOnlyTransaction()) {
Status<FindResult> status = transaction.execute(new Request(OPLOG_DB, null, true, null), FindCommand.INSTANCE, new FindArgument.Builder().setCollection(OPLOG_COL).setSlaveOk(true).build());
if (!status.isOk()) {
throw new RetrierAbortException(new MongoException(status));
}
Iterator<BsonDocument> batch = status.getResult().getCursor().getFirstBatch();
if (!batch.hasNext()) {
lastAppliedHash = 0;
lastAppliedOpTime = OpTime.EPOCH;
} else {
BsonDocument doc = batch.next();
BsonDocument subDoc = BsonReaderTool.getDocument(doc, KEY);
lastAppliedHash = BsonReaderTool.getLong(subDoc, "hash");
long optimeAsLong = BsonReaderTool.getLong(subDoc, "optime_i");
BsonDateTime optimeAsDateTime = DefaultBsonValues.newDateTime(optimeAsLong);
lastAppliedOpTime = new OpTime(TimestampToDateTime.toTimestamp(optimeAsDateTime, DefaultBsonValues::newTimestamp), BsonReaderTool.getLong(subDoc, "optime_t"));
}
notifyLastAppliedOpTimeChange();
return Empty.getInstance();
}
}, Hint.INFREQUENT_ROLLBACK);
} catch (RetrierGiveUpException ex) {
throw new OplogManagerPersistException(ex);
}
}
use of com.torodb.core.retrier.RetrierAbortException in project torodb by torodb.
the class SimpleAnalyzedOplogBatchExecutorTest method testVisit_CudAnalyzedOplog_UserEx.
@Test
public void testVisit_CudAnalyzedOplog_UserEx() throws Exception {
//GIVEN
OplogOperation lastOp = mock(OplogOperation.class);
CudAnalyzedOplogBatch batch = mock(CudAnalyzedOplogBatch.class);
ApplierContext applierContext = new ApplierContext.Builder().setReapplying(true).setUpdatesAsUpserts(true).build();
given(batch.getOriginalBatch()).willReturn(Lists.newArrayList(mock(OplogOperation.class), mock(OplogOperation.class), mock(OplogOperation.class), lastOp));
Timer timer = mock(Timer.class);
Context context = mock(Context.class);
given(metrics.getCudBatchTimer()).willReturn(timer);
given(timer.time()).willReturn(context);
doThrow(new DatabaseNotFoundException("test")).when(executor).execute(eq(batch), any());
//WHEN
try {
executor.visit(batch, applierContext);
fail("An exception was expected");
} catch (RetrierGiveUpException | RetrierAbortException ignore) {
}
//THEN
then(metrics).should().getCudBatchTimer();
then(timer).should().time();
then(executor).should(times(1)).execute(batch, applierContext);
}
Aggregations