Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MINOR: Session windows should accept zero as session gap #18734

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -79,12 +79,12 @@ private SessionWindows(final long gapMs, final long graceMs) {
this.gapMs = gapMs;
this.graceMs = graceMs;

if (gapMs <= 0) {
throw new IllegalArgumentException("Gap time cannot be zero or negative.");
if (gapMs < 0) {
throw new IllegalArgumentException("Gap time cannot be negative.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we should update the javadocs for the public APIs to define the inactivity gap semantics/boundaries

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JavaDocs. Good catch!

Did briefly look into docs. Did not found anything that we would need to update.

}

if (graceMs < 0) {
throw new IllegalArgumentException("Grace period must not be negative.");
throw new IllegalArgumentException("Grace period cannot be negative.");
}
}

Original file line number Diff line number Diff line change
@@ -66,13 +66,13 @@ public void withGraceAPIShouldSetGracePeriod() {
}

@Test
public void windowSizeMustNotBeNegative() {
public void sessionGapCannotBeNegative() {
assertThrows(IllegalArgumentException.class, () -> SessionWindows.ofInactivityGapWithNoGrace(ofMillis(-1)));
}

@Test
public void windowSizeMustNotBeZero() {
assertThrows(IllegalArgumentException.class, () -> SessionWindows.ofInactivityGapWithNoGrace(ofMillis(0)));
public void sessionGapCanBeZero() {
SessionWindows.ofInactivityGapWithNoGrace(ofMillis(0));
}

@Test
Original file line number Diff line number Diff line change
@@ -48,6 +48,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
@@ -371,6 +372,40 @@ public void shouldAggregateSessionWindowsWithInternalStoreName() {
doAggregateSessionWindows(supplier);
}

@Test
public void sessionGapOfZeroShouldOnlyPutRecordsWithSameTsIntoSameSession() {
final MockApiProcessorSupplier<Windowed<String>, Integer, Void, Void> supplier = new MockApiProcessorSupplier<>();
final KTable<Windowed<String>, Integer> table = groupedStream
.windowedBy(SessionWindows.ofInactivityGapWithNoGrace(Duration.ZERO))
.aggregate(
() -> 0,
(aggKey, value, aggregate) -> aggregate + 1,
(aggKey, aggOne, aggTwo) -> aggOne + aggTwo,
Materialized.with(null, Serdes.Integer()));
table.toStream().process(supplier);

try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<String, String> inputTopic =
driver.createInputTopic(TOPIC, new StringSerializer(), new StringSerializer());
inputTopic.pipeInput("1", "1", 10);
inputTopic.pipeInput("1", "1", 11);
inputTopic.pipeInput("1", "1", 11);
inputTopic.pipeInput("1", "1", 12);
}

final Map<Windowed<String>, ValueAndTimestamp<Integer>> result
= supplier.theCapturedProcessor().lastValueAndTimestampPerKey();
assertEquals(
ValueAndTimestamp.make(1, 10),
result.get(new Windowed<>("1", new SessionWindow(10L, 10L))));
assertEquals(
ValueAndTimestamp.make(2, 11L),
result.get(new Windowed<>("1", new SessionWindow(11L, 11L))));
assertEquals(
ValueAndTimestamp.make(1, 12L),
result.get(new Windowed<>("1", new SessionWindow(12L, 12L))));
}

private void doCountSessionWindows(final MockApiProcessorSupplier<Windowed<String>, Long, Void, Void> supplier) {
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<String, String> inputTopic =