use of org.apache.kafka.common.message.LeaveGroupResponseData in project kafka by apache.
the class KafkaAdminClientTest method testRemoveMembersFromGroupRetriableErrors.
@Test
public void testRemoveMembersFromGroupRetriableErrors() throws Exception {
try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(mockCluster(1, 0))) {
env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
env.kafkaClient().prepareResponse(new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.COORDINATOR_LOAD_IN_PROGRESS.code())));
/*
* We need to return two responses here, one for NOT_COORDINATOR call when calling remove member
* api using coordinator that has moved. This will retry whole operation. So we need to again respond with a
* FindCoordinatorResponse.
*
* And the same reason for the following COORDINATOR_NOT_AVAILABLE error response
*/
env.kafkaClient().prepareResponse(new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NOT_COORDINATOR.code())));
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
env.kafkaClient().prepareResponse(new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.COORDINATOR_NOT_AVAILABLE.code())));
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
MemberResponse memberResponse = new MemberResponse().setGroupInstanceId("instance-1").setErrorCode(Errors.NONE.code());
env.kafkaClient().prepareResponse(new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code()).setMembers(Collections.singletonList(memberResponse))));
MemberToRemove memberToRemove = new MemberToRemove("instance-1");
Collection<MemberToRemove> membersToRemove = singletonList(memberToRemove);
final RemoveMembersFromConsumerGroupResult result = env.adminClient().removeMembersFromConsumerGroup(GROUP_ID, new RemoveMembersFromConsumerGroupOptions(membersToRemove));
assertNull(result.all().get());
assertNull(result.memberResult(memberToRemove).get());
}
}
use of org.apache.kafka.common.message.LeaveGroupResponseData in project kafka by apache.
the class KafkaAdminClientTest method testRemoveMembersFromGroup.
@Test
public void testRemoveMembersFromGroup() throws Exception {
try (AdminClientUnitTestEnv env = mockClientEnv()) {
final String instanceOne = "instance-1";
final String instanceTwo = "instance-2";
env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
// Retriable FindCoordinatorResponse errors should be retried
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.COORDINATOR_LOAD_IN_PROGRESS, Node.noNode()));
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
// Retriable errors should be retried
env.kafkaClient().prepareResponse(new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.COORDINATOR_LOAD_IN_PROGRESS.code())));
// Inject a top-level non-retriable error
env.kafkaClient().prepareResponse(new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.UNKNOWN_SERVER_ERROR.code())));
Collection<MemberToRemove> membersToRemove = Arrays.asList(new MemberToRemove(instanceOne), new MemberToRemove(instanceTwo));
final RemoveMembersFromConsumerGroupResult unknownErrorResult = env.adminClient().removeMembersFromConsumerGroup(GROUP_ID, new RemoveMembersFromConsumerGroupOptions(membersToRemove));
MemberToRemove memberOne = new MemberToRemove(instanceOne);
MemberToRemove memberTwo = new MemberToRemove(instanceTwo);
TestUtils.assertFutureError(unknownErrorResult.memberResult(memberOne), UnknownServerException.class);
TestUtils.assertFutureError(unknownErrorResult.memberResult(memberTwo), UnknownServerException.class);
MemberResponse responseOne = new MemberResponse().setGroupInstanceId(instanceOne).setErrorCode(Errors.UNKNOWN_MEMBER_ID.code());
MemberResponse responseTwo = new MemberResponse().setGroupInstanceId(instanceTwo).setErrorCode(Errors.NONE.code());
// Inject one member level error.
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
env.kafkaClient().prepareResponse(new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code()).setMembers(Arrays.asList(responseOne, responseTwo))));
final RemoveMembersFromConsumerGroupResult memberLevelErrorResult = env.adminClient().removeMembersFromConsumerGroup(GROUP_ID, new RemoveMembersFromConsumerGroupOptions(membersToRemove));
TestUtils.assertFutureError(memberLevelErrorResult.all(), UnknownMemberIdException.class);
TestUtils.assertFutureError(memberLevelErrorResult.memberResult(memberOne), UnknownMemberIdException.class);
assertNull(memberLevelErrorResult.memberResult(memberTwo).get());
// Return with missing member.
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
env.kafkaClient().prepareResponse(new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code()).setMembers(Collections.singletonList(responseTwo))));
final RemoveMembersFromConsumerGroupResult missingMemberResult = env.adminClient().removeMembersFromConsumerGroup(GROUP_ID, new RemoveMembersFromConsumerGroupOptions(membersToRemove));
TestUtils.assertFutureError(missingMemberResult.all(), IllegalArgumentException.class);
// The memberOne was not included in the response.
TestUtils.assertFutureError(missingMemberResult.memberResult(memberOne), IllegalArgumentException.class);
assertNull(missingMemberResult.memberResult(memberTwo).get());
// Return with success.
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
env.kafkaClient().prepareResponse(new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code()).setMembers(Arrays.asList(responseTwo, new MemberResponse().setGroupInstanceId(instanceOne).setErrorCode(Errors.NONE.code())))));
final RemoveMembersFromConsumerGroupResult noErrorResult = env.adminClient().removeMembersFromConsumerGroup(GROUP_ID, new RemoveMembersFromConsumerGroupOptions(membersToRemove));
assertNull(noErrorResult.all().get());
assertNull(noErrorResult.memberResult(memberOne).get());
assertNull(noErrorResult.memberResult(memberTwo).get());
// Test the "removeAll" scenario
final List<TopicPartition> topicPartitions = Arrays.asList(1, 2, 3).stream().map(partition -> new TopicPartition("my_topic", partition)).collect(Collectors.toList());
// construct the DescribeGroupsResponse
DescribeGroupsResponseData data = prepareDescribeGroupsResponseData(GROUP_ID, Arrays.asList(instanceOne, instanceTwo), topicPartitions);
// Return with partial failure for "removeAll" scenario
// 1 prepare response for AdminClient.describeConsumerGroups
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
env.kafkaClient().prepareResponse(new DescribeGroupsResponse(data));
// 2 KafkaAdminClient encounter partial failure when trying to delete all members
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
env.kafkaClient().prepareResponse(new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code()).setMembers(Arrays.asList(responseOne, responseTwo))));
final RemoveMembersFromConsumerGroupResult partialFailureResults = env.adminClient().removeMembersFromConsumerGroup(GROUP_ID, new RemoveMembersFromConsumerGroupOptions());
ExecutionException exception = assertThrows(ExecutionException.class, () -> partialFailureResults.all().get());
assertTrue(exception.getCause() instanceof KafkaException);
assertTrue(exception.getCause().getCause() instanceof UnknownMemberIdException);
// Return with success for "removeAll" scenario
// 1 prepare response for AdminClient.describeConsumerGroups
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
env.kafkaClient().prepareResponse(new DescribeGroupsResponse(data));
// 2. KafkaAdminClient should delete all members correctly
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
env.kafkaClient().prepareResponse(new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code()).setMembers(Arrays.asList(responseTwo, new MemberResponse().setGroupInstanceId(instanceOne).setErrorCode(Errors.NONE.code())))));
final RemoveMembersFromConsumerGroupResult successResult = env.adminClient().removeMembersFromConsumerGroup(GROUP_ID, new RemoveMembersFromConsumerGroupOptions());
assertNull(successResult.all().get());
}
}
use of org.apache.kafka.common.message.LeaveGroupResponseData in project kafka by apache.
the class KafkaAdminClientTest method testRemoveMembersFromGroup.
private void testRemoveMembersFromGroup(String reason, String expectedReason) throws Exception {
final Cluster cluster = mockCluster(3, 0);
final Time time = new MockTime();
try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(time, cluster)) {
env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
env.kafkaClient().prepareResponse(body -> {
if (!(body instanceof LeaveGroupRequest)) {
return false;
}
LeaveGroupRequestData leaveGroupRequest = ((LeaveGroupRequest) body).data();
return leaveGroupRequest.members().stream().allMatch(member -> member.reason().equals(expectedReason));
}, new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code()).setMembers(Arrays.asList(new MemberResponse().setGroupInstanceId("instance-1"), new MemberResponse().setGroupInstanceId("instance-2")))));
Collection<MemberToRemove> membersToRemove = Arrays.asList(new MemberToRemove("instance-1"), new MemberToRemove("instance-2"));
RemoveMembersFromConsumerGroupOptions options = new RemoveMembersFromConsumerGroupOptions(membersToRemove);
options.reason(reason);
final RemoveMembersFromConsumerGroupResult result = env.adminClient().removeMembersFromConsumerGroup(GROUP_ID, options);
assertNull(result.all().get());
}
}
use of org.apache.kafka.common.message.LeaveGroupResponseData in project kafka by apache.
the class KafkaAdminClientTest method testRemoveMembersFromGroupNonRetriableErrors.
@Test
public void testRemoveMembersFromGroupNonRetriableErrors() throws Exception {
// Non-retriable errors throw an exception
final List<Errors> nonRetriableErrors = Arrays.asList(Errors.GROUP_AUTHORIZATION_FAILED, Errors.INVALID_GROUP_ID, Errors.GROUP_ID_NOT_FOUND);
try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(mockCluster(1, 0))) {
env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
for (Errors error : nonRetriableErrors) {
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
env.kafkaClient().prepareResponse(new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(error.code())));
MemberToRemove memberToRemove = new MemberToRemove("instance-1");
Collection<MemberToRemove> membersToRemove = singletonList(memberToRemove);
final RemoveMembersFromConsumerGroupResult result = env.adminClient().removeMembersFromConsumerGroup(GROUP_ID, new RemoveMembersFromConsumerGroupOptions(membersToRemove));
TestUtils.assertFutureError(result.all(), error.exception().getClass());
TestUtils.assertFutureError(result.memberResult(memberToRemove), error.exception().getClass());
}
}
}
use of org.apache.kafka.common.message.LeaveGroupResponseData in project kafka by apache.
the class KafkaAdminClientTest method testRemoveMembersFromGroupNumRetries.
@Test
public void testRemoveMembersFromGroupNumRetries() throws Exception {
final Cluster cluster = mockCluster(3, 0);
final Time time = new MockTime();
try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(time, cluster, AdminClientConfig.RETRIES_CONFIG, "0")) {
env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
env.kafkaClient().prepareResponse(new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NOT_COORDINATOR.code())));
env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
Collection<MemberToRemove> membersToRemove = Arrays.asList(new MemberToRemove("instance-1"), new MemberToRemove("instance-2"));
final RemoveMembersFromConsumerGroupResult result = env.adminClient().removeMembersFromConsumerGroup(GROUP_ID, new RemoveMembersFromConsumerGroupOptions(membersToRemove));
TestUtils.assertFutureError(result.all(), TimeoutException.class);
}
}
Aggregations