crates/edge/sdk/src/cache.rs 3.0 KiB 106 lines Raw
1 //! The plugin side of the Wire 2 cache.2 //!3 //! A codec knows one engine's slot format. It does not know a tier, a quota, a4 //! TTL, or a directory: it asks the core for a descriptor and writes or reads5 //! that descriptor. The SDK owns the request/reply plumbing, so a codec never6 //! reads a frame.7 8 use std::io;9 use std::os::fd::OwnedFd;10 use std::sync::Arc;11 use std::time::Duration;12 13 mod chain;14 mod descriptor;15 mod driver;16 17 pub use chain::{18     AnchorSet, Chain, TABLE_LIMIT, TipTable, anchor_key, cache_key, canonical_parts, chain_keys,19     hash_into, key_after, lineage_key, part_kind_name, seed, template_seed,20 };21 pub use descriptor::{copy_in, copy_out, measure_write, verify_file};22 pub use driver::{CacheDriver, CacheSlot, PrefixOutcome, PrefixReply};23 24 use inf3r_proto::body;25 26 /// A restored cache file. The codec reads it and then releases the lease.27 pub struct CacheFile {28     pub key: [u8; 32],29     pub lease_id: u64,30     pub bytes: u64,31     pub crc32c: u32,32     pub token_count: u64,33     pub tier: String,34     pub file: OwnedFd,35 }36 37 /// A writable temp file. The codec writes it and then commits the lease.38 pub struct CacheWrite {39     pub lease_id: u64,40     pub max_bytes: u64,41     pub tier: String,42     pub file: OwnedFd,43 }44 45 /// The fields of one reservation.46 #[derive(Clone, PartialEq, Eq, Debug)]47 pub struct Reserve {48     pub key: [u8; 32],49     pub lineage: Option<[u8; 32]>,50     pub supersedes: Option<[u8; 32]>,51     pub kind: body::CacheEntryKind,52     pub est_bytes: u64,53     pub prefill_ms: u32,54     pub token_count: u64,55     pub pin: bool,56 }57 58 /// What a flush saved.59 #[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]60 pub struct FlushReport {61     pub saved: u32,62     pub skipped: u32,63     pub dirty_remaining: u32,64 }65 66 /// The core end of one cache session, as the codec sees it.67 pub trait CacheTransport: Send + Sync {68     fn lookup(69         &self,70         keys: &[[u8; 32]],71         lineage: Option<[u8; 32]>,72         timeout: Duration,73     ) -> io::Result<Option<CacheFile>>;74 75     fn reserve(&self, request: Reserve, timeout: Duration) -> io::Result<Option<CacheWrite>>;76 77     fn release(&self, lease_id: u64, ok: bool, restore_ms: u32) -> io::Result<()>;78 79     fn commit(80         &self,81         lease_id: u64,82         ok: bool,83         bytes: u64,84         crc32c: u32,85         token_count: u64,86         save_ms: u32,87     ) -> io::Result<()>;88 }89 90 /// The engine-specific half. One per plugin process.91 pub trait CacheCodec: Send + Sync {92     /// The static declaration that `catalogue` carries.93     fn spec(&self) -> body::CacheSpec;94 95     /// The bundle half of the composite fingerprint, reported after `grant`.96     fn fingerprint(&self) -> [u8; 32];97 98     /// Called once before the first job. The codec keeps this handle.99     fn attach(&self, transport: Arc<dyn CacheTransport>);100 101     /// Saves every dirty slot inside the budget. It may use the transport.102     fn flush(&self, budget: Duration) -> FlushReport;103 104     /// Erases one VRAM slot. This is correctness, not cache policy.105     fn erase(&self, slot: u32) -> io::Result<()>;106 }