turbo_tasks_backend/database/
db_invalidation.rs

1use std::{
2    borrow::Cow,
3    fs::{self, File, read_dir},
4    io::{self, BufReader, BufWriter, ErrorKind, Write},
5    path::Path,
6};
7
8use anyhow::Context;
9use serde::{Deserialize, Serialize};
10
11const INVALIDATION_MARKER: &str = "__turbo_tasks_invalidated_db";
12
13const EXPLANATION: &str = "The cache database has been invalidated. The existence of this file \
14                           will cause the cache directory to be cleaned up the next time \
15                           Turbopack starts up.";
16const EASTER_EGG: &str =
17    "you just wrote me, and this is crazy, but if you see me, delete everything maybe?";
18
19/// The data written to the file at [`INVALIDATION_MARKER`].
20#[derive(Serialize, Deserialize)]
21struct InvalidationFile<'a> {
22    #[serde(skip_deserializing)]
23    _explanation: Option<&'static str>,
24    #[serde(skip_deserializing)]
25    _easter_egg: Option<&'static str>,
26    /// See [`StartupCacheState::Invalidated::reason_code`].
27    reason_code: Cow<'a, str>,
28}
29
30/// Information about if there's was a pre-existing cache or if the cache was detected as
31/// invalidated during startup.
32///
33/// If the cache was invalidated, the application may choose to show a warning to the user or log it
34/// to telemetry.
35///
36/// This value is returned by [`crate::turbo_backing_storage`] and
37/// [`crate::default_backing_storage`].
38pub enum StartupCacheState {
39    NoCache,
40    Cached,
41    Invalidated {
42        /// A short code passed to [`BackingStorage::invalidate`]. This value is
43        /// application-specific.
44        ///
45        /// If the value is `None` or doesn't match an expected value, the application should just
46        /// treat this reason as unknown. The invalidation file may have been corrupted or
47        /// modified by an external tool.
48        ///
49        /// See [`invalidation_reasons`] for some common reason codes.
50        ///
51        /// [`BackingStorage::invalidate`]: crate::BackingStorage::invalidate
52        reason_code: Option<String>,
53    },
54}
55
56/// Common invalidation reason codes. The application or libraries it uses may choose to use these
57/// reasons, or it may define it's own reasons.
58pub mod invalidation_reasons {
59    /// This invalidation reason is used by [`crate::turbo_backing_storage`] when the database was
60    /// invalidated by a panic.
61    pub const PANIC: &str = concat!(module_path!(), "::PANIC");
62    /// Indicates that the user explicitly clicked a button or ran a command that invalidates the
63    /// cache.
64    pub const USER_REQUEST: &str = concat!(module_path!(), "::USER_REQUEST");
65}
66
67/// Atomically create an invalidation marker.
68///
69/// Makes a best-effort attempt to write `reason_code` to the file, but ignores any failure with
70/// writing to the file.
71///
72/// Because attempting to delete currently open database files could cause issues, actual deletion
73/// of files is deferred until the next start-up (in [`check_db_invalidation_and_cleanup`]).
74///
75/// In the case that no database is currently open (e.g. via a separate CLI subcommand), you should
76/// call [`cleanup_db`] *after* this to eagerly remove the database files.
77///
78/// This should be run with the base (non-versioned) path, as that likely aligns closest with user
79/// expectations (e.g. if they're clearing the cache for disk space reasons).
80///
81/// In most cases, you should prefer a higher-level API like [`crate::BackingStorage::invalidate`]
82/// to this one.
83pub(crate) fn invalidate_db(base_path: &Path, reason_code: &str) -> anyhow::Result<()> {
84    match File::create_new(base_path.join(INVALIDATION_MARKER)) {
85        Ok(file) => {
86            let mut writer = BufWriter::new(file);
87            // ignore errors: We've already successfully invalidated the cache just by creating the
88            // marker file, writing the reason_code is best-effort.
89            let _ = serde_json::to_writer_pretty(
90                &mut writer,
91                &InvalidationFile {
92                    _explanation: Some(EXPLANATION),
93                    _easter_egg: Some(EASTER_EGG),
94                    reason_code: Cow::Borrowed(reason_code),
95                },
96            );
97            let _ = writer.flush();
98            Ok(())
99        }
100        // the database was already invalidated, avoid overwriting that reason or risking concurrent
101        // writes to the same file.
102        Err(err) if err.kind() == ErrorKind::AlreadyExists => Ok(()),
103        // just ignore if the cache directory doesn't exist at all
104        Err(err) if err.kind() == ErrorKind::NotFound => Ok(()),
105        Err(err) => Err(err).context("Failed to invalidate database"),
106    }
107}
108
109/// Called during startup. See if the db is in a partially-completed invalidation state. Find and
110/// delete any invalidated database files.
111///
112/// This should be run with the base (non-versioned) path.
113///
114/// In most cases, you should prefer a higher-level API like
115/// [`crate::KeyValueDatabaseBackingStorage::open_versioned_on_disk`] to this one.
116pub(crate) fn check_db_invalidation_and_cleanup(
117    base_path: &Path,
118) -> anyhow::Result<StartupCacheState> {
119    match File::open(base_path.join(INVALIDATION_MARKER)) {
120        Ok(file) => {
121            // Best-effort: Try to read the reason_code from the file, if the file format is
122            // corrupted (or anything else) just use `None`.
123            let reason_code = serde_json::from_reader::<_, InvalidationFile>(BufReader::new(file))
124                .ok()
125                .map(|contents| contents.reason_code.into_owned());
126            // `file` is dropped at this point: That's important for Windows where we can't delete
127            // open files.
128
129            // if this cleanup fails, we might try to open an invalid database later, so it's best
130            // to just propagate the error here.
131            cleanup_db(base_path)?;
132            Ok(StartupCacheState::Invalidated { reason_code })
133        }
134        Err(err) if err.kind() == ErrorKind::NotFound => {
135            if fs::exists(base_path)? {
136                Ok(StartupCacheState::Cached)
137            } else {
138                Ok(StartupCacheState::NoCache)
139            }
140        }
141        Err(err) => Err(err)
142            .with_context(|| format!("Failed to check for {INVALIDATION_MARKER} in {base_path:?}")),
143    }
144}
145
146/// Helper for [`check_db_invalidation_and_cleanup`]. You can call this to explicitly clean up a
147/// database after running [`invalidate_db`] when turbo-tasks is not running.
148///
149/// You should not run this if the database has not yet been invalidated, as this operation is not
150/// atomic and could result in a partially-deleted and corrupted database.
151pub(crate) fn cleanup_db(base_path: &Path) -> anyhow::Result<()> {
152    cleanup_db_inner(base_path).with_context(|| {
153        format!(
154            "Unable to remove invalid database. If this issue persists you can work around by \
155             deleting {base_path:?}."
156        )
157    })
158}
159
160fn cleanup_db_inner(base_path: &Path) -> io::Result<()> {
161    let Ok(contents) = read_dir(base_path) else {
162        return Ok(());
163    };
164
165    // delete everything except the invalidation marker
166    for entry in contents {
167        let entry = entry?;
168        if entry.file_name() != INVALIDATION_MARKER {
169            fs::remove_dir_all(entry.path())?;
170        }
171    }
172
173    // delete the invalidation marker last, once we're sure everything is cleaned up
174    fs::remove_file(base_path.join(INVALIDATION_MARKER))?;
175    Ok(())
176}