1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// SPDX-License-Identifier: BUSL-1.1
//! Snapshot, checkpoint, WAL append, cancel, range scan, and collection policy handlers.
use sonic_rs;
use tracing::{debug, info, warn};
use crate::bridge::envelope::{ErrorCode, Response};
use crate::types::RequestId;
use crate::data::executor::core_loop::CoreLoop;
use crate::data::executor::task::ExecutionTask;
impl CoreLoop {
pub(in crate::data::executor) fn execute_wal_append(
&self,
task: &ExecutionTask,
payload: &[u8],
) -> Response {
debug!(core = self.core_id, len = payload.len(), "wal append");
self.response_ok(task)
}
pub(in crate::data::executor) fn execute_cancel(
&mut self,
task: &ExecutionTask,
target_request_id: RequestId,
) -> Response {
debug!(core = self.core_id, %target_request_id, "cancel");
let pos = self
.task_queue
.iter()
.position(|t| t.request_id() == target_request_id);
if let Some(pos) = pos {
self.task_queue.remove(pos);
}
self.response_ok(task)
}
/// Read the conflict resolution policy for a collection and return it as JSON.
/// Returns the ephemeral default when no explicit policy has been registered.
pub(in crate::data::executor) fn execute_get_collection_policy(
&mut self,
task: &ExecutionTask,
collection: &str,
) -> Response {
debug!(core = self.core_id, %collection, "get collection policy");
let tenant_id = task.request.tenant_id;
let engine = match self.get_crdt_engine(tenant_id) {
Ok(e) => e,
Err(e) => {
warn!(core = self.core_id, error = %e, "failed to create CRDT engine for get policy");
return self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
);
}
};
let policy = engine.get_collection_policy(collection);
match sonic_rs::to_string(&policy) {
Ok(json) => self.response_with_payload(task, json.into_bytes()),
Err(e) => self.response_error(
task,
ErrorCode::Internal {
detail: format!("failed to serialize policy: {e}"),
},
),
}
}
pub(in crate::data::executor) fn execute_set_collection_policy(
&mut self,
task: &ExecutionTask,
collection: &str,
policy_json: &str,
) -> Response {
debug!(core = self.core_id, %collection, "set collection policy");
let tenant_id = task.request.tenant_id;
let engine = match self.get_crdt_engine(tenant_id) {
Ok(e) => e,
Err(e) => {
warn!(core = self.core_id, error = %e, "failed to create CRDT engine");
return self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
);
}
};
match engine.set_collection_policy(collection, policy_json) {
Ok(()) => self.response_ok(task),
Err(e) => {
warn!(core = self.core_id, error = %e, "set collection policy failed");
self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
)
}
}
}
#[allow(clippy::too_many_arguments)]
pub(in crate::data::executor) fn execute_range_scan(
&mut self,
task: &ExecutionTask,
tid: u64,
collection: &str,
field: &str,
lower: Option<&[u8]>,
upper: Option<&[u8]>,
limit: usize,
) -> Response {
debug!(core = self.core_id, %collection, %field, limit, "range scan");
// Try index-backed range scan first.
let results = match self
.sparse
.range_scan(tid, collection, field, lower, upper, limit)
{
Ok(r) => r,
Err(e) => {
warn!(core = self.core_id, error = %e, "sparse range scan failed");
return self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
);
}
};
// If the index returned nothing, fall back to full scan + sort.
// This handles collections without a secondary index on `field`.
if results.is_empty() {
let scan_result = self.scan_collection(tid, collection, limit.max(1000));
match scan_result {
Ok(mut docs) => {
super::super::document::sort::sort_rows(
&mut docs,
&[(field.to_string(), true)],
);
docs.truncate(limit);
// Raw msgpack passthrough — no decode/re-encode.
let rows: Vec<_> = docs
.iter()
.map(|(id, val)| {
let mp = super::super::super::doc_format::json_to_msgpack(val);
(id.clone(), mp)
})
.collect();
match super::super::super::response_codec::encode_raw_document_rows(&rows) {
Ok(payload) => return self.response_with_payload(task, payload),
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
);
}
}
}
Err(e) => {
return self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
);
}
}
}
match super::super::super::response_codec::encode(&results) {
Ok(payload) => self.response_with_payload(task, payload),
Err(e) => {
warn!(core = self.core_id, error = %e, "range scan serialization failed");
self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
)
}
}
}
/// Execute a snapshot creation request: export all engine state as bytes.
///
/// Returns the serialized `CoreSnapshot` as the response payload.
/// The Control Plane collects these from all cores and writes to disk.
pub(in crate::data::executor) fn execute_create_snapshot(
&self,
task: &ExecutionTask,
) -> Response {
match self.export_snapshot() {
Ok(snapshot) => match snapshot.to_bytes() {
Ok(bytes) => {
info!(
core = self.core_id,
watermark = snapshot.watermark,
documents = snapshot.sparse_documents.len(),
vectors = snapshot.hnsw_indexes.len(),
size_bytes = bytes.len(),
"snapshot exported"
);
self.response_with_payload(task, bytes)
}
Err(e) => {
warn!(core = self.core_id, error = %e, "snapshot serialization failed");
self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
)
}
},
Err(e) => {
warn!(core = self.core_id, error = %e, "snapshot export failed");
self.response_error(
task,
ErrorCode::Internal {
detail: e.to_string(),
},
)
}
}
}
/// Execute a coordinated checkpoint: flush all engine state to disk
/// and return this core's checkpoint LSN.
///
/// 1. Checkpoint vector indexes (HNSW segments → disk files).
/// 2. Export CRDT snapshots (Loro docs → disk files).
/// 3. redb sparse engine is already ACID — no action needed.
/// 4. CSR is rebuilt from redb edge store on startup — no action needed.
/// 5. Return the core's watermark LSN as the checkpoint point.
pub(in crate::data::executor) fn execute_checkpoint(
&mut self,
task: &ExecutionTask,
) -> Response {
let checkpoint_lsn = self.watermark.as_u64();
// 1. Flush vector indexes to disk.
let vectors_checkpointed = self.checkpoint_vector_indexes();
// 2. Flush CRDT snapshots to disk.
let crdts_checkpointed = self.checkpoint_crdt_engines();
// 3. Flush spatial R-tree indexes to disk.
let spatial_checkpointed = self.checkpoint_spatial_indexes();
// 4. Compact CSR write buffers into dense arrays for clean state.
if let Err(e) = self.csr.compact_all() {
tracing::warn!(error = %e, "CSR compaction rejected by memory governor during snapshot; skipping");
}
// 5. Record completed flushes in the checkpoint coordinator
// and advance the checkpoint LSN for WAL truncation safety.
self.checkpoint_coordinator
.record_flush("vector", vectors_checkpointed);
self.checkpoint_coordinator
.record_flush("crdt", crdts_checkpointed);
self.checkpoint_coordinator
.record_flush("spatial", spatial_checkpointed);
self.checkpoint_coordinator
.complete_checkpoint(checkpoint_lsn);
info!(
core = self.core_id,
checkpoint_lsn,
vectors_checkpointed,
crdts_checkpointed,
spatial_checkpointed,
dirty_pages = self.checkpoint_coordinator.total_dirty_pages(),
"core checkpoint complete"
);
// Return the checkpoint LSN as the response payload.
let payload = checkpoint_lsn.to_le_bytes().to_vec();
self.response_with_payload(task, payload)
}
/// Checkpoint all CRDT tenant engines to disk.
///
/// Each tenant's Loro state is exported as a snapshot and written to
/// `{data_dir}/crdt-ckpt/tenant-{id}.ckpt` with atomic temp+rename.
///
/// Called from both `snapshot.rs` (explicit checkpoint command) and
/// `compact.rs` (periodic maintenance via `maybe_run_maintenance`).
pub(in crate::data::executor) fn checkpoint_crdt_engines(&self) -> usize {
if self.crdt_engines.is_empty() {
return 0;
}
let ckpt_dir = self.data_dir.join("crdt-ckpt");
if std::fs::create_dir_all(&ckpt_dir).is_err() {
warn!(core = self.core_id, "failed to create CRDT checkpoint dir");
return 0;
}
let mut checkpointed = 0;
for (tenant_id, engine) in &self.crdt_engines {
match engine.export_snapshot_bytes() {
Ok(snapshot) => {
if snapshot.is_empty() {
continue;
}
let ckpt_path = ckpt_dir.join(format!("tenant-{tenant_id}.ckpt"));
let tmp_path = ckpt_dir.join(format!("tenant-{tenant_id}.ckpt.tmp"));
if nodedb_wal::segment::atomic_write_fsync(&tmp_path, &ckpt_path, &snapshot)
.is_ok()
{
checkpointed += 1;
}
}
Err(e) => {
warn!(
core = self.core_id,
tenant = tenant_id.as_u64(),
error = %e,
"CRDT checkpoint export failed"
);
}
}
}
if checkpointed > 0 {
info!(
core = self.core_id,
checkpointed,
total = self.crdt_engines.len(),
"CRDT engines checkpointed"
);
}
checkpointed
}
}