Search in sources :

Example 41 with Metrics

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

the class ThreadCacheTest method shouldNotFlushAfterDelete.

@Test
public void shouldNotFlushAfterDelete() throws Exception {
    final Bytes key = Bytes.wrap(new byte[] { 0 });
    final ThreadCache cache = new ThreadCache("testCache", 10000L, new MockStreamsMetrics(new Metrics()));
    final List<ThreadCache.DirtyEntry> received = new ArrayList<>();
    final String namespace = "namespace";
    cache.addDirtyEntryFlushListener(namespace, new ThreadCache.DirtyEntryFlushListener() {

        @Override
        public void apply(final List<ThreadCache.DirtyEntry> dirty) {
            received.addAll(dirty);
        }
    });
    cache.put(namespace, key, dirtyEntry(key.get()));
    assertEquals(key.get(), cache.delete(namespace, key).value);
    // flushing should have no further effect
    cache.flush(namespace);
    assertEquals(0, received.size());
    assertEquals(cache.flushes(), 1);
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) ArrayList(java.util.ArrayList) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Test(org.junit.Test)

Example 42 with Metrics

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

the class ThreadCacheTest method shouldEvictAfterPutIfAbsent.

@Test
public void shouldEvictAfterPutIfAbsent() throws Exception {
    final List<ThreadCache.DirtyEntry> received = new ArrayList<>();
    final String namespace = "namespace";
    final ThreadCache cache = new ThreadCache("testCache", 1, new MockStreamsMetrics(new Metrics()));
    cache.addDirtyEntryFlushListener(namespace, new ThreadCache.DirtyEntryFlushListener() {

        @Override
        public void apply(final List<ThreadCache.DirtyEntry> dirty) {
            received.addAll(dirty);
        }
    });
    cache.putIfAbsent(namespace, Bytes.wrap(new byte[] { 0 }), dirtyEntry(new byte[] { 5 }));
    cache.putIfAbsent(namespace, Bytes.wrap(new byte[] { 1 }), dirtyEntry(new byte[] { 6 }));
    cache.putIfAbsent(namespace, Bytes.wrap(new byte[] { 1 }), dirtyEntry(new byte[] { 6 }));
    assertEquals(cache.evicts(), 3);
    assertEquals(received.size(), 3);
}
Also used : MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) ArrayList(java.util.ArrayList) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Test(org.junit.Test)

Example 43 with Metrics

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

the class FetcherTest method testFetchMaxPollRecords.

@Test
public void testFetchMaxPollRecords() {
    Fetcher<byte[], byte[]> fetcher = createFetcher(subscriptions, new Metrics(time), 2);
    List<ConsumerRecord<byte[], byte[]>> records;
    subscriptions.assignFromUser(singleton(tp));
    subscriptions.seek(tp, 1);
    client.prepareResponse(matchesOffset(tp, 1), fetchResponse(this.records, Errors.NONE, 100L, 0));
    client.prepareResponse(matchesOffset(tp, 4), fetchResponse(this.nextRecords, Errors.NONE, 100L, 0));
    assertEquals(1, fetcher.sendFetches());
    consumerClient.poll(0);
    records = fetcher.fetchedRecords().get(tp);
    assertEquals(2, records.size());
    assertEquals(3L, subscriptions.position(tp).longValue());
    assertEquals(1, records.get(0).offset());
    assertEquals(2, records.get(1).offset());
    assertEquals(0, fetcher.sendFetches());
    consumerClient.poll(0);
    records = fetcher.fetchedRecords().get(tp);
    assertEquals(1, records.size());
    assertEquals(4L, subscriptions.position(tp).longValue());
    assertEquals(3, records.get(0).offset());
    assertTrue(fetcher.sendFetches() > 0);
    consumerClient.poll(0);
    records = fetcher.fetchedRecords().get(tp);
    assertEquals(2, records.size());
    assertEquals(6L, subscriptions.position(tp).longValue());
    assertEquals(4, records.get(0).offset());
    assertEquals(5, records.get(1).offset());
}
Also used : Metrics(org.apache.kafka.common.metrics.Metrics) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Test(org.junit.Test)

Example 44 with Metrics

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

the class ConsumerCoordinatorTest method testProtocolMetadataOrder.

@Test
public void testProtocolMetadataOrder() {
    RoundRobinAssignor roundRobin = new RoundRobinAssignor();
    RangeAssignor range = new RangeAssignor();
    try (Metrics metrics = new Metrics(time)) {
        ConsumerCoordinator coordinator = buildCoordinator(metrics, Arrays.<PartitionAssignor>asList(roundRobin, range), ConsumerConfig.DEFAULT_EXCLUDE_INTERNAL_TOPICS, false);
        List<ProtocolMetadata> metadata = coordinator.metadata();
        assertEquals(2, metadata.size());
        assertEquals(roundRobin.name(), metadata.get(0).name());
        assertEquals(range.name(), metadata.get(1).name());
    }
    try (Metrics metrics = new Metrics(time)) {
        ConsumerCoordinator coordinator = buildCoordinator(metrics, Arrays.<PartitionAssignor>asList(range, roundRobin), ConsumerConfig.DEFAULT_EXCLUDE_INTERNAL_TOPICS, false);
        List<ProtocolMetadata> metadata = coordinator.metadata();
        assertEquals(2, metadata.size());
        assertEquals(range.name(), metadata.get(0).name());
        assertEquals(roundRobin.name(), metadata.get(1).name());
    }
}
Also used : Metrics(org.apache.kafka.common.metrics.Metrics) RangeAssignor(org.apache.kafka.clients.consumer.RangeAssignor) RoundRobinAssignor(org.apache.kafka.clients.consumer.RoundRobinAssignor) ProtocolMetadata(org.apache.kafka.common.requests.JoinGroupRequest.ProtocolMetadata) Test(org.junit.Test)

Example 45 with Metrics

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

the class ConsumerCoordinatorTest method testAutoCommitDynamicAssignmentRebalance.

@Test
public void testAutoCommitDynamicAssignmentRebalance() {
    final String consumerId = "consumer";
    ConsumerCoordinator coordinator = buildCoordinator(new Metrics(), assignors, ConsumerConfig.DEFAULT_EXCLUDE_INTERNAL_TOPICS, true);
    subscriptions.subscribe(singleton(topic1), rebalanceListener);
    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady();
    // haven't joined, so should not cause a commit
    time.sleep(autoCommitIntervalMs);
    consumerClient.poll(0);
    client.prepareResponse(joinGroupFollowerResponse(1, consumerId, "leader", Errors.NONE));
    client.prepareResponse(syncGroupResponse(singletonList(t1p), Errors.NONE));
    coordinator.joinGroupIfNeeded();
    subscriptions.seek(t1p, 100);
    client.prepareResponse(offsetCommitResponse(Collections.singletonMap(t1p, Errors.NONE)));
    time.sleep(autoCommitIntervalMs);
    coordinator.poll(time.milliseconds());
    assertEquals(100L, subscriptions.committed(t1p).offset());
}
Also used : 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