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

Code before:

    fn on_ready_destroy_peer(
        &mut self,
        merged_by_target: bool,
        clear_stat: PeerClearMetaStat,
    ) -> bool {
        assert_eq!(
            self.fsm.peer.pending_remove,
            Some(PendingRemoveReason::DESTROY)
        );
        // Clean remains if needs and notify all pending requests to exit.
        self.fsm
            .peer
            .ready_to_destroy(merged_by_target, clear_stat, &self.ctx.raft_metrics);

        let region_id = self.region_id();
        let is_peer_initialized = self.fsm.peer.is_initialized();
        let mut meta = self.ctx.store_meta.lock().unwrap();
        // Some places use `force_send().unwrap()` if the StoreMeta lock is held.
        // So in here, it's necessary to held the StoreMeta lock when closing the
        // router.
        self.ctx.router.close(region_id);
        self.fsm.stop();

        if is_peer_initialized
            && !merged_by_target
            && meta
                .region_ranges
                .remove(&enc_end_key(self.fsm.peer.region()))
                .is_none()
        {
            panic!("{} meta corruption detected", self.fsm.peer.tag);
        }

        if meta.regions.remove(&region_id).is_none() && !merged_by_target {
            panic!("{} meta corruption detected", self.fsm.peer.tag)
        }

        // Clear merge related structures.
        if let Some(&need_atomic) = meta.destroyed_region_for_snap.get(&region_id) {
            if need_atomic {
                panic!(
                    "{} should destroy with target region atomically",
                    self.fsm.peer.tag
                );
            } else {
                // Remove itself from atomic_snap_regions as it has cleaned both
                // data and metadata.
                let target_region_id = *meta.targets_map.get(&region_id).unwrap();
                meta.atomic_snap_regions
                    .get_mut(&target_region_id)

Review comment (bug):
```suggestion
        assert_eq!(
            self.fsm.peer.pending_remove,
            Some(PendingRemoveReason::DESTROY),
            "{}", ...
        );
```

Nit, add more debug info in case the assert fails.

Code after:
            merged_by_target,
            self.ctx.store_meta.clone(),
            self.ctx.pending_create_peers.clone(),
        );
        // If clear_stat is valid, it means the destroy task above was executed
        // synchronously. In this case, we should immediately perform the
        // follow-up steps required to complete the peer destruction process.
        if let Some(clear_stat) = clear_stat {
            self.on_ready_destroy_peer(merged_by_target, clear_stat);
        }

        true
    }

    fn on_ready_destroy_peer(
        &mut self,
        merged_by_target: bool,
        clear_stat: PeerClearMetaStat,
    ) -> bool {
        assert_eq!(
            self.fsm.peer.pending_remove,
            Some(PendingRemoveReason::Destroy),
            "terminate the peer under pending_remove state, exepcted {:?} actual {:?} ",
            Some(PendingRemoveReason::Destroy),
            self.fsm.peer.pending_remove,
        );
        // Clean remains if needs and notify all pending requests to exit.
        self.fsm
            .peer
            .ready_to_destroy(merged_by_target, clear_stat, &self.ctx.raft_metrics);

        let region_id = self.region_id();
        let is_peer_initialized = self.fsm.peer.is_initialized();
        let mut meta = self.ctx.store_meta.lock().unwrap();
        // Some places use `force_send().unwrap()` if the StoreMeta lock is held.
        // So in here, it's necessary to held the StoreMeta lock when closing the
        // router.
        self.ctx.router.close(region_id);
        self.fsm.stop();

        if is_peer_initialized
            && !merged_by_target
            && meta
                .region_ranges
                .remove(&enc_end_key(self.fsm.peer.region()))
                .is_none()
        {
            panic!("{} meta corruption detected", self.fsm.peer.tag);
        }

        if meta.regions.remove(&region_id).is_none() && !merged_by_target {