Search in sources :

Example 1 with MemberIdentity

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;
}
Also used : MemberIdentity(org.apache.kafka.common.message.LeaveGroupRequestData.MemberIdentity) ArrayList(java.util.ArrayList) KafkaException(org.apache.kafka.common.KafkaException) ThrottlingQuotaExceededException(org.apache.kafka.common.errors.ThrottlingQuotaExceededException) KafkaException(org.apache.kafka.common.KafkaException) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) UnacceptableCredentialException(org.apache.kafka.common.errors.UnacceptableCredentialException) AuthenticationException(org.apache.kafka.common.errors.AuthenticationException) KafkaStorageException(org.apache.kafka.common.errors.KafkaStorageException) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) ConfigException(org.apache.kafka.common.config.ConfigException) DisconnectException(org.apache.kafka.common.errors.DisconnectException) InvalidRequestException(org.apache.kafka.common.errors.InvalidRequestException) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) RetriableException(org.apache.kafka.common.errors.RetriableException) UnsupportedSaslMechanismException(org.apache.kafka.common.errors.UnsupportedSaslMechanismException) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) StaleMetadataException(org.apache.kafka.clients.StaleMetadataException) ApiException(org.apache.kafka.common.errors.ApiException)

Example 2 with MemberIdentity

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()));
    }
}
Also used : LeaveGroupRequestData(org.apache.kafka.common.message.LeaveGroupRequestData) MemberIdentity(org.apache.kafka.common.message.LeaveGroupRequestData.MemberIdentity) LeaveGroupResponseData(org.apache.kafka.common.message.LeaveGroupResponseData) Test(org.junit.jupiter.api.Test)

Example 3 with MemberIdentity

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());
}
Also used : CoordinatorKey(org.apache.kafka.clients.admin.internals.CoordinatorKey) MemberIdentity(org.apache.kafka.common.message.LeaveGroupRequestData.MemberIdentity) RemoveMembersFromConsumerGroupHandler(org.apache.kafka.clients.admin.internals.RemoveMembersFromConsumerGroupHandler) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 4 with MemberIdentity

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);
    }
}
Also used : MemberResponse(org.apache.kafka.common.message.LeaveGroupResponseData.MemberResponse) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) HashMap(java.util.HashMap) Errors(org.apache.kafka.common.protocol.Errors) MemberIdentity(org.apache.kafka.common.message.LeaveGroupRequestData.MemberIdentity) HashSet(java.util.HashSet)

Example 5 with MemberIdentity

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;
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) MemberIdentity(org.apache.kafka.common.message.LeaveGroupRequestData.MemberIdentity) KafkaException(org.apache.kafka.common.KafkaException) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) Map(java.util.Map) KafkaException(org.apache.kafka.common.KafkaException)

Aggregations

MemberIdentity (org.apache.kafka.common.message.LeaveGroupRequestData.MemberIdentity)5 HashMap (java.util.HashMap)2 Map (java.util.Map)2 KafkaException (org.apache.kafka.common.KafkaException)2 Errors (org.apache.kafka.common.protocol.Errors)2 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 TreeMap (java.util.TreeMap)1 StaleMetadataException (org.apache.kafka.clients.StaleMetadataException)1 CoordinatorKey (org.apache.kafka.clients.admin.internals.CoordinatorKey)1 RemoveMembersFromConsumerGroupHandler (org.apache.kafka.clients.admin.internals.RemoveMembersFromConsumerGroupHandler)1 ConfigException (org.apache.kafka.common.config.ConfigException)1 ApiException (org.apache.kafka.common.errors.ApiException)1 AuthenticationException (org.apache.kafka.common.errors.AuthenticationException)1 DisconnectException (org.apache.kafka.common.errors.DisconnectException)1 InvalidRequestException (org.apache.kafka.common.errors.InvalidRequestException)1 InvalidTopicException (org.apache.kafka.common.errors.InvalidTopicException)1 KafkaStorageException (org.apache.kafka.common.errors.KafkaStorageException)1