use of org.apache.kafka.common.requests.JoinGroupRequest 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.JoinGroupRequest 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.JoinGroupRequest in project kafka by apache.
the class KafkaConsumerTest method prepareRebalance.
private Node prepareRebalance(MockClient client, Node node, final Set<String> subscribedTopics, ConsumerPartitionAssignor assignor, List<TopicPartition> partitions, Node coordinator) {
if (coordinator == null) {
// lookup coordinator
client.prepareResponseFrom(FindCoordinatorResponse.prepareResponse(Errors.NONE, groupId, node), node);
coordinator = new Node(Integer.MAX_VALUE - node.id(), node.host(), node.port());
}
// join group
client.prepareResponseFrom(body -> {
JoinGroupRequest joinGroupRequest = (JoinGroupRequest) body;
Iterator<JoinGroupRequestData.JoinGroupRequestProtocol> protocolIterator = joinGroupRequest.data().protocols().iterator();
assertTrue(protocolIterator.hasNext());
ByteBuffer protocolMetadata = ByteBuffer.wrap(protocolIterator.next().metadata());
ConsumerPartitionAssignor.Subscription subscription = ConsumerProtocol.deserializeSubscription(protocolMetadata);
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.JoinGroupRequest in project kafka by apache.
the class ConsumerCoordinatorTest method testIllegalGenerationOnSyncGroup.
@Test
public void testIllegalGenerationOnSyncGroup() {
subscriptions.subscribe(singleton(topic1), rebalanceListener);
client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));
// join initially, but let coordinator rebalance on sync
client.prepareResponse(joinGroupFollowerResponse(1, consumerId, "leader", Errors.NONE));
client.prepareResponse(syncGroupResponse(Collections.emptyList(), Errors.ILLEGAL_GENERATION));
// then let the full join/sync finish successfully
client.prepareResponse(body -> {
JoinGroupRequest joinRequest = (JoinGroupRequest) body;
// member ID should not be reset under ILLEGAL_GENERATION error
return joinRequest.data().memberId().equals(consumerId);
}, joinGroupFollowerResponse(2, consumerId, "leader", Errors.NONE));
client.prepareResponse(syncGroupResponse(singletonList(t1p), Errors.NONE));
coordinator.joinGroupIfNeeded(time.timer(Long.MAX_VALUE));
assertFalse(coordinator.rejoinNeededOrPending());
assertEquals(singleton(t1p), subscriptions.assignedPartitions());
}
use of org.apache.kafka.common.requests.JoinGroupRequest in project kafka by apache.
the class AbstractCoordinatorTest method expectJoinGroup.
private void expectJoinGroup(String expectedMemberId, String expectedReason, int responseGeneration, String responseMemberId) {
JoinGroupResponse response = joinGroupFollowerResponse(responseGeneration, responseMemberId, "leaderId", Errors.NONE, PROTOCOL_TYPE);
mockClient.prepareResponse(body -> {
if (!(body instanceof JoinGroupRequest)) {
return false;
}
JoinGroupRequestData joinGroupRequest = ((JoinGroupRequest) body).data();
// abstract coordinator never sets reason to null
String actualReason = joinGroupRequest.reason();
boolean isReasonMatching = expectedReason == null || expectedReason.equals(actualReason);
return joinGroupRequest.memberId().equals(expectedMemberId) && joinGroupRequest.protocolType().equals(PROTOCOL_TYPE) && isReasonMatching;
}, response);
}
Aggregations