Search in sources :

Example 41 with SessionWindow

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

the class AbstractSessionBytesStoreTest method shouldPutAndBackwardFindSessionsInRange.

@Test
public void shouldPutAndBackwardFindSessionsInRange() {
    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 LinkedList<KeyValue<Windowed<String>, Long>> expected = new LinkedList<>();
    expected.add(KeyValue.pair(a1, 1L));
    expected.add(KeyValue.pair(a2, 2L));
    try (final KeyValueIterator<Windowed<String>, Long> values = sessionStore.backwardFindSessions(key, 0, 1000L)) {
        assertEquals(toList(expected.descendingIterator()), toList(values));
    }
    final List<KeyValue<Windowed<String>, Long>> expected2 = Collections.singletonList(KeyValue.pair(a2, 2L));
    try (final KeyValueIterator<Windowed<String>, Long> values2 = sessionStore.backwardFindSessions(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) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 42 with SessionWindow

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

the class SessionKeySchemaTest method testLowerBoundMatchesTrailingZeros.

@Test
public void testLowerBoundMatchesTrailingZeros() {
    final Bytes lower = sessionKeySchema.lowerRange(Bytes.wrap(new byte[] { 0xA, 0xB, 0xC }), Long.MAX_VALUE);
    assertThat("appending zeros to key should still be in range", lower.compareTo(SessionKeySchema.toBinary(new Windowed<>(Bytes.wrap(new byte[] { 0xA, 0xB, 0xC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }), new SessionWindow(Long.MAX_VALUE, Long.MAX_VALUE)))) < 0);
    assertThat(lower, equalTo(SessionKeySchema.toBinary(new Windowed<>(Bytes.wrap(new byte[] { 0xA, 0xB, 0xC }), new SessionWindow(0, 0)))));
}
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 43 with SessionWindow

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

the class AbstractRocksDBSegmentedBytesStoreTest method before.

@Before
public void before() {
    if (schema instanceof SessionKeySchema) {
        windows[0] = new SessionWindow(10L, 10L);
        windows[1] = new SessionWindow(500L, 1000L);
        windows[2] = new SessionWindow(1_000L, 1_500L);
        windows[3] = new SessionWindow(30_000L, 60_000L);
        // All four of the previous windows will go into segment 1.
        // The nextSegmentWindow is computed be a high enough time that when it gets written
        // to the segment store, it will advance stream time past the first segment's retention time and
        // expire it.
        nextSegmentWindow = new SessionWindow(segmentInterval + retention, segmentInterval + retention);
    }
    if (schema instanceof WindowKeySchema) {
        windows[0] = timeWindowForSize(10L, windowSizeForTimeWindow);
        windows[1] = timeWindowForSize(500L, windowSizeForTimeWindow);
        windows[2] = timeWindowForSize(1_000L, windowSizeForTimeWindow);
        windows[3] = timeWindowForSize(60_000L, windowSizeForTimeWindow);
        // All four of the previous windows will go into segment 1.
        // The nextSegmentWindow is computed be a high enough time that when it gets written
        // to the segment store, it will advance stream time past the first segment's retention time and
        // expire it.
        nextSegmentWindow = timeWindowForSize(segmentInterval + retention, windowSizeForTimeWindow);
    }
    bytesStore = getBytesStore();
    stateDir = TestUtils.tempDirectory();
    context = new InternalMockProcessorContext<>(stateDir, Serdes.String(), Serdes.Long(), new MockRecordCollector(), new ThreadCache(new LogContext("testCache "), 0, new MockStreamsMetrics(new Metrics())));
    bytesStore.init((StateStoreContext) context, bytesStore);
}
Also used : MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) MockRecordCollector(org.apache.kafka.test.MockRecordCollector) LogContext(org.apache.kafka.common.utils.LogContext) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Before(org.junit.Before)

Example 44 with SessionWindow

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

the class CachingInMemorySessionStoreTest method shouldThrowIfTryingToPutIntoClosedCachingStore.

@Test
public void shouldThrowIfTryingToPutIntoClosedCachingStore() {
    cachingStore.close();
    assertThrows(InvalidStateStoreException.class, () -> cachingStore.put(new Windowed<>(keyA, new SessionWindow(0, 0)), "1".getBytes()));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Example 45 with SessionWindow

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

the class CachingInMemorySessionStoreTest method shouldFetchAllSessionsWithSameRecordKey.

@Test
public void shouldFetchAllSessionsWithSameRecordKey() {
    final List<KeyValue<Windowed<Bytes>, byte[]>> expected = asList(KeyValue.pair(new Windowed<>(keyA, new SessionWindow(0, 0)), "1".getBytes()), KeyValue.pair(new Windowed<>(keyA, new SessionWindow(10, 10)), "2".getBytes()), KeyValue.pair(new Windowed<>(keyA, new SessionWindow(100, 100)), "3".getBytes()), KeyValue.pair(new Windowed<>(keyA, new SessionWindow(1000, 1000)), "4".getBytes()));
    for (final KeyValue<Windowed<Bytes>, byte[]> kv : expected) {
        cachingStore.put(kv.key, kv.value);
    }
    // add one that shouldn't appear in the results
    cachingStore.put(new Windowed<>(keyAA, new SessionWindow(0, 0)), "5".getBytes());
    final List<KeyValue<Windowed<Bytes>, byte[]>> results = toList(cachingStore.fetch(keyA));
    verifyKeyValueList(expected, results);
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes) KeyValue(org.apache.kafka.streams.KeyValue) StreamsTestUtils.verifyWindowedKeyValue(org.apache.kafka.test.StreamsTestUtils.verifyWindowedKeyValue) 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