Search in sources :

Example 1 with Position

use of org.apache.kafka.streams.query.Position in project kafka by apache.

the class RocksDBWindowStoreTest method shouldMatchPositionAfterPut.

@Test
public void shouldMatchPositionAfterPut() {
    final MeteredWindowStore<Integer, String> meteredSessionStore = (MeteredWindowStore<Integer, String>) windowStore;
    final ChangeLoggingWindowBytesStore changeLoggingSessionBytesStore = (ChangeLoggingWindowBytesStore) meteredSessionStore.wrapped();
    final RocksDBWindowStore rocksDBWindowStore = (RocksDBWindowStore) changeLoggingSessionBytesStore.wrapped();
    context.setRecordContext(new ProcessorRecordContext(0, 1, 0, "", new RecordHeaders()));
    windowStore.put(0, "0", SEGMENT_INTERVAL);
    context.setRecordContext(new ProcessorRecordContext(0, 2, 0, "", new RecordHeaders()));
    windowStore.put(1, "1", SEGMENT_INTERVAL);
    context.setRecordContext(new ProcessorRecordContext(0, 3, 0, "", new RecordHeaders()));
    windowStore.put(2, "2", SEGMENT_INTERVAL);
    context.setRecordContext(new ProcessorRecordContext(0, 4, 0, "", new RecordHeaders()));
    windowStore.put(3, "3", SEGMENT_INTERVAL);
    final Position expected = Position.fromMap(mkMap(mkEntry("", mkMap(mkEntry(0, 4L)))));
    final Position actual = rocksDBWindowStore.getPosition();
    assertEquals(expected, actual);
}
Also used : RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ProcessorRecordContext(org.apache.kafka.streams.processor.internals.ProcessorRecordContext) Position(org.apache.kafka.streams.query.Position) Test(org.junit.Test)

Example 2 with Position

use of org.apache.kafka.streams.query.Position in project kafka by apache.

the class StoreQueryUtilsTest method shouldReturnErrorOnNullContext.

@Test
public void shouldReturnErrorOnNullContext() {
    @SuppressWarnings("unchecked") final KeyQuery<String, Integer> query = Mockito.mock(KeyQuery.class);
    @SuppressWarnings("unchecked") final KeyValueStore<String, Integer> store = Mockito.mock(KeyValueStore.class);
    final Position position = Position.emptyPosition().withComponent("topic", 0, 1);
    final QueryResult<Integer> queryResult = StoreQueryUtils.handleBasicQueries(query, PositionBound.at(position), new QueryConfig(false), store, position, null);
    assertThat(queryResult.isFailure(), is(true));
    assertThat(queryResult.getFailureReason(), is(FailureReason.NOT_UP_TO_BOUND));
    assertThat(queryResult.getFailureMessage(), is("The store is not initialized yet, so it is not yet up to the bound" + " PositionBound{position=Position{position={topic={0=1}}}}"));
}
Also used : QueryConfig(org.apache.kafka.streams.query.QueryConfig) Position(org.apache.kafka.streams.query.Position) Test(org.junit.Test)

Example 3 with Position

use of org.apache.kafka.streams.query.Position in project kafka by apache.

the class AbstractRocksDBSegmentedBytesStoreTest method getChangelogRecordsMultipleTopics.

private List<ConsumerRecord<byte[], byte[]>> getChangelogRecordsMultipleTopics() {
    final List<ConsumerRecord<byte[], byte[]>> records = new ArrayList<>();
    final Headers headers = new RecordHeaders();
    Position position1 = Position.emptyPosition();
    position1 = position1.withComponent("A", 0, 1);
    headers.add(ChangelogRecordDeserializationHelper.CHANGELOG_VERSION_HEADER_RECORD_CONSISTENCY);
    headers.add(new RecordHeader(ChangelogRecordDeserializationHelper.CHANGELOG_POSITION_HEADER_KEY, PositionSerde.serialize(position1).array()));
    records.add(new ConsumerRecord<>("", 0, 0L, RecordBatch.NO_TIMESTAMP, TimestampType.NO_TIMESTAMP_TYPE, -1, -1, serializeKey(new Windowed<>("a", windows[0])).get(), serializeValue(50L), headers, Optional.empty()));
    headers.remove(ChangelogRecordDeserializationHelper.CHANGELOG_POSITION_HEADER_KEY);
    position1 = position1.withComponent("B", 0, 2);
    headers.add(new RecordHeader(ChangelogRecordDeserializationHelper.CHANGELOG_POSITION_HEADER_KEY, PositionSerde.serialize(position1).array()));
    records.add(new ConsumerRecord<>("", 0, 0L, RecordBatch.NO_TIMESTAMP, TimestampType.NO_TIMESTAMP_TYPE, -1, -1, serializeKey(new Windowed<>("a", windows[2])).get(), serializeValue(100L), headers, Optional.empty()));
    headers.remove(ChangelogRecordDeserializationHelper.CHANGELOG_POSITION_HEADER_KEY);
    position1 = position1.withComponent("A", 0, 3);
    headers.add(new RecordHeader(ChangelogRecordDeserializationHelper.CHANGELOG_POSITION_HEADER_KEY, PositionSerde.serialize(position1).array()));
    records.add(new ConsumerRecord<>("", 0, 0L, RecordBatch.NO_TIMESTAMP, TimestampType.NO_TIMESTAMP_TYPE, -1, -1, serializeKey(new Windowed<>("a", windows[3])).get(), serializeValue(200L), headers, Optional.empty()));
    return records;
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) Position(org.apache.kafka.streams.query.Position) Headers(org.apache.kafka.common.header.Headers) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ArrayList(java.util.ArrayList) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord)

Example 4 with Position

use of org.apache.kafka.streams.query.Position in project kafka by apache.

the class ChangeLoggingKeyValueBytesStoreTest method shouldLogPositionOnPut.

@Test
public void shouldLogPositionOnPut() {
    context.setRecordContext(new ProcessorRecordContext(-1, INPUT_OFFSET, INPUT_PARTITION, INPUT_TOPIC_NAME, new RecordHeaders()));
    context.setTime(1L);
    store.put(hi, there);
    assertThat(collector.collected().size(), equalTo(1));
    assertThat(collector.collected().get(0).headers(), is(notNullValue()));
    final Header versionHeader = collector.collected().get(0).headers().lastHeader(ChangelogRecordDeserializationHelper.CHANGELOG_VERSION_HEADER_KEY);
    assertThat(versionHeader, is(notNullValue()));
    assertThat(versionHeader.equals(ChangelogRecordDeserializationHelper.CHANGELOG_VERSION_HEADER_RECORD_CONSISTENCY), is(true));
    final Header vectorHeader = collector.collected().get(0).headers().lastHeader(ChangelogRecordDeserializationHelper.CHANGELOG_POSITION_HEADER_KEY);
    assertThat(vectorHeader, is(notNullValue()));
    final Position position = PositionSerde.deserialize(ByteBuffer.wrap(vectorHeader.value()));
    assertThat(position.getPartitionPositions(INPUT_TOPIC_NAME), is(notNullValue()));
    assertThat(position.getPartitionPositions(INPUT_TOPIC_NAME), hasEntry(0, 100L));
}
Also used : RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ProcessorRecordContext(org.apache.kafka.streams.processor.internals.ProcessorRecordContext) Header(org.apache.kafka.common.header.Header) Position(org.apache.kafka.streams.query.Position) Test(org.junit.Test)

Example 5 with Position

use of org.apache.kafka.streams.query.Position in project kafka by apache.

the class ChangeLoggingWindowBytesStoreTest method shouldLogPutsWithPosition.

@Test
public void shouldLogPutsWithPosition() {
    EasyMock.expect(inner.getPosition()).andReturn(POSITION).anyTimes();
    inner.put(bytesKey, value, 0);
    EasyMock.expectLastCall();
    init();
    final Bytes key = WindowKeySchema.toStoreKeyBinary(bytesKey, 0, 0);
    EasyMock.reset(context);
    final RecordMetadata recordContext = new ProcessorRecordContext(0L, 1L, 0, "", new RecordHeaders());
    EasyMock.expect(context.recordMetadata()).andStubReturn(Optional.of(recordContext));
    EasyMock.expect(context.timestamp()).andStubReturn(0L);
    final Position position = Position.fromMap(mkMap(mkEntry("", mkMap(mkEntry(0, 1L)))));
    context.logChange(store.name(), key, value, 0L, position);
    EasyMock.replay(context);
    store.put(bytesKey, value, context.timestamp());
    EasyMock.verify(inner, context);
}
Also used : RecordMetadata(org.apache.kafka.streams.processor.api.RecordMetadata) Bytes(org.apache.kafka.common.utils.Bytes) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ProcessorRecordContext(org.apache.kafka.streams.processor.internals.ProcessorRecordContext) Position(org.apache.kafka.streams.query.Position) Test(org.junit.Test)

Aggregations

Position (org.apache.kafka.streams.query.Position)27 RecordHeaders (org.apache.kafka.common.header.internals.RecordHeaders)17 Test (org.junit.Test)17 ProcessorRecordContext (org.apache.kafka.streams.processor.internals.ProcessorRecordContext)11 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)7 Bytes (org.apache.kafka.common.utils.Bytes)7 Windowed (org.apache.kafka.streams.kstream.Windowed)7 Headers (org.apache.kafka.common.header.Headers)6 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)5 Map (java.util.Map)4 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)4 StateStore (org.apache.kafka.streams.processor.StateStore)4 StateStoreContext (org.apache.kafka.streams.processor.StateStoreContext)4 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 NoSuchElementException (java.util.NoSuchElementException)3 Objects (java.util.Objects)3 Set (java.util.Set)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConcurrentNavigableMap (java.util.concurrent.ConcurrentNavigableMap)3