use of org.apache.samza.storage.kv.Entry in project samza by apache.
the class TestRemoteTable method doTestPutAll.
public void doTestPutAll(boolean sync, boolean error, boolean hasDelete) {
TableWriteFunction<String, String, Void> writeFn = mock(TableWriteFunction.class);
RemoteTable<String, String, Void> table = getTable("testPutAll-" + sync + error + hasDelete, mock(TableReadFunction.class), writeFn, false);
CompletableFuture<Void> future;
if (error) {
future = new CompletableFuture();
future.completeExceptionally(new RuntimeException("Test exception"));
} else {
future = CompletableFuture.completedFuture(null);
}
// Sync is backed by async so needs to mock the async method
doReturn(future).when(writeFn).putAllAsync(any());
if (hasDelete) {
doReturn(future).when(writeFn).deleteAllAsync(any());
}
List<Entry<String, String>> entries = Arrays.asList(new Entry<>("foo1", "bar1"), new Entry<>("foo2", hasDelete ? null : "bar2"));
ArgumentCaptor<List> argCaptor = ArgumentCaptor.forClass(List.class);
if (sync) {
table.putAll(entries);
} else {
table.putAllAsync(entries).join();
}
verify(writeFn, times(1)).putAllAsync(argCaptor.capture());
if (hasDelete) {
ArgumentCaptor<List> delArgCaptor = ArgumentCaptor.forClass(List.class);
verify(writeFn, times(1)).deleteAllAsync(delArgCaptor.capture());
Assert.assertEquals(Arrays.asList("foo2"), delArgCaptor.getValue());
Assert.assertEquals(1, argCaptor.getValue().size());
Assert.assertEquals("foo1", ((Entry) argCaptor.getValue().get(0)).getKey());
verify(table.writeRateLimiter, times(1)).throttle(anyCollection());
} else {
Assert.assertEquals(entries, argCaptor.getValue());
}
verify(table.writeRateLimiter, times(1)).throttleRecords(anyCollection());
}
use of org.apache.samza.storage.kv.Entry in project samza by apache.
the class TestBatchTable method setup.
@Before
public void setup() {
final Answer getAnswer = invocation -> {
Integer key = invocation.getArgumentAt(0, Integer.class);
return tableDb.get(key);
};
final Answer getAsyncAnswer = invocation -> {
Integer key = invocation.getArgumentAt(0, Integer.class);
return CompletableFuture.completedFuture(tableDb.get(key));
};
final Answer getAllAsyncAnswer = invocation -> {
final List<Integer> list = invocation.getArgumentAt(0, List.class);
final Map<Integer, Integer> map = new HashMap<>();
list.forEach(k -> map.put(k, tableDb.get(k)));
return CompletableFuture.completedFuture(map);
};
final Answer putAnswer = invocation -> {
Integer key = invocation.getArgumentAt(0, Integer.class);
Integer value = invocation.getArgumentAt(1, Integer.class);
tableDb.put(key, value);
return null;
};
final Answer putAsyncAnswer = invocation -> {
final Integer key = invocation.getArgumentAt(0, Integer.class);
final Integer value = invocation.getArgumentAt(1, Integer.class);
tableDb.put(key, value);
return CompletableFuture.completedFuture(null);
};
final Answer putAllAsyncAnswer = invocation -> {
final List<Entry<Integer, Integer>> list = invocation.getArgumentAt(0, List.class);
list.forEach(entry -> tableDb.put(entry.getKey(), entry.getValue()));
return CompletableFuture.completedFuture(null);
};
final Answer deleteAnswer = invocation -> {
final Integer key = invocation.getArgumentAt(0, Integer.class);
tableDb.remove(key);
return null;
};
final Answer deleteAsyncAnswer = invocation -> {
final Integer key = invocation.getArgumentAt(0, Integer.class);
tableDb.remove(key);
return CompletableFuture.completedFuture(null);
};
final Answer deleteAllAsyncAnswer = invocation -> {
final List<Integer> list = invocation.getArgumentAt(0, List.class);
list.forEach(k -> tableDb.remove(k));
return CompletableFuture.completedFuture(null);
};
table = mock(ReadWriteUpdateTable.class);
final BatchMetrics batchMetrics = mock(BatchMetrics.class);
tableDb = new HashMap<>();
asyncBatchingTable = new AsyncBatchingTable("id", table, new CompactBatchProvider().withMaxBatchSize(BATCH_SIZE).withMaxBatchDelay(BATCH_DELAY), Executors.newSingleThreadScheduledExecutor());
asyncBatchingTable.createBatchProcessor(() -> 0, mock(BatchMetrics.class));
doAnswer(putAnswer).when(table).put(anyInt(), anyInt());
doAnswer(putAsyncAnswer).when(table).putAsync(anyInt(), anyInt());
doAnswer(putAllAsyncAnswer).when(table).putAllAsync(anyList());
doAnswer(i -> null).when(table).update(anyInt(), anyInt());
doAnswer(i -> CompletableFuture.completedFuture(null)).when(table).updateAsync(anyInt(), anyInt());
doAnswer(i -> CompletableFuture.completedFuture(null)).when(table).updateAllAsync(anyList());
doAnswer(deleteAnswer).when(table).delete(anyInt());
doAnswer(deleteAsyncAnswer).when(table).deleteAsync(anyInt());
doAnswer(deleteAllAsyncAnswer).when(table).deleteAllAsync(anyList());
doAnswer(getAnswer).when(table).get(anyInt());
doAnswer(getAsyncAnswer).when(table).getAsync(anyInt());
doAnswer(getAllAsyncAnswer).when(table).getAllAsync(anyList());
}
use of org.apache.samza.storage.kv.Entry in project samza by apache.
the class TestAsyncRetriableTable method testPutWithoutRetry.
@Test
public void testPutWithoutRetry() {
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).putAsync(any(), any());
doReturn(CompletableFuture.completedFuture(null)).when(writeFn).putAllAsync(any());
doReturn(CompletableFuture.completedFuture(null)).when(writeFn).deleteAsync(any());
doReturn(CompletableFuture.completedFuture(null)).when(writeFn).deleteAllAsync(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.putAsync("foo", "bar").join();
verify(writeFn, times(1)).putAsync(any(), any());
assertEquals(++times, table.writeRetryMetrics.successCount.getCount());
table.putAllAsync(Arrays.asList(new Entry("1", "2"))).join();
verify(writeFn, times(1)).putAllAsync(any());
assertEquals(++times, table.writeRetryMetrics.successCount.getCount());
table.deleteAsync("1").join();
verify(writeFn, times(1)).deleteAsync(any());
assertEquals(++times, table.writeRetryMetrics.successCount.getCount());
table.deleteAllAsync(Arrays.asList("1", "2")).join();
verify(writeFn, times(1)).deleteAllAsync(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.storage.kv.Entry in project samza by apache.
the class TestAsyncRemoteTable method testPutAllAsyncWithArgs.
@Test
public void testPutAllAsyncWithArgs() {
verifyFailure(() -> roTable.putAllAsync(Arrays.asList(new Entry(1, 2)), Arrays.asList(0, 0)));
rwTable.putAllAsync(Arrays.asList(new Entry(1, 2)), Arrays.asList(0, 0));
verify(writeFn, times(1)).putAllAsync(any(), any());
}
use of org.apache.samza.storage.kv.Entry in project samza by apache.
the class TestAsyncRemoteTable method testPutAllAsync.
@Test
public void testPutAllAsync() {
verifyFailure(() -> roTable.putAllAsync(Arrays.asList(new Entry(1, 2))));
rwTable.putAllAsync(Arrays.asList(new Entry(1, 2)));
verify(writeFn, times(1)).putAllAsync(any());
}
Aggregations