Search in sources :

Example 31 with SessionWindow

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);
}
Also used : SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) Test(org.junit.Test)

Example 32 with SessionWindow

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)));
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) HashMap(java.util.HashMap) Properties(java.util.Properties) CountDownLatch(java.util.concurrent.CountDownLatch) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) Windowed(org.apache.kafka.streams.kstream.Windowed) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Example 33 with SessionWindow

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());
}
Also used : SessionStore(org.apache.kafka.streams.state.SessionStore) Windowed(org.apache.kafka.streams.kstream.Windowed) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow)

Example 34 with SessionWindow

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());
    }
}
Also used : SessionStore(org.apache.kafka.streams.state.SessionStore) Windowed(org.apache.kafka.streams.kstream.Windowed) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow)

Example 35 with SessionWindow

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));
    }
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValue(org.apache.kafka.streams.KeyValue) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Aggregations

SessionWindow (org.apache.kafka.streams.kstream.internals.SessionWindow)117 Test (org.junit.Test)107 Windowed (org.apache.kafka.streams.kstream.Windowed)101 Bytes (org.apache.kafka.common.utils.Bytes)50 KeyValue (org.apache.kafka.streams.KeyValue)46 ArrayList (java.util.ArrayList)10 StreamsTestUtils.verifyWindowedKeyValue (org.apache.kafka.test.StreamsTestUtils.verifyWindowedKeyValue)8 LinkedList (java.util.LinkedList)6 Properties (java.util.Properties)6 ReadOnlySessionStoreStub (org.apache.kafka.test.ReadOnlySessionStoreStub)6 HashMap (java.util.HashMap)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)4 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)4 KeyValueTimestamp (org.apache.kafka.streams.KeyValueTimestamp)4 IntegrationTest (org.apache.kafka.test.IntegrationTest)4 KafkaMetric (org.apache.kafka.common.metrics.KafkaMetric)3 File (java.io.File)2 ByteBuffer (java.nio.ByteBuffer)2 HashSet (java.util.HashSet)2