Search in sources :

Example 1 with RemoveMembersFromConsumerGroupOptions

use of org.apache.kafka.clients.admin.RemoveMembersFromConsumerGroupOptions in project kafka by apache.

the class StreamsResetter method maybeDeleteActiveConsumers.

private void maybeDeleteActiveConsumers(final String groupId, final Admin adminClient) throws ExecutionException, InterruptedException {
    final DescribeConsumerGroupsResult describeResult = adminClient.describeConsumerGroups(Collections.singleton(groupId), new DescribeConsumerGroupsOptions().timeoutMs(10 * 1000));
    final List<MemberDescription> members = new ArrayList<>(describeResult.describedGroups().get(groupId).get().members());
    if (!members.isEmpty()) {
        if (options.has(forceOption)) {
            System.out.println("Force deleting all active members in the group: " + groupId);
            adminClient.removeMembersFromConsumerGroup(groupId, new RemoveMembersFromConsumerGroupOptions()).all().get();
        } else {
            throw new IllegalStateException("Consumer group '" + groupId + "' is still active " + "and has following members: " + members + ". " + "Make sure to stop all running application instances before running the reset tool." + " You can use option '--force' to remove active members from the group.");
        }
    }
}
Also used : RemoveMembersFromConsumerGroupOptions(org.apache.kafka.clients.admin.RemoveMembersFromConsumerGroupOptions) MemberDescription(org.apache.kafka.clients.admin.MemberDescription) ArrayList(java.util.ArrayList) DescribeConsumerGroupsResult(org.apache.kafka.clients.admin.DescribeConsumerGroupsResult) DescribeConsumerGroupsOptions(org.apache.kafka.clients.admin.DescribeConsumerGroupsOptions)

Example 2 with RemoveMembersFromConsumerGroupOptions

use of org.apache.kafka.clients.admin.RemoveMembersFromConsumerGroupOptions in project kafka by apache.

the class KafkaStreams method removeStreamThread.

private Optional<String> removeStreamThread(final long timeoutMs) throws TimeoutException {
    final long startMs = time.milliseconds();
    if (isRunningOrRebalancing()) {
        synchronized (changeThreadCount) {
            // make a copy of threads to avoid holding lock
            for (final StreamThread streamThread : new ArrayList<>(threads)) {
                final boolean callingThreadIsNotCurrentStreamThread = !streamThread.getName().equals(Thread.currentThread().getName());
                if (streamThread.isAlive() && (callingThreadIsNotCurrentStreamThread || getNumLiveStreamThreads() == 1)) {
                    log.info("Removing StreamThread " + streamThread.getName());
                    final Optional<String> groupInstanceID = streamThread.getGroupInstanceID();
                    streamThread.requestLeaveGroupDuringShutdown();
                    streamThread.shutdown();
                    if (!streamThread.getName().equals(Thread.currentThread().getName())) {
                        final long remainingTimeMs = timeoutMs - (time.milliseconds() - startMs);
                        if (remainingTimeMs <= 0 || !streamThread.waitOnThreadState(StreamThread.State.DEAD, remainingTimeMs)) {
                            log.warn("{} did not shutdown in the allotted time.", streamThread.getName());
                        // Don't remove from threads until shutdown is complete. We will trim it from the
                        // list once it reaches DEAD, and if for some reason it's hanging indefinitely in the
                        // shutdown then we should just consider this thread.id to be burned
                        } else {
                            log.info("Successfully removed {} in {}ms", streamThread.getName(), time.milliseconds() - startMs);
                            threads.remove(streamThread);
                            queryableStoreProvider.removeStoreProviderForThread(streamThread.getName());
                        }
                    } else {
                        log.info("{} is the last remaining thread and must remove itself, therefore we cannot wait " + "for it to complete shutdown as this will result in deadlock.", streamThread.getName());
                    }
                    final long cacheSizePerThread = getCacheSizePerThread(getNumLiveStreamThreads());
                    log.info("Resizing thread cache due to thread removal, new cache size per thread is {}", cacheSizePerThread);
                    resizeThreadCache(cacheSizePerThread);
                    if (groupInstanceID.isPresent() && callingThreadIsNotCurrentStreamThread) {
                        final MemberToRemove memberToRemove = new MemberToRemove(groupInstanceID.get());
                        final Collection<MemberToRemove> membersToRemove = Collections.singletonList(memberToRemove);
                        final RemoveMembersFromConsumerGroupResult removeMembersFromConsumerGroupResult = adminClient.removeMembersFromConsumerGroup(applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG), new RemoveMembersFromConsumerGroupOptions(membersToRemove));
                        try {
                            final long remainingTimeMs = timeoutMs - (time.milliseconds() - startMs);
                            removeMembersFromConsumerGroupResult.memberResult(memberToRemove).get(remainingTimeMs, TimeUnit.MILLISECONDS);
                        } catch (final java.util.concurrent.TimeoutException e) {
                            log.error("Could not remove static member {} from consumer group {} due to a timeout: {}", groupInstanceID.get(), applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG), e);
                            throw new TimeoutException(e.getMessage(), e);
                        } catch (final InterruptedException e) {
                            Thread.currentThread().interrupt();
                        } catch (final ExecutionException e) {
                            log.error("Could not remove static member {} from consumer group {} due to: {}", groupInstanceID.get(), applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG), e);
                            throw new StreamsException("Could not remove static member " + groupInstanceID.get() + " from consumer group " + applicationConfigs.getString(StreamsConfig.APPLICATION_ID_CONFIG) + " for the following reason: ", e.getCause());
                        }
                    }
                    final long remainingTimeMs = timeoutMs - (time.milliseconds() - startMs);
                    if (remainingTimeMs <= 0) {
                        throw new TimeoutException("Thread " + streamThread.getName() + " did not stop in the allotted time");
                    }
                    return Optional.of(streamThread.getName());
                }
            }
        }
        log.warn("There are no threads eligible for removal");
    } else {
        log.warn("Cannot remove a stream thread when Kafka Streams client is in state  " + state());
    }
    return Optional.empty();
}
Also used : GlobalStreamThread(org.apache.kafka.streams.processor.internals.GlobalStreamThread) StreamThread(org.apache.kafka.streams.processor.internals.StreamThread) StreamsException(org.apache.kafka.streams.errors.StreamsException) ArrayList(java.util.ArrayList) MemberToRemove(org.apache.kafka.clients.admin.MemberToRemove) RemoveMembersFromConsumerGroupResult(org.apache.kafka.clients.admin.RemoveMembersFromConsumerGroupResult) RemoveMembersFromConsumerGroupOptions(org.apache.kafka.clients.admin.RemoveMembersFromConsumerGroupOptions) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Aggregations

ArrayList (java.util.ArrayList)2 RemoveMembersFromConsumerGroupOptions (org.apache.kafka.clients.admin.RemoveMembersFromConsumerGroupOptions)2 ExecutionException (java.util.concurrent.ExecutionException)1 DescribeConsumerGroupsOptions (org.apache.kafka.clients.admin.DescribeConsumerGroupsOptions)1 DescribeConsumerGroupsResult (org.apache.kafka.clients.admin.DescribeConsumerGroupsResult)1 MemberDescription (org.apache.kafka.clients.admin.MemberDescription)1 MemberToRemove (org.apache.kafka.clients.admin.MemberToRemove)1 RemoveMembersFromConsumerGroupResult (org.apache.kafka.clients.admin.RemoveMembersFromConsumerGroupResult)1 TimeoutException (org.apache.kafka.common.errors.TimeoutException)1 StreamsException (org.apache.kafka.streams.errors.StreamsException)1 GlobalStreamThread (org.apache.kafka.streams.processor.internals.GlobalStreamThread)1 StreamThread (org.apache.kafka.streams.processor.internals.StreamThread)1