use of org.apache.kafka.common.message.LeaveGroupResponseData in project kafka by apache.
the class LeaveGroupResponseTest method testEqualityWithSerialization.
@Test
public void testEqualityWithSerialization() {
LeaveGroupResponseData responseData = new LeaveGroupResponseData().setErrorCode(Errors.NONE.code()).setThrottleTimeMs(throttleTimeMs);
for (short version : ApiKeys.LEAVE_GROUP.allVersions()) {
LeaveGroupResponse primaryResponse = LeaveGroupResponse.parse(MessageUtil.toByteBuffer(responseData, version), version);
LeaveGroupResponse secondaryResponse = LeaveGroupResponse.parse(MessageUtil.toByteBuffer(responseData, version), version);
assertEquals(primaryResponse, primaryResponse);
assertEquals(primaryResponse, secondaryResponse);
assertEquals(primaryResponse.hashCode(), secondaryResponse.hashCode());
}
}
use of org.apache.kafka.common.message.LeaveGroupResponseData in project kafka by apache.
the class ConsumerCoordinatorTest method testPendingMemberShouldLeaveGroup.
/**
* This test checks if a consumer that has a valid member ID but an invalid generation
* ({@link org.apache.kafka.clients.consumer.internals.AbstractCoordinator.Generation#NO_GENERATION})
* can still execute a leave group request. Such a situation may arise when a consumer has initiated a JoinGroup
* request without a memberId, but is shutdown or restarted before it has a chance to initiate and complete the
* second request.
*/
@Test
public void testPendingMemberShouldLeaveGroup() {
final String consumerId = "consumer-id";
subscriptions.subscribe(singleton(topic1), rebalanceListener);
client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));
// here we return a DEFAULT_GENERATION_ID, but valid member id and leader id.
client.prepareResponse(joinGroupFollowerResponse(-1, consumerId, "leader-id", Errors.MEMBER_ID_REQUIRED));
// execute join group
coordinator.joinGroupIfNeeded(time.timer(0));
final AtomicBoolean received = new AtomicBoolean(false);
client.prepareResponse(body -> {
received.set(true);
LeaveGroupRequest leaveRequest = (LeaveGroupRequest) body;
return validateLeaveGroup(groupId, consumerId, leaveRequest);
}, new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code())));
coordinator.maybeLeaveGroup("pending member leaves");
assertTrue(received.get());
}
use of org.apache.kafka.common.message.LeaveGroupResponseData in project kafka by apache.
the class ConsumerCoordinatorTest method testMaybeLeaveGroup.
@Test
public void testMaybeLeaveGroup() {
subscriptions.subscribe(singleton(topic1), rebalanceListener);
joinAsFollowerAndReceiveAssignment(coordinator, singletonList(t1p));
final AtomicBoolean received = new AtomicBoolean(false);
client.prepareResponse(body -> {
received.set(true);
LeaveGroupRequest leaveRequest = (LeaveGroupRequest) body;
return validateLeaveGroup(groupId, consumerId, leaveRequest);
}, new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code())));
coordinator.maybeLeaveGroup("test maybe leave group");
assertTrue(received.get());
AbstractCoordinator.Generation generation = coordinator.generationIfStable();
assertNull(generation);
}
use of org.apache.kafka.common.message.LeaveGroupResponseData in project kafka by apache.
the class ConsumerCoordinatorTest method testLeaveGroupOnClose.
@Test
public void testLeaveGroupOnClose() {
subscriptions.subscribe(singleton(topic1), rebalanceListener);
joinAsFollowerAndReceiveAssignment(coordinator, singletonList(t1p));
final AtomicBoolean received = new AtomicBoolean(false);
client.prepareResponse(body -> {
received.set(true);
LeaveGroupRequest leaveRequest = (LeaveGroupRequest) body;
return validateLeaveGroup(groupId, consumerId, leaveRequest);
}, new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code())));
coordinator.close(time.timer(0));
assertTrue(received.get());
}
use of org.apache.kafka.common.message.LeaveGroupResponseData in project kafka by apache.
the class KafkaAdminClientTest method testRemoveMembersFromGroupRetryBackoff.
@Test
public void testRemoveMembersFromGroupRetryBackoff() throws Exception {
MockTime time = new MockTime();
int retryBackoff = 100;
try (final AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(time, mockCluster(3, 0), newStrMap(AdminClientConfig.RETRY_BACKOFF_MS_CONFIG, "" + retryBackoff))) {
MockClient mockClient = env.kafkaClient();
mockClient.setNodeApiVersions(NodeApiVersions.create());
AtomicLong firstAttemptTime = new AtomicLong(0);
AtomicLong secondAttemptTime = new AtomicLong(0);
mockClient.prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
env.kafkaClient().prepareResponse(body -> {
firstAttemptTime.set(time.milliseconds());
return true;
}, new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NOT_COORDINATOR.code())));
mockClient.prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
MemberResponse responseOne = new MemberResponse().setGroupInstanceId("instance-1").setErrorCode(Errors.NONE.code());
env.kafkaClient().prepareResponse(body -> {
secondAttemptTime.set(time.milliseconds());
return true;
}, new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code()).setMembers(Collections.singletonList(responseOne))));
Collection<MemberToRemove> membersToRemove = singletonList(new MemberToRemove("instance-1"));
final KafkaFuture<Void> future = env.adminClient().removeMembersFromConsumerGroup(GROUP_ID, new RemoveMembersFromConsumerGroupOptions(membersToRemove)).all();
TestUtils.waitForCondition(() -> mockClient.numAwaitingResponses() == 1, "Failed awaiting RemoveMembersFromGroup first request failure");
TestUtils.waitForCondition(() -> ((KafkaAdminClient) env.adminClient()).numPendingCalls() == 1, "Failed to add retry RemoveMembersFromGroup call on first failure");
time.sleep(retryBackoff);
future.get();
long actualRetryBackoff = secondAttemptTime.get() - firstAttemptTime.get();
assertEquals(retryBackoff, actualRetryBackoff, "RemoveMembersFromGroup retry did not await expected backoff!");
}
}
Aggregations