Search in sources :

Example 61 with Metrics

use of org.apache.kafka.common.metrics.Metrics in project kafka by apache.

the class StreamThreadStateStoreProviderTest method before.

@Before
public void before() throws IOException {
    final TopologyBuilder builder = new TopologyBuilder();
    builder.addSource("the-source", "the-source");
    builder.addProcessor("the-processor", new MockProcessorSupplier(), "the-source");
    builder.addStateStore(Stores.create("kv-store").withStringKeys().withStringValues().inMemory().build(), "the-processor");
    builder.addStateStore(Stores.create("window-store").withStringKeys().withStringValues().persistent().windowed(10, 10, 2, false).build(), "the-processor");
    final Properties properties = new Properties();
    final String applicationId = "applicationId";
    properties.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId);
    properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    stateDir = TestUtils.tempDirectory();
    final String stateConfigDir = stateDir.getPath();
    properties.put(StreamsConfig.STATE_DIR_CONFIG, stateConfigDir);
    final StreamsConfig streamsConfig = new StreamsConfig(properties);
    final MockClientSupplier clientSupplier = new MockClientSupplier();
    configureRestoreConsumer(clientSupplier, "applicationId-kv-store-changelog");
    configureRestoreConsumer(clientSupplier, "applicationId-window-store-changelog");
    builder.setApplicationId(applicationId);
    final ProcessorTopology topology = builder.build(null);
    final Map<TaskId, StreamTask> tasks = new HashMap<>();
    stateDirectory = new StateDirectory(applicationId, stateConfigDir, new MockTime());
    taskOne = createStreamsTask(applicationId, streamsConfig, clientSupplier, topology, new TaskId(0, 0));
    tasks.put(new TaskId(0, 0), taskOne);
    taskTwo = createStreamsTask(applicationId, streamsConfig, clientSupplier, topology, new TaskId(0, 1));
    tasks.put(new TaskId(0, 1), taskTwo);
    storesAvailable = true;
    thread = new StreamThread(builder, streamsConfig, clientSupplier, applicationId, "clientId", UUID.randomUUID(), new Metrics(), Time.SYSTEM, new StreamsMetadataState(builder, StreamsMetadataState.UNKNOWN_HOST), 0) {

        @Override
        public Map<TaskId, StreamTask> tasks() {
            return tasks;
        }

        @Override
        public boolean isInitialized() {
            return storesAvailable;
        }
    };
    provider = new StreamThreadStateStoreProvider(thread);
}
Also used : ProcessorTopology(org.apache.kafka.streams.processor.internals.ProcessorTopology) TaskId(org.apache.kafka.streams.processor.TaskId) TopologyBuilder(org.apache.kafka.streams.processor.TopologyBuilder) HashMap(java.util.HashMap) StreamThread(org.apache.kafka.streams.processor.internals.StreamThread) Properties(java.util.Properties) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) StreamsMetadataState(org.apache.kafka.streams.processor.internals.StreamsMetadataState) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) MockClientSupplier(org.apache.kafka.test.MockClientSupplier) HashMap(java.util.HashMap) Map(java.util.Map) StreamTask(org.apache.kafka.streams.processor.internals.StreamTask) MockTime(org.apache.kafka.common.utils.MockTime) StreamsConfig(org.apache.kafka.streams.StreamsConfig) StateDirectory(org.apache.kafka.streams.processor.internals.StateDirectory) Before(org.junit.Before)

Example 62 with Metrics

use of org.apache.kafka.common.metrics.Metrics in project kafka by apache.

the class ThreadCacheTest method shouldSkipEntriesWhereValueHasBeenEvictedFromCache.

@Test
public void shouldSkipEntriesWhereValueHasBeenEvictedFromCache() throws Exception {
    final String namespace = "streams";
    final int entrySize = memoryCacheEntrySize(new byte[1], new byte[1], "");
    final ThreadCache cache = new ThreadCache("testCache", entrySize * 5, new MockStreamsMetrics(new Metrics()));
    cache.addDirtyEntryFlushListener(namespace, new ThreadCache.DirtyEntryFlushListener() {

        @Override
        public void apply(final List<ThreadCache.DirtyEntry> dirty) {
        }
    });
    byte[][] bytes = { { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 } };
    for (int i = 0; i < 5; i++) {
        cache.put(namespace, Bytes.wrap(bytes[i]), dirtyEntry(bytes[i]));
    }
    assertEquals(5, cache.size());
    final ThreadCache.MemoryLRUCacheBytesIterator range = cache.range(namespace, Bytes.wrap(new byte[] { 0 }), Bytes.wrap(new byte[] { 5 }));
    // should evict byte[] {0}
    cache.put(namespace, Bytes.wrap(new byte[] { 6 }), dirtyEntry(new byte[] { 6 }));
    assertEquals(Bytes.wrap(new byte[] { 1 }), range.peekNextKey());
}
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 63 with Metrics

use of org.apache.kafka.common.metrics.Metrics in project kafka by apache.

the class NamedCacheTest method setUp.

@Before
public void setUp() throws Exception {
    streamMetrics = new MockStreamsMetrics(new Metrics());
    cache = new NamedCache("name", streamMetrics);
}
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) Before(org.junit.Before)

Example 64 with Metrics

use of org.apache.kafka.common.metrics.Metrics in project kafka by apache.

the class MergedSortedCacheKeyValueStoreIteratorTest method setUp.

@Before
public void setUp() throws Exception {
    store = new InMemoryKeyValueStore<>(namespace, Serdes.Bytes(), Serdes.ByteArray());
    cache = new ThreadCache("testCache", 10000L, new MockStreamsMetrics(new Metrics()));
}
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) Before(org.junit.Before)

Example 65 with Metrics

use of org.apache.kafka.common.metrics.Metrics 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

Metrics (org.apache.kafka.common.metrics.Metrics)103 Test (org.junit.Test)76 MockStreamsMetrics (org.apache.kafka.streams.processor.internals.MockStreamsMetrics)41 HashMap (java.util.HashMap)31 StreamsConfig (org.apache.kafka.streams.StreamsConfig)28 TaskId (org.apache.kafka.streams.processor.TaskId)27 Before (org.junit.Before)22 MockTime (org.apache.kafka.common.utils.MockTime)21 TopicPartition (org.apache.kafka.common.TopicPartition)20 HashSet (java.util.HashSet)19 StreamsMetrics (org.apache.kafka.streams.StreamsMetrics)17 MockClientSupplier (org.apache.kafka.test.MockClientSupplier)17 UUID (java.util.UUID)16 PartitionAssignor (org.apache.kafka.clients.consumer.internals.PartitionAssignor)15 Bytes (org.apache.kafka.common.utils.Bytes)14 MockProcessorSupplier (org.apache.kafka.test.MockProcessorSupplier)14 KStreamBuilder (org.apache.kafka.streams.kstream.KStreamBuilder)13 SubscriptionInfo (org.apache.kafka.streams.processor.internals.assignment.SubscriptionInfo)13 MockProcessorContext (org.apache.kafka.test.MockProcessorContext)13 MockInternalTopicManager (org.apache.kafka.test.MockInternalTopicManager)11