use of org.apache.samza.util.TimestampedValue in project samza by apache.
the class TestTimeSeriesStoreImpl method testPutWithMultipleEntries.
@Test
public void testPutWithMultipleEntries() {
TimeSeriesStore<String, byte[]> timeSeriesStore = newTimeSeriesStore(new StringSerde("UTF-8"), true);
// insert 100 entries at timestamps "1" and "2"
for (int i = 0; i < 100; i++) {
timeSeriesStore.put("hello", "world-1".getBytes(), 1L);
timeSeriesStore.put("hello", "world-2".getBytes(), 2L);
}
// read from time-range [0,2) should return 100 entries
List<TimestampedValue<byte[]>> values = readStore(timeSeriesStore, "hello", 0L, 2L);
Assert.assertEquals(100, values.size());
values.forEach(timeSeriesValue -> {
Assert.assertEquals("world-1", new String(timeSeriesValue.getValue()));
});
// read from time-range [2,4) should return 100 entries
values = readStore(timeSeriesStore, "hello", 2L, 4L);
Assert.assertEquals(100, values.size());
values.forEach(timeSeriesValue -> {
Assert.assertEquals("world-2", new String(timeSeriesValue.getValue()));
});
// read all entries in the store
values = readStore(timeSeriesStore, "hello", 0L, Integer.MAX_VALUE);
Assert.assertEquals(200, values.size());
}
use of org.apache.samza.util.TimestampedValue in project samza by apache.
the class TestTimeSeriesStoreImpl method testDeletesInOverwriteMode.
@Test
public void testDeletesInOverwriteMode() {
// instantiate a store in overwrite mode
TimeSeriesStore<String, byte[]> timeSeriesStore = newTimeSeriesStore(new StringSerde("UTF-8"), false);
// insert an entry with key "hello" at timestamps "1" and "2"
timeSeriesStore.put("hello", "world-1".getBytes(), 1L);
timeSeriesStore.put("hello", "world-1".getBytes(), 2L);
timeSeriesStore.put("hello", "world-2".getBytes(), 2L);
List<TimestampedValue<byte[]>> values = readStore(timeSeriesStore, "hello", 1L, 3L);
Assert.assertEquals(2, values.size());
timeSeriesStore.remove("hello", 0L, 3L);
values = readStore(timeSeriesStore, "hello", 1L, 3L);
Assert.assertEquals(0, values.size());
}
use of org.apache.samza.util.TimestampedValue in project samza by apache.
the class TestTimeSeriesStoreImpl method testGetOnTimestampBoundaries.
@Test
public void testGetOnTimestampBoundaries() {
TimeSeriesStore<String, byte[]> timeSeriesStore = newTimeSeriesStore(new StringSerde("UTF-8"), true);
// insert an entry with key "hello" at timestamps "1" and "2"
timeSeriesStore.put("hello", "world-1".getBytes(), 1L);
timeSeriesStore.put("hello", "world-1".getBytes(), 2L);
timeSeriesStore.put("hello", "world-2".getBytes(), 2L);
// read from time-range
List<TimestampedValue<byte[]>> values = readStore(timeSeriesStore, "hello", 0L, 1L);
Assert.assertEquals(0, values.size());
// read from time-range [1,2) should return one entry
values = readStore(timeSeriesStore, "hello", 1L, 2L);
Assert.assertEquals(1, values.size());
Assert.assertEquals("world-1", new String(values.get(0).getValue()));
// read from time-range [2,3) should return two entries
values = readStore(timeSeriesStore, "hello", 2L, 3L);
Assert.assertEquals(2, values.size());
Assert.assertEquals("world-1", new String(values.get(0).getValue()));
Assert.assertEquals(2L, values.get(0).getTimestamp());
// read from time-range [0,3) should return three entries
values = readStore(timeSeriesStore, "hello", 0L, 3L);
Assert.assertEquals(3, values.size());
// read from time-range [2,999999) should return two entries
values = readStore(timeSeriesStore, "hello", 2L, 999999L);
Assert.assertEquals(2, values.size());
// read from time-range [3,4) should return no entries
values = readStore(timeSeriesStore, "hello", 3L, 4L);
Assert.assertEquals(0, values.size());
}
use of org.apache.samza.util.TimestampedValue in project samza by apache.
the class TestTimeSeriesStoreImpl method testGetOnTimestampBoundariesWithOverwriteMode.
@Test
public void testGetOnTimestampBoundariesWithOverwriteMode() {
// instantiate a store in overwrite mode
TimeSeriesStore<String, byte[]> timeSeriesStore = newTimeSeriesStore(new StringSerde("UTF-8"), false);
// insert an entry with key "hello" at timestamps "1" and "2"
timeSeriesStore.put("hello", "world-1".getBytes(), 1L);
timeSeriesStore.put("hello", "world-1".getBytes(), 2L);
timeSeriesStore.put("hello", "world-2".getBytes(), 2L);
// read from time-range
List<TimestampedValue<byte[]>> values = readStore(timeSeriesStore, "hello", 0L, 1L);
Assert.assertEquals(0, values.size());
// read from time-range [1,2) should return one entry
values = readStore(timeSeriesStore, "hello", 1L, 2L);
Assert.assertEquals(1, values.size());
Assert.assertEquals("world-1", new String(values.get(0).getValue()));
// read from time-range [2,3) should return the most recent entry
values = readStore(timeSeriesStore, "hello", 2L, 3L);
Assert.assertEquals(1, values.size());
Assert.assertEquals("world-2", new String(values.get(0).getValue()));
Assert.assertEquals(2L, values.get(0).getTimestamp());
// read from time-range [0,3) should return two entries
values = readStore(timeSeriesStore, "hello", 0L, 3L);
Assert.assertEquals(2, values.size());
// read from time-range [2,999999) should return one entry
values = readStore(timeSeriesStore, "hello", 2L, 999999L);
Assert.assertEquals(1, values.size());
// read from time-range [3,4) should return no entries
values = readStore(timeSeriesStore, "hello", 3L, 4L);
Assert.assertEquals(0, values.size());
}
use of org.apache.samza.util.TimestampedValue in project samza by apache.
the class TestOperatorImplGraph method testJoinChain.
@Test
public void testJoinChain() {
String inputStreamId1 = "input1";
String inputStreamId2 = "input2";
String inputSystem = "input-system";
String inputPhysicalName1 = "input-stream1";
String inputPhysicalName2 = "input-stream2";
HashMap<String, String> configs = new HashMap<>();
configs.put(JobConfig.JOB_NAME, "jobName");
configs.put(JobConfig.JOB_ID, "jobId");
StreamTestUtils.addStreamConfigs(configs, inputStreamId1, inputSystem, inputPhysicalName1);
StreamTestUtils.addStreamConfigs(configs, inputStreamId2, inputSystem, inputPhysicalName2);
Config config = new MapConfig(configs);
when(this.context.getJobContext().getConfig()).thenReturn(config);
Integer joinKey = new Integer(1);
Function<Object, Integer> keyFn = (Function & Serializable) m -> joinKey;
JoinFunction testJoinFunction = new TestJoinFunction("jobName-jobId-join-j1", (BiFunction & Serializable) (m1, m2) -> KV.of(m1, m2), keyFn, keyFn);
StreamApplicationDescriptorImpl graphSpec = new StreamApplicationDescriptorImpl(appDesc -> {
GenericSystemDescriptor sd = new GenericSystemDescriptor(inputSystem, "mockFactoryClass");
GenericInputDescriptor inputDescriptor1 = sd.getInputDescriptor(inputStreamId1, mock(Serde.class));
GenericInputDescriptor inputDescriptor2 = sd.getInputDescriptor(inputStreamId2, mock(Serde.class));
MessageStream<Object> inputStream1 = appDesc.getInputStream(inputDescriptor1);
MessageStream<Object> inputStream2 = appDesc.getInputStream(inputDescriptor2);
inputStream1.join(inputStream2, testJoinFunction, mock(Serde.class), mock(Serde.class), mock(Serde.class), Duration.ofHours(1), "j1");
}, config);
TaskName mockTaskName = mock(TaskName.class);
TaskModel taskModel = mock(TaskModel.class);
when(taskModel.getTaskName()).thenReturn(mockTaskName);
when(this.context.getTaskContext().getTaskModel()).thenReturn(taskModel);
KeyValueStore mockLeftStore = mock(KeyValueStore.class);
when(this.context.getTaskContext().getStore(eq("jobName-jobId-join-j1-L"))).thenReturn(mockLeftStore);
KeyValueStore mockRightStore = mock(KeyValueStore.class);
when(this.context.getTaskContext().getStore(eq("jobName-jobId-join-j1-R"))).thenReturn(mockRightStore);
OperatorImplGraph opImplGraph = new OperatorImplGraph(graphSpec.getOperatorSpecGraph(), this.context, mock(Clock.class));
// verify that join function is initialized once.
assertEquals(TestJoinFunction.getInstanceByTaskName(mockTaskName, "jobName-jobId-join-j1").numInitCalled, 1);
InputOperatorImpl inputOpImpl1 = opImplGraph.getInputOperator(new SystemStream(inputSystem, inputPhysicalName1));
InputOperatorImpl inputOpImpl2 = opImplGraph.getInputOperator(new SystemStream(inputSystem, inputPhysicalName2));
PartialJoinOperatorImpl leftPartialJoinOpImpl = (PartialJoinOperatorImpl) inputOpImpl1.registeredOperators.iterator().next();
PartialJoinOperatorImpl rightPartialJoinOpImpl = (PartialJoinOperatorImpl) inputOpImpl2.registeredOperators.iterator().next();
assertEquals(leftPartialJoinOpImpl.getOperatorSpec(), rightPartialJoinOpImpl.getOperatorSpec());
assertNotSame(leftPartialJoinOpImpl, rightPartialJoinOpImpl);
// verify that left partial join operator calls getFirstKey
Object mockLeftMessage = mock(Object.class);
long currentTimeMillis = System.currentTimeMillis();
when(mockLeftStore.get(eq(joinKey))).thenReturn(new TimestampedValue<>(mockLeftMessage, currentTimeMillis));
IncomingMessageEnvelope leftMessage = new IncomingMessageEnvelope(mock(SystemStreamPartition.class), "", "", mockLeftMessage);
inputOpImpl1.onMessage(leftMessage, mock(MessageCollector.class), mock(TaskCoordinator.class));
// verify that right partial join operator calls getSecondKey
Object mockRightMessage = mock(Object.class);
when(mockRightStore.get(eq(joinKey))).thenReturn(new TimestampedValue<>(mockRightMessage, currentTimeMillis));
IncomingMessageEnvelope rightMessage = new IncomingMessageEnvelope(mock(SystemStreamPartition.class), "", "", mockRightMessage);
inputOpImpl2.onMessage(rightMessage, mock(MessageCollector.class), mock(TaskCoordinator.class));
// verify that the join function apply is called with the correct messages on match
assertEquals(((TestJoinFunction) TestJoinFunction.getInstanceByTaskName(mockTaskName, "jobName-jobId-join-j1")).joinResults.size(), 1);
KV joinResult = (KV) ((TestJoinFunction) TestJoinFunction.getInstanceByTaskName(mockTaskName, "jobName-jobId-join-j1")).joinResults.iterator().next();
assertEquals(joinResult.getKey(), mockLeftMessage);
assertEquals(joinResult.getValue(), mockRightMessage);
}
Aggregations