canic-core 0.99.25

Canic — a canister orchestration and management toolkit for the Internet Computer
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
//! Module: ops::storage::placement::binding
//!
//! Responsibility: provide deterministic binding registry claim and binding operations.
//! Does not own: placement policy, provisioning workflow, or endpoint DTOs.
//! Boundary: storage ops facade over stable binding registry records.

#[cfg(test)]
mod tests;

use crate::{
    InternalError,
    dto::placement::binding::{
        PlacementBindingRegistryEntry, PlacementBindingRegistryResponse,
        PlacementBindingStatusResponse,
    },
    ops::{prelude::*, storage::StorageOpsError},
    storage::stable::placement_binding::{
        PlacementBindingEntryRecord, PlacementBindingKey, PlacementBindingRegistry,
    },
};
use thiserror::Error as ThisError;

///
/// PlacementBindingRegistryOpsError
///
/// Typed storage failure for binding registry claim and binding operations.
///

#[derive(Debug, ThisError)]
pub enum PlacementBindingRegistryOpsError {
    #[error("invalid binding key: {0}")]
    InvalidKey(String),

    #[error("binding key '{key_value}' in pool '{pool}' already bound to instance {pid}")]
    KeyBound {
        pool: String,
        key_value: String,
        pid: Principal,
    },

    #[error(
        "binding key '{key_value}' in pool '{pool}' is pending for provisional child {expected}, not {actual}"
    )]
    ProvisionalPidMismatch {
        pool: String,
        key_value: String,
        expected: Principal,
        actual: Principal,
    },
}

impl From<PlacementBindingRegistryOpsError> for InternalError {
    fn from(err: PlacementBindingRegistryOpsError) -> Self {
        StorageOpsError::from(err).into()
    }
}

///
/// PlacementBindingRegistryOps
///
/// Storage-ops facade for binding registry claim and binding operations.
///

pub struct PlacementBindingRegistryOps;

///
/// PlacementBindingEntryState
///
/// Internal binding registry state view used by placement workflows.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PlacementBindingEntryState {
    Pending {
        claim_id: u64,
        owner_pid: Principal,
        created_at: u64,
        provisional_pid: Option<Principal>,
    },
    Bound {
        instance_pid: Principal,
        bound_at: u64,
    },
}

///
/// PlacementBindingPendingClaim
///
/// Pending binding claim returned when a caller owns a logical key reservation.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PlacementBindingPendingClaim {
    pub claim_id: u64,
    pub owner_pid: Principal,
    pub created_at: u64,
}

///
/// PlacementBindingClaimResult
///
/// Result of attempting to claim one logical binding key.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PlacementBindingClaimResult {
    Bound {
        instance_pid: Principal,
        bound_at: u64,
    },
    PendingExisting {
        claim_id: u64,
        owner_pid: Principal,
        created_at: u64,
        provisional_pid: Option<Principal>,
    },
    Claimed(PlacementBindingPendingClaim),
}

///
/// PlacementBindingReleaseResult
///
/// Result of attempting to release a stale pending binding claim.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PlacementBindingReleaseResult {
    Missing,
    Bound {
        instance_pid: Principal,
        bound_at: u64,
    },
    PendingRetained {
        owner_pid: Principal,
        created_at: u64,
        provisional_pid: Option<Principal>,
    },
    ReleasedStalePending {
        owner_pid: Principal,
        created_at: u64,
        provisional_pid: Option<Principal>,
    },
}

impl PlacementBindingRegistryOps {
    pub const PENDING_TTL_SECS: u64 = 300;

    // Claim one logical key for in-progress instance creation before async work begins.
    pub fn claim_pending(
        pool: &str,
        key_value: &str,
        owner_pid: Principal,
        claim_id: u64,
        created_at: u64,
    ) -> Result<PlacementBindingClaimResult, InternalError> {
        let key = PlacementBindingKey::try_new(pool, key_value)
            .map_err(PlacementBindingRegistryOpsError::InvalidKey)?;

        match PlacementBindingRegistry::get(&key) {
            Some(PlacementBindingEntryRecord::Bound {
                instance_pid,
                bound_at,
            }) => Ok(PlacementBindingClaimResult::Bound {
                instance_pid,
                bound_at,
            }),

            Some(PlacementBindingEntryRecord::Pending {
                claim_id,
                owner_pid: existing_owner_pid,
                created_at: existing_created_at,
                provisional_pid,
            }) => Ok(PlacementBindingClaimResult::PendingExisting {
                claim_id,
                owner_pid: existing_owner_pid,
                created_at: existing_created_at,
                provisional_pid,
            }),

            None => {
                PlacementBindingRegistry::insert(
                    key,
                    PlacementBindingEntryRecord::Pending {
                        claim_id,
                        owner_pid,
                        created_at,
                        provisional_pid: None,
                    },
                );

                Ok(PlacementBindingClaimResult::Claimed(
                    PlacementBindingPendingClaim {
                        claim_id,
                        owner_pid,
                        created_at,
                    },
                ))
            }
        }
    }

    // Read one entry with its internal claim state for workflow classification.
    #[must_use]
    pub fn lookup_state(pool: &str, key_value: &str) -> Option<PlacementBindingEntryState> {
        let key = PlacementBindingKey::try_new(pool, key_value).ok()?;
        PlacementBindingRegistry::get(&key).map(entry_to_state)
    }

    // Attach the created child pid only if the caller still owns the current pending claim.
    pub fn set_provisional_pid_if_claim_matches(
        pool: &str,
        key_value: &str,
        expected_claim_id: u64,
        provisional_pid: Principal,
    ) -> Result<bool, InternalError> {
        let key = PlacementBindingKey::try_new(pool, key_value)
            .map_err(PlacementBindingRegistryOpsError::InvalidKey)?;
        let entry = PlacementBindingRegistry::get(&key);

        let Some(PlacementBindingEntryRecord::Pending {
            claim_id,
            owner_pid,
            created_at,
            ..
        }) = entry
        else {
            return Ok(false);
        };

        if claim_id != expected_claim_id {
            return Ok(false);
        }

        PlacementBindingRegistry::insert(
            key,
            PlacementBindingEntryRecord::Pending {
                claim_id,
                owner_pid,
                created_at,
                provisional_pid: Some(provisional_pid),
            },
        );

        Ok(true)
    }

    #[must_use]
    pub fn lookup_key(pool: &str, key_value: &str) -> Option<Principal> {
        let key = PlacementBindingKey::try_new(pool, key_value).ok()?;
        match PlacementBindingRegistry::get(&key) {
            Some(PlacementBindingEntryRecord::Bound { instance_pid, .. }) => Some(instance_pid),
            Some(PlacementBindingEntryRecord::Pending { .. }) | None => None,
        }
    }

    #[must_use]
    pub fn lookup_entry(pool: &str, key_value: &str) -> Option<PlacementBindingStatusResponse> {
        let key = PlacementBindingKey::try_new(pool, key_value).ok()?;
        PlacementBindingRegistry::get(&key).map(entry_to_response)
    }

    // Release one stale pending claim so recovery/admin paths can clear dead keys.
    pub fn release_stale_pending_if_claim_matches(
        pool: &str,
        key_value: &str,
        expected_claim_id: u64,
        now: u64,
    ) -> Result<PlacementBindingReleaseResult, InternalError> {
        let key = PlacementBindingKey::try_new(pool, key_value)
            .map_err(PlacementBindingRegistryOpsError::InvalidKey)?;

        let Some(entry) = PlacementBindingRegistry::get(&key) else {
            return Ok(PlacementBindingReleaseResult::Missing);
        };

        match entry {
            PlacementBindingEntryRecord::Bound {
                instance_pid,
                bound_at,
            } => Ok(PlacementBindingReleaseResult::Bound {
                instance_pid,
                bound_at,
            }),

            PlacementBindingEntryRecord::Pending {
                claim_id,
                owner_pid,
                created_at,
                provisional_pid,
            } if claim_id != expected_claim_id
                || !is_pending_stale(now, created_at)
                || provisional_pid.is_none() =>
            {
                Ok(PlacementBindingReleaseResult::PendingRetained {
                    owner_pid,
                    created_at,
                    provisional_pid,
                })
            }

            PlacementBindingEntryRecord::Pending {
                claim_id: _,
                owner_pid,
                created_at,
                provisional_pid,
            } => {
                let _ = PlacementBindingRegistry::remove(&key);

                Ok(PlacementBindingReleaseResult::ReleasedStalePending {
                    owner_pid,
                    created_at,
                    provisional_pid,
                })
            }
        }
    }

    // Finalize a resolved child into the canonical bound state.
    pub fn bind(
        pool: &str,
        key_value: &str,
        pid: Principal,
        bound_at: u64,
    ) -> Result<(), InternalError> {
        let key = PlacementBindingKey::try_new(pool, key_value)
            .map_err(PlacementBindingRegistryOpsError::InvalidKey)?;

        match PlacementBindingRegistry::get(&key) {
            Some(PlacementBindingEntryRecord::Bound { instance_pid, .. })
                if instance_pid == pid =>
            {
                Ok(())
            }

            Some(PlacementBindingEntryRecord::Bound { instance_pid, .. }) => {
                Err(PlacementBindingRegistryOpsError::KeyBound {
                    pool: pool.to_string(),
                    key_value: key_value.to_string(),
                    pid: instance_pid,
                }
                .into())
            }

            Some(PlacementBindingEntryRecord::Pending {
                provisional_pid: Some(expected_pid),
                ..
            }) if expected_pid != pid => {
                Err(PlacementBindingRegistryOpsError::ProvisionalPidMismatch {
                    pool: pool.to_string(),
                    key_value: key_value.to_string(),
                    expected: expected_pid,
                    actual: pid,
                }
                .into())
            }

            Some(PlacementBindingEntryRecord::Pending { .. }) | None => {
                PlacementBindingRegistry::insert(
                    key,
                    PlacementBindingEntryRecord::Bound {
                        instance_pid: pid,
                        bound_at,
                    },
                );
                Ok(())
            }
        }
    }

    // Finalize a created child only if the caller still owns the current pending claim.
    pub fn bind_if_claim_matches(
        pool: &str,
        key_value: &str,
        expected_claim_id: u64,
        pid: Principal,
        bound_at: u64,
    ) -> Result<bool, InternalError> {
        let key = PlacementBindingKey::try_new(pool, key_value)
            .map_err(PlacementBindingRegistryOpsError::InvalidKey)?;

        match PlacementBindingRegistry::get(&key) {
            Some(PlacementBindingEntryRecord::Pending {
                claim_id,
                provisional_pid: Some(expected_pid),
                ..
            }) if claim_id == expected_claim_id && expected_pid != pid => {
                Err(PlacementBindingRegistryOpsError::ProvisionalPidMismatch {
                    pool: pool.to_string(),
                    key_value: key_value.to_string(),
                    expected: expected_pid,
                    actual: pid,
                }
                .into())
            }

            Some(PlacementBindingEntryRecord::Pending { claim_id, .. })
                if claim_id != expected_claim_id =>
            {
                Ok(false)
            }

            Some(PlacementBindingEntryRecord::Pending { .. }) => {
                PlacementBindingRegistry::insert(
                    key,
                    PlacementBindingEntryRecord::Bound {
                        instance_pid: pid,
                        bound_at,
                    },
                );
                Ok(true)
            }

            Some(PlacementBindingEntryRecord::Bound { .. }) | None => Ok(false),
        }
    }

    #[must_use]
    pub fn entries_response() -> PlacementBindingRegistryResponse {
        let entries = PlacementBindingRegistry::export()
            .entries
            .into_iter()
            .map(|record| PlacementBindingRegistryEntry {
                pool: record.key.pool.to_string(),
                key_value: record.key.key_value.to_string(),
                status: entry_to_response(record.entry),
            })
            .collect();

        PlacementBindingRegistryResponse(entries)
    }

    #[cfg(test)]
    pub(crate) fn clear_for_test() {
        PlacementBindingRegistry::clear();
    }
}

// Decide whether an in-progress claim can be reclaimed by a later caller.
const fn is_pending_stale(now: u64, created_at: u64) -> bool {
    now.saturating_sub(created_at) > PlacementBindingRegistryOps::PENDING_TTL_SECS
}

// Convert the storage-owned entry state into the public placement DTO shape.
const fn entry_to_response(entry: PlacementBindingEntryRecord) -> PlacementBindingStatusResponse {
    match entry {
        PlacementBindingEntryRecord::Pending {
            claim_id: _,
            owner_pid,
            created_at,
            provisional_pid,
        } => PlacementBindingStatusResponse::Pending {
            owner_pid,
            created_at,
            provisional_pid,
        },
        PlacementBindingEntryRecord::Bound {
            instance_pid,
            bound_at,
        } => PlacementBindingStatusResponse::Bound {
            instance_pid,
            bound_at,
        },
    }
}

const fn entry_to_state(entry: PlacementBindingEntryRecord) -> PlacementBindingEntryState {
    match entry {
        PlacementBindingEntryRecord::Pending {
            claim_id,
            owner_pid,
            created_at,
            provisional_pid,
        } => PlacementBindingEntryState::Pending {
            claim_id,
            owner_pid,
            created_at,
            provisional_pid,
        },
        PlacementBindingEntryRecord::Bound {
            instance_pid,
            bound_at,
        } => PlacementBindingEntryState::Bound {
            instance_pid,
            bound_at,
        },
    }
}