use of org.apache.kafka.streams.kstream.internals.SessionWindow in project kafka by apache.
the class MeteredSessionStoreTest method shouldRemoveFromStoreAndRecordRemoveMetric.
@Test
public void shouldRemoveFromStoreAndRecordRemoveMetric() {
innerStore.remove(WINDOWED_KEY_BYTES);
expectLastCall();
init();
store.remove(new Windowed<>(KEY, new SessionWindow(0, 0)));
// it suffices to verify one remove metric since all remove metrics are recorded by the same sensor
// and the sensor is tested elsewhere
final KafkaMetric metric = metric("remove-rate");
assertTrue((Double) metric.metricValue() > 0);
verify(innerStore);
}
use of org.apache.kafka.streams.kstream.internals.SessionWindow in project kafka by apache.
the class KStreamAggregationIntegrationTest method shouldCountSessionWindows.
@SuppressWarnings("deprecation")
@Test
public void shouldCountSessionWindows() throws Exception {
final long sessionGap = 5 * 60 * 1000L;
final List<KeyValue<String, String>> t1Messages = Arrays.asList(new KeyValue<>("bob", "start"), new KeyValue<>("penny", "start"), new KeyValue<>("jo", "pause"), new KeyValue<>("emily", "pause"));
final long t1 = mockTime.milliseconds() - TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS);
IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(userSessionsStream, t1Messages, TestUtils.producerConfig(CLUSTER.bootstrapServers(), StringSerializer.class, StringSerializer.class, new Properties()), t1);
final long t2 = t1 + (sessionGap / 2);
IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(userSessionsStream, Collections.singletonList(new KeyValue<>("emily", "resume")), TestUtils.producerConfig(CLUSTER.bootstrapServers(), StringSerializer.class, StringSerializer.class, new Properties()), t2);
final long t3 = t1 + sessionGap + 1;
IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(userSessionsStream, Arrays.asList(new KeyValue<>("bob", "pause"), new KeyValue<>("penny", "stop")), TestUtils.producerConfig(CLUSTER.bootstrapServers(), StringSerializer.class, StringSerializer.class, new Properties()), t3);
final long t4 = t3 + (sessionGap / 2);
IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(userSessionsStream, Arrays.asList(// bobs session continues
new KeyValue<>("bob", "resume"), // jo's starts new session
new KeyValue<>("jo", "resume")), TestUtils.producerConfig(CLUSTER.bootstrapServers(), StringSerializer.class, StringSerializer.class, new Properties()), t4);
final long t5 = t4 - 1;
IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(userSessionsStream, Collections.singletonList(// jo has late arrival
new KeyValue<>("jo", "late")), TestUtils.producerConfig(CLUSTER.bootstrapServers(), StringSerializer.class, StringSerializer.class, new Properties()), t5);
final Map<Windowed<String>, KeyValue<Long, Long>> results = new HashMap<>();
final CountDownLatch latch = new CountDownLatch(13);
// noinspection deprecation
builder.stream(userSessionsStream, Consumed.with(Serdes.String(), Serdes.String())).groupByKey(Grouped.with(Serdes.String(), Serdes.String())).windowedBy(SessionWindows.with(ofMillis(sessionGap))).count().toStream().transform(() -> new Transformer<Windowed<String>, Long, KeyValue<Object, Object>>() {
private ProcessorContext context;
@Override
public void init(final ProcessorContext context) {
this.context = context;
}
@Override
public KeyValue<Object, Object> transform(final Windowed<String> key, final Long value) {
results.put(key, KeyValue.pair(value, context.timestamp()));
latch.countDown();
return null;
}
@Override
public void close() {
}
});
startStreams();
latch.await(30, TimeUnit.SECONDS);
assertThat(results.get(new Windowed<>("bob", new SessionWindow(t1, t1))), equalTo(KeyValue.pair(1L, t1)));
assertThat(results.get(new Windowed<>("penny", new SessionWindow(t1, t1))), equalTo(KeyValue.pair(1L, t1)));
assertThat(results.get(new Windowed<>("jo", new SessionWindow(t1, t1))), equalTo(KeyValue.pair(1L, t1)));
assertThat(results.get(new Windowed<>("jo", new SessionWindow(t5, t4))), equalTo(KeyValue.pair(2L, t4)));
assertThat(results.get(new Windowed<>("emily", new SessionWindow(t1, t2))), equalTo(KeyValue.pair(2L, t2)));
assertThat(results.get(new Windowed<>("bob", new SessionWindow(t3, t4))), equalTo(KeyValue.pair(2L, t4)));
assertThat(results.get(new Windowed<>("penny", new SessionWindow(t3, t3))), equalTo(KeyValue.pair(1L, t3)));
}
use of org.apache.kafka.streams.kstream.internals.SessionWindow in project kafka by apache.
the class PositionRestartIntegrationTest method setUpSessionPAPITopology.
private void setUpSessionPAPITopology(final SessionBytesStoreSupplier supplier, final StreamsBuilder builder) {
final StoreBuilder<?> sessionStoreStoreBuilder;
final ProcessorSupplier<Integer, Integer, Void, Void> processorSupplier;
sessionStoreStoreBuilder = Stores.sessionStoreBuilder(supplier, Serdes.Integer(), Serdes.Integer());
processorSupplier = () -> new ContextualProcessor<Integer, Integer, Void, Void>() {
@Override
public void process(final Record<Integer, Integer> record) {
final SessionStore<Integer, Integer> stateStore = context().getStateStore(sessionStoreStoreBuilder.name());
stateStore.put(new Windowed<>(record.key(), new SessionWindow(WINDOW_START, WINDOW_START)), record.value());
}
};
if (cache) {
sessionStoreStoreBuilder.withCachingEnabled();
} else {
sessionStoreStoreBuilder.withCachingDisabled();
}
if (log) {
sessionStoreStoreBuilder.withLoggingEnabled(Collections.emptyMap());
} else {
sessionStoreStoreBuilder.withLoggingDisabled();
}
builder.addStateStore(sessionStoreStoreBuilder);
builder.stream(INPUT_TOPIC_NAME, Consumed.with(Serdes.Integer(), Serdes.Integer())).process(processorSupplier, sessionStoreStoreBuilder.name());
}
use of org.apache.kafka.streams.kstream.internals.SessionWindow in project kafka by apache.
the class IQv2StoreIntegrationTest method setUpSessionPAPITopology.
private void setUpSessionPAPITopology(final SessionBytesStoreSupplier supplier, final StreamsBuilder builder) {
final StoreBuilder<?> sessionStoreStoreBuilder;
final ProcessorSupplier<Integer, Integer, Void, Void> processorSupplier;
sessionStoreStoreBuilder = Stores.sessionStoreBuilder(supplier, Serdes.Integer(), Serdes.Integer());
processorSupplier = () -> new ContextualProcessor<Integer, Integer, Void, Void>() {
@Override
public void process(final Record<Integer, Integer> record) {
final SessionStore<Integer, Integer> stateStore = context().getStateStore(sessionStoreStoreBuilder.name());
stateStore.put(new Windowed<>(record.key(), new SessionWindow(WINDOW_START, WINDOW_START)), record.value());
}
};
if (cache) {
sessionStoreStoreBuilder.withCachingEnabled();
} else {
sessionStoreStoreBuilder.withCachingDisabled();
}
if (log) {
sessionStoreStoreBuilder.withLoggingEnabled(Collections.emptyMap());
} else {
sessionStoreStoreBuilder.withLoggingDisabled();
}
if (storeToTest.global()) {
builder.addGlobalStore(sessionStoreStoreBuilder, INPUT_TOPIC_NAME, Consumed.with(Serdes.Integer(), Serdes.Integer()), processorSupplier);
} else {
builder.addStateStore(sessionStoreStoreBuilder);
builder.stream(INPUT_TOPIC_NAME, Consumed.with(Serdes.Integer(), Serdes.Integer())).process(processorSupplier, sessionStoreStoreBuilder.name());
}
}
use of org.apache.kafka.streams.kstream.internals.SessionWindow in project kafka by apache.
the class AbstractSessionBytesStoreTest method shouldPutAndFindSessionsInRange.
@Test
public void shouldPutAndFindSessionsInRange() {
final String key = "a";
final Windowed<String> a1 = new Windowed<>(key, new SessionWindow(10, 10L));
final Windowed<String> a2 = new Windowed<>(key, new SessionWindow(500L, 1000L));
sessionStore.put(a1, 1L);
sessionStore.put(a2, 2L);
sessionStore.put(new Windowed<>(key, new SessionWindow(1500L, 2000L)), 1L);
sessionStore.put(new Windowed<>(key, new SessionWindow(2500L, 3000L)), 2L);
final List<KeyValue<Windowed<String>, Long>> expected = Arrays.asList(KeyValue.pair(a1, 1L), KeyValue.pair(a2, 2L));
try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.findSessions(key, 0, 1000L)) {
assertEquals(expected, toList(values));
}
final List<KeyValue<Windowed<String>, Long>> expected2 = Collections.singletonList(KeyValue.pair(a2, 2L));
try (final KeyValueIterator<Windowed<String>, Long> values2 = sessionStore.findSessions(key, 400L, 600L)) {
assertEquals(expected2, toList(values2));
}
}
Aggregations