hachikuji commented on code in PR #12508: URL: https://github.com/apache/kafka/pull/12508#discussion_r947216441
########## raft/src/main/java/org/apache/kafka/raft/LeaderState.java: ########## @@ -290,13 +318,30 @@ private ReplicaState getReplicaState(int remoteNodeId) { return state; } - Map<Integer, Long> getVoterEndOffsets() { - return getReplicaEndOffsets(voterStates); + List<DescribeQuorumResponseData.ReplicaState> quorumResponseVoterStates(long currentTimeMs) { + return quorumResponseReplicaStates(voterStates, OptionalInt.of(localId), currentTimeMs); } - Map<Integer, Long> getObserverStates(final long currentTimeMs) { + List<DescribeQuorumResponseData.ReplicaState> quorumResponseObserverStates(long currentTimeMs) { clearInactiveObservers(currentTimeMs); - return getReplicaEndOffsets(observerStates); + return quorumResponseReplicaStates(observerStates, OptionalInt.empty(), currentTimeMs); + } + + private static <R extends ReplicaState> List<DescribeQuorumResponseData.ReplicaState> quorumResponseReplicaStates( + Map<Integer, R> state, + OptionalInt leaderId, + long currentTimeMs) { + Map<Integer, Long> replicaEndOffsets = getReplicaEndOffsets(state); Review Comment: We moved the logic here, but we're still building all these unnecessary maps. It is simpler to build the replica states in a single pass. For example: ```java private static List<DescribeQuorumResponseData.ReplicaState> quorumResponseReplicaStates( Collection<ReplicaState> states, int leaderId, long currentTimeMs ) { return states.stream().map(state -> { final long lastCaughtUpTimestamp; final long lastFetchTimestamp; if (state.nodeId == leaderId) { lastCaughtUpTimestamp = currentTimeMs; lastFetchTimestamp = currentTimeMs; } else { lastCaughtUpTimestamp = state.lastCaughtUpTimestamp.orElse(-1); lastFetchTimestamp = state.lastFetchTimestamp.orElse(-1); } return new DescribeQuorumResponseData.ReplicaState() .setReplicaId(state.nodeId) .setLogEndOffset(state.endOffset.map(md -> md.offset).orElse(-1L)) .setLastCaughtUpTimestamp(lastCaughtUpTimestamp) .setLastFetchTimestamp(lastFetchTimestamp); }).collect(Collectors.toList()); } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org