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

Code before:
                warn!(
                    "warm up the entry cache failed";
                    "region_id" => self.region_id,
                    "peer_id" => self.peer_id,
                    "last_entry_index" => last_entry_index.unwrap_or(0),
                    "expected_high" => range.1,
                );
            }
            Err(e) => {
                warn!(
                    "warm up the entry cache failed";
                    "region_id" => self.region_id,
                    "peer_id" => self.peer_id,
                    "err" => ?e,
                );
            }
        }
    }

    pub fn compact_entry_cache(&mut self, idx: u64, state: Option<&mut CacheWarmupState>) {
        if state.is_some_and(|s| !s.check_stale()) {
            // Since this peer may be warming up the entry cache, log compaction
            // should be temporarily skipped. Otherwise, the warmup task may fail.
            return;
        }
        self.term_cache.compact_to(idx);
        self.cache.compact_to(idx);
    }

    #[inline]
    pub fn is_entry_cache_empty(&self) -> bool {
        self.cache.is_empty()
    }

    #[inline]
    pub fn entry_cache_first_index(&self) -> Option<u64> {
        self.cache.first_index()
    }

    /// Evict entries from the cache.
    pub fn evict_entry_cache(&mut self, half: bool) {
        fail_point!("mock_evict_entry_cache", |_| {});

        if !self.is_entry_cache_empty() {
            let cache = &mut self.cache;
            let cache_len = cache.cache.len();
            let drain_to = if half { cache_len / 2 } else { cache_len - 1 };
            let idx = cache.cache[drain_to].index;
            let mem_size_change = cache.compact_to(idx + 1);
            RAFT_ENTRIES_EVICT_BYTES.inc_by(mem_size_change);
        } else if !half {

Review comment (performance):
Should we reset `term_cache` during applying snapshot?

Code after:
                        if !is_valid {
                            error!(
                                "unexpected warmup state";
                                "region_id" => self.region_id,
                                "peer_id" => self.peer_id,
                                "cache_first" => ?self.entry_cache_first_index(),
                                "last_index" => self.last_index(),
                                "warmup_state_high" => range.1,
                                "last_entry_index" => index,
                            );
                            return;
                        }
                        entries.truncate((range.1 - range.0) as usize);
                        self.cache.prepend(entries);
                        WARM_UP_ENTRY_CACHE_COUNTER.finished.inc();
                        fail_point!("on_entry_cache_warmed_up");
                        return;
                    }
                }
                warn!(
                    "warm up the entry cache failed";
                    "region_id" => self.region_id,
                    "peer_id" => self.peer_id,
                    "last_entry_index" => last_entry_index.unwrap_or(0),
                    "expected_high" => range.1,
                );
            }
            Err(e) => {
                warn!(
                    "warm up the entry cache failed";
                    "region_id" => self.region_id,
                    "peer_id" => self.peer_id,
                    "err" => ?e,
                );
            }
        }
    }

    pub fn compact_entry_cache(&mut self, idx: u64, state: Option<&mut CacheWarmupState>) {
        if state.is_some_and(|s| !s.check_stale()) {
            // Since this peer may be warming up the entry cache, log compaction
            // should be temporarily skipped. Otherwise, the warmup task may fail.
            return;
        }
        self.cache.compact_to(idx);
    }

    #[inline]
    pub fn is_entry_cache_empty(&self) -> bool {
        self.cache.is_empty()
    }