Repository: apache/kafka
File: streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java (Java)

Code before:
            
            if (hasMissingSourceTopics) {
                handleMissingSourceTopicsWithTimeout(missingTopicsDetail);
            } else {
                // Reset timeout tracking when no missing source topics are reported
                firstMissingSourceTopicTime = -1L;
            }

            final Map<StreamsRebalanceData.HostInfo, StreamsRebalanceData.EndpointPartitions> partitionsByEndpoint =
                    streamsRebalanceData.get().partitionsByHost();
            final Map<HostInfo, Set<TopicPartition>> activeHostInfoMap = new HashMap<>();
            final Map<HostInfo, Set<TopicPartition>> standbyHostInfoMap = new HashMap<>();

            partitionsByEndpoint.forEach((hostInfo, endpointPartitions) -> {
                activeHostInfoMap.put(new HostInfo(hostInfo.host(), hostInfo.port()), new HashSet<>(endpointPartitions.activePartitions()));
                standbyHostInfoMap.put(new HostInfo(hostInfo.host(), hostInfo.port()), new HashSet<>(endpointPartitions.standbyPartitions()));
            });
            streamsMetadataState.onChange(
                    activeHostInfoMap,
                    standbyHostInfoMap,
                    getTopicPartitionInfo(activeHostInfoMap)
            );
        }
    }

    private void handleMissingSourceTopicsWithTimeout(final String missingTopicsDetail) {
        final long currentTime = time.milliseconds();
        
        // Start timeout tracking on first encounter with missing topics
        if (firstMissingSourceTopicTime == -1L) {
            firstMissingSourceTopicTime = currentTime;
            log.info("Missing source topics detected: {}. Will wait up to {}ms before failing.", 
                missingTopicsDetail, maxPollTimeMs);
        }
        
        final long elapsedTime = currentTime - firstMissingSourceTopicTime;
        if (elapsedTime >= maxPollTimeMs) {
            final String errorMsg = String.format("Missing source topics: %s. Timeout exceeded after %dms.", 
                missingTopicsDetail, elapsedTime);
            log.error(errorMsg);
            
            // Reset timer for next timeout cycle
            firstMissingSourceTopicTime = currentTime;
            
            throw new MissingSourceTopicException(errorMsg);
        } else {
            log.debug("Missing source topics: {}. Elapsed time: {}ms, timeout in: {}ms", 
                missingTopicsDetail, elapsedTime, maxPollTimeMs - elapsedTime);
        }
    }
    

Review comment (refactor):
Yes, using `org.apache.kafka.common.utils.Timer` should simplify this.

Code after:
            
            if (hasMissingSourceTopics) {
                handleMissingSourceTopicsWithTimeout(missingTopicsDetail);
            } else {
                // Reset timeout tracking when no missing source topics are reported
                topicsReadyTimer = null;
            }

            final Map<StreamsRebalanceData.HostInfo, StreamsRebalanceData.EndpointPartitions> partitionsByEndpoint =
                    streamsRebalanceData.get().partitionsByHost();
            final Map<HostInfo, Set<TopicPartition>> activeHostInfoMap = new HashMap<>();
            final Map<HostInfo, Set<TopicPartition>> standbyHostInfoMap = new HashMap<>();

            partitionsByEndpoint.forEach((hostInfo, endpointPartitions) -> {
                activeHostInfoMap.put(new HostInfo(hostInfo.host(), hostInfo.port()), new HashSet<>(endpointPartitions.activePartitions()));
                standbyHostInfoMap.put(new HostInfo(hostInfo.host(), hostInfo.port()), new HashSet<>(endpointPartitions.standbyPartitions()));
            });
            streamsMetadataState.onChange(
                    activeHostInfoMap,
                    standbyHostInfoMap,
                    getTopicPartitionInfo(activeHostInfoMap)
            );
        }
    }

    private void handleMissingSourceTopicsWithTimeout(final String missingTopicsDetail) {
        // Start timeout tracking on first encounter with missing topics
        if (topicsReadyTimer == null) {
            topicsReadyTimer = time.timer(maxPollTimeMs);
            log.info("Missing source topics detected: {}. Will wait up to {}ms before failing.", 
                missingTopicsDetail, maxPollTimeMs);
        } else {
            topicsReadyTimer.update();
        }
        
        if (topicsReadyTimer.isExpired()) {
            final long elapsedTime = topicsReadyTimer.elapsedMs();
            final String errorMsg = String.format("Missing source topics: %s. Timeout exceeded after %dms.", 
                missingTopicsDetail, elapsedTime);
            log.error(errorMsg);
            
            throw new MissingSourceTopicException(errorMsg);
        } else {
            log.debug("Missing source topics: {}. Elapsed time: {}ms, timeout in: {}ms", 
                missingTopicsDetail, topicsReadyTimer.elapsedMs(), topicsReadyTimer.remainingMs());
        }
    }
    

    static Map<TopicPartition, PartitionInfo> getTopicPartitionInfo(final Map<HostInfo, Set<TopicPartition>> partitionsByHost) {
        final Map<TopicPartition, PartitionInfo> topicToPartitionInfo = new HashMap<>();