Search in sources :

Example 1 with MemberToRemove

use of org.apache.kafka.clients.admin.MemberToRemove 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)1 ExecutionException (java.util.concurrent.ExecutionException)1 MemberToRemove (org.apache.kafka.clients.admin.MemberToRemove)1 RemoveMembersFromConsumerGroupOptions (org.apache.kafka.clients.admin.RemoveMembersFromConsumerGroupOptions)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