Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions pkgs/id/crates/id-core/src/waiting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
//! use a fake clock. The event store holds one pool and mirrors it into the
//! `waiting` table (P1.11). [`WaitingPool::restore`] rebuilds the pool from those
//! rows on open. The pool keeps its own set of arrived keys, for the filter in
//! [`WaitingPool::insert`].
//! [`WaitingPool::insert`]. The store does not feed that set: it releases
//! waiters through [`WaitingPool::release`], which records nothing, and its
//! own applied table is its duplicate and replay record — so the set cannot
//! grow with every event the store ever applies (#1730).
//!
//! The caller's loop:
//!
Expand Down Expand Up @@ -164,7 +167,10 @@ pub struct WaitingPool<K, E> {
/// entry.
held: HashMap<AuthorKey, BTreeSet<u64>>,
/// Keys that have arrived, and stay arrived. A dependency in this set is already satisfied.
/// The store calls this present: it is applied, voided by a fork, or a fork proof header.
/// Only the standalone [`insert`](Self::insert)/[`park`](Self::park) protocol
/// records here, through [`arrived`](Self::arrived). The event store never
/// does: it releases through [`release`](Self::release) and tracks applied
/// keys in its own table, so this set stays empty on the store path (#1730).
arrived: HashSet<K>,
/// Events whose dependencies had all arrived when they were inserted,
/// paired with their key so an arrival can drop the copy (#4730).
Expand Down Expand Up @@ -422,6 +428,23 @@ where
/// is dropped without being returned, because the caller now holds the event;
/// a copy still in the ready queue is dropped the same way (#4730).
pub fn arrived(&mut self, key: K, now_ms: u64) -> Vec<E> {
let released = self.release(key.clone(), now_ms);
self.arrived.insert(key);
released
}

/// Release the events that were waiting only for `key`, exactly as
/// [`arrived`](Self::arrived) does — the held copy of `key` is dropped and
/// events due at `now_ms` expire first — but record nothing: `key` is not
/// added to the arrived set, so a later [`insert`](Self::insert) does not
/// treat it as a satisfied dependency or as a duplicate.
///
/// This is the event store's path (#1730). The store calls it for every
/// applied event and every fork proof, and its own applied table already
/// recognises duplicates and replays; recording each key here as well
/// grew this pool by one set entry per event ever applied, in a pool no
/// store caller ever read back.
pub fn release(&mut self, key: K, now_ms: u64) -> Vec<E> {
self.expire_due(now_ms);
if let Some(seq) = self.by_key.get(&key).copied() {
self.remove(seq);
Expand All @@ -445,7 +468,6 @@ where
released.push(done.event);
}
}
self.arrived.insert(key);
released
}

Expand Down Expand Up @@ -948,4 +970,29 @@ mod tests {
.unwrap();
assert_eq!(pool.waiting_count(author(1)), 1);
}

#[test]
fn test_release_releases_waiters_without_recording_the_key() {
let mut pool: WaitingPool<u32, &str> = WaitingPool::new(WaitingLimits::default());
let author = [7u8; 32];
pool.insert(author, 10, [20], "a", 0).unwrap();
// release frees the waiter exactly as arrived does...
assert_eq!(pool.release(20, 0), vec!["a"]);
// ...but the key was not recorded: an event depending on it still
// waits, and the key itself is not treated as a duplicate (#1730).
pool.insert(author, 11, [20], "b", 0).unwrap();
assert!(pool.take_ready().is_empty());
assert_eq!(pool.len(), 1);
pool.insert(author, 20, [], "c", 0).unwrap();
assert_eq!(pool.take_ready(), vec!["c"]);
}

#[test]
fn test_release_drops_the_held_copy_like_arrived() {
let mut pool: WaitingPool<u32, &str> = WaitingPool::new(WaitingLimits::default());
let author = [7u8; 32];
pool.insert(author, 10, [20], "a", 0).unwrap();
assert!(pool.release(10, 0).is_empty());
assert!(pool.is_empty());
}
}
28 changes: 18 additions & 10 deletions pkgs/id/src/event_store/ingest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3108,10 +3108,18 @@ mod tests {
}

#[test]
fn test_a_hold_of_an_arrived_key_is_skipped_and_leaves_no_row() {
// #3911: the ordinary hold path refuses a key the pool has already
// arrived, without a duplicate release. The pool does not hold it, and
// no row survives the transaction.
fn test_a_hold_of_a_released_key_is_held_again_release_only_contract() {
// #3911 originally pinned this path the other way: a hold of a key
// the pool had arrived was skipped, because the store's release
// recorded every key in the pool's arrived set. #1730 removed that
// recording (release-only store path, PR #3752): the set grew by
// one entry per event ever applied. Duplicate and replay
// recognition for an already-applied event lives in the store's
// applied table, above this Mirror — so at the Mirror level a
// hold after a release is a fresh hold, and its row is the hold's
// own. (#3911's restore guards still refuse keys the pool's
// standalone insert/park protocol recorded as arrived; the store
// path no longer records any.)
let (_dir, mut store) = open();
let signer = key(60);
let events = chain_of(&signer, 2);
Expand All @@ -3125,9 +3133,9 @@ mod tests {
})
.unwrap();

// Simulate a durable stale row for the key after it has arrived. The
// skipped hold must remove that row in its own transaction; release
// itself already deleted the original row above.
// A durable row for the key exists again (written directly, the
// way a stale row would appear). The fresh hold takes ownership
// of it: exactly one row survives, and it is the hold's.
let stale = Row {
id: events[1].id,
author: author(&signer),
Expand All @@ -3144,9 +3152,9 @@ mod tests {
Ok((again, true))
})
.unwrap();
assert_eq!(again, waiting_rows::Hold::Skipped);
assert!(!store.is_waiting(&events[1].id));
assert!(store.waiting_ids().unwrap().is_empty());
assert_eq!(again, waiting_rows::Hold::Held);
assert!(store.is_waiting(&events[1].id));
assert_eq!(store.waiting_ids().unwrap().len(), 1);
store.check_invariants().unwrap();
}

Expand Down
12 changes: 6 additions & 6 deletions pkgs/id/src/event_store/waiting_rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use p2panda_core::{Hash, SeqNum};
use redb::{ReadableMultimapTable, ReadableTable, WriteTransaction};

use super::{StoreError, WAITING, WAITING_BY_DEP, be_u64, corrupt, hash_of, row_event, seq_num};
use super::{be_u64, corrupt, hash_of, row_event, seq_num, StoreError, WAITING, WAITING_BY_DEP};
use crate::chain::Event;
use crate::waiting::{AuthorKey, WaitRefusal, WaitingPool};

Expand Down Expand Up @@ -193,7 +193,7 @@ impl Mirror<'_> {
}

/// Deletes the rows of the events the pool has expired into its buffer, and returns
/// how many. `pool.arrived` expires due events as a side effect of a release, into
/// how many. `pool.release` expires due events as a side effect of a release, into
/// the buffer only `pool.expire` drains; a caller that releases without expiring
/// first would commit with the pool and the rows disagreeing -- the expired events
/// gone from the pool, their rows still on disk, and their expiry left for a later,
Expand Down Expand Up @@ -273,8 +273,8 @@ impl Mirror<'_> {
/// that dependency. Returns the released events in arrival order. Their rows stay until the
/// caller decides each one.
pub fn release(&mut self, id: &Hash, now: u64) -> Result<Vec<Event>, StoreError> {
let released = self.pool.arrived(*id, now);
// `arrived` expired every event due at `now` into the pool's buffer; their
let released = self.pool.release(*id, now);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Update the existing mirror test for release-only semantics

The existing test_a_hold_of_an_arrived_key_is_skipped_and_leaves_no_row in pkgs/id/src/event_store/ingest.rs calls Mirror::release without storing the event, then holds the same key again and asserts Hold::Skipped at line 3147. With this change, neither the pool nor the durable store remembers that key, so the second hold returns Hold::Held and the assertion fails. Update that regression test to exercise the intended release-only contract; otherwise the existing library test suite remains broken.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — and thank you, the name-filtered waiting run I did first missed exactly this. The history matters: this PR's fix is from Nov 1, and test_a_hold_of_an_arrived_key_is_skipped_and_leaves_no_row landed Nov 2 in ea6d79a (#3911), pinning at the Mirror level a contract that rested on the store recording every released key in the pool's arrived set — the growth #1730 removes.

Updated the test to the release-only contract (test_a_hold_of_a_released_key_is_held_again_release_only_contract): a Mirror-level hold after a release is a fresh hold whose single row is its own, with the layering stated in the comment — duplicate/replay recognition for an already-applied event lives in the store's applied table above the Mirror (that is #1730's acceptance wording), while #3911's restore guards still refuse keys recorded through the pool's standalone insert/park protocol. Full cargo test -p id -p id-core is running to catch any other test resting on the old recording; results follow on the PR before merge.

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// `release` expired every event due at `now` into the pool's buffer; their
// rows go in this transaction too, not a later one's.
self.flush_expired(now)?;
delete(self.txn, id)?;
Expand All @@ -288,8 +288,8 @@ impl Mirror<'_> {
/// seen. Returns the released events in arrival order.
pub fn present_proof(&mut self, id: &Hash, now: u64) -> Result<Vec<Event>, StoreError> {
let held = get(self.txn, id)?;
let released = self.pool.arrived(*id, now);
// As in `release`: the events `arrived` expired lose their rows here. The
let released = self.pool.release(*id, now);
// As in `release`: the events `release` expired lose their rows here. The
// flush runs before the park below, so a held proof that was itself due is
// re-held for good by the park rather than deleted by the flush.
self.flush_expired(now)?;
Expand Down
27 changes: 27 additions & 0 deletions pkgs/id/tests/waiting_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,30 @@ proptest! {
prop_assert_eq!(run(&reordered, max_age_ms)?, drawn);
}
}

/// The event store's path (#1730): holds go through `restore`, releases
/// through `release`. Releasing any number of applied keys must leave the
/// pool's retained state exactly as it was — the pool used to record every
/// released key in its arrived set forever (+37 MB per 2M arrivals in a
/// pool reporting itself empty). Held events aside, pool state after N
/// releases is independent of N.
#[test]
fn store_path_releases_do_not_grow_the_pool() {
let mut pool: WaitingPool<u32, u32> = WaitingPool::new(WaitingLimits::default());
// One held event waiting on key 9, restored the way the store holds.
pool.restore(author_key(1), 1_000_000, [9], 1, 0).unwrap();
assert_eq!(pool.len(), 1);
// A long stream of applied events, none of them key 9.
for key in (0..10_000u32).filter(|key| *key != 9) {
assert!(pool.release(key, 0).is_empty());
}
assert_eq!(pool.len(), 1, "releases retained state in the pool");
// The one real dependency still releases the held event...
assert_eq!(pool.release(9, 0), vec![1]);
assert!(pool.is_empty());
// ...and none of the released keys was recorded as arrived: a later
// insert depending on one still waits for it.
pool.insert(author_key(1), 1_000_001, [5], 2, 0).unwrap();
assert!(pool.take_ready().is_empty());
assert_eq!(pool.len(), 1);
}
Loading