Repository: tikv/tikv
File: components/raftstore/src/store/peer.rs (Rust)

Code before:
                    down_peers.push(stats);
                    down_peer_ids.push(p.get_id());
                }
            }
        }
        self.down_peer_ids = down_peer_ids;
        if !self.down_peer_ids.is_empty() {
            self.refill_disk_full_peers(ctx);
        }
        down_peers
    }

    /// Collects all pending peers and update `peers_start_pending_time`.
    pub fn collect_pending_peers<T>(&mut self, ctx: &PollContext<EK, ER, T>) -> Vec<metapb::Peer> {
        let mut pending_peers = Vec::with_capacity(self.region().get_peers().len());
        let status = self.raft_group.status();
        let truncated_idx = self.get_store().truncated_index();

        for peer_id in &self.wait_data_peers {
            if let Some(p) = self.get_peer_from_cache(*peer_id) {
                pending_peers.push(p);
            }
        }

        if status.progress.is_none() {
            // If `progress` is None, the current node is not the leader. Since
            // we can't determine the progress of followers, we conservatively
            // mark all other peers as pending.
            for peer in self.region().get_peers() {
                if peer.get_id() == self.peer.get_id() {
                    continue;
                }
                if let Some(p) = self.get_peer_from_cache(peer.get_id()) {
                    pending_peers.push(p);
                }
            }
            return pending_peers;
        }

        for i in 0..self.peers_start_pending_time.len() {
            let (_, pending_after) = self.peers_start_pending_time[i];
            let elapsed = duration_to_sec(pending_after.saturating_elapsed());
            RAFT_PEER_PENDING_DURATION.observe(elapsed);
        }

        let progresses = status.progress.unwrap().iter();
        for (&id, progress) in progresses {
            if id == self.peer.get_id() {
                continue;
            }
            // The `matched` is 0 only in these two cases:

Review comment (suggestion):
NIT: it's better to add extra annotations here to specify that the calling of this part will only be executed by a new split peer with already `campaigned`, who will most likely be elected as the new leader of the region.


Code after:
                    down_peers.push(stats);
                    down_peer_ids.push(p.get_id());
                }
            }
        }
        self.down_peer_ids = down_peer_ids;
        if !self.down_peer_ids.is_empty() {
            self.refill_disk_full_peers(ctx);
        }
        down_peers
    }

    /// Collects all pending peers and update `peers_start_pending_time`.
    pub fn collect_pending_peers<T>(&mut self, ctx: &PollContext<EK, ER, T>) -> Vec<metapb::Peer> {
        let mut pending_peers = Vec::with_capacity(self.region().get_peers().len());
        let status = self.raft_group.status();
        let truncated_idx = self.get_store().truncated_index();

        for peer_id in &self.wait_data_peers {
            if let Some(p) = self.get_peer_from_cache(*peer_id) {
                pending_peers.push(p);
            }
        }

        if status.progress.is_none() {
            // If `progress` is None, the current node is not the leader.
            // Typically, `collect_pending_peers` is called only on the leader
            // as part of the heartbeat process. However, after a split, the new
            // peer that starts the first campaign is asked to send a heartbeat
            // immediately, even though it may not have been elected yet. Since
            // we can't determine the progress of followers at this point, we
            // conservatively mark all other peers as pending.
            assert!(!self.is_leader());
            for peer in self.region().get_peers() {
                if peer.get_id() == self.peer.get_id() {
                    continue;
                }
                if let Some(p) = self.get_peer_from_cache(peer.get_id()) {
                    pending_peers.push(p);
                }
            }
            return pending_peers;
        }

        for i in 0..self.peers_start_pending_time.len() {
            let (_, pending_after) = self.peers_start_pending_time[i];
            let elapsed = duration_to_sec(pending_after.saturating_elapsed());
            RAFT_PEER_PENDING_DURATION.observe(elapsed);
        }

        let progresses = status.progress.unwrap().iter();