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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! SLA-aware admission control integration for the ARQ query executor.
//!
//! [`ArqSlaGate`] is the entry point that wraps [`oxirs_core::sla::AdmissionController`]
//! and [`oxirs_core::sla::PriorityDispatcher`] with ARQ-specific logic:
//!
//! * Translates rejected admission attempts into typed [`ArqSlaError`] values.
//! * Looks up tenant SLA classes via [`TenantConfigRegistry`].
//! * Applies per-tenant overrides (custom token cost, soft-vs-strict mode).
//! * Provides a synchronous "admit and execute" wrapper that funnels work
//! through the priority dispatcher.
//!
//! The gate is designed to be wired into [`crate::executor::QueryExecutor`]
//! before each `execute()` call. See `tests/sla_admission.rs` for an
//! end-to-end integration example.
use std::fmt;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use oxirs_core::sla::{
AdmissionController, AdmissionError, PrioritizedQuery, PriorityDispatcher, SlaClass,
};
use thiserror::Error;
use crate::tenant_config::TenantConfigRegistry;
// ─────────────────────────────────────────────────────────────────────────────
// ArqSlaError
// ─────────────────────────────────────────────────────────────────────────────
/// Errors raised by [`ArqSlaGate`] when a query cannot proceed.
#[derive(Debug, Error, Clone, PartialEq, Eq)]
pub enum ArqSlaError {
/// The tenant has not been registered with the gate.
#[error("tenant '{tenant_id}' is not registered with the ARQ SLA gate")]
TenantNotRegistered {
/// Identifier of the unknown tenant.
tenant_id: String,
},
/// The tenant's token bucket is exhausted.
#[error("SLA exceeded for tenant '{tenant_id}' (class {class}): {detail}")]
SlaExceeded {
/// Identifier of the tenant whose budget is exhausted.
tenant_id: String,
/// SLA tier that was being enforced.
class: SlaClassDisplay,
/// Free-form description of the rejection reason.
detail: String,
},
/// A queued query was dropped or the dispatcher was shut down.
#[error("priority dispatcher closed for tenant '{tenant_id}'")]
DispatcherClosed {
/// Identifier of the tenant whose query was dropped.
tenant_id: String,
},
}
/// Wrapper around [`SlaClass`] that gives `Display` and PEq impls suitable
/// for error formatting.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SlaClassDisplay(pub SlaClass);
impl fmt::Display for SlaClassDisplay {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0.name())
}
}
impl From<SlaClass> for SlaClassDisplay {
fn from(class: SlaClass) -> Self {
SlaClassDisplay(class)
}
}
// ─────────────────────────────────────────────────────────────────────────────
// AdmittedQuery
// ─────────────────────────────────────────────────────────────────────────────
/// Hand-back struct returned by [`ArqSlaGate::admit`] on a successful gate
/// pass. The caller consumes the inner payload after performing the query.
#[derive(Debug)]
pub struct AdmittedQuery<P> {
/// Tenant the work belongs to.
pub tenant_id: String,
/// SLA class enforced by the gate.
pub sla_class: SlaClass,
/// Per-query timeout (if the tenant config specified one).
pub timeout: Option<Duration>,
/// The payload (typically the algebra to execute).
pub payload: P,
}
// ─────────────────────────────────────────────────────────────────────────────
// ArqSlaGate
// ─────────────────────────────────────────────────────────────────────────────
/// Front door for ARQ tenant admission control.
///
/// Internally wraps:
///
/// * an [`AdmissionController`] (token-bucket gate),
/// * a [`PriorityDispatcher`] (max-heap by SLA tier), and
/// * the [`TenantConfigRegistry`] that drives both.
///
/// The gate is `Clone`-cheap; share one instance across executor threads.
#[derive(Clone)]
pub struct ArqSlaGate {
registry: TenantConfigRegistry,
controller: AdmissionController,
dispatcher: Arc<Mutex<PriorityDispatcher<DispatcherSlot>>>,
}
#[derive(Debug)]
struct DispatcherSlot {
label: String,
}
impl ArqSlaGate {
/// Build a gate driven by the given registry.
///
/// Tenants present in the registry at construction time are pre-registered
/// with the underlying [`AdmissionController`] so the very first admission
/// attempt for each tenant has a fresh full bucket.
pub fn new(registry: TenantConfigRegistry) -> Self {
let controller = AdmissionController::new();
for tenant_id in registry.tenant_ids() {
if let Some(class) = registry.sla_class(&tenant_id) {
controller.register_tenant(&tenant_id, class);
}
}
Self {
registry,
controller,
dispatcher: Arc::new(Mutex::new(PriorityDispatcher::new())),
}
}
/// Register a tenant on the fly (forwards into both the registry and the
/// admission controller).
pub fn register_tenant(&self, config: crate::tenant_config::TenantConfig) {
self.controller
.register_tenant(&config.tenant_id, config.sla_class);
self.registry.register(config);
}
/// Convenience: peek at the snapshot of the underlying registry.
pub fn registry(&self) -> &TenantConfigRegistry {
&self.registry
}
/// Convenience: peek at the underlying admission controller.
pub fn controller(&self) -> &AdmissionController {
&self.controller
}
/// Try to admit a query for `tenant_id`.
///
/// On acceptance, the payload is returned wrapped in an [`AdmittedQuery`].
/// On rejection, the call returns either [`ArqSlaError::TenantNotRegistered`]
/// or [`ArqSlaError::SlaExceeded`] depending on the reason.
pub fn admit<P>(&self, tenant_id: &str, payload: P) -> Result<AdmittedQuery<P>, ArqSlaError> {
let config =
self.registry
.get(tenant_id)
.ok_or_else(|| ArqSlaError::TenantNotRegistered {
tenant_id: tenant_id.to_owned(),
})?;
match self
.controller
.try_admit_with_cost(tenant_id, config.max_query_cost)
{
Ok(()) => Ok(AdmittedQuery {
tenant_id: tenant_id.to_owned(),
sla_class: config.sla_class,
timeout: config.query_timeout,
payload,
}),
Err(err) => {
if !config.strict_admission {
// Soft mode: pass through with the SLA tier still attached
// but no token consumed. Useful for canary tenants.
Ok(AdmittedQuery {
tenant_id: tenant_id.to_owned(),
sla_class: config.sla_class,
timeout: config.query_timeout,
payload,
})
} else {
Err(self.translate_admission_error(&config.tenant_id, config.sla_class, err))
}
}
}
}
fn translate_admission_error(
&self,
tenant_id: &str,
class: SlaClass,
err: AdmissionError,
) -> ArqSlaError {
match err {
AdmissionError::RateLimitExceeded { .. } => ArqSlaError::SlaExceeded {
tenant_id: tenant_id.to_owned(),
class: class.into(),
detail: "token bucket exhausted".to_owned(),
},
AdmissionError::TenantNotRegistered { .. } => {
// Should not happen because admit() registers via registry,
// but handle defensively.
ArqSlaError::TenantNotRegistered {
tenant_id: tenant_id.to_owned(),
}
}
}
}
/// Enqueue an admitted query into the priority dispatcher.
///
/// Returns the SLA tier under which the query was enqueued. Use
/// [`Self::next_dispatch`] to drain the queue.
pub fn enqueue<P>(&self, admitted: &AdmittedQuery<P>, label: impl Into<String>) {
let mut dispatcher = self.dispatcher.lock().unwrap_or_else(|e| e.into_inner());
dispatcher.enqueue(
admitted.tenant_id.clone(),
admitted.sla_class,
DispatcherSlot {
label: label.into(),
},
);
}
/// Pop the next pending query (highest priority first).
///
/// Returns `None` if the dispatcher is empty. This API is sufficient for
/// a single-thread executor; multi-threaded callers should pair it with
/// their own work-stealing loop.
pub fn next_dispatch(&self) -> Option<DispatchedQuery> {
let mut dispatcher = self.dispatcher.lock().unwrap_or_else(|e| e.into_inner());
dispatcher.dequeue().map(
|prioritized: PrioritizedQuery<DispatcherSlot>| DispatchedQuery {
tenant_id: prioritized.tenant_id,
priority: prioritized.priority,
label: prioritized.payload.label,
},
)
}
/// Number of queries currently waiting in the dispatcher.
pub fn pending_count(&self) -> usize {
let dispatcher = self.dispatcher.lock().unwrap_or_else(|e| e.into_inner());
dispatcher.len()
}
}
/// Outcome of [`ArqSlaGate::next_dispatch`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DispatchedQuery {
/// Tenant that submitted the query.
pub tenant_id: String,
/// Numeric SLA priority used by the dispatcher.
pub priority: u8,
/// Free-form label set by the caller of [`ArqSlaGate::enqueue`].
pub label: String,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tenant_config::TenantConfig;
fn registry_with(tenants: &[(&str, SlaClass)]) -> TenantConfigRegistry {
let registry = TenantConfigRegistry::new();
for (id, class) in tenants {
registry.register(TenantConfig::new(*id, *class));
}
registry
}
#[test]
fn test_admit_unregistered_tenant_fails() {
let gate = ArqSlaGate::new(TenantConfigRegistry::new());
let err = gate
.admit("ghost", "select * { ?s ?p ?o }".to_string())
.expect_err("unregistered tenants must be rejected");
assert!(matches!(err, ArqSlaError::TenantNotRegistered { .. }));
}
#[test]
fn test_admit_registered_tenant_succeeds() {
let registry = registry_with(&[("alpha", SlaClass::Platinum)]);
let gate = ArqSlaGate::new(registry);
let admitted = gate
.admit("alpha", "query".to_string())
.expect("Platinum bucket should admit first request");
assert_eq!(admitted.tenant_id, "alpha");
assert_eq!(admitted.sla_class, SlaClass::Platinum);
assert_eq!(admitted.payload, "query");
}
#[test]
fn test_bronze_exhaustion_returns_sla_exceeded() {
let registry = registry_with(&[("budget", SlaClass::Bronze)]);
let gate = ArqSlaGate::new(registry);
// Bronze capacity = 5, max_query_cost = 1 by default. Drain it.
let mut last_err: Option<ArqSlaError> = None;
for _ in 0..50 {
if let Err(err) = gate.admit("budget", ()) {
last_err = Some(err);
break;
}
}
let err = last_err.expect("Bronze tenant must eventually be rejected");
assert!(
matches!(err, ArqSlaError::SlaExceeded { ref class, .. } if class.0 == SlaClass::Bronze)
);
}
#[test]
fn test_soft_admission_passes_through_after_exhaustion() {
let registry = TenantConfigRegistry::new();
registry
.register(TenantConfig::new("canary", SlaClass::Bronze).with_strict_admission(false));
let gate = ArqSlaGate::new(registry);
// Drain the bucket
for _ in 0..20 {
let _ = gate.admit("canary", ()).expect("soft admission must pass");
}
}
#[test]
fn test_dispatcher_orders_by_sla() {
let registry = registry_with(&[
("free", SlaClass::Bronze),
("paid", SlaClass::Gold),
("vip", SlaClass::Platinum),
]);
let gate = ArqSlaGate::new(registry);
let bronze = gate.admit("free", "b").expect("admit bronze");
let gold = gate.admit("paid", "g").expect("admit gold");
let platinum = gate.admit("vip", "p").expect("admit platinum");
gate.enqueue(&bronze, "bronze-job");
gate.enqueue(&gold, "gold-job");
gate.enqueue(&platinum, "platinum-job");
assert_eq!(gate.pending_count(), 3);
let first = gate
.next_dispatch()
.expect("dispatcher must yield platinum first");
assert_eq!(first.tenant_id, "vip");
assert_eq!(first.label, "platinum-job");
let second = gate.next_dispatch().expect("then gold");
assert_eq!(second.tenant_id, "paid");
let third = gate.next_dispatch().expect("then bronze");
assert_eq!(third.tenant_id, "free");
assert!(gate.next_dispatch().is_none());
}
#[test]
fn test_register_tenant_via_gate() {
let gate = ArqSlaGate::new(TenantConfigRegistry::new());
gate.register_tenant(TenantConfig::new("late", SlaClass::Gold));
let admitted = gate
.admit("late", "q".to_string())
.expect("late-registered tenant must admit");
assert_eq!(admitted.sla_class, SlaClass::Gold);
}
#[test]
fn test_max_query_cost_drains_bucket_faster() {
let registry = TenantConfigRegistry::new();
registry.register(TenantConfig::new("greedy", SlaClass::Silver).with_max_query_cost(10.0));
let gate = ArqSlaGate::new(registry);
// Silver capacity = 20. At cost = 10, two should pass and the third
// should be rejected.
assert!(gate.admit("greedy", ()).is_ok());
assert!(gate.admit("greedy", ()).is_ok());
let err = gate
.admit("greedy", ())
.expect_err("third high-cost call must be rejected");
assert!(matches!(err, ArqSlaError::SlaExceeded { .. }));
}
#[test]
fn test_query_timeout_propagates() {
let registry = TenantConfigRegistry::new();
registry.register(
TenantConfig::new("timed", SlaClass::Gold)
.with_query_timeout(Duration::from_millis(750)),
);
let gate = ArqSlaGate::new(registry);
let admitted = gate.admit("timed", ()).expect("admit");
assert_eq!(admitted.timeout, Some(Duration::from_millis(750)));
}
#[test]
fn test_dispatcher_pending_count_tracks_enqueue_dequeue() {
let registry = registry_with(&[("a", SlaClass::Silver)]);
let gate = ArqSlaGate::new(registry);
let admitted = gate.admit("a", ()).expect("admit");
gate.enqueue(&admitted, "j1");
gate.enqueue(&admitted, "j2");
assert_eq!(gate.pending_count(), 2);
let _ = gate.next_dispatch();
assert_eq!(gate.pending_count(), 1);
let _ = gate.next_dispatch();
assert_eq!(gate.pending_count(), 0);
}
#[test]
fn test_sla_class_display_format() {
let display: SlaClassDisplay = SlaClass::Platinum.into();
assert_eq!(format!("{display}"), "platinum");
}
#[test]
fn test_pre_registered_in_registry_admitted_immediately() {
let registry = TenantConfigRegistry::new();
registry.register(TenantConfig::new("preexisting", SlaClass::Gold));
let gate = ArqSlaGate::new(registry);
// First admission should succeed because gate's `new()` registered the
// tenant with the AdmissionController during construction.
let admitted = gate
.admit("preexisting", ())
.expect("pre-registered tenant must be in admission controller");
assert_eq!(admitted.sla_class, SlaClass::Gold);
}
}