Search in sources :

Example 41 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class WindowKeySchema method hasNextCondition.

@Override
public HasNextCondition hasNextCondition(final Bytes binaryKey, final long from, final long to) {
    return new HasNextCondition() {

        @Override
        public boolean hasNext(final KeyValueIterator<Bytes, ?> iterator) {
            if (iterator.hasNext()) {
                final Bytes bytes = iterator.peekNextKey();
                final Bytes keyBytes = WindowStoreUtils.bytesKeyFromBinaryKey(bytes.get());
                if (!keyBytes.equals(binaryKey)) {
                    return false;
                }
                final long time = WindowStoreUtils.timestampFromBinaryKey(bytes.get());
                return time >= from && time <= to;
            }
            return false;
        }
    };
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator)

Example 42 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class NamedCacheTest method shouldBeReentrantAndNotBreakLRU.

@Test
public void shouldBeReentrantAndNotBreakLRU() throws Exception {
    final LRUCacheEntry dirty = new LRUCacheEntry(new byte[] { 3 }, true, 0, 0, 0, "");
    final LRUCacheEntry clean = new LRUCacheEntry(new byte[] { 3 });
    cache.put(Bytes.wrap(new byte[] { 0 }), dirty);
    cache.put(Bytes.wrap(new byte[] { 1 }), clean);
    cache.put(Bytes.wrap(new byte[] { 2 }), clean);
    assertEquals(3 * cache.head().size(), cache.sizeInBytes());
    cache.setListener(new ThreadCache.DirtyEntryFlushListener() {

        @Override
        public void apply(final List<ThreadCache.DirtyEntry> dirty) {
            cache.put(Bytes.wrap(new byte[] { 3 }), clean);
            // evict key 1
            cache.evict();
            // evict key 2
            cache.evict();
        }
    });
    assertEquals(3 * cache.head().size(), cache.sizeInBytes());
    // Evict key 0
    cache.evict();
    final Bytes entryFour = Bytes.wrap(new byte[] { 4 });
    cache.put(entryFour, dirty);
    // check that the LRU is still correct
    final NamedCache.LRUNode head = cache.head();
    final NamedCache.LRUNode tail = cache.tail();
    assertEquals(2, cache.size());
    assertEquals(2 * head.size(), cache.sizeInBytes());
    // dirty should be the newest
    assertEquals(entryFour, head.key());
    assertEquals(Bytes.wrap(new byte[] { 3 }), tail.key());
    assertSame(tail, head.next());
    assertNull(head.previous());
    assertSame(head, tail.previous());
    assertNull(tail.next());
    // evict key 3
    cache.evict();
    assertSame(cache.head(), cache.tail());
    assertEquals(entryFour, cache.head().key());
    assertNull(cache.head().next());
    assertNull(cache.head().previous());
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) Test(org.junit.Test)

Example 43 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class MergedSortedCacheKeyValueStoreIteratorTest method shouldIterateOverRange.

@Test
public void shouldIterateOverRange() throws Exception {
    final byte[][] bytes = { { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 11 } };
    for (int i = 0; i < bytes.length; i += 2) {
        store.put(Bytes.wrap(bytes[i]), bytes[i]);
        cache.put(namespace, Bytes.wrap(bytes[i + 1]), new LRUCacheEntry(bytes[i + 1]));
    }
    final Bytes from = Bytes.wrap(new byte[] { 2 });
    final Bytes to = Bytes.wrap(new byte[] { 9 });
    final KeyValueIterator<Bytes, byte[]> storeIterator = new DelegatingPeekingKeyValueIterator<>("store", store.range(from, to));
    final ThreadCache.MemoryLRUCacheBytesIterator cacheIterator = cache.range(namespace, from, to);
    final MergedSortedCacheKeyValueStoreIterator<byte[], byte[]> iterator = new MergedSortedCacheKeyValueStoreIterator<>(cacheIterator, storeIterator, serdes);
    byte[][] values = new byte[8][];
    int index = 0;
    int bytesIndex = 2;
    while (iterator.hasNext()) {
        final byte[] value = iterator.next().value;
        values[index++] = value;
        assertArrayEquals(bytes[bytesIndex++], value);
    }
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) Test(org.junit.Test)

Example 44 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class MergedSortedCacheKeyValueStoreIteratorTest method shouldSkipAllDeletedFromCache.

@Test
public void shouldSkipAllDeletedFromCache() throws Exception {
    final byte[][] bytes = { { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 11 } };
    for (byte[] aByte : bytes) {
        Bytes aBytes = Bytes.wrap(aByte);
        store.put(aBytes, aByte);
        cache.put(namespace, aBytes, new LRUCacheEntry(aByte));
    }
    cache.put(namespace, Bytes.wrap(bytes[1]), new LRUCacheEntry(null));
    cache.put(namespace, Bytes.wrap(bytes[2]), new LRUCacheEntry(null));
    cache.put(namespace, Bytes.wrap(bytes[3]), new LRUCacheEntry(null));
    cache.put(namespace, Bytes.wrap(bytes[8]), new LRUCacheEntry(null));
    cache.put(namespace, Bytes.wrap(bytes[11]), new LRUCacheEntry(null));
    final MergedSortedCacheKeyValueStoreIterator<byte[], byte[]> iterator = createIterator();
    assertArrayEquals(bytes[0], iterator.next().key);
    assertArrayEquals(bytes[4], iterator.next().key);
    assertArrayEquals(bytes[5], iterator.next().key);
    assertArrayEquals(bytes[6], iterator.next().key);
    assertArrayEquals(bytes[7], iterator.next().key);
    assertArrayEquals(bytes[9], iterator.next().key);
    assertArrayEquals(bytes[10], iterator.next().key);
    assertFalse(iterator.hasNext());
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) Test(org.junit.Test)

Example 45 with Bytes

use of org.apache.kafka.common.utils.Bytes in project kafka by apache.

the class MergedSortedCacheKeyValueStoreIteratorTest method shouldPeekNextKey.

@Test
public void shouldPeekNextKey() throws Exception {
    final KeyValueStore<Bytes, byte[]> kv = new InMemoryKeyValueStore<>("one", Serdes.Bytes(), Serdes.ByteArray());
    final ThreadCache cache = new ThreadCache("testCache", 1000000L, new MockStreamsMetrics(new Metrics()));
    byte[][] bytes = { { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 } };
    final String namespace = "one";
    for (int i = 0; i < bytes.length - 1; i += 2) {
        kv.put(Bytes.wrap(bytes[i]), bytes[i]);
        cache.put(namespace, Bytes.wrap(bytes[i + 1]), new LRUCacheEntry(bytes[i + 1]));
    }
    final Bytes from = Bytes.wrap(new byte[] { 2 });
    final Bytes to = Bytes.wrap(new byte[] { 9 });
    final KeyValueIterator<Bytes, byte[]> storeIterator = kv.range(from, to);
    final ThreadCache.MemoryLRUCacheBytesIterator cacheIterator = cache.range(namespace, from, to);
    final MergedSortedCacheKeyValueStoreIterator<byte[], byte[]> iterator = new MergedSortedCacheKeyValueStoreIterator<>(cacheIterator, storeIterator, serdes);
    final byte[][] values = new byte[8][];
    int index = 0;
    int bytesIndex = 2;
    while (iterator.hasNext()) {
        final byte[] keys = iterator.peekNextKey();
        values[index++] = keys;
        assertArrayEquals(bytes[bytesIndex++], keys);
        iterator.next();
    }
}
Also used : MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Bytes(org.apache.kafka.common.utils.Bytes) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) Test(org.junit.Test)

Aggregations

Bytes (org.apache.kafka.common.utils.Bytes)54 Test (org.junit.Test)34 Metrics (org.apache.kafka.common.metrics.Metrics)14 MockStreamsMetrics (org.apache.kafka.streams.processor.internals.MockStreamsMetrics)14 Windowed (org.apache.kafka.streams.kstream.Windowed)5 ArrayList (java.util.ArrayList)4 KeyValue (org.apache.kafka.streams.KeyValue)4 SessionWindow (org.apache.kafka.streams.kstream.internals.SessionWindow)3 MockProcessorContext (org.apache.kafka.test.MockProcessorContext)3 ByteBuffer (java.nio.ByteBuffer)2 NoSuchElementException (java.util.NoSuchElementException)2 ProcessorRecordContext (org.apache.kafka.streams.processor.internals.ProcessorRecordContext)2 KeyValueIterator (org.apache.kafka.streams.state.KeyValueIterator)2 Before (org.junit.Before)2 MockProducer (org.apache.kafka.clients.producer.MockProducer)1 Serializer (org.apache.kafka.common.serialization.Serializer)1 StreamsException (org.apache.kafka.streams.errors.StreamsException)1 RecordContext (org.apache.kafka.streams.processor.internals.RecordContext)1 StateSerdes (org.apache.kafka.streams.state.StateSerdes)1