Skip to content

Commit

Permalink
backup: change file name with local timestamp (tikv#8707)
Browse files Browse the repository at this point in the history
Signed-off-by: luancheng <luancheng@pingcap.com>
  • Loading branch information
3pointer committed Sep 21, 2020
1 parent d4880b2 commit b43070a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
16 changes: 12 additions & 4 deletions components/backup/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::f64::INFINITY;
use std::fmt;
use std::sync::atomic::*;
use std::sync::*;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{borrow::Cow, time::*};

use concurrency_manager::ConcurrencyManager;
Expand Down Expand Up @@ -871,16 +872,23 @@ fn get_max_start_key(start_key: Option<&Key>, region: &Region) -> Option<Key> {
}
}

/// Construct an backup file name based on the given store id and region.
/// A name consists with three parts: store id, region_id and a epoch version.
/// Construct an backup file name based on the given store id, region, range start key and local unix timestamp.
/// A name consists with five parts: store id, region_id, a epoch version, the hash of range start key and timestamp.
/// range start key is used to keep the unique file name for file, to handle different tables exists on the same region.
/// local unix timestamp is used to keep the unique file name for file, to handle receive the same request after connection reset.
fn backup_file_name(store_id: u64, region: &Region, key: Option<String>) -> String {
let start = SystemTime::now();
let since_the_epoch = start
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
match key {
Some(k) => format!(
"{}_{}_{}_{}",
"{}_{}_{}_{}_{}",
store_id,
region.get_id(),
region.get_region_epoch().get_version(),
k
k,
since_the_epoch.as_millis()
),
None => format!(
"{}_{}_{}",
Expand Down
13 changes: 11 additions & 2 deletions components/backup/tests/integrations/test_backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,17 @@ fn test_backup_rawkv() {
&tmp.path().join(format!("{}", backup_ts.next().next())),
);
let resps3 = block_on(rx.collect::<Vec<_>>());
assert_eq!(files1, resps3[0].files);

let files3 = resps3[0].files.clone();

// After https://github.com/tikv/tikv/pull/8707 merged.
// the backup file name will based on local timestamp.
// so the two backup's file name may not be same, we should skip this check.
assert_eq!(files1.len(), 1);
assert_eq!(files3.len(), 1);
assert_eq!(files1[0].sha256, files3[0].sha256);
assert_eq!(files1[0].total_bytes, files3[0].total_bytes);
assert_eq!(files1[0].total_kvs, files3[0].total_kvs);
assert_eq!(files1[0].size, files3[0].size);
suite.stop();
}

Expand Down

0 comments on commit b43070a

Please sign in to comment.