chore: rustfmt the workspace and make clippy -D warnings pass
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -31,7 +31,15 @@ pub struct Plan {
|
||||
|
||||
fn plan_of(extension: &'static str, compress: bool) -> Plan {
|
||||
let gz = extension.ends_with("gz");
|
||||
Plan { extension, compress, content_type: if gz || compress { "application/gzip" } else { "application/octet-stream" } }
|
||||
Plan {
|
||||
extension,
|
||||
compress,
|
||||
content_type: if gz || compress {
|
||||
"application/gzip"
|
||||
} else {
|
||||
"application/octet-stream"
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Decide the delivery from the manifest `restore_hints` and the first bytes of the exported file.
|
||||
@@ -92,20 +100,32 @@ pub enum Refused {
|
||||
|
||||
impl ExportGate {
|
||||
pub fn new(concurrency: usize, min_free: u64) -> Self {
|
||||
Self { permits: Arc::new(Semaphore::new(concurrency)), min_free }
|
||||
Self {
|
||||
permits: Arc::new(Semaphore::new(concurrency)),
|
||||
min_free,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_env() -> Self {
|
||||
let number = |name: &str| std::env::var(name).ok().and_then(|v| v.trim().parse::<u64>().ok());
|
||||
let number = |name: &str| {
|
||||
std::env::var(name)
|
||||
.ok()
|
||||
.and_then(|v| v.trim().parse::<u64>().ok())
|
||||
};
|
||||
Self::new(
|
||||
number("TESSERA_EXPORT_CONCURRENCY").map_or(DEFAULT_CONCURRENCY, |n| n.clamp(1, 64) as usize),
|
||||
number("TESSERA_EXPORT_CONCURRENCY")
|
||||
.map_or(DEFAULT_CONCURRENCY, |n| n.clamp(1, 64) as usize),
|
||||
number("TESSERA_EXPORT_MIN_FREE_BYTES").unwrap_or(DEFAULT_MARGIN),
|
||||
)
|
||||
}
|
||||
|
||||
/// Admit one export of about `size` bytes into `dir`. The permit lives as long as the response body.
|
||||
pub fn admit(&self, dir: &std::path::Path, size: u64) -> Result<OwnedSemaphorePermit, Refused> {
|
||||
let permit = self.permits.clone().try_acquire_owned().map_err(|_| Refused::Busy)?;
|
||||
let permit = self
|
||||
.permits
|
||||
.clone()
|
||||
.try_acquire_owned()
|
||||
.map_err(|_| Refused::Busy)?;
|
||||
match free_bytes(dir) {
|
||||
// `size` is the snapshot's recorded size; when a producer did not record one it is 0 and the margin alone applies.
|
||||
Some(free) if free < size.saturating_add(self.min_free) => Err(Refused::NoSpace),
|
||||
@@ -128,7 +148,9 @@ fn free_bytes(_dir: &std::path::Path) -> Option<u64> {
|
||||
/// Remove export directories a crash left behind. Only directories with our own prefix, only directly
|
||||
/// inside `data_dir`; run before the listener starts. Older `.tmp*` leftovers are deliberately not touched.
|
||||
pub fn sweep_stale(data_dir: &std::path::Path) -> usize {
|
||||
let Ok(entries) = std::fs::read_dir(data_dir) else { return 0 };
|
||||
let Ok(entries) = std::fs::read_dir(data_dir) else {
|
||||
return 0;
|
||||
};
|
||||
let mut removed = 0;
|
||||
for entry in entries.flatten() {
|
||||
let name = entry.file_name();
|
||||
@@ -156,8 +178,26 @@ pub struct Guarded<S> {
|
||||
}
|
||||
|
||||
impl<S> Guarded<S> {
|
||||
pub fn new(inner: S, permit: OwnedSemaphorePermit, dir: tempfile::TempDir, db: Db, principal: Principal, snapshot: Uuid, request: Uuid) -> Self {
|
||||
Self { inner: Box::pin(inner), _permit: permit, _dir: dir, db, principal, snapshot, request, completed: false, failed: false }
|
||||
pub fn new(
|
||||
inner: S,
|
||||
permit: OwnedSemaphorePermit,
|
||||
dir: tempfile::TempDir,
|
||||
db: Db,
|
||||
principal: Principal,
|
||||
snapshot: Uuid,
|
||||
request: Uuid,
|
||||
) -> Self {
|
||||
Self {
|
||||
inner: Box::pin(inner),
|
||||
_permit: permit,
|
||||
_dir: dir,
|
||||
db,
|
||||
principal,
|
||||
snapshot,
|
||||
request,
|
||||
completed: false,
|
||||
failed: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,7 +223,13 @@ impl<S> Drop for Guarded<S> {
|
||||
} else {
|
||||
(AuditAction::ExportDisconnected, AuditOutcome::Failed)
|
||||
};
|
||||
if let Err(error) = self.db.audit_event(&self.principal, action, AuditResource::Snapshot(self.snapshot), outcome, self.request) {
|
||||
if let Err(error) = self.db.audit_event(
|
||||
&self.principal,
|
||||
action,
|
||||
AuditResource::Snapshot(self.snapshot),
|
||||
outcome,
|
||||
self.request,
|
||||
) {
|
||||
tracing::error!("could not audit the end of an export: {error:#}");
|
||||
}
|
||||
}
|
||||
@@ -204,25 +250,117 @@ mod tests {
|
||||
fn every_producer_of_a_snapshot_gets_the_right_delivery() {
|
||||
let gz = [0x1f, 0x8b, 8, 0];
|
||||
let cases: Vec<(&str, serde_json::Value, Vec<u8>, &str, bool)> = vec![
|
||||
("hub database orbit, postgres plain SQL", json!({"engine": "postgres", "format": "plain"}), b"-- PostgreSQL dump\n".to_vec(), "sql.gz", true),
|
||||
("hub database orbit, mysql plain SQL", json!({"engine": "mysql", "format": "plain"}), b"-- MySQL dump\n".to_vec(), "sql.gz", true),
|
||||
("agent capture, postgres custom", json!({"engine": "postgres", "format": "custom", "sha256": "x"}), b"PGDMP\x01\x0e\x00".to_vec(), "dump", false),
|
||||
("hub postgres provider, custom", json!({"format": "custom", "restore_command_hint": "pg_restore"}), b"PGDMP\x01".to_vec(), "dump", false),
|
||||
("restic filesystem, tar by hint", json!({"format": "tar"}), ustar(), "tar.gz", true),
|
||||
("restic filesystem snapshot with no hint but a tar payload", serde_json::Value::Null, ustar(), "tar.gz", true),
|
||||
("hub filesystem orbit archive", json!({"paths": [], "format": "tessera-fs-v1"}), b"a/b\0\x03\0\0\0\0\0\0\0xyz".to_vec(), "tessera-fs.gz", true),
|
||||
("sqlite database", json!({"format": "sqlite"}), b"SQLite format 3\0....".to_vec(), "sqlite.gz", true),
|
||||
("SQL that is already gzip", json!({"format": "sql_gzip"}), gz.to_vec(), "sql.gz", false),
|
||||
("hint says plain SQL but the file is gzip: no double compression", json!({"format": "plain"}), gz.to_vec(), "sql.gz", false),
|
||||
("a tar that is already gzip", json!({"format": "tar"}), gz.to_vec(), "tar.gz", false),
|
||||
("zstd payload", serde_json::Value::Null, vec![0x28, 0xb5, 0x2f, 0xfd, 0], "zst", false),
|
||||
("legacy wormhole commit with no hints", serde_json::Value::Null, b"opaque payload".to_vec(), "dump.gz", true),
|
||||
("demo seed", json!({"demo": true}), b"demo".to_vec(), "dump.gz", true),
|
||||
(
|
||||
"hub database orbit, postgres plain SQL",
|
||||
json!({"engine": "postgres", "format": "plain"}),
|
||||
b"-- PostgreSQL dump\n".to_vec(),
|
||||
"sql.gz",
|
||||
true,
|
||||
),
|
||||
(
|
||||
"hub database orbit, mysql plain SQL",
|
||||
json!({"engine": "mysql", "format": "plain"}),
|
||||
b"-- MySQL dump\n".to_vec(),
|
||||
"sql.gz",
|
||||
true,
|
||||
),
|
||||
(
|
||||
"agent capture, postgres custom",
|
||||
json!({"engine": "postgres", "format": "custom", "sha256": "x"}),
|
||||
b"PGDMP\x01\x0e\x00".to_vec(),
|
||||
"dump",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"hub postgres provider, custom",
|
||||
json!({"format": "custom", "restore_command_hint": "pg_restore"}),
|
||||
b"PGDMP\x01".to_vec(),
|
||||
"dump",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"restic filesystem, tar by hint",
|
||||
json!({"format": "tar"}),
|
||||
ustar(),
|
||||
"tar.gz",
|
||||
true,
|
||||
),
|
||||
(
|
||||
"restic filesystem snapshot with no hint but a tar payload",
|
||||
serde_json::Value::Null,
|
||||
ustar(),
|
||||
"tar.gz",
|
||||
true,
|
||||
),
|
||||
(
|
||||
"hub filesystem orbit archive",
|
||||
json!({"paths": [], "format": "tessera-fs-v1"}),
|
||||
b"a/b\0\x03\0\0\0\0\0\0\0xyz".to_vec(),
|
||||
"tessera-fs.gz",
|
||||
true,
|
||||
),
|
||||
(
|
||||
"sqlite database",
|
||||
json!({"format": "sqlite"}),
|
||||
b"SQLite format 3\0....".to_vec(),
|
||||
"sqlite.gz",
|
||||
true,
|
||||
),
|
||||
(
|
||||
"SQL that is already gzip",
|
||||
json!({"format": "sql_gzip"}),
|
||||
gz.to_vec(),
|
||||
"sql.gz",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"hint says plain SQL but the file is gzip: no double compression",
|
||||
json!({"format": "plain"}),
|
||||
gz.to_vec(),
|
||||
"sql.gz",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"a tar that is already gzip",
|
||||
json!({"format": "tar"}),
|
||||
gz.to_vec(),
|
||||
"tar.gz",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"zstd payload",
|
||||
serde_json::Value::Null,
|
||||
vec![0x28, 0xb5, 0x2f, 0xfd, 0],
|
||||
"zst",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"legacy wormhole commit with no hints",
|
||||
serde_json::Value::Null,
|
||||
b"opaque payload".to_vec(),
|
||||
"dump.gz",
|
||||
true,
|
||||
),
|
||||
(
|
||||
"demo seed",
|
||||
json!({"demo": true}),
|
||||
b"demo".to_vec(),
|
||||
"dump.gz",
|
||||
true,
|
||||
),
|
||||
];
|
||||
for (name, hints, head, extension, compress) in cases {
|
||||
let got = plan(&hints, &head);
|
||||
assert_eq!((got.extension, got.compress), (extension, compress), "{name}");
|
||||
let ct = if extension.ends_with("gz") { "application/gzip" } else { "application/octet-stream" };
|
||||
assert_eq!(
|
||||
(got.extension, got.compress),
|
||||
(extension, compress),
|
||||
"{name}"
|
||||
);
|
||||
let ct = if extension.ends_with("gz") {
|
||||
"application/gzip"
|
||||
} else {
|
||||
"application/octet-stream"
|
||||
};
|
||||
assert_eq!(got.content_type, ct, "{name}");
|
||||
}
|
||||
}
|
||||
@@ -235,10 +373,19 @@ mod tests {
|
||||
make("tessera-export-bbbb");
|
||||
make("vault");
|
||||
make(".tmpXYZ");
|
||||
std::fs::write(dir.path().join("tessera-export-file"), b"a file, not a directory").unwrap();
|
||||
std::fs::write(
|
||||
dir.path().join("tessera-export-file"),
|
||||
b"a file, not a directory",
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::create_dir_all(dir.path().join("vault/tessera-export-nested")).unwrap();
|
||||
assert_eq!(sweep_stale(dir.path()), 2);
|
||||
for kept in ["vault", ".tmpXYZ", "tessera-export-file", "vault/tessera-export-nested"] {
|
||||
for kept in [
|
||||
"vault",
|
||||
".tmpXYZ",
|
||||
"tessera-export-file",
|
||||
"vault/tessera-export-nested",
|
||||
] {
|
||||
assert!(dir.path().join(kept).exists(), "{kept} must survive");
|
||||
}
|
||||
assert!(!dir.path().join("tessera-export-aaaa").exists());
|
||||
@@ -248,12 +395,30 @@ mod tests {
|
||||
fn admission_is_bounded_by_concurrency_and_by_free_disk() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let gate = ExportGate::new(1, 0);
|
||||
let held = gate.admit(dir.path(), 1).ok().expect("first export is admitted");
|
||||
assert!(matches!(gate.admit(dir.path(), 1), Err(Refused::Busy)), "second export is refused at once");
|
||||
let held = gate
|
||||
.admit(dir.path(), 1)
|
||||
.ok()
|
||||
.expect("first export is admitted");
|
||||
assert!(
|
||||
matches!(gate.admit(dir.path(), 1), Err(Refused::Busy)),
|
||||
"second export is refused at once"
|
||||
);
|
||||
drop(held);
|
||||
assert!(gate.admit(dir.path(), 1).is_ok(), "the permit comes back when the first ends");
|
||||
assert!(
|
||||
gate.admit(dir.path(), 1).is_ok(),
|
||||
"the permit comes back when the first ends"
|
||||
);
|
||||
let greedy = ExportGate::new(4, u64::MAX / 2);
|
||||
assert!(matches!(greedy.admit(dir.path(), 10), Err(Refused::NoSpace)));
|
||||
assert!(matches!(ExportGate::new(4, 0).admit(dir.path(), u64::MAX), Err(Refused::NoSpace)), "a snapshot bigger than the disk");
|
||||
assert!(matches!(
|
||||
greedy.admit(dir.path(), 10),
|
||||
Err(Refused::NoSpace)
|
||||
));
|
||||
assert!(
|
||||
matches!(
|
||||
ExportGate::new(4, 0).admit(dir.path(), u64::MAX),
|
||||
Err(Refused::NoSpace)
|
||||
),
|
||||
"a snapshot bigger than the disk"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user