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