icydb-core 0.226.0

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
Documentation
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Module: db::session::request
//! Responsibility: one monotonic aggregate execution scope per request entry.
//! Does not own: caller authorization, per-execution limits, or physical charging sites.
//! Boundary: request roots issue shared scope handles that every derived session retains.

use std::{
    cell::{Cell, RefCell},
    rc::Rc,
};

use crate::db::executor::budget::{
    ExecutionBudgetExceeded, HardExecutionBudget, HardExecutionContext,
    HardExecutionFailureHeadroom, resource_index,
};
#[cfg(feature = "diagnostics")]
use crate::db::{
    diagnostics::{
        RequestDiagnosticResourceUsage, RequestDiagnostics, RequestDiagnosticsState,
        RequestQueryPlanEvidence,
    },
    session::query::QueryPlanCacheAttribution,
};
use icydb_diagnostic_code::{DiagnosticExecutionBudgetResource, DiagnosticExecutionBudgetScope};

const REQUEST_FAILURE_HEADROOM: HardExecutionFailureHeadroom =
    HardExecutionFailureHeadroom::new(500_000_000, 64 * 1_024);
const REQUEST_HARD_BUDGET: HardExecutionBudget = HardExecutionBudget::new(
    [
        256,                 // query executions
        1_024,               // planning operations
        256,                 // plan compilations
        250_000,             // key/index entries visited
        250_000,             // rows visited
        128 * 1_024 * 1_024, // stored bytes read
        16_000_000,          // predicate/expression steps
        16_000_000,          // nested value steps
        128 * 1_024 * 1_024, // decoded bytes
        128 * 1_024 * 1_024, // materialized bytes
        250_000,             // sort entries
        32_000_000,          // sort comparisons
        128 * 1_024 * 1_024, // sort temporary bytes
        100_000,             // group/distinct entries
        128 * 1_024 * 1_024, // group/distinct state bytes
        1_000_000,           // cursor steps
        128 * 1_024 * 1_024, // temporary bytes
        1_000_000,           // diagnostic steps
        100_000,             // result rows
        64 * 1_024 * 1_024,  // result bytes
        4_500_000_000,       // instruction units
    ],
    REQUEST_FAILURE_HEADROOM,
);

thread_local! {
    static CURRENT_REQUEST_SCOPE: RefCell<Option<RequestExecutionScope>> =
        const { RefCell::new(None) };
}

/// Non-cloneable capability owning one request's aggregate database counters.
///
/// Construct this once at request entry and derive every database session used
/// by the request from it. Sessions retain the counters, so dropping this
/// value does not reset work already attached to a derived session.
pub struct RequestExecutionRoot {
    scope: RequestExecutionScope,
}

impl RequestExecutionRoot {
    /// Mint the fixed production request profile.
    ///
    /// This constructor is runtime wiring for generated and guarded facade
    /// request entry. It is intentionally not a budget-policy configuration
    /// surface.
    #[doc(hidden)]
    #[must_use]
    pub fn __new_runtime_root() -> Self {
        Self::from_budget(REQUEST_HARD_BUDGET)
    }

    /// Reuse the active synchronous request scope or mint the production root.
    ///
    /// This is runtime wiring for the public scoped-entry helper. Re-entering
    /// that helper inside one active database segment must retain the existing
    /// counters instead of creating a budget-reset escape hatch.
    #[doc(hidden)]
    #[must_use]
    pub fn __new_or_current_runtime_root() -> Self {
        current_request_scope().map_or_else(Self::__new_runtime_root, |scope| Self { scope })
    }

    /// Make this root current only while one synchronous call tree executes.
    ///
    /// The previous scope is restored before this method returns, including
    /// during host unwinding. The scope is never retained ambiently across an
    /// async suspension point.
    #[doc(hidden)]
    pub fn __with_current_scope<T>(&self, run: impl FnOnce() -> T) -> T {
        let _guard = CurrentRequestScopeGuard::enter(self.scope());
        run()
    }

    /// Whether no root is active or this root owns the active counters.
    ///
    /// Generated facade wiring uses this before accepting an explicit root.
    /// A different active root would reset aggregate accounting inside a
    /// request and must fail closed.
    #[doc(hidden)]
    #[must_use]
    pub fn __is_compatible_with_current(&self) -> bool {
        match current_request_scope() {
            Some(current) => current.same_counters(&self.scope),
            None => true,
        }
    }

    /// Whether this root owns the counters currently installed for this poll.
    #[doc(hidden)]
    #[must_use]
    pub fn __is_current(&self) -> bool {
        current_request_scope().is_some_and(|current| current.same_counters(&self.scope))
    }

    #[cfg(test)]
    #[must_use]
    pub(in crate::db) fn new_for_tests(budget: HardExecutionBudget) -> Self {
        Self::from_budget(budget)
    }

    fn from_budget(budget: HardExecutionBudget) -> Self {
        Self {
            scope: RequestExecutionScope {
                counters: Rc::new(RequestExecutionCounters {
                    budget,
                    observed: [const { Cell::new(0) };
                        DiagnosticExecutionBudgetResource::ALL.len()],
                    #[cfg(feature = "diagnostics")]
                    diagnostics: RefCell::new(None),
                }),
            },
        }
    }

    pub(in crate::db) fn scope(&self) -> RequestExecutionScope {
        self.scope.clone()
    }

    #[cfg(test)]
    #[must_use]
    pub(in crate::db) fn observed(&self, resource: DiagnosticExecutionBudgetResource) -> u64 {
        self.scope.observed(resource)
    }
}

pub(in crate::db) fn current_request_scope() -> Option<RequestExecutionScope> {
    CURRENT_REQUEST_SCOPE.with(|current| current.borrow().clone())
}

struct CurrentRequestScopeGuard {
    previous: Option<RequestExecutionScope>,
}

impl CurrentRequestScopeGuard {
    fn enter(scope: RequestExecutionScope) -> Self {
        let previous = CURRENT_REQUEST_SCOPE.with(|current| current.replace(Some(scope)));
        Self { previous }
    }
}

impl Drop for CurrentRequestScopeGuard {
    fn drop(&mut self) {
        CURRENT_REQUEST_SCOPE.with(|current| {
            current.replace(self.previous.take());
        });
    }
}

/// Shared internal handle retained by every session derived from one root.
#[derive(Clone)]
pub(in crate::db) struct RequestExecutionScope {
    counters: Rc<RequestExecutionCounters>,
}

impl RequestExecutionScope {
    fn same_counters(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.counters, &other.counters)
    }

    pub(in crate::db) fn charge(
        &self,
        context: HardExecutionContext,
        resource: DiagnosticExecutionBudgetResource,
        amount: u64,
    ) -> Result<(), ExecutionBudgetExceeded> {
        self.counters.charge(context, resource, amount)
    }

    #[cfg(feature = "diagnostics")]
    pub(in crate::db) fn enable_diagnostics(&self) -> bool {
        let mut diagnostics = self.counters.diagnostics.borrow_mut();
        if diagnostics.is_some() {
            return false;
        }
        *diagnostics = Some(RequestDiagnosticsState::default());
        true
    }

    #[cfg(feature = "diagnostics")]
    pub(in crate::db) fn diagnostics_enabled(&self) -> bool {
        self.counters.diagnostics.borrow().is_some()
    }

    #[cfg(feature = "diagnostics")]
    pub(in crate::db) fn diagnostics_snapshot(&self) -> Option<RequestDiagnostics> {
        let mut snapshot = self
            .counters
            .diagnostics
            .borrow()
            .as_ref()
            .map(RequestDiagnosticsState::snapshot)?;
        let response_bytes = request_diagnostics_bytes_estimate(&snapshot);
        let context = HardExecutionContext::new(
            DiagnosticExecutionBudgetScope::Request,
            icydb_diagnostic_code::DiagnosticExecutionLane::TrustedRead,
            0,
        );
        let charged = self.counters.charge_fail_soft(
            context,
            DiagnosticExecutionBudgetResource::DiagnosticSteps,
            1,
        ) && self.counters.charge_fail_soft(
            context,
            DiagnosticExecutionBudgetResource::ResultBytes,
            response_bytes,
        );
        if !charged {
            self.suppress_diagnostics(1);
            snapshot.shapes.clear();
            snapshot.warnings.clear();
            snapshot.suppressed_observations = snapshot.suppressed_observations.saturating_add(1);
        }
        Some(snapshot)
    }

    #[cfg(feature = "diagnostics")]
    pub(in crate::db) fn record_query_plan(
        &self,
        evidence: RequestQueryPlanEvidence,
        cache: QueryPlanCacheAttribution,
    ) {
        if !self.diagnostics_enabled() {
            return;
        }
        let context = HardExecutionContext::new(
            DiagnosticExecutionBudgetScope::Request,
            icydb_diagnostic_code::DiagnosticExecutionLane::TrustedRead,
            evidence.normalized_shape_fingerprint_prefix,
        );
        let retained_bytes = evidence.retained_bytes_estimate();
        let diagnostic_steps = evidence.work_steps_estimate();
        if !self.counters.charge_fail_soft(
            context,
            DiagnosticExecutionBudgetResource::DiagnosticSteps,
            diagnostic_steps,
        ) || !self.counters.charge_fail_soft(
            context,
            DiagnosticExecutionBudgetResource::TemporaryBytes,
            retained_bytes,
        ) {
            self.suppress_diagnostics(1);
            return;
        }
        if let Some(diagnostics) = self.counters.diagnostics.borrow_mut().as_mut() {
            diagnostics.observe_plan(evidence, cache.hits, cache.misses);
        }
    }

    #[cfg(feature = "diagnostics")]
    pub(in crate::db) fn record_execution(
        &self,
        context: HardExecutionContext,
        usage: RequestDiagnosticResourceUsage,
    ) {
        if !self.diagnostics_enabled() {
            return;
        }
        if !self.counters.charge_fail_soft(
            context,
            DiagnosticExecutionBudgetResource::DiagnosticSteps,
            1,
        ) {
            self.suppress_diagnostics(1);
            return;
        }
        if let Some(diagnostics) = self.counters.diagnostics.borrow_mut().as_mut() {
            diagnostics.observe_execution(context.normalized_shape_fingerprint_prefix(), usage);
        }
    }

    #[cfg(feature = "diagnostics")]
    pub(in crate::db) fn record_exact_key_hashes(
        &self,
        context: HardExecutionContext,
        hashes: &[[u8; 16]],
    ) {
        if hashes.is_empty() || !self.diagnostics_enabled() {
            return;
        }
        let steps = u64::try_from(hashes.len()).unwrap_or(u64::MAX);
        let retained_bytes = steps.saturating_mul(16);
        if !self.counters.charge_fail_soft(
            context,
            DiagnosticExecutionBudgetResource::DiagnosticSteps,
            steps,
        ) || !self.counters.charge_fail_soft(
            context,
            DiagnosticExecutionBudgetResource::TemporaryBytes,
            retained_bytes,
        ) {
            self.suppress_diagnostics(steps);
            return;
        }
        if let Some(diagnostics) = self.counters.diagnostics.borrow_mut().as_mut() {
            diagnostics
                .observe_exact_key_hashes(context.normalized_shape_fingerprint_prefix(), hashes);
        }
    }

    #[cfg(feature = "diagnostics")]
    fn suppress_diagnostics(&self, count: u64) {
        if let Some(diagnostics) = self.counters.diagnostics.borrow_mut().as_mut() {
            diagnostics.suppress(count);
        }
    }

    #[cfg(test)]
    fn observed(&self, resource: DiagnosticExecutionBudgetResource) -> u64 {
        self.counters.observed[resource_index(resource)].get()
    }
}

struct RequestExecutionCounters {
    budget: HardExecutionBudget,
    observed: [Cell<u64>; DiagnosticExecutionBudgetResource::ALL.len()],
    #[cfg(feature = "diagnostics")]
    diagnostics: RefCell<Option<RequestDiagnosticsState>>,
}

impl RequestExecutionCounters {
    fn charge(
        &self,
        context: HardExecutionContext,
        resource: DiagnosticExecutionBudgetResource,
        amount: u64,
    ) -> Result<(), ExecutionBudgetExceeded> {
        let index = resource_index(resource);
        let counter = &self.observed[index];
        let current = counter.get();
        let (observed, overflowed) = current.overflowing_add(amount);
        let observed = if overflowed { u64::MAX } else { observed };
        counter.set(observed);
        let limit = self.budget.limit(resource);
        if overflowed || observed > limit {
            return Err(ExecutionBudgetExceeded::new(
                resource,
                limit,
                observed,
                context.with_scope(DiagnosticExecutionBudgetScope::Request),
            ));
        }

        Ok(())
    }

    #[cfg(feature = "diagnostics")]
    fn charge_fail_soft(
        &self,
        _context: HardExecutionContext,
        resource: DiagnosticExecutionBudgetResource,
        amount: u64,
    ) -> bool {
        let index = resource_index(resource);
        let counter = &self.observed[index];
        let current = counter.get();
        let limit = self.budget.limit(resource);
        let Some(observed) = current.checked_add(amount) else {
            counter.set(limit);
            return false;
        };
        if observed > limit {
            counter.set(limit);
            return false;
        }
        counter.set(observed);
        true
    }
}

#[cfg(feature = "diagnostics")]
fn request_diagnostics_bytes_estimate(diagnostics: &RequestDiagnostics) -> u64 {
    let shape_bytes = diagnostics.shapes.iter().fold(0_u64, |total, shape| {
        total
            .saturating_add(u64::try_from(shape.entity.len()).unwrap_or(u64::MAX))
            .saturating_add(
                u64::try_from(shape.selected_index.as_ref().map_or(0, String::len))
                    .unwrap_or(u64::MAX),
            )
            .saturating_add(
                shape
                    .residual_fields
                    .iter()
                    .chain(shape.compound_index_candidate.iter())
                    .fold(0_u64, |bytes, field| {
                        bytes.saturating_add(u64::try_from(field.len()).unwrap_or(u64::MAX))
                    }),
            )
            .saturating_add(256)
    });
    diagnostics
        .warnings
        .iter()
        .fold(shape_bytes, |total, warning| {
            total
                .saturating_add(u64::try_from(warning.message.len()).unwrap_or(u64::MAX))
                .saturating_add(32)
        })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn synchronous_scope_is_installed_then_removed() {
        assert!(current_request_scope().is_none());
        let root = RequestExecutionRoot::new_for_tests(REQUEST_HARD_BUDGET);

        root.__with_current_scope(|| {
            assert!(current_request_scope().is_some());
        });

        assert!(current_request_scope().is_none());
    }

    #[test]
    fn nested_entry_reuses_current_counters() {
        let resource = DiagnosticExecutionBudgetResource::QueryExecutions;
        let budget = REQUEST_HARD_BUDGET.with_limit_for_tests(resource, 1);
        let root = RequestExecutionRoot::new_for_tests(budget);
        let context = HardExecutionContext::new(
            DiagnosticExecutionBudgetScope::Execution,
            icydb_diagnostic_code::DiagnosticExecutionLane::PublicRead,
            0,
        );

        root.__with_current_scope(|| {
            let nested = RequestExecutionRoot::__new_or_current_runtime_root();
            nested
                .scope()
                .charge(context, resource, 1)
                .expect("first nested charge should fit");
            let exhausted = root
                .scope()
                .charge(context, resource, 1)
                .expect_err("parent should observe the nested charge");

            assert_eq!(exhausted.scope(), DiagnosticExecutionBudgetScope::Request);
            assert_eq!(exhausted.observed(), 2);
        });
    }

    #[test]
    fn explicit_root_compatibility_rejects_a_different_active_root() {
        let first = RequestExecutionRoot::new_for_tests(REQUEST_HARD_BUDGET);
        let second = RequestExecutionRoot::new_for_tests(REQUEST_HARD_BUDGET);

        assert!(first.__is_compatible_with_current());
        assert!(!first.__is_current());
        first.__with_current_scope(|| {
            assert!(first.__is_current());
            assert!(first.__is_compatible_with_current());
            assert!(!second.__is_compatible_with_current());
        });
        assert!(second.__is_compatible_with_current());
    }

    #[cfg(feature = "diagnostics")]
    #[test]
    fn request_diagnostic_work_is_charged_to_the_shared_root() {
        let root = RequestExecutionRoot::new_for_tests(REQUEST_HARD_BUDGET);
        let scope = root.scope();
        assert!(scope.enable_diagnostics());
        scope.record_query_plan(
            RequestQueryPlanEvidence::bounded(
                12,
                "Token",
                crate::db::RequestDiagnosticAccessPath::ByKey,
                None,
                Vec::new(),
                Vec::new(),
                vec![[1; 16]],
            ),
            QueryPlanCacheAttribution {
                hits: 1,
                ..QueryPlanCacheAttribution::default()
            },
        );

        assert_eq!(
            root.observed(DiagnosticExecutionBudgetResource::DiagnosticSteps),
            2,
        );
        assert!(root.observed(DiagnosticExecutionBudgetResource::TemporaryBytes) >= 21);
    }

    #[cfg(feature = "diagnostics")]
    #[test]
    fn exhausted_diagnostic_allowance_suppresses_detail_without_an_error() {
        let budget = REQUEST_HARD_BUDGET
            .with_limit_for_tests(DiagnosticExecutionBudgetResource::DiagnosticSteps, 0);
        let root = RequestExecutionRoot::new_for_tests(budget);
        let scope = root.scope();
        assert!(scope.enable_diagnostics());
        scope.record_query_plan(
            RequestQueryPlanEvidence::bounded(
                13,
                "Token",
                crate::db::RequestDiagnosticAccessPath::ByKey,
                None,
                Vec::new(),
                Vec::new(),
                Vec::new(),
            ),
            QueryPlanCacheAttribution::default(),
        );

        let snapshot = scope
            .diagnostics_snapshot()
            .expect("enabled diagnostics should still return a bounded snapshot");
        assert!(snapshot.shapes.is_empty());
        assert!(snapshot.suppressed_observations >= 2);
    }
}