Search in sources :

Example 1 with Change

use of org.apache.kafka.streams.kstream.internals.Change in project kafka by apache.

the class CachingSessionStore method putAndMaybeForward.

private void putAndMaybeForward(final ThreadCache.DirtyEntry entry, final InternalProcessorContext<?, ?> context) {
    final Bytes binaryKey = cacheFunction.key(entry.key());
    final Windowed<Bytes> bytesKey = SessionKeySchema.from(binaryKey);
    if (flushListener != null) {
        final byte[] newValueBytes = entry.newValue();
        final byte[] oldValueBytes = newValueBytes == null || sendOldValues ? wrapped().fetchSession(bytesKey.key(), bytesKey.window().start(), bytesKey.window().end()) : null;
        // we can skip flushing to downstream as well as writing to underlying store
        if (newValueBytes != null || oldValueBytes != null) {
            // we need to get the old values if needed, and then put to store, and then flush
            wrapped().put(bytesKey, entry.newValue());
            final ProcessorRecordContext current = context.recordContext();
            context.setRecordContext(entry.entry().context());
            try {
                flushListener.apply(new Record<>(binaryKey.get(), new Change<>(newValueBytes, sendOldValues ? oldValueBytes : null), entry.entry().context().timestamp(), entry.entry().context().headers()));
            } finally {
                context.setRecordContext(current);
            }
        }
    } else {
        wrapped().put(bytesKey, entry.newValue());
    }
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) ProcessorRecordContext(org.apache.kafka.streams.processor.internals.ProcessorRecordContext) Change(org.apache.kafka.streams.kstream.internals.Change)

Example 2 with Change

use of org.apache.kafka.streams.kstream.internals.Change in project kafka by apache.

the class KTableSuppressProcessorTest method finalResultsSuppressionShouldBufferAndEmitAtGraceExpiration.

@Test
public void finalResultsSuppressionShouldBufferAndEmitAtGraceExpiration() {
    final Harness<Windowed<String>, Long> harness = new Harness<>(finalResults(ofMillis(1L)), timeWindowedSerdeFrom(String.class, 1L), Long());
    final MockInternalNewProcessorContext<Windowed<String>, Change<Long>> context = harness.context;
    final long windowStart = 99L;
    final long recordTime = 99L;
    final long windowEnd = 100L;
    context.setRecordMetadata("topic", 0, 0);
    context.setTimestamp(recordTime);
    final Windowed<String> key = new Windowed<>("hey", new TimeWindow(windowStart, windowEnd));
    final Change<Long> value = ARBITRARY_CHANGE;
    harness.processor.process(new Record<>(key, value, recordTime));
    assertThat(context.forwarded(), hasSize(0));
    // although the stream time is now 100, we have to wait 1 ms after the window *end* before we
    // emit "hey", so we don't emit yet.
    final long windowStart2 = 100L;
    final long recordTime2 = 100L;
    final long windowEnd2 = 101L;
    context.setRecordMetadata("topic", 0, 1);
    context.setTimestamp(recordTime2);
    harness.processor.process(new Record<>(new Windowed<>("dummyKey1", new TimeWindow(windowStart2, windowEnd2)), ARBITRARY_CHANGE, recordTime2));
    assertThat(context.forwarded(), hasSize(0));
    // ok, now it's time to emit "hey"
    final long windowStart3 = 101L;
    final long recordTime3 = 101L;
    final long windowEnd3 = 102L;
    context.setRecordMetadata("topic", 0, 1);
    context.setTimestamp(recordTime3);
    harness.processor.process(new Record<>(new Windowed<>("dummyKey2", new TimeWindow(windowStart3, windowEnd3)), ARBITRARY_CHANGE, recordTime3));
    assertThat(context.forwarded(), hasSize(1));
    final MockProcessorContext.CapturedForward capturedForward = context.forwarded().get(0);
    assertThat(capturedForward.record(), is(new Record<>(key, value, recordTime)));
}
Also used : String(org.apache.kafka.common.serialization.Serdes.String) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Change(org.apache.kafka.streams.kstream.internals.Change) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) MockProcessorContext(org.apache.kafka.streams.processor.api.MockProcessorContext) Windowed(org.apache.kafka.streams.kstream.Windowed) Long(org.apache.kafka.common.serialization.Serdes.Long) Record(org.apache.kafka.streams.processor.api.Record) Test(org.junit.Test)

Example 3 with Change

use of org.apache.kafka.streams.kstream.internals.Change in project kafka by apache.

the class KTableSuppressProcessorTest method windowedZeroTimeLimitShouldImmediatelyEmit.

@Test
public void windowedZeroTimeLimitShouldImmediatelyEmit() {
    final Harness<Windowed<String>, Long> harness = new Harness<>(untilTimeLimit(ZERO, unbounded()), timeWindowedSerdeFrom(String.class, 100L), Long());
    final MockInternalNewProcessorContext<Windowed<String>, Change<Long>> context = harness.context;
    final long timestamp = ARBITRARY_LONG;
    context.setRecordMetadata("", 0, 0L);
    context.setTimestamp(timestamp);
    final Windowed<String> key = new Windowed<>("hey", new TimeWindow(0L, 100L));
    final Change<Long> value = ARBITRARY_CHANGE;
    harness.processor.process(new Record<>(key, value, timestamp));
    assertThat(context.forwarded(), hasSize(1));
    final MockProcessorContext.CapturedForward capturedForward = context.forwarded().get(0);
    assertThat(capturedForward.record(), is(new Record<>(key, value, timestamp)));
}
Also used : String(org.apache.kafka.common.serialization.Serdes.String) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Change(org.apache.kafka.streams.kstream.internals.Change) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) MockProcessorContext(org.apache.kafka.streams.processor.api.MockProcessorContext) Windowed(org.apache.kafka.streams.kstream.Windowed) Long(org.apache.kafka.common.serialization.Serdes.Long) Record(org.apache.kafka.streams.processor.api.Record) Test(org.junit.Test)

Example 4 with Change

use of org.apache.kafka.streams.kstream.internals.Change in project kafka by apache.

the class KTableSuppressProcessorTest method suppressShouldEmitWhenOverRecordCapacity.

@Test
public void suppressShouldEmitWhenOverRecordCapacity() {
    final Harness<String, Long> harness = new Harness<>(untilTimeLimit(Duration.ofDays(100), maxRecords(1)), String(), Long());
    final MockInternalNewProcessorContext<String, Change<Long>> context = harness.context;
    final long timestamp = 100L;
    context.setRecordMetadata("", 0, 0L);
    context.setTimestamp(timestamp);
    final String key = "hey";
    final Change<Long> value = new Change<>(null, ARBITRARY_LONG);
    harness.processor.process(new Record<>(key, value, timestamp));
    context.setRecordMetadata("", 0, 1L);
    context.setTimestamp(timestamp + 1);
    harness.processor.process(new Record<>("dummyKey", value, timestamp + 1));
    assertThat(context.forwarded(), hasSize(1));
    final MockProcessorContext.CapturedForward capturedForward = context.forwarded().get(0);
    assertThat(capturedForward.record(), is(new Record<>(key, value, timestamp)));
}
Also used : Long(org.apache.kafka.common.serialization.Serdes.Long) Record(org.apache.kafka.streams.processor.api.Record) String(org.apache.kafka.common.serialization.Serdes.String) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Change(org.apache.kafka.streams.kstream.internals.Change) MockProcessorContext(org.apache.kafka.streams.processor.api.MockProcessorContext) Test(org.junit.Test)

Example 5 with Change

use of org.apache.kafka.streams.kstream.internals.Change in project kafka by apache.

the class KTableSuppressProcessorTest method finalResultsWithZeroGraceAtWindowEndShouldImmediatelyEmit.

@Test
public void finalResultsWithZeroGraceAtWindowEndShouldImmediatelyEmit() {
    final Harness<Windowed<String>, Long> harness = new Harness<>(finalResults(ofMillis(0L)), timeWindowedSerdeFrom(String.class, 100L), Long());
    final MockInternalNewProcessorContext<Windowed<String>, Change<Long>> context = harness.context;
    final long timestamp = 100L;
    context.setRecordMetadata("", 0, 0L);
    context.setTimestamp(timestamp);
    final Windowed<String> key = new Windowed<>("hey", new TimeWindow(0, 100L));
    final Change<Long> value = ARBITRARY_CHANGE;
    harness.processor.process(new Record<>(key, value, timestamp));
    assertThat(context.forwarded(), hasSize(1));
    final MockProcessorContext.CapturedForward capturedForward = context.forwarded().get(0);
    assertThat(capturedForward.record(), is(new Record<>(key, value, timestamp)));
}
Also used : String(org.apache.kafka.common.serialization.Serdes.String) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Change(org.apache.kafka.streams.kstream.internals.Change) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) MockProcessorContext(org.apache.kafka.streams.processor.api.MockProcessorContext) Windowed(org.apache.kafka.streams.kstream.Windowed) Long(org.apache.kafka.common.serialization.Serdes.Long) Record(org.apache.kafka.streams.processor.api.Record) Test(org.junit.Test)

Aggregations

Change (org.apache.kafka.streams.kstream.internals.Change)28 Test (org.junit.Test)23 Long (org.apache.kafka.common.serialization.Serdes.Long)15 String (org.apache.kafka.common.serialization.Serdes.String)15 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)15 Record (org.apache.kafka.streams.processor.api.Record)12 MockProcessorContext (org.apache.kafka.streams.processor.api.MockProcessorContext)11 ProcessorRecordContext (org.apache.kafka.streams.processor.internals.ProcessorRecordContext)10 Windowed (org.apache.kafka.streams.kstream.Windowed)8 MockInternalProcessorContext (org.apache.kafka.test.MockInternalProcessorContext)7 LinkedList (java.util.LinkedList)6 RecordHeaders (org.apache.kafka.common.header.internals.RecordHeaders)6 TimeWindow (org.apache.kafka.streams.kstream.internals.TimeWindow)6 Eviction (org.apache.kafka.streams.state.internals.TimeOrderedKeyValueBuffer.Eviction)6 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)5 RecordBatchingStateRestoreCallback (org.apache.kafka.streams.processor.internals.RecordBatchingStateRestoreCallback)5 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)4 Bytes (org.apache.kafka.common.utils.Bytes)3 ProcessorNode (org.apache.kafka.streams.processor.internals.ProcessorNode)3 StreamsException (org.apache.kafka.streams.errors.StreamsException)2