use of org.apache.samza.table.AsyncReadWriteUpdateTable in project samza by apache.
the class TestAsyncRetriableTable method testUpdateWithoutRetry.
@Test
public void testUpdateWithoutRetry() {
TableRetryPolicy policy = new TableRetryPolicy();
policy.withFixedBackoff(Duration.ofMillis(100));
TableReadFunction readFn = mock(TableReadFunction.class);
TableWriteFunction writeFn = mock(TableWriteFunction.class);
doReturn(true).when(writeFn).isRetriable(any());
doReturn(CompletableFuture.completedFuture(null)).when(writeFn).updateAsync(any(), any());
doReturn(CompletableFuture.completedFuture(null)).when(writeFn).updateAllAsync(any());
AsyncReadWriteUpdateTable delegate = new AsyncRemoteTable(readFn, writeFn);
AsyncRetriableTable table = new AsyncRetriableTable("t1", delegate, null, policy, schedExec, readFn, writeFn);
int times = 0;
table.init(TestRemoteTable.getMockContext());
verify(readFn, times(0)).init(any(), any());
verify(writeFn, times(0)).init(any(), any());
table.updateAsync("foo", "bar").join();
verify(writeFn, times(1)).updateAsync(any(), any());
assertEquals(++times, table.writeRetryMetrics.successCount.getCount());
table.updateAllAsync(Arrays.asList(new Entry<>("1", "2"))).join();
verify(writeFn, times(1)).updateAllAsync(any());
assertEquals(++times, table.writeRetryMetrics.successCount.getCount());
assertEquals(0, table.writeRetryMetrics.retryCount.getCount());
assertEquals(0, table.writeRetryMetrics.retryTimer.getSnapshot().getMax());
assertEquals(0, table.writeRetryMetrics.permFailureCount.getCount());
assertNull(table.readRetryMetrics);
}
use of org.apache.samza.table.AsyncReadWriteUpdateTable in project samza by apache.
the class TestAsyncRetriableTable method testUpdateWithOneRetry.
@Test
public void testUpdateWithOneRetry() {
TableRetryPolicy policy = new TableRetryPolicy();
policy.withFixedBackoff(Duration.ofMillis(10));
TableReadFunction<String, String> readFn = mock(TableReadFunction.class);
TableWriteFunction<String, String, String> writeFn = mock(TableWriteFunction.class);
doReturn(true).when(writeFn).isRetriable(any());
AtomicInteger times = new AtomicInteger();
doAnswer(invocation -> {
CompletableFuture<Map<String, String>> future = new CompletableFuture();
if (times.get() > 0) {
future.complete(null);
} else {
times.incrementAndGet();
future.completeExceptionally(new RuntimeException("test exception"));
}
return future;
}).when(writeFn).updateAsync(any(), any());
AsyncReadWriteUpdateTable delegate = new AsyncRemoteTable(readFn, writeFn);
AsyncRetriableTable table = new AsyncRetriableTable("t1", delegate, null, policy, schedExec, readFn, writeFn);
table.init(TestRemoteTable.getMockContext());
table.updateAsync(1, 2).join();
verify(writeFn, times(2)).updateAsync(any(), any());
assertEquals(1, table.writeRetryMetrics.retryCount.getCount());
assertEquals(0, table.writeRetryMetrics.successCount.getCount());
assertEquals(0, table.writeRetryMetrics.permFailureCount.getCount());
assertTrue(table.writeRetryMetrics.retryTimer.getSnapshot().getMax() > 0);
}
use of org.apache.samza.table.AsyncReadWriteUpdateTable in project samza by apache.
the class TestAsyncRetriableTable method testGetWithRetryDisabled.
@Test
public void testGetWithRetryDisabled() {
TableRetryPolicy policy = new TableRetryPolicy();
policy.withFixedBackoff(Duration.ofMillis(10));
policy.withStopAfterDelay(Duration.ofMillis(100));
TableReadFunction<String, String> readFn = mock(TableReadFunction.class);
doReturn(false).when(readFn).isRetriable(any());
CompletableFuture<String> future = new CompletableFuture();
future.completeExceptionally(new RuntimeException("test exception"));
doReturn(future).when(readFn).getAsync(anyString());
AsyncReadWriteUpdateTable delegate = new AsyncRemoteTable(readFn, null);
AsyncRetriableTable table = new AsyncRetriableTable("t1", delegate, policy, null, schedExec, readFn, null);
table.init(TestRemoteTable.getMockContext());
try {
table.getAsync("foo").join();
fail();
} catch (Throwable t) {
}
verify(readFn, times(1)).getAsync(any());
assertEquals(0, table.readRetryMetrics.retryCount.getCount());
assertEquals(0, table.readRetryMetrics.successCount.getCount());
assertEquals(0, table.readRetryMetrics.permFailureCount.getCount());
assertEquals(0, table.readRetryMetrics.retryTimer.getSnapshot().getMax());
}
use of org.apache.samza.table.AsyncReadWriteUpdateTable in project samza by apache.
the class TestAsyncRetriableTable method testPutWithPermFailureOnMaxCount.
@Test
public void testPutWithPermFailureOnMaxCount() {
TableRetryPolicy policy = new TableRetryPolicy();
policy.withFixedBackoff(Duration.ofMillis(5));
policy.withStopAfterAttempts(10);
TableReadFunction<String, String> readFn = mock(TableReadFunction.class);
TableWriteFunction<String, String, String> writeFn = mock(TableWriteFunction.class);
doReturn(true).when(writeFn).isRetriable(any());
CompletableFuture<String> future = new CompletableFuture();
future.completeExceptionally(new RuntimeException("test exception"));
doReturn(future).when(writeFn).putAllAsync(any());
AsyncReadWriteUpdateTable delegate = new AsyncRemoteTable(readFn, writeFn);
AsyncRetriableTable table = new AsyncRetriableTable("t1", delegate, null, policy, schedExec, readFn, writeFn);
table.init(TestRemoteTable.getMockContext());
try {
table.putAllAsync(Arrays.asList(new Entry(1, 2))).join();
fail();
} catch (Throwable t) {
}
verify(writeFn, atLeast(11)).putAllAsync(any());
assertEquals(10, table.writeRetryMetrics.retryCount.getCount());
assertEquals(0, table.writeRetryMetrics.successCount.getCount());
assertEquals(1, table.writeRetryMetrics.permFailureCount.getCount());
assertTrue(table.writeRetryMetrics.retryTimer.getSnapshot().getMax() > 0);
}
use of org.apache.samza.table.AsyncReadWriteUpdateTable in project samza by apache.
the class TestAsyncRetriableTable method testUpdateWithRetryDisabled.
@Test
public void testUpdateWithRetryDisabled() {
TableRetryPolicy policy = new TableRetryPolicy();
policy.withFixedBackoff(Duration.ofMillis(10));
policy.withStopAfterDelay(Duration.ofMillis(100));
TableReadFunction<String, String> readFn = mock(TableReadFunction.class);
TableWriteFunction<String, String, String> writeFn = mock(TableWriteFunction.class);
doReturn(false).when(writeFn).isRetriable(any());
CompletableFuture<String> future = new CompletableFuture();
future.completeExceptionally(new RuntimeException("test exception"));
doReturn(future).when(writeFn).updateAsync(any(), any());
AsyncReadWriteUpdateTable delegate = new AsyncRemoteTable(readFn, writeFn);
AsyncRetriableTable table = new AsyncRetriableTable("t1", delegate, null, policy, schedExec, readFn, writeFn);
table.init(TestRemoteTable.getMockContext());
try {
table.updateAsync("foo", "bar").join();
fail();
} catch (Throwable t) {
}
verify(writeFn, times(1)).updateAsync(any(), any());
assertEquals(0, table.writeRetryMetrics.retryCount.getCount());
assertEquals(0, table.writeRetryMetrics.successCount.getCount());
assertEquals(0, table.writeRetryMetrics.permFailureCount.getCount());
assertEquals(0, table.writeRetryMetrics.retryTimer.getSnapshot().getMax());
}
Aggregations