Search in sources :

Example 1 with HeartbeatResponseData

use of org.apache.kafka.common.message.HeartbeatResponseData 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 2 with HeartbeatResponseData

use of org.apache.kafka.common.message.HeartbeatResponseData in project kafka by apache.

the class KafkaConsumerTest method prepareHeartbeatResponse.

private AtomicBoolean prepareHeartbeatResponse(MockClient client, Node coordinator, Errors error) {
    final AtomicBoolean heartbeatReceived = new AtomicBoolean(false);
    client.prepareResponseFrom(body -> {
        heartbeatReceived.set(true);
        return true;
    }, new HeartbeatResponse(new HeartbeatResponseData().setErrorCode(error.code())), coordinator);
    return heartbeatReceived;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HeartbeatResponse(org.apache.kafka.common.requests.HeartbeatResponse) HeartbeatResponseData(org.apache.kafka.common.message.HeartbeatResponseData)

Aggregations

HeartbeatResponseData (org.apache.kafka.common.message.HeartbeatResponseData)2 HeartbeatResponse (org.apache.kafka.common.requests.HeartbeatResponse)2 ByteBuffer (java.nio.ByteBuffer)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 MockClient (org.apache.kafka.clients.MockClient)1 ConsumerMetadata (org.apache.kafka.clients.consumer.internals.ConsumerMetadata)1 Node (org.apache.kafka.common.Node)1 TopicIdPartition (org.apache.kafka.common.TopicIdPartition)1 JoinGroupResponseData (org.apache.kafka.common.message.JoinGroupResponseData)1 FetchRequest (org.apache.kafka.common.requests.FetchRequest)1 JoinGroupResponse (org.apache.kafka.common.requests.JoinGroupResponse)1 Test (org.junit.jupiter.api.Test)1