Search in sources :

Example 1 with JoinGroupResponse

use of org.apache.kafka.common.requests.JoinGroupResponse in project apache-kafka-on-k8s by banzaicloud.

the class ConsumerCoordinatorTest method joinGroupLeaderResponse.

private JoinGroupResponse joinGroupLeaderResponse(int generationId, String memberId, Map<String, List<String>> subscriptions, Errors error) {
    Map<String, ByteBuffer> metadata = new HashMap<>();
    for (Map.Entry<String, List<String>> subscriptionEntry : subscriptions.entrySet()) {
        PartitionAssignor.Subscription subscription = new PartitionAssignor.Subscription(subscriptionEntry.getValue());
        ByteBuffer buf = ConsumerProtocol.serializeSubscription(subscription);
        metadata.put(subscriptionEntry.getKey(), buf);
    }
    return new JoinGroupResponse(error, generationId, partitionAssignor.name(), memberId, memberId, metadata);
}
Also used : HashMap(java.util.HashMap) JoinGroupResponse(org.apache.kafka.common.requests.JoinGroupResponse) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap)

Example 2 with JoinGroupResponse

use of org.apache.kafka.common.requests.JoinGroupResponse in project kafka by apache.

the class KafkaConsumerTest method testShouldAttemptToRejoinGroupAfterSyncGroupFailed.

@Test
public void testShouldAttemptToRejoinGroupAfterSyncGroupFailed() throws Exception {
    ConsumerMetadata metadata = createMetadata(subscription);
    MockClient client = new MockClient(time, metadata);
    initMetadata(client, Collections.singletonMap(topic, 1));
    Node node = metadata.fetch().nodes().get(0);
    KafkaConsumer<String, String> consumer = newConsumer(time, client, subscription, metadata, assignor, false, groupInstanceId);
    consumer.subscribe(singleton(topic), getConsumerRebalanceListener(consumer));
    client.prepareResponseFrom(FindCoordinatorResponse.prepareResponse(Errors.NONE, groupId, node), node);
    Node coordinator = new Node(Integer.MAX_VALUE - node.id(), node.host(), node.port());
    client.prepareResponseFrom(joinGroupFollowerResponse(assignor, 1, memberId, leaderId, Errors.NONE), coordinator);
    client.prepareResponseFrom(syncGroupResponse(singletonList(tp0), Errors.NONE), coordinator);
    client.prepareResponseFrom(fetchResponse(tp0, 0, 1), node);
    client.prepareResponseFrom(fetchResponse(tp0, 1, 0), node);
    consumer.updateAssignmentMetadataIfNeeded(time.timer(Long.MAX_VALUE));
    consumer.poll(Duration.ZERO);
    // heartbeat fails due to rebalance in progress
    client.prepareResponseFrom(body -> true, new HeartbeatResponse(new HeartbeatResponseData().setErrorCode(Errors.REBALANCE_IN_PROGRESS.code())), coordinator);
    // join group
    final ByteBuffer byteBuffer = ConsumerProtocol.serializeSubscription(new ConsumerPartitionAssignor.Subscription(singletonList(topic)));
    // This member becomes the leader
    final JoinGroupResponse leaderResponse = new JoinGroupResponse(new JoinGroupResponseData().setErrorCode(Errors.NONE.code()).setGenerationId(1).setProtocolName(assignor.name()).setLeader(memberId).setMemberId(memberId).setMembers(Collections.singletonList(new JoinGroupResponseData.JoinGroupResponseMember().setMemberId(memberId).setMetadata(byteBuffer.array()))));
    client.prepareResponseFrom(leaderResponse, coordinator);
    // sync group fails due to disconnect
    client.prepareResponseFrom(syncGroupResponse(singletonList(tp0), Errors.NONE), coordinator, true);
    // should try and find the new coordinator
    client.prepareResponseFrom(FindCoordinatorResponse.prepareResponse(Errors.NONE, groupId, node), node);
    // rejoin group
    client.prepareResponseFrom(joinGroupFollowerResponse(assignor, 1, memberId, leaderId, Errors.NONE), coordinator);
    client.prepareResponseFrom(syncGroupResponse(singletonList(tp0), Errors.NONE), coordinator);
    client.prepareResponseFrom(body -> body instanceof FetchRequest && ((FetchRequest) body).fetchData(topicNames).containsKey(new TopicIdPartition(topicId, tp0)), fetchResponse(tp0, 1, 1), node);
    time.sleep(heartbeatIntervalMs);
    Thread.sleep(heartbeatIntervalMs);
    consumer.updateAssignmentMetadataIfNeeded(time.timer(Long.MAX_VALUE));
    final ConsumerRecords<String, String> records = consumer.poll(Duration.ZERO);
    assertFalse(records.isEmpty());
    consumer.close(Duration.ofMillis(0));
}
Also used : ConsumerMetadata(org.apache.kafka.clients.consumer.internals.ConsumerMetadata) HeartbeatResponse(org.apache.kafka.common.requests.HeartbeatResponse) JoinGroupResponse(org.apache.kafka.common.requests.JoinGroupResponse) JoinGroupResponseData(org.apache.kafka.common.message.JoinGroupResponseData) Node(org.apache.kafka.common.Node) TopicIdPartition(org.apache.kafka.common.TopicIdPartition) ByteBuffer(java.nio.ByteBuffer) FetchRequest(org.apache.kafka.common.requests.FetchRequest) HeartbeatResponseData(org.apache.kafka.common.message.HeartbeatResponseData) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.jupiter.api.Test)

Example 3 with JoinGroupResponse

use of org.apache.kafka.common.requests.JoinGroupResponse in project kafka by apache.

the class WorkerCoordinatorTest method joinGroupLeaderResponse.

private JoinGroupResponse joinGroupLeaderResponse(int generationId, String memberId, Map<String, Long> configOffsets, Errors error) {
    List<JoinGroupResponseData.JoinGroupResponseMember> metadata = new ArrayList<>();
    for (Map.Entry<String, Long> configStateEntry : configOffsets.entrySet()) {
        // We need a member URL, but it doesn't matter for the purposes of this test. Just set it to the member ID
        String memberUrl = configStateEntry.getKey();
        long configOffset = configStateEntry.getValue();
        ByteBuffer buf = ConnectProtocol.serializeMetadata(new ConnectProtocol.WorkerState(memberUrl, configOffset));
        metadata.add(new JoinGroupResponseData.JoinGroupResponseMember().setMemberId(configStateEntry.getKey()).setMetadata(buf.array()));
    }
    return new JoinGroupResponse(new JoinGroupResponseData().setErrorCode(error.code()).setGenerationId(generationId).setProtocolName(EAGER.protocol()).setLeader(memberId).setMemberId(memberId).setMembers(metadata));
}
Also used : JoinGroupResponseData(org.apache.kafka.common.message.JoinGroupResponseData) JoinGroupResponse(org.apache.kafka.common.requests.JoinGroupResponse) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with JoinGroupResponse

use of org.apache.kafka.common.requests.JoinGroupResponse in project kafka by apache.

the class ConsumerCoordinatorTest method joinGroupLeaderResponse.

private JoinGroupResponse joinGroupLeaderResponse(int generationId, String memberId, Map<String, List<String>> subscriptions, Errors error) {
    Map<String, ByteBuffer> metadata = new HashMap<>();
    for (Map.Entry<String, List<String>> subscriptionEntry : subscriptions.entrySet()) {
        PartitionAssignor.Subscription subscription = new PartitionAssignor.Subscription(subscriptionEntry.getValue());
        ByteBuffer buf = ConsumerProtocol.serializeSubscription(subscription);
        metadata.put(subscriptionEntry.getKey(), buf);
    }
    return new JoinGroupResponse(error, generationId, partitionAssignor.name(), memberId, memberId, metadata);
}
Also used : HashMap(java.util.HashMap) JoinGroupResponse(org.apache.kafka.common.requests.JoinGroupResponse) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ByteBuffer(java.nio.ByteBuffer) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap)

Example 5 with JoinGroupResponse

use of org.apache.kafka.common.requests.JoinGroupResponse in project apache-kafka-on-k8s by banzaicloud.

the class KafkaConsumerTest method shouldAttemptToRejoinGroupAfterSyncGroupFailed.

@Test
public void shouldAttemptToRejoinGroupAfterSyncGroupFailed() throws Exception {
    Time time = new MockTime();
    Cluster cluster = TestUtils.singletonCluster(topic, 1);
    Node node = cluster.nodes().get(0);
    Metadata metadata = new Metadata(0, Long.MAX_VALUE, false);
    metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
    MockClient client = new MockClient(time, metadata);
    client.setNode(node);
    PartitionAssignor assignor = new RoundRobinAssignor();
    KafkaConsumer<String, String> consumer = newConsumer(time, client, metadata, assignor, false);
    consumer.subscribe(singleton(topic), getConsumerRebalanceListener(consumer));
    client.prepareResponseFrom(new FindCoordinatorResponse(Errors.NONE, node), node);
    Node coordinator = new Node(Integer.MAX_VALUE - node.id(), node.host(), node.port());
    client.prepareResponseFrom(joinGroupFollowerResponse(assignor, 1, "memberId", "leaderId", Errors.NONE), coordinator);
    client.prepareResponseFrom(syncGroupResponse(singletonList(tp0), Errors.NONE), coordinator);
    client.prepareResponseFrom(fetchResponse(tp0, 0, 1), node);
    client.prepareResponseFrom(fetchResponse(tp0, 1, 0), node);
    consumer.poll(0);
    // heartbeat fails due to rebalance in progress
    client.prepareResponseFrom(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            return true;
        }
    }, new HeartbeatResponse(Errors.REBALANCE_IN_PROGRESS), coordinator);
    // join group
    final ByteBuffer byteBuffer = ConsumerProtocol.serializeSubscription(new PartitionAssignor.Subscription(singletonList(topic)));
    // This member becomes the leader
    final JoinGroupResponse leaderResponse = new JoinGroupResponse(Errors.NONE, 1, assignor.name(), "memberId", "memberId", Collections.singletonMap("memberId", byteBuffer));
    client.prepareResponseFrom(leaderResponse, coordinator);
    // sync group fails due to disconnect
    client.prepareResponseFrom(syncGroupResponse(singletonList(tp0), Errors.NONE), coordinator, true);
    // should try and find the new coordinator
    client.prepareResponseFrom(new FindCoordinatorResponse(Errors.NONE, node), node);
    // rejoin group
    client.prepareResponseFrom(joinGroupFollowerResponse(assignor, 1, "memberId", "leaderId", Errors.NONE), coordinator);
    client.prepareResponseFrom(syncGroupResponse(singletonList(tp0), Errors.NONE), coordinator);
    client.prepareResponseFrom(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(final AbstractRequest body) {
            return body instanceof FetchRequest && ((FetchRequest) body).fetchData().containsKey(tp0);
        }
    }, fetchResponse(tp0, 1, 1), node);
    time.sleep(heartbeatIntervalMs);
    Thread.sleep(heartbeatIntervalMs);
    final ConsumerRecords<String, String> records = consumer.poll(0);
    assertFalse(records.isEmpty());
    consumer.close(0, TimeUnit.MILLISECONDS);
}
Also used : HeartbeatResponse(org.apache.kafka.common.requests.HeartbeatResponse) FindCoordinatorResponse(org.apache.kafka.common.requests.FindCoordinatorResponse) JoinGroupResponse(org.apache.kafka.common.requests.JoinGroupResponse) Node(org.apache.kafka.common.Node) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) 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) ByteBuffer(java.nio.ByteBuffer) FetchRequest(org.apache.kafka.common.requests.FetchRequest) 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

JoinGroupResponse (org.apache.kafka.common.requests.JoinGroupResponse)8 ByteBuffer (java.nio.ByteBuffer)7 HashMap (java.util.HashMap)5 Map (java.util.Map)5 ArrayList (java.util.ArrayList)3 Collections.singletonList (java.util.Collections.singletonList)3 Collections.singletonMap (java.util.Collections.singletonMap)3 List (java.util.List)3 JoinGroupResponseData (org.apache.kafka.common.message.JoinGroupResponseData)3 MockClient (org.apache.kafka.clients.MockClient)2 Node (org.apache.kafka.common.Node)2 FetchRequest (org.apache.kafka.common.requests.FetchRequest)2 HeartbeatResponse (org.apache.kafka.common.requests.HeartbeatResponse)2 Collections.emptyList (java.util.Collections.emptyList)1 Metadata (org.apache.kafka.clients.Metadata)1 ConsumerPartitionAssignor (org.apache.kafka.clients.consumer.ConsumerPartitionAssignor)1 ConsumerMetadata (org.apache.kafka.clients.consumer.internals.ConsumerMetadata)1 PartitionAssignor (org.apache.kafka.clients.consumer.internals.PartitionAssignor)1 Cluster (org.apache.kafka.common.Cluster)1 TopicIdPartition (org.apache.kafka.common.TopicIdPartition)1