| 1 | // Copyright 2026 Deno Land Inc. Apache-2.0 license. |
| 2 | |
| 3 | //! The vocabulary the core is written in: what it is told, what it |
| 4 | //! answers with, and the records it reads and writes. |
| 5 | //! |
| 6 | //! Everything here is data. `Event` is the only way in, `Effect` the only |
| 7 | //! way out, and the rest are the shapes those two carry. No decision is |
| 8 | //! made in this file — that is `State` in the crate root. |
| 9 | use super::*; |
| 10 | |
| 11 | pub type Ms = i64; |
| 12 | |
| 13 | pub type CellId = String; |
| 14 | pub type NodeId = String; |
| 15 | pub type RequestId = u64; |
| 16 | pub type WebSocketId = u64; |
| 17 | pub type OpId = u64; |
| 18 | pub type Epoch = u64; |
| 19 | |
| 20 | /// One runtime that this node can currently serve locally. |
| 21 | /// |
| 22 | /// Presence is a read-only projection of the decision core, not a second |
| 23 | /// inventory maintained by an adapter. Keeping the epoch beside the cell ID |
| 24 | /// lets management and inspection traffic identify the exact fenced runtime. |
| 25 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 26 | pub struct PresenceCell { |
| 27 | pub id: CellId, |
| 28 | pub epoch: Epoch, |
| 29 | } |
| 30 | |
| 31 | /// Cumulative lifecycle decisions exposed to management from the same state |
| 32 | /// machine that made them. These are advisory counters, but they are not a |
| 33 | /// second shell-owned model and replay to the same values for the same events. |
| 34 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 35 | pub struct ActivitySnapshot { |
| 36 | pub acquired: u64, |
| 37 | pub proxied: u64, |
| 38 | pub expired_owner_leases: u64, |
| 39 | pub restored: u64, |
| 40 | pub advanced_epochs: u64, |
| 41 | } |
| 42 | |
| 43 | /// Management-facing lifecycle state derived atomically from [`State`]. |
| 44 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 45 | pub struct PresenceSnapshot { |
| 46 | pub serving: bool, |
| 47 | pub cells: Vec<PresenceCell>, |
| 48 | pub activity: ActivitySnapshot, |
| 49 | } |
| 50 | |
| 51 | impl PresenceSnapshot { |
| 52 | pub fn owned_cells(&self) -> usize { |
| 53 | self.cells.len() |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 58 | pub struct Config { |
| 59 | /// Resident cells plus activation reservations may never exceed this. |
| 60 | pub max_resident: usize, |
| 61 | /// Complete nonresident routes which may be in flight at once. |
| 62 | /// |
| 63 | /// This is deliberately independent of the stateless Worker pool. A warm |
| 64 | /// request consumes no activation slot, while a cold request holds one |
| 65 | /// across ownership resolution, capacity waiting, restore, and publish. |
| 66 | pub max_activations: usize, |
| 67 | /// Evictions that may hold a durability proof in flight at once. |
| 68 | /// |
| 69 | /// A proof is a round trip to the bucket, so draining a node one cell at a |
| 70 | /// time takes the number of cells times that latency -- a node shedding |
| 71 | /// five hundred cells against a two hundred millisecond proof refuses |
| 72 | /// admission for a minute and a half while it walks down. Bounded |
| 73 | /// concurrency is what makes the walk down finish in a time anyone can |
| 74 | /// reason about. |
| 75 | pub max_evictions: usize, |
| 76 | /// Evictions in flight at once during a shutdown handoff. |
| 77 | /// |
| 78 | /// `max_evictions` is tuned for a background walk down beside live |
| 79 | /// traffic. A drain has no live traffic to protect and a stop grace to |
| 80 | /// beat, so it runs wider -- but still bounded, because a node handing |
| 81 | /// off thousands of cells at once is the same thundering herd against |
| 82 | /// the bucket that eviction bounding exists to prevent. |
| 83 | pub max_releases: usize, |
| 84 | /// Concurrent outbound WebSockets one cell may hold. |
| 85 | /// |
| 86 | /// Distinct from the node-wide pin budget, which counts *cells* held |
| 87 | /// resident: one socket is enough to pin a cell, so that budget says |
| 88 | /// nothing about how many a single cell may open. This bounds what one |
| 89 | /// application can consume on its own behalf. |
| 90 | pub max_outbound_websockets: usize, |
| 91 | /// What an evicted cell's ownership record should say. |
| 92 | /// |
| 93 | /// Releasing it lets any node take the cell next, which is what makes a |
| 94 | /// loaded node shed load rather than merely stop hosting it: keeping the |
| 95 | /// record means every later request for that cell still routes here, to a |
| 96 | /// node that already decided it has no room. Keeping it is right when the |
| 97 | /// local eviction snapshot is the point, because a same-node wake is a |
| 98 | /// rename instead of a restore. |
| 99 | pub ownership_on_evict: OwnershipOnEvict, |
| 100 | /// Production executors require a live self-node lease before serving. |
| 101 | /// Deterministic unit slices which do not exercise node authority can |
| 102 | /// disable this explicitly. |
| 103 | pub require_node_lease: bool, |
| 104 | /// Exact cross-node request protocol this process can authenticate and |
| 105 | /// understand. A live owner speaking another version is unavailable, not |
| 106 | /// stale: incompatibility never authorizes takeover. |
| 107 | pub peer_protocol: u16, |
| 108 | /// How long an activation effect may remain outstanding before the core |
| 109 | /// stops waiting for it. |
| 110 | /// |
| 111 | /// Without this a swallowed effect is invisible: no event ever arrives, no |
| 112 | /// timer is watching, and the request waits forever while every piece of |
| 113 | /// state remains perfectly consistent. celld shipped that and parked |
| 114 | /// requests past ninety seconds. `None` restores the old behaviour, which |
| 115 | /// only a test that wants to observe an indefinite wait should ask for. |
| 116 | pub operation_deadline_ms: Option<u64>, |
| 117 | /// How close an armed alarm may be before the cell stops being worth |
| 118 | /// evicting. Inside this window the wake would cost more than the |
| 119 | /// residency it saves, so the cell is held. |
| 120 | pub alarm_resident_ms: u64, |
| 121 | /// How long a cell may sit unused before the node gives it back, with no |
| 122 | /// pressure involved. `None` keeps every cell resident until something |
| 123 | /// needs the room. |
| 124 | pub idle_evict_ms: Option<u64>, |
| 125 | /// Ceilings and low watermarks for load shedding. |
| 126 | /// |
| 127 | /// `max_resident` is a hard cap on reservations; this is the softer, |
| 128 | /// resource-aware question of whether the node is overloaded at all. |
| 129 | /// Without it a node has only a cell count to reason about, so it meets |
| 130 | /// memory exhaustion by running into it rather than shedding. |
| 131 | pub pressure: pressure::PressureConfig, |
| 132 | } |
| 133 | |
| 134 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 135 | pub struct OwnerRecord { |
| 136 | /// `None` is a deliberately released, fenced record. Epochs never reset. |
| 137 | pub node: Option<NodeId>, |
| 138 | pub epoch: Epoch, |
| 139 | pub etag: String, |
| 140 | } |
| 141 | |
| 142 | /// The routing and authority fields read from `nodes/<node>.json`. |
| 143 | /// |
| 144 | /// The executor samples wall time and returns the verbatim record. The core, |
| 145 | /// not the storage adapter, decides whether it is live and routable. |
| 146 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 147 | pub struct NodeLeaseRecord { |
| 148 | pub node: NodeId, |
| 149 | pub addr: String, |
| 150 | pub expires_ms: u64, |
| 151 | pub peer_protocol: u16, |
| 152 | /// Per-process generation; production stores this in |
| 153 | /// `ownership_index_generation`. |
| 154 | pub generation: String, |
| 155 | /// The folded node-log state: `None` means this session never opened |
| 156 | /// a fleet log — it never acked past the bucket. Anything not sealed |
| 157 | /// gates a takeover of this node's cells behind node-log recovery. |
| 158 | pub log_state: Option<crate::log_tier::LogState>, |
| 159 | /// Object version observed by the read. Empty only in synthetic events. |
| 160 | pub etag: String, |
| 161 | } |
| 162 | |
| 163 | /// One advisory fleet-capacity observation returned by the storage shell. |
| 164 | /// |
| 165 | /// Membership and expiry remain authoritative node-lease facts. Load is only |
| 166 | /// used to choose where an unowned cell should try to land; the chosen peer |
| 167 | /// must still atomically admit the handoff before it acquires ownership. |
| 168 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 169 | pub struct CapacityPeer { |
| 170 | pub node: NodeId, |
| 171 | pub addr: String, |
| 172 | pub expires_ms: u64, |
| 173 | pub peer_protocol: u16, |
| 174 | pub sampled_ms: u64, |
| 175 | pub resident_cells: usize, |
| 176 | pub host_websockets: usize, |
| 177 | pub rss_bytes: u64, |
| 178 | /// The memory this peer's cells hold, which is what it decides its own |
| 179 | /// pressure on. `None` from a peer that predates the field. |
| 180 | pub in_use_bytes: Option<u64>, |
| 181 | pub pressured: bool, |
| 182 | } |
| 183 | |
| 184 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 185 | pub struct NodeLeaseSpec { |
| 186 | pub addr: String, |
| 187 | pub peer_protocol: u16, |
| 188 | pub generation: String, |
| 189 | /// The generation named by a clean local reload certificate. An initial |
| 190 | /// lease CAS can resume local cells only when it replaces this exact, |
| 191 | /// still-live generation. |
| 192 | pub resume_generation: Option<String>, |
| 193 | pub ttl_ms: u64, |
| 194 | } |
| 195 | |
| 196 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 197 | pub enum CasGuard { |
| 198 | Absent, |
| 199 | Match(String), |
| 200 | } |
| 201 | |
| 202 | /// See [`Config::ownership_on_evict`]. |
| 203 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 204 | pub enum OwnershipOnEvict { |
| 205 | /// Publish the cell as unowned so any node may take it next. |
| 206 | #[default] |
| 207 | Release, |
| 208 | /// Keep the record, so a same-node wake can reuse the local snapshot. |
| 209 | Sticky, |
| 210 | } |
| 211 | |
| 212 | /// Why the node is halting. The shell writes this down: a process that |
| 213 | /// self-fences and exits without saying why leaves an operator with an exit |
| 214 | /// code and nothing else. |
| 215 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 216 | pub enum HaltReason { |
| 217 | /// The node lease was not renewed inside its TTL, so this node can no |
| 218 | /// longer prove it owns anything it is serving. |
| 219 | NodeLeaseExpired, |
| 220 | } |
| 221 | |
| 222 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 223 | pub enum CasOutcome { |
| 224 | Applied, |
| 225 | Rejected, |
| 226 | } |
| 227 | |
| 228 | /// Observable result of a successful restore effect. A non-fresh activation |
| 229 | /// may still discover that no local or replicated database exists; the |
| 230 | /// adapter reports what happened instead of making the core infer I/O truth. |
| 231 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 232 | pub struct RestoreOutcome { |
| 233 | pub restored: bool, |
| 234 | /// The alarm the restored database already had armed. |
| 235 | /// |
| 236 | /// A cell carries its alarm in its own SQLite, so a cell that arrives |
| 237 | /// here from another node — or wakes cold — has one the isolate has not |
| 238 | /// re-armed yet. Nothing else tells this node about it: the observer |
| 239 | /// fires when a *running* isolate calls `setAlarm`, which is exactly the |
| 240 | /// case this is not. Without it the mirror reads "no alarm" and every |
| 241 | /// residency decision that consults it is wrong in the direction of |
| 242 | /// shedding a cell that is about to fire. |
| 243 | pub alarm: Option<RestoredAlarm>, |
| 244 | } |
| 245 | |
| 246 | /// See [`RestoreOutcome::alarm`]. |
| 247 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 248 | pub struct RestoredAlarm { |
| 249 | pub at_ms: i64, |
| 250 | /// Whether a durable wake entry already covers it. Read from the same |
| 251 | /// flusher the observer consults, not assumed: claiming coverage this |
| 252 | /// node cannot prove is how an alarm gets lost across an eviction. |
| 253 | pub covered: bool, |
| 254 | } |
| 255 | |
| 256 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 257 | pub enum LeaseCasOutcome { |
| 258 | Applied { etag: String }, |
| 259 | Rejected, |
| 260 | } |
| 261 | |
| 262 | /// Deterministic timers are versioned effects, not implicit clock reads. A |
| 263 | /// stale firing from a replaced lease generation is harmless. |
| 264 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 265 | pub enum Timer { |
| 266 | NodeLeaseRenew { |
| 267 | generation: u64, |
| 268 | }, |
| 269 | NodeLeaseFence { |
| 270 | generation: u64, |
| 271 | }, |
| 272 | CellAlarm { |
| 273 | cell: CellId, |
| 274 | generation: u64, |
| 275 | }, |
| 276 | /// Fires if `op` is still outstanding. Identified by operation rather than |
| 277 | /// by cell, so a completion that lands first simply leaves a stale timer |
| 278 | /// that finds nothing to expire. |
| 279 | OperationDeadline { |
| 280 | op: OpId, |
| 281 | }, |
| 282 | /// Fires if `cell` is still parked behind the admission gate. Keyed by |
| 283 | /// cell because a parked cell has no operation: `OperationDeadline` is |
| 284 | /// armed per emitted effect, and parking is the state of having emitted |
| 285 | /// none. Without this the queue is the one stall with no bound. |
| 286 | QueuedActivation { |
| 287 | cell: CellId, |
| 288 | generation: u64, |
| 289 | }, |
| 290 | } |
| 291 | |
| 292 | /// Which mechanism proved a gated write durable. The fences differ: the |
| 293 | /// fleet's follower ensemble arbitrates (a takeover seals a member first), |
| 294 | /// the bucket only stores — so bucket proofs verify ownership before any |
| 295 | /// reveal and fleet proofs need not; a TLA+ spec checks the pair. |
| 296 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 297 | pub enum ProofSource { |
| 298 | Fleet, |
| 299 | Bucket, |
| 300 | } |
| 301 | |
| 302 | /// Whether a failed operation definitely did not commit or may have committed |
| 303 | /// before its caller lost the response. |
| 304 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 305 | pub enum Failure { |
| 306 | Definite, |
| 307 | Ambiguous, |
| 308 | } |
| 309 | |
| 310 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 311 | pub enum WebSocketKind { |
| 312 | Hibernatable, |
| 313 | Regular, |
| 314 | /// A transport the cell opened itself with `new WebSocket(url)`. It pins |
| 315 | /// its cell exactly as a regular one does -- a live transport cannot move |
| 316 | /// with ownership -- but unlike an inbound client socket it is created by |
| 317 | /// application code at a rate the application chooses, so how much of the |
| 318 | /// node it may hold is budgeted. |
| 319 | Outbound, |
| 320 | } |
| 321 | |
| 322 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 323 | pub enum Event { |
| 324 | StartNodeLease { |
| 325 | now_ms: u64, |
| 326 | /// The monotonic instant `now_ms` was sampled. The initial lease's |
| 327 | /// TTL is anchored here; without it the anchor is 0 and every |
| 328 | /// millisecond of pre-actor startup counts against the first |
| 329 | /// acquisition (a slow boot then discards a perfectly good lease). |
| 330 | now_mono_ms: u64, |
| 331 | spec: NodeLeaseSpec, |
| 332 | }, |
| 333 | SelfNodeLeaseRead { |
| 334 | op: OpId, |
| 335 | now_ms: u64, |
| 336 | now_mono_ms: u64, |
| 337 | result: Result<Option<NodeLeaseRecord>, Failure>, |
| 338 | }, |
| 339 | NodeLeaseCasCompleted { |
| 340 | op: OpId, |
| 341 | now_mono_ms: u64, |
| 342 | result: Result<LeaseCasOutcome, Failure>, |
| 343 | /// The folded log state the shell serialized into THIS attempt's |
| 344 | /// body. The shell stamps the body from its own slot at |
| 345 | /// serialization time — after the core chose `desired` — so the |
| 346 | /// core's readback comparisons must use this value, never its |
| 347 | /// pre-attempt belief. `None` means the body carried no log |
| 348 | /// object (bucket posture, or a backend that does not fold). |
| 349 | stamped_log_state: Option<crate::log_tier::LogState>, |
| 350 | }, |
| 351 | /// The executor scanned the local paths after an exact node-generation |
| 352 | /// handoff. These cells retain their existing ownership epochs, so the |
| 353 | /// core can materialize them without reading or changing each owner. |
| 354 | LocalCellsRead { |
| 355 | result: Result<Vec<LocalCell>, Failure>, |
| 356 | }, |
| 357 | TimerFired { |
| 358 | timer: Timer, |
| 359 | now_ms: u64, |
| 360 | now_mono_ms: u64, |
| 361 | }, |
| 362 | Request { |
| 363 | request: RequestId, |
| 364 | cell: CellId, |
| 365 | }, |
| 366 | /// Production form of [`Event::Request`] with the sampled clocks. Untimed |
| 367 | /// model slices retain the compact variant above and use the state's last |
| 368 | /// observed clocks. |
| 369 | RequestAt { |
| 370 | request: RequestId, |
| 371 | cell: CellId, |
| 372 | now_mono_ms: u64, |
| 373 | }, |
| 374 | /// A peer selected this node as a possible landing place for an unowned |
| 375 | /// cell. Unlike ordinary ingress, this request must refuse immediately if |
| 376 | /// its advertised capacity has gone stale; waiting here would strand the |
| 377 | /// forwarding node instead of letting it traverse another candidate. |
| 378 | CapacityRequestAt { |
| 379 | request: RequestId, |
| 380 | cell: CellId, |
| 381 | now_mono_ms: u64, |
| 382 | }, |
| 383 | /// Reserve an idle resident isolate for a top-level Worker request. The |
| 384 | /// shell falls back to the stateless pool when no resident is available; |
| 385 | /// choosing and pinning a resident is lifecycle policy and therefore |
| 386 | /// belongs in the replayable core. |
| 387 | WorkerRequest { |
| 388 | request: RequestId, |
| 389 | }, |
| 390 | /// Stop cold activation work for a planned same-node replacement while |
| 391 | /// leaving published runtimes and ownership intact. Late shell completions |
| 392 | /// are stale, so a long restore cannot consume the complete shutdown grace |
| 393 | /// and disable the certified local-reload path for every resident cell. |
| 394 | BeginPreserve, |
| 395 | Cancel { |
| 396 | request: RequestId, |
| 397 | }, |
| 398 | ActivityFinished { |
| 399 | request: RequestId, |
| 400 | }, |
| 401 | /// A request that ran locally advanced its cell's committed WAL to |
| 402 | /// `position`. Its response is withheld — the output gate — until that |
| 403 | /// position is replicated, so celld never acknowledges a write it could |
| 404 | /// still lose. Read-only requests emit [`Event::ActivityFinished`] instead |
| 405 | /// and pay no durability latency. The epoch is not carried: the core reads |
| 406 | /// it from the pinned cell's resident phase, which cannot change while the |
| 407 | /// request holds it. |
| 408 | Wrote { |
| 409 | request: RequestId, |
| 410 | position: u64, |
| 411 | }, |
| 412 | /// A read-only local response is ready. It can escape immediately only |
| 413 | /// when its cell has no outstanding write durability proof. |
| 414 | ReadOutput { |
| 415 | request: RequestId, |
| 416 | }, |
| 417 | /// The shell finished proving a gated write durable. `Ok(position)` reports |
| 418 | /// the committed-write position the replica has *actually* proved durable; |
| 419 | /// the core acknowledges only when it covers the gated write's position, so |
| 420 | /// a replicator that proves less than it was asked to cannot force an early |
| 421 | /// ack. `Err` failed the proof outright. |
| 422 | /// |
| 423 | /// `source` says which mechanism proved it, because the two carry |
| 424 | /// different ack fences (`CelldAckFence.tla`): a fleet proof is already |
| 425 | /// arbitrated — a takeover seals a follower before restoring, so a stale |
| 426 | /// owner's next ack-all fails closed — while a bucket proof needs C1's |
| 427 | /// ownership verification before anything is revealed. |
| 428 | DurableReached { |
| 429 | op: OpId, |
| 430 | result: Result<u64, Failure>, |
| 431 | source: ProofSource, |
| 432 | }, |
| 433 | /// C1's answer: the ownership record still names this node at this epoch |
| 434 | /// (`Ok`), or does not, or could not be read. |
| 435 | OwnershipVerified { |
| 436 | op: OpId, |
| 437 | result: Result<(), Failure>, |
| 438 | }, |
| 439 | WebSocketOpened { |
| 440 | cell: CellId, |
| 441 | websocket: WebSocketId, |
| 442 | kind: WebSocketKind, |
| 443 | }, |
| 444 | WebSocketClosed { |
| 445 | cell: CellId, |
| 446 | websocket: WebSocketId, |
| 447 | }, |
| 448 | AlarmObserved { |
| 449 | cell: CellId, |
| 450 | at_ms: Option<i64>, |
| 451 | covered: bool, |
| 452 | now_ms: u64, |
| 453 | now_mono_ms: u64, |
| 454 | }, |
| 455 | AlarmFinished { |
| 456 | op: OpId, |
| 457 | /// The cell and epoch the firing was dispatched against. The gate the |
| 458 | /// core opens for the consuming commit is keyed by these, and not by |
| 459 | /// the alarm's own op: an activity fold can supersede the firing state |
| 460 | /// while the handler's write is still unproven, and that write exists |
| 461 | /// — and must hold every reader of the cell — regardless. |
| 462 | cell: CellId, |
| 463 | epoch: Epoch, |
| 464 | now_ms: u64, |
| 465 | now_mono_ms: u64, |
| 466 | /// The deadline standing after the handler ran, whether a wake entry |
| 467 | /// covers it, and the position of the consuming commit if the firing |
| 468 | /// wrote. The position is sampled last, so it covers the consume |
| 469 | /// itself, and the core proves it durable before the alarm settles. |
| 470 | result: Result<(Option<i64>, bool, Option<u64>), Failure>, |
| 471 | }, |
| 472 | WakeHint { |
| 473 | cell: CellId, |
| 474 | }, |
| 475 | WakeHintAt { |
| 476 | cell: CellId, |
| 477 | now_mono_ms: u64, |
| 478 | }, |
| 479 | OwnerRead { |
| 480 | op: OpId, |
| 481 | /// Wall-clock observation made when the ownership read completed. It |
| 482 | /// bounds reuse of a shared owner-node lease without letting the core |
| 483 | /// read a clock itself. |
| 484 | now_ms: u64, |
| 485 | result: Result<Option<OwnerRecord>, Failure>, |
| 486 | }, |
| 487 | NodeLogRecovered { |
| 488 | op: OpId, |
| 489 | result: Result<(), Failure>, |
| 490 | }, |
| 491 | /// The log tier changed the folded log object the next lease write |
| 492 | /// must carry (lease-fold): renew NOW so the change is durable before |
| 493 | /// the caller proceeds. A no-op unless the lease is held. |
| 494 | NudgeNodeLease { |
| 495 | now_ms: u64, |
| 496 | now_mono_ms: u64, |
| 497 | }, |
| 498 | NodeLeaseRead { |
| 499 | op: OpId, |
| 500 | now_ms: u64, |
| 501 | result: Result<Option<NodeLeaseRecord>, Failure>, |
| 502 | }, |
| 503 | CapacityPeersRead { |
| 504 | op: OpId, |
| 505 | now_ms: u64, |
| 506 | result: Result<Vec<CapacityPeer>, Failure>, |
| 507 | }, |
| 508 | OwnerCasCompleted { |
| 509 | op: OpId, |
| 510 | result: Result<CasOutcome, Failure>, |
| 511 | }, |
| 512 | OwnerReleased { |
| 513 | op: OpId, |
| 514 | result: Result<CasOutcome, Failure>, |
| 515 | }, |
| 516 | RestoreCompleted { |
| 517 | op: OpId, |
| 518 | result: Result<RestoreOutcome, Failure>, |
| 519 | }, |
| 520 | RuntimeStarted { |
| 521 | op: OpId, |
| 522 | /// The isolate now holding this cell's realm. Placement happens in the |
| 523 | /// shell, so the core has to be told; the walk down groups on it to |
| 524 | /// empty a heap rather than scatter its cuts across every one. `None` |
| 525 | /// when no runtime placed the cell. |
| 526 | isolate: Option<crate::isolate::IsolateId>, |
| 527 | result: Result<(), Failure>, |
| 528 | }, |
| 529 | Published { |
| 530 | op: OpId, |
| 531 | result: Result<(), Failure>, |
| 532 | }, |
| 533 | DurabilityChecked { |
| 534 | op: OpId, |
| 535 | result: Result<(), Failure>, |
| 536 | }, |
| 537 | RuntimeStopped { |
| 538 | op: OpId, |
| 539 | }, |
| 540 | /// Policy input for this first slice. Later eviction selection emits this |
| 541 | /// from the same core rather than an external caller choosing a victim. |
| 542 | Evict { |
| 543 | cell: CellId, |
| 544 | }, |
| 545 | /// A periodic resource sample from the edge. |
| 546 | /// |
| 547 | /// The core never reads a clock or a proc file; the shell measures and |
| 548 | /// hands the numbers over, and every decision that follows -- whether the |
| 549 | /// node is overloaded, whether the latch stays hot, how far to shed -- |
| 550 | /// happens here where a schedule can replay it. |
| 551 | LoadSampled { |
| 552 | load: pressure::Load, |
| 553 | now_mono_ms: u64, |
| 554 | }, |
| 555 | /// Retire an exact cached remote route after a dispatch failure which is |
| 556 | /// known not to have executed the request. Newer route generations are |
| 557 | /// unaffected by delayed failure reports. |
| 558 | InvalidateRemote { |
| 559 | cell: CellId, |
| 560 | node: NodeId, |
| 561 | epoch: Epoch, |
| 562 | }, |
| 563 | NodeFenced, |
| 564 | /// The node is shutting down: hand every resident cell to a peer by |
| 565 | /// releasing its owner record, so a takeover is immediate rather than |
| 566 | /// waiting out the node lease. Unlike an on-demand evict this always |
| 567 | /// releases, whatever `ownership_on_evict` is set to. Releases run at |
| 568 | /// most `max_releases` at a time, and the pump takes the next resident |
| 569 | /// as each one completes -- a cell that is active when the drain begins |
| 570 | /// is picked up once its activity finishes. |
| 571 | ReleaseAll, |
| 572 | } |
| 573 | |
| 574 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 575 | pub enum Route { |
| 576 | Local, |
| 577 | Remote { |
| 578 | node: NodeId, |
| 579 | addr: String, |
| 580 | epoch: Epoch, |
| 581 | peer_protocol: u16, |
| 582 | }, |
| 583 | } |
| 584 | |
| 585 | /// Exact resident isolate reserved for a top-level Worker request. |
| 586 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 587 | pub struct WorkerRoute { |
| 588 | pub cell: CellId, |
| 589 | pub epoch: Epoch, |
| 590 | /// A pending durability proof made stale by selecting this still-routable |
| 591 | /// resident. The executor uses the ID only to release its effect waiter; |
| 592 | /// a late completion is ignored by the core's phase check. |
| 593 | pub retired_durability: Option<OpId>, |
| 594 | } |
| 595 | |
| 596 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 597 | pub enum RequestError { |
| 598 | NodeUnavailable, |
| 599 | ResolveFailed, |
| 600 | AcquireFailed, |
| 601 | RestoreFailed, |
| 602 | RuntimeFailed, |
| 603 | PublishFailed, |
| 604 | NodeFenced, |
| 605 | PeerIncompatible, |
| 606 | CapacityExhausted, |
| 607 | /// A local write ran but its durability could not be proven, so the |
| 608 | /// response must fail rather than falsely acknowledge the write. |
| 609 | DurabilityUnproven, |
| 610 | } |
| 611 | |
| 612 | /// Work performed outside the core. Every asynchronous effect is versioned; |
| 613 | /// completion events with an obsolete `op` are ignored. |
| 614 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 615 | pub enum Effect { |
| 616 | ScheduleTimer { |
| 617 | timer: Timer, |
| 618 | at_mono_ms: u64, |
| 619 | }, |
| 620 | ReadSelfNodeLease { |
| 621 | op: OpId, |
| 622 | }, |
| 623 | CasNodeLease { |
| 624 | op: OpId, |
| 625 | guard: CasGuard, |
| 626 | record: NodeLeaseRecord, |
| 627 | /// The prior record's peer-visible expiry for a renewal. The shell |
| 628 | /// uses it to report how much authority remained when the attempt |
| 629 | /// completed. An initial acquisition has no prior authority. |
| 630 | authority_expires_ms: Option<u64>, |
| 631 | }, |
| 632 | /// Read the local cell inventory. This is emitted only after the initial |
| 633 | /// node lease CAS replaced the exact certified predecessor generation. |
| 634 | ReadLocalCells, |
| 635 | ReadOwner { |
| 636 | op: OpId, |
| 637 | cell: CellId, |
| 638 | }, |
| 639 | ReadNodeLease { |
| 640 | op: OpId, |
| 641 | cell: CellId, |
| 642 | owner: NodeId, |
| 643 | }, |
| 644 | /// The takeover interlock, as a core decision (lease-fold): the dead |
| 645 | /// owner's folded log state was not sealed, so its acked tail may |
| 646 | /// exist only on its followers. The executor recovers every |
| 647 | /// non-sealed session of `owner` into the bucket and reports |
| 648 | /// `NodeLogRecovered`; only then does the claim proceed. |
| 649 | RecoverNodeLog { |
| 650 | op: OpId, |
| 651 | cell: CellId, |
| 652 | owner: NodeId, |
| 653 | }, |
| 654 | /// Enumerate recent node leases and return their advisory load records. |
| 655 | /// Listing and bounded parallel reads are adapter mechanics; selection, |
| 656 | /// reservations, and exclusions are deterministic core policy. |
| 657 | ReadCapacityPeers { |
| 658 | op: OpId, |
| 659 | cell: CellId, |
| 660 | }, |
| 661 | CasOwner { |
| 662 | op: OpId, |
| 663 | cell: CellId, |
| 664 | guard: CasGuard, |
| 665 | epoch: Epoch, |
| 666 | takeover: bool, |
| 667 | }, |
| 668 | /// Bring the bucket's wake entry for this cell into line with its alarm. |
| 669 | /// |
| 670 | /// Emitted wherever the alarm settles, which is the only place that |
| 671 | /// knows. An arm needs an entry; a consumed alarm needs its entry gone, |
| 672 | /// or every later due scan finds a hint for an alarm that already fired |
| 673 | /// and wakes a cell with nothing to do. `next_alarm_ms` is -1 when no |
| 674 | /// alarm remains. |
| 675 | ReconcileWakeEntry { |
| 676 | cell: CellId, |
| 677 | next_alarm_ms: i64, |
| 678 | }, |
| 679 | /// Publish an evicted cell as unowned, keeping its epoch, so the next |
| 680 | /// node to want it can take it without waiting for this one to notice. |
| 681 | ReleaseOwner { |
| 682 | op: OpId, |
| 683 | cell: CellId, |
| 684 | epoch: Epoch, |
| 685 | }, |
| 686 | Restore { |
| 687 | op: OpId, |
| 688 | cell: CellId, |
| 689 | spec: RestoreSpec, |
| 690 | }, |
| 691 | StartRuntime { |
| 692 | op: OpId, |
| 693 | cell: CellId, |
| 694 | epoch: Epoch, |
| 695 | }, |
| 696 | Publish { |
| 697 | op: OpId, |
| 698 | cell: CellId, |
| 699 | epoch: Epoch, |
| 700 | }, |
| 701 | /// Prove that every commit made before this effect is recoverable from |
| 702 | /// replica authority. Voluntary eviction cannot begin until this succeeds. |
| 703 | EnsureDurable { |
| 704 | op: OpId, |
| 705 | cell: CellId, |
| 706 | epoch: Epoch, |
| 707 | }, |
| 708 | /// The output gate: prove the cell's committed `position` is replicated so |
| 709 | /// a withheld local write response can be released. Unlike |
| 710 | /// [`Effect::EnsureDurable`] this is per-request and changes no cell phase, |
| 711 | /// so the cell keeps serving co-resident requests while one response waits. |
| 712 | AwaitDurable { |
| 713 | op: OpId, |
| 714 | cell: CellId, |
| 715 | epoch: Epoch, |
| 716 | position: u64, |
| 717 | }, |
| 718 | /// C1 for bucket-proof acks: read `own.json` and answer whether it |
| 719 | /// still names this node at this epoch. The core holds the gated write |
| 720 | /// until `Event::OwnershipVerified` returns. Fleet-proof acks never |
| 721 | /// emit this — the ensemble seal is their fence. |
| 722 | VerifyOwnership { |
| 723 | op: OpId, |
| 724 | cell: CellId, |
| 725 | epoch: Epoch, |
| 726 | }, |
| 727 | StopRuntime { |
| 728 | op: OpId, |
| 729 | cell: CellId, |
| 730 | epoch: Epoch, |
| 731 | cause: StopCause, |
| 732 | }, |
| 733 | FireAlarm { |
| 734 | op: OpId, |
| 735 | cell: CellId, |
| 736 | epoch: Epoch, |
| 737 | scheduled_ms: i64, |
| 738 | }, |
| 739 | Complete { |
| 740 | request: RequestId, |
| 741 | result: Result<Route, RequestError>, |
| 742 | }, |
| 743 | /// Release a withheld local write response now that its durability is |
| 744 | /// decided: `Ok` acknowledges the write, `Err` fails it. Emitted only for a |
| 745 | /// request the shell held open via [`Event::Wrote`] or |
| 746 | /// [`Event::ReadOutput`]. |
| 747 | ReleaseResponse { |
| 748 | request: RequestId, |
| 749 | result: Result<(), RequestError>, |
| 750 | }, |
| 751 | /// Complete the synchronous resident-selection decision. `None` means |
| 752 | /// the executor must use the ordinary stateless Worker pool. |
| 753 | CompleteWorker { |
| 754 | request: RequestId, |
| 755 | route: Option<WorkerRoute>, |
| 756 | }, |
| 757 | /// Refuse a transport the node cannot afford to hold. The shell closes it; |
| 758 | /// the cell carries on without it. |
| 759 | CloseWebSocket { |
| 760 | cell: CellId, |
| 761 | websocket: WebSocketId, |
| 762 | }, |
| 763 | Halt { |
| 764 | code: i32, |
| 765 | reason: HaltReason, |
| 766 | }, |
| 767 | } |
| 768 | |
| 769 | /// One database encoded by the local cell path. |
| 770 | #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 771 | pub struct LocalCell { |
| 772 | pub id: CellId, |
| 773 | pub epoch: Epoch, |
| 774 | } |
| 775 | |
| 776 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 777 | pub enum StopCause { |
| 778 | Cleanup, |
| 779 | /// `rebalance` means this eviction hands the cell to the fleet: its |
| 780 | /// ownership record is released and the local replica is not worth |
| 781 | /// keeping. An idle eviction is the opposite on both counts -- it |
| 782 | /// keeps the record so the next activation here renames the file into |
| 783 | /// place instead of paying a full remote restore. |
| 784 | Evict { |
| 785 | rebalance: bool, |
| 786 | }, |
| 787 | Fence, |
| 788 | /// A durability proof came back negative, so this runtime's memory and its |
| 789 | /// local database may hold writes the bucket does not. celld has just told |
| 790 | /// the caller those writes failed; continuing to serve them is the |
| 791 | /// divergence, not the failure. Stop, keep no local snapshot, and let the |
| 792 | /// next activation restore from the bucket. |
| 793 | Reset, |
| 794 | } |