saddle-framework 0.3.36

The single business-facing facade for Saddle applications
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
//! Focused actual-account controls called by the production listener harness.
use super::*;
use std::{
    pin::pin,
    sync::atomic::{AtomicUsize, Ordering},
    task::{Context, Poll, Waker},
};
static BUILDS: AtomicUsize = AtomicUsize::new(0);
static DROPS: AtomicUsize = AtomicUsize::new(0);
static DROP_MINIMUM: AtomicUsize = AtomicUsize::new(0);
static DROP_VIEW: std::sync::Mutex<Option<saddle_runtime::profusegw::ProfuseGwResourceView>> =
    std::sync::Mutex::new(None);
pub(super) fn inner_constructed() {
    BUILDS.fetch_add(1, Ordering::SeqCst);
}

crate::database_operations! {
    relational;
    pub mod fixtures {
        namespace "control.named";
        schema { "named_records" as records { "id" as id:u64, "stamp" as stamp:timestamp_utc(0) } }
        unique records { id };
        update Write { parameters WriteParams {id:u64,stamp:timestamp_utc(0)}
            table records; values {stamp:bind(stamp)} where records.id==bind(id); }
        query Rows { parameters RowsParams {id:u64,stamp:timestamp_utc(0)}
            from records as r; where r.id==bind(id) && r.stamp==bind(stamp);
            result rows RowsRow {id:r.id} }
        query Optional { parameters OptionalParams {id:u64,stamp:timestamp_utc(0)}
            from records as r; where r.id==bind(id) && r.stamp==bind(stamp);
            result optional OptionalRow {id:r.id} }
        query Probe { parameters ProbeParams {id:u64,stamp:timestamp_utc(0)}
            from records as r; where r.id==bind(id) && r.stamp==bind(stamp);
            result probe ProbeRow {id:r.id} }
        query Page { parameters PageParams {}
            from records as r;
            order {r.id asc nulls_last} result page PageRow {id:r.id,stamp:r.stamp} }
        query Closed { parameters ClosedParams {id:u64}
            from records as r; where r.id==bind(id);
            result optional ClosedRow {id:r.id} }
    }
}
macro_rules! dropped_parameters {
    ($($p:ty),*) => {$ (impl Drop for $p {
        fn drop(&mut self) {
            let minimum = DROP_MINIMUM.load(Ordering::SeqCst);
            if minimum != 0 {
                assert!(DROP_VIEW.lock().unwrap().as_ref().unwrap().snapshot().unwrap().framework_charged >= minimum,
                    "physical parameter destruction occurs while the stored future is still charged");
            }
            DROPS.fetch_add(1, Ordering::SeqCst);
        }
    })*};
}
dropped_parameters!(
    fixtures::WriteParams,
    fixtures::RowsParams,
    fixtures::OptionalParams,
    fixtures::ProbeParams
);
static PAGE_PHASE: AtomicUsize = AtomicUsize::new(0);
static PAGE_CONVERSION_DROPS: AtomicUsize = AtomicUsize::new(0);
static PAGE_ORIGINAL_DROPS: AtomicUsize = AtomicUsize::new(0);
impl Drop for fixtures::PageParams {
    fn drop(&mut self) {
        let phase = PAGE_PHASE.load(Ordering::SeqCst);
        if phase != 0 {
            let charged = DROP_VIEW
                .lock()
                .unwrap()
                .as_ref()
                .unwrap()
                .snapshot()
                .unwrap()
                .framework_charged;
            assert!(charged >= DROP_MINIMUM.load(Ordering::SeqCst));
            match phase {
                1 => {
                    PAGE_CONVERSION_DROPS.fetch_add(1, Ordering::SeqCst);
                }
                2 => {
                    PAGE_ORIGINAL_DROPS.fetch_add(1, Ordering::SeqCst);
                }
                _ => panic!("unexpected page lifecycle phase"),
            }
        }
        DROPS.fetch_add(1, Ordering::SeqCst);
    }
}
fn stamp(year: u16) -> saddle_db::internal::DbTimestamp<0> {
    saddle_db::internal::DbTimestamp::new(
        saddle_db::internal::DbDate::new(year, 1, 1).unwrap(),
        0,
        0,
        1,
        0,
    )
    .unwrap()
}
fn page(_year: u16) -> crate::database_rows::PageRequest<fixtures::Page> {
    crate::database_rows::PageRequest::first(fixtures::PageParams {}, 1).unwrap()
}
fn send<F: Future + Send>(_: &F) {}
impl DatabaseRequest {
    pub(crate) async fn control_named_storage_admission(
        &mut self,
        view: &saddle_runtime::profusegw::ProfuseGwResourceView,
        surface: usize,
    ) {
        assert!(self.state.reserved_context.is_some() && self.process.is_some());
        let before = view.snapshot().unwrap().framework_charged;
        let start_drops = DROPS.load(Ordering::SeqCst);
        macro_rules! unpolled {
            ($call:expr) => {{
                let f = $call;
                send(&f);
                drop(f);
            }};
        }
        unpolled!(self.named_write::<fixtures::Write>(fixtures::WriteParams {
            id: 7,
            stamp: stamp(2020)
        }));
        unpolled!(self.named_rows::<fixtures::Rows>(fixtures::RowsParams {
            id: 7,
            stamp: stamp(2020)
        }));
        unpolled!(
            self.named_optional::<fixtures::Optional>(fixtures::OptionalParams {
                id: 7,
                stamp: stamp(2020)
            })
        );
        unpolled!(self.named_probe::<fixtures::Probe>(fixtures::ProbeParams {
            id: 7,
            stamp: stamp(2020)
        }));
        unpolled!(self.named_page::<fixtures::Page>(page(2020)));
        assert_eq!(DROPS.load(Ordering::SeqCst) - start_drops, 5);
        assert_eq!(BUILDS.load(Ordering::SeqCst), 0);
        assert_eq!(view.snapshot().unwrap().framework_charged, before);
        macro_rules! reject {
            ($call:expr) => {{
                let result = $call.await;
                let Err(QueryFailure::Target { parameters, error }) = result else {
                    panic!("original target rejection required")
                };
                assert_eq!(error, saddle_db::internal::TimestampTargetError::OutOfRange);
                assert_eq!(parameters.id, 7);
                assert_eq!(parameters.stamp.as_sql_str(), stamp(2107).as_sql_str());
                drop(parameters);
            }};
        }
        reject!(self.named_write::<fixtures::Write>(fixtures::WriteParams {
            id: 7,
            stamp: stamp(2107)
        }));
        reject!(self.named_rows::<fixtures::Rows>(fixtures::RowsParams {
            id: 7,
            stamp: stamp(2107)
        }));
        reject!(
            self.named_optional::<fixtures::Optional>(fixtures::OptionalParams {
                id: 7,
                stamp: stamp(2107)
            })
        );
        reject!(self.named_probe::<fixtures::Probe>(fixtures::ProbeParams {
            id: 7,
            stamp: stamp(2107)
        }));
        assert_eq!(DROPS.load(Ordering::SeqCst) - start_drops, 9);
        assert_eq!(BUILDS.load(Ordering::SeqCst), 0);
        assert_eq!(view.snapshot().unwrap().framework_charged, before);
        assert!(self.state.reserved_failure.lock().unwrap().is_none());
        assert!(matches!(
            *self.state.terminal.lock().unwrap(),
            RequestTerminal::Untouched(_)
        ));

        // The actual closed-process inner reaches its original State result.
        // Its allocation remains charged even after Ready until the owner drops.
        let original_process = self.process.take();
        let private =
            self.named_optional_inner::<fixtures::Closed>(fixtures::ClosedParams { id: 7 });
        let bytes = std::mem::size_of_val(&private);
        drop(private);
        {
            let mut future =
                pin!(self.named_optional::<fixtures::Closed>(fixtures::ClosedParams { id: 7 }));
            assert!(matches!(
                future
                    .as_mut()
                    .poll(&mut Context::from_waker(Waker::noop())),
                Poll::Ready(Err(QueryFailure::Database(ScopeDatabaseError::State)))
            ));
            assert_eq!(view.snapshot().unwrap().framework_charged - before, bytes);
            drop(future); // pin! backing owner is dropped at this block's end below.
        }
        assert_eq!(view.snapshot().unwrap().framework_charged, before);
        self.process = original_process;

        struct Held<const N: usize>([u8; N]);
        impl<const N: usize> Future for Held<N> {
            type Output = ();
            fn poll(self: std::pin::Pin<&mut Self>, _: &mut Context<'_>) -> Poll<()> {
                std::hint::black_box(&self.0);
                Poll::Pending
            }
        }
        let memory = self.framework_future_memory();
        macro_rules! fill {
            ($n:expr) => {{
                let mut held = Vec::new();
                loop {
                    match saddle_admission::managed_framework_future(
                        &memory,
                        || Held::<$n>([0; $n]),
                        |_| {},
                    ) {
                        Ok(f) => held.push(f),
                        Err(saddle_admission::AdmissionError::FrameworkReserveExceeded {
                            ..
                        }) => break,
                        Err(e) => panic!("unexpected reserve failure {e:?}"),
                    }
                    assert!(held.len() < 1024);
                }
                held
            }};
        }
        let large = fill!(32768);
        let medium = fill!(1024);
        let small = fill!(64);
        let last = fill!(1);
        macro_rules! denied {
            ($call:expr) => {{
                assert!(matches!(
                    $call.await,
                    Err(QueryFailure::Database(ScopeDatabaseError::Resource))
                ));
            }};
        }
        let builds = BUILDS.load(Ordering::SeqCst);
        match surface {
            0 => denied!(self.named_write::<fixtures::Write>(fixtures::WriteParams {
                id: 7,
                stamp: stamp(2020)
            })),
            1 => denied!(self.named_rows::<fixtures::Rows>(fixtures::RowsParams {
                id: 7,
                stamp: stamp(2020)
            })),
            2 => denied!(
                self.named_optional::<fixtures::Optional>(fixtures::OptionalParams {
                    id: 7,
                    stamp: stamp(2020)
                })
            ),
            3 => denied!(self.named_probe::<fixtures::Probe>(fixtures::ProbeParams {
                id: 7,
                stamp: stamp(2020)
            })),
            4 => denied!(self.named_page::<fixtures::Page>(page(2020))),
            _ => panic!("unknown surface"),
        }
        assert_eq!(
            BUILDS.load(Ordering::SeqCst),
            builds,
            "no inner construction before reserve"
        );
        assert!(matches!(
            *self.state.terminal.lock().unwrap(),
            RequestTerminal::Untouched(_)
        ));
        let occurrence = {
            let slot = self.state.reserved_failure.lock().unwrap();
            let e = slot.as_ref().unwrap();
            assert!(e.original_was_written());
            serde_json::to_value(e.occurrence()).unwrap()
        };
        drop((large, medium, small, last));
        macro_rules! blocked {
            ($call:expr) => {
                assert!(matches!(
                    $call.await,
                    Err(QueryFailure::Database(ScopeDatabaseError::State))
                ));
            };
        }
        blocked!(self.named_write::<fixtures::Write>(fixtures::WriteParams {
            id: 7,
            stamp: stamp(2020)
        }));
        blocked!(self.named_rows::<fixtures::Rows>(fixtures::RowsParams {
            id: 7,
            stamp: stamp(2020)
        }));
        blocked!(
            self.named_optional::<fixtures::Optional>(fixtures::OptionalParams {
                id: 7,
                stamp: stamp(2020)
            })
        );
        blocked!(self.named_probe::<fixtures::Probe>(fixtures::ProbeParams {
            id: 7,
            stamp: stamp(2020)
        }));
        blocked!(self.named_page::<fixtures::Page>(page(2020)));
        assert_eq!(BUILDS.load(Ordering::SeqCst), builds);
        assert_eq!(
            serde_json::to_value(
                self.state
                    .reserved_failure
                    .lock()
                    .unwrap()
                    .as_ref()
                    .unwrap()
                    .occurrence()
            )
            .unwrap(),
            occurrence
        );
        assert!(matches!(
            *self.state.terminal.lock().unwrap(),
            RequestTerminal::Untouched(_)
        ));
        println!(
            "NAMED_STORAGE_ADMISSION surface={surface} PASS unpolled/target-intact/no-inner/noSQL/RequestDb/original-preserved"
        );
    }
}

impl DatabaseRequest {
    pub(crate) fn control_named_storage_config(
        config: saddle_db::DatabaseConfig,
    ) -> saddle_db::DatabaseConfig {
        config
            .register_owned_write::<fixtures::Write>()
            .register_owned_query::<fixtures::Rows>()
            .register_owned_query::<fixtures::Optional>()
            .register_owned_query::<fixtures::Probe>()
            .register_owned_query::<fixtures::Page>()
    }
    pub(crate) async fn control_named_storage_normal(&mut self) {
        let write = self
            .named_write::<fixtures::Write>(fixtures::WriteParams {
                id: 7,
                stamp: stamp(2020),
            })
            .await;
        if let Err(QueryFailure::Database(error)) = &write {
            panic!("named normal write database error: {error:?}");
        }
        assert!(write.is_ok());
        drop(write);
        let rows = self
            .named_rows::<fixtures::Rows>(fixtures::RowsParams {
                id: 7,
                stamp: stamp(2020),
            })
            .await;
        assert!(rows.is_ok());
        drop(rows);
        let row = self
            .named_optional::<fixtures::Optional>(fixtures::OptionalParams {
                id: 7,
                stamp: stamp(2020),
            })
            .await;
        assert!(matches!(row, Ok(Some(_))));
        drop(row);
        let probe = self
            .named_probe::<fixtures::Probe>(fixtures::ProbeParams {
                id: 7,
                stamp: stamp(2020),
            })
            .await;
        assert!(matches!(
            probe,
            Ok(crate::database::MatchCardinality::One(_))
        ));
        drop(probe);
        let page = self.named_page::<fixtures::Page>(page(2020)).await;
        assert!(page.is_ok());
        drop(page);
        assert!(self.state.reserved_failure.lock().unwrap().is_none());
        println!("NAMED_STORAGE_NORMAL PASS five actual SQL surfaces");
    }
}

impl DatabaseRequest {
    pub(crate) async fn control_named_storage_database_failure(&mut self) {
        let result = self
            .named_write::<fixtures::Write>(fixtures::WriteParams {
                id: 7,
                stamp: stamp(2020),
            })
            .await;
        assert!(matches!(
            result,
            Err(QueryFailure::Database(ScopeDatabaseError::Operation))
        ));
        // Execution originals are recorded by ReservedDiagnostic, not the
        // admission-only first-failure slot. The harness independently reads
        // complete 1142 original records after this operation returns.
        assert!(self.state.reserved_failure.lock().unwrap().is_none());
        println!(
            "NAMED_STORAGE_DATABASE_FAILURE PASS Operation/execution-owner/admission-slot-untouched"
        );
    }
    pub(crate) async fn control_named_storage_cancel(
        &mut self,
        view: &saddle_runtime::profusegw::ProfuseGwResourceView,
    ) {
        let before = view.snapshot().unwrap().framework_charged;
        // Match repr(C) ViewStorage's prefix. Retained<T> is exactly the
        // existing Option<Arc<T>> wrapper; its outer child Option is distinct.
        type RetainedShape = Option<std::sync::Arc<()>>;
        let prefix = std::alloc::Layout::new::<[std::sync::atomic::AtomicUsize; 2]>()
            .extend(std::alloc::Layout::new::<(
                saddle_core::request_context::RequestExecutionView,
                Option<RetainedShape>,
                Option<RetainedShape>,
                RetainedShape,
            )>())
            .unwrap()
            .0;
        let [_, (_, payload), _] = saddle_core::request_context::request_context_layouts();
        let held_view = saddle_admission::StorageDemand::embedded(
            prefix,
            std::alloc::Layout::new::<()>(),
            &[(payload, 1)],
        )
        .unwrap()
        .bytes();
        // Shared ScopeStorage layout already includes its original permit.
        let held_scope = saddle_runtime::request_task::reserved::supervised_scope_layouts()
            .into_iter()
            .find(|(name, _)| *name == "scope_shared_allocation")
            .unwrap()
            .1
            .size();
        let retained = held_view + held_scope;
        let private = self.named_page_inner::<fixtures::Page>(page(2020));
        let bytes = std::mem::size_of_val(&private);
        drop(private);
        let state = self.state.clone();
        let drops = DROPS.load(Ordering::SeqCst);
        *DROP_VIEW.lock().unwrap() = Some(view.clone());
        DROP_MINIMUM.store(before + bytes, Ordering::SeqCst);
        PAGE_CONVERSION_DROPS.store(0, Ordering::SeqCst);
        PAGE_ORIGINAL_DROPS.store(0, Ordering::SeqCst);
        PAGE_PHASE.store(1, Ordering::SeqCst);
        {
            let mut future = pin!(self.named_page::<fixtures::Page>(page(2020)));
            assert!(
                future
                    .as_mut()
                    .poll(&mut Context::from_waker(Waker::noop()))
                    .is_pending()
            );
            assert!(
                !matches!(
                    *state.terminal.lock().unwrap(),
                    RequestTerminal::Untouched(_)
                ),
                "real reserved serial owner entered before cancellation"
            );
            assert!(view.snapshot().unwrap().framework_charged >= before + bytes);
            assert_eq!(PAGE_CONVERSION_DROPS.load(Ordering::SeqCst), 1);
            assert_eq!(PAGE_ORIGINAL_DROPS.load(Ordering::SeqCst), 0);
            PAGE_PHASE.store(2, Ordering::SeqCst);
        }
        PAGE_PHASE.store(0, Ordering::SeqCst);
        assert_eq!(PAGE_CONVERSION_DROPS.load(Ordering::SeqCst), 1);
        assert_eq!(PAGE_ORIGINAL_DROPS.load(Ordering::SeqCst), 1);
        DROP_MINIMUM.store(0, Ordering::SeqCst);
        *DROP_VIEW.lock().unwrap() = None;
        assert_eq!(DROPS.load(Ordering::SeqCst) - drops, 2);
        assert!(
            matches!(
                *state.terminal.lock().unwrap(),
                RequestTerminal::PostDatabase(_)
            ),
            "original ReservedDbOwner restored physical terminal"
        );
        assert_eq!(
            view.snapshot().unwrap().framework_charged,
            before + retained,
            "named F fully refunded; original scope view/observation remain physically owned"
        );
        assert!(view.snapshot().unwrap().healthy);
        println!(
            "NAMED_STORAGE_CANCEL PASS actual-owner/Pending/physical-parameter-before-refund/original-terminal"
        );
    }
}

impl DatabaseRequest {
    pub(crate) async fn control_named_storage_legacy(&mut self) {
        assert!(self.state.reserved_context.is_none());
        assert!(self.state.legacy.is_some());
        self.control_named_storage_normal().await;
        println!(
            "NAMED_STORAGE_LEGACY PASS original nonreserved entry/tasks/normal-five/charged-memory"
        );
    }
}