Search in sources :

Example 1 with ApplierContext

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

the class SimpleAnalyzedOplogBatchExecutorTest method testVisit_CudAnalyzedOplog_Success.

@Test
public void testVisit_CudAnalyzedOplog_Success() 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);
    doNothing().when(executor).execute(eq(batch), any());
    //WHEN
    OplogOperation result = executor.visit(batch, applierContext);
    //THEN
    then(metrics).should().getCudBatchTimer();
    then(timer).should().time();
    then(context).should().close();
    then(executor).should(times(1)).execute(batch, applierContext);
    assertEquals(lastOp, result);
}
Also used : ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Context(com.codahale.metrics.Timer.Context) Timer(com.codahale.metrics.Timer) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Test(org.junit.Test)

Example 2 with ApplierContext

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

the class SimpleAnalyzedOplogBatchExecutorTest method testVisit_SingleOp_NotRepyingRollback.

/**
   * Test the behaviour of the method
   * {@link SimpleAnalyzedOplogBatchExecutor#visit(com.torodb.mongodb.repl.oplogreplier.batch.SingleOpAnalyzedOplogBatch, com.torodb.mongodb.repl.oplogreplier.ApplierContext) that visits a single op}
   * when
   * {@link SimpleAnalyzedOplogBatchExecutor#execute(com.eightkdata.mongowp.server.api.oplog.OplogOperation, com.torodb.mongodb.repl.oplogreplier.ApplierContext) the execution}
   * fails until the given attempt.
   *
   *
   * @param myRetrier
   * @param atteptsToSucceed
   * @return true if the execution finishes or false if it throw an exception.
   * @throws Exception
   */
private boolean testVisit_SingleOp_NotRepyingRollback(Retrier myRetrier, int atteptsToSucceed) throws Exception {
    //GIVEN
    OplogOperation operation = mock(OplogOperation.class);
    SingleOpAnalyzedOplogBatch batch = new SingleOpAnalyzedOplogBatch(operation);
    ApplierContext applierContext = new ApplierContext.Builder().setReapplying(true).setUpdatesAsUpserts(false).build();
    executor = spy(new SimpleAnalyzedOplogBatchExecutor(metrics, applier, server, myRetrier, namespaceJobExecutor));
    Timer timer = mock(Timer.class);
    Context context = mock(Context.class);
    given(metrics.getSingleOpTimer(operation)).willReturn(timer);
    given(timer.time()).willReturn(context);
    doAnswer(new Answer() {

        int attempts = 0;

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            try {
                ApplierContext context = invocation.getArgument(1);
                if (attempts == 0) {
                    assert !context.treatUpdateAsUpsert() : "on this test, first attept should be not trying updates as upserts";
                    throw new RollbackException("Forcing a rollback on the first attempt");
                }
                assert context.treatUpdateAsUpsert() : "on this test, only the first attept should be " + "not trying updates as upserts, but " + attempts + " is not trying updates as upserts";
                if (attempts < (atteptsToSucceed - 1)) {
                    throw new RollbackException("forcing a rollback on the " + attempts + "th attempt");
                }
                return null;
            } finally {
                attempts++;
            }
        }
    }).when(executor).execute(eq(operation), any());
    try {
        //WHEN
        OplogOperation result = executor.visit(batch, applierContext);
        //THEN
        then(executor).should(times(atteptsToSucceed)).execute(eq(operation), any());
        assertEquals(operation, result);
        return true;
    } catch (RetrierGiveUpException ignore) {
        return false;
    } finally {
        then(metrics).should().getSingleOpTimer(operation);
        then(timer).should().time();
    }
}
Also used : ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Context(com.codahale.metrics.Timer.Context) RollbackException(com.torodb.core.transaction.RollbackException) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Timer(com.codahale.metrics.Timer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation)

Example 3 with ApplierContext

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

the class SimpleAnalyzedOplogBatchExecutorTest method testVisit_CudAnalyzedOplog_NotRepyingRollback.

/**
   * Test the behaviour of the method
   * {@link SimpleAnalyzedOplogBatchExecutor#visit(com.torodb.mongodb.repl.oplogreplier.batch.CudAnalyzedOplogBatch, com.torodb.mongodb.repl.oplogreplier.ApplierContext)  that visits a cud batch}
   * when
   * {@link SimpleAnalyzedOplogBatchExecutor#execute(com.torodb.mongodb.repl.oplogreplier.batch.CudAnalyzedOplogBatch, com.torodb.mongodb.repl.oplogreplier.ApplierContext) the execution}
   * fails until the given attempt.
   *
   *
   * @param myRetrier
   * @param atteptsToSucceed
   * @return true if the execution finishes or false if it throw an exception.
   * @throws Exception
   */
private boolean testVisit_CudAnalyzedOplog_NotRepyingRollback(Retrier myRetrier, int atteptsToSucceed) throws Exception {
    //GIVEN
    OplogOperation lastOp = mock(OplogOperation.class);
    CudAnalyzedOplogBatch batch = mock(CudAnalyzedOplogBatch.class);
    ApplierContext applierContext = new ApplierContext.Builder().setReapplying(true).setUpdatesAsUpserts(false).build();
    given(batch.getOriginalBatch()).willReturn(Lists.newArrayList(mock(OplogOperation.class), mock(OplogOperation.class), mock(OplogOperation.class), lastOp));
    executor = spy(new SimpleAnalyzedOplogBatchExecutor(metrics, applier, server, myRetrier, namespaceJobExecutor));
    Timer timer = mock(Timer.class);
    Context context = mock(Context.class);
    given(metrics.getCudBatchTimer()).willReturn(timer);
    given(timer.time()).willReturn(context);
    doAnswer(new Answer() {

        int attempts = 0;

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            try {
                ApplierContext context = invocation.getArgument(1);
                if (attempts == 0) {
                    assert !context.treatUpdateAsUpsert() : "on this test, first attept should be not trying updates as upserts";
                    throw new RollbackException("Forcing a rollback on the first attempt");
                }
                assert context.treatUpdateAsUpsert() : "on this test, only the first attept should be " + "not trying updates as upserts, but " + attempts + " is not trying updates as upserts";
                if (attempts < (atteptsToSucceed - 1)) {
                    throw new RollbackException("forcing a rollback on the " + attempts + "th attempt");
                }
                return null;
            } finally {
                attempts++;
            }
        }
    }).when(executor).execute(eq(batch), any());
    boolean success;
    try {
        //WHEN
        OplogOperation result = executor.visit(batch, applierContext);
        //THEN
        then(executor).should(times(atteptsToSucceed)).execute(eq(batch), any());
        assertEquals(lastOp, result);
        success = true;
    } catch (RetrierGiveUpException ignore) {
        success = false;
    }
    then(metrics.getCudBatchSize()).should().update(batch.getOriginalBatch().size());
    then(metrics).should().getCudBatchTimer();
    then(metrics.getCudBatchTimer()).should().time();
    return success;
}
Also used : ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Context(com.codahale.metrics.Timer.Context) RollbackException(com.torodb.core.transaction.RollbackException) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Timer(com.codahale.metrics.Timer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation)

Example 4 with ApplierContext

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

the class SimpleAnalyzedOplogBatchExecutorTest method testExecute_CudAnalyzedOplogBatch.

@Test
public void testExecute_CudAnalyzedOplogBatch() throws Exception {
    //GIVEN
    CudAnalyzedOplogBatch cudBatch = mock(CudAnalyzedOplogBatch.class);
    ApplierContext applierContext = new ApplierContext.Builder().setReapplying(true).setUpdatesAsUpserts(true).build();
    NamespaceJob job1 = mock(NamespaceJob.class);
    NamespaceJob job2 = mock(NamespaceJob.class);
    NamespaceJob job3 = mock(NamespaceJob.class);
    doNothing().when(executor).execute(any(), any(), any());
    given(cudBatch.streamNamespaceJobs()).willReturn(Stream.of(job1, job2, job3));
    //WHEN
    executor.execute(cudBatch, applierContext);
    //THEN
    then(server).should().openConnection();
    then(conn).should().close();
    then(executor).should().execute(job1, applierContext, conn);
    then(executor).should().execute(job2, applierContext, conn);
    then(executor).should().execute(job3, applierContext, conn);
}
Also used : ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Test(org.junit.Test)

Example 5 with ApplierContext

use of com.torodb.mongodb.repl.oplogreplier.ApplierContext 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)

Aggregations

ApplierContext (com.torodb.mongodb.repl.oplogreplier.ApplierContext)13 OplogOperation (com.eightkdata.mongowp.server.api.oplog.OplogOperation)10 Context (com.codahale.metrics.Timer.Context)9 Test (org.junit.Test)9 Timer (com.codahale.metrics.Timer)7 RetrierGiveUpException (com.torodb.core.retrier.RetrierGiveUpException)6 RetrierAbortException (com.torodb.core.retrier.RetrierAbortException)4 RollbackException (com.torodb.core.transaction.RollbackException)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 DatabaseNotFoundException (com.torodb.core.exceptions.user.DatabaseNotFoundException)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 Answer (org.mockito.stubbing.Answer)2 Histogram (com.codahale.metrics.Histogram)1 Meter (com.codahale.metrics.Meter)1 OpTime (com.eightkdata.mongowp.OpTime)1 MongoException (com.eightkdata.mongowp.exceptions.MongoException)1 OplogStartMissingException (com.eightkdata.mongowp.exceptions.OplogStartMissingException)1 UserException (com.torodb.core.exceptions.user.UserException)1 ReadOplogTransaction (com.torodb.mongodb.repl.OplogManager.ReadOplogTransaction)1 UnexpectedOplogApplierException (com.torodb.mongodb.repl.oplogreplier.OplogApplier.UnexpectedOplogApplierException)1