//! Object-key derivation and tunables for template and snapshot artifacts. //! //! Builder/template-manager (templates) and sandbox-manager (snapshots) use //! the helpers here so they agree on object keys without coordination. Keys //! are a pure function of the artifact identifier plus an optional key //! prefix; nothing about the key flows on the wire. //! //! Also defines [`StorageUri`] — the self-describing connection-string //! format used by `ClusterApi.RegisterTemplateRelease` / //! `FindTemplateRelease`. The cluster service validates inserts against //! this parser, so consumers (template-manager, etc.) can decode every //! bit they need from the URI alone, without object-store env vars. //! //! # Examples //! //! ```rust //! # use pplx_space_artifact_model::*; //! # use pplx_space_template_model::TemplateName; //! # use pplx_space_content_digest::{ContentHash, Digest}; //! let template = TemplateName::parse("seed")?; //! let hash = ContentHash::new(Digest::sha256([0xab; 32])); //! let key = template_object_key("", &template, hash, TemplateArtifact::RootfsZstd); //! assert!(key.starts_with("templates/seed/sha256:")); //! assert!(key.ends_with("/rootfs.ext4.zst")); //! # Ok::<(), Box>(()) //! ``` use std::str::FromStr; use pplx_space_content_digest::ContentHash; use pplx_space_template_model::TemplateName; /// Number of prefix shards under `suspend_snapshots/` in object storage. /// /// S3 auto-partitions a bucket per key prefix and rate-limits each /// partition independently (~3,500 PUT/s). A single shared prefix /// (`suspend_snapshots//`) throttles a node-wide drain into one /// partition; sharding the prefix into `//` lets S3 grant /// each shard its own request budget, multiplying the per-second PUT /// ceiling. 64 shards is comfortable headroom over the worst-case ~6.4K /// parts a single 400-sandbox node-drain produces. pub const SNAPSHOT_SHARD_COUNT: u32 = 64; const SUSPEND_SNAPSHOT_KEY_SEGMENT: &str = "suspend_snapshots"; /// Returns the prefix shard for a sandbox's suspend-snapshot artifacts. /// /// Deterministic and stable across Rust versions: the FNV-1a 64-bit hash /// is a published algorithm with fixed constants, so the shard a given /// `sandbox_id` lands in never changes. That means the storage URI written /// into `snapshot_storage_locations` is reconstructible from the /// `sandbox_id` alone, which simplifies operator scripts that need to find /// artifacts without going through cluster-api. /// /// # Examples /// /// ```rust /// # use pplx_space_artifact_model::{snapshot_shard, SNAPSHOT_SHARD_COUNT}; /// let s = snapshot_shard("019e3e2d-7038-7c40-b8fa-b226d6a702c3"); /// assert!((s as u32) < SNAPSHOT_SHARD_COUNT); /// // Stable: same input always lands in the same shard. /// assert_eq!(s, snapshot_shard("019e3e2d-7038-7c40-b8fa-b226d6a702c3")); /// ``` pub fn snapshot_shard(sandbox_id: &str) -> u8 { // FNV-1a 64-bit. Constants are part of the published spec, so this is // version- and platform-stable. const OFFSET: u64 = 0xcbf2_9ce4_8422_2325; const PRIME: u64 = 0x0000_0100_0000_01b3; let mut h = OFFSET; for b in sandbox_id.as_bytes() { h ^= u64::from(*b); h = h.wrapping_mul(PRIME); } (h % u64::from(SNAPSHOT_SHARD_COUNT)) as u8 } /// Identifies one artifact of a template build. #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] pub enum TemplateArtifact { /// zstd-compressed root filesystem transfer artifact (`rootfs.ext4.zst`). RootfsZstd, /// Builder-emitted metadata document (`metadata.json`). Metadata, /// Template-manager manifest document (`manifest.json`). Manifest, } impl TemplateArtifact { /// Every artifact a published build consists of, in an order safe to write /// them in. /// /// `Manifest` goes last because it is the only one that describes the /// others: a puller reads `manifest.json` first and then fetches exactly the /// files it declares, so a manifest that lands before its siblings names /// objects that are not there yet. Writing it last means a partially written /// build has no manifest at all, which every reader treats as "not present" /// rather than as a build it can act on. /// /// This is the set both the builder's upload and the deploy-phase /// template-publish copy work from, so gaining a fourth artifact is one /// edit rather than three. pub const PUBLISHED: [Self; 3] = [Self::Metadata, Self::RootfsZstd, Self::Manifest]; /// Returns the file name component as it appears under the build directory. pub const fn file_name(self) -> &'static str { match self { Self::RootfsZstd => "rootfs.ext4.zst", Self::Metadata => "metadata.json", Self::Manifest => "manifest.json", } } } /// Returns the object-store key for one file of a template build. /// /// The layout mirrors the on-node cache: /// `/templates/