Search in sources :

Example 36 with SessionWindow

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

the class AbstractSessionBytesStoreTest method shouldFetchAndIterateOverExactBinaryKeys.

@Test
public void shouldFetchAndIterateOverExactBinaryKeys() {
    final SessionStore<Bytes, String> sessionStore = buildSessionStore(RETENTION_PERIOD, Serdes.Bytes(), Serdes.String());
    sessionStore.init((StateStoreContext) context, sessionStore);
    final Bytes key1 = Bytes.wrap(new byte[] { 0 });
    final Bytes key2 = Bytes.wrap(new byte[] { 0, 0 });
    final Bytes key3 = Bytes.wrap(new byte[] { 0, 0, 0 });
    sessionStore.put(new Windowed<>(key1, new SessionWindow(1, 100)), "1");
    sessionStore.put(new Windowed<>(key2, new SessionWindow(2, 100)), "2");
    sessionStore.put(new Windowed<>(key3, new SessionWindow(3, 100)), "3");
    sessionStore.put(new Windowed<>(key1, new SessionWindow(4, 100)), "4");
    sessionStore.put(new Windowed<>(key2, new SessionWindow(5, 100)), "5");
    sessionStore.put(new Windowed<>(key3, new SessionWindow(6, 100)), "6");
    sessionStore.put(new Windowed<>(key1, new SessionWindow(7, 100)), "7");
    sessionStore.put(new Windowed<>(key2, new SessionWindow(8, 100)), "8");
    sessionStore.put(new Windowed<>(key3, new SessionWindow(9, 100)), "9");
    final List<String> expectedKey1 = asList("1", "4", "7");
    try (KeyValueIterator<Windowed<Bytes>, String> iterator = sessionStore.findSessions(key1, 0L, Long.MAX_VALUE)) {
        assertThat(valuesToSet(iterator), equalTo(new HashSet<>(expectedKey1)));
    }
    final List<String> expectedKey2 = asList("2", "5", "8");
    try (KeyValueIterator<Windowed<Bytes>, String> iterator = sessionStore.findSessions(key2, 0L, Long.MAX_VALUE)) {
        assertThat(valuesToSet(iterator), equalTo(new HashSet<>(expectedKey2)));
    }
    final List<String> expectedKey3 = asList("3", "6", "9");
    try (KeyValueIterator<Windowed<Bytes>, String> iterator = sessionStore.findSessions(key3, 0L, Long.MAX_VALUE)) {
        assertThat(valuesToSet(iterator), equalTo(new HashSet<>(expectedKey3)));
    }
    sessionStore.close();
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 37 with SessionWindow

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

the class AbstractSessionBytesStoreTest method shouldBackwardFetchAllSessionsWithSameRecordKey.

@Test
public void shouldBackwardFetchAllSessionsWithSameRecordKey() {
    final LinkedList<KeyValue<Windowed<String>, Long>> expected = new LinkedList<>();
    expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(0, 0)), 1L));
    expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(10, 10)), 2L));
    expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(100, 100)), 3L));
    expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(1000, 1000)), 4L));
    for (final KeyValue<Windowed<String>, Long> kv : expected) {
        sessionStore.put(kv.key, kv.value);
    }
    // add one that shouldn't appear in the results
    sessionStore.put(new Windowed<>("aa", new SessionWindow(0, 0)), 5L);
    try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.backwardFetch("a")) {
        assertEquals(toList(expected.descendingIterator()), toList(values));
    }
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValue(org.apache.kafka.streams.KeyValue) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 38 with SessionWindow

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

the class AbstractSessionBytesStoreTest method shouldRestore.

@SuppressWarnings("unchecked")
@Test
public void shouldRestore() {
    final List<KeyValue<Windowed<String>, Long>> expected = Arrays.asList(KeyValue.pair(new Windowed<>("a", new SessionWindow(0, 0)), 1L), KeyValue.pair(new Windowed<>("a", new SessionWindow(10, 10)), 2L), KeyValue.pair(new Windowed<>("a", new SessionWindow(100, 100)), 3L), KeyValue.pair(new Windowed<>("a", new SessionWindow(1000, 1000)), 4L));
    for (final KeyValue<Windowed<String>, Long> kv : expected) {
        sessionStore.put(kv.key, kv.value);
    }
    try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.fetch("a")) {
        assertEquals(expected, toList(values));
    }
    sessionStore.close();
    try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.fetch("a")) {
        assertEquals(Collections.emptyList(), toList(values));
    }
    final List<KeyValue<byte[], byte[]>> changeLog = new ArrayList<>();
    for (final ProducerRecord<Object, Object> record : recordCollector.collected()) {
        changeLog.add(new KeyValue<>(((Bytes) record.key()).get(), (byte[]) record.value()));
    }
    context.restore(sessionStore.name(), changeLog);
    try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.fetch("a")) {
        assertEquals(expected, toList(values));
    }
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList) 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 39 with SessionWindow

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

the class AbstractSessionBytesStoreTest method shouldBackwardFetchAllSessionsWithinKeyRange.

@Test
public void shouldBackwardFetchAllSessionsWithinKeyRange() {
    final LinkedList<KeyValue<Windowed<String>, Long>> expected = new LinkedList<>();
    expected.add(KeyValue.pair(new Windowed<>("aa", new SessionWindow(10, 10)), 2L));
    expected.add(KeyValue.pair(new Windowed<>("aaa", new SessionWindow(100, 100)), 3L));
    expected.add(KeyValue.pair(new Windowed<>("aaaa", new SessionWindow(100, 100)), 6L));
    expected.add(KeyValue.pair(new Windowed<>("b", new SessionWindow(1000, 1000)), 4L));
    expected.add(KeyValue.pair(new Windowed<>("bb", new SessionWindow(1500, 2000)), 5L));
    for (final KeyValue<Windowed<String>, Long> kv : expected) {
        sessionStore.put(kv.key, kv.value);
    }
    // add some that should only be fetched in infinite fetch
    sessionStore.put(new Windowed<>("a", new SessionWindow(0, 0)), 1L);
    sessionStore.put(new Windowed<>("bbb", new SessionWindow(2500, 3000)), 6L);
    try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.backwardFetch("aa", "bb")) {
        assertEquals(toList(expected.descendingIterator()), toList(values));
    }
    try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.backwardFindSessions("aa", "bb", 0L, Long.MAX_VALUE)) {
        assertEquals(toList(expected.descendingIterator()), toList(values));
    }
    // infinite keyFrom fetch case
    expected.add(0, KeyValue.pair(new Windowed<>("a", new SessionWindow(0, 0)), 1L));
    try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.backwardFetch(null, "bb")) {
        assertEquals(toList(expected.descendingIterator()), toList(values));
    }
    // remove the one added for unlimited start fetch case
    expected.remove(0);
    // infinite keyTo fetch case
    expected.add(KeyValue.pair(new Windowed<>("bbb", new SessionWindow(2500, 3000)), 6L));
    try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.backwardFetch("aa", null)) {
        assertEquals(toList(expected.descendingIterator()), toList(values));
    }
    // fetch all case
    expected.add(0, KeyValue.pair(new Windowed<>("a", new SessionWindow(0, 0)), 1L));
    try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.backwardFetch(null, null)) {
        assertEquals(toList(expected.descendingIterator()), toList(values));
    }
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValue(org.apache.kafka.streams.KeyValue) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 40 with SessionWindow

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

the class AbstractSessionBytesStoreTest method shouldFetchAllSessionsWithSameRecordKey.

@Test
public void shouldFetchAllSessionsWithSameRecordKey() {
    final LinkedList<KeyValue<Windowed<String>, Long>> expected = new LinkedList<>();
    expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(0, 0)), 1L));
    expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(10, 10)), 2L));
    expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(100, 100)), 3L));
    expected.add(KeyValue.pair(new Windowed<>("a", new SessionWindow(1000, 1000)), 4L));
    for (final KeyValue<Windowed<String>, Long> kv : expected) {
        sessionStore.put(kv.key, kv.value);
    }
    // add one that shouldn't appear in the results
    sessionStore.put(new Windowed<>("aa", new SessionWindow(0, 0)), 5L);
    try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.fetch("a")) {
        assertEquals(expected, toList(values));
    }
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValue(org.apache.kafka.streams.KeyValue) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) LinkedList(java.util.LinkedList) 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