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) {
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;
}
use of org.apache.kafka.common.requests.HeartbeatResponse in project apache-kafka-on-k8s by banzaicloud.
the class ConsumerNetworkClientTest method sendExpiry.
@Test
public void sendExpiry() throws InterruptedException {
long unsentExpiryMs = 10;
final AtomicBoolean isReady = new AtomicBoolean();
final AtomicBoolean disconnected = new AtomicBoolean();
client = new MockClient(time) {
@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, unsentExpiryMs, Integer.MAX_VALUE);
RequestFuture<ClientResponse> future1 = consumerClient.send(node, heartbeat());
assertEquals(1, consumerClient.pendingRequestCount());
assertEquals(1, consumerClient.pendingRequestCount(node));
assertFalse(future1.isDone());
time.sleep(unsentExpiryMs + 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(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(0);
assertTrue(future3.isDone());
assertFalse(future3.succeeded());
assertEquals(0, consumerClient.pendingRequestCount());
assertEquals(0, consumerClient.pendingRequestCount(node));
}
use of org.apache.kafka.common.requests.HeartbeatResponse 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));
}
use of org.apache.kafka.common.requests.HeartbeatResponse in project kafka by apache.
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());
}
use of org.apache.kafka.common.requests.HeartbeatResponse in project apache-kafka-on-k8s by banzaicloud.
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;
}
Aggregations