Diffinery

homeflare/celldpublic

celld/crates/celld/cell_host.rs
226 lines · 8.3 KBRaw History
1// Copyright 2026 Deno Land Inc. Apache-2.0 license.
2
3//! The Actor's one boundary to whichever engine runs cells.
4
5use crate::engine_api::StopMode;
6use crate::generation::GenerationId;
7use crate::runtime::RuntimeManager;
8
9/// The Actor's cell-runtime boundary: the engine's runtime, or the scripted
10/// host the deterministic worlds drive.
11#[derive(Clone)]
12pub(crate) enum CellHost {
13 Engine(RuntimeManager),
14 #[cfg(all(test, celld_internal_tests))]
15 Scripted(crate::conformance_sim_cell_host::SimCellHost),
16}
17
18impl From<RuntimeManager> for CellHost {
19 fn from(runtime: RuntimeManager) -> Self {
20 Self::Engine(runtime)
21 }
22}
23
24impl CellHost {
25 pub(crate) async fn fetch_cell(
26 &self,
27 cell: String,
28 name: Option<String>,
29 request: crate::engine_api::RuntimeFetch,
30 cancel: Option<tokio::sync::oneshot::Receiver<()>>,
31 ) -> anyhow::Result<crate::engine_api::HttpResponse> {
32 match self {
33 Self::Engine(runtime) => runtime.fetch_cell(cell, name, request, cancel).await,
34 #[cfg(all(test, celld_internal_tests))]
35 Self::Scripted(runtime) => {
36 runtime
37 .fetch_cell_for_world(cell, name, request, cancel)
38 .await
39 }
40 }
41 }
42
43 pub(crate) fn local_reload_cells(&self) -> anyhow::Result<Vec<celld_logic::LocalCell>> {
44 match self {
45 Self::Engine(runtime) => runtime.local_reload_cells(),
46 #[cfg(all(test, celld_internal_tests))]
47 Self::Scripted(runtime) => runtime.local_reload_cells(),
48 }
49 }
50
51 pub(crate) async fn restore_cell(
52 &self,
53 cell: &str,
54 spec: &celld_logic::RestoreSpec,
55 ) -> anyhow::Result<celld_logic::RestoreOutcome> {
56 match self {
57 Self::Engine(runtime) => runtime.restore_cell(cell, spec).await,
58 #[cfg(all(test, celld_internal_tests))]
59 Self::Scripted(runtime) => runtime.restore_cell(cell, spec).await,
60 }
61 }
62
63 /// Start a cell's runtime, reporting the isolate that took its realm and
64 /// the application generation that isolate belongs to.
65 pub(crate) async fn start_cell(
66 &self,
67 cell: String,
68 epoch: u64,
69 fresh: bool,
70 ) -> anyhow::Result<(celld_logic::isolate::HeapId, GenerationId)> {
71 match self {
72 Self::Engine(runtime) => runtime.start_cell(cell, epoch, fresh).await,
73 // The scripted host maps its deterministic isolate pool to heap
74 // identities on the generation a node boots with; no scripted
75 // world adopts another.
76 #[cfg(all(test, celld_internal_tests))]
77 Self::Scripted(runtime) => runtime
78 .start_cell(cell, epoch, fresh)
79 .await
80 .map(|isolate| (isolate, crate::generation::FIRST_GENERATION)),
81 }
82 }
83
84 /// Take a cell out of its isolate for a generation swap: close its
85 /// application database and give the placement back, and touch nothing
86 /// else. Ownership, the epoch, the replica, and the wake entry all stay.
87 pub(crate) async fn swap_out_cell(&self, cell: &str, epoch: u64) -> anyhow::Result<()> {
88 match self {
89 Self::Engine(runtime) => runtime.swap_out_cell(cell, epoch).await,
90 #[cfg(all(test, celld_internal_tests))]
91 Self::Scripted(runtime) => runtime.swap_out_cell(cell, epoch).await,
92 }
93 }
94
95 pub(crate) fn publish_cell(&self, cell: &str, epoch: u64) -> anyhow::Result<()> {
96 match self {
97 Self::Engine(runtime) => runtime.publish_cell(cell, epoch),
98 #[cfg(all(test, celld_internal_tests))]
99 Self::Scripted(runtime) => runtime.publish_cell(cell, epoch),
100 }
101 }
102
103 pub(crate) async fn ensure_durable(
104 &self,
105 cell: &str,
106 epoch: u64,
107 revocable: bool,
108 ) -> anyhow::Result<()> {
109 match self {
110 Self::Engine(runtime) => runtime.ensure_durable(cell, epoch, revocable).await,
111 // The scripted host owns a real replicator over a real object
112 // store, so it takes the same gate. Letting it skip the gate made
113 // the gate unreachable from the deterministic worlds, which is
114 // why no case noticed when the pre-close check was removed.
115 #[cfg(all(test, celld_internal_tests))]
116 Self::Scripted(runtime) => runtime.ensure_durable(cell, epoch, revocable).await,
117 }
118 }
119
120 pub(crate) async fn await_durable(
121 &self,
122 cell: &str,
123 epoch: u64,
124 position: u64,
125 ) -> anyhow::Result<(u64, celld_logic::ProofSource)> {
126 match self {
127 Self::Engine(runtime) => runtime.await_durable(cell, epoch, position).await,
128 #[cfg(all(test, celld_internal_tests))]
129 Self::Scripted(runtime) => runtime.await_durable(cell, epoch, position).await,
130 }
131 }
132
133 pub(crate) async fn stop_cell(
134 &self,
135 cell: &str,
136 epoch: u64,
137 mode: StopMode,
138 ) -> anyhow::Result<()> {
139 match self {
140 Self::Engine(runtime) => runtime.stop_cell(cell, epoch, mode).await,
141 #[cfg(all(test, celld_internal_tests))]
142 Self::Scripted(runtime) => runtime.stop_cell(cell, epoch, mode).await,
143 }
144 }
145
146 pub(crate) fn abort_activity(&self, request_id: crate::js::RequestId) {
147 match self {
148 Self::Engine(_) => crate::js::abort_request_for_shutdown(request_id),
149 #[cfg(all(test, celld_internal_tests))]
150 Self::Scripted(_) => {}
151 }
152 }
153
154 /// Keep the snapshot and its consumer under the host's reporter lock.
155 /// An activity completion sent after a newer report can otherwise erase it.
156 pub(crate) fn with_alarm<T>(
157 &self,
158 cell: &str,
159 f: impl FnOnce(celld_logic::wake::AlarmSnapshot, bool) -> T,
160 ) -> T {
161 match self {
162 Self::Engine(runtime) => runtime
163 .with_alarm_snapshot(cell, |alarm| f(alarm, runtime.alarm_covered(cell, alarm))),
164 #[cfg(all(test, celld_internal_tests))]
165 Self::Scripted(runtime) => runtime.with_alarm(cell, f),
166 }
167 }
168
169 /// Legacy scripted drivers supply their own observations. A host connected
170 /// to the activity path supplies them through the same boundary as an engine.
171 pub(crate) fn alarm_observation(
172 &self,
173 cell: &str,
174 ) -> Option<(celld_logic::wake::AlarmSnapshot, bool)> {
175 match self {
176 Self::Engine(_) => Some(self.with_alarm(cell, |alarm, covered| (alarm, covered))),
177 #[cfg(all(test, celld_internal_tests))]
178 Self::Scripted(runtime) => runtime
179 .observes_activity_alarms()
180 .then(|| self.with_alarm(cell, |alarm, covered| (alarm, covered))),
181 }
182 }
183
184 /// Make an armed alarm discoverable before a graceful handoff continues.
185 /// The runtime cache and the wake flusher share the reporter ordering, so
186 /// re-reading after the awaited reconcile gives the core one observation
187 /// that cannot overtake the bucket operation it describes.
188 pub(crate) async fn refresh_handoff_alarm_coverage(
189 &self,
190 cell: &str,
191 alarm: celld_logic::wake::AlarmSnapshot,
192 ) -> Option<(celld_logic::wake::AlarmSnapshot, bool)> {
193 #[cfg(all(test, celld_internal_tests))]
194 if matches!(self, Self::Scripted(host) if !host.observes_activity_alarms()) {
195 return None;
196 }
197 if crate::js::publish_observed_wake_entry(cell, alarm)
198 .await
199 .is_err()
200 {
201 return None;
202 }
203 self.alarm_observation(cell)
204 }
205
206 pub(crate) async fn fire_alarm(
207 &self,
208 op: celld_logic::OpId,
209 cell: String,
210 scheduled_ms: i64,
211 ) -> anyhow::Result<(celld_logic::wake::AlarmSnapshot, bool, Option<u64>)> {
212 match self {
213 Self::Engine(runtime) => runtime.fire_alarm(op, cell, scheduled_ms).await,
214 #[cfg(all(test, celld_internal_tests))]
215 Self::Scripted(runtime) => runtime.fire_alarm(op, cell, scheduled_ms).await,
216 }
217 }
218
219 pub(crate) fn abort_alarm(&self, cell: &str, op: celld_logic::OpId) {
220 match self {
221 Self::Engine(runtime) => runtime.abort_alarm(cell, op),
222 #[cfg(all(test, celld_internal_tests))]
223 Self::Scripted(_) => {}
224 }
225 }
226}