Source:
<code><START> private Map<Node, FetchSessionHandler.FetchRequestData> prepareFetchRequests() { <END> Map<Node, FetchSessionHandler.Builder> fetchable = new LinkedHashMap<>(); int newMetadataUpdateVersion = metadata.updateVersion(); if (metadataUpdateVersion.getAndSet(newMetadataUpdateVersion) != newMetadataUpdateVersion) { subscriptions.forEachAssignedPartition( tp -> subscriptions.maybeValidatePositionForCurrentLeader(tp, metadata.leaderAndEpoch(tp))); } long currentTimeMs = time.milliseconds(); for (TopicPartition partition : fetchablePartitions()) { SubscriptionState.FetchPosition position = this.subscriptions.position(partition); Node node = selectReadReplica(partition, position.currentLeader.leader, currentTimeMs); if (node == null || node.isEmpty()) { metadata.requestUpdate(); } else if (client.isUnavailable(node)) { client.maybeThrowAuthFailure(node); log.trace("Skipping fetch for partition because node is awaiting reconnect backoff", partition, node); } else if (this.nodesWithPendingFetchRequests.contains(node.id())) { log.trace("Skipping fetch for partition because previous request to has not been processed", partition, node); } else { FetchSessionHandler.Builder builder = fetchable.get(node); if (builder == null) { int id = node.id(); FetchSessionHandler handler = sessionHandler(id); if (handler == null) { handler = new FetchSessionHandler(logContext, id); sessionHandlers.put(id, handler); } builder = handler.newBuilder(); fetchable.put(node, builder); } builder.add(partition, new FetchRequest.PartitionData(position.offset, FetchRequest.INVALID_LOG_START_OFFSET, this.fetchSize, position.currentLeader.epoch)); log.debug("Added fetch request for partition at position to node {}", isolationLevel, partition, position, node); } } Map<Node, FetchSessionHandler.FetchRequestData> reqs = new LinkedHashMap<>(); for (Map.Entry<Node, FetchSessionHandler.Builder> entry : fetchable.entrySet()) { reqs.put(entry.getKey(), entry.getValue().build()); } return reqs; }</code><technical_language>fetchablePartitions method is another nice opportunity forEachAssignedPartition</technical_language>

Target:
private Map<Node, FetchSessionHandler.FetchRequestData> prepareFetchRequests() { Map<Node, FetchSessionHandler.Builder> fetchable = new LinkedHashMap<>(); maybeValidateAssignments(); long currentTimeMs = time.milliseconds(); for (TopicPartition partition : fetchablePartitions()) { SubscriptionState.FetchPosition position = this.subscriptions.position(partition); Node node = selectReadReplica(partition, position.currentLeader.leader, currentTimeMs); if (node == null || node.isEmpty()) { log.info("Requesting metadata update for partition since the position is missing the current leader node", partition, position); metadata.requestUpdate(); } else if (client.isUnavailable(node)) { client.maybeThrowAuthFailure(node); log.trace("Skipping fetch for partition because node is awaiting reconnect backoff", partition, node); } else if (this.nodesWithPendingFetchRequests.contains(node.id())) { log.trace("Skipping fetch for partition because previous request to has not been processed", partition, node); } else { FetchSessionHandler.Builder builder = fetchable.get(node); if (builder == null) { int id = node.id(); FetchSessionHandler handler = sessionHandler(id); if (handler == null) { handler = new FetchSessionHandler(logContext, id); sessionHandlers.put(id, handler); } builder = handler.newBuilder(); fetchable.put(node, builder); } builder.add(partition, new FetchRequest.PartitionData(position.offset, FetchRequest.INVALID_LOG_START_OFFSET, this.fetchSize, position.currentLeader.epoch)); log.debug("Added fetch request for partition at position to node {}", isolationLevel, partition, position, node); } } Map<Node, FetchSessionHandler.FetchRequestData> reqs = new LinkedHashMap<>(); for (Map.Entry<Node, FetchSessionHandler.Builder> entry : fetchable.entrySet()) { reqs.put(entry.getKey(), entry.getValue().build()); } return reqs; }