Search in sources :

Example 6 with HeartbeatResponse

use of org.apache.kafka.common.requests.HeartbeatResponse 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)

Example 7 with HeartbeatResponse

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

the class ConsumerNetworkClientTest method send.

@Test
public void send() {
    client.prepareResponse(heartbeatResponse(Errors.NONE));
    RequestFuture<ClientResponse> future = consumerClient.send(node, heartbeat());
    assertEquals(1, consumerClient.pendingRequestCount());
    assertEquals(1, consumerClient.pendingRequestCount(node));
    assertFalse(future.isDone());
    consumerClient.poll(future);
    assertTrue(future.isDone());
    assertTrue(future.succeeded());
    ClientResponse clientResponse = future.value();
    HeartbeatResponse response = (HeartbeatResponse) clientResponse.responseBody();
    assertEquals(Errors.NONE, response.error());
}
Also used : ClientResponse(org.apache.kafka.clients.ClientResponse) HeartbeatResponse(org.apache.kafka.common.requests.HeartbeatResponse) Test(org.junit.Test)

Example 8 with HeartbeatResponse

use of org.apache.kafka.common.requests.HeartbeatResponse 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)

Example 9 with HeartbeatResponse

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

the class ConsumerNetworkClientTest method sendExpiry.

@Test
public void sendExpiry() {
    int requestTimeoutMs = 10;
    final AtomicBoolean isReady = new AtomicBoolean();
    final AtomicBoolean disconnected = new AtomicBoolean();
    client = new MockClient(time, metadata) {

        @Override
        public boolean ready(Node node, long now) {
            if (isReady.get())
                return super.ready(node, now);
            else
                return false;
        }

        @Override
        public boolean connectionFailed(Node node) {
            return disconnected.get();
        }
    };
    // Queue first send, sleep long enough for this to expire and then queue second send
    consumerClient = new ConsumerNetworkClient(new LogContext(), client, metadata, time, 100, requestTimeoutMs, Integer.MAX_VALUE);
    RequestFuture<ClientResponse> future1 = consumerClient.send(node, heartbeat());
    assertEquals(1, consumerClient.pendingRequestCount());
    assertEquals(1, consumerClient.pendingRequestCount(node));
    assertFalse(future1.isDone());
    time.sleep(requestTimeoutMs + 1);
    RequestFuture<ClientResponse> future2 = consumerClient.send(node, heartbeat());
    assertEquals(2, consumerClient.pendingRequestCount());
    assertEquals(2, consumerClient.pendingRequestCount(node));
    assertFalse(future2.isDone());
    // First send should have expired and second send still pending
    consumerClient.poll(time.timer(0));
    assertTrue(future1.isDone());
    assertFalse(future1.succeeded());
    assertEquals(1, consumerClient.pendingRequestCount());
    assertEquals(1, consumerClient.pendingRequestCount(node));
    assertFalse(future2.isDone());
    // Enable send, the un-expired send should succeed on poll
    isReady.set(true);
    client.prepareResponse(heartbeatResponse(Errors.NONE));
    consumerClient.poll(future2);
    ClientResponse clientResponse = future2.value();
    HeartbeatResponse response = (HeartbeatResponse) clientResponse.responseBody();
    assertEquals(Errors.NONE, response.error());
    // Disable ready flag to delay send and queue another send. Disconnection should remove pending send
    isReady.set(false);
    RequestFuture<ClientResponse> future3 = consumerClient.send(node, heartbeat());
    assertEquals(1, consumerClient.pendingRequestCount());
    assertEquals(1, consumerClient.pendingRequestCount(node));
    disconnected.set(true);
    consumerClient.poll(time.timer(0));
    assertTrue(future3.isDone());
    assertFalse(future3.succeeded());
    assertEquals(0, consumerClient.pendingRequestCount());
    assertEquals(0, consumerClient.pendingRequestCount(node));
}
Also used : ClientResponse(org.apache.kafka.clients.ClientResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HeartbeatResponse(org.apache.kafka.common.requests.HeartbeatResponse) Node(org.apache.kafka.common.Node) LogContext(org.apache.kafka.common.utils.LogContext) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.jupiter.api.Test)

Aggregations

HeartbeatResponse (org.apache.kafka.common.requests.HeartbeatResponse)9 MockClient (org.apache.kafka.clients.MockClient)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 ClientResponse (org.apache.kafka.clients.ClientResponse)4 Node (org.apache.kafka.common.Node)4 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)3 Test (org.junit.Test)3 Test (org.junit.jupiter.api.Test)3 ByteBuffer (java.nio.ByteBuffer)2 HeartbeatResponseData (org.apache.kafka.common.message.HeartbeatResponseData)2 FetchRequest (org.apache.kafka.common.requests.FetchRequest)2 JoinGroupResponse (org.apache.kafka.common.requests.JoinGroupResponse)2 LogContext (org.apache.kafka.common.utils.LogContext)2 Metadata (org.apache.kafka.clients.Metadata)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 JoinGroupResponseData (org.apache.kafka.common.message.JoinGroupResponseData)1 FindCoordinatorResponse (org.apache.kafka.common.requests.FindCoordinatorResponse)1