File size: 8,721 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use std::{
    env,
    ffi::{OsStr, OsString},
    fs::{DirEntry, read_dir, remove_dir_all, rename},
    path::{Path, PathBuf},
    time::Duration,
};

use anyhow::Result;

/// 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
/// correctly.
///
/// See `crates/napi/build.rs` for details.
pub struct GitVersionInfo<'a> {
    /// Output of `git describe --match 'v[0-9]' --dirty`.
    pub describe: &'a str,
    /// Is the git repository dirty? Always forced to `false` when the `CI` environment variable is
    /// set and non-empty.
    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;

/// 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.
const DELETION_PREFIX: &str = "__stale_";

/// Given a base path, creates a version directory for the given `version_info`. Automatically
/// cleans up old/stale databases.
///
/// **Environment Variables**
/// - `TURBO_ENGINE_VERSION`: Forces use of a specific database version.
/// - `TURBO_ENGINE_IGNORE_DIRTY`: Enable persistent caching 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.
pub fn handle_db_versioning(
    base_path: &Path,
    version_info: &GitVersionInfo,
    is_ci: bool,
) -> Result<PathBuf> {
    if let Ok(version) = env::var("TURBO_ENGINE_VERSION") {
        return Ok(base_path.join(version));
    }
    let ignore_dirty = env::var("TURBO_ENGINE_IGNORE_DIRTY").ok().is_some();
    let disabled_versioning = env::var("TURBO_ENGINE_DISABLE_VERSIONING").ok().is_some();
    let version = if disabled_versioning {
        println!(
            "WARNING: Persistent Caching versioning is disabled. Manual removal of the persistent \
             caching database might be required."
        );
        Some("unversioned")
    } else if !version_info.dirty {
        Some(version_info.describe)
    } else if ignore_dirty {
        println!(
            "WARNING: The git repository is dirty, but Persistent Caching is still enabled. \
             Manual removal of the persistent caching database might be required."
        );
        Some(version_info.describe)
    } else {
        println!(
            "WARNING: The git repository is dirty: Persistent Caching is disabled. Use \
             TURBO_ENGINE_IGNORE_DIRTY=1 to ignore dirtiness of the repository."
        );
        None
    };
    let path;
    if let Some(version) = version {
        path = base_path.join(version);

        let max_other_db_versions = if is_ci {
            0
        } else {
            DEFAULT_MAX_OTHER_DB_VERSIONS
        };

        if let Ok(read_dir) = read_dir(base_path) {
            let mut old_dbs = Vec::new();
            for entry in read_dir {
                let Ok(entry) = entry else { continue };

                // skip our target version (if it exists)
                let name = entry.file_name();
                if name == version {
                    continue;
                }

                // skip non-directories
                let Ok(file_type) = entry.file_type() else {
                    continue;
                };
                if !file_type.is_dir() {
                    continue;
                }

                // Find and try to finish removing any partially deleted directories
                if name
                    .as_encoded_bytes()
                    .starts_with(AsRef::<OsStr>::as_ref(DELETION_PREFIX).as_encoded_bytes())
                {
                    // failures during cleanup of a cache directory are not fatal
                    let _ = remove_dir_all(entry.path());
                    continue;
                }

                old_dbs.push(entry);
            }

            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()?)
                    }
                    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);
                    }
                }
            }
        }
    } else {
        path = base_path.join("temp");
        if path.exists() {
            // propagate errors: if this fails we may have stale files left over in the temp
            // directory
            remove_dir_all(&path)?;
        }
    }

    Ok(path)
}

#[cfg(test)]
mod tests {
    use std::{fs, thread::sleep};

    use rstest::rstest;
    use tempfile::TempDir;

    use super::*;

    fn count_entries(base_path: &Path) -> usize {
        fs::read_dir(base_path)
            .unwrap()
            .collect::<Result<Vec<_>, _>>()
            .unwrap()
            .len()
    }

    #[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) {
        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();

        // sleep to ensure `current_version_name` has the oldest atime/mtime
        // it should be preserved regardless of atime/mtime
        sleep(Duration::from_millis(100));

        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();
        }

        assert_eq!(
            count_entries(base_path),
            num_other_dirs + 1, // +1 for current version
        );

        let versioned_path = handle_db_versioning(base_path, &version_info, is_ci).unwrap();

        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
        );
    }

    #[test]
    fn test_cleanup_of_prefixed_items() {
        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,
        };

        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();

        assert_eq!(count_entries(base_path), 0);
    }
}