Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/aien-kv-store/src/holder.rs # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / aien-kv-store / src / holder.rs` 111 lines ยท 3683 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //! Holder generation allocation (ADR 0003 section 5.2). 17 //! 18 //! A chunk holder is a real block table in the cache manager under the 19 //! reserved sequence slot `u32::MAX`, with a nonzero 32-bit generation. The 20 //! first draft derived the generation as `job_id as u32`. That wraps silently 21 //! after 2^32 jobs, aliases job `2^32 + n` onto job `n`, and maps job `2^32` 22 //! onto the reserved zero. Two live holders could then share one id and one 23 //! would free the other's blocks. 24 //! 25 //! Two rules replace it: 26 //! 27 //! * [`HolderGenerations`] hands out a nonzero generation that no live holder 28 //! uses, skipping live values when the counter wraps, and refuses 29 //! (`Exhausted`) instead of reusing a live one. 30 //! * [`generation_for_job`] is the checked form of the old mapping for callers 31 //! that must derive it from a `JobId`: it refuses any id that does not fit. 32 33 use crate::store::JobId; 34 use alloc::collections::BTreeSet; 35 36 #[derive(Debug, Clone, Copy, PartialEq, Eq)] 37 pub enum HolderError { 38 /// Every generation is held by a live holder. 39 Exhausted, 40 /// The job id is zero or does not fit in 32 bits. 41 JobIdOutOfRange, 42 } 43 44 /// The checked job-id mapping: `Ok` only when the plain cast would be exact 45 /// and nonzero. 46 pub fn generation_for_job(job: JobId) -> Result { 47 match u32::try_from(job.0) { 48 Ok(g) if g != 0 => Ok(g), 49 _ => Err(HolderError::JobIdOutOfRange), 50 } 51 } 52 53 /// Collision-safe generation allocator. Not thread-safe by itself; it lives 54 /// with the cache owner that already serialises holder creation. 55 #[derive(Debug, Clone)] 56 pub struct HolderGenerations { 57 next: u32, 58 max: u32, 59 live: BTreeSet, 60 } 61 62 impl Default for HolderGenerations { 63 fn default() -> Self { 64 Self::new() 65 } 66 } 67 68 impl HolderGenerations { 69 /// Generations `1..=u32::MAX`. 70 pub fn new() -> Self { 71 Self::with_max(u32::MAX) 72 } 73 74 /// Generations `1..=max` (tests use a small range to reach the wrap). 75 pub fn with_max(max: u32) -> Self { 76 Self::starting_at(1, max) 77 } 78 79 /// As `with_max`, with the counter starting at `first` (clamped into range). 80 pub fn starting_at(first: u32, max: u32) -> Self { 81 let max = max.max(1); 82 Self { 83 next: first.clamp(1, max), 84 max, 85 live: BTreeSet::new(), 86 } 87 } 88 89 pub fn live(&self) -> usize { 90 self.live.len() 91 } 92 93 pub fn alloc(&mut self) -> Result { 94 if self.live.len() as u64 >= self.max as u64 { 95 return Err(HolderError::Exhausted); 96 } 97 // At least one value is free, so this loop ends within `live + 1` steps. 98 loop { 99 let g = self.next; 100 self.next = if g >= self.max { 1 } else { g + 1 }; 101 if self.live.insert(g) { 102 return Ok(g); 103 } 104 } 105 } 106 107 /// Returns false if `generation` was not live. 108 pub fn release(&mut self, generation: u32) -> bool { 109 self.live.remove(&generation) 110 } 111 }