Search in sources :

Example 96 with MockStreamsMetrics

use of org.apache.kafka.streams.processor.internals.MockStreamsMetrics in project kafka by apache.

the class ThreadCacheTest method basicPutGet.

@Test
public void basicPutGet() {
    final List<KeyValue<String, String>> toInsert = Arrays.asList(new KeyValue<>("K1", "V1"), new KeyValue<>("K2", "V2"), new KeyValue<>("K3", "V3"), new KeyValue<>("K4", "V4"), new KeyValue<>("K5", "V5"));
    final KeyValue<String, String> kv = toInsert.get(0);
    final ThreadCache cache = new ThreadCache(logContext, toInsert.size() * memoryCacheEntrySize(kv.key.getBytes(), kv.value.getBytes(), ""), new MockStreamsMetrics(new Metrics()));
    for (final KeyValue<String, String> kvToInsert : toInsert) {
        final Bytes key = Bytes.wrap(kvToInsert.key.getBytes());
        final byte[] value = kvToInsert.value.getBytes();
        cache.put(namespace, key, new LRUCacheEntry(value, new RecordHeaders(), true, 1L, 1L, 1, ""));
    }
    for (final KeyValue<String, String> kvToInsert : toInsert) {
        final Bytes key = Bytes.wrap(kvToInsert.key.getBytes());
        final LRUCacheEntry entry = cache.get(namespace, key);
        assertTrue(entry.isDirty());
        assertEquals(new String(entry.value()), kvToInsert.value);
    }
    assertEquals(cache.gets(), 5);
    assertEquals(cache.puts(), 5);
    assertEquals(cache.evicts(), 0);
    assertEquals(cache.flushes(), 0);
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) KeyValue(org.apache.kafka.streams.KeyValue) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Test(org.junit.Test)

Example 97 with MockStreamsMetrics

use of org.apache.kafka.streams.processor.internals.MockStreamsMetrics in project kafka by apache.

the class ThreadCacheTest method shouldCleanupNamedCacheOnClose.

@Test
public void shouldCleanupNamedCacheOnClose() {
    final ThreadCache cache = new ThreadCache(logContext, 100000, new MockStreamsMetrics(new Metrics()));
    cache.put(namespace1, Bytes.wrap(new byte[] { 1 }), cleanEntry(new byte[] { 1 }));
    cache.put(namespace2, Bytes.wrap(new byte[] { 1 }), cleanEntry(new byte[] { 1 }));
    assertEquals(cache.size(), 2);
    cache.close(namespace2);
    assertEquals(cache.size(), 1);
    assertNull(cache.get(namespace2, Bytes.wrap(new byte[] { 1 })));
}
Also used : MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Test(org.junit.Test)

Example 98 with MockStreamsMetrics

use of org.apache.kafka.streams.processor.internals.MockStreamsMetrics in project kafka by apache.

the class ThreadCacheTest method checkOverheads.

private void checkOverheads(final double entryFactor, final double systemFactor, final long desiredCacheSize, final int keySizeBytes, final int valueSizeBytes) {
    final Runtime runtime = Runtime.getRuntime();
    final long numElements = desiredCacheSize / memoryCacheEntrySize(new byte[keySizeBytes], new byte[valueSizeBytes], "");
    System.gc();
    final long prevRuntimeMemory = runtime.totalMemory() - runtime.freeMemory();
    final ThreadCache cache = new ThreadCache(logContext, desiredCacheSize, new MockStreamsMetrics(new Metrics()));
    final long size = cache.sizeBytes();
    assertEquals(size, 0);
    for (int i = 0; i < numElements; i++) {
        final String keyStr = "K" + i;
        final Bytes key = Bytes.wrap(keyStr.getBytes());
        final byte[] value = new byte[valueSizeBytes];
        cache.put(namespace, key, new LRUCacheEntry(value, new RecordHeaders(), true, 1L, 1L, 1, ""));
    }
    System.gc();
    final double ceiling = desiredCacheSize + desiredCacheSize * entryFactor;
    final long usedRuntimeMemory = runtime.totalMemory() - runtime.freeMemory() - prevRuntimeMemory;
    assertTrue((double) cache.sizeBytes() <= ceiling);
    assertTrue("Used memory size " + usedRuntimeMemory + " greater than expected " + cache.sizeBytes() * systemFactor, cache.sizeBytes() * systemFactor >= usedRuntimeMemory);
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics)

Example 99 with MockStreamsMetrics

use of org.apache.kafka.streams.processor.internals.MockStreamsMetrics in project kafka by apache.

the class ThreadCacheTest method shouldPutIfAbsent.

@Test
public void shouldPutIfAbsent() {
    final ThreadCache cache = new ThreadCache(logContext, 100000, new MockStreamsMetrics(new Metrics()));
    final Bytes key = Bytes.wrap(new byte[] { 10 });
    final byte[] value = { 30 };
    assertNull(cache.putIfAbsent(namespace, key, dirtyEntry(value)));
    assertArrayEquals(value, cache.putIfAbsent(namespace, key, dirtyEntry(new byte[] { 8 })).value());
    assertArrayEquals(value, cache.get(namespace, key).value());
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Test(org.junit.Test)

Example 100 with MockStreamsMetrics

use of org.apache.kafka.streams.processor.internals.MockStreamsMetrics in project kafka by apache.

the class ThreadCacheTest method evict.

@Test
public void evict() {
    final List<KeyValue<String, String>> received = new ArrayList<>();
    final List<KeyValue<String, String>> expected = Collections.singletonList(new KeyValue<>("K1", "V1"));
    final List<KeyValue<String, String>> toInsert = Arrays.asList(new KeyValue<>("K1", "V1"), new KeyValue<>("K2", "V2"), new KeyValue<>("K3", "V3"), new KeyValue<>("K4", "V4"), new KeyValue<>("K5", "V5"));
    final KeyValue<String, String> kv = toInsert.get(0);
    final ThreadCache cache = new ThreadCache(logContext, memoryCacheEntrySize(kv.key.getBytes(), kv.value.getBytes(), ""), new MockStreamsMetrics(new Metrics()));
    cache.addDirtyEntryFlushListener(namespace, dirty -> {
        for (final ThreadCache.DirtyEntry dirtyEntry : dirty) {
            received.add(new KeyValue<>(dirtyEntry.key().toString(), new String(dirtyEntry.newValue())));
        }
    });
    for (final KeyValue<String, String> kvToInsert : toInsert) {
        final Bytes key = Bytes.wrap(kvToInsert.key.getBytes());
        final byte[] value = kvToInsert.value.getBytes();
        cache.put(namespace, key, new LRUCacheEntry(value, new RecordHeaders(), true, 1, 1, 1, ""));
    }
    for (int i = 0; i < expected.size(); i++) {
        final KeyValue<String, String> expectedRecord = expected.get(i);
        final KeyValue<String, String> actualRecord = received.get(i);
        assertEquals(expectedRecord, actualRecord);
    }
    assertEquals(cache.evicts(), 4);
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList) 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) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) Test(org.junit.Test)

Aggregations

MockStreamsMetrics (org.apache.kafka.streams.processor.internals.MockStreamsMetrics)103 Metrics (org.apache.kafka.common.metrics.Metrics)101 Test (org.junit.Test)59 Before (org.junit.Before)38 LogContext (org.apache.kafka.common.utils.LogContext)32 Bytes (org.apache.kafka.common.utils.Bytes)27 ArrayList (java.util.ArrayList)17 NoOpRecordCollector (org.apache.kafka.test.NoOpRecordCollector)15 InternalMockProcessorContext (org.apache.kafka.test.InternalMockProcessorContext)14 ProcessorRecordContext (org.apache.kafka.streams.processor.internals.ProcessorRecordContext)12 MockProcessorContext (org.apache.kafka.test.MockProcessorContext)10 File (java.io.File)7 RecordHeaders (org.apache.kafka.common.header.internals.RecordHeaders)7 MockTime (org.apache.kafka.common.utils.MockTime)7 KeyValue (org.apache.kafka.streams.KeyValue)7 MockRecordCollector (org.apache.kafka.test.MockRecordCollector)7 StreamsMetricsImpl (org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl)6 Properties (java.util.Properties)5 Serializer (org.apache.kafka.common.serialization.Serializer)4 StreamsConfig (org.apache.kafka.streams.StreamsConfig)4