Details
Description
The KRaftMetadataCache.getPartitionReplicaEndpoints method, called when a replica selector is being used (e.g. for Fetch From Follower), currently takes a single TopicPartition for which to calculate node endpoints per replica ID, but then goes on to process all the partitions of the topic. As a result it seems to both calculate endpoints which are not relevant for the given partition and to recalculate the relevant endpoints multiple times as replicas of many different partitions.
This could be very detrimental to performance if it touches topics with larger number of partitions.
Unlike its KRaft counterpart, ZkMetadataCache.getPartitionReplicaEndpoints seems to do the right thing, processing only the partition info of the given partition. Also the problem doesn't seem to be a recent regression - it looks like it was present in the original form of KRaftMetadataCache introduced more than 3 years ago.