guardian-db 0.19.0

High-performance, local-first decentralized database built on Rust and Iroh
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
//! A PostgreSQL-style lock manager for the single-node gateway.
//!
//! The default gateway is a single coordinator, so it can offer real lock
//! semantics: the eight table-level lock modes with PostgreSQL's exact conflict
//! matrix, the four row-level lock modes, advisory locks (session/transaction,
//! shared/exclusive), `NOWAIT` / `SKIP LOCKED`, blocking waits with
//! `lock_timeout`, and deadlock detection (victim aborted with SQLSTATE 40P01).
//! Lock state is exposed through `pg_catalog.pg_locks`.
//!
//! Locks are held by a *session* (one statement runs at a time per connection)
//! and tagged with a [`LockScope`] (released at transaction end, or at session
//! end for session-level advisory locks).

use std::collections::{HashMap, HashSet};
use std::sync::Mutex;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};
use tokio::sync::Notify;

use crate::relational::RelError;

/// Identifies a connection (lock holder). One statement runs at a time per
/// session, so session granularity is sufficient for the wait-for graph.
pub type SessionId = u64;

/// The object a lock is taken on.
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum LockObject {
    /// A whole table, by OID.
    Table(u32),
    /// A single row: `(table OID, row id)`.
    Row(u32, String),
    /// An advisory lock keyed by a 64-bit value.
    Advisory(i64),
}

impl LockObject {
    fn kind(&self) -> &'static str {
        match self {
            LockObject::Table(_) => "relation",
            LockObject::Row(_, _) => "tuple",
            LockObject::Advisory(_) => "advisory",
        }
    }

    fn describe(&self) -> String {
        match self {
            LockObject::Table(oid) => format!("relation {oid}"),
            LockObject::Row(oid, rid) => format!("row ({oid},{rid})"),
            LockObject::Advisory(key) => format!("advisory {key}"),
        }
    }
}

/// Lock modes across the three object kinds. On any given object every recorded
/// mode is of the matching kind.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum LockMode {
    // Table-level (ordered weakest to strongest).
    AccessShare,
    RowShare,
    RowExclusive,
    ShareUpdateExclusive,
    Share,
    ShareRowExclusive,
    Exclusive,
    AccessExclusive,
    // Row-level.
    ForKeyShare,
    ForShare,
    ForNoKeyUpdate,
    ForUpdate,
    // Advisory.
    AdvisoryShared,
    AdvisoryExclusive,
}

impl LockMode {
    fn table_index(self) -> Option<usize> {
        Some(match self {
            LockMode::AccessShare => 0,
            LockMode::RowShare => 1,
            LockMode::RowExclusive => 2,
            LockMode::ShareUpdateExclusive => 3,
            LockMode::Share => 4,
            LockMode::ShareRowExclusive => 5,
            LockMode::Exclusive => 6,
            LockMode::AccessExclusive => 7,
            _ => return None,
        })
    }

    fn row_index(self) -> Option<usize> {
        Some(match self {
            LockMode::ForKeyShare => 0,
            LockMode::ForShare => 1,
            LockMode::ForNoKeyUpdate => 2,
            LockMode::ForUpdate => 3,
            _ => return None,
        })
    }

    /// PostgreSQL's canonical name for this mode (used by `pg_locks`).
    pub fn pg_name(self) -> &'static str {
        match self {
            LockMode::AccessShare => "AccessShareLock",
            LockMode::RowShare => "RowShareLock",
            LockMode::RowExclusive => "RowExclusiveLock",
            LockMode::ShareUpdateExclusive => "ShareUpdateExclusiveLock",
            LockMode::Share => "ShareLock",
            LockMode::ShareRowExclusive => "ShareRowExclusiveLock",
            LockMode::Exclusive => "ExclusiveLock",
            LockMode::AccessExclusive => "AccessExclusiveLock",
            LockMode::ForKeyShare => "KeyShareLock",
            LockMode::ForShare => "ShareLock",
            LockMode::ForNoKeyUpdate => "NoKeyUpdateLock",
            LockMode::ForUpdate => "ExclusiveLock",
            LockMode::AdvisoryShared | LockMode::AdvisoryExclusive => "AdvisoryLock",
        }
    }

    /// Does a request for `self` conflict with an already-held `other` (on the
    /// same object)?
    pub fn conflicts(self, other: LockMode) -> bool {
        // Table conflict sets (bit b set => index b conflicts), symmetric.
        const TABLE: [u8; 8] = [
            1 << 7,
            (1 << 6) | (1 << 7),
            (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7),
            (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7),
            (1 << 2) | (1 << 3) | (1 << 5) | (1 << 6) | (1 << 7),
            (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7),
            (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7),
            0xFF,
        ];
        const ROW: [u8; 4] = [
            1 << 3,
            (1 << 2) | (1 << 3),
            (1 << 1) | (1 << 2) | (1 << 3),
            (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3),
        ];
        if let (Some(a), Some(b)) = (self.table_index(), other.table_index()) {
            return (TABLE[a] >> b) & 1 == 1;
        }
        if let (Some(a), Some(b)) = (self.row_index(), other.row_index()) {
            return (ROW[a] >> b) & 1 == 1;
        }
        // Advisory: shared/shared coexist; any exclusive conflicts.
        matches!(self, LockMode::AdvisoryExclusive) || matches!(other, LockMode::AdvisoryExclusive)
    }
}

/// When is a lock released?
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum LockScope {
    /// Released at transaction end (table, row, advisory-xact locks).
    Transaction,
    /// Held until the session releases it or disconnects (advisory session locks).
    Session,
}

/// How to behave when a lock cannot be granted immediately.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum WaitPolicy {
    /// Block until the lock is available (the default).
    Wait,
    /// Fail immediately with `LockNotAvailable` (55P03).
    NoWait,
    /// Do not wait; report that the object is locked so the caller can skip it.
    SkipLocked,
}

/// Outcome of an acquire attempt.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Acquired {
    Granted,
    /// `SKIP LOCKED` and the object was locked by another session.
    Skipped,
}

#[derive(Clone, Debug)]
struct Holding {
    holder: SessionId,
    mode: LockMode,
    scope: LockScope,
}

#[derive(Default)]
struct Inner {
    granted: HashMap<LockObject, Vec<Holding>>,
    /// What each currently-blocked session is waiting for (for deadlock cycles).
    waiting: HashMap<SessionId, (LockObject, LockMode)>,
}

impl Inner {
    /// Can `holder` be granted `mode` on `object` right now? (Own locks never
    /// conflict.)
    fn grantable(&self, object: &LockObject, mode: LockMode, holder: SessionId) -> bool {
        match self.granted.get(object) {
            None => true,
            Some(holders) => holders
                .iter()
                .all(|h| h.holder == holder || !mode.conflicts(h.mode)),
        }
    }

    fn grant(&mut self, object: LockObject, mode: LockMode, holder: SessionId, scope: LockScope) {
        self.granted.entry(object).or_default().push(Holding {
            holder,
            mode,
            scope,
        });
    }

    /// Sessions that `who` is currently waiting behind (holders of conflicting
    /// locks on the object it wants).
    fn blockers(&self, who: SessionId) -> Vec<SessionId> {
        let Some((object, mode)) = self.waiting.get(&who) else {
            return Vec::new();
        };
        let Some(holders) = self.granted.get(object) else {
            return Vec::new();
        };
        let mut out: Vec<SessionId> = holders
            .iter()
            .filter(|h| h.holder != who && mode.conflicts(h.mode))
            .map(|h| h.holder)
            .collect();
        out.sort_unstable();
        out.dedup();
        out
    }

    /// Is `start` part of a wait-for cycle (i.e. reachable from itself)?
    fn deadlocked(&self, start: SessionId) -> bool {
        let mut visited = HashSet::new();
        let mut stack: Vec<SessionId> = self.blockers(start);
        while let Some(node) = stack.pop() {
            if node == start {
                return true;
            }
            if visited.insert(node) {
                stack.extend(self.blockers(node));
            }
        }
        false
    }

    fn release_where(&mut self, mut pred: impl FnMut(&Holding) -> bool) {
        self.granted.retain(|_, holders| {
            holders.retain(|h| !pred(h));
            !holders.is_empty()
        });
    }
}

/// A row in `pg_locks`.
#[derive(Clone, Debug)]
pub struct LockRecord {
    pub locktype: String,
    pub object: String,
    pub mode: String,
    pub holder: SessionId,
    pub granted: bool,
}

/// The lock manager. Shared across all sessions via `Arc`.
pub struct LockManager {
    inner: Mutex<Inner>,
    notify: Notify,
    next_session: AtomicU64,
}

impl Default for LockManager {
    fn default() -> Self {
        Self::new()
    }
}

impl LockManager {
    pub fn new() -> Self {
        Self {
            inner: Mutex::new(Inner::default()),
            notify: Notify::new(),
            next_session: AtomicU64::new(1),
        }
    }

    /// Allocate a fresh session id.
    pub fn new_session(&self) -> SessionId {
        self.next_session.fetch_add(1, Ordering::Relaxed)
    }

    /// Non-blocking acquire. Returns `true` if granted, `false` if a conflicting
    /// lock is held by another session.
    pub fn try_acquire(
        &self,
        holder: SessionId,
        object: LockObject,
        mode: LockMode,
        scope: LockScope,
    ) -> bool {
        let mut inner = self.inner.lock().unwrap();
        if inner.grantable(&object, mode, holder) {
            inner.grant(object, mode, holder, scope);
            true
        } else {
            false
        }
    }

    /// Is `object` locked in a way that conflicts with `mode` by some *other*
    /// session right now? (Used for `SKIP LOCKED` scans.)
    pub fn is_conflicting(&self, holder: SessionId, object: &LockObject, mode: LockMode) -> bool {
        let inner = self.inner.lock().unwrap();
        !inner.grantable(object, mode, holder)
    }

    /// Acquire a lock, blocking per `wait`, with deadlock detection and an
    /// optional `timeout`.
    pub async fn acquire(
        &self,
        holder: SessionId,
        object: LockObject,
        mode: LockMode,
        scope: LockScope,
        wait: WaitPolicy,
        timeout: Option<Duration>,
    ) -> Result<Acquired, RelError> {
        let deadline = timeout.map(|t| Instant::now() + t);
        loop {
            // Register interest *before* checking, so a release between the
            // check and the await is not lost.
            let notified = self.notify.notified();
            tokio::pin!(notified);
            notified.as_mut().enable();

            {
                let mut inner = self.inner.lock().unwrap();
                if inner.grantable(&object, mode, holder) {
                    inner.grant(object.clone(), mode, holder, scope);
                    inner.waiting.remove(&holder);
                    return Ok(Acquired::Granted);
                }
                match wait {
                    WaitPolicy::NoWait => {
                        inner.waiting.remove(&holder);
                        return Err(RelError::LockNotAvailable(object.describe()));
                    }
                    WaitPolicy::SkipLocked => {
                        inner.waiting.remove(&holder);
                        return Ok(Acquired::Skipped);
                    }
                    WaitPolicy::Wait => {
                        inner.waiting.insert(holder, (object.clone(), mode));
                        if inner.deadlocked(holder) {
                            inner.waiting.remove(&holder);
                            return Err(RelError::DeadlockDetected {
                                detail: format!(
                                    "process {holder} waits for {}; a cycle was found",
                                    object.describe()
                                ),
                            });
                        }
                    }
                }
            }

            // Wait for a release notification (or the lock timeout).
            match deadline {
                None => notified.await,
                Some(d) => {
                    let now = Instant::now();
                    if now >= d {
                        self.inner.lock().unwrap().waiting.remove(&holder);
                        return Err(RelError::LockNotAvailable(format!(
                            "lock timeout on {}",
                            object.describe()
                        )));
                    }
                    let _ = tokio::time::timeout(d - now, notified).await;
                }
            }
        }
    }

    /// Release one held lock matching `(holder, object, mode)`; returns whether
    /// one was found (used by `pg_advisory_unlock`).
    pub fn release_one(&self, holder: SessionId, object: &LockObject, mode: LockMode) -> bool {
        let mut inner = self.inner.lock().unwrap();
        let mut removed = false;
        if let Some(holders) = inner.granted.get_mut(object) {
            if let Some(pos) = holders
                .iter()
                .position(|h| h.holder == holder && h.mode == mode)
            {
                holders.remove(pos);
                removed = true;
            }
            if holders.is_empty() {
                inner.granted.remove(object);
            }
        }
        drop(inner);
        if removed {
            self.notify.notify_waiters();
        }
        removed
    }

    /// Release all of a session's transaction-scoped locks (at COMMIT/ROLLBACK).
    pub fn release_transaction(&self, holder: SessionId) {
        {
            let mut inner = self.inner.lock().unwrap();
            inner.release_where(|h| h.holder == holder && h.scope == LockScope::Transaction);
            inner.waiting.remove(&holder);
        }
        self.notify.notify_waiters();
    }

    /// Release a session's session-scoped (advisory) locks (`pg_advisory_unlock_all`).
    pub fn release_session_advisory(&self, holder: SessionId) {
        {
            let mut inner = self.inner.lock().unwrap();
            inner.release_where(|h| h.holder == holder && h.scope == LockScope::Session);
        }
        self.notify.notify_waiters();
    }

    /// Release every lock held by a session (at disconnect).
    pub fn release_session(&self, holder: SessionId) {
        {
            let mut inner = self.inner.lock().unwrap();
            inner.release_where(|h| h.holder == holder);
            inner.waiting.remove(&holder);
        }
        self.notify.notify_waiters();
    }

    /// Snapshot of all granted and waiting locks (for `pg_locks`).
    pub fn snapshot(&self) -> Vec<LockRecord> {
        let inner = self.inner.lock().unwrap();
        let mut out = Vec::new();
        for (object, holders) in &inner.granted {
            for h in holders {
                out.push(LockRecord {
                    locktype: object.kind().to_string(),
                    object: object.describe(),
                    mode: h.mode.pg_name().to_string(),
                    holder: h.holder,
                    granted: true,
                });
            }
        }
        for (holder, (object, mode)) in &inner.waiting {
            out.push(LockRecord {
                locktype: object.kind().to_string(),
                object: object.describe(),
                mode: mode.pg_name().to_string(),
                holder: *holder,
                granted: false,
            });
        }
        out
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::time::Duration;

    #[test]
    fn table_conflict_matrix_matches_postgres() {
        use LockMode::*;
        // A few well-known cells.
        assert!(!AccessShare.conflicts(RowExclusive)); // readers and writers coexist
        assert!(AccessShare.conflicts(AccessExclusive)); // DDL blocks readers
        assert!(!Share.conflicts(Share)); // two SHARE coexist
        assert!(Share.conflicts(RowExclusive)); // SHARE blocks writes
        assert!(RowExclusive.conflicts(Share));
        assert!(ShareUpdateExclusive.conflicts(ShareUpdateExclusive)); // self-conflict
        assert!(AccessExclusive.conflicts(AccessShare));
        assert!(!RowShare.conflicts(RowExclusive));
        // Symmetry over all pairs.
        let all = [
            AccessShare,
            RowShare,
            RowExclusive,
            ShareUpdateExclusive,
            Share,
            ShareRowExclusive,
            Exclusive,
            AccessExclusive,
        ];
        for a in all {
            for b in all {
                assert_eq!(a.conflicts(b), b.conflicts(a), "asymmetry {a:?} {b:?}");
            }
        }
    }

    #[test]
    fn row_conflict_matrix() {
        use LockMode::*;
        assert!(!ForKeyShare.conflicts(ForKeyShare));
        assert!(!ForKeyShare.conflicts(ForShare));
        assert!(ForKeyShare.conflicts(ForUpdate));
        assert!(ForShare.conflicts(ForNoKeyUpdate));
        assert!(ForUpdate.conflicts(ForUpdate));
        let all = [ForKeyShare, ForShare, ForNoKeyUpdate, ForUpdate];
        for a in all {
            for b in all {
                assert_eq!(a.conflicts(b), b.conflicts(a));
            }
        }
    }

    #[test]
    fn advisory_conflict() {
        use LockMode::*;
        assert!(!AdvisoryShared.conflicts(AdvisoryShared));
        assert!(AdvisoryShared.conflicts(AdvisoryExclusive));
        assert!(AdvisoryExclusive.conflicts(AdvisoryExclusive));
    }

    #[tokio::test]
    async fn blocks_then_grants_after_release() {
        let lm = Arc::new(LockManager::new());
        let s1 = lm.new_session();
        let s2 = lm.new_session();
        let obj = LockObject::Row(1, "r".into());
        lm.acquire(
            s1,
            obj.clone(),
            LockMode::ForUpdate,
            LockScope::Transaction,
            WaitPolicy::Wait,
            None,
        )
        .await
        .unwrap();

        let lm2 = lm.clone();
        let obj2 = obj.clone();
        let handle = tokio::spawn(async move {
            lm2.acquire(
                s2,
                obj2,
                LockMode::ForUpdate,
                LockScope::Transaction,
                WaitPolicy::Wait,
                None,
            )
            .await
        });
        tokio::time::sleep(Duration::from_millis(40)).await;
        assert!(!handle.is_finished(), "s2 should be blocked behind s1");

        lm.release_transaction(s1);
        let got = tokio::time::timeout(Duration::from_secs(2), handle)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(got.unwrap(), Acquired::Granted);
    }

    #[tokio::test]
    async fn nowait_fails_immediately() {
        let lm = Arc::new(LockManager::new());
        let s1 = lm.new_session();
        let s2 = lm.new_session();
        let obj = LockObject::Table(5);
        lm.acquire(
            s1,
            obj.clone(),
            LockMode::AccessExclusive,
            LockScope::Transaction,
            WaitPolicy::Wait,
            None,
        )
        .await
        .unwrap();
        let err = lm
            .acquire(
                s2,
                obj,
                LockMode::AccessShare,
                LockScope::Transaction,
                WaitPolicy::NoWait,
                None,
            )
            .await
            .unwrap_err();
        assert_eq!(err.sqlstate(), "55P03");
    }

    #[tokio::test]
    async fn skip_locked_reports_skip() {
        let lm = Arc::new(LockManager::new());
        let s1 = lm.new_session();
        let s2 = lm.new_session();
        let obj = LockObject::Row(1, "r".into());
        lm.acquire(
            s1,
            obj.clone(),
            LockMode::ForUpdate,
            LockScope::Transaction,
            WaitPolicy::Wait,
            None,
        )
        .await
        .unwrap();
        let got = lm
            .acquire(
                s2,
                obj,
                LockMode::ForUpdate,
                LockScope::Transaction,
                WaitPolicy::SkipLocked,
                None,
            )
            .await
            .unwrap();
        assert_eq!(got, Acquired::Skipped);
    }

    #[tokio::test]
    async fn deadlock_is_detected() {
        let lm = Arc::new(LockManager::new());
        let s1 = lm.new_session();
        let s2 = lm.new_session();
        let a = LockObject::Row(1, "a".into());
        let b = LockObject::Row(1, "b".into());
        lm.acquire(
            s1,
            a.clone(),
            LockMode::ForUpdate,
            LockScope::Transaction,
            WaitPolicy::Wait,
            None,
        )
        .await
        .unwrap();
        lm.acquire(
            s2,
            b.clone(),
            LockMode::ForUpdate,
            LockScope::Transaction,
            WaitPolicy::Wait,
            None,
        )
        .await
        .unwrap();

        let lm1 = lm.clone();
        let b1 = b.clone();
        let h1 = tokio::spawn(async move {
            lm1.acquire(
                s1,
                b1,
                LockMode::ForUpdate,
                LockScope::Transaction,
                WaitPolicy::Wait,
                None,
            )
            .await
        });
        tokio::time::sleep(Duration::from_millis(30)).await;
        // s2 now requests a -> closes the cycle and must detect a deadlock.
        let res2 = lm
            .acquire(
                s2,
                a,
                LockMode::ForUpdate,
                LockScope::Transaction,
                WaitPolicy::Wait,
                None,
            )
            .await;
        assert!(res2.is_err(), "one waiter must be aborted");
        assert_eq!(res2.unwrap_err().sqlstate(), "40P01");
        // Releasing the victim lets the other proceed.
        lm.release_transaction(s2);
        let _ = tokio::time::timeout(Duration::from_secs(2), h1)
            .await
            .unwrap()
            .unwrap();
        let _ = b;
    }

    #[tokio::test]
    async fn lock_timeout_fires() {
        let lm = Arc::new(LockManager::new());
        let s1 = lm.new_session();
        let s2 = lm.new_session();
        let obj = LockObject::Table(9);
        lm.acquire(
            s1,
            obj.clone(),
            LockMode::Exclusive,
            LockScope::Transaction,
            WaitPolicy::Wait,
            None,
        )
        .await
        .unwrap();
        let err = lm
            .acquire(
                s2,
                obj,
                LockMode::Exclusive,
                LockScope::Transaction,
                WaitPolicy::Wait,
                Some(Duration::from_millis(50)),
            )
            .await
            .unwrap_err();
        assert_eq!(err.sqlstate(), "55P03");
    }

    #[test]
    fn snapshot_reports_granted() {
        let lm = LockManager::new();
        let s1 = lm.new_session();
        assert!(lm.try_acquire(
            s1,
            LockObject::Table(7),
            LockMode::RowExclusive,
            LockScope::Transaction
        ));
        let snap = lm.snapshot();
        assert_eq!(snap.len(), 1);
        assert_eq!(snap[0].mode, "RowExclusiveLock");
        assert!(snap[0].granted);
    }
}