Search in sources :

Example 6 with MockClient

use of org.apache.kafka.clients.MockClient in project kafka by apache.

the class KafkaConsumerTest method testSubscriptionChangesWithAutoCommitEnabled.

/**
     * Verify that when a consumer changes its topic subscription its assigned partitions
     * do not immediately change, and the latest consumed offsets of its to-be-revoked
     * partitions are properly committed (when auto-commit is enabled).
     * Upon unsubscribing from subscribed topics the consumer subscription and assignment
     * are both updated right away but its consumed offsets are not auto committed.
     */
@Test
public void testSubscriptionChangesWithAutoCommitEnabled() {
    int rebalanceTimeoutMs = 60000;
    int sessionTimeoutMs = 30000;
    int heartbeatIntervalMs = 3000;
    // adjust auto commit interval lower than heartbeat so we don't need to deal with
    // a concurrent heartbeat request
    int autoCommitIntervalMs = 1000;
    Time time = new MockTime();
    Map<String, Integer> tpCounts = new HashMap<>();
    tpCounts.put(topic, 1);
    tpCounts.put(topic2, 1);
    tpCounts.put(topic3, 1);
    Cluster cluster = TestUtils.singletonCluster(tpCounts);
    Node node = cluster.nodes().get(0);
    Metadata metadata = new Metadata(0, Long.MAX_VALUE);
    metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
    MockClient client = new MockClient(time, metadata);
    client.setNode(node);
    PartitionAssignor assignor = new RangeAssignor();
    final KafkaConsumer<String, String> consumer = newConsumer(time, client, metadata, assignor, rebalanceTimeoutMs, sessionTimeoutMs, heartbeatIntervalMs, true, autoCommitIntervalMs);
    // initial subscription
    consumer.subscribe(Arrays.asList(topic, topic2), getConsumerRebalanceListener(consumer));
    // verify that subscription has changed but assignment is still unchanged
    assertTrue(consumer.subscription().size() == 2);
    assertTrue(consumer.subscription().contains(topic) && consumer.subscription().contains(topic2));
    assertTrue(consumer.assignment().isEmpty());
    // mock rebalance responses
    Node coordinator = prepareRebalance(client, node, assignor, Arrays.asList(tp0, t2p0), null);
    consumer.poll(0);
    // verify that subscription is still the same, and now assignment has caught up
    assertTrue(consumer.subscription().size() == 2);
    assertTrue(consumer.subscription().contains(topic) && consumer.subscription().contains(topic2));
    assertTrue(consumer.assignment().size() == 2);
    assertTrue(consumer.assignment().contains(tp0) && consumer.assignment().contains(t2p0));
    // mock a response to the outstanding fetch so that we have data available on the next poll
    Map<TopicPartition, FetchInfo> fetches1 = new HashMap<>();
    fetches1.put(tp0, new FetchInfo(0, 1));
    fetches1.put(t2p0, new FetchInfo(0, 10));
    client.respondFrom(fetchResponse(fetches1), node);
    client.poll(0, time.milliseconds());
    ConsumerRecords<String, String> records = consumer.poll(0);
    // clear out the prefetch so it doesn't interfere with the rest of the test
    fetches1.put(tp0, new FetchInfo(1, 0));
    fetches1.put(t2p0, new FetchInfo(10, 0));
    client.respondFrom(fetchResponse(fetches1), node);
    client.poll(0, time.milliseconds());
    // verify that the fetch occurred as expected
    assertEquals(11, records.count());
    assertEquals(1L, consumer.position(tp0));
    assertEquals(10L, consumer.position(t2p0));
    // subscription change
    consumer.subscribe(Arrays.asList(topic, topic3), getConsumerRebalanceListener(consumer));
    // verify that subscription has changed but assignment is still unchanged
    assertTrue(consumer.subscription().size() == 2);
    assertTrue(consumer.subscription().contains(topic) && consumer.subscription().contains(topic3));
    assertTrue(consumer.assignment().size() == 2);
    assertTrue(consumer.assignment().contains(tp0) && consumer.assignment().contains(t2p0));
    // mock the offset commit response for to be revoked partitions
    Map<TopicPartition, Long> partitionOffsets1 = new HashMap<>();
    partitionOffsets1.put(tp0, 1L);
    partitionOffsets1.put(t2p0, 10L);
    AtomicBoolean commitReceived = prepareOffsetCommitResponse(client, coordinator, partitionOffsets1);
    // mock rebalance responses
    prepareRebalance(client, node, assignor, Arrays.asList(tp0, t3p0), coordinator);
    // mock a response to the next fetch from the new assignment
    Map<TopicPartition, FetchInfo> fetches2 = new HashMap<>();
    fetches2.put(tp0, new FetchInfo(1, 1));
    fetches2.put(t3p0, new FetchInfo(0, 100));
    client.prepareResponse(fetchResponse(fetches2));
    records = consumer.poll(0);
    // verify that the fetch occurred as expected
    assertEquals(101, records.count());
    assertEquals(2L, consumer.position(tp0));
    assertEquals(100L, consumer.position(t3p0));
    // verify that the offset commits occurred as expected
    assertTrue(commitReceived.get());
    // verify that subscription is still the same, and now assignment has caught up
    assertTrue(consumer.subscription().size() == 2);
    assertTrue(consumer.subscription().contains(topic) && consumer.subscription().contains(topic3));
    assertTrue(consumer.assignment().size() == 2);
    assertTrue(consumer.assignment().contains(tp0) && consumer.assignment().contains(t3p0));
    consumer.unsubscribe();
    // verify that subscription and assignment are both cleared
    assertTrue(consumer.subscription().isEmpty());
    assertTrue(consumer.assignment().isEmpty());
    client.requests().clear();
    consumer.close();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TopicPartition(org.apache.kafka.common.TopicPartition) PartitionAssignor(org.apache.kafka.clients.consumer.internals.PartitionAssignor) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 7 with MockClient

use of org.apache.kafka.clients.MockClient in project kafka by apache.

the class KafkaConsumerTest method testManualAssignmentChangeWithAutoCommitEnabled.

@Test
public void testManualAssignmentChangeWithAutoCommitEnabled() {
    int rebalanceTimeoutMs = 60000;
    int sessionTimeoutMs = 30000;
    int heartbeatIntervalMs = 3000;
    int autoCommitIntervalMs = 1000;
    Time time = new MockTime();
    Map<String, Integer> tpCounts = new HashMap<>();
    tpCounts.put(topic, 1);
    tpCounts.put(topic2, 1);
    Cluster cluster = TestUtils.singletonCluster(tpCounts);
    Node node = cluster.nodes().get(0);
    Metadata metadata = new Metadata(0, Long.MAX_VALUE);
    metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
    MockClient client = new MockClient(time, metadata);
    client.setNode(node);
    PartitionAssignor assignor = new RangeAssignor();
    final KafkaConsumer<String, String> consumer = newConsumer(time, client, metadata, assignor, rebalanceTimeoutMs, sessionTimeoutMs, heartbeatIntervalMs, true, autoCommitIntervalMs);
    // lookup coordinator
    client.prepareResponseFrom(new GroupCoordinatorResponse(Errors.NONE, node), node);
    Node coordinator = new Node(Integer.MAX_VALUE - node.id(), node.host(), node.port());
    // manual assignment
    consumer.assign(Arrays.asList(tp0));
    consumer.seekToBeginning(Arrays.asList(tp0));
    // fetch offset for one topic
    client.prepareResponseFrom(offsetResponse(Collections.singletonMap(tp0, 0L), Errors.NONE), coordinator);
    assertEquals(0, consumer.committed(tp0).offset());
    // verify that assignment immediately changes
    assertTrue(consumer.assignment().equals(Collections.singleton(tp0)));
    // there shouldn't be any need to lookup the coordinator or fetch committed offsets.
    // we just lookup the starting position and send the record fetch.
    client.prepareResponse(listOffsetsResponse(Collections.singletonMap(tp0, 10L), Errors.NONE));
    client.prepareResponse(fetchResponse(tp0, 10L, 1));
    ConsumerRecords<String, String> records = consumer.poll(0);
    assertEquals(1, records.count());
    assertEquals(11L, consumer.position(tp0));
    // mock the offset commit response for to be revoked partitions
    AtomicBoolean commitReceived = prepareOffsetCommitResponse(client, coordinator, tp0, 11);
    // new manual assignment
    consumer.assign(Arrays.asList(t2p0));
    // verify that assignment immediately changes
    assertTrue(consumer.assignment().equals(Collections.singleton(t2p0)));
    // verify that the offset commits occurred as expected
    assertTrue(commitReceived.get());
    client.requests().clear();
    consumer.close();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) GroupCoordinatorResponse(org.apache.kafka.common.requests.GroupCoordinatorResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PartitionAssignor(org.apache.kafka.clients.consumer.internals.PartitionAssignor) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 8 with MockClient

use of org.apache.kafka.clients.MockClient in project kafka by apache.

the class ConsumerCoordinatorTest method setup.

@Before
public void setup() {
    this.time = new MockTime();
    this.subscriptions = new SubscriptionState(OffsetResetStrategy.EARLIEST);
    this.metadata = new Metadata(0, Long.MAX_VALUE);
    this.metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
    this.client = new MockClient(time, metadata);
    this.consumerClient = new ConsumerNetworkClient(client, metadata, time, 100, 1000);
    this.metrics = new Metrics(time);
    this.rebalanceListener = new MockRebalanceListener();
    this.mockOffsetCommitCallback = new MockCommitCallback();
    this.partitionAssignor.clear();
    client.setNode(node);
    this.coordinator = buildCoordinator(metrics, assignors, ConsumerConfig.DEFAULT_EXCLUDE_INTERNAL_TOPICS, autoCommitEnabled);
}
Also used : Metrics(org.apache.kafka.common.metrics.Metrics) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) ProtocolMetadata(org.apache.kafka.common.requests.JoinGroupRequest.ProtocolMetadata) Metadata(org.apache.kafka.clients.Metadata) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) Before(org.junit.Before)

Example 9 with MockClient

use of org.apache.kafka.clients.MockClient in project kafka by apache.

the class KafkaConsumerTest method prepareHeartbeatResponse.

private AtomicBoolean prepareHeartbeatResponse(MockClient client, Node coordinator) {
    final AtomicBoolean heartbeatReceived = new AtomicBoolean(false);
    client.prepareResponseFrom(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            heartbeatReceived.set(true);
            return true;
        }
    }, new HeartbeatResponse(Errors.NONE), coordinator);
    return heartbeatReceived;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HeartbeatResponse(org.apache.kafka.common.requests.HeartbeatResponse) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) MockClient(org.apache.kafka.clients.MockClient)

Example 10 with MockClient

use of org.apache.kafka.clients.MockClient in project kafka by apache.

the class KafkaConsumerTest method testOffsetOfPausedPartitions.

@Test
public void testOffsetOfPausedPartitions() {
    int rebalanceTimeoutMs = 60000;
    int sessionTimeoutMs = 30000;
    int heartbeatIntervalMs = 3000;
    int autoCommitIntervalMs = 1000;
    Time time = new MockTime();
    Cluster cluster = TestUtils.singletonCluster(topic, 2);
    Node node = cluster.nodes().get(0);
    Metadata metadata = new Metadata(0, Long.MAX_VALUE);
    metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
    MockClient client = new MockClient(time, metadata);
    client.setNode(node);
    PartitionAssignor assignor = new RangeAssignor();
    final KafkaConsumer<String, String> consumer = newConsumer(time, client, metadata, assignor, rebalanceTimeoutMs, sessionTimeoutMs, heartbeatIntervalMs, true, autoCommitIntervalMs);
    // lookup coordinator
    client.prepareResponseFrom(new GroupCoordinatorResponse(Errors.NONE, node), node);
    Node coordinator = new Node(Integer.MAX_VALUE - node.id(), node.host(), node.port());
    // manual assignment
    Set<TopicPartition> partitions = Utils.mkSet(tp0, tp1);
    consumer.assign(partitions);
    // verify consumer's assignment
    assertTrue(consumer.assignment().equals(partitions));
    consumer.pause(partitions);
    consumer.seekToEnd(partitions);
    // fetch and verify committed offset of two partitions
    Map<TopicPartition, Long> offsets = new HashMap<>();
    offsets.put(tp0, 0L);
    offsets.put(tp1, 0L);
    client.prepareResponseFrom(offsetResponse(offsets, Errors.NONE), coordinator);
    assertEquals(0, consumer.committed(tp0).offset());
    assertEquals(0, consumer.committed(tp1).offset());
    // fetch and verify consumer's position in the two partitions
    client.prepareResponse(listOffsetsResponse(Collections.singletonMap(tp0, 3L), Errors.NONE));
    client.prepareResponse(listOffsetsResponse(Collections.singletonMap(tp1, 3L), Errors.NONE));
    assertEquals(3L, consumer.position(tp0));
    assertEquals(3L, consumer.position(tp1));
    client.requests().clear();
    consumer.unsubscribe();
    consumer.close();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) GroupCoordinatorResponse(org.apache.kafka.common.requests.GroupCoordinatorResponse) TopicPartition(org.apache.kafka.common.TopicPartition) PartitionAssignor(org.apache.kafka.clients.consumer.internals.PartitionAssignor) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Aggregations

MockClient (org.apache.kafka.clients.MockClient)23 Metadata (org.apache.kafka.clients.Metadata)19 Node (org.apache.kafka.common.Node)19 MockTime (org.apache.kafka.common.utils.MockTime)19 Cluster (org.apache.kafka.common.Cluster)17 PartitionAssignor (org.apache.kafka.clients.consumer.internals.PartitionAssignor)16 Time (org.apache.kafka.common.utils.Time)16 Test (org.junit.Test)16 HashMap (java.util.HashMap)11 LinkedHashMap (java.util.LinkedHashMap)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 TopicPartition (org.apache.kafka.common.TopicPartition)6 GroupCoordinatorResponse (org.apache.kafka.common.requests.GroupCoordinatorResponse)5 Metrics (org.apache.kafka.common.metrics.Metrics)3 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)3 Before (org.junit.Before)3 ClientRequest (org.apache.kafka.clients.ClientRequest)2 WakeupException (org.apache.kafka.common.errors.WakeupException)2 HeartbeatResponse (org.apache.kafka.common.requests.HeartbeatResponse)2 ProtocolMetadata (org.apache.kafka.common.requests.JoinGroupRequest.ProtocolMetadata)2