-
-
Notifications
You must be signed in to change notification settings - Fork 127
perf(send): memoize the per-group device list behind a topology generation #824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
891ea38
perf(send): memoize the per-group device list behind a topology gener…
jlucaso1 eac657c
perf(send): enforce topology recording by construction and scope memo…
jlucaso1 f3abd7e
fix(send): record every lookup alias on device-list updates
jlucaso1 756178c
fix(send): harden the group-devices memo from adversarial review find…
jlucaso1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| //! Device-topology change tracking for the per-group device-list memo. | ||
| //! | ||
| //! "Topology" here means anything that can change a device-list answer: | ||
| //! registry record writes/invalidations and LID-PN mapping changes. Instead of | ||
| //! trusting every write path to remember a manual generation bump, the bump | ||
| //! lives INSIDE the write chokepoints ([`DeviceRegistryCache`] and | ||
| //! `LidPnCache::add`), so a writer cannot forget it by construction. | ||
| //! | ||
| //! Each change also logs WHICH canonical users it touched (both namespaces), | ||
| //! so a memo whose generation went stale can prove "none of the changed users | ||
| //! are in my group" and re-stamp itself instead of recomputing. Every doubtful | ||
| //! case (log overflow, global events) degrades to a recompute, never to | ||
| //! serving stale data. | ||
|
|
||
| use std::collections::VecDeque; | ||
| use std::sync::Arc; | ||
| use std::sync::atomic::Ordering; | ||
|
|
||
| use portable_atomic::AtomicU64; | ||
| use wacore_binary::CompactString; | ||
|
|
||
| /// Bounded log capacity. Sized so a burst (e.g. a usync response for a large | ||
| /// group) still fits; overflow just disables the scoped-revalidation fast | ||
| /// path until affected memos recompute once. | ||
| const TOPOLOGY_LOG_CAPACITY: usize = 256; | ||
|
|
||
| struct TopologyLog { | ||
| /// (generation that the change produced, canonical user touched). | ||
| entries: VecDeque<(u64, CompactString)>, | ||
| /// Highest generation evicted from `entries` (0 = nothing evicted). | ||
| /// A memo older than this cannot be proven clean and must recompute. | ||
| floor: u64, | ||
| } | ||
|
|
||
| /// Shared tracker: a monotonic generation plus the bounded changed-users log. | ||
| pub(crate) struct DeviceTopology { | ||
| generation: AtomicU64, | ||
| log: std::sync::Mutex<TopologyLog>, | ||
| } | ||
|
|
||
| impl DeviceTopology { | ||
| pub(crate) fn new() -> Arc<Self> { | ||
| Arc::new(Self { | ||
| generation: AtomicU64::new(0), | ||
| log: std::sync::Mutex::new(TopologyLog { | ||
| entries: VecDeque::with_capacity(TOPOLOGY_LOG_CAPACITY), | ||
| floor: 0, | ||
| }), | ||
| }) | ||
| } | ||
|
|
||
| pub(crate) fn current(&self) -> u64 { | ||
| self.generation.load(Ordering::Acquire) | ||
| } | ||
|
|
||
| /// Record one topology change touching the given users (pass BOTH | ||
| /// namespaces of an identity when known: a mapping change alters which | ||
| /// canonical record either key resolves to). | ||
| pub(crate) fn record<'a>(&self, users: impl IntoIterator<Item = &'a str>) { | ||
| let mut log = self.log.lock().unwrap_or_else(|p| p.into_inner()); | ||
| let generation = self.generation.load(Ordering::Acquire) + 1; | ||
| for user in users { | ||
| if log.entries.len() == TOPOLOGY_LOG_CAPACITY | ||
| && let Some((evicted_gen, _)) = log.entries.pop_front() | ||
| { | ||
| log.floor = evicted_gen; | ||
| } | ||
| log.entries | ||
| .push_back((generation, CompactString::from(user))); | ||
| } | ||
| // Publish the generation only after the log holds the users, so a | ||
| // reader that observes the new generation can always find (or rule | ||
| // out) the corresponding entries. | ||
| self.generation.store(generation, Ordering::Release); | ||
| } | ||
|
|
||
| /// Record a change whose blast radius is unknown (bulk warm-up, cache | ||
| /// clear): bumps and poisons the scoped fast path so every memo | ||
| /// recomputes once. | ||
| pub(crate) fn record_global(&self) { | ||
| let mut log = self.log.lock().unwrap_or_else(|p| p.into_inner()); | ||
| let generation = self.generation.load(Ordering::Acquire) + 1; | ||
| log.entries.clear(); | ||
| log.floor = generation; | ||
| self.generation.store(generation, Ordering::Release); | ||
| } | ||
|
|
||
| /// Whether every change after `since` only touched users for which | ||
| /// `is_member` returns false. `false` on any doubt (log overflow past | ||
| /// `since`), so callers recompute. | ||
| pub(crate) fn unchanged_for(&self, since: u64, is_member: impl Fn(&str) -> bool) -> bool { | ||
| let log = self.log.lock().unwrap_or_else(|p| p.into_inner()); | ||
| if log.floor > since { | ||
| return false; | ||
| } | ||
| log.entries | ||
| .iter() | ||
| .filter(|(generation, _)| *generation > since) | ||
| .all(|(_, user)| !is_member(user)) | ||
| } | ||
| } | ||
|
|
||
| /// The device registry cache plus its topology tracker, fused so every write | ||
| /// records the change. Reads are pass-through; the only write entry points | ||
| /// are [`insert`](Self::insert), [`invalidate`](Self::invalidate) and the | ||
| /// non-recording [`promote`](Self::promote) (whose data is by definition what | ||
| /// the DB fallback already answered). | ||
| pub(crate) struct DeviceRegistryCache { | ||
| cache: crate::cache_store::TypedCache<String, Arc<wacore::store::traits::DeviceListRecord>>, | ||
| topology: Arc<DeviceTopology>, | ||
| } | ||
|
|
||
| impl DeviceRegistryCache { | ||
| pub(crate) fn new( | ||
| cache: crate::cache_store::TypedCache<String, Arc<wacore::store::traits::DeviceListRecord>>, | ||
| topology: Arc<DeviceTopology>, | ||
| ) -> Self { | ||
| Self { cache, topology } | ||
| } | ||
|
|
||
| pub(crate) async fn get( | ||
| &self, | ||
| key: &str, | ||
| ) -> Option<Arc<wacore::store::traits::DeviceListRecord>> { | ||
| self.cache.get(key).await | ||
| } | ||
|
|
||
| /// Write a record and log the touched users. `touched` carries the keys | ||
| /// whose answers change (canonical key, plus the original alias when the | ||
| /// canonical flipped). | ||
| pub(crate) async fn insert<'a>( | ||
| &self, | ||
| key: String, | ||
| record: Arc<wacore::store::traits::DeviceListRecord>, | ||
| touched: impl IntoIterator<Item = &'a str>, | ||
| ) { | ||
| self.cache.insert(key, record).await; | ||
| self.topology.record(touched); | ||
| } | ||
|
|
||
| pub(crate) async fn invalidate(&self, key: &str) { | ||
| self.cache.invalidate(key).await; | ||
| self.topology.record([key]); | ||
| } | ||
|
|
||
| /// Cache-fill from the DB row the fallback path would have returned: the | ||
| /// answer is unchanged, so no topology change is recorded. | ||
| pub(crate) async fn promote( | ||
| &self, | ||
| key: String, | ||
| record: Arc<wacore::store::traits::DeviceListRecord>, | ||
| ) { | ||
| self.cache.insert(key, record).await; | ||
| } | ||
|
|
||
| #[cfg(feature = "debug-diagnostics")] | ||
| pub(crate) fn entry_count(&self) -> u64 { | ||
| self.cache.entry_count() | ||
| } | ||
|
|
||
| /// Test-only passthrough for moka maintenance flushes. | ||
| #[cfg(test)] | ||
| pub(crate) async fn run_pending_tasks(&self) { | ||
| self.cache.run_pending_tasks().await; | ||
| } | ||
|
|
||
| /// Test-only raw write that bypasses topology recording, for fixture | ||
| /// seeding and for proving that memo hits really are hits (a raw change | ||
| /// must be served stale). | ||
| #[cfg(test)] | ||
| pub(crate) async fn raw_insert_for_tests( | ||
| &self, | ||
| key: String, | ||
| record: Arc<wacore::store::traits::DeviceListRecord>, | ||
| ) { | ||
| self.cache.insert(key, record).await; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.