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

Code before:
            self.register_raft_gc_log_tick();
            return;
        } else {
            replicated_idx
        };
        // Avoid compacting unpersisted raft logs when persist is far behind apply.
        if compact_idx > self.fsm.peer.raft_group.raft.raft_log.persisted {
            compact_idx = self.fsm.peer.raft_group.raft.raft_log.persisted;
        }
        assert!(compact_idx >= first_idx);
        // Have no idea why subtract 1 here, but original code did this by magic.
        compact_idx -= 1;
        if compact_idx < first_idx {
            // In case compact_idx == first_idx before subtraction.
            self.ctx
                .raft_metrics
                .raft_log_gc_skipped
                .compact_idx_too_small
                .inc();
            return;
        }

        self.fsm
            .peer
            .raft_group
            .mut_store()
            .cancel_generating_snap(Some(compact_idx));

        // Create a compact log request and notify directly.
        let region_id = self.fsm.peer.region().get_id();
        let peer = self.fsm.peer.peer.clone();
        let term = self.fsm.peer.get_index_term(compact_idx);
        let request =
            new_compact_log_request(region_id, peer, compact_idx, term, voter_replicated_idx);
        self.propose_raft_command_internal(
            request,
            Callback::None,
            DiskFullOpt::AllowedOnAlmostFull,
        );

        self.fsm.skip_gc_raft_log_ticks = 0;
        self.register_raft_gc_log_tick();
        PEER_GC_RAFT_LOG_COUNTER.inc_by(compact_idx - first_idx);
    }

    fn register_entry_cache_evict_tick(&mut self) {
        self.schedule_tick(PeerTick::EntryCacheEvict)
    }

    fn on_entry_cache_evict_tick(&mut self) {
        fail_point!("on_entry_cache_evict_tick", |_| {});

Review comment (suggestion):
Please add explanatory comments to clarify the reason behind canceling snapshot generation before the `compact_to` index.

Code after:
            self.register_raft_gc_log_tick();
            return;
        } else {
            replicated_idx
        };
        // Avoid compacting unpersisted raft logs when persist is far behind apply.
        if compact_idx > self.fsm.peer.raft_group.raft.raft_log.persisted {
            compact_idx = self.fsm.peer.raft_group.raft.raft_log.persisted;
        }
        assert!(compact_idx >= first_idx);
        // Have no idea why subtract 1 here, but original code did this by magic.
        compact_idx -= 1;
        if compact_idx < first_idx {
            // In case compact_idx == first_idx before subtraction.
            self.ctx
                .raft_metrics
                .raft_log_gc_skipped
                .compact_idx_too_small
                .inc();
            return;
        }

        // If RaftEngine's compact idx exceeds the snapshot idx,
        // means the snapshot idx has already been compacted.
        // We should cancel the snapshot generating as it is now out of date.
        self.fsm
            .peer
            .raft_group
            .mut_store()
            .cancel_generating_snap(Some(compact_idx));

        // Create a compact log request and notify directly.
        let region_id = self.fsm.peer.region().get_id();
        let peer = self.fsm.peer.peer.clone();
        let term = self.fsm.peer.get_index_term(compact_idx);
        let request =
            new_compact_log_request(region_id, peer, compact_idx, term, voter_replicated_idx);
        self.propose_raft_command_internal(
            request,
            Callback::None,
            DiskFullOpt::AllowedOnAlmostFull,
        );

        self.fsm.skip_gc_raft_log_ticks = 0;
        self.register_raft_gc_log_tick();
        PEER_GC_RAFT_LOG_COUNTER.inc_by(compact_idx - first_idx);
    }

    fn register_entry_cache_evict_tick(&mut self) {
        self.schedule_tick(PeerTick::EntryCacheEvict)
    }