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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//! Query execution and retry logic for subscriptions.
use std::sync::atomic::Ordering;
use tracing::{debug, trace, warn};
use vibesql_storage::Database;
use super::SubscriptionManager;
use crate::subscription::{
classify_error_str, compute_delta_with_pk, hash_rows, PartialRowDelta, Subscription,
SubscriptionError, SubscriptionErrorKind, SubscriptionId, SubscriptionUpdate,
};
impl SubscriptionManager {
/// Execute query with retry logic for transient errors
pub(crate) async fn execute_with_retry(
&self,
subscription: &mut Subscription,
db: &Database,
id: SubscriptionId,
) {
loop {
// Parse and execute the query
let result = self.execute_subscription_query(subscription, db, id).await;
match result {
Ok(rows) => {
// Successful execution - reset retry count
subscription.retry_count = 0;
// Convert to Row format
let result_rows: Vec<crate::Row> =
rows.iter().map(|r| crate::Row { values: r.values.to_vec() }).collect();
// Hash results for comparison
let new_hash = hash_rows(&result_rows);
if new_hash != subscription.last_result_hash {
debug!(
subscription_id = %id,
old_hash = subscription.last_result_hash,
new_hash = new_hash,
row_count = result_rows.len(),
"Results changed, notifying subscriber"
);
// Determine whether to send Delta, Partial, or Full update
let update = if let Some(ref old_rows) = subscription.last_result {
// We have previous results - compute delta using PK columns
if let Some(delta) = compute_delta_with_pk(
id,
old_rows,
&result_rows,
&subscription.pk_columns,
) {
// Check if we can use Partial updates (selective column updates)
// Conditions:
// 1. Subscription is selective_eligible (confident PK detection)
// 2. Delta has only updates (no inserts or deletes)
// 3. Updates exist
if let SubscriptionUpdate::Delta {
ref inserts,
ref updates,
ref deletes,
..
} = delta
{
if subscription.selective_eligible
&& inserts.is_empty()
&& deletes.is_empty()
&& !updates.is_empty()
{
// Convert to Partial updates
let partial_updates: Vec<PartialRowDelta> = updates
.iter()
.filter_map(|(old_row, new_row)| {
PartialRowDelta::from_rows(
old_row,
new_row,
&subscription.pk_columns,
)
})
.collect();
if !partial_updates.is_empty() {
debug!(
subscription_id = %id,
partial_updates = partial_updates.len(),
"Sending partial update (selective columns)"
);
SubscriptionUpdate::Partial {
subscription_id: id,
updates: partial_updates,
}
} else {
// Fall back to delta if partial conversion failed
debug!(
subscription_id = %id,
updates = updates.len(),
"Sending delta update (partial conversion failed)"
);
delta
}
} else {
// Log delta statistics and send as-is
debug!(
subscription_id = %id,
inserts = inserts.len(),
updates = updates.len(),
deletes = deletes.len(),
selective_eligible = subscription.selective_eligible,
"Sending delta update"
);
delta
}
} else {
delta
}
} else {
// No delta (shouldn't happen if hash changed, but be safe)
SubscriptionUpdate::Full {
subscription_id: id,
rows: result_rows.clone(),
}
}
} else {
// No previous results - send full (first update after initial)
debug!(
subscription_id = %id,
"No previous result, sending full update"
);
SubscriptionUpdate::Full {
subscription_id: id,
rows: result_rows.clone(),
}
};
// Update stored state
subscription.last_result_hash = new_hash;
subscription.last_result = Some(result_rows);
// Check for slow consumer before sending
let capacity = subscription.notify_tx.capacity();
let max_capacity = subscription.notify_tx.max_capacity();
let used = max_capacity.saturating_sub(capacity);
let usage_percent =
if max_capacity > 0 { (used * 100) / max_capacity } else { 0 };
if usage_percent >= subscription.slow_consumer_threshold_percent as usize {
warn!(
subscription_id = %id,
used = used,
max_capacity = max_capacity,
usage_percent = usage_percent,
threshold = subscription.slow_consumer_threshold_percent,
"Slow consumer detected: subscription channel is {}% full. \
Consider increasing channel_buffer_size or client is consuming too slowly.",
usage_percent
);
}
// Use try_send for non-blocking send with backpressure detection
match subscription.notify_tx.try_send(update) {
Ok(()) => {
subscription.updates_sent += 1;
trace!(
subscription_id = %id,
updates_sent = subscription.updates_sent,
"Update sent successfully"
);
}
Err(tokio::sync::mpsc::error::TrySendError::Full(_)) => {
subscription.updates_dropped += 1;
warn!(
subscription_id = %id,
updates_dropped = subscription.updates_dropped,
channel_buffer_size = subscription.channel_buffer_size,
"Subscription channel full, dropping update. \
Consider increasing channel_buffer_size in SubscriptionConfig \
or ensure client is consuming updates faster."
);
}
Err(tokio::sync::mpsc::error::TrySendError::Closed(_)) => {
trace!(
subscription_id = %id,
"Notification channel closed, subscription will be cleaned up"
);
}
}
} else {
trace!(
subscription_id = %id,
"Results unchanged, no notification needed"
);
}
return;
}
Err(error_msg) => {
// Classify the error to determine retry strategy
let error_kind = classify_error_str(&error_msg);
match error_kind {
SubscriptionErrorKind::Permanent => {
// Permanent error - don't retry, notify subscriber and stop
debug!(
subscription_id = %id,
error = %error_msg,
"Permanent error, not retrying"
);
let _ = subscription
.notify_tx
.send(SubscriptionUpdate::Error {
subscription_id: id,
message: format!(
"Query execution failed: {} (error will not be retried)",
error_msg
),
})
.await;
return;
}
SubscriptionErrorKind::Transient | SubscriptionErrorKind::Unknown => {
// Transient error - may retry
subscription.retry_count += 1;
if subscription.retry_count > subscription.retry_policy.max_retries {
// Exceeded max retries - circuit breaker
warn!(
subscription_id = %id,
retry_count = subscription.retry_count,
max_retries = subscription.retry_policy.max_retries,
error = %error_msg,
"Subscription failed after max retries"
);
let _ = subscription
.notify_tx
.send(SubscriptionUpdate::Error {
subscription_id: id,
message: format!(
"Subscription failed after {} retries: {}",
subscription.retry_policy.max_retries, error_msg
),
})
.await;
return;
}
// Calculate backoff and retry
let backoff = subscription
.retry_policy
.calculate_backoff(subscription.retry_count - 1);
warn!(
subscription_id = %id,
retry_attempt = subscription.retry_count,
backoff_ms = backoff.as_millis(),
error_kind = %error_kind,
error = %error_msg,
"Retrying subscription query after transient error"
);
tokio::time::sleep(backoff).await;
// Loop continues to retry
}
}
}
}
}
}
/// Execute the subscription query and return rows or error message
pub(crate) async fn execute_subscription_query(
&self,
subscription: &Subscription,
db: &Database,
id: SubscriptionId,
) -> Result<Vec<vibesql_storage::Row>, String> {
// Re-execute the query
let executor = vibesql_executor::SelectExecutor::new(db);
// Parse and execute the query
match vibesql_parser::Parser::parse_sql(&subscription.query) {
Ok(vibesql_ast::Statement::Select(select)) => {
executor.execute(&select).map_err(|e| e.to_string())
}
Ok(_) => {
// Not a SELECT - shouldn't happen for subscriptions
warn!(
subscription_id = %id,
"Subscription query is not a SELECT"
);
Err("Subscription query is not a SELECT".to_string())
}
Err(e) => Err(format!("Failed to parse query: {}", e)),
}
}
/// Send initial results to a new subscriber
///
/// Executes the query and sends the initial results. This should be called
/// right after subscribing to provide immediate data. The initial results
/// are always sent as a Full update.
///
/// # Errors
///
/// - `NotFound` if the subscription doesn't exist
/// - `ParseError` if the query fails to execute
/// - `ResultSetTooLarge` if the result set exceeds the configured limit
/// - `ChannelClosed` if the notification channel is closed
pub async fn send_initial_results(
&self,
id: SubscriptionId,
db: &Database,
) -> Result<(), SubscriptionError> {
let mut sub_ref =
self.subscriptions.get_mut(&id).ok_or(SubscriptionError::NotFound(id))?;
let subscription = sub_ref.value_mut();
// Execute the query
let executor = vibesql_executor::SelectExecutor::new(db);
let stmt = vibesql_parser::Parser::parse_sql(&subscription.query)
.map_err(|e| SubscriptionError::ParseError(e.to_string()))?;
let rows = match stmt {
vibesql_ast::Statement::Select(select) => executor
.execute(&select)
.map_err(|e| SubscriptionError::ParseError(e.to_string()))?,
_ => return Err(SubscriptionError::ParseError("Not a SELECT query".to_string())),
};
// Check result set size limit
if rows.len() > self.config.max_result_rows {
self.result_set_exceeded_count.fetch_add(1, Ordering::Relaxed);
return Err(SubscriptionError::ResultSetTooLarge {
rows: rows.len(),
max: self.config.max_result_rows,
});
}
// Convert to Row format
let result_rows: Vec<crate::Row> =
rows.iter().map(|r| crate::Row { values: r.values.to_vec() }).collect();
// Update hash and store result for delta computation
subscription.last_result_hash = hash_rows(&result_rows);
subscription.last_result = Some(result_rows.clone());
// Send initial results (always Full for initial)
subscription
.notify_tx
.send(SubscriptionUpdate::Full { subscription_id: id, rows: result_rows })
.await
.map_err(|_| SubscriptionError::ChannelClosed)?;
Ok(())
}
}