Search in sources :

Example 1 with CHANGELOG_HEADERS

use of org.apache.kafka.streams.state.internals.InMemoryTimeOrderedKeyValueBuffer.CHANGELOG_HEADERS in project kafka by apache.

the class TimeOrderedKeyValueBufferTest method shouldFlush.

@Test
public void shouldFlush() {
    final TimeOrderedKeyValueBuffer<String, String> buffer = bufferSupplier.apply(testName);
    final MockInternalProcessorContext context = makeContext();
    buffer.init((StateStoreContext) context, buffer);
    putRecord(buffer, context, 2L, 0L, "asdf", "2093j");
    putRecord(buffer, context, 1L, 1L, "zxcv", "3gon4i");
    putRecord(buffer, context, 0L, 2L, "deleteme", "deadbeef");
    // replace "deleteme" with a tombstone
    buffer.evictWhile(() -> buffer.minTimestamp() < 1, kv -> {
    });
    // flush everything to the changelog
    buffer.flush();
    // the buffer should serialize the buffer time and the value as byte[],
    // which we can't compare for equality using ProducerRecord.
    // As a workaround, I'm deserializing them and shoving them in a KeyValue, just for ease of testing.
    final List<ProducerRecord<String, KeyValue<Long, BufferValue>>> collected = ((MockRecordCollector) context.recordCollector()).collected().stream().map(pr -> {
        final KeyValue<Long, BufferValue> niceValue;
        if (pr.value() == null) {
            niceValue = null;
        } else {
            final byte[] serializedValue = (byte[]) pr.value();
            final ByteBuffer valueBuffer = ByteBuffer.wrap(serializedValue);
            final BufferValue contextualRecord = BufferValue.deserialize(valueBuffer);
            final long timestamp = valueBuffer.getLong();
            niceValue = new KeyValue<>(timestamp, contextualRecord);
        }
        return new ProducerRecord<>(pr.topic(), pr.partition(), pr.timestamp(), pr.key().toString(), niceValue, pr.headers());
    }).collect(Collectors.toList());
    assertThat(collected, is(asList(new ProducerRecord<>(APP_ID + "-" + testName + "-changelog", // Producer will assign
    0, null, "deleteme", null, new RecordHeaders()), new ProducerRecord<>(APP_ID + "-" + testName + "-changelog", 0, null, "zxcv", new KeyValue<>(1L, getBufferValue("3gon4i", 1)), CHANGELOG_HEADERS), new ProducerRecord<>(APP_ID + "-" + testName + "-changelog", 0, null, "asdf", new KeyValue<>(2L, getBufferValue("2093j", 0)), CHANGELOG_HEADERS))));
    cleanup(context, buffer);
}
Also used : StreamsConfig(org.apache.kafka.streams.StreamsConfig) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) TaskId(org.apache.kafka.streams.processor.TaskId) ProcessorRecordContext(org.apache.kafka.streams.processor.internals.ProcessorRecordContext) RunWith(org.junit.runner.RunWith) Random(java.util.Random) Eviction(org.apache.kafka.streams.state.internals.TimeOrderedKeyValueBuffer.Eviction) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) Function(java.util.function.Function) RecordBatchingStateRestoreCallback(org.apache.kafka.streams.processor.internals.RecordBatchingStateRestoreCallback) ByteBuffer(java.nio.ByteBuffer) Collections.singletonList(java.util.Collections.singletonList) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) StateStoreContext(org.apache.kafka.streams.processor.StateStoreContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) Serdes(org.apache.kafka.common.serialization.Serdes) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Record(org.apache.kafka.streams.processor.api.Record) Assert.fail(org.junit.Assert.fail) MockRecordCollector(org.apache.kafka.test.MockRecordCollector) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LinkedList(java.util.LinkedList) TimestampType(org.apache.kafka.common.record.TimestampType) Parameterized(org.junit.runners.Parameterized) Utils(org.apache.kafka.common.utils.Utils) Properties(java.util.Properties) TestUtils(org.apache.kafka.test.TestUtils) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Collection(java.util.Collection) KeyValue(org.apache.kafka.streams.KeyValue) CHANGELOG_HEADERS(org.apache.kafka.streams.state.internals.InMemoryTimeOrderedKeyValueBuffer.CHANGELOG_HEADERS) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) MockInternalProcessorContext(org.apache.kafka.test.MockInternalProcessorContext) List(java.util.List) Header(org.apache.kafka.common.header.Header) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Change(org.apache.kafka.streams.kstream.internals.Change) KeyValue(org.apache.kafka.streams.KeyValue) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) MockInternalProcessorContext(org.apache.kafka.test.MockInternalProcessorContext) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 Collections.singletonList (java.util.Collections.singletonList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 Random (java.util.Random)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)1 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)1 Header (org.apache.kafka.common.header.Header)1 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)1 RecordHeaders (org.apache.kafka.common.header.internals.RecordHeaders)1 TimestampType (org.apache.kafka.common.record.TimestampType)1