use of org.apache.kafka.common.message.LeaveGroupRequestData.MemberIdentity in project kafka by apache.
the class KafkaAdminClient method getMembersFromGroup.
private List<MemberIdentity> getMembersFromGroup(String groupId) {
Collection<MemberDescription> members;
try {
members = describeConsumerGroups(Collections.singleton(groupId)).describedGroups().get(groupId).get().members();
} catch (Exception ex) {
throw new KafkaException("Encounter exception when trying to get members from group: " + groupId, ex);
}
List<MemberIdentity> membersToRemove = new ArrayList<>();
for (final MemberDescription member : members) {
if (member.groupInstanceId().isPresent()) {
membersToRemove.add(new MemberIdentity().setGroupInstanceId(member.groupInstanceId().get()));
} else {
membersToRemove.add(new MemberIdentity().setMemberId(member.consumerId()));
}
}
return membersToRemove;
}
use of org.apache.kafka.common.message.LeaveGroupRequestData.MemberIdentity in project kafka by apache.
the class LeaveGroupRequestTest method testSingleLeaveConstructor.
@Test
public void testSingleLeaveConstructor() {
final LeaveGroupRequestData expectedData = new LeaveGroupRequestData().setGroupId(groupId).setMemberId(memberIdOne);
List<MemberIdentity> singleMember = Collections.singletonList(new MemberIdentity().setMemberId(memberIdOne));
builder = new LeaveGroupRequest.Builder(groupId, singleMember);
for (short version = 0; version <= 2; version++) {
LeaveGroupRequest request = builder.build(version);
assertEquals(expectedData, request.data());
assertEquals(singleMember, request.members());
int expectedThrottleTime = version >= 1 ? throttleTimeMs : AbstractResponse.DEFAULT_THROTTLE_TIME;
LeaveGroupResponse expectedResponse = new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NOT_CONTROLLER.code()).setThrottleTimeMs(expectedThrottleTime));
assertEquals(expectedResponse, request.getErrorResponse(throttleTimeMs, Errors.NOT_CONTROLLER.exception()));
}
}
use of org.apache.kafka.common.message.LeaveGroupRequestData.MemberIdentity in project kafka by apache.
the class KafkaAdminClient method removeMembersFromConsumerGroup.
@Override
public RemoveMembersFromConsumerGroupResult removeMembersFromConsumerGroup(String groupId, RemoveMembersFromConsumerGroupOptions options) {
List<MemberIdentity> members;
if (options.removeAll()) {
members = getMembersFromGroup(groupId);
} else {
members = options.members().stream().map(MemberToRemove::toMemberIdentity).collect(Collectors.toList());
}
String reason = options.reason() == null ? LEAVE_GROUP_REASON : LEAVE_GROUP_REASON + ": " + options.reason();
members.forEach(member -> member.setReason(reason));
SimpleAdminApiFuture<CoordinatorKey, Map<MemberIdentity, Errors>> future = RemoveMembersFromConsumerGroupHandler.newFuture(groupId);
RemoveMembersFromConsumerGroupHandler handler = new RemoveMembersFromConsumerGroupHandler(groupId, members, logContext);
invokeDriver(handler, future, options.timeoutMs);
return new RemoveMembersFromConsumerGroupResult(future.get(CoordinatorKey.byGroupId(groupId)), options.members());
}
use of org.apache.kafka.common.message.LeaveGroupRequestData.MemberIdentity in project kafka by apache.
the class RemoveMembersFromConsumerGroupHandler method handleResponse.
@Override
public ApiResult<CoordinatorKey, Map<MemberIdentity, Errors>> handleResponse(Node coordinator, Set<CoordinatorKey> groupIds, AbstractResponse abstractResponse) {
validateKeys(groupIds);
final LeaveGroupResponse response = (LeaveGroupResponse) abstractResponse;
final Errors error = response.topLevelError();
if (error != Errors.NONE) {
final Map<CoordinatorKey, Throwable> failed = new HashMap<>();
final Set<CoordinatorKey> groupsToUnmap = new HashSet<>();
handleGroupError(groupId, error, failed, groupsToUnmap);
return new ApiResult<>(Collections.emptyMap(), failed, new ArrayList<>(groupsToUnmap));
} else {
final Map<MemberIdentity, Errors> memberErrors = new HashMap<>();
for (MemberResponse memberResponse : response.memberResponses()) {
memberErrors.put(new MemberIdentity().setMemberId(memberResponse.memberId()).setGroupInstanceId(memberResponse.groupInstanceId()), Errors.forCode(memberResponse.errorCode()));
}
return ApiResult.completed(groupId, memberErrors);
}
}
use of org.apache.kafka.common.message.LeaveGroupRequestData.MemberIdentity in project kafka by apache.
the class RemoveMembersFromConsumerGroupResult method all.
/**
* Returns a future which indicates whether the request was 100% success, i.e. no
* either top level or member level error.
* If not, the first member error shall be returned.
*/
public KafkaFuture<Void> all() {
final KafkaFutureImpl<Void> result = new KafkaFutureImpl<>();
this.future.whenComplete((memberErrors, throwable) -> {
if (throwable != null) {
result.completeExceptionally(throwable);
} else {
if (removeAll()) {
for (Map.Entry<MemberIdentity, Errors> entry : memberErrors.entrySet()) {
Exception exception = entry.getValue().exception();
if (exception != null) {
Throwable ex = new KafkaException("Encounter exception when trying to remove: " + entry.getKey(), exception);
result.completeExceptionally(ex);
return;
}
}
} else {
for (MemberToRemove memberToRemove : memberInfos) {
if (maybeCompleteExceptionally(memberErrors, memberToRemove.toMemberIdentity(), result)) {
return;
}
}
}
result.complete(null);
}
});
return result;
}
Aggregations