Skip to content
Open
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
10 changes: 9 additions & 1 deletion pkgs/id/crates/id-worlds/src/world/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -989,13 +989,21 @@ impl WorldActor {
);
}

// The handoff's journal position is the ownership boundary
// (#13711): stamp the next position on the entry and on the
// lease history in the same step, before the durable append.
// If the append fails, `next_lease` is dropped and the
// counter — derived from the history — never advanced, so
// the retry reuses the same position.
let position = self.core.next_lease_position();
let mut next_lease = self.core.write_lease().clone();
next_lease.apply_handoff(&from, &to, epoch, expires_at_ms)?;
next_lease.apply_handoff(&from, &to, epoch, expires_at_ms, position)?;
self.persist(JournalEntry::LeaseHandedOff {
from,
to,
epoch,
expires_at_ms,
position,
})?;
self.core.write_lease = next_lease;
self.core.since_checkpoint = self.core.since_checkpoint.saturating_add(1);
Expand Down
14 changes: 10 additions & 4 deletions pkgs/id/crates/id-worlds/src/world/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,37 +1228,41 @@ impl WorldCore {
holder,
epoch,
expires_at_ms,
position,
} => {
core.write_lease
.apply_grant(&holder, epoch, expires_at_ms)?;
.apply_grant(&holder, epoch, expires_at_ms, position)?;
core.since_checkpoint = core.since_checkpoint.saturating_add(1);
}
JournalEntry::LeaseRenewed {
holder,
epoch,
expires_at_ms,
position,
} => {
core.write_lease
.apply_renew(&holder, epoch, expires_at_ms)?;
.apply_renew(&holder, epoch, expires_at_ms, position)?;
core.since_checkpoint = core.since_checkpoint.saturating_add(1);
}
JournalEntry::LeaseHandedOff {
from,
to,
epoch,
expires_at_ms,
position,
} => {
core.write_lease
.apply_handoff(&from, &to, epoch, expires_at_ms)?;
.apply_handoff(&from, &to, epoch, expires_at_ms, position)?;
core.since_checkpoint = core.since_checkpoint.saturating_add(1);
}
JournalEntry::LeaseRevoked {
former_holder,
epoch,
reason,
position,
} => {
core.write_lease
.apply_revoke(&former_holder, epoch, &reason)?;
.apply_revoke(&former_holder, epoch, &reason, position)?;
core.since_checkpoint = core.since_checkpoint.saturating_add(1);
}
JournalEntry::LeaseWriteRejected {
Expand All @@ -1268,6 +1272,7 @@ impl WorldCore {
observed_epoch,
reason,
received_at_ms,
position,
} => {
core.write_lease.apply_rejection(LeaseWriteRejection {
writer,
Expand All @@ -1276,6 +1281,7 @@ impl WorldCore {
observed_epoch,
reason,
received_at_ms,
position,
})?;
core.since_checkpoint = core.since_checkpoint.saturating_add(1);
}
Expand Down
23 changes: 23 additions & 0 deletions pkgs/id/crates/id-worlds/src/world/core/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,29 @@ impl WorldCore {
&self.write_lease
}

/// The replay watermark (#13711): the highest stamped lease
/// position this world has replayed — `0` when no lease record
/// in the journal carries a position. A successor that reports
/// this value has replayed through the grant at that position;
/// the failover barrier (#7572) admits writes only once the
/// watermark reaches the grant's position. History is
/// append-only, so the watermark never decreases within a
/// journal, and a checkpoint carries the whole history, so it
/// survives compaction.
#[must_use]
pub(crate) fn lease_position_watermark(&self) -> u64 {
self.write_lease.last_position()
}

/// The position the next live lease record must carry (#13711):
/// one past the watermark. The append authority stamps it in
/// the same serialized operation as the append; callers never
/// choose a position.
#[must_use]
pub(crate) fn next_lease_position(&self) -> u64 {
self.write_lease.last_position().saturating_add(1)
}

/// The per-world resource requests, independent of current server ceilings.
/// The world's total-memory budget ledger (#557): the effective
/// limit, the logical charge measured at restore, its high-water
Expand Down
66 changes: 58 additions & 8 deletions pkgs/id/crates/id-worlds/src/world/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,12 @@ pub enum JournalEntry {
/// authority's clock; absent for an untermed holder.
#[serde(default, skip_serializing_if = "Option::is_none")]
expires_at_ms: Option<u64>,
/// The entry's durable per-world journal position (#13711):
/// strictly increasing across lease entries in journal order,
/// assigned by the append authority. `0` means unstamped: a
/// lease entry written before positions existed.
#[serde(default)]
position: u64,
},
/// The current holder's lease term was renewed by the authority
/// (design #2819). The epoch is unchanged.
Expand All @@ -545,6 +551,10 @@ pub enum JournalEntry {
epoch: u64,
/// The new term expiry, in unix milliseconds.
expires_at_ms: u64,
/// The entry's durable per-world journal position (#13711);
/// `0` means unstamped (pre-positions entry).
#[serde(default)]
position: u64,
},
/// The write lease was handed from one holder to another
/// (design #2819). This entry's journal position is the ownership
Expand All @@ -561,6 +571,10 @@ pub enum JournalEntry {
/// absent for an untermed holder.
#[serde(default, skip_serializing_if = "Option::is_none")]
expires_at_ms: Option<u64>,
/// The entry's durable per-world journal position (#13711);
/// `0` means unstamped (pre-positions entry).
#[serde(default)]
position: u64,
},
/// The write lease was revoked by the authority (design #2819):
/// the former holder is fenced at the resulting epoch and no
Expand All @@ -572,6 +586,10 @@ pub enum JournalEntry {
epoch: u64,
/// Why the lease was revoked.
reason: String,
/// The entry's durable per-world journal position (#13711);
/// `0` means unstamped (pre-positions entry).
#[serde(default)]
position: u64,
},
/// An authenticated append attempt was refused by the append gate
/// and the authority journaled the attempt as an audit record
Expand All @@ -591,6 +609,10 @@ pub enum JournalEntry {
/// When the authority received the attempt, in unix
/// milliseconds on the authority's clock.
received_at_ms: u64,
/// The entry's durable per-world journal position (#13711);
/// `0` means unstamped (pre-positions entry).
#[serde(default)]
position: u64,
},
/// One UTF-8-safe part of a compacted program snapshot. The complete set
/// immediately precedes its checkpoint and is keyed by that checkpoint's
Expand Down Expand Up @@ -862,17 +884,30 @@ pub const JOURNAL_VERSION_BLOB: u32 = 14;
/// containing one must be refused at the header check instead.
pub const JOURNAL_VERSION_NOTIFY: u32 = 15;

/// Journal version 16 (#13711): a lease entry
/// ([`JournalEntry::LeaseGranted`] and its siblings) carrying a
/// stamped journal `position`, or a checkpoint carrying a lease
/// history with stamped positions. The field is optional, so a
/// version-15 binary would parse such an entry yet drop the
/// position at its next checkpoint — silently unmooring the
/// replay barrier the position names — so a journal containing a
/// stamped position must be refused at the header check instead.
/// Unstamped (position 0) lease entries keep version 7.
pub const JOURNAL_VERSION_LEASE_POSITION: u32 = 16;

/// Newest journal version this binary can read. Journals with a higher
/// header version are refused at the header check, before any entry is
/// applied.
pub const MAX_JOURNAL_VERSION: u32 = JOURNAL_VERSION_NOTIFY;
pub const MAX_JOURNAL_VERSION: u32 = JOURNAL_VERSION_LEASE_POSITION;

/// The header version a journal must carry once it contains `entry`: version 2
/// for execution-profile entries, version 3 for region-handoff entries,
/// version 4 for entries persisting a per-world total memory budget, version 5
/// for checkpoints with open-presence metadata, and version 6 for checkpoint
/// part entries. Checkpoints combine the requirements of their transfers,
/// retained events, profile and presence field.
/// retained events, profile and presence field. A lease entry carrying a
/// stamped journal position requires version 16, as does a checkpoint whose
/// lease history carries one; unstamped lease entries keep version 7.
///
/// Any entry that records a program-bundle install additionally requires
/// [`JOURNAL_VERSION_PROGRAM_BUNDLE`].
Expand Down Expand Up @@ -940,11 +975,17 @@ fn entry_kind_version(entry: &JournalEntry) -> u32 {
JournalEntry::EntityExported { .. } | JournalEntry::EntityImported { .. } => {
JOURNAL_VERSION_REGION_HANDOFF
}
JournalEntry::LeaseGranted { .. }
| JournalEntry::LeaseRenewed { .. }
| JournalEntry::LeaseHandedOff { .. }
| JournalEntry::LeaseRevoked { .. }
| JournalEntry::LeaseWriteRejected { .. } => JOURNAL_VERSION_WRITE_LEASE,
JournalEntry::LeaseGranted { position, .. }
| JournalEntry::LeaseRenewed { position, .. }
| JournalEntry::LeaseHandedOff { position, .. }
| JournalEntry::LeaseRevoked { position, .. }
| JournalEntry::LeaseWriteRejected { position, .. } => {
if *position == 0 {
JOURNAL_VERSION_WRITE_LEASE
} else {
JOURNAL_VERSION_LEASE_POSITION
}
}
JournalEntry::AuthorKeyBound { .. } | JournalEntry::SignedMoveAccepted { .. } => {
JOURNAL_VERSION_SIGNED_PLAY
}
Expand Down Expand Up @@ -987,11 +1028,20 @@ fn entry_kind_version(entry: &JournalEntry) -> u32 {
admin_history,
..
} => {
// A checkpoint with a stamped lease history requires
// version 16, above every other content version: the
// check is ordered highest-first, so this arm comes
// before the notify/blob/kv arms below.
let base = if lease
.as_ref()
.is_some_and(|lease| lease.last_position() > 0)
{
JOURNAL_VERSION_LEASE_POSITION
// A retained `Notification` event is a version-15 kind
// wherever the checkpoint carries it — the retained
// events or the admin history (the both-places check
// the bundle-provenance rule uses).
let base = if events
} else if events
.iter()
.any(|event| matches!(event.kind, WorldEventKind::Notification { .. }))
|| admin_history.as_ref().is_some_and(|history| {
Expand Down
Loading