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
// SPDX-License-Identifier: BUSL-1.1
//! Frame dispatch: `process_frame` routes incoming frames to the
//! per-kind handler methods.
use std::sync::Arc;
use tracing::{debug, error, info, warn};
use crate::control::security::audit::AuditLog;
use crate::control::security::jwt::JwtValidator;
use crate::control::security::rls::RlsPolicyStore;
use crate::control::state::SharedState;
use super::super::dlq::SyncDlq;
use super::super::wire::*;
use super::state::SyncSession;
impl SyncSession {
/// Process an incoming frame and return a response frame (if any).
///
/// Security-context parameters are optional — when provided,
/// per-delta RLS enforcement, rate limiting, silent rejection,
/// and DLQ persistence are active. `None` puts the session in
/// permissive mode (testing / internal replication channels).
///
/// `shared` is forwarded to `handle_handshake` so the durable
/// `SyncProducerRegistry` can be consulted during the Lite client
/// fencing decision.
///
/// # Timeseries push
///
/// In production, the listener intercepts `TimeseriesPush` before
/// calling `process_frame` and routes it through
/// [`SharedStateTimeseriesDispatcher`] for Data Plane ingest. If a frame of
/// this type ever reaches `process_frame`, it means the listener
/// interception is broken and `SharedState` is not available here;
/// we emit a loud rejection ACK and an `error!` log so the failure
/// is audible rather than silently dropping data after ACKing.
///
/// [`SharedStateTimeseriesDispatcher`]: super::super::timeseries_handler::SharedStateTimeseriesDispatcher
pub fn process_frame(
&mut self,
frame: &SyncFrame,
jwt_validator: &JwtValidator,
rls_store: Option<&RlsPolicyStore>,
audit_log: Option<&mut AuditLog>,
dlq: Option<&mut SyncDlq>,
shared: Option<&Arc<SharedState>>,
) -> Option<SyncFrame> {
match frame.msg_type {
SyncMessageType::Handshake => {
let msg: HandshakeMsg = frame.decode_body()?;
self.handle_handshake(&msg, jwt_validator, self.server_clock.clone(), shared)
}
SyncMessageType::DeltaPush => {
let msg: DeltaPushMsg = frame.decode_body()?;
self.handle_delta_push(&msg, rls_store, audit_log, dlq)
}
SyncMessageType::VectorClockSync => {
let msg: VectorClockSyncMsg = frame.decode_body()?;
self.handle_vector_clock_sync(&msg)
}
SyncMessageType::ShapeSubscribe => {
let msg: super::super::shape::handler::ShapeSubscribeMsg = frame.decode_body()?;
let tenant_id = self.tenant_id.map(|t| t.as_u64()).unwrap_or(0);
let current_lsn = self.server_clock.values().copied().max().unwrap_or(0);
// Record the subscription so CollectionPurged broadcast
// notifies this session when the shape's source
// collection is hard-deleted. Graph shapes have no
// single source collection; skip tracking for those.
if let Some(coll) = msg.shape.collection() {
self.track_collection(tenant_id, coll);
}
// Route to the persistent registry when SharedState is present;
// fall back to a throwaway registry for the permissive/test path.
if let Some(s) = shared {
super::super::shape::handler::handle_subscribe(
&self.session_id,
tenant_id,
&msg,
&s.shape_registry,
current_lsn,
|_shape, _lsn| super::super::shape::handler::ShapeSnapshotData::empty(),
)
} else {
let registry = super::super::shape::registry::ShapeRegistry::new();
super::super::shape::handler::handle_subscribe(
&self.session_id,
tenant_id,
&msg,
®istry,
current_lsn,
|_shape, _lsn| super::super::shape::handler::ShapeSnapshotData::empty(),
)
}
}
SyncMessageType::ShapeUnsubscribe => {
let msg: super::super::shape::handler::ShapeUnsubscribeMsg = frame.decode_body()?;
if let Some(s) = shared {
super::super::shape::handler::handle_unsubscribe(
&self.session_id,
&msg,
&s.shape_registry,
);
} else {
let registry = super::super::shape::registry::ShapeRegistry::new();
super::super::shape::handler::handle_unsubscribe(
&self.session_id,
&msg,
®istry,
);
}
None
}
SyncMessageType::TimeseriesPush => {
// Production path: listener.rs intercepts TimeseriesPush
// before this dispatch and runs it through
// SharedStateDispatcher. Reaching this arm means the
// listener interception is broken — emit a rejection
// ACK and a loud error log instead of silently dropping
// data.
let msg: TimeseriesPushMsg = frame.decode_body()?;
error!(
session = %self.session_id,
collection = %msg.collection,
samples = msg.sample_count,
"timeseries push reached generic process_frame — listener \
interception is broken; data NOT ingested, returning \
rejection ACK"
);
let ack = TimeseriesAckMsg {
collection: msg.collection.clone(),
accepted: 0,
rejected: msg.sample_count,
lsn: 0,
applied_seq: 0,
status: AckStatus::Applied,
};
SyncFrame::try_encode(SyncMessageType::TimeseriesAck, &ack)
}
SyncMessageType::TimeseriesAck => None,
// ColumnarInsert is intercepted in session_handler before reaching here.
// ColumnarInsertAck is server→client; receiving it here means a mis-wired
// client — ignore silently.
SyncMessageType::ColumnarInsert | SyncMessageType::ColumnarInsertAck => None,
// VectorInsert / VectorDelete are intercepted in session_handler before
// reaching here. VectorInsertAck / VectorDeleteAck are server→client;
// receiving them here means a mis-wired client — ignore silently.
SyncMessageType::VectorInsert
| SyncMessageType::VectorDelete
| SyncMessageType::VectorInsertAck
| SyncMessageType::VectorDeleteAck => None,
// FtsIndex / FtsDelete are intercepted in session_handler before
// reaching here. FtsIndexAck / FtsDeleteAck are server→client;
// receiving them here means a mis-wired client — ignore silently.
SyncMessageType::FtsIndex
| SyncMessageType::FtsDelete
| SyncMessageType::FtsIndexAck
| SyncMessageType::FtsDeleteAck => None,
// SpatialInsert / SpatialDelete are intercepted in session_handler
// before reaching here. The Ack variants are server→client; receiving
// them here means a mis-wired client — ignore silently.
SyncMessageType::SpatialInsert
| SyncMessageType::SpatialDelete
| SyncMessageType::SpatialInsertAck
| SyncMessageType::SpatialDeleteAck => None,
SyncMessageType::ResyncRequest => {
// In the production path (shared = Some), session_handler.rs
// intercepts ResyncRequest before process_frame and dispatches
// handle_resync_request_async. This arm is reached only when
// shared is None (permissive/test path) — log and drop.
if let Some(msg) = frame.decode_body::<ResyncRequestMsg>() {
warn!(
session = %self.session_id,
reason = ?msg.reason,
from_mutation_id = msg.from_mutation_id,
collection = %msg.collection,
shape_id = %msg.shape_id,
"client requested re-sync (no SharedState; dropping)"
);
}
None
}
SyncMessageType::TokenRefresh => {
let msg: TokenRefreshMsg = frame.decode_body()?;
self.handle_token_refresh(&msg, jwt_validator)
}
SyncMessageType::Throttle => {
if let Some(msg) = frame.decode_body::<ThrottleMsg>() {
info!(
session = %self.session_id,
throttle = msg.throttle,
queue_depth = msg.queue_depth,
suggested_rate = msg.suggested_rate,
"client throttle signal received"
);
}
None
}
SyncMessageType::PingPong => {
let msg: PingPongMsg = frame.decode_body()?;
if msg.is_pong {
None
} else {
self.handle_ping(&msg)
}
}
SyncMessageType::PresenceUpdate
| SyncMessageType::PresenceBroadcast
| SyncMessageType::PresenceLeave => {
debug!(
session = %self.session_id,
msg_type = frame.msg_type as u8,
"presence frame ignored (handled at listener level)"
);
None
}
SyncMessageType::CollectionSchema => {
// A peer announces a collection descriptor. Materialize it
// into the local catalog (create-only) so it becomes
// catalog-visible and queryable cluster-wide; the handler
// handles the permissive `shared == None` path by warn+skip.
let msg: CollectionSchemaSyncMsg = frame.decode_body()?;
self.handle_collection_schema(&msg, shared)
}
_ => {
warn!(
session = %self.session_id,
msg_type = frame.msg_type as u8,
"unhandled sync message type"
);
None
}
}
}
}