Repository: tikv/tikv (language: rust)

Issue:
compact_log_backup: `input_max_ts` and `input_min_ts` should be included into `Compaction`
## Development Task
As title, for filtering out unnecessary compactions.


Discussion / hints:

Fix patch:
diff --git a/Cargo.lock b/Cargo.lock
index d025033adc0..d8dec4f7ba3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3724,7 +3724,7 @@ dependencies = [
 [[package]]
 name = "kvproto"
 version = "0.0.2"
-source = "git+https://github.com/pingcap/kvproto.git#17bdaaa37b9fbc3231cf90a3dec9ecbfa3f27e4e"
+source = "git+https://github.com/pingcap/kvproto.git#c35d2b41011503a386db9ae2b7f4bcec653dab61"
 dependencies = [
  "futures 0.3.15",
  "grpcio",
diff --git a/cmd/tikv-ctl/src/cmd.rs b/cmd/tikv-ctl/src/cmd.rs
index d11c76c4090..ae562292b2f 100644
--- a/cmd/tikv-ctl/src/cmd.rs
+++ b/cmd/tikv-ctl/src/cmd.rs
@@ -640,14 +640,19 @@ pub enum Cmd {
             )
         )]
         name: String,
-        #[structopt(long = "from", help("from when we need to compact."))]
+        #[structopt(
+            long = "from",
+            help(
+                "from when we need to include files into the compaction.\
+                files contains any record within the [--from, --until) will be selected."
+            )
+        )]
         from_ts: u64,
         #[structopt(
             long = "until",
             help(
-                "until when we need to compact. \
-                Also note that records out of the [--from, --until) range may also be compacted \
-                if their neighbour in the same file needs to be compacted."
+                "until when we need to include files into the compaction.\
+                files contains any record within the [--from, --until) will be selected."
             )
         )]
         until_ts: u64,
diff --git a/cmd/tikv-ctl/src/main.rs b/cmd/tikv-ctl/src/main.rs
index eeb8bb63f0a..9b8852c62b1 100644
--- a/cmd/tikv-ctl/src/main.rs
+++ b/cmd/tikv-ctl/src/main.rs
@@ -471,8 +471,13 @@ fn main() {
                 ((log_to_term, checkpoint), with_status_server),
                 (save_meta, with_lock),
             );
-            exec.run(hooks)
-                .expect("failed to execute compact-log-backup")
+            match exec.run(hooks) {
+                Ok(()) => tikv_util::info!("Compact log backup successfully."),
+                Err(err) => {
+                    tikv_util::error!("Failed to compact log backup."; "err" => %err, "err_verbose" => ?err);
+                    std::process::exit(1);
+                }
+            }
         }
         // Commands below requires either the data dir or the host.
         cmd => {
diff --git a/components/compact-log-backup/src/compaction/meta.rs b/components/compact-log-backup/src/compaction/meta.rs
index 62d500528a7..30491b1095a 100644
--- a/components/compact-log-backup/src/compaction/meta.rs
+++ b/components/compact-log-backup/src/compaction/meta.rs
@@ -162,12 +162,23 @@ impl Ord for SortByOffset {
 /// Collecting metadata of subcomapctions.
 ///
 /// Finally, it calculates which files can be deleted.
-#[derive(Default, Debug)]
+#[derive(Debug)]
 pub struct CompactionRunInfoBuilder {
     files: HashMap<Arc<str>, BTreeSet<SortByOffset>>,
     compaction: brpb::LogFileCompaction,
 }
 
+impl Default for CompactionRunInfoBuilder {
+    fn default() -> Self {
+        let mut this = Self {
+            files: Default::default(),
+            compaction: Default::default(),
+        };
+        this.compaction.input_min_ts = u64::MAX;
+        this
+    }
+}
+
 /// A set of deletable log files from the same metadata.
 pub struct ExpiringFilesOfMeta {
     meta_path: Arc<str>,
@@ -230,6 +241,8 @@ impl CompactionRunInfoBuilder {
                 .insert(SortByOffset(file.id.clone()));
         }
         self.compaction.artifacts_hash ^= c.origin.crc64();
+        self.compaction.input_min_ts = self.compaction.input_min_ts.min(c.origin.input_min_ts);
+        self.compaction.input_max_ts = self.compaction.input_max_ts.max(c.origin.input_max_ts);
     }
 
     pub fn mut_meta(&mut self) -> &mut brpb::LogFileCompaction {
@@ -382,6 +395,11 @@ mod test {
         let mig = coll.mig(st.storage().as_ref()).await.unwrap();
         assert_eq!(mig.edit_meta.len(), 1);
         assert!(mig.edit_meta[0].destruct_self);
+
+        assert_eq!(mig.compactions.len(), 1);
+        let c = &mig.compactions[0];
+        assert_eq!(c.input_min_ts, 10);
+        assert_eq!(c.input_max_ts, 25);
     }
 
     #[tokio::test]
@@ -439,5 +457,10 @@ mod test {
             _ => unreachable!(),
         };
         mig.edit_meta.iter().for_each(check);
+
+        assert_eq!(mig.compactions.len(), 1);
+        let c = &mig.compactions[0];
+        assert_eq!(c.input_min_ts, 10);
+        assert_eq!(c.input_max_ts, 20);
     }
 }


Test patch:
diff --git a/components/compact-log-backup/src/execute/test.rs b/components/compact-log-backup/src/execute/test.rs
index 78ab92ec0b6..68e21bbd477 100644
--- a/components/compact-log-backup/src/execute/test.rs
+++ b/components/compact-log-backup/src/execute/test.rs
@@ -209,7 +209,7 @@ async fn load_locks(storage: &dyn ExternalStorage) -> Vec<String> {
 }
 
 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
-async fn test_consistency_guard_() {
+async fn test_consistency_guard() {
     let st = TmpStorage::create();
     let strg = st.storage().as_ref();
     put_checkpoint(strg, 1, 42).await;
diff --git a/components/compact-log-backup/src/test_util.rs b/components/compact-log-backup/src/test_util.rs
index 7f3763ceed8..08d32a61bbb 100644
--- a/components/compact-log-backup/src/test_util.rs
+++ b/components/compact-log-backup/src/test_util.rs
@@ -6,6 +6,7 @@ use std::{
     ops::Not,
     path::{Path, PathBuf},
     sync::{Arc, Mutex},
+    u64,
 };
 
 use engine_rocks::RocksEngine;
@@ -253,7 +254,7 @@ impl LogFileBuilder {
             is_meta: false,
 
             content: zstd::Encoder::new(Cursor::new(vec![]), 3).unwrap(),
-            min_ts: 0,
+            min_ts: u64::MAX,
             max_ts: 0,
             min_key: vec![],
             max_key: vec![],