Search in sources :

Example 11 with Entry

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());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Entry(org.apache.samza.storage.kv.Entry) List(java.util.List)

Example 12 with Entry

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());
}
Also used : HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) Answer(org.mockito.stubbing.Answer) Mockito(org.mockito.Mockito) List(java.util.List) Entry(org.apache.samza.storage.kv.Entry) ReadWriteUpdateTable(org.apache.samza.table.ReadWriteUpdateTable) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) LinkedList(java.util.LinkedList) Assert(org.junit.Assert) Before(org.junit.Before) Answer(org.mockito.stubbing.Answer) ReadWriteUpdateTable(org.apache.samza.table.ReadWriteUpdateTable) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 13 with Entry

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);
}
Also used : TableReadFunction(org.apache.samza.table.remote.TableReadFunction) Entry(org.apache.samza.storage.kv.Entry) AsyncRemoteTable(org.apache.samza.table.remote.AsyncRemoteTable) AsyncReadWriteUpdateTable(org.apache.samza.table.AsyncReadWriteUpdateTable) TableWriteFunction(org.apache.samza.table.remote.TableWriteFunction) Test(org.junit.Test)

Example 14 with Entry

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());
}
Also used : Entry(org.apache.samza.storage.kv.Entry) Test(org.junit.Test)

Example 15 with Entry

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());
}
Also used : Entry(org.apache.samza.storage.kv.Entry) Test(org.junit.Test)

Aggregations

Entry (org.apache.samza.storage.kv.Entry)15 Test (org.junit.Test)12 CompletableFuture (java.util.concurrent.CompletableFuture)6 HashMap (java.util.HashMap)5 Map (java.util.Map)4 AsyncReadWriteUpdateTable (org.apache.samza.table.AsyncReadWriteUpdateTable)4 AsyncRemoteTable (org.apache.samza.table.remote.AsyncRemoteTable)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 KeyValueStore (org.apache.samza.storage.kv.KeyValueStore)2 Duration (java.time.Duration)1 LinkedList (java.util.LinkedList)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 Executors (java.util.concurrent.Executors)1 SamzaException (org.apache.samza.SamzaException)1 TaskName (org.apache.samza.container.TaskName)1 TaskMode (org.apache.samza.job.model.TaskMode)1 MetricsRegistryMap (org.apache.samza.metrics.MetricsRegistryMap)1