sib 0.0.17

A high-performance, secure, and cross-platform modules optimized for efficiency, scalability, and reliability.
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
use crossbeam::queue::ArrayQueue;
use std::{cell::UnsafeCell, fmt, num::NonZeroU64, sync::Arc, time::Duration};

#[cfg(feature = "rt-tokio")]
use tokio::{sync::Notify, time};

#[cfg(feature = "rt-may")]
use may::coroutine;

/// Your FDB handle type (adapt the path to your project)
use crate::database::fdb::db::FDB;

/// Internal storage: fixed slots + free-list of indices
struct PoolInner<T> {
    slots: Box<[UnsafeCell<T>]>,
    freelist: ArrayQueue<usize>,
    #[cfg(feature = "rt-tokio")]
    notify: Notify, // wake async waiters
}

unsafe impl<T: Send> Send for PoolInner<T> {}
unsafe impl<T: Send> Sync for PoolInner<T> {}

/// Zero-copy guard
pub struct Loan<'a, T> {
    inner: &'a PoolInner<T>,
    idx: usize,
}

impl<'a, T> Loan<'a, T> {
    #[inline]
    fn get_mut(&mut self) -> &mut T {
        // SAFETY: unique access guaranteed by free-list protocol
        unsafe { &mut *self.inner.slots[self.idx].get() }
    }
}
impl<'a, T> std::ops::Deref for Loan<'a, T> {
    type Target = T;
    fn deref(&self) -> &T {
        // SAFETY: unique access while loan is alive, but here shared ref is fine
        unsafe { &*self.inner.slots[self.idx].get() }
    }
}
impl<'a, T> std::ops::DerefMut for Loan<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.get_mut()
    }
}
impl<'a, T> Drop for Loan<'a, T> {
    fn drop(&mut self) {
        // Return index to freelist and notify any waiters.
        // This should never fail (queue capacity == slots.len()).
        let pushed = self.inner.freelist.push(self.idx).is_ok();
        debug_assert!(pushed, "freelist push should never fail");

        #[cfg(feature = "rt-tokio")]
        self.inner.notify.notify_one();
    }
}

impl<'a, T> fmt::Debug for Loan<'a, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Loan").field("idx", &self.idx).finish()
    }
}

/// The pool itself (generic)
#[derive(Clone)]
struct Pool<T> {
    inner: Arc<PoolInner<T>>,
}

impl<T> Pool<T> {
    /// Build a pool with size
    fn new_with<F>(size: NonZeroU64, mut init: F) -> std::io::Result<Self>
    where
        F: FnMut(usize) -> std::io::Result<T>,
    {
        let n = size.get() as usize;
        // init slots
        let mut vec = Vec::with_capacity(n);
        for i in 0..n {
            vec.push(UnsafeCell::new(init(i)?));
        }
        let slots = vec.into_boxed_slice();

        // free-list initially contains all indices
        let freelist = ArrayQueue::new(n);
        for i in 0..n {
            // capacity is exactly n; this cannot fail
            freelist.push(i).unwrap();
        }

        Ok(Self {
            inner: Arc::new(PoolInner {
                slots,
                freelist,
                #[cfg(feature = "rt-tokio")]
                notify: Notify::new(),
            }),
        })
    }

    /// Try to acquire immediately (non-blocking). Returns `None` if empty.
    #[inline]
    fn try_acquire(&self) -> Option<Loan<'_, T>> {
        self.inner.freelist.pop().map(|idx| Loan {
            inner: &self.inner,
            idx,
        })
    }

    /// Blocking acquire with timeout
    #[cfg(feature = "rt-may")]
    fn acquire_blocking(&self, timeout: Duration) -> std::io::Result<Loan<'_, T>> {
        if let Some(loan) = self.try_acquire() {
            return Ok(loan);
        }
        let start = std::time::Instant::now();
        // exponential backoff in microseconds
        let mut backoff = 10u64; // μs
        loop {
            if let Some(loan) = self.try_acquire() {
                return Ok(loan);
            }
            if start.elapsed() >= timeout {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::TimedOut,
                    "FDB pool acquire (may) got timed out",
                ));
            }
            // yield/sleep cooperatively
            coroutine::yield_now();
            // short sleep after a few spins
            if backoff < 2000 {
                coroutine::sleep(Duration::from_micros(backoff));
                backoff = (backoff * 2).min(2000);
            }
        }
    }

    /// Async acquire with timeout (using Notify)
    #[cfg(feature = "rt-tokio")]
    async fn acquire(&self, timeout: Duration) -> std::io::Result<Loan<'_, T>> {
        // Fast path
        if let Some(loan) = self.try_acquire() {
            return Ok(loan);
        }

        // Wait loop with Notify
        let fut = async {
            loop {
                // wait until someone returns an index
                self.inner.notify.notified().await;
                if let Some(loan) = self.try_acquire() {
                    return Ok(loan);
                }
                // spurious wake or contention: loop again
            }
        };

        match time::timeout(timeout, fut).await {
            Ok(res) => res,
            Err(_) => Err(std::io::Error::new(
                std::io::ErrorKind::TimedOut,
                "FDB pool acquire (may) got timed out",
            )),
        }
    }
}

/// FDB pool wrapper
#[derive(Clone)]
pub struct FDBPool {
    pool: Pool<FDB>,
}

impl FDBPool {
    /// Build an FDB pool by opening `pool_size` FDB handles up-front.
    pub fn new(cluster_path: String, pool_size: NonZeroU64) -> std::io::Result<Self> {
        let pool = Pool::new_with(pool_size, |_i| {
            // Open one FDB handle per slot
            FDB::new(&cluster_path)
        })?;
        Ok(Self { pool })
    }

    /// Non-blocking try-acquire. Returns None if pool is empty.
    pub fn try_loan(&self) -> Option<Loan<'_, FDB>> {
        self.pool.try_acquire()
    }

    /// blocking loan with timeout
    #[cfg(feature = "rt-may")]
    pub fn loan_blocking(&self, timeout: Duration) -> std::io::Result<Loan<'_, FDB>> {
        self.pool.acquire_blocking(timeout)
    }

    /// async loan with timeout
    #[cfg(feature = "rt-tokio")]
    pub async fn loan(&self, timeout: Duration) -> std::io::Result<Loan<'_, FDB>> {
        self.pool.acquire(timeout).await
    }

    /// loan fdb connection (immutable)
    #[cfg(feature = "rt-may")]
    pub fn with_loan<R, F: FnOnce(&FDB) -> R>(
        &self,
        timeout: Duration,
        f: F,
    ) -> std::io::Result<R> {
        let loan = self.loan_blocking(timeout)?;
        Ok(f(&*loan))
    }

    /// loan fdb connection (mutable)
    #[cfg(feature = "rt-may")]
    pub fn with_loan_mut<R, F: FnOnce(&mut FDB) -> R>(
        &self,
        timeout: Duration,
        f: F,
    ) -> std::io::Result<R> {
        let mut loan = self.loan_blocking(timeout)?;
        Ok(f(&mut *loan))
    }

    /// Async loan with timeout (immutable)
    #[cfg(feature = "rt-tokio")]
    pub async fn with_loan_async<R, Fut, F>(&self, timeout: Duration, f: F) -> std::io::Result<R>
    where
        F: for<'a> FnOnce(&'a FDB) -> Fut,
        Fut: std::future::Future<Output = R>,
    {
        let loan = self.loan(timeout).await?;
        Ok(f(&*loan).await)
    }

    /// Async loan with timeout (mutable)
    #[cfg(feature = "rt-tokio")]
    pub async fn with_loan_mut_async<R, Fut, F>(
        &self,
        timeout: Duration,
        f: F,
    ) -> std::io::Result<R>
    where
        F: for<'a> FnOnce(&'a mut FDB) -> Fut,
        Fut: std::future::Future<Output = R>,
    {
        let mut loan = self.loan(timeout).await?;
        Ok(f(&mut *loan).await)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::num::NonZeroU64;

    pub fn default_fdb_cluster_path() -> String {
        // OS defaults
        #[cfg(target_os = "macos")]
        {
            "/usr/local/etc/foundationdb/fdb.cluster".to_owned()
        }
        #[cfg(target_os = "linux")]
        {
            "/etc/foundationdb/fdb.cluster".to_owned()
        }
        #[cfg(not(any(target_os = "macos", target_os = "linux")))]
        {
            "".to_owned()
        }
    }

    #[test]
    fn test_fdb_pool_acquire_and_release() {
        // pool of 3 integers initialized to 10, 20, 30
        let pool =
            Pool::new_with(NonZeroU64::new(3).unwrap(), |i| Ok((i as u32 + 1) * 10)).unwrap();

        // take all three
        let l1 = pool.try_acquire().expect("slot 1");
        let l2 = pool.try_acquire().expect("slot 2");
        let l3 = pool.try_acquire().expect("slot 3");

        // now empty
        assert!(pool.try_acquire().is_none(), "pool should be empty");

        // drop one, should be able to acquire again
        drop(l2);
        let l4 = pool.try_acquire().expect("slot after release");
        // value must be one of the initial values
        let v = *l4;
        assert!([10, 20, 30].contains(&v));
        drop(l1);
        drop(l3);
        drop(l4);

        // After returning all, we can acquire 3 again
        let _a = pool.try_acquire().unwrap();
        let _b = pool.try_acquire().unwrap();
        let _c = pool.try_acquire().unwrap();
        assert!(pool.try_acquire().is_none());
    }

    #[test]
    fn test_zero_copy_mutation() {
        #[derive(Debug, Clone, Copy)]
        struct Item(u64);

        let pool = Pool::new_with(NonZeroU64::new(2).unwrap(), |_i| Ok(Item(0))).unwrap();

        // Acquire both, mutate through &mut Loan to ensure zero-copy uniqueness works.
        let mut a = pool.try_acquire().expect("a");
        let mut b = pool.try_acquire().expect("b");

        a.0 += 5;
        b.0 += 7;
        assert_eq!(a.0, 5);
        assert_eq!(b.0, 7);

        // Release both and reacquire to verify persisted mutation in-place.
        drop(a);
        drop(b);

        // Drain pool and sum values regardless of order.
        let x = pool.try_acquire().unwrap().0;
        let y = pool.try_acquire().unwrap().0;
        assert_eq!(x + y, 12, "mutations must persist in the same slots");
    }

    #[cfg(feature = "rt-may")]
    #[test]
    fn test_fdb_pool_concurrent_acquire_and_increment() {
        #[derive(Default)]
        struct Counter {
            hits: u64,
        }

        const NUMBER_OF_WORKERS: usize = 1;
        crate::init_global_poller(NUMBER_OF_WORKERS, 1 * 1024 * 1024);

        let pool = std::sync::Arc::new(
            Pool::new_with(NonZeroU64::new(3).unwrap(), |_i| Ok(Counter::default())).unwrap(),
        );

        // Spawn 30 coroutines; each increments once.
        let jobs = 30usize;
        let mut hs = Vec::new();
        for _ in 0..jobs {
            let pool = pool.clone();
            hs.push(unsafe {
                may::coroutine::spawn(move || {
                    // SAFETY: The pool's free-list protocol guarantees unique access.
                    let mut loan = pool
                        .acquire_blocking(Duration::from_millis(200))
                        .expect("acquire");
                    loan.hits += 1;
                    // drop -> return to freelist
                })
            });
        }
        for h in hs {
            h.join().unwrap();
        }

        // Drain and sum
        let mut total = 0u64;
        for _ in 0..3 {
            let l = pool.try_acquire().expect("slot back after tasks");
            total += l.hits;
        }
        assert_eq!(total, jobs as u64);
    }

    #[cfg(feature = "rt-tokio")]
    #[tokio::test]
    async fn test_fdb_pool_concurrent_acquire_and_increment() {
        // Each slot holds a small counter struct.
        #[derive(Default)]
        struct Counter {
            hits: u64,
        }

        let pool = std::sync::Arc::new(
            Pool::new_with(NonZeroU64::new(4).unwrap(), |_i| Ok(Counter::default())).unwrap(),
        );

        // 64 tasks, each acquires a slot, increments the counter once, and releases.
        let tasks = 64usize;
        let mut joins = Vec::new();
        for _ in 0..tasks {
            let p = pool.clone();
            joins.push(tokio::spawn(async move {
                let mut loan = p
                    .acquire(Duration::from_millis(200))
                    .await
                    .expect("acquire should succeed");
                loan.hits += 1;
            }));
        }
        for j in joins {
            j.await.unwrap();
        }

        // Drain all 4 slots and sum their hits; should equal 64.
        let mut total = 0u64;
        for _ in 0..4 {
            let l = pool.try_acquire().expect("slot back after tasks");
            total += l.hits;
        }
        assert_eq!(total, 64);
    }

    #[cfg(feature = "rt-may")]
    #[test]
    fn test_fdb_pool_timeout_when_all_held() {
        const TIME: Duration = Duration::from_millis(5);
        const NUMBER_OF_WORKERS: usize = 1;
        crate::init_global_poller(NUMBER_OF_WORKERS, 1 * 1024 * 1024);

        let pool = Pool::new_with(NonZeroU64::new(1).unwrap(), |_| Ok(123u32)).unwrap();

        // keep the guard alive so the single slot stays held
        let _guard = pool.try_acquire().expect("first acquire");

        let start = std::time::Instant::now();
        let res = pool.acquire_blocking(TIME);

        assert!(res.is_err(), "expected timeout");
        assert!(start.elapsed() >= TIME);
    }

    #[cfg(feature = "rt-tokio")]
    #[tokio::test]
    async fn test_fdb_pool_timeout_when_all_held() {
        use std::time::Instant;
        // Pool of size 1; hold it and try to acquire with a short timeout.
        let pool = Pool::new_with(NonZeroU64::new(1).unwrap(), |_| Ok(())).unwrap();

        let loan = pool.try_acquire().expect("first acquire");
        let start = Instant::now();

        let res = pool.acquire(Duration::from_millis(50)).await;
        assert!(res.is_err(), "should time out");
        let elapsed = start.elapsed();
        assert!(
            elapsed >= Duration::from_millis(45),
            "timeout should approximate requested duration"
        );

        drop(loan);
    }

    #[test]
    fn test_fdb_run_transaction_set_and_get() {
        use crate::database::fdb::trans::FDBTransaction;

        let fdb_guard = crate::fdb_network_start!();

        // give it time to start
        std::thread::sleep(Duration::from_secs(1));

        let cluster_path = default_fdb_cluster_path();
        let pool = FDBPool::new(cluster_path, NonZeroU64::new(1).unwrap()).expect("create pool");

        let lease = pool.try_loan().expect("acquire");
        let key = b"key1";
        let value = b"hello";

        {
            // set
            let tr = FDBTransaction::new(&*lease).expect("new transaction failed");
            tr.set(key, value);
            tr.commit_blocking().expect("commit failed");
        }

        {
            // get
            let tr = FDBTransaction::new(&*lease).expect("new transaction failed");
            let fut: crate::database::fdb::future::FDBFuture = tr.get(key, false).unwrap();
            fut.block_until_ready();
            let result = fut.get_value().expect("get failed");
            assert_eq!(result.iter().as_slice(), value.as_slice(), "value mismatch");
        }

        // Now stop FDB network
        crate::fdb_network_stop!(fdb_guard);
    }

    #[test]
    fn test_fdb_run_transaction_set_and_get_with_run() {
        let fdb_guard = crate::fdb_network_start!();

        // give it time to start
        std::thread::sleep(Duration::from_secs(1));

        let cluster_path = default_fdb_cluster_path();
        let pool = FDBPool::new(cluster_path, NonZeroU64::new(1).unwrap()).expect("create pool");

        let lease = pool.try_loan().expect("acquire");
        let key = b"key2";
        let value = b"hello";

        // set
        crate::database::fdb::trans::run(&*lease, |tr| {
            tr.set(key, value);
            Ok(crate::database::fdb::trans::FDBTransactionOutcome::Ok(()))
        })
        .expect("commit failed");

        // get
        crate::database::fdb::trans::run(&*lease, |tr| {
            tr.get_blocking_value_optional(key, false)
                .and_then(|res_opt| {
                    let res = res_opt.expect("key missing");
                    assert_eq!(res.iter().as_slice(), value.as_slice(), "value mismatch");
                    Ok(crate::database::fdb::trans::FDBTransactionOutcome::Ok(()))
                })
        })
        .expect("commit failed");

        // Now stop FDB network
        crate::fdb_network_stop!(fdb_guard);
    }

    #[test]
    fn test_fdb_run_transaction_retries_on_conflict_increment() {
        use crate::database::fdb::pool::FDBPool;
        use crate::database::fdb::trans::{self, FDBTransactionOutcome};

        use std::num::NonZeroU64;
        use std::sync::{
            Arc,
            atomic::{AtomicUsize, Ordering},
        };
        use std::thread;
        use std::time::Duration;

        let fdb_guard = crate::fdb_network_start!();

        // give it time to start
        thread::sleep(Duration::from_secs(1));

        // Pool must allow 2 concurrent leases for the two threads
        let cluster_path = default_fdb_cluster_path();
        let pool =
            Arc::new(FDBPool::new(cluster_path, NonZeroU64::new(2).unwrap()).expect("create pool"));

        // Use a unique key so parallel test runs don’t collide
        let key = "conflict_inc".to_owned()
            + &format!(
                "_{}",
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .as_nanos()
            );

        // Initialize key = "0"
        {
            let lease = pool.try_loan().expect("acquire init lease");
            trans::run(&*lease, |tr| {
                tr.set(key.as_bytes(), b"0");
                Ok(FDBTransactionOutcome::Ok(()))
            })
            .expect("init commit failed");
        }

        let attempts_a = Arc::new(AtomicUsize::new(0));
        let attempts_b = Arc::new(AtomicUsize::new(0));

        // Thread A
        let pool_a = Arc::clone(&pool);
        let key_a = key.clone();
        let a_ctr = Arc::clone(&attempts_a);
        let t1 = thread::spawn(move || {
            let lease = pool_a.try_loan().expect("acquire lease A");
            trans::run(&*lease, |tr| {
                a_ctr.fetch_add(1, Ordering::Relaxed);

                // Read-modify-write (conflicts with other thread)
                let cur = tr
                    .get_blocking_value_optional(key_a.as_bytes(), false)?
                    .unwrap_or_else(|| b"0".to_vec());

                // Force overlap so both transactions likely read before either commits
                thread::sleep(Duration::from_millis(50));

                let n = std::str::from_utf8(&cur).unwrap().parse::<u64>().unwrap();
                let next = (n + 1).to_string();
                tr.set(key_a.as_bytes(), next.as_bytes());

                Ok(FDBTransactionOutcome::Ok(()))
            })
        });

        // Thread B
        let pool_b = Arc::clone(&pool);
        let key_b = key.clone();
        let b_ctr = Arc::clone(&attempts_b);
        let t2 = thread::spawn(move || {
            let lease = pool_b.try_loan().expect("acquire lease B");
            trans::run(&*lease, |tr| {
                b_ctr.fetch_add(1, Ordering::Relaxed);

                let cur = tr
                    .get_blocking_value_optional(key_b.as_bytes(), false)?
                    .unwrap_or_else(|| b"0".to_vec());

                thread::sleep(Duration::from_millis(50));

                let n = std::str::from_utf8(&cur).unwrap().parse::<u64>().unwrap();
                let next = (n + 1).to_string();
                tr.set(key_b.as_bytes(), next.as_bytes());

                Ok(FDBTransactionOutcome::Ok(()))
            })
        });

        t1.join().unwrap().expect("thread 1 failed");
        t2.join().unwrap().expect("thread 2 failed");

        // Final value must be 2 if both increments committed (with retries)
        {
            let lease = pool.try_loan().expect("acquire final lease");
            trans::run(&*lease, |tr| {
                let final_val = tr
                    .get_blocking_value_optional(key.as_bytes(), false)?
                    .expect("key missing");

                let n = std::str::from_utf8(final_val.as_slice())
                    .unwrap()
                    .parse::<u64>()
                    .unwrap();

                assert_eq!(n, 2, "final value mismatch");
                Ok(FDBTransactionOutcome::Ok(()))
            })
            .expect("final read commit failed");
        }

        eprintln!(
            "attempts: a={}, b={}",
            attempts_a.load(Ordering::Relaxed),
            attempts_b.load(Ordering::Relaxed),
        );

        // Stop FDB network
        crate::fdb_network_stop!(fdb_guard);
    }

    #[test]
    fn test_fdb_run_transaction_retry_branch_is_executed() {
        use crate::database::fdb::pool::FDBPool;
        use crate::database::fdb::trans::{self, FDBTransactionOutcome};
        use std::num::NonZeroU64;
        use std::sync::{
            Arc,
            atomic::{AtomicUsize, Ordering},
        };
        use std::time::Duration;

        // Start FDB network
        let fdb_guard = crate::fdb_network_start!();

        std::thread::sleep(Duration::from_secs(1));

        let cluster_path = default_fdb_cluster_path();
        let pool = FDBPool::new(cluster_path, NonZeroU64::new(1).unwrap()).expect("create pool");
        let lease = pool.try_loan().expect("acquire");

        let calls = Arc::new(AtomicUsize::new(0));
        let calls_cl = Arc::clone(&calls);

        // choose a retryable FDB error code
        const RETRYABLE_CODE: i32 = 1020; // not_committed (commonly retryable)

        let key = "retry_test_key";
        let value = b"ok";

        // Run: first attempt returns Retry, second attempt does the write and commits.
        let res = trans::run(&*lease, move |tr| {
            let n = calls_cl.fetch_add(1, Ordering::SeqCst);

            if n == 0 {
                // Force the Retry branch
                return Ok(FDBTransactionOutcome::Retry(RETRYABLE_CODE));
            }

            tr.set(key.as_bytes(), value);
            Ok(FDBTransactionOutcome::Ok(()))
        });

        assert!(res.is_ok(), "run() should succeed after retry");
        assert!(
            calls.load(Ordering::SeqCst) >= 2,
            "closure should be invoked at least twice (retry path)"
        );

        // Verify the write landed
        trans::run(&*lease, |tr| {
            let got = tr
                .get_blocking_value_optional(key.as_bytes(), false)?
                .expect("key missing");
            assert_eq!(got.as_slice(), value);
            Ok(FDBTransactionOutcome::Ok(()))
        })
        .expect("verify commit failed");

        // Stop network
        crate::fdb_network_stop!(fdb_guard);
    }
}