operon 0.7.0

A workflow engine for parallel, incremental scheduling of DAG-defined multiplex tasks.
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
use std::sync::RwLock;

use crate::meta_storage::mem::error::{MemMetaError, MemResult};
use crate::meta_storage::mem::store::MemStore;
use crate::meta_storage::{MetaStorageError, MetaTicketApi};
use crate::schema::{
    DimensionMetadata, Job, OptionCoordinate, Resolution, TableShape, TaskMetadata, Ticket,
    TicketStatus,
};

/// A ticket's primary key: its coordinate, with unresolved dimensions left as `None`.
type TicketKey = Box<[OptionCoordinate]>;

/// A stored ticket's state, keyed in the table by its coordinate.
#[derive(Debug, Clone, Copy)]
struct TicketRow {
    deps_done: usize,
    deps_quota: usize,
    status: TicketStatus,
}

/// One task's ticket table.
#[derive(Debug, Default)]
pub(super) struct TicketTable {
    rows: RwLock<TicketRows>,
}

/// A ticket table's rows, alongside the per-status counters they are summarized by and the
/// secondary indexes they are queried through.
///
/// One lock spans all three: it makes a slice op's scan and write-back atomic, and it keeps a
/// reader from observing the counters or an index midway through an update.
#[derive(Debug, Default)]
struct TicketRows {
    map: HashMap<TicketKey, TicketRow>,
    indexes: HashMap<PinMask, PinIndex>,
    done: i64,
    queued: i64,
    waiting: i64,
}

impl TicketRows {
    /// Adjusts the counter for `status` by `delta`.
    fn count(&mut self, status: TicketStatus, delta: i64) {
        match status {
            TicketStatus::Done => self.done += delta,
            TicketStatus::Queued => self.queued += delta,
            TicketStatus::Waiting => self.waiting += delta,
        }
    }

    /// Adds a key to every index built so far.
    fn index(&mut self, key: &TicketKey) {
        for (mask, index) in &mut self.indexes {
            let _ = index
                .entry(mask.apply(key))
                .or_default()
                .insert(key.clone());
        }
    }

    /// Drops a key from every index built so far.
    fn unindex(&mut self, key: &TicketKey) {
        for (mask, index) in &mut self.indexes {
            let Entry::Occupied(mut bucket) = index.entry(mask.apply(key)) else {
                continue;
            };
            let _ = bucket.get_mut().remove(key);
            if bucket.get().is_empty() {
                let _value = bucket.remove();
            }
        }
    }

    /// The keys whose coordinate matches every pin, building the index for that pin set on first
    /// use.
    ///
    /// A pin set covering every dimension names one coordinate outright, so it is served straight
    /// from the rows.
    fn matching(&mut self, mask: &PinMask, pinned: &TicketKey) -> Vec<TicketKey> {
        if mask.is_full() {
            return Vec::from_iter(self.map.contains_key(pinned).then(|| pinned.clone()));
        }

        if !self.indexes.contains_key(mask) {
            let mut index = PinIndex::default();
            for key in self.map.keys() {
                let _ = index
                    .entry(mask.apply(key))
                    .or_default()
                    .insert(key.clone());
            }
            let _old_value = self.indexes.insert(mask.clone(), index);
        }

        self.indexes[mask]
            .get(pinned)
            .map(|bucket| bucket.iter().cloned().collect())
            .unwrap_or_default()
    }

    /// The waiting tickets whose coordinate matches every pin, alongside their keys.
    fn matching_waiting(
        &mut self,
        mask: &PinMask,
        pinned: &TicketKey,
    ) -> Vec<(TicketKey, TicketRow)> {
        self.matching(mask, pinned)
            .into_iter()
            .filter_map(|key| self.map.get(&key).map(|row| (key, *row)))
            .filter(|(_, row)| row.status == TicketStatus::Waiting)
            .collect()
    }

    /// Inserts a ticket, keeping the counters in step.
    fn insert(&mut self, key: TicketKey, row: TicketRow) {
        self.count(row.status, 1);
        self.index(&key);
        let _ = self.map.insert(key, row);
    }

    /// Inserts a ticket, leaving an existing one at the same key untouched.
    fn insert_new(&mut self, key: TicketKey, row: TicketRow) {
        if self.map.contains_key(&key) {
            return;
        }
        self.insert(key, row);
    }

    /// Replaces the row at `key`, keeping the counters in step.
    fn replace(&mut self, key: &TicketKey, old_status: TicketStatus, row: TicketRow) {
        let _ = self.map.insert(key.clone(), row);
        self.count(old_status, -1);
        self.count(row.status, 1);
    }

    /// Removes the row at `key`, keeping the counters in step.
    fn remove(&mut self, key: &TicketKey) -> Option<TicketRow> {
        let row = self.map.remove(key)?;
        self.count(row.status, -1);
        self.unindex(key);
        Some(row)
    }

    fn clear(&mut self) {
        self.map.clear();
        self.indexes.clear();
        self.done = 0;
        self.queued = 0;
        self.waiting = 0;
    }
}

/// One of a task's dimensions, as a query sees it: pinned to a concrete value, or free to vary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
enum PinSlot {
    Free,
    Pinned,
}

/// The set of a task's dimensions that a query pins, one slot per dimension.
///
/// Every op pins a subset that the DAG fixes in advance, so a task sees only a handful of masks.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct PinMask(Box<[PinSlot]>);

impl PinMask {
    /// Builds the pin set a query fixes and the coordinate it pins to, from the upstream dimensions
    /// and the values they carry.
    ///
    /// A dimension is pinned when it belongs to this task, is not aggregated, and carries a
    /// resolved value; every other dimension stays free.
    fn project<const N: usize>(
        task_meta: TaskMetadata<N>,
        pins: impl IntoIterator<Item = (&'static str, OptionCoordinate)>,
        aggregate_dims: &[&'static str],
    ) -> (Self, TicketKey) {
        let mut slots = vec![PinSlot::Free; N];
        let mut pinned = vec![OptionCoordinate::none(); N];
        for (dim, coord) in pins {
            if aggregate_dims.contains(&dim) {
                continue;
            }
            let Some(value) = coord.0 else {
                continue;
            };
            let Some(idx) = task_meta.dims.iter().position(|d| *d == dim) else {
                continue;
            };
            slots[idx] = PinSlot::Pinned;
            pinned[idx] = OptionCoordinate::some(value);
        }
        (Self(slots.into()), pinned.into())
    }

    /// Projects a coordinate onto this pin set, nulling every dimension the set leaves free.
    ///
    /// Two coordinates share a projection exactly when they agree on every pinned dimension, so a
    /// projection is the bucket key both a stored ticket and a query resolve to.
    fn apply(&self, key: &[OptionCoordinate]) -> TicketKey {
        self.0
            .iter()
            .zip(key)
            .map(|(slot, &coord)| match slot {
                PinSlot::Pinned => coord,
                PinSlot::Free => OptionCoordinate::none(),
            })
            .collect()
    }

    /// Whether the set pins every dimension, which names one coordinate outright.
    fn is_full(&self) -> bool {
        self.0.iter().all(|slot| *slot == PinSlot::Pinned)
    }
}

/// A secondary index over one pin set, mapping a projected coordinate to the keys holding it.
type PinIndex = HashMap<TicketKey, HashSet<TicketKey>>;

/// Helper struct for querying the in-memory tickets of a task.
#[derive(Debug)]
pub struct MemTicketQueryBuilder<'a, const N: usize> {
    store: &'a MemStore,
    task_meta: TaskMetadata<N>,
}

impl MemStore {
    /// Helper method to create a `MemTicketQueryBuilder` for the tickets of a given task.
    pub(super) fn ticket<const N: usize>(
        &self,
        task_meta: TaskMetadata<N>,
    ) -> MemTicketQueryBuilder<'_, N> {
        MemTicketQueryBuilder {
            store: self,
            task_meta,
        }
    }
}

impl<const N: usize> MemTicketQueryBuilder<'_, N> {
    /// The table this task's tickets live in, if it has been initialized.
    fn table(&self) -> MemResult<Option<std::sync::Arc<TicketTable>>> {
        self.store.ticket_table(self.task_meta.id)
    }

    /// The table this task's tickets live in, erroring if it has not been initialized.
    fn require_table(&self) -> MemResult<std::sync::Arc<TicketTable>> {
        self.table()?.ok_or(MetaStorageError::Internal(
            "ticket table was not initialized",
        ))
    }

    /// Reconstructs a typed ticket from a stored key and row.
    fn ticket_of(key: &TicketKey, row: &TicketRow) -> Ticket<N> {
        let coordinate = std::array::from_fn(|i| key[i]);
        Ticket::from_parts(coordinate, row.deps_done, row.deps_quota, row.status)
    }

    /// Splits a typed ticket into the key and row it is stored as.
    fn split(ticket: &Ticket<N>) -> (TicketKey, TicketRow) {
        let key = Box::from(&ticket.coordinate[..]);
        let row = TicketRow {
            deps_done: ticket.deps_done(),
            deps_quota: ticket.deps_quota(),
            status: ticket.status,
        };
        (key, row)
    }
}

impl<const N: usize> MetaTicketApi<N> for MemTicketQueryBuilder<'_, N> {
    type Error = MemMetaError;

    async fn init(&self) -> MemResult<TableShape> {
        self.store.init_ticket_table(self.task_meta.id)?;
        Ok(TableShape::CURRENT)
    }

    async fn clear(&self) -> MemResult<()> {
        if let Some(table) = self.table()? {
            table.rows.write()?.clear();
        }
        Ok(())
    }

    async fn get_all(&self, status: TicketStatus) -> MemResult<Vec<Ticket<N>>> {
        let Some(table) = self.table()? else {
            return Ok(Vec::new());
        };
        let rows = table.rows.read()?;
        Ok(rows
            .map
            .iter()
            .filter(|(_, row)| row.status == status)
            .map(|(key, row)| Self::ticket_of(key, row))
            .collect())
    }

    async fn put(&self, ticket: Ticket<N>) -> MemResult<()> {
        let table = self.require_table()?;
        let (key, row) = Self::split(&ticket);
        table.rows.write()?.insert_new(key, row);
        Ok(())
    }

    async fn dump(&self) -> MemResult<Vec<Ticket<N>>> {
        let Some(table) = self.table()? else {
            return Ok(Vec::new());
        };
        let rows = table.rows.read()?;
        let tickets = rows
            .map
            .iter()
            .map(|(key, row)| Self::ticket_of(key, row))
            .collect::<Vec<_>>();
        Ok(tickets)
    }

    async fn hydrate(&self, tickets: Vec<Ticket<N>>) -> MemResult<()> {
        let table = self.require_table()?;
        let mut rows = table.rows.write()?;
        rows.clear();
        for ticket in &tickets {
            let (key, row) = Self::split(ticket);
            rows.insert(key, row);
        }
        Ok(())
    }

    async fn raise_deps_done<const M: usize>(
        &self,
        upstream_meta: TaskMetadata<M>,
        upstream_job: Job<M>,
        aggregate_dims: &[&'static str],
    ) -> MemResult<Vec<Ticket<N>>> {
        let pins = upstream_meta
            .dims
            .iter()
            .zip(upstream_job.coordinate)
            .map(|(dim, coord)| (*dim, OptionCoordinate::some(coord)));

        let table = self.require_table()?;
        let mut rows = table.rows.write()?;

        let (mask, pinned) = PinMask::project(self.task_meta, pins, aggregate_dims);
        let stale = rows.matching_waiting(&mask, &pinned);

        let mut promoted = Vec::new();
        for (key, row) in stale {
            let deps_done = row.deps_done + 1;
            let status = if deps_done >= row.deps_quota {
                TicketStatus::Queued
            } else {
                TicketStatus::Waiting
            };
            let raised = TicketRow {
                deps_done,
                deps_quota: row.deps_quota,
                status,
            };
            rows.replace(&key, row.status, raised);
            if status == TicketStatus::Queued {
                promoted.push(Self::ticket_of(&key, &raised));
            }
        }

        Ok(promoted)
    }

    async fn raise_deps_quota<const M: usize>(
        &self,
        upstream_meta: TaskMetadata<M>,
        upstream_ticket: Ticket<M>,
        aggregate_dims: &[&'static str],
        ub: usize,
    ) -> MemResult<Vec<Ticket<N>>> {
        let pins = upstream_meta
            .dims
            .iter()
            .copied()
            .zip(upstream_ticket.coordinate);

        let table = self.require_table()?;
        let mut rows = table.rows.write()?;

        let (mask, pinned) = PinMask::project(self.task_meta, pins, aggregate_dims);
        let stale = rows.matching_waiting(&mask, &pinned);

        let mut promoted = Vec::new();
        for (key, row) in stale {
            // Mirrors the backing `deps_quota + $ub - 1` arithmetic, which is signed.
            let quota = i64::try_from(row.deps_quota)? + i64::try_from(ub)? - 1;
            let status = if i64::try_from(row.deps_done)? >= quota {
                TicketStatus::Queued
            } else {
                TicketStatus::Waiting
            };
            let quota = usize::try_from(quota)?;
            let raised = TicketRow {
                deps_done: row.deps_done,
                deps_quota: quota,
                status,
            };
            rows.replace(&key, row.status, raised);
            if status == TicketStatus::Queued {
                promoted.push(Self::ticket_of(&key, &raised));
            }
        }

        Ok(promoted)
    }

    async fn explode<const M: usize, const IDX: usize>(
        &self,
        res_meta: DimensionMetadata<M>,
        res: Resolution<M>,
    ) -> MemResult<Vec<Ticket<N>>> {
        const { assert!(IDX < N) }
        if self.task_meta.dims[IDX] != res_meta.id {
            tracing::warn!("Invalid resolution received for explosion.");
            return Ok(vec![]);
        }

        let pins = res_meta
            .deps
            .iter()
            .zip(res.coordinate)
            .map(|(dim, coord)| (*dim, OptionCoordinate::some(coord)));

        let table = self.require_table()?;
        let mut rows = table.rows.write()?;

        let (mask, pinned) = PinMask::project(self.task_meta, pins, &[]);
        let popped_keys = rows.matching(&mask, &pinned);

        let popped = popped_keys
            .into_iter()
            .filter_map(|key| rows.remove(&key).map(|row| (key, row)))
            .collect::<Vec<_>>();
        let tickets = popped
            .iter()
            .map(|(key, row)| Self::ticket_of(key, row))
            .collect::<Vec<_>>();

        if tickets
            .iter()
            .any(|ticket| ticket.coordinate[IDX].is_some())
        {
            return Err(MetaStorageError::invalid_explosion(
                self.task_meta.id,
                res_meta.id,
            ));
        }

        for ticket in &tickets {
            for coord in 0..res.ub {
                let exploded = ticket.with_coordinate::<IDX>(coord).update_status();
                let (key, row) = Self::split(&exploded);
                rows.insert_new(key, row);
            }
        }

        Ok(tickets)
    }

    async fn mark_done(&self, job: Job<N>) -> MemResult<()> {
        let table = self.require_table()?;
        let mut rows = table.rows.write()?;

        let key: TicketKey = job
            .coordinate
            .iter()
            .map(|&c| OptionCoordinate::some(c))
            .collect();
        let Some(row) = rows.map.get(&key).copied() else {
            return Ok(());
        };
        let done = TicketRow {
            status: TicketStatus::Done,
            ..row
        };
        rows.replace(&key, row.status, done);

        Ok(())
    }

    async fn get_status(&self) -> MemResult<(i64, i64, i64)> {
        let Some(table) = self.table()? else {
            return Err(MetaStorageError::missing_ticket_summary(self.task_meta.id));
        };
        let rows = table.rows.read()?;
        Ok((rows.done, rows.queued, rows.waiting))
    }
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;

    use super::*;
    use crate::meta_storage::mem::{MemConn, MemMetaStorage};
    use crate::meta_storage::{MetaBackend, MetaClientApi, MetaConnApi};

    /// A task over one dimension `i`, which is also the dimension it spawns.
    fn task_beta() -> TaskMetadata<1> {
        TaskMetadata {
            id: "beta",
            dims: ["i"],
            spawn_dim: Some("i"),
            priority: &[],
        }
    }

    /// An upstream task over the same dimension `i`.
    fn task_alpha() -> TaskMetadata<1> {
        TaskMetadata {
            id: "alpha",
            dims: ["i"],
            spawn_dim: None,
            priority: &[],
        }
    }

    /// A task over two dimensions, whose upstreams pin one dimension each.
    fn task_gamma() -> TaskMetadata<2> {
        TaskMetadata {
            id: "gamma",
            dims: ["i", "j"],
            spawn_dim: None,
            priority: &[],
        }
    }

    /// An upstream task over `j` alone.
    fn task_over_j() -> TaskMetadata<1> {
        TaskMetadata {
            id: "over_j",
            dims: ["j"],
            spawn_dim: None,
            priority: &[],
        }
    }

    /// Builds an initialized store, returning it alongside a connection to borrow clients from.
    async fn store() -> (MemMetaStorage, MemConn) {
        let backend = MemMetaStorage::default();
        let conn = backend.worker_conn().await.expect("conn");
        (backend, conn)
    }

    #[tokio::test]
    async fn put_counts_tickets_by_status() {
        let (_backend, conn) = store().await;
        let client = conn.as_client();
        let tickets = client.ticket(task_beta());
        let _ = tickets.init().await.unwrap();

        tickets
            .put(Ticket::new(1).with_coordinate::<0>(0))
            .await
            .unwrap();
        tickets
            .put(Ticket::new(0).with_coordinate::<0>(1))
            .await
            .unwrap();

        // (done, queued, waiting): a zero quota is ready on arrival.
        assert_eq!(tickets.get_status().await.unwrap(), (0, 1, 1));
        assert_eq!(
            tickets.get_all(TicketStatus::Queued).await.unwrap().len(),
            1
        );
    }

    #[tokio::test]
    async fn put_leaves_existing_coordinate_untouched() {
        let (_backend, conn) = store().await;
        let client = conn.as_client();
        let tickets = client.ticket(task_beta());
        let _ = tickets.init().await.unwrap();

        tickets
            .put(Ticket::new(1).with_coordinate::<0>(0))
            .await
            .unwrap();
        tickets
            .put(Ticket::new(0).with_coordinate::<0>(0))
            .await
            .unwrap();

        assert_eq!(tickets.get_status().await.unwrap(), (0, 0, 1));
    }

    #[tokio::test]
    async fn raise_deps_done_promotes_only_pinned_coordinate() {
        let (_backend, conn) = store().await;
        let client = conn.as_client();
        let tickets = client.ticket(task_beta());
        let _ = tickets.init().await.unwrap();

        tickets
            .put(Ticket::new(1).with_coordinate::<0>(0))
            .await
            .unwrap();
        tickets
            .put(Ticket::new(1).with_coordinate::<0>(1))
            .await
            .unwrap();

        let promoted = tickets
            .raise_deps_done(task_alpha(), Job { coordinate: [0] }, &[])
            .await
            .unwrap();

        assert_eq!(promoted.len(), 1);
        assert_eq!(promoted[0].coordinate[0].0, Some(0));
        assert_eq!(tickets.get_status().await.unwrap(), (0, 1, 1));
    }

    #[tokio::test]
    async fn raise_deps_done_holds_ticket_short_of_its_quota() {
        let (_backend, conn) = store().await;
        let client = conn.as_client();
        let tickets = client.ticket(task_beta());
        let _ = tickets.init().await.unwrap();

        tickets
            .put(Ticket::new(2).with_coordinate::<0>(0))
            .await
            .unwrap();

        let promoted = tickets
            .raise_deps_done(task_alpha(), Job { coordinate: [0] }, &[])
            .await
            .unwrap();

        assert!(promoted.is_empty());
        assert_eq!(tickets.get_status().await.unwrap(), (0, 0, 1));
    }

    #[tokio::test]
    async fn raise_deps_quota_reopens_ready_ticket() {
        let (_backend, conn) = store().await;
        let client = conn.as_client();
        let tickets = client.ticket(task_beta());
        let _ = tickets.init().await.unwrap();

        tickets
            .put(Ticket::new(1).with_coordinate::<0>(0))
            .await
            .unwrap();

        // A quota of 3 leaves the ticket needing 3 dependencies, none of which are done.
        let promoted = tickets
            .raise_deps_quota(task_alpha(), Ticket::new(1).with_coordinate::<0>(0), &[], 3)
            .await
            .unwrap();

        assert!(promoted.is_empty());
        assert_eq!(tickets.get_status().await.unwrap(), (0, 0, 1));
    }

    #[tokio::test]
    async fn explode_expands_ticket_along_its_spawn_dimension() {
        let (_backend, conn) = store().await;
        let client = conn.as_client();
        let tickets = client.ticket(task_beta());
        let _ = tickets.init().await.unwrap();

        tickets.put(Ticket::new(0)).await.unwrap();

        let popped = tickets
            .explode::<0, 0>(
                DimensionMetadata { id: "i", deps: [] },
                Resolution {
                    coordinate: [],
                    ub: 3,
                },
            )
            .await
            .unwrap();

        assert_eq!(popped.len(), 1);
        assert_eq!(tickets.get_status().await.unwrap(), (0, 3, 0));
        let queued = tickets.get_all(TicketStatus::Queued).await.unwrap();
        let mut coords = queued
            .iter()
            .map(|ticket| ticket.coordinate[0].0)
            .collect::<Vec<_>>();
        coords.sort();
        assert_eq!(coords, vec![Some(0), Some(1), Some(2)]);
    }

    #[tokio::test]
    async fn explode_rejects_already_resolved_dimension() {
        let (_backend, conn) = store().await;
        let client = conn.as_client();
        let tickets = client.ticket(task_beta());
        let _ = tickets.init().await.unwrap();

        tickets
            .put(Ticket::new(0).with_coordinate::<0>(0))
            .await
            .unwrap();

        let exploded = tickets
            .explode::<0, 0>(
                DimensionMetadata { id: "i", deps: [] },
                Resolution {
                    coordinate: [],
                    ub: 2,
                },
            )
            .await;

        assert!(matches!(
            exploded,
            Err(MetaStorageError::InvalidExplosion { .. })
        ));
    }

    #[tokio::test]
    async fn mark_done_moves_ticket_to_done() {
        let (_backend, conn) = store().await;
        let client = conn.as_client();
        let tickets = client.ticket(task_beta());
        let _ = tickets.init().await.unwrap();

        tickets
            .put(Ticket::new(0).with_coordinate::<0>(0))
            .await
            .unwrap();
        tickets.mark_done(Job { coordinate: [0] }).await.unwrap();

        assert_eq!(tickets.get_status().await.unwrap(), (1, 0, 0));
    }

    #[tokio::test]
    async fn clear_resets_rows_and_counters() {
        let (_backend, conn) = store().await;
        let client = conn.as_client();
        let tickets = client.ticket(task_beta());
        let _ = tickets.init().await.unwrap();

        tickets
            .put(Ticket::new(1).with_coordinate::<0>(0))
            .await
            .unwrap();
        tickets.clear().await.unwrap();

        assert_eq!(tickets.get_status().await.unwrap(), (0, 0, 0));
        assert!(
            tickets
                .get_all(TicketStatus::Waiting)
                .await
                .unwrap()
                .is_empty()
        );
    }

    #[tokio::test]
    async fn get_status_reports_missing_table() {
        let (_backend, conn) = store().await;
        let client = conn.as_client();
        let tickets = client.ticket(task_beta());

        assert!(matches!(
            tickets.get_status().await,
            Err(MetaStorageError::MissingTicketSummary { .. })
        ));
    }

    #[tokio::test]
    async fn put_reaches_index_built_before_it() {
        let (_backend, conn) = store().await;
        let client = conn.as_client();
        let tickets = client.ticket(task_beta());
        let _ = tickets.init().await.unwrap();

        tickets
            .put(Ticket::new(1).with_coordinate::<0>(0))
            .await
            .unwrap();
        let _newly_ready = tickets
            .raise_deps_done(task_alpha(), Job { coordinate: [0] }, &[])
            .await
            .unwrap();

        tickets
            .put(Ticket::new(1).with_coordinate::<0>(1))
            .await
            .unwrap();
        let promoted = tickets
            .raise_deps_done(task_alpha(), Job { coordinate: [1] }, &[])
            .await
            .unwrap();

        assert_eq!(promoted.len(), 1);
        assert_eq!(promoted[0].coordinate[0].0, Some(1));
    }

    #[tokio::test]
    async fn explosion_reaches_index_built_before_it() {
        let (_backend, conn) = store().await;
        let client = conn.as_client();
        let tickets = client.ticket(task_beta());
        let _ = tickets.init().await.unwrap();

        tickets.put(Ticket::new(1)).await.unwrap();
        // Builds the index over `i` while the only ticket is still unexploded.
        let _newly_ready = tickets
            .raise_deps_done(task_alpha(), Job { coordinate: [1] }, &[])
            .await
            .unwrap();

        let _replaced = tickets
            .explode::<0, 0>(
                DimensionMetadata { id: "i", deps: [] },
                Resolution {
                    coordinate: [],
                    ub: 3,
                },
            )
            .await
            .unwrap();

        let promoted = tickets
            .raise_deps_done(task_alpha(), Job { coordinate: [1] }, &[])
            .await
            .unwrap();

        assert_eq!(promoted.len(), 1);
        assert_eq!(promoted[0].coordinate[0].0, Some(1));
        assert_eq!(tickets.get_status().await.unwrap(), (0, 1, 2));
    }

    #[tokio::test]
    async fn clear_drops_index_built_before_it() {
        let (_backend, conn) = store().await;
        let client = conn.as_client();
        let tickets = client.ticket(task_beta());
        let _ = tickets.init().await.unwrap();

        tickets
            .put(Ticket::new(1).with_coordinate::<0>(0))
            .await
            .unwrap();
        let _newly_ready = tickets
            .raise_deps_done(task_alpha(), Job { coordinate: [0] }, &[])
            .await
            .unwrap();
        tickets.clear().await.unwrap();

        tickets
            .put(Ticket::new(1).with_coordinate::<0>(0))
            .await
            .unwrap();
        let promoted = tickets
            .raise_deps_done(task_alpha(), Job { coordinate: [0] }, &[])
            .await
            .unwrap();

        assert_eq!(promoted.len(), 1);
        assert_eq!(tickets.get_status().await.unwrap(), (0, 1, 0));
    }

    #[tokio::test]
    async fn upstreams_pinning_different_dimensions_keep_separate_indexes() {
        let (_backend, conn) = store().await;
        let client = conn.as_client();
        let tickets = client.ticket(task_gamma());
        let _ = tickets.init().await.unwrap();

        for i in 0..2 {
            for j in 0..2 {
                tickets
                    .put(
                        Ticket::new(2)
                            .with_coordinate::<0>(i)
                            .with_coordinate::<1>(j),
                    )
                    .await
                    .unwrap();
            }
        }

        // Pins `i` alone: reaches (0,0) and (0,1), neither of which meets its quota of 2.
        let promoted = tickets
            .raise_deps_done(task_alpha(), Job { coordinate: [0] }, &[])
            .await
            .unwrap();
        assert!(promoted.is_empty());

        // Pins `j` alone: reaches (0,0) and (1,0), so only (0,0) reaches its quota.
        let promoted = tickets
            .raise_deps_done(task_over_j(), Job { coordinate: [0] }, &[])
            .await
            .unwrap();

        assert_eq!(promoted.len(), 1);
        assert_eq!(promoted[0].coordinate.map(|c| c.0), [Some(0), Some(0)]);
        assert_eq!(tickets.get_status().await.unwrap(), (0, 1, 3));
    }

    #[tokio::test]
    async fn init_is_idempotent() {
        let (_backend, conn) = store().await;
        let client = conn.as_client();
        let tickets = client.ticket(task_beta());
        let _ = tickets.init().await.unwrap();
        tickets
            .put(Ticket::new(1).with_coordinate::<0>(0))
            .await
            .unwrap();
        let _ = tickets.init().await.unwrap();

        assert_eq!(tickets.get_status().await.unwrap(), (0, 0, 1));
    }

    /// Every operation on a table whose lock a panic has poisoned reports it as a backend error.
    #[tokio::test]
    async fn a_poisoned_table_errors_rather_than_panicking() {
        let store = MemStore::default();
        let tickets = store.ticket(task_beta());
        let _ = tickets.init().await.unwrap();

        let table = store.ticket_table("beta").unwrap().expect("table");
        let panicked = std::thread::spawn(move || {
            let _guard = table.rows.write().expect("uncontended");
            panic!("a store operation unwinds while holding the lock");
        })
        .join();
        assert!(panicked.is_err());

        assert!(matches!(
            tickets.get_status().await,
            Err(MetaStorageError::Backend(MemMetaError::Poisoned))
        ));
        assert!(matches!(
            tickets.put(Ticket::new(0)).await,
            Err(MetaStorageError::Backend(MemMetaError::Poisoned))
        ));
        assert!(matches!(
            tickets.get_all(TicketStatus::Queued).await,
            Err(MetaStorageError::Backend(MemMetaError::Poisoned))
        ));
    }
}