Repository: tikv/tikv
File: components/resolved_ts/src/endpoint.rs (Rust)

Code before:
    resolver_status: ResolverStatus,
}

impl ObserveRegion {
    fn new(meta: Region, rrp: Arc<RegionReadProgress>, memory_quota: Arc<MemoryQuota>) -> Self {
        ObserveRegion {
            resolver: Resolver::with_read_progress(meta.id, Some(rrp), memory_quota.clone()),
            meta,
            handle: ObserveHandle::new(),
            resolver_status: ResolverStatus::Pending {
                tracked_index: 0,
                locks: vec![],
                cancelled: Arc::new(AtomicBool::new(false)),
                memory_quota,
            },
        }
    }

    fn read_progress(&self) -> &Arc<RegionReadProgress> {
        self.resolver.read_progress().unwrap()
    }

    fn track_change_log(&mut self, change_logs: &[ChangeLog]) -> Result<()> {
        if matches!(self.resolver_status, ResolverStatus::Pending { .. }) {
            self.resolver_status
                .track_change_log(self.meta.id, change_logs)
        } else {
            for log in change_logs {
                match log {
                    ChangeLog::Error(e) => {
                        debug!(
                            "skip change log error";
                            "region" => self.meta.id,
                            "error" => ?e,
                        );
                        continue;
                    }
                    ChangeLog::Admin(req_type) => match req_type {
                        AdminCmdType::Split
                        | AdminCmdType::BatchSplit
                        | AdminCmdType::PrepareMerge
                        | AdminCmdType::RollbackMerge
                        | AdminCmdType::CommitMerge => {
                            info!(
                                "region met split/merge command, stop tracking since key range changed, wait for re-register";
                                "req_type" => ?req_type,
                            );
                            // Stop tracking so that `tracked_index` larger than the split/merge
                            // command index won't be published until `RegionUpdate` event
                            // trigger the region re-register and re-scan the new key range
                            self.resolver.stop_tracking();

Review comment (suggestion):
Question: Why is this change needed?
It separates the implementation of ResolverStatus::Pending from other state, which may make the readability worse.

Code after:
                        // and key range (i.e. `CompactLog`, `ComputeHash`) we may not need to
                        // return error
                        return Err(box_err!(
                            "region met admin command {:?} while initializing resolver",
                            req_type
                        ));
                    }
                    ChangeLog::Rows { rows, index } => {
                        for row in rows {
                            let lock = match row {
                                ChangeRow::Prewrite { key, start_ts, .. } => PendingLock::Track {
                                    key: key.clone(),
                                    start_ts: *start_ts,
                                },
                                ChangeRow::Commit {
                                    key,
                                    start_ts,
                                    commit_ts,
                                    ..
                                } => PendingLock::Untrack {
                                    key: key.clone(),
                                    start_ts: *start_ts,
                                    commit_ts: *commit_ts,
                                },
                                // One pc command do not contains any lock, so just skip it
                                ChangeRow::OnePc { .. } | ChangeRow::IngestSsT => continue,
                            };
                            self.resolver_status.push_pending_lock(lock, self.meta.id)?;
                        }
                        self.resolver_status
                            .update_tracked_index(*index, self.meta.id);
                    }
                }
            }
        } else {
            for log in change_logs {
                match log {
                    ChangeLog::Error(e) => {
                        debug!(
                            "skip change log error";
                            "region" => self.meta.id,
                            "error" => ?e,
                        );
                        continue;
                    }
                    ChangeLog::Admin(req_type) => match req_type {
                        AdminCmdType::Split
                        | AdminCmdType::BatchSplit
                        | AdminCmdType::PrepareMerge
                        | AdminCmdType::RollbackMerge
                        | AdminCmdType::CommitMerge => {