You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@samza.apache.org by we...@apache.org on 2019/05/20 16:49:37 UTC
[samza] branch master updated: SAMZA-2200: Update table sendTo()
and join() operation to accept additional arguments (#1041)
This is an automated email from the ASF dual-hosted git repository.
weisong44 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/samza.git
The following commit(s) were added to refs/heads/master by this push:
new 1772177 SAMZA-2200: Update table sendTo() and join() operation to accept additional arguments (#1041)
1772177 is described below
commit 1772177bf4e3c93308028fcf3f3f821c9d4a6ea6
Author: Wei Song <ws...@linkedin.com>
AuthorDate: Mon May 20 09:49:32 2019 -0700
SAMZA-2200: Update table sendTo() and join() operation to accept additional arguments (#1041)
---
.../org/apache/samza/operators/MessageStream.java | 6 +-
.../main/java/org/apache/samza/table/Table.java | 4 +-
.../samza/table/descriptors/TableDescriptor.java | 2 +-
.../apache/samza/operators/MessageStreamImpl.java | 8 +-
.../operators/impl/SendToTableOperatorImpl.java | 11 +-
.../impl/StreamTableJoinOperatorImpl.java | 3 +-
.../apache/samza/operators/spec/OperatorSpecs.java | 10 +-
.../operators/spec/SendToTableOperatorSpec.java | 9 +-
.../spec/StreamTableJoinOperatorSpec.java | 9 +-
.../impl/TestStreamTableJoinOperatorImpl.java | 1 +
.../samza/operators/spec/TestOperatorSpec.java | 6 +-
.../samza/test/table/TestRemoteTableEndToEnd.java | 282 +++++++++------------
12 files changed, 155 insertions(+), 196 deletions(-)
diff --git a/samza-api/src/main/java/org/apache/samza/operators/MessageStream.java b/samza-api/src/main/java/org/apache/samza/operators/MessageStream.java
index 451b925..8fd1b7a 100644
--- a/samza-api/src/main/java/org/apache/samza/operators/MessageStream.java
+++ b/samza-api/src/main/java/org/apache/samza/operators/MessageStream.java
@@ -207,13 +207,14 @@ public interface MessageStream<M> {
*
* @param table the table being joined
* @param joinFn the join function
+ * @param args additional arguments passed to the table
* @param <K> the type of join key
* @param <R> the type of table record
* @param <JM> the type of messages resulting from the {@code joinFn}
* @return the joined {@link MessageStream}
*/
<K, R extends KV, JM> MessageStream<JM> join(Table<R> table,
- StreamTableJoinFunction<? extends K, ? super M, ? super R, ? extends JM> joinFn);
+ StreamTableJoinFunction<? extends K, ? super M, ? super R, ? extends JM> joinFn, Object ... args);
/**
* Merges all {@code otherStreams} with this {@link MessageStream}.
@@ -290,11 +291,12 @@ public interface MessageStream<M> {
* scheme when propogated to next operator.
*
* @param table the table to write messages to
+ * @param args additional arguments passed to the table
* @param <K> the type of key in the table
* @param <V> the type of record value in the table
* @return this {@link MessageStream}
*/
- <K, V> MessageStream<KV<K, V>> sendTo(Table<KV<K, V>> table);
+ <K, V> MessageStream<KV<K, V>> sendTo(Table<KV<K, V>> table, Object ... args);
/**
* Broadcasts messages in this {@link MessageStream} to all instances of its downstream operators..
diff --git a/samza-api/src/main/java/org/apache/samza/table/Table.java b/samza-api/src/main/java/org/apache/samza/table/Table.java
index c454012..9e0510d 100644
--- a/samza-api/src/main/java/org/apache/samza/table/Table.java
+++ b/samza-api/src/main/java/org/apache/samza/table/Table.java
@@ -34,11 +34,11 @@ import org.apache.samza.task.InitableTask;
* A {@link Table} is an abstraction for data sources that support random access by key. It is an evolution of the
* existing {@link KeyValueStore} API. It offers support for both local and remote data sources and composition through
* hybrid tables. For remote data sources, a {@code RemoteTable} provides optimized access with caching, rate-limiting,
- * and retry support.
+ * batching, and retry support.
* <p>
* Use a {@link TableDescriptor} to specify the properties of a {@link Table}. For High Level API
* {@link StreamApplication}s, use {@link StreamApplicationDescriptor#getTable} to obtain the {@link Table} instance for
- * the descriptor that can be used with the {@link MessageStream} operators like {@link MessageStream#sendTo(Table)}.
+ * the descriptor that can be used with the {@link MessageStream} operators like {@link MessageStream#sendTo(Table, Object[])}.
* Alternatively, use {@link TaskContext#getTable(String)} in {@link InitableFunction#init} to get the table instance
* for use within operator functions. For Low Level API {@link TaskApplication}s, use {@link TaskContext#getTable}
* in {@link InitableTask#init} to get the table instance for use within the Task.
diff --git a/samza-api/src/main/java/org/apache/samza/table/descriptors/TableDescriptor.java b/samza-api/src/main/java/org/apache/samza/table/descriptors/TableDescriptor.java
index 81a98e6..6ccaf5c 100644
--- a/samza-api/src/main/java/org/apache/samza/table/descriptors/TableDescriptor.java
+++ b/samza-api/src/main/java/org/apache/samza/table/descriptors/TableDescriptor.java
@@ -48,7 +48,7 @@ import org.apache.samza.task.InitableTask;
* </pre>
* For High Level API {@link StreamApplication}s, use {@link StreamApplicationDescriptor#getTable(TableDescriptor)} to
* obtain the corresponding {@link Table} instance that can be used with the {@link MessageStream} operators like
- * {@link MessageStream#sendTo(Table)}. Alternatively, use {@link TaskContext#getTable(String)} in
+ * {@link MessageStream#sendTo(Table, Object[])}. Alternatively, use {@link TaskContext#getTable(String)} in
* {@link InitableFunction#init} to get the table instance for use within operator functions. For Low Level API
* {@link TaskApplication}s, use {@link TaskContext#getTable(String)} in {@link InitableTask#init} to get the
* table instance for use within the Task.
diff --git a/samza-core/src/main/java/org/apache/samza/operators/MessageStreamImpl.java b/samza-core/src/main/java/org/apache/samza/operators/MessageStreamImpl.java
index 6110ed1..dbc55c1 100644
--- a/samza-core/src/main/java/org/apache/samza/operators/MessageStreamImpl.java
+++ b/samza-core/src/main/java/org/apache/samza/operators/MessageStreamImpl.java
@@ -155,10 +155,10 @@ public class MessageStreamImpl<M> implements MessageStream<M> {
@Override
public <K, R extends KV, JM> MessageStream<JM> join(Table<R> table,
- StreamTableJoinFunction<? extends K, ? super M, ? super R, ? extends JM> joinFn) {
+ StreamTableJoinFunction<? extends K, ? super M, ? super R, ? extends JM> joinFn, Object ... args) {
String opId = this.streamAppDesc.getNextOpId(OpCode.JOIN);
StreamTableJoinOperatorSpec<K, M, R, JM> joinOpSpec = OperatorSpecs.createStreamTableJoinOperatorSpec(
- ((TableImpl) table).getTableId(), (StreamTableJoinFunction<K, M, R, JM>) joinFn, opId);
+ ((TableImpl) table).getTableId(), (StreamTableJoinFunction<K, M, R, JM>) joinFn, opId, args);
this.operatorSpec.registerNextOperatorSpec(joinOpSpec);
return new MessageStreamImpl<>(this.streamAppDesc, joinOpSpec);
}
@@ -189,10 +189,10 @@ public class MessageStreamImpl<M> implements MessageStream<M> {
}
@Override
- public <K, V> MessageStream<KV<K, V>> sendTo(Table<KV<K, V>> table) {
+ public <K, V> MessageStream<KV<K, V>> sendTo(Table<KV<K, V>> table, Object ... args) {
String opId = this.streamAppDesc.getNextOpId(OpCode.SEND_TO);
SendToTableOperatorSpec<K, V> op =
- OperatorSpecs.createSendToTableOperatorSpec(((TableImpl) table).getTableId(), opId);
+ OperatorSpecs.createSendToTableOperatorSpec(((TableImpl) table).getTableId(), opId, args);
this.operatorSpec.registerNextOperatorSpec(op);
return new MessageStreamImpl<>(this.streamAppDesc, op);
}
diff --git a/samza-core/src/main/java/org/apache/samza/operators/impl/SendToTableOperatorImpl.java b/samza-core/src/main/java/org/apache/samza/operators/impl/SendToTableOperatorImpl.java
index a5e8ea1..10d8edd 100644
--- a/samza-core/src/main/java/org/apache/samza/operators/impl/SendToTableOperatorImpl.java
+++ b/samza-core/src/main/java/org/apache/samza/operators/impl/SendToTableOperatorImpl.java
@@ -19,7 +19,6 @@
package org.apache.samza.operators.impl;
import java.util.Collections;
-import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import org.apache.samza.context.Context;
import org.apache.samza.operators.KV;
@@ -55,14 +54,8 @@ public class SendToTableOperatorImpl<K, V> extends OperatorImpl<KV<K, V>, KV<K,
@Override
protected CompletionStage<Collection<KV<K, V>>> handleMessageAsync(KV<K, V> message, MessageCollector collector,
TaskCoordinator coordinator) {
- K key = message.getKey();
- V val = message.getValue();
- if (val == null) {
- table.delete(key);
- } else {
- table.put(key, val);
- }
- return CompletableFuture.completedFuture(Collections.singleton(message));
+ return table.putAsync(message.getKey(), message.getValue(), sendToTableOpSpec.getArgs())
+ .thenApply(result -> Collections.singleton(message));
}
@Override
diff --git a/samza-core/src/main/java/org/apache/samza/operators/impl/StreamTableJoinOperatorImpl.java b/samza-core/src/main/java/org/apache/samza/operators/impl/StreamTableJoinOperatorImpl.java
index ec4d45d..7d74dfc 100644
--- a/samza-core/src/main/java/org/apache/samza/operators/impl/StreamTableJoinOperatorImpl.java
+++ b/samza-core/src/main/java/org/apache/samza/operators/impl/StreamTableJoinOperatorImpl.java
@@ -65,9 +65,10 @@ class StreamTableJoinOperatorImpl<K, M, R extends KV, JM> extends OperatorImpl<M
}
K key = joinOpSpec.getJoinFn().getMessageKey(message);
+ Object[] args = joinOpSpec.getArgs();
return Optional.ofNullable(key)
- .map(joinKey -> table.getAsync(joinKey)
+ .map(joinKey -> table.getAsync(joinKey, args)
.thenApply(val -> getJoinOutput(joinKey, val, message)))
.orElseGet(() -> CompletableFuture.completedFuture(getJoinOutput(key, null, message)));
}
diff --git a/samza-core/src/main/java/org/apache/samza/operators/spec/OperatorSpecs.java b/samza-core/src/main/java/org/apache/samza/operators/spec/OperatorSpecs.java
index 89d6b38..96b67fa 100644
--- a/samza-core/src/main/java/org/apache/samza/operators/spec/OperatorSpecs.java
+++ b/samza-core/src/main/java/org/apache/samza/operators/spec/OperatorSpecs.java
@@ -213,11 +213,12 @@ public class OperatorSpecs {
* @param <M> the type of input messages
* @param <R> the type of table record
* @param <JM> the type of the join result
+ * @param args additional arguments passed to the table
* @return the {@link StreamTableJoinOperatorSpec}
*/
public static <K, M, R, JM> StreamTableJoinOperatorSpec<K, M, R, JM> createStreamTableJoinOperatorSpec(
- String tableId, StreamTableJoinFunction<K, M, R, JM> joinFn, String opId) {
- return new StreamTableJoinOperatorSpec(tableId, joinFn, opId);
+ String tableId, StreamTableJoinFunction<K, M, R, JM> joinFn, String opId, Object ... args) {
+ return new StreamTableJoinOperatorSpec(tableId, joinFn, opId, args);
}
/**
@@ -226,13 +227,14 @@ public class OperatorSpecs {
*
* @param tableId the table Id for the underlying table
* @param opId the unique ID of the operator
+ * @param args additional arguments passed to the table
* @param <K> the type of the table record key
* @param <V> the type of the table record value
* @return the {@link SendToTableOperatorSpec}
*/
public static <K, V> SendToTableOperatorSpec<K, V> createSendToTableOperatorSpec(
- String tableId, String opId) {
- return new SendToTableOperatorSpec(tableId, opId);
+ String tableId, String opId, Object ... args) {
+ return new SendToTableOperatorSpec(tableId, opId, args);
}
/**
diff --git a/samza-core/src/main/java/org/apache/samza/operators/spec/SendToTableOperatorSpec.java b/samza-core/src/main/java/org/apache/samza/operators/spec/SendToTableOperatorSpec.java
index 02248ae..570a419 100644
--- a/samza-core/src/main/java/org/apache/samza/operators/spec/SendToTableOperatorSpec.java
+++ b/samza-core/src/main/java/org/apache/samza/operators/spec/SendToTableOperatorSpec.java
@@ -34,22 +34,29 @@ import org.apache.samza.operators.functions.WatermarkFunction;
public class SendToTableOperatorSpec<K, V> extends OperatorSpec<KV<K, V>, KV<K, V>> {
private final String tableId;
+ private final Object[] args;
/**
* Constructor for a {@link SendToTableOperatorSpec}.
*
* @param tableId the Id of the table written to
* @param opId the unique ID for this operator
+ * @param args additional arguments passed to the table
*/
- SendToTableOperatorSpec(String tableId, String opId) {
+ SendToTableOperatorSpec(String tableId, String opId, Object ... args) {
super(OpCode.SEND_TO, opId);
this.tableId = tableId;
+ this.args = args;
}
public String getTableId() {
return tableId;
}
+ public Object[] getArgs() {
+ return args;
+ }
+
@Override
public WatermarkFunction getWatermarkFn() {
return null;
diff --git a/samza-core/src/main/java/org/apache/samza/operators/spec/StreamTableJoinOperatorSpec.java b/samza-core/src/main/java/org/apache/samza/operators/spec/StreamTableJoinOperatorSpec.java
index a4f6068..3c100f8 100644
--- a/samza-core/src/main/java/org/apache/samza/operators/spec/StreamTableJoinOperatorSpec.java
+++ b/samza-core/src/main/java/org/apache/samza/operators/spec/StreamTableJoinOperatorSpec.java
@@ -36,6 +36,7 @@ public class StreamTableJoinOperatorSpec<K, M, R, JM> extends OperatorSpec<M, JM
private final String tableId;
private final StreamTableJoinFunction<K, M, R, JM> joinFn;
+ private final Object[] args;
/**
* Constructor for {@link StreamTableJoinOperatorSpec}.
@@ -43,11 +44,13 @@ public class StreamTableJoinOperatorSpec<K, M, R, JM> extends OperatorSpec<M, JM
* @param tableId the Id of the table on the right side of the join
* @param joinFn the user-defined join function to get join keys and results
* @param opId the unique ID for this operator
+ * @param args additional arguments passed to the table
*/
- StreamTableJoinOperatorSpec(String tableId, StreamTableJoinFunction<K, M, R, JM> joinFn, String opId) {
+ StreamTableJoinOperatorSpec(String tableId, StreamTableJoinFunction<K, M, R, JM> joinFn, String opId, Object ... args) {
super(OpCode.JOIN, opId);
this.tableId = tableId;
this.joinFn = joinFn;
+ this.args = args;
}
public String getTableId() {
@@ -58,6 +61,10 @@ public class StreamTableJoinOperatorSpec<K, M, R, JM> extends OperatorSpec<M, JM
return this.joinFn;
}
+ public Object[] getArgs() {
+ return args;
+ }
+
@Override
public WatermarkFunction getWatermarkFn() {
return joinFn instanceof WatermarkFunction ? (WatermarkFunction) joinFn : null;
diff --git a/samza-core/src/test/java/org/apache/samza/operators/impl/TestStreamTableJoinOperatorImpl.java b/samza-core/src/test/java/org/apache/samza/operators/impl/TestStreamTableJoinOperatorImpl.java
index 69950d9..236fd39 100644
--- a/samza-core/src/test/java/org/apache/samza/operators/impl/TestStreamTableJoinOperatorImpl.java
+++ b/samza-core/src/test/java/org/apache/samza/operators/impl/TestStreamTableJoinOperatorImpl.java
@@ -46,6 +46,7 @@ public class TestStreamTableJoinOperatorImpl {
StreamTableJoinOperatorSpec mockJoinOpSpec = mock(StreamTableJoinOperatorSpec.class);
when(mockJoinOpSpec.getTableId()).thenReturn(tableId);
+ when(mockJoinOpSpec.getArgs()).thenReturn(new Object[0]);
when(mockJoinOpSpec.getJoinFn()).thenReturn(
new StreamTableJoinFunction<String, KV<String, String>, KV<String, String>, String>() {
@Override
diff --git a/samza-core/src/test/java/org/apache/samza/operators/spec/TestOperatorSpec.java b/samza-core/src/test/java/org/apache/samza/operators/spec/TestOperatorSpec.java
index fd908ca..71c3486 100644
--- a/samza-core/src/test/java/org/apache/samza/operators/spec/TestOperatorSpec.java
+++ b/samza-core/src/test/java/org/apache/samza/operators/spec/TestOperatorSpec.java
@@ -300,24 +300,26 @@ public class TestOperatorSpec {
String tableId = "t1";
StreamTableJoinOperatorSpec<String, Object, Object, TestOutputMessageEnvelope> joinOperatorSpec =
- new StreamTableJoinOperatorSpec<>(tableId, joinFn, "join-3");
+ new StreamTableJoinOperatorSpec<>(tableId, joinFn, "join-3", 1, null, "3");
StreamTableJoinOperatorSpec<String, Object, Object, TestOutputMessageEnvelope> joinOpSpecCopy =
(StreamTableJoinOperatorSpec<String, Object, Object, TestOutputMessageEnvelope>) OperatorSpecTestUtils.copyOpSpec(joinOperatorSpec);
assertNotEquals(joinOpSpecCopy, joinOperatorSpec);
assertEquals(joinOpSpecCopy.getOpId(), joinOperatorSpec.getOpId());
assertEquals(joinOpSpecCopy.getTableId(), joinOperatorSpec.getTableId());
+ assertArrayEquals(joinOpSpecCopy.getArgs(), joinOperatorSpec.getArgs());
}
@Test
public void testSendToTableOperatorSpec() {
String tableId = "t1";
SendToTableOperatorSpec<String, Integer> sendOpSpec =
- new SendToTableOperatorSpec<>(tableId, "output-1");
+ new SendToTableOperatorSpec<>(tableId, "output-1", 1, null, "3");
SendToTableOperatorSpec<String, Integer> sendToCopy = (SendToTableOperatorSpec<String, Integer>) OperatorSpecTestUtils
.copyOpSpec(sendOpSpec);
assertNotEquals(sendToCopy, sendOpSpec);
assertEquals(sendToCopy.getOpId(), sendOpSpec.getOpId());
+ assertArrayEquals(sendToCopy.getArgs(), sendOpSpec.getArgs());
assertTrue(sendToCopy.getTableId().equals(sendOpSpec.getTableId()));
}
diff --git a/samza-test/src/test/java/org/apache/samza/test/table/TestRemoteTableEndToEnd.java b/samza-test/src/test/java/org/apache/samza/test/table/TestRemoteTableEndToEnd.java
index c48f46c..0a3b13e 100644
--- a/samza-test/src/test/java/org/apache/samza/test/table/TestRemoteTableEndToEnd.java
+++ b/samza-test/src/test/java/org/apache/samza/test/table/TestRemoteTableEndToEnd.java
@@ -264,6 +264,61 @@ public class TestRemoteTableEndToEnd extends IntegrationTestHarness {
}
}
+ static private class TestReadWriteMapFunction implements MapFunction<PageView, PageView> {
+
+ private final String counterTableName;
+ private ReadWriteTable counterTable;
+
+ private TestReadWriteMapFunction(String counterTableName) {
+ this.counterTableName = counterTableName;
+ }
+
+ @Override
+ public void init(Context context) {
+ counterTable = context.getTaskContext().getTable(counterTableName);
+ }
+
+ @Override
+ public PageView apply(PageView pageView) {
+ try {
+ // Counter manipulation
+ badOpId();
+ Assert.assertNull(getCounterValue(false));
+ Integer beforeValue = getCounterValue(true);
+ Assert.assertEquals(beforeValue, incCounterValue(false));
+ Assert.assertEquals(beforeValue, decCounterValue(false));
+ Assert.assertEquals(Integer.valueOf(beforeValue + 1), incCounterValue(true));
+ Assert.assertEquals(beforeValue, decCounterValue(true));
+ Assert.assertEquals(beforeValue, getCounterValue(true));
+ incCounterValue(true);
+ return pageView;
+ } catch (Exception ex) {
+ throw new SamzaException(ex);
+ }
+ }
+
+ private Integer getCounterValue(boolean shouldReturn) {
+ return (Integer) counterTable.readAsync(1, shouldReturn).join();
+ }
+
+ private Integer incCounterValue(boolean shouldModifdy) {
+ return (Integer) counterTable.writeAsync(1, shouldModifdy).join();
+ }
+
+ private Integer decCounterValue(boolean shouldModifdy) {
+ return (Integer) counterTable.writeAsync(2, shouldModifdy).join();
+ }
+
+ private void badOpId() {
+ try {
+ counterTable.readAsync(0).join();
+ Assert.fail("Shouldn't reach here");
+ } catch (SamzaException ex) {
+ // Expected exception
+ }
+ }
+ }
+
private <K, V> Table<KV<K, V>> getCachingTable(TableDescriptor<K, V, ?> actualTableDesc, boolean defaultCache,
StreamApplicationDescriptor appDesc) {
String id = actualTableDesc.getTableId();
@@ -281,7 +336,8 @@ public class TestRemoteTableEndToEnd extends IntegrationTestHarness {
return appDesc.getTable(cachingDesc);
}
- private void doTestStreamTableJoinRemoteTable(boolean withCache, boolean defaultCache, String testName) throws Exception {
+ private void doTestStreamTableJoinRemoteTable(boolean withCache, boolean defaultCache, boolean withArgs, String testName)
+ throws Exception {
writtenRecords.put(testName, new ArrayList<>());
@@ -322,11 +378,34 @@ public class TestRemoteTableEndToEnd extends IntegrationTestHarness {
final DelegatingSystemDescriptor ksd = new DelegatingSystemDescriptor("test");
final GenericInputDescriptor<PageView> isd = ksd.getInputDescriptor("PageView", new NoOpSerde<>());
- appDesc.getInputStream(isd)
- .map(pv -> new KV<>(pv.getMemberId(), pv))
- .join(joinTable, new PageViewToProfileJoinFunction())
- .map(m -> new KV(m.getMemberId(), m))
- .sendTo(outputTable);
+ if (!withArgs) {
+ appDesc.getInputStream(isd)
+ .map(pv -> new KV<>(pv.getMemberId(), pv))
+ .join(joinTable, new PageViewToProfileJoinFunction())
+ .map(m -> new KV(m.getMemberId(), m))
+ .sendTo(outputTable);
+
+ } else {
+ counters.put(testName, new AtomicInteger());
+
+ final RemoteTableDescriptor counterTableDesc =
+ new RemoteTableDescriptor("counter-table-1")
+ .withReadFunction(new InMemoryCounterReadFunction(testName))
+ .withWriteFunction(new InMemoryCounterWriteFunction(testName))
+ .withRateLimiterDisabled();
+
+ final Table counterTable = withCache
+ ? getCachingTable(counterTableDesc, defaultCache, appDesc)
+ : appDesc.getTable(counterTableDesc);
+
+ final String counterTableName = ((TableImpl) counterTable).getTableId();
+ appDesc.getInputStream(isd)
+ .map(new TestReadWriteMapFunction(counterTableName))
+ .map(pv -> new KV<>(pv.getMemberId(), pv))
+ .join(joinTable, new PageViewToProfileJoinFunction(), true)
+ .map(m -> new KV(m.getMemberId(), m))
+ .sendTo(outputTable, true);
+ }
};
final Config config = new MapConfig(configs);
@@ -337,21 +416,42 @@ public class TestRemoteTableEndToEnd extends IntegrationTestHarness {
final int numExpected = count * partitionCount;
Assert.assertEquals(numExpected, writtenRecords.get(testName).size());
Assert.assertTrue(writtenRecords.get(testName).get(0) instanceof EnrichedPageView);
+ if (!withArgs) {
+ writtenRecords.get(testName).forEach(epv -> Assert.assertFalse(epv.company.contains("-")));
+ } else {
+ writtenRecords.get(testName).forEach(epv -> Assert.assertTrue(epv.company.endsWith("-r-w")));
+ Assert.assertEquals(numExpected, counters.get(testName).get());
+ }
}
@Test
public void testStreamTableJoinRemoteTable() throws Exception {
- doTestStreamTableJoinRemoteTable(false, false, "testStreamTableJoinRemoteTable");
+ doTestStreamTableJoinRemoteTable(false, false, false, "testStreamTableJoinRemoteTable");
}
@Test
public void testStreamTableJoinRemoteTableWithCache() throws Exception {
- doTestStreamTableJoinRemoteTable(true, false, "testStreamTableJoinRemoteTableWithCache");
+ doTestStreamTableJoinRemoteTable(true, false, false, "testStreamTableJoinRemoteTableWithCache");
}
@Test
public void testStreamTableJoinRemoteTableWithDefaultCache() throws Exception {
- doTestStreamTableJoinRemoteTable(true, true, "testStreamTableJoinRemoteTableWithDefaultCache");
+ doTestStreamTableJoinRemoteTable(true, true, false, "testStreamTableJoinRemoteTableWithDefaultCache");
+ }
+
+ @Test
+ public void testStreamTableJoinRemoteTableWithArgs() throws Exception {
+ doTestStreamTableJoinRemoteTable(false, false, true, "testStreamTableJoinRemoteTableWithArgs");
+ }
+
+ @Test
+ public void testStreamTableJoinRemoteTableWithCacheWithArgs() throws Exception {
+ doTestStreamTableJoinRemoteTable(true, false, true, "testStreamTableJoinRemoteTableWithCacheWithArgs");
+ }
+
+ @Test
+ public void testStreamTableJoinRemoteTableWithDefaultCacheWithArgs() throws Exception {
+ doTestStreamTableJoinRemoteTable(true, true, true, "testStreamTableJoinRemoteTableWithDefaultCacheWithArgs");
}
private Context createMockContext() {
@@ -404,175 +504,19 @@ public class TestRemoteTableEndToEnd extends IntegrationTestHarness {
null, null, null,
null);
table.init(createMockContext());
- int failureCount = 0;
try {
table.put("abc", "efg");
+ Assert.fail();
} catch (SamzaException ex) {
- ++failureCount;
+ // Ignore
}
try {
table.delete("abc");
+ Assert.fail();
} catch (SamzaException ex) {
- ++failureCount;
+ // Ignore
}
table.flush();
table.close();
}
-
- private void doTestReadWriteWithArgs(boolean withCache, boolean defaultCache, String testName) throws Exception {
-
- writtenRecords.put(testName, new ArrayList<>());
- counters.put(testName, new AtomicInteger());
-
- final int count = 10;
- final PageView[] pageViews = generatePageViews(count);
- final String profiles = Base64Serializer.serialize(generateProfiles(count));
-
- final int partitionCount = 4;
- final Map<String, String> configs = TestLocalTableEndToEnd.getBaseJobConfig(bootstrapUrl(), zkConnect());
- configs.put("streams.PageView.samza.system", "test");
- configs.put("streams.PageView.source", Base64Serializer.serialize(pageViews));
- configs.put("streams.PageView.partitionCount", String.valueOf(partitionCount));
-
- final RateLimiter readRateLimiter = mock(RateLimiter.class, withSettings().serializable());
- final TableRateLimiter.CreditFunction creditFunction = (k, v, args) -> 1;
- final StreamApplication app = appDesc -> {
-
- final RemoteTableDescriptor joinTableDesc =
- new RemoteTableDescriptor<Integer, TestTableData.Profile>("profile-table-1")
- .withReadFunction(InMemoryProfileReadFunction.getInMemoryReadFunction(testName, profiles))
- .withRateLimiter(readRateLimiter, creditFunction, null);
-
- final RemoteTableDescriptor outputTableDesc =
- new RemoteTableDescriptor<Integer, EnrichedPageView>("enriched-page-view-table-1")
- .withReadFunction(new DummyReadFunction())
- .withReadRateLimiterDisabled()
- .withWriteFunction(new InMemoryEnrichedPageViewWriteFunction(testName))
- .withWriteRateLimit(1000);
-
- final RemoteTableDescriptor counterTableDesc =
- new RemoteTableDescriptor("counter-table-1")
- .withReadFunction(new InMemoryCounterReadFunction(testName))
- .withWriteFunction(new InMemoryCounterWriteFunction(testName))
- .withRateLimiterDisabled();
-
- final Table joinTable = withCache
- ? getCachingTable(joinTableDesc, defaultCache, appDesc)
- : appDesc.getTable(joinTableDesc);
-
- final Table outputTable = withCache
- ? getCachingTable(outputTableDesc, defaultCache, appDesc)
- : appDesc.getTable(outputTableDesc);
-
- final Table counterTable = withCache
- ? getCachingTable(counterTableDesc, defaultCache, appDesc)
- : appDesc.getTable(counterTableDesc);
-
- final String joinTableName = ((TableImpl) joinTable).getTableId();
- final String outputTableName = ((TableImpl) outputTable).getTableId();
- final String counterTableName = ((TableImpl) counterTable).getTableId();
-
- final DelegatingSystemDescriptor ksd = new DelegatingSystemDescriptor("test");
- final GenericInputDescriptor<PageView> isd = ksd.getInputDescriptor("PageView", new NoOpSerde<>());
-
- appDesc.getInputStream(isd)
- .map(new JoinMapFunction(joinTableName, outputTableName, counterTableName));
- };
-
- final Config config = new MapConfig(configs);
- final LocalApplicationRunner runner = new LocalApplicationRunner(app, config);
- executeRun(runner, config);
- runner.waitForFinish();
-
- final int numExpected = count * partitionCount;
- Assert.assertEquals(numExpected, writtenRecords.get(testName).size());
- Assert.assertTrue(writtenRecords.get(testName).get(0) instanceof EnrichedPageView);
- writtenRecords.get(testName).forEach(epv -> {
- Assert.assertTrue(epv.company.endsWith("-r-w"));
- });
- Assert.assertEquals(numExpected, counters.get(testName).get());
- }
-
- static private class JoinMapFunction implements MapFunction<PageView, EnrichedPageView> {
-
- private final String joinTableName;
- private final String outputTableName;
- private final String counterTableName;
- private ReadWriteTable<Integer, Profile> inputTable;
- private ReadWriteTable<Integer, EnrichedPageView> outputTable;
- private ReadWriteTable counterTable;
-
- private JoinMapFunction(String joinTableName, String outputTableName, String counterTableName) {
- this.joinTableName = joinTableName;
- this.outputTableName = outputTableName;
- this.counterTableName = counterTableName;
- }
-
- @Override
- public void init(Context context) {
- inputTable = context.getTaskContext().getTable(joinTableName);
- outputTable = context.getTaskContext().getTable(outputTableName);
- counterTable = context.getTaskContext().getTable(counterTableName);
- }
-
- @Override
- public EnrichedPageView apply(PageView pageView) {
- try {
- // Counter manipulation
- badOpId();
- Assert.assertNull(getCounterValue(false));
- Integer beforeValue = getCounterValue(true);
- Assert.assertEquals(beforeValue, incCounterValue(false));
- Assert.assertEquals(beforeValue, decCounterValue(false));
- Assert.assertEquals(Integer.valueOf(beforeValue + 1), incCounterValue(true));
- Assert.assertEquals(beforeValue, decCounterValue(true));
- Assert.assertEquals(beforeValue, getCounterValue(true));
- incCounterValue(true);
-
- // Generate EnrichedPageView
- Profile profile = inputTable.getAsync(pageView.memberId, true).join();
- EnrichedPageView epv = new EnrichedPageView(pageView.getPageKey(), profile.memberId, profile.company);
- outputTable.putAsync(epv.memberId, epv, true).join();
- return epv;
- } catch (Exception ex) {
- throw new SamzaException(ex);
- }
- }
-
- private Integer getCounterValue(boolean shouldReturn) {
- return (Integer) counterTable.readAsync(1, shouldReturn).join();
- }
-
- private Integer incCounterValue(boolean shouldModifdy) {
- return (Integer) counterTable.writeAsync(1, shouldModifdy).join();
- }
-
- private Integer decCounterValue(boolean shouldModifdy) {
- return (Integer) counterTable.writeAsync(2, shouldModifdy).join();
- }
-
- private void badOpId() {
- try {
- outputTable.readAsync(0).join();
- Assert.fail("Shouldn't reach here");
- } catch (SamzaException ex) {
- // Expected exception
- }
- }
- }
-
- @Test
- public void testReadWriteWithArgs() throws Exception {
- doTestReadWriteWithArgs(false, false, "testReadWriteWithArgs");
- }
-
- @Test
- public void testReadWriteWithArgsWithCache() throws Exception {
- doTestReadWriteWithArgs(true, false, "testReadWriteWithArgsWithCache");
- }
-
- @Test
- public void testReadWriteWithArgsWithDefaultCache() throws Exception {
- doTestReadWriteWithArgs(true, true, "testReadWriteWithArgsWithDefaultCache");
- }
}