Search in sources :

Example 21 with Change

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

the class KTableSuppressProcessorTest method finalResultsWithZeroGraceShouldStillBufferUntilTheWindowEnd.

/**
 * Testing a special case of final results: that even with a grace period of 0,
 * it will still buffer events and emit only after the end of the window.
 * As opposed to emitting immediately the way regular suppression would with a time limit of 0.
 */
@Test
public void finalResultsWithZeroGraceShouldStillBufferUntilTheWindowEnd() {
    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;
    // note the record is in the past, but the window end is in the future, so we still have to buffer,
    // even though the grace period is 0.
    final long timestamp = 5L;
    final long windowEnd = 100L;
    context.setRecordMetadata("", 0, 0L);
    context.setTimestamp(timestamp);
    final Windowed<String> key = new Windowed<>("hey", new TimeWindow(0, windowEnd));
    final Change<Long> value = ARBITRARY_CHANGE;
    harness.processor.process(new Record<>(key, value, timestamp));
    assertThat(context.forwarded(), hasSize(0));
    context.setRecordMetadata("", 0, 1L);
    context.setTimestamp(windowEnd);
    harness.processor.process(new Record<>(new Windowed<>("dummyKey", new TimeWindow(windowEnd, windowEnd + 100L)), ARBITRARY_CHANGE, windowEnd));
    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 22 with Change

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

the class KTableSuppressProcessorTest method suppressShouldShutDownWhenOverRecordCapacity.

@Test
public void suppressShouldShutDownWhenOverRecordCapacity() {
    final Harness<String, Long> harness = new Harness<>(untilTimeLimit(Duration.ofDays(100), maxRecords(1).shutDownWhenFull()), String(), Long());
    final MockInternalNewProcessorContext<String, Change<Long>> context = harness.context;
    final long timestamp = 100L;
    context.setRecordMetadata("", 0, 0L);
    context.setTimestamp(timestamp);
    context.setCurrentNode(new ProcessorNode("testNode"));
    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);
    try {
        harness.processor.process(new Record<>("dummyKey", value, timestamp));
        fail("expected an exception");
    } catch (final StreamsException e) {
        assertThat(e.getMessage(), containsString("buffer exceeded its max capacity"));
    }
}
Also used : ProcessorNode(org.apache.kafka.streams.processor.internals.ProcessorNode) StreamsException(org.apache.kafka.streams.errors.StreamsException) Long(org.apache.kafka.common.serialization.Serdes.Long) String(org.apache.kafka.common.serialization.Serdes.String) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Change(org.apache.kafka.streams.kstream.internals.Change) Test(org.junit.Test)

Example 23 with Change

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

the class KTableSuppressProcessorTest method finalResultsShouldDropTombstonesForTimeWindows.

/**
 * It's desirable to drop tombstones for final-results windowed streams, since (as described in the
 * {@link SuppressedInternal} javadoc), they are unnecessary to emit.
 */
@Test
public void finalResultsShouldDropTombstonesForTimeWindows() {
    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 = new Change<>(null, ARBITRARY_LONG);
    harness.processor.process(new Record<>(key, value, timestamp));
    assertThat(context.forwarded(), hasSize(0));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Long(org.apache.kafka.common.serialization.Serdes.Long) 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) Test(org.junit.Test)

Example 24 with Change

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

the class KTableSuppressProcessorTest method suppressShouldNotDropTombstonesForSessionWindows.

/**
 * It's NOT OK to drop tombstones for non-final-results windowed streams, since we may have emitted some results for
 * the window before getting the tombstone (see the {@link SuppressedInternal} javadoc).
 */
@Test
public void suppressShouldNotDropTombstonesForSessionWindows() {
    final Harness<Windowed<String>, Long> harness = new Harness<>(untilTimeLimit(ofMillis(0), maxRecords(0)), sessionWindowedSerdeFrom(String.class), 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 SessionWindow(0L, 0L));
    final Change<Long> value = new Change<>(null, ARBITRARY_LONG);
    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) 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) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Example 25 with Change

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

the class KTableSuppressProcessorTest method suppressShouldNotDropTombstonesForTimeWindows.

/**
 * It's NOT OK to drop tombstones for non-final-results windowed streams, since we may have emitted some results for
 * the window before getting the tombstone (see the {@link SuppressedInternal} javadoc).
 */
@Test
public void suppressShouldNotDropTombstonesForTimeWindows() {
    final Harness<Windowed<String>, Long> harness = new Harness<>(untilTimeLimit(ofMillis(0), maxRecords(0)), timeWindowedSerdeFrom(String.class, 100L), Long());
    final MockInternalNewProcessorContext<Windowed<String>, Change<Long>> context = harness.context;
    final long timestamp = 100L;
    final Headers headers = new RecordHeaders().add("k", "v".getBytes(StandardCharsets.UTF_8));
    context.setRecordMetadata("", 0, 0L);
    context.setTimestamp(timestamp);
    context.setHeaders(headers);
    final Windowed<String> key = new Windowed<>("hey", new TimeWindow(0L, 100L));
    final Change<Long> value = new Change<>(null, ARBITRARY_LONG);
    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, headers)));
}
Also used : Headers(org.apache.kafka.common.header.Headers) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) 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) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) 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