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
//! Module: session::mutation_job
//! Responsibility: charged mutation-job start, state load, phase dispatch, and terminal acknowledgement.
//! Does not own: SQL lowering, Forward/Verify execution, or authorization.
//! Boundary: trusted session API -> excluded mutation progress record.

use crate::{
    db::{
        DbSession, MutationJobError, MutationJobId, MutationJobState,
        executor::budget::{ExecutionBudgetExceeded, HardExecutionContext},
        integrity::with_mutation_progress_store,
    },
    traits::CanisterKind,
};
use icydb_diagnostic_code::{
    DiagnosticExecutionBudgetResource, DiagnosticExecutionBudgetScope, DiagnosticExecutionLane,
};

#[cfg(feature = "sql")]
use crate::db::{
    MutationJobAdvanceReceipt, MutationJobAdvanceRequest, MutationJobPhase,
    integrity::InsertMutationJobResult,
    mutation_job::{CanonicalMutationIntent, MutationJobRecord},
};

#[cfg(feature = "sql")]
const MUTATION_JOB_START_SHAPE: u64 = 0x6d75_7461_7465_0100;
const MUTATION_JOB_LOAD_SHAPE: u64 = 0x6d75_7461_7465_0101;
const MUTATION_JOB_ACKNOWLEDGE_SHAPE: u64 = 0x6d75_7461_7465_0102;
#[cfg(feature = "sql")]
const MUTATION_JOB_ADVANCE_SHAPE: u64 = 0x6d75_7461_7465_0103;

impl<C: CanisterKind> DbSession<C> {
    /// Start one durable trusted fixed SQL mutation job.
    ///
    /// SQL is parsed and admitted exactly once for a new identity. The
    /// catalog-native intent and initial engine checkpoint are durably retained
    /// before this method returns, and no target row is read or mutated.
    /// Repeating the same canonical request returns the retained state without
    /// replacing its operation timestamp or resetting progress.
    #[cfg(feature = "sql")]
    pub fn start_trusted_sql_mutation_job(
        &self,
        job_id: MutationJobId,
        sql: &str,
    ) -> Result<MutationJobState, MutationJobError> {
        job_id.validate()?;
        self.charge_mutation_job_operation(
            DiagnosticExecutionLane::Mutation,
            MUTATION_JOB_START_SHAPE,
        )?;
        let prepared = self.prepare_mutation_job_start(job_id, sql)?;
        let submitted_intent = CanonicalMutationIntent::decode(&prepared.canonical_intent)?;
        let submitted = MutationJobRecord::new(
            job_id,
            prepared.canonical_intent,
            prepared.engine_continuation,
        )?;
        with_mutation_progress_store::<C, _>(|store| match store.insert_mutation(&submitted)? {
            InsertMutationJobResult::Inserted => Ok(submitted.state().clone()),
            InsertMutationJobResult::Occupied(retained) => {
                resolve_occupied_mutation_job_start(&retained, &submitted_intent)
            }
        })
    }

    /// Load the bounded public state for one retained mutation job.
    pub fn mutation_job_state(
        &self,
        job_id: MutationJobId,
    ) -> Result<MutationJobState, MutationJobError> {
        self.charge_mutation_job_operation(
            DiagnosticExecutionLane::TrustedRead,
            MUTATION_JOB_LOAD_SHAPE,
        )?;
        with_mutation_progress_store::<C, _>(|store| store.load_mutation(job_id))
            .map(|record| record.state().clone())
    }

    /// Advance one durable mutation job through one bounded engine-owned step.
    ///
    /// The request carries only job identity, expected sequence, and a replay
    /// key. SQL and continuation bytes remain private IcyDB custody.
    #[cfg(feature = "sql")]
    pub fn advance_trusted_mutation_job(
        &self,
        request: &MutationJobAdvanceRequest,
    ) -> Result<MutationJobAdvanceReceipt, MutationJobError> {
        let retained =
            with_mutation_progress_store::<C, _>(|store| store.load_mutation(request.job_id))?;
        if let Some(receipt) = retained.exact_replay(request)? {
            return Ok(receipt.clone());
        }
        self.charge_mutation_job_operation(
            DiagnosticExecutionLane::Mutation,
            MUTATION_JOB_ADVANCE_SHAPE,
        )?;
        retained.ensure_can_advance(request)?;
        match retained.state().phase {
            MutationJobPhase::Forward => self.advance_mutation_job_forward(&retained, request),
            MutationJobPhase::Verify => self.advance_mutation_job_verify(&retained, request),
        }
    }

    /// Remove one terminal mutation job after its result has been consumed.
    ///
    /// Repeating acknowledgement after a lost response succeeds when the job
    /// is already absent. Active jobs and stale terminal sequences fail closed.
    pub fn acknowledge_mutation_job(
        &self,
        job_id: MutationJobId,
        expected_terminal_sequence: u64,
    ) -> Result<(), MutationJobError> {
        self.charge_mutation_job_operation(
            DiagnosticExecutionLane::Mutation,
            MUTATION_JOB_ACKNOWLEDGE_SHAPE,
        )?;
        with_mutation_progress_store::<C, _>(|store| {
            store.acknowledge_mutation(job_id, expected_terminal_sequence)
        })
    }

    fn charge_mutation_job_operation(
        &self,
        lane: DiagnosticExecutionLane,
        shape: u64,
    ) -> Result<(), MutationJobError> {
        self.db
            .request_execution_scope()
            .charge(
                HardExecutionContext::new(DiagnosticExecutionBudgetScope::Execution, lane, shape),
                DiagnosticExecutionBudgetResource::QueryExecutions,
                1,
            )
            .map_err(mutation_job_execution_budget_error)
    }
}

#[cfg(feature = "sql")]
fn resolve_occupied_mutation_job_start(
    retained: &MutationJobRecord,
    submitted_intent: &CanonicalMutationIntent,
) -> Result<MutationJobState, MutationJobError> {
    let retained_intent = CanonicalMutationIntent::decode(retained.canonical_intent())?;
    if retained_intent.same_start_request(submitted_intent) {
        return Ok(retained.state().clone());
    }
    if !retained_intent.same_authority(submitted_intent) {
        return Err(MutationJobError::AuthorityMismatch);
    }
    Err(MutationJobError::IdentityConflict)
}

const fn mutation_job_execution_budget_error(error: ExecutionBudgetExceeded) -> MutationJobError {
    MutationJobError::ExecutionBudgetExceeded {
        resource: error.resource().raw(),
        limit: error.limit(),
        observed: error.observed(),
        scope: error.scope().raw(),
        lane: error.lane().raw(),
        normalized_shape_fingerprint_prefix: error.normalized_shape_fingerprint_prefix(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        db::{
            MutationJobAdvanceRequest, MutationJobIdempotencyKey, MutationJobPhase,
            MutationJobRestartReason, MutationJobStatus, RequestExecutionRoot, StoreRegistry,
            mutation_job::{MutationJobRecord, MutationJobTransition},
        },
        traits::Path,
    };
    #[cfg(feature = "sql")]
    use crate::{
        db::{
            data::{AcceptedFixedUpdatePatch, FieldSlot},
            executor::budget::{HardExecutionBudget, HardExecutionFailureHeadroom},
            query::plan::expr::{BinaryOp, Expr, FieldId},
        },
        types::Timestamp,
        value::Value,
    };

    struct TestCanister;

    impl Path for TestCanister {
        const PATH: &'static str = "db::session::mutation_job::tests::Canister";
    }

    impl CanisterKind for TestCanister {
        const COMMIT_MEMORY_ID: u8 = 244;
        const COMMIT_STABLE_KEY: &'static str = "icydb.test.mutation-job.commit.v1";
        const STARTUP_MEMORY_ID: u8 = 246;
        const STARTUP_STABLE_KEY: &'static str = "icydb.test.mutation-job.startup.control.v1";
        const INTEGRITY_PROGRESS_MEMORY_ID: u8 = 245;
        const INTEGRITY_PROGRESS_STABLE_KEY: &'static str = "icydb.test.mutation-job.progress.v1";
    }

    thread_local! {
        static STORE_REGISTRY: StoreRegistry = StoreRegistry::new();
    }

    fn job_id() -> MutationJobId {
        MutationJobId::try_from_bytes([19; 32]).expect("nonzero mutation job id should admit")
    }

    fn session() -> DbSession<TestCanister> {
        let root = RequestExecutionRoot::__new_runtime_root();
        DbSession::new(&STORE_REGISTRY, &root)
    }

    #[cfg(feature = "sql")]
    fn exhausted_session() -> DbSession<TestCanister> {
        let budget =
            HardExecutionBudget::uniform_for_tests(0, HardExecutionFailureHeadroom::new(500, 256));
        let root = RequestExecutionRoot::new_for_tests(budget);
        DbSession::new(&STORE_REGISTRY, &root)
    }

    #[test]
    fn session_load_and_terminal_acknowledgement_preserve_the_store_contract() {
        let initial = MutationJobRecord::new(job_id(), vec![1, 2], vec![3])
            .expect("bounded initial record should admit");
        with_mutation_progress_store::<TestCanister, _>(|store| {
            store.insert_mutation(&initial).map(|_| ())
        })
        .expect("initial record should insert");

        let session = session();
        assert_eq!(
            session.mutation_job_state(job_id()),
            Ok(initial.state().clone())
        );
        assert_eq!(
            session.acknowledge_mutation_job(job_id(), 0),
            Err(MutationJobError::Active),
        );

        let request = MutationJobAdvanceRequest::new(
            job_id(),
            0,
            MutationJobIdempotencyKey::new("authority-drift")
                .expect("bounded idempotency key should admit"),
        );
        let (terminal, _) = initial
            .apply_transition(
                &request,
                MutationJobTransition::new(
                    MutationJobStatus::RestartRequired(
                        MutationJobRestartReason::AcceptedSchemaChanged,
                    ),
                    MutationJobPhase::Forward,
                    Vec::new(),
                    0,
                    0,
                    0,
                ),
            )
            .expect("terminal restart receipt should admit");
        with_mutation_progress_store::<TestCanister, _>(|store| store.replace_mutation(&terminal))
            .expect("terminal record should replace active state");

        assert_eq!(
            session.acknowledge_mutation_job(job_id(), 0),
            Err(MutationJobError::StaleSequence {
                expected: 0,
                actual: 1,
            }),
        );
        assert_eq!(session.acknowledge_mutation_job(job_id(), 1), Ok(()));
        assert_eq!(session.acknowledge_mutation_job(job_id(), 1), Ok(()));
        assert_eq!(
            session.mutation_job_state(job_id()),
            Err(MutationJobError::NotFound),
        );
    }

    #[cfg(feature = "sql")]
    fn canonical_intent(
        authority: u8,
        scope_value: u64,
        timestamp: i64,
    ) -> CanonicalMutationIntent {
        let scope = Expr::Binary {
            op: BinaryOp::Eq,
            left: Box::new(Expr::Field(FieldId::new("collection_id"))),
            right: Box::new(Expr::Literal(Value::Nat64(scope_value))),
        };
        let patch = AcceptedFixedUpdatePatch::from_canonical_fields(vec![(
            FieldSlot::from_validated_index(1),
            vec![3, 4, 5],
        )])
        .expect("fixed patch should admit");
        CanonicalMutationIntent::new(
            [authority; 16],
            [authority; 32],
            "journaled".to_string(),
            "schema::Token".to_string(),
            7,
            11,
            1,
            [authority; 16],
            &scope,
            &patch,
            Timestamp::from_millis(timestamp),
            17,
        )
        .expect("canonical intent should admit")
    }

    #[cfg(feature = "sql")]
    #[test]
    fn occupied_start_distinguishes_replay_authority_drift_and_identity_conflict() {
        let retained_intent = canonical_intent(1, 7, 100);
        let retained = MutationJobRecord::new(
            job_id(),
            retained_intent.encode().expect("intent should encode"),
            vec![9],
        )
        .expect("record should admit");

        assert_eq!(
            resolve_occupied_mutation_job_start(&retained, &canonical_intent(1, 7, 200)),
            Ok(retained.state().clone()),
        );
        assert_eq!(
            resolve_occupied_mutation_job_start(&retained, &canonical_intent(2, 7, 200)),
            Err(MutationJobError::AuthorityMismatch),
        );
        assert_eq!(
            resolve_occupied_mutation_job_start(&retained, &canonical_intent(1, 8, 200)),
            Err(MutationJobError::IdentityConflict),
        );
    }

    #[cfg(feature = "sql")]
    #[test]
    fn aggregate_budget_exhaustion_does_not_advance_durable_state() {
        let initial = MutationJobRecord::new(job_id(), vec![1, 2], vec![3])
            .expect("bounded initial record should admit");
        with_mutation_progress_store::<TestCanister, _>(|store| {
            store.insert_mutation(&initial).map(|_| ())
        })
        .expect("initial record should insert");
        let request = MutationJobAdvanceRequest::new(
            job_id(),
            0,
            MutationJobIdempotencyKey::new("budget-exhausted")
                .expect("bounded idempotency key should admit"),
        );

        assert!(matches!(
            exhausted_session().advance_trusted_mutation_job(&request),
            Err(MutationJobError::ExecutionBudgetExceeded {
                limit: 0,
                observed: 1,
                ..
            })
        ));
        assert_eq!(
            session().mutation_job_state(job_id()),
            Ok(initial.state().clone()),
        );
    }

    #[cfg(feature = "sql")]
    #[test]
    fn exact_replay_precedes_advance_budget_accounting() {
        let initial = MutationJobRecord::new(job_id(), vec![1, 2], vec![3])
            .expect("bounded initial record should admit");
        let request = MutationJobAdvanceRequest::new(
            job_id(),
            0,
            MutationJobIdempotencyKey::new("lost-response")
                .expect("bounded idempotency key should admit"),
        );
        let (advanced, receipt) = initial
            .apply_transition(
                &request,
                MutationJobTransition::new(
                    MutationJobStatus::Active,
                    MutationJobPhase::Forward,
                    vec![4],
                    1,
                    1,
                    0,
                ),
            )
            .expect("bounded successor should admit");
        with_mutation_progress_store::<TestCanister, _>(|store| {
            store.insert_mutation(&advanced).map(|_| ())
        })
        .expect("advanced record should insert");

        assert_eq!(
            exhausted_session().advance_trusted_mutation_job(&request),
            Ok(receipt),
        );
        assert_eq!(
            session().mutation_job_state(job_id()),
            Ok(advanced.state().clone()),
        );
    }
}