Search in sources :

Example 6 with RetrierGiveUpException

use of com.torodb.core.retrier.RetrierGiveUpException 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);
    }
}
Also used : DeleteArgument(com.torodb.mongodb.commands.signatures.general.DeleteCommand.DeleteArgument) InsertResult(com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertResult) WriteMongodTransaction(com.torodb.mongodb.core.WriteMongodTransaction) MongoException(com.eightkdata.mongowp.exceptions.MongoException) Request(com.eightkdata.mongowp.server.api.Request) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) DeleteStatement(com.torodb.mongodb.commands.signatures.general.DeleteCommand.DeleteStatement) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder) RetrierAbortException(com.torodb.core.retrier.RetrierAbortException) InsertArgument(com.torodb.mongodb.commands.signatures.general.InsertCommand.InsertArgument) DefaultBsonValues.newLong(com.eightkdata.mongowp.bson.utils.DefaultBsonValues.newLong) UserException(com.torodb.core.exceptions.user.UserException) Locked(com.torodb.mongodb.annotations.Locked)

Example 7 with RetrierGiveUpException

use of com.torodb.core.retrier.RetrierGiveUpException 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);
    }
}
Also used : MongoException(com.eightkdata.mongowp.exceptions.MongoException) ReadOnlyMongodTransaction(com.torodb.mongodb.core.ReadOnlyMongodTransaction) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder) Request(com.eightkdata.mongowp.server.api.Request) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) OpTime(com.eightkdata.mongowp.OpTime) DefaultBsonValues(com.eightkdata.mongowp.bson.utils.DefaultBsonValues) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) RetrierAbortException(com.torodb.core.retrier.RetrierAbortException) BsonDateTime(com.eightkdata.mongowp.bson.BsonDateTime) FindResult(com.torodb.mongodb.commands.signatures.general.FindCommand.FindResult) Locked(com.torodb.mongodb.annotations.Locked)

Example 8 with RetrierGiveUpException

use of com.torodb.core.retrier.RetrierGiveUpException 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);
}
Also used : ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Context(com.codahale.metrics.Timer.Context) Timer(com.codahale.metrics.Timer) RetrierAbortException(com.torodb.core.retrier.RetrierAbortException) DatabaseNotFoundException(com.torodb.core.exceptions.user.DatabaseNotFoundException) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Test(org.junit.Test)

Example 9 with RetrierGiveUpException

use of com.torodb.core.retrier.RetrierGiveUpException in project torodb by torodb.

the class SimpleAnalyzedOplogBatchExecutorTest method testVisit_SingleOp_OplogApplyingEx.

@Test
public void testVisit_SingleOp_OplogApplyingEx() throws Exception {
    //GIVEN
    OplogOperation operation = mock(OplogOperation.class);
    SingleOpAnalyzedOplogBatch batch = new SingleOpAnalyzedOplogBatch(operation);
    ApplierContext applierContext = new ApplierContext.Builder().setReapplying(true).setUpdatesAsUpserts(true).build();
    Timer timer = mock(Timer.class);
    Context context = mock(Context.class);
    given(metrics.getSingleOpTimer(operation)).willReturn(timer);
    given(timer.time()).willReturn(context);
    doThrow(new OplogApplyingException(new MongoException(ErrorCode.BAD_VALUE))).when(executor).execute(operation, applierContext);
    //WHEN
    try {
        executor.visit(batch, applierContext);
        fail("An exception was expected");
    } catch (RetrierGiveUpException | RetrierAbortException ignore) {
    }
    //THEN
    then(metrics).should().getSingleOpTimer(operation);
    then(timer).should().time();
    then(executor).should(times(1)).execute(operation, applierContext);
}
Also used : ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Context(com.codahale.metrics.Timer.Context) OplogApplyingException(com.torodb.mongodb.repl.oplogreplier.OplogOperationApplier.OplogApplyingException) MongoException(com.eightkdata.mongowp.exceptions.MongoException) Timer(com.codahale.metrics.Timer) RetrierAbortException(com.torodb.core.retrier.RetrierAbortException) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Test(org.junit.Test)

Example 10 with RetrierGiveUpException

use of com.torodb.core.retrier.RetrierGiveUpException in project torodb by torodb.

the class SimpleAnalyzedOplogBatchExecutorTest method testVisit_SingleOp_UserEx.

@Test
public void testVisit_SingleOp_UserEx() throws Exception {
    //GIVEN
    OplogOperation operation = mock(OplogOperation.class);
    SingleOpAnalyzedOplogBatch batch = new SingleOpAnalyzedOplogBatch(operation);
    ApplierContext applierContext = new ApplierContext.Builder().setReapplying(true).setUpdatesAsUpserts(true).build();
    Timer timer = mock(Timer.class);
    Context context = mock(Context.class);
    given(metrics.getSingleOpTimer(operation)).willReturn(timer);
    given(timer.time()).willReturn(context);
    doThrow(new DatabaseNotFoundException("test")).when(executor).execute(operation, applierContext);
    //WHEN
    try {
        executor.visit(batch, applierContext);
        fail("An exception was expected");
    } catch (RetrierGiveUpException | RetrierAbortException ignore) {
    }
    //THEN
    then(metrics).should().getSingleOpTimer(operation);
    then(timer).should().time();
    then(executor).should(times(1)).execute(operation, applierContext);
}
Also used : ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Context(com.codahale.metrics.Timer.Context) Timer(com.codahale.metrics.Timer) RetrierAbortException(com.torodb.core.retrier.RetrierAbortException) DatabaseNotFoundException(com.torodb.core.exceptions.user.DatabaseNotFoundException) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Test(org.junit.Test)

Aggregations

RetrierGiveUpException (com.torodb.core.retrier.RetrierGiveUpException)11 RetrierAbortException (com.torodb.core.retrier.RetrierAbortException)8 OplogOperation (com.eightkdata.mongowp.server.api.oplog.OplogOperation)7 Timer (com.codahale.metrics.Timer)6 Context (com.codahale.metrics.Timer.Context)6 ApplierContext (com.torodb.mongodb.repl.oplogreplier.ApplierContext)6 MongoException (com.eightkdata.mongowp.exceptions.MongoException)4 RollbackException (com.torodb.core.transaction.RollbackException)4 UserException (com.torodb.core.exceptions.user.UserException)3 WriteMongodTransaction (com.torodb.mongodb.core.WriteMongodTransaction)3 Test (org.junit.Test)3 Request (com.eightkdata.mongowp.server.api.Request)2 BsonDocumentBuilder (com.eightkdata.mongowp.utils.BsonDocumentBuilder)2 DatabaseNotFoundException (com.torodb.core.exceptions.user.DatabaseNotFoundException)2 Locked (com.torodb.mongodb.annotations.Locked)2 ReadOnlyMongodTransaction (com.torodb.mongodb.core.ReadOnlyMongodTransaction)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 OpTime (com.eightkdata.mongowp.OpTime)1