Unverified Commit ae1e53a1 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Retain fewer stale cache versions and use a TTL (#96941)

In CI we do not retain old versions of the db at all, but in dev or
non-CI settings we would retain up to 2 indefinitely.

Given how large caches can grow, this PR reduces it to no more than 1
old version, and then only for a certain amount of time, currently 3
days. The idea being to support switching back and forth between feature
branches and a next.js upgrade branch, 3 days allows this worflow to
survive a weekend.

To support this the `CURRENT` file format is modified. Instead of just
storing a sequence number it now stores a small json object containing
the `max_sequence_number` and the mtime. We store the last_write_time in
the file to support the fact that we anticipate `cache` directories to
be copied around and the native mtime would record the copy time instead
of the commit time.

This should reduce dev time disk usage in the common case.
parent 4991d120
......@@ -10026,6 +10026,8 @@ dependencies = [
"rand 0.10.1",
"rayon",
"rustc-hash 2.1.1",
"serde",
"serde_json",
"smallvec",
"tempfile",
"thread_local",
......@@ -10130,6 +10132,7 @@ dependencies = [
"hashbrown 0.14.5",
"indexmap 2.13.0",
"indoc",
"jiff",
"lzzzz",
"parking_lot",
"rand 0.10.1",
......
......@@ -76,10 +76,10 @@ function findSeedSource(
const currentWorktree = path.resolve(worktreeInfo.worktreeRoot)
// We are going to find the best candidate worktree
// based on the newest mtime of the CURRENT file in the cache directory.
// based on the most recently written cache directory.
// We only look at our version
let best: { versionDir: string; mtimeMs: number } | undefined
let best: { versionDir: string; commitTimeMs: number } | undefined
for (const root of [
worktreeInfo.mainRepoRoot,
...listLinkedWorktreeRoots(worktreeInfo.mainRepoRoot),
......@@ -93,21 +93,31 @@ function findSeedSource(
'turbopack',
version
)
const mtimeMs = currentMtimeMs(versionDir)
if (mtimeMs === undefined) continue
if (!best || mtimeMs > best.mtimeMs) {
best = { versionDir, mtimeMs }
const commitTimeMs = currentCommitTimeMs(versionDir)
if (commitTimeMs === undefined) continue
if (!best || commitTimeMs > best.commitTimeMs) {
best = { versionDir, commitTimeMs }
}
}
return best?.versionDir
}
function currentMtimeMs(versionDir: string): number | undefined {
// When the cache in `versionDir` was last committed to, in epoch milliseconds, or undefined if
// there is no usable cache there. Read from the `commit_time` the persistence layer records in
// CURRENT.
function currentCommitTimeMs(versionDir: string): number | undefined {
let commitTime
try {
return fs.statSync(path.join(versionDir, 'CURRENT')).mtimeMs
commitTime = JSON.parse(
fs.readFileSync(path.join(versionDir, 'CURRENT'), 'utf8')
).commit_time
} catch {
// missing, unreadable, or not valid JSON - treat it as not a seed candidate
return undefined
}
if (typeof commitTime !== 'string') return undefined
const parsed = Date.parse(commitTime)
return Number.isNaN(parsed) ? undefined : parsed
}
function dirHasEntries(dir: string): boolean {
......
......@@ -20,7 +20,7 @@ crc32fast = { workspace = true }
dashmap = { workspace = true}
either = { workspace = true }
fs-err = { workspace = true }
jiff = "0.2.10"
jiff = { version = "0.2.10", features = ["serde"] }
lzzzz = { workspace = true }
memmap2 = "0.9.5"
nohash-hasher = { workspace = true }
......@@ -31,6 +31,8 @@ postcard = { workspace = true, features = ["alloc", "use-std"] }
zerocopy = { version = "0.8", features = ["derive"] }
quick_cache = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
smallvec = { workspace = true }
thread_local = { workspace = true }
tracing = { workspace = true }
......
......@@ -12,7 +12,7 @@ It supports having multiple key families, which are stored in separate files, bu
## On disk format
There is a single `CURRENT` file which stores the latest committed sequence number.
There is a single `CURRENT` file, a small JSON object holding the latest committed sequence number (`max_sequence_number`) and when that commit happened (`commit_time`). The commit time is stored in the file rather than taken from its mtime so that it survives the directory being copied or restored. External tools read this file, so its field names are a stable contract.
All other files have a sequence number as file name, e. g. `0000123.sst`. All files are immutable once their sequence number is <= the committed sequence number. But they might be deleted when they are superseded by other committed files.
......
......@@ -24,6 +24,7 @@ use turbo_persistence::{
BLOCK_HEADER_SIZE, checksum_block,
meta_file::MetaFile,
mmap_helper::advise_mmap_for_persistence,
read_current_version,
sst_filter::SstFilter,
static_sorted_file::{
BLOCK_TYPE_FIXED_KEY_NO_HASH, BLOCK_TYPE_FIXED_KEY_WITH_HASH, BLOCK_TYPE_KEY_NO_HASH,
......@@ -211,9 +212,9 @@ fn format_bytes(bytes: u64) -> String {
/// and apply SstFilter to skip superseded entries.
fn collect_sst_info(db_path: &Path) -> Result<BTreeMap<u32, Vec<SstInfo>>> {
// Read the CURRENT sequence number — only files with seq <= current are valid.
let current: u32 = File::open(db_path.join("CURRENT"))?
.read_u32::<BE>()
.context("Failed to read CURRENT file")?;
let current = read_current_version(db_path)?
.context("CURRENT file is missing")?
.max_sequence_number;
// Read .del files to find sequences that were deleted but not yet cleaned up.
let mut deleted_seqs: HashSet<u32> = HashSet::new();
......
......@@ -2,7 +2,7 @@ use std::{
borrow::Cow,
collections::HashSet,
fmt::Display,
io::{BufWriter, Write},
io::{BufWriter, ErrorKind, Write},
mem::take,
ops::RangeInclusive,
path::{Path, PathBuf},
......@@ -20,6 +20,7 @@ use jiff::Timestamp;
use memmap2::Mmap;
use nohash_hasher::BuildNoHashHasher;
use parking_lot::{Mutex, RwLock};
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
use tracing::span::EnteredSpan;
......@@ -159,24 +160,61 @@ impl WriteOperationGuard<'_> {
}
}
/// The contents of the `CURRENT` file: which sequence number is committed, and when that commit
/// happened.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CurrentDbVersion {
/// The highest sequence number that is part of the committed database.
pub max_sequence_number: u32,
/// When this database was last committed to.
pub commit_time: Timestamp,
}
/// Reads the `CURRENT` file in the database directory `path`.
///
/// Returns `Ok(None)` if the file doesn't exist, which for a writable database means "not
/// initialized yet".
pub fn read_current_version(path: &Path) -> Result<Option<CurrentDbVersion>> {
let current_path = path.join("CURRENT");
let content = match fs::read(&current_path) {
Ok(content) => content,
Err(e) if e.kind() == ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(e).context("Failed to read CURRENT file"),
};
serde_json::from_slice::<CurrentDbVersion>(&content)
.with_context(|| {
format!(
"CURRENT file at {} is corrupt ({} bytes)",
current_path.display(),
content.len()
)
})
.map(Some)
}
/// Durably and atomically updates the `CURRENT` file in the database directory `path` to point at
/// `seq`.
/// `seq`, stamping it as used now.
///
/// The write is made atomic by writing `seq` to a temporary `CURRENT.next` file, flushing it, and
/// then `rename`ing it over `CURRENT`. A `rename` within a directory is atomic on POSIX and
/// replaces the destination on Windows, so a concurrent or crashing writer can never observe a
/// torn `CURRENT` (in-place overwrites, by contrast, can leave a partially-written value on a
/// crash mid-write). After the rename we fsync the directory so the new `CURRENT` → inode mapping
/// survives a crash.
/// The write is made atomic by writing to a temporary `CURRENT.next` file, flushing it, and then
/// `rename`ing it over `CURRENT`. A `rename` within a directory is atomic on POSIX and replaces the
/// destination on Windows, so a concurrent or crashing writer can never observe a torn `CURRENT`
/// (in-place overwrites, by contrast, can leave a partially-written value on a crash mid-write).
/// After the rename we fsync the directory so the new `CURRENT` → inode mapping survives a crash.
fn commit_current(path: &Path, seq: u32) -> Result<()> {
let version: &CurrentDbVersion = &CurrentDbVersion {
max_sequence_number: seq,
commit_time: Timestamp::now(),
};
let mut contents =
serde_json::to_string(version).context("Failed to serialize the CURRENT file")?;
contents.push('\n');
let next_path = path.join("CURRENT.next");
let mut next_file = File::create(&next_path)?;
next_file.write_u32::<BE>(seq)?;
next_file.write_all(contents.as_bytes())?;
next_file.sync_data()?;
drop(next_file);
fs::rename(&next_path, path.join("CURRENT"))?;
// Fsync the directory. This is the single durability barrier for a commit: by the time we get
// here every file created earlier in the commit (SST/meta/blob and any `.del` file) already
// exists, so this one fsync flushes *all* of their directory entries together with the CURRENT
......@@ -459,7 +497,7 @@ impl<S: ParallelScheduler, const FAMILIES: usize> TurboPersistence<S, FAMILIES>
Ok(())
}
Err(e) => {
if !read_only && e.kind() == std::io::ErrorKind::NotFound {
if !read_only && e.kind() == ErrorKind::NotFound {
self.create_and_init_directory()
.context("Creating and initializing persistence directory failed")?;
Ok(())
......@@ -479,18 +517,11 @@ impl<S: ParallelScheduler, const FAMILIES: usize> TurboPersistence<S, FAMILIES>
/// Loads an existing database directory and performs cleanup if necessary.
fn load_directory(&mut self, entries: ReadDir, read_only: bool) -> Result<bool> {
let mut meta_files = Vec::new();
let mut current_file = match File::open(self.path.join("CURRENT")) {
Ok(file) => file,
Err(e) => {
if !read_only && e.kind() == std::io::ErrorKind::NotFound {
return Ok(false);
} else {
return Err(e).context("Failed to open CURRENT file");
}
}
let current = match read_current_version(&self.path)? {
Some(version) => version.max_sequence_number,
None if !read_only => return Ok(false),
None => bail!("Failed to open database: CURRENT file is missing"),
};
let current = current_file.read_u32::<BE>()?;
drop(current_file);
let mut deleted_files = HashSet::new();
for entry in entries {
......
......@@ -30,7 +30,10 @@ mod tests;
pub use arc_bytes::ArcBytes;
pub use compression::checksum_block;
pub use db::{CommitStats, CompactConfig, MetaFileEntryInfo, MetaFileInfo, TurboPersistence};
pub use db::{
CommitStats, CompactConfig, CurrentDbVersion, MetaFileEntryInfo, MetaFileInfo,
TurboPersistence, read_current_version,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FamilyKind {
......
......@@ -6,7 +6,7 @@ use rayon::iter::{IntoParallelIterator, ParallelIterator};
use crate::{
DbConfig, FamilyConfig, FamilyKind,
constants::{MAX_MEDIUM_VALUE_SIZE, MAX_SMALL_VALUE_SIZE},
db::{CompactConfig, TurboPersistence},
db::{CompactConfig, TurboPersistence, read_current_version},
parallel_scheduler::ParallelScheduler,
write_batch::WriteBatch,
};
......@@ -2229,3 +2229,38 @@ fn stale_current_next_is_recovered() -> Result<()> {
Ok(())
}
/// `CURRENT` round-trips through JSON, recording both the sequence number and the commit time.
#[test]
fn current_file_is_json_with_commit_time() -> Result<()> {
use crate::parallel_scheduler::SerialScheduler;
let tempdir = tempfile::tempdir()?;
let path = tempdir.path();
let before = jiff::Timestamp::now();
{
let db = TurboPersistence::<SerialScheduler, 1>::open(path.to_path_buf())?;
let batch = db.write_batch()?;
batch.put(0, vec![1u8], vec![42u8].into())?;
db.commit_write_batch(batch)?;
db.shutdown()?;
}
let after = jiff::Timestamp::now();
// next.js parses `CURRENT` without going through this crate, so these field names are a
// public contract.
let raw = fs::read_to_string(path.join("CURRENT"))?;
assert!(raw.contains("max_sequence_number"), "got: {raw}");
assert!(raw.contains("commit_time"), "got: {raw}");
let version = read_current_version(path)?.expect("CURRENT should exist");
assert!(version.max_sequence_number > 0);
assert!(
version.commit_time >= before && version.commit_time <= after,
"commit_time {} outside [{before}, {after}]",
version.commit_time
);
Ok(())
}
......@@ -45,6 +45,7 @@ dashmap = { workspace = true, features = ["raw-api"]}
fs-err = { workspace = true }
hashbrown = { workspace = true, features = ["raw"] }
indexmap = { workspace = true }
jiff = "0.2.10"
lzzzz = { workspace = true, optional = true }
parking_lot = { workspace = true }
rand = { workspace = true }
......
......@@ -7,6 +7,8 @@ use std::{
use anyhow::Result;
use fs_err::{DirEntry, read_dir, remove_dir_all, rename};
use jiff::Timestamp;
use turbo_persistence::read_current_version;
/// Information gathered by `vergen_gitcl` in the top-level binary crate and passed down. This
/// information must be computed in the top-level crate for cargo incremental compilation to work
......@@ -21,10 +23,9 @@ pub struct GitVersionInfo<'a> {
pub dirty: bool,
}
/// Specifies many databases that have a different version than the current one are retained.
/// For example if `DEFAULT_MAX_OTHER_DB_VERSIONS` is 2, there can be at most 3 databases in the
/// directory, the current one and two older/newer ones. On CI it never keeps any other versions.
const DEFAULT_MAX_OTHER_DB_VERSIONS: usize = 2;
/// How many days a database with a version other than the current one is retained since it was
/// last used. Overridable via the `TURBO_ENGINE_VERSION_TTL_DAYS` environment variable.
const DEFAULT_OTHER_DB_VERSION_TTL_DAYS: u64 = 3;
/// Directories are prefixed with this before being deleted, so that if we fail to fully delete the
/// directory, we can pick up where we left off last time.
......@@ -33,12 +34,19 @@ const DELETION_PREFIX: &str = "__stale_";
/// Given a base path, creates a version directory for the given `version_info`. Automatically
/// cleans up old/stale databases.
///
/// The current version is always retained. Alongside it, exactly one database whose version isn't
/// the current one is kept — the most recently used, and only if it was used within
/// [`DEFAULT_OTHER_DB_VERSION_TTL_DAYS`] — so that switching back to a branch you recently left
/// still finds its cache intact. On CI none are retained.
///
/// **Environment Variables**
/// - `TURBO_ENGINE_VERSION`: Forces use of a specific database version.
/// - `TURBO_ENGINE_IGNORE_DIRTY`: Enable filesystem cache in a dirty git repository. Otherwise a
/// temporary directory is created.
/// - `TURBO_ENGINE_DISABLE_VERSIONING`: Ignores versioning and always uses the same "unversioned"
/// database when set.
/// - `TURBO_ENGINE_VERSION_TTL_DAYS`: How many days to retain a database whose version isn't the
/// current one, as a whole number. Overrides [`DEFAULT_OTHER_DB_VERSION_TTL_DAYS`].
pub fn handle_db_versioning(
base_path: &Path,
version_info: &GitVersionInfo,
......@@ -74,14 +82,31 @@ pub fn handle_db_versioning(
if let Some(version) = version {
path = base_path.join(version);
let max_other_db_versions = if is_ci {
0
// On CI nothing is ever switched back to, so no other version is worth its disk.
let ttl = if is_ci {
None
} else {
DEFAULT_MAX_OTHER_DB_VERSIONS
Some(other_db_version_ttl())
};
if let Ok(read_dir) = read_dir(base_path) {
let mut old_dbs = Vec::new();
let evict = |entry: DirEntry| {
let mut new_name = OsString::from(DELETION_PREFIX);
new_name.push(entry.file_name());
let new_path = base_path.join(new_name);
// rename first, it's an atomic operation
let rename_result = rename(entry.path(), &new_path);
// Only try to delete the files if the rename succeeded, it's not safe to delete
// contents if we didn't manage to first poison the directory by renaming it.
if rename_result.is_ok() {
// It's okay if this fails, as we've already poisoned the directory.
let _ = remove_dir_all(&new_path);
}
};
// Of the other versions we keep only the most recently used one, and only if it's
// within the TTL.
let mut newest: Option<(Duration, DirEntry)> = None;
for entry in read_dir {
let Ok(entry) = entry else { continue };
......@@ -109,34 +134,24 @@ pub fn handle_db_versioning(
continue;
}
old_dbs.push(entry);
}
// With no TTL nothing is retained, so don't read an age that can't change the
// outcome — which also means a corrupt `CURRENT` can't fail the run.
let Some(ttl) = ttl else {
evict(entry);
continue;
};
if old_dbs.len() > max_other_db_versions {
old_dbs.sort_by_cached_key(|entry| {
fn get_age(e: &DirEntry) -> Result<Duration> {
let m = e.metadata()?;
// Maybe change this: We care more about the atime/mtime of the files inside
// the directory than the directory itself. atime is also fragile because it
// can be impacted by recursive scanning tools (e.g. ripgrep). It might be
// better for us to always explicitly touch a specific file inside the
// versioned directory when reading the cache, and then use that file's
// mtime.
Ok(m.accessed().or_else(|_| m.modified())?.elapsed()?)
let age = time_since_last_commit(&entry)?;
if age > ttl {
evict(entry);
continue;
}
match &newest {
Some((newest_age, _)) if *newest_age <= age => evict(entry),
_ => {
if let Some((_, previous)) = newest.replace((age, entry)) {
evict(previous);
}
get_age(entry).unwrap_or(Duration::MAX)
});
for entry in old_dbs.into_iter().skip(max_other_db_versions) {
let mut new_name = OsString::from(DELETION_PREFIX);
new_name.push(entry.file_name());
let new_path = base_path.join(new_name);
// rename first, it's an atomic operation
let rename_result = rename(entry.path(), &new_path);
// Only try to delete the files if the rename succeeded, it's not safe to delete
// contents if we didn't manage to first poison the directory by renaming it.
if rename_result.is_ok() {
// It's okay if this fails, as we've already poisoned the directory.
let _ = remove_dir_all(&new_path);
}
}
}
......@@ -153,81 +168,209 @@ pub fn handle_db_versioning(
Ok(path)
}
/// How long to retain a database whose version isn't the current one. Falls back to
/// [`DEFAULT_OTHER_DB_VERSION_TTL_DAYS`] if `TURBO_ENGINE_VERSION_TTL_DAYS` is unset or unparsable.
fn other_db_version_ttl() -> Duration {
let Ok(raw) = env::var("TURBO_ENGINE_VERSION_TTL_DAYS") else {
return ttl_from_days(DEFAULT_OTHER_DB_VERSION_TTL_DAYS);
};
match raw.trim().parse::<u64>() {
Ok(days) => ttl_from_days(days),
Err(_) => {
eprintln!(
"WARNING: Ignoring TURBO_ENGINE_VERSION_TTL_DAYS={raw:?}, expected a whole number \
of days."
);
ttl_from_days(DEFAULT_OTHER_DB_VERSION_TTL_DAYS)
}
}
}
fn ttl_from_days(days: u64) -> Duration {
Duration::from_secs(days.saturating_mul(24 * 60 * 60))
}
/// How long ago the version directory `entry` was last committed to, read from the `commit_time`
/// its `CURRENT` file records
///
/// A directory with no `CURRENT` isn't a database we finished writing — access to the cache root is
/// serialized, so it can't be one that's mid-initialization — and gets [`Duration::MAX`] so it's
/// evicted ahead of any real cache
fn time_since_last_commit(entry: &DirEntry) -> Result<Duration> {
let Some(version) = read_current_version(&entry.path())? else {
return Ok(Duration::MAX);
};
Ok(Timestamp::now()
.duration_since(version.commit_time)
.try_into()
.unwrap_or_default())
}
#[cfg(test)]
mod tests {
use std::{fs, thread::sleep};
use std::fs;
use rstest::rstest;
use tempfile::TempDir;
use turbo_persistence::CurrentDbVersion;
use super::*;
fn count_entries(base_path: &Path) -> usize {
fs::read_dir(base_path)
.unwrap()
.collect::<Result<Vec<_>, _>>()
const CURRENT_VERSION: &str = "mock-version";
fn version_info() -> GitVersionInfo<'static> {
GitVersionInfo {
describe: CURRENT_VERSION,
dirty: false,
}
}
/// Creates a version directory that looks like a real database (i.e. has a `CURRENT` file),
/// last committed to `committed_ago` in the past.
fn create_version_dir(base_path: &Path, name: &str, committed_ago: Duration) {
let path = base_path.join(name);
fs::create_dir(&path).unwrap();
let commit_time = Timestamp::now() - jiff::SignedDuration::try_from(committed_ago).unwrap();
fs::write(
path.join("CURRENT"),
serde_json::to_vec(&CurrentDbVersion {
max_sequence_number: 0,
commit_time,
})
.unwrap(),
)
.unwrap();
}
fn entry_names(base_path: &Path) -> Vec<String> {
let mut names = fs::read_dir(base_path)
.unwrap()
.len()
.map(|e| e.unwrap().file_name().into_string().unwrap())
.collect::<Vec<_>>();
names.sort();
names
}
/// Only the most recently used other version survives, and the current version survives
/// regardless of how stale it is. On CI no other version survives at all.
#[rstest]
#[case::not_ci(false, DEFAULT_MAX_OTHER_DB_VERSIONS)]
#[case::ci(true, 0)]
fn test_max_versions(#[case] is_ci: bool, #[case] max_other_db_versions: usize) {
#[case::not_ci(false, &["mock-version", "other-dir-0"])]
#[case::ci(true, &["mock-version"])]
fn test_only_most_recently_used_other_version_is_retained(
#[case] is_ci: bool,
#[case] expected: &[&str],
) {
let tmp_dir = TempDir::new().unwrap();
let base_path = tmp_dir.path();
let current_version_name = "mock-version";
let version_info = GitVersionInfo {
describe: current_version_name,
dirty: false,
};
fs::create_dir(base_path.join(current_version_name)).unwrap();
// the least recently used of all, and preserved anyway
create_version_dir(base_path, CURRENT_VERSION, Duration::from_secs(60 * 60));
// sleep to ensure `current_version_name` has the oldest atime/mtime
// it should be preserved regardless of atime/mtime
sleep(Duration::from_millis(100));
for i in 0..4 {
// `other-dir-0` is the most recently used, so it's the one retained
create_version_dir(
base_path,
&format!("other-dir-{i}"),
Duration::from_secs(i + 1),
);
}
let num_other_dirs = max_other_db_versions + 3;
for i in 0..num_other_dirs {
fs::create_dir(base_path.join(format!("other-dir-{i}"))).unwrap();
let versioned_path = handle_db_versioning(base_path, &version_info(), is_ci).unwrap();
assert_eq!(versioned_path, base_path.join(CURRENT_VERSION));
assert_eq!(entry_names(base_path), expected);
}
assert_eq!(
count_entries(base_path),
num_other_dirs + 1, // +1 for current version
/// A version that hasn't been used within the TTL is evicted, even with the retention slot
/// free.
#[test]
fn test_ttl_evicts_unused_version() {
let tmp_dir = TempDir::new().unwrap();
let base_path = tmp_dir.path();
create_version_dir(base_path, CURRENT_VERSION, Duration::ZERO);
create_version_dir(
base_path,
"stale-version",
ttl_from_days(DEFAULT_OTHER_DB_VERSION_TTL_DAYS) + Duration::from_secs(60),
);
let versioned_path = handle_db_versioning(base_path, &version_info, is_ci).unwrap();
handle_db_versioning(base_path, &version_info(), /* is_ci */ false).unwrap();
assert_eq!(entry_names(base_path), vec![CURRENT_VERSION]);
}
/// A directory with no `CURRENT` file isn't one of ours, so it's evicted rather than occupying
/// the single retention slot — even when it holds recently written files.
#[rstest]
#[case::empty(false)]
#[case::with_recent_data_file(true)]
fn test_version_without_stamp_is_evicted(#[case] with_data_file: bool) {
let tmp_dir = TempDir::new().unwrap();
let base_path = tmp_dir.path();
create_version_dir(base_path, CURRENT_VERSION, Duration::ZERO);
let unstamped = base_path.join("unstamped-version");
fs::create_dir(&unstamped).unwrap();
if with_data_file {
fs::write(unstamped.join("00000001.sst"), b"data").unwrap();
}
handle_db_versioning(base_path, &version_info(), /* is_ci */ false).unwrap();
assert_eq!(entry_names(base_path), vec![CURRENT_VERSION]);
}
assert_eq!(versioned_path, base_path.join(current_version_name));
assert!(base_path.join(current_version_name).exists());
assert_eq!(
count_entries(base_path),
max_other_db_versions + 1, // +1 for current version
/// A `CURRENT` that exists but can't be parsed is an unexpected failure, not something to
/// silently act on: the error propagates instead of the directory being deleted.
#[test]
fn test_corrupt_current_propagates_error() {
let tmp_dir = TempDir::new().unwrap();
let base_path = tmp_dir.path();
create_version_dir(base_path, CURRENT_VERSION, Duration::ZERO);
let corrupt = base_path.join("corrupt-version");
fs::create_dir(&corrupt).unwrap();
fs::write(corrupt.join("CURRENT"), b"not json").unwrap();
assert!(
handle_db_versioning(base_path, &version_info(), /* is_ci */ false).is_err(),
"an unreadable CURRENT should surface as an error"
);
assert!(
entry_names(base_path).contains(&"corrupt-version".to_string()),
"the directory should not have been deleted"
);
}
/// On CI every other version is evicted regardless of age, so an unreadable `CURRENT` in one of
/// them is never consulted and can't fail the run.
#[test]
fn test_cleanup_of_prefixed_items() {
fn test_ci_ignores_unreadable_current() {
let tmp_dir = TempDir::new().unwrap();
let base_path = tmp_dir.path();
let current_version_name = "mock-version";
let version_info = GitVersionInfo {
describe: current_version_name,
dirty: false,
};
create_version_dir(base_path, CURRENT_VERSION, Duration::ZERO);
let corrupt = base_path.join("corrupt-version");
fs::create_dir(&corrupt).unwrap();
fs::write(corrupt.join("CURRENT"), b"not json").unwrap();
handle_db_versioning(base_path, &version_info(), /* is_ci */ true).unwrap();
assert_eq!(entry_names(base_path), vec![CURRENT_VERSION]);
}
#[test]
fn test_cleanup_of_prefixed_items() {
let tmp_dir = TempDir::new().unwrap();
let base_path = tmp_dir.path();
for i in 0..5 {
fs::create_dir(base_path.join(format!("{DELETION_PREFIX}other-dir-{i}"))).unwrap();
}
assert_eq!(count_entries(base_path), 5);
handle_db_versioning(base_path, &version_info, /* is_ci */ false).unwrap();
handle_db_versioning(base_path, &version_info(), /* is_ci */ false).unwrap();
assert_eq!(count_entries(base_path), 0);
assert!(entry_names(base_path).is_empty());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment