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
// SPDX-License-Identifier: BUSL-1.1
//! Async Data Plane dispatch helpers for the sync WebSocket listener.
//!
//! Contains async functions that cross the Control Plane / Data Plane boundary
//! via the SPSC bridge: shape-subscription snapshot queries and CRDT delta
//! constraint validation.
use std::time::Duration;
use tracing::{info, warn};
use crate::control::state::SharedState;
use super::wire::{CompensationHint, DeltaPushMsg, DeltaRejectMsg, SyncFrame, SyncMessageType};
/// Handle ShapeSubscribe with real WAL LSN and Data Plane snapshot.
pub(super) async fn handle_shape_subscribe_async(
shared: &SharedState,
session: &super::session::SyncSession,
frame: &SyncFrame,
) -> Option<SyncFrame> {
use crate::bridge::envelope::PhysicalPlan;
use crate::bridge::physical_plan::DocumentOp;
use crate::control::server::pgwire::ddl::sync_dispatch::dispatch_async;
use crate::types::TenantId;
let msg: super::shape::handler::ShapeSubscribeMsg = frame.decode_body()?;
let tenant_id = session.tenant_id.map(|t| t.as_u64()).unwrap_or(0);
// Quota enforcement — reject before dispatch.
let tid = TenantId::new(tenant_id);
if let Err(e) = shared.check_tenant_quota(tid) {
warn!(tenant_id, error = %e, "sync: shape subscribe rejected by quota");
return None;
}
// Get current WAL LSN — this is the watermark for the snapshot.
let current_lsn = shared.wal.next_lsn().as_u64().saturating_sub(1);
// Dispatch a query to the Data Plane to get matching data for this shape.
shared.tenant_request_start(tid);
let snapshot_data = match &msg.shape.shape_type {
nodedb_types::sync::shape::ShapeType::Document { collection, .. } => {
// Query the Data Plane for all documents in this collection.
let plan = PhysicalPlan::Document(DocumentOp::RangeScan {
collection: collection.clone(),
field: String::new(), // Empty = full collection scan.
lower: None,
upper: None,
limit: 10_000, // Cap for safety.
});
match dispatch_async(
shared,
TenantId::new(tenant_id),
collection,
plan,
Duration::from_secs(10),
)
.await
{
Ok(payload) => super::shape::handler::ShapeSnapshotData {
data: payload,
doc_count: 1, // Approximate — actual count in payload.
},
Err(e) => {
tracing::warn!(
shape_id = %msg.shape.shape_id,
error = %e,
"shape snapshot query failed, sending empty snapshot"
);
super::shape::handler::ShapeSnapshotData::empty()
}
}
}
nodedb_types::sync::shape::ShapeType::Vector { collection, .. } => {
// For vector shapes, the snapshot is the collection metadata.
// Full vector data is too large — Lite rebuilds from its own HNSW.
super::shape::handler::ShapeSnapshotData {
data: collection.as_bytes().to_vec(),
doc_count: 0,
}
}
nodedb_types::sync::shape::ShapeType::Graph { .. } => {
// Graph shapes: snapshot is the subgraph from root nodes.
// For now, return empty — full graph snapshot needs BFS dispatch.
super::shape::handler::ShapeSnapshotData::empty()
}
nodedb_types::sync::shape::ShapeType::Array {
array_name,
coord_range,
} => {
// Array shapes: validate the array exists, initialize the subscriber
// cursor, and return empty snapshot data. Full catch-up (op-log
// replay or tile snapshot) is driven by Phase H on the Lite side.
//
// 1. Validate the array exists in the schema registry.
let array_known = shared.array_sync_schemas.schema_hlc(array_name).is_some();
if !array_known {
warn!(
session = %session.session_id,
array = %array_name,
"array shape subscribe: array not known to Origin schema registry"
);
// Return without registering — the subscribe response will go
// back with an empty snapshot, and the Lite peer will retry
// when the schema is synced.
shared.tenant_request_end(tid);
return super::shape::handler::handle_subscribe(
&session.session_id,
tenant_id,
&msg,
&super::shape::registry::ShapeRegistry::new(),
current_lsn,
|_, _| super::shape::handler::ShapeSnapshotData::empty(),
);
}
// 2. Initialize the subscriber cursor at Hlc::ZERO so Phase H's
// catch-up path delivers all history on first sync.
shared.array_subscriber_cursors.register(
&session.session_id,
array_name,
coord_range.clone(),
);
info!(
session = %session.session_id,
array = %array_name,
"array shape subscribed; cursor initialized at HLC::ZERO"
);
// 3. Return empty snapshot data — catch-up via Phase H.
super::shape::handler::ShapeSnapshotData::empty()
}
// ShapeType is #[non_exhaustive]: new variants added in future protocol
// versions reach this arm before the handler is updated. Return empty
// snapshot — the subscriber will receive a well-formed but unpopulated
// response and can retry once the server is updated.
_ => {
warn!(
session = %session.session_id,
"shape subscribe: unknown shape_type variant, sending empty snapshot"
);
super::shape::handler::ShapeSnapshotData::empty()
}
};
shared.tenant_request_end(tid);
// Register the shape subscription.
let registry = super::shape::registry::ShapeRegistry::new();
let response = super::shape::handler::handle_subscribe(
&session.session_id,
tenant_id,
&msg,
®istry,
current_lsn,
|_shape, _lsn| snapshot_data,
);
info!(
session = %session.session_id,
shape_id = %msg.shape.shape_id,
lsn = current_lsn,
"shape subscribed with WAL LSN watermark"
);
response
}
/// Async constraint validation for a delta before sending DeltaAck.
///
/// Dispatches the delta to the Data Plane's CRDT engine for pre-validation
/// (UNIQUE, FK constraints). If validation fails, converts the DeltaAck
/// to a DeltaReject with a typed CompensationHint.
pub(super) async fn validate_delta_constraints(
shared: &SharedState,
delta_msg: &DeltaPushMsg,
ack_frame: SyncFrame,
) -> Option<SyncFrame> {
use crate::bridge::envelope::PhysicalPlan;
use crate::bridge::physical_plan::CrdtOp;
use crate::control::server::pgwire::ddl::sync_dispatch::dispatch_async_with_source;
use crate::types::TenantId;
// Dispatch a CrdtApply plan to the Data Plane. If the CRDT engine
// rejects it (constraint violation), we get an error back.
// Uses EventSource::CrdtSync so triggers are NOT fired on replicated deltas.
let tenant_id = TenantId::new(0); // Trust mode default tenant.
// Quota enforcement — reject before dispatch.
if let Err(e) = shared.check_tenant_quota(tenant_id) {
warn!(error = %e, "sync: delta validation rejected by quota");
let reject = DeltaRejectMsg {
mutation_id: delta_msg.mutation_id,
reason: e.to_string(),
compensation: Some(CompensationHint::Custom {
constraint: "quota".into(),
detail: e.to_string(),
}),
};
return SyncFrame::try_encode(SyncMessageType::DeltaReject, &reject);
}
let surrogate = match shared
.surrogate_assigner
.assign(&delta_msg.collection, delta_msg.document_id.as_bytes())
{
Ok(s) => s,
Err(e) => {
warn!(error = %e, "sync: surrogate assignment failed");
let reject = DeltaRejectMsg {
mutation_id: delta_msg.mutation_id,
reason: e.to_string(),
compensation: Some(CompensationHint::Custom {
constraint: "surrogate".into(),
detail: e.to_string(),
}),
};
return SyncFrame::try_encode(SyncMessageType::DeltaReject, &reject);
}
};
let plan = PhysicalPlan::Crdt(CrdtOp::Apply {
collection: delta_msg.collection.clone(),
document_id: delta_msg.document_id.clone(),
delta: delta_msg.delta.clone(),
peer_id: delta_msg.peer_id,
mutation_id: delta_msg.mutation_id,
surrogate,
});
shared.tenant_request_start(tenant_id);
let dispatch_result = dispatch_async_with_source(
shared,
tenant_id,
&delta_msg.collection,
plan,
Duration::from_secs(10),
crate::event::EventSource::CrdtSync,
)
.await;
shared.tenant_request_end(tenant_id);
match dispatch_result {
Ok(_payload) => {
// Constraint check passed — send the original DeltaAck.
Some(ack_frame)
}
Err(e) => {
let error_detail = e.to_string();
// Constraint check failed — convert to DeltaReject.
warn!(
collection = %delta_msg.collection,
doc = %delta_msg.document_id,
error = %error_detail,
"sync: delta constraint violation"
);
let hint = if error_detail.contains("unique") || error_detail.contains("UNIQUE") {
CompensationHint::UniqueViolation {
field: "unknown".into(),
conflicting_value: delta_msg.document_id.clone(),
}
} else if error_detail.contains("foreign") || error_detail.contains("FK") {
CompensationHint::ForeignKeyMissing {
referenced_id: delta_msg.document_id.clone(),
}
} else {
CompensationHint::Custom {
constraint: "constraint".into(),
detail: error_detail.clone(),
}
};
let reject = DeltaRejectMsg {
mutation_id: delta_msg.mutation_id,
reason: error_detail,
compensation: Some(hint),
};
SyncFrame::try_encode(SyncMessageType::DeltaReject, &reject)
}
}
}