use of org.apache.kafka.common.requests.AbstractRequest in project kafka by apache.
the class KafkaConsumerTest method prepareRebalance.
private Node prepareRebalance(MockClient client, Node node, final Set<String> subscribedTopics, PartitionAssignor assignor, List<TopicPartition> partitions, Node coordinator) {
if (coordinator == null) {
// lookup coordinator
client.prepareResponseFrom(new GroupCoordinatorResponse(Errors.NONE, node), node);
coordinator = new Node(Integer.MAX_VALUE - node.id(), node.host(), node.port());
}
// join group
client.prepareResponseFrom(new MockClient.RequestMatcher() {
@Override
public boolean matches(AbstractRequest body) {
JoinGroupRequest joinGroupRequest = (JoinGroupRequest) body;
PartitionAssignor.Subscription subscription = ConsumerProtocol.deserializeSubscription(joinGroupRequest.groupProtocols().get(0).metadata());
return subscribedTopics.equals(new HashSet<>(subscription.topics()));
}
}, joinGroupFollowerResponse(assignor, 1, "memberId", "leaderId", Errors.NONE), coordinator);
// sync group
client.prepareResponseFrom(syncGroupResponse(partitions, Errors.NONE), coordinator);
return coordinator;
}
use of org.apache.kafka.common.requests.AbstractRequest 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;
}
use of org.apache.kafka.common.requests.AbstractRequest 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);
}
use of org.apache.kafka.common.requests.AbstractRequest in project apache-kafka-on-k8s by banzaicloud.
the class KafkaConsumerTest method prepareRebalance.
private Node prepareRebalance(MockClient client, Node node, final Set<String> subscribedTopics, PartitionAssignor assignor, List<TopicPartition> partitions, Node coordinator) {
if (coordinator == null) {
// lookup coordinator
client.prepareResponseFrom(new FindCoordinatorResponse(Errors.NONE, node), node);
coordinator = new Node(Integer.MAX_VALUE - node.id(), node.host(), node.port());
}
// join group
client.prepareResponseFrom(new MockClient.RequestMatcher() {
@Override
public boolean matches(AbstractRequest body) {
JoinGroupRequest joinGroupRequest = (JoinGroupRequest) body;
PartitionAssignor.Subscription subscription = ConsumerProtocol.deserializeSubscription(joinGroupRequest.groupProtocols().get(0).metadata());
return subscribedTopics.equals(new HashSet<>(subscription.topics()));
}
}, joinGroupFollowerResponse(assignor, 1, "memberId", "leaderId", Errors.NONE), coordinator);
// sync group
client.prepareResponseFrom(syncGroupResponse(partitions, Errors.NONE), coordinator);
return coordinator;
}
use of org.apache.kafka.common.requests.AbstractRequest in project apache-kafka-on-k8s by banzaicloud.
the class KafkaConsumerTest method testFetchProgressWithMissingPartitionPosition.
@Test
public void testFetchProgressWithMissingPartitionPosition() {
// Verifies that we can make progress on one partition while we are awaiting
// a reset on another partition.
Time time = new MockTime();
Cluster cluster = TestUtils.singletonCluster(topic, 2);
Node node = cluster.nodes().get(0);
Metadata metadata = createMetadata();
metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
MockClient client = new MockClient(time, metadata);
client.setNode(node);
KafkaConsumer<String, String> consumer = newConsumerNoAutoCommit(time, client, metadata);
consumer.assign(Arrays.asList(tp0, tp1));
consumer.seekToEnd(singleton(tp0));
consumer.seekToBeginning(singleton(tp1));
client.prepareResponse(new MockClient.RequestMatcher() {
@Override
public boolean matches(AbstractRequest body) {
ListOffsetRequest request = (ListOffsetRequest) body;
Map<TopicPartition, Long> expectedTimestamps = new HashMap<>();
expectedTimestamps.put(tp0, ListOffsetRequest.LATEST_TIMESTAMP);
expectedTimestamps.put(tp1, ListOffsetRequest.EARLIEST_TIMESTAMP);
return expectedTimestamps.equals(request.partitionTimestamps());
}
}, listOffsetsResponse(Collections.singletonMap(tp0, 50L), Collections.singletonMap(tp1, Errors.NOT_LEADER_FOR_PARTITION)));
client.prepareResponse(new MockClient.RequestMatcher() {
@Override
public boolean matches(AbstractRequest body) {
FetchRequest request = (FetchRequest) body;
return request.fetchData().keySet().equals(singleton(tp0)) && request.fetchData().get(tp0).fetchOffset == 50L;
}
}, fetchResponse(tp0, 50L, 5));
ConsumerRecords<String, String> records = consumer.poll(5);
assertEquals(5, records.count());
assertEquals(singleton(tp0), records.partitions());
}
Aggregations