Search in sources :

Example 96 with SessionWindow

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

the class CachingInMemorySessionStoreTest method shouldFetchRangeCorrectlyAcrossSegments.

@Test
public void shouldFetchRangeCorrectlyAcrossSegments() {
    final Windowed<Bytes> a1 = new Windowed<>(keyA, new SessionWindow(SEGMENT_INTERVAL * 0, SEGMENT_INTERVAL * 0));
    final Windowed<Bytes> aa1 = new Windowed<>(keyAA, new SessionWindow(SEGMENT_INTERVAL * 0, SEGMENT_INTERVAL * 0));
    final Windowed<Bytes> a2 = new Windowed<>(keyA, new SessionWindow(SEGMENT_INTERVAL * 1, SEGMENT_INTERVAL * 1));
    final Windowed<Bytes> a3 = new Windowed<>(keyA, new SessionWindow(SEGMENT_INTERVAL * 2, SEGMENT_INTERVAL * 2));
    final Windowed<Bytes> aa3 = new Windowed<>(keyAA, new SessionWindow(SEGMENT_INTERVAL * 2, SEGMENT_INTERVAL * 2));
    cachingStore.put(a1, "1".getBytes());
    cachingStore.put(aa1, "1".getBytes());
    cachingStore.put(a2, "2".getBytes());
    cachingStore.put(a3, "3".getBytes());
    cachingStore.put(aa3, "3".getBytes());
    final KeyValueIterator<Windowed<Bytes>, byte[]> rangeResults = cachingStore.findSessions(keyA, keyAA, 0, SEGMENT_INTERVAL * 2);
    final List<Windowed<Bytes>> keys = new ArrayList<>();
    while (rangeResults.hasNext()) {
        keys.add(rangeResults.next().key);
    }
    rangeResults.close();
    assertEquals(asList(a1, aa1, a2, a3, aa3), keys);
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes) ArrayList(java.util.ArrayList) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Example 97 with SessionWindow

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

the class CachingPersistentSessionStoreTest method shouldClearNamespaceCacheOnClose.

@Test
public void shouldClearNamespaceCacheOnClose() {
    final Windowed<Bytes> a1 = new Windowed<>(keyA, new SessionWindow(0, 0));
    cachingStore.put(a1, "1".getBytes());
    assertEquals(1, cache.size());
    cachingStore.close();
    assertEquals(0, cache.size());
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Example 98 with SessionWindow

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

the class CachingPersistentSessionStoreTest method addSingleSession.

private void addSingleSession(final String sessionId, final List<KeyValue<Windowed<Bytes>, byte[]>> allSessions) {
    final int timestamp = allSessions.size() * 10;
    final Windowed<Bytes> key = new Windowed<>(Bytes.wrap(sessionId.getBytes()), new SessionWindow(timestamp, timestamp));
    final byte[] value = "1".getBytes();
    cachingStore.put(key, value);
    allSessions.add(KeyValue.pair(key, value));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow)

Example 99 with SessionWindow

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

the class CachingPersistentSessionStoreTest method shouldRemove.

@Test
public void shouldRemove() {
    final Windowed<Bytes> a = new Windowed<>(keyA, new SessionWindow(0, 0));
    final Windowed<Bytes> b = new Windowed<>(keyB, new SessionWindow(0, 0));
    cachingStore.put(a, "2".getBytes());
    cachingStore.put(b, "2".getBytes());
    cachingStore.remove(a);
    try (final KeyValueIterator<Windowed<Bytes>, byte[]> rangeIter = cachingStore.findSessions(keyA, 0, 0)) {
        assertFalse(rangeIter.hasNext());
    }
    assertNull(cachingStore.fetchSession(keyA, 0, 0));
    assertThat(cachingStore.fetchSession(keyB, 0, 0), equalTo("2".getBytes()));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Example 100 with SessionWindow

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

the class CachingPersistentSessionStoreTest method shouldBackwardFetchRangeCorrectlyAcrossSegments.

@Test
public void shouldBackwardFetchRangeCorrectlyAcrossSegments() {
    final Windowed<Bytes> a1 = new Windowed<>(keyA, new SessionWindow(SEGMENT_INTERVAL * 0, SEGMENT_INTERVAL * 0));
    final Windowed<Bytes> aa1 = new Windowed<>(keyAA, new SessionWindow(SEGMENT_INTERVAL * 0, SEGMENT_INTERVAL * 0));
    final Windowed<Bytes> a2 = new Windowed<>(keyA, new SessionWindow(SEGMENT_INTERVAL * 1, SEGMENT_INTERVAL * 1));
    final Windowed<Bytes> a3 = new Windowed<>(keyA, new SessionWindow(SEGMENT_INTERVAL * 2, SEGMENT_INTERVAL * 2));
    final Windowed<Bytes> aa3 = new Windowed<>(keyAA, new SessionWindow(SEGMENT_INTERVAL * 2, SEGMENT_INTERVAL * 2));
    cachingStore.put(a1, "1".getBytes());
    cachingStore.put(aa1, "1".getBytes());
    cachingStore.put(a2, "2".getBytes());
    cachingStore.put(a3, "3".getBytes());
    cachingStore.put(aa3, "3".getBytes());
    final KeyValueIterator<Windowed<Bytes>, byte[]> rangeResults = cachingStore.backwardFindSessions(keyA, keyAA, 0, SEGMENT_INTERVAL * 2);
    final List<Windowed<Bytes>> keys = new ArrayList<>();
    while (rangeResults.hasNext()) {
        keys.add(rangeResults.next().key);
    }
    rangeResults.close();
    assertEquals(asList(aa3, a3, a2, aa1, a1), keys);
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes) ArrayList(java.util.ArrayList) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Aggregations

SessionWindow (org.apache.kafka.streams.kstream.internals.SessionWindow)126 Test (org.junit.Test)112 Windowed (org.apache.kafka.streams.kstream.Windowed)110 Bytes (org.apache.kafka.common.utils.Bytes)50 KeyValue (org.apache.kafka.streams.KeyValue)50 ArrayList (java.util.ArrayList)10 Properties (java.util.Properties)9 HashMap (java.util.HashMap)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)8 StreamsTestUtils.verifyWindowedKeyValue (org.apache.kafka.test.StreamsTestUtils.verifyWindowedKeyValue)8 LinkedList (java.util.LinkedList)6 ReadOnlySessionStoreStub (org.apache.kafka.test.ReadOnlySessionStoreStub)6 GenericRow (io.confluent.ksql.GenericRow)4 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)4 KeyValueTimestamp (org.apache.kafka.streams.KeyValueTimestamp)4 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)4 IntegrationTest (org.apache.kafka.test.IntegrationTest)4 GenericKey (io.confluent.ksql.GenericKey)3 KafkaMetric (org.apache.kafka.common.metrics.KafkaMetric)3