slatedb-mirror 0.18.1

Local whole-object mirror for an object store.
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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
//! A policy-driven local mirror of whole objects from an [`ObjectStore`].
//!
//! [`ObjectStoreMirror`] implements the object-store API and keeps local copies
//! with the same bytes as their remote objects. A [`MirrorPolicy`] selects the
//! read and write routes. The mirror handles local files, downloads, and cleanup.
//! The policy decides which objects to keep and when callers can read them locally.
//!
//! Use [`ObjectStoreMirror::builder`] to choose a local directory, a remote store,
//! and a policy. [`ObjectStoreMirrorBuilder::build`] recovers local copies and
//! starts the background tasks. Policies use a [`MirrorHandle`] to fetch,
//! prefetch, and evict objects.
//!
//! Mirrored paths must never be overwritten. Local copies are disposable and
//! require callers to detect corruption. See [`ObjectStoreMirror`] for routing,
//! error handling, lifecycle details, and a complete example.

use std::fmt;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use bytes::Bytes;
use futures::future;
use futures::stream::{self, BoxStream, StreamExt};
use object_store::path::Path;
use object_store::{
    CopyOptions, GetOptions, GetResult, GetResultPayload, ListResult, MultipartUpload, ObjectMeta,
    ObjectStore, PutMultipartOptions, PutOptions, PutPayload, PutResult, RenameOptions,
};
use slatedb_common::clock::{DefaultSystemClock, SystemClock};

use crate::error::MIRROR_STORE_NAME;
use crate::inner::{local_error, Inner};
use crate::layout::{LocalName, LocalObject, LOCK_FILE};
use crate::multipart::{self, committed};
use crate::ordering::OpKind;
use crate::retry::Retry;
use crate::startup::recover;
use crate::{scan, MirrorError, MirrorHandle, MirrorPolicy, MultipartRoute, ReadRoute, WriteRoute};
use slatedb_common::vfs::{StdVfs, Vfs};

/// The default for [`ObjectStoreMirrorBuilder::with_download_concurrency`].
pub const DEFAULT_DOWNLOAD_CONCURRENCY: usize = 8;

/// The default for [`ObjectStoreMirrorBuilder::with_remote_scan_interval`].
pub const DEFAULT_REMOTE_SCAN_INTERVAL: Duration = Duration::from_secs(600);

/// Wraps a route error from the policy in `MirrorError::Policy`, unless it's
/// already a mirror error.
fn policy_error(err: object_store::Error) -> object_store::Error {
    if MirrorError::find(&err).is_some() {
        err
    } else {
        MirrorError::Policy {
            source: Box::new(err),
        }
        .into()
    }
}

fn range_error(source: impl std::error::Error + Send + Sync + 'static) -> object_store::Error {
    object_store::Error::Generic {
        store: MIRROR_STORE_NAME,
        source: Box::new(source),
    }
}

fn bytes_payload(bytes: Bytes) -> GetResultPayload {
    GetResultPayload::Stream(stream::once(async move { Ok(bytes) }).boxed())
}

/// An [`ObjectStore`] wrapper that keeps whole objects in a local directory.
///
/// The remote store holds the authoritative objects. Local copies contain the
/// same bytes, without splitting objects into blocks or changing their format.
/// A [`MirrorPolicy`] chooses which objects to mirror. The mirror does not impose
/// a size limit or automatically evict objects to reclaim disk space.
///
/// # Read and write routes
///
/// A [`MirrorPolicy`] selects how each request uses the remote store and
/// local copies:
///
/// - GET and HEAD use [`ReadRoute`].
/// - PUT uses [`WriteRoute`].
/// - Multipart uploads use [`MultipartRoute`].
///
/// The `Observe` routes pass the whole object to [`MirrorPolicy::observe`].
/// Multipart uploads do not support observation.
///
/// Policies manage local copies through [`MirrorHandle`]:
///
/// - [`fetch`](MirrorHandle::fetch) waits for a local copy.
/// - [`prefetch`](MirrorHandle::prefetch) schedules a background download.
/// - [`evict`](MirrorHandle::evict) removes a local copy.
///
/// Fetching an existing local copy does not refresh it. Use
/// [`ReadRoute::Refetch`] to replace it.
///
/// Downloads, mirrored writes, deletes, and evictions coordinate per path.
/// A later download or mirrored write can cancel an eviction that has not started.
///
/// Listings always use the remote store. Deletes remove the remote object and
/// its local copy. Copy and rename operations return [`MirrorError::Unsupported`].
///
/// # Object lifetime and local integrity
///
/// Policies must only mirror paths that are never overwritten. The mirror does
/// not track remote updates to existing objects. Route mutable objects through
/// `Remote` or `Observe`.
///
/// Local copies are disposable. The default [`StdVfs`] flushes writes without
/// syncing them to disk. Local reads can return corrupted bytes without an
/// error, including after a machine crash. Startup checks metadata, filenames,
/// and file sizes, but does not check object contents.
///
/// Callers must detect corruption and signal their policy to route a retry
/// through [`ReadRoute::Refetch`]. Repeating a [`ReadRoute::Local`] read does
/// not repair the local copy. Callers must also check the refetched data for
/// corruption and limit the number of retries.
///
/// # Startup and background tasks
///
/// Call [`ObjectStoreMirrorBuilder::build`] inside a Tokio runtime. It creates
/// the local directory, acquires its exclusive lock, and recovers complete local
/// copies. Use a dedicated directory for each mirror and remote store.
///
/// The builder starts [`MirrorPolicy::run`] and a periodic remote scan. The scan
/// removes local copies of objects deleted remotely. It does not download new
/// objects or refresh existing copies. The scan requires strongly consistent
/// remote reads, writes, deletes, and listings. If the remote store lacks these
/// guarantees, disable the scan with
/// [`with_remote_scan_interval(None)`](ObjectStoreMirrorBuilder::with_remote_scan_interval).
///
/// The defaults allow [eight concurrent downloads](DEFAULT_DOWNLOAD_CONCURRENCY)
/// and [a remote scan every ten minutes](DEFAULT_REMOTE_SCAN_INTERVAL). Downloads
/// and remote scan listings retry transient errors without a limit by default.
/// Use [`ObjectStoreMirrorBuilder::with_max_retries`] to bound these retries.
///
/// Dropping the last `Arc<ObjectStoreMirror>` cancels the background tasks.
/// Cloned [`MirrorHandle`] values keep the local state and directory lock alive
/// until those handles are also dropped.
///
/// # Errors
///
/// Mirror errors travel inside [`object_store::Error`]. Use [`MirrorError::find`]
/// to recover the specific error from its source chain.
/// [`MirrorError::WriteCommitted`] means that the remote write succeeded, but
/// local installation or the observation hook failed. Account for that remote
/// write before deciding whether to retry it.
///
/// # Example
///
/// This policy mirrors every write and serves every read locally. It assumes
/// that callers write each path once. Existing remote objects need an explicit
/// [`MirrorHandle::fetch`] before a local read.
///
/// ```
/// use std::sync::Arc;
///
/// use async_trait::async_trait;
/// use object_store::memory::InMemory;
/// use object_store::path::Path;
/// use object_store::{
///     GetOptions, ObjectStore, ObjectStoreExt, PutMultipartOptions, PutOptions,
/// };
/// use slatedb_mirror::{MirrorPolicy, MultipartRoute, ObjectStoreMirror, ReadRoute, WriteRoute};
///
/// struct ImmutableObjects;
///
/// #[async_trait]
/// impl MirrorPolicy for ImmutableObjects {
///     fn read_route(&self, _: &Path, _: &GetOptions) -> object_store::Result<ReadRoute> {
///         Ok(ReadRoute::Local)
///     }
///
///     fn put_route(&self, _: &Path, _: &PutOptions) -> object_store::Result<WriteRoute> {
///         Ok(WriteRoute::Mirror)
///     }
///
///     fn put_multipart_route(
///         &self,
///         _: &Path,
///         _: &PutMultipartOptions,
///     ) -> object_store::Result<MultipartRoute> {
///         Ok(MultipartRoute::Mirror)
///     }
/// }
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let local_dir = tempfile::tempdir()?;
/// let remote: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
/// let mirror = ObjectStoreMirror::builder(
///     local_dir.path(),
///     remote,
///     Arc::new(ImmutableObjects),
/// )
/// .build()
/// .await?;
///
/// let path = Path::from("objects/first.bin");
/// mirror.put(&path, "hello".into()).await?;
/// assert!(mirror.handle().contains(&path));
/// let bytes = mirror.get(&path).await?.bytes().await?;
/// assert_eq!(bytes.as_ref(), b"hello");
/// # Ok(())
/// # }
/// ```
pub struct ObjectStoreMirror {
    pub(crate) inner: Arc<Inner>,
}

impl ObjectStoreMirror {
    pub fn builder(
        local_dir: impl Into<PathBuf>,
        object_store: Arc<dyn ObjectStore>,
        policy: Arc<dyn MirrorPolicy>,
    ) -> ObjectStoreMirrorBuilder {
        ObjectStoreMirrorBuilder {
            local_dir: local_dir.into(),
            remote: object_store,
            policy,
            vfs: Arc::new(StdVfs::new()),
            clock: Arc::new(DefaultSystemClock::new()),
            download_concurrency: DEFAULT_DOWNLOAD_CONCURRENCY,
            remote_scan_interval: Some(DEFAULT_REMOTE_SCAN_INTERVAL),
            max_retries: None,
        }
    }

    /// A handle to this mirror, like the one the policy gets.
    pub fn handle(&self) -> MirrorHandle {
        MirrorHandle {
            inner: Arc::clone(&self.inner),
        }
    }

    async fn get_observed(
        &self,
        location: &Path,
        mut options: GetOptions,
    ) -> object_store::Result<GetResult> {
        let inner = &self.inner;
        if options.head {
            return inner.remote.get_opts(location, options).await;
        }
        // `observe` needs the whole object. A `NotModified` or other error
        // returns here, before `observe`.
        let range = options.range.take();
        let result = inner.remote.get_opts(location, options).await?;
        let meta = result.meta.clone();
        let attributes = result.attributes.clone();
        let extensions = result.extensions.clone();
        let bytes = result.bytes().await?;
        let range = match range {
            Some(range) => range.as_range(bytes.len() as u64).map_err(range_error)?,
            None => 0..bytes.len() as u64,
        };
        inner
            .policy
            .observe(location, &bytes, &self.handle())
            .await?;
        Ok(GetResult {
            payload: bytes_payload(bytes.slice(range.start as usize..range.end as usize)),
            meta,
            range,
            attributes,
            extensions,
        })
    }

    async fn get_local(
        &self,
        location: &Path,
        options: GetOptions,
    ) -> object_store::Result<GetResult> {
        let inner = &self.inner;
        let Some(object) = inner.local(location) else {
            return Err(MirrorError::NotLocal {
                path: location.clone(),
            }
            .into());
        };
        inner.read_local(location, &object, &options).await
    }

    async fn get_refetched(
        &self,
        location: &Path,
        options: GetOptions,
    ) -> object_store::Result<GetResult> {
        let inner = &self.inner;
        let ticket = inner
            .ordering
            .register(location, OpKind::Download)
            .expect("downloads are always queued");
        ticket.ready().await;
        let object = inner.download(location).await?;
        // Keep the ticket until the read is done, so an eviction can't remove
        // the file underneath it.
        let result = inner.read_local(location, &object, &options).await;
        drop(ticket);
        result
    }

    async fn put_mirrored(
        &self,
        location: &Path,
        payload: PutPayload,
        opts: PutOptions,
    ) -> object_store::Result<PutResult> {
        let inner = &self.inner;
        let name = LocalName::new(location);
        inner.prefixes.lock().check_or_insert(&name)?;
        let ticket = inner
            .ordering
            .register(location, OpKind::Write)
            .expect("writes are always queued");
        ticket.ready().await;

        let size = payload.content_length() as u64;
        let attributes = opts.attributes.clone();
        let temp = inner.temp_file(&name);
        let (local, remote) = future::join(
            inner.write_payload(&temp.path, &payload),
            inner.remote.put_opts(location, payload.clone(), opts),
        )
        .await;
        let put = match remote {
            Ok(put) => put,
            Err(err) => {
                temp.remove().await;
                return Err(err);
            }
        };
        if let Err(err) = local {
            temp.remove().await;
            return Err(committed(location, local_error(err)));
        }

        let object = LocalObject {
            meta: ObjectMeta {
                location: location.clone(),
                last_modified: inner.clock.now(),
                size,
                e_tag: put.e_tag.clone(),
                version: put.version.clone(),
            },
            attributes,
        };
        inner
            .install(location, &name, temp, object)
            .await
            .map_err(|err| committed(location, err))?;
        drop(ticket);
        Ok(put)
    }
}

impl Drop for ObjectStoreMirror {
    fn drop(&mut self) {
        self.inner.shutdown.cancel();
    }
}

impl fmt::Debug for ObjectStoreMirror {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ObjectStoreMirror")
            .field("root", &self.inner.root)
            .field("remote", &self.inner.remote)
            .finish_non_exhaustive()
    }
}

impl fmt::Display for ObjectStoreMirror {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "ObjectStoreMirror({})", self.inner.remote)
    }
}

#[async_trait]
impl ObjectStore for ObjectStoreMirror {
    async fn put_opts(
        &self,
        location: &Path,
        payload: PutPayload,
        opts: PutOptions,
    ) -> object_store::Result<PutResult> {
        let inner = &self.inner;
        let route = inner
            .policy
            .put_route(location, &opts)
            .map_err(policy_error)?;
        match route {
            WriteRoute::Remote => inner.remote.put_opts(location, payload, opts).await,
            WriteRoute::Observe => {
                let bytes = Bytes::from(payload.clone());
                let put = inner.remote.put_opts(location, payload, opts).await?;
                inner
                    .policy
                    .observe(location, &bytes, &self.handle())
                    .await
                    .map_err(|err| committed(location, err))?;
                Ok(put)
            }
            WriteRoute::Mirror => self.put_mirrored(location, payload, opts).await,
        }
    }

    async fn put_multipart_opts(
        &self,
        location: &Path,
        opts: PutMultipartOptions,
    ) -> object_store::Result<Box<dyn MultipartUpload>> {
        let inner = &self.inner;
        let route = inner
            .policy
            .put_multipart_route(location, &opts)
            .map_err(policy_error)?;
        match route {
            MultipartRoute::Remote => inner.remote.put_multipart_opts(location, opts).await,
            MultipartRoute::Mirror => multipart::start(Arc::clone(inner), location, opts).await,
        }
    }

    async fn get_opts(
        &self,
        location: &Path,
        options: GetOptions,
    ) -> object_store::Result<GetResult> {
        let inner = &self.inner;
        let route = inner
            .policy
            .read_route(location, &options)
            .map_err(policy_error)?;
        match route {
            ReadRoute::Remote => inner.remote.get_opts(location, options).await,
            ReadRoute::Observe => self.get_observed(location, options).await,
            ReadRoute::Local => self.get_local(location, options).await,
            ReadRoute::Refetch => self.get_refetched(location, options).await,
        }
    }

    fn delete_stream(
        &self,
        locations: BoxStream<'static, object_store::Result<Path>>,
    ) -> BoxStream<'static, object_store::Result<Path>> {
        let inner = Arc::clone(&self.inner);
        locations
            .then(move |location| {
                let inner = Arc::clone(&inner);
                async move {
                    let location = location?;
                    inner.delete(&location).await?;
                    Ok(location)
                }
            })
            .boxed()
    }

    fn list(&self, prefix: Option<&Path>) -> BoxStream<'static, object_store::Result<ObjectMeta>> {
        self.inner.remote.list(prefix)
    }

    fn list_with_offset(
        &self,
        prefix: Option<&Path>,
        offset: &Path,
    ) -> BoxStream<'static, object_store::Result<ObjectMeta>> {
        self.inner.remote.list_with_offset(prefix, offset)
    }

    async fn list_with_delimiter(&self, prefix: Option<&Path>) -> object_store::Result<ListResult> {
        self.inner.remote.list_with_delimiter(prefix).await
    }

    async fn copy_opts(
        &self,
        _from: &Path,
        _to: &Path,
        _options: CopyOptions,
    ) -> object_store::Result<()> {
        Err(MirrorError::Unsupported { operation: "copy" }.into())
    }

    async fn rename_opts(
        &self,
        _from: &Path,
        _to: &Path,
        _options: RenameOptions,
    ) -> object_store::Result<()> {
        Err(MirrorError::Unsupported {
            operation: "rename",
        }
        .into())
    }
}

/// Configures and builds an [`ObjectStoreMirror`].
pub struct ObjectStoreMirrorBuilder {
    local_dir: PathBuf,
    remote: Arc<dyn ObjectStore>,
    policy: Arc<dyn MirrorPolicy>,
    vfs: Arc<dyn Vfs>,
    clock: Arc<dyn SystemClock>,
    download_concurrency: usize,
    remote_scan_interval: Option<Duration>,
    max_retries: Option<u32>,
}

impl fmt::Debug for ObjectStoreMirrorBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ObjectStoreMirrorBuilder")
            .field("local_dir", &self.local_dir)
            .field("remote", &self.remote)
            .field("vfs", &self.vfs)
            .field("download_concurrency", &self.download_concurrency)
            .field("remote_scan_interval", &self.remote_scan_interval)
            .field("max_retries", &self.max_retries)
            .finish_non_exhaustive()
    }
}

impl ObjectStoreMirrorBuilder {
    /// Sets the virtual filesystem used for local I/O. The default is
    /// [`StdVfs`].
    pub fn with_vfs(mut self, vfs: Arc<dyn Vfs>) -> Self {
        self.vfs = vfs;
        self
    }

    /// Sets the maximum number of concurrent downloads used by `fetch`,
    /// `prefetch`, and `Refetch`. The default is
    /// [`DEFAULT_DOWNLOAD_CONCURRENCY`].
    pub fn with_download_concurrency(mut self, concurrency: usize) -> Self {
        self.download_concurrency = concurrency;
        self
    }

    /// Sets the interval of the remote scan that removes local copies of
    /// objects deleted remotely. The default is
    /// [`DEFAULT_REMOTE_SCAN_INTERVAL`]. `None` disables the scan.
    ///
    /// The scan assumes strongly consistent reads, writes, deletes, and
    /// listings. Disable it if the remote store doesn't provide them.
    pub fn with_remote_scan_interval(mut self, interval: Option<Duration>) -> Self {
        self.remote_scan_interval = interval;
        self
    }

    /// Sets the clock used for retry backoff, the remote scan, and the
    /// last-modified time of mirrored writes. The default uses Tokio's clock.
    pub fn with_system_clock(mut self, clock: Arc<dyn SystemClock>) -> Self {
        self.clock = clock;
        self
    }

    /// Sets how many times the mirror retries a transient error from its own
    /// remote calls (downloads and remote-scan LISTs). The default, `None`,
    /// retries forever, like SlateDB's `object_store_max_retries`.
    pub fn with_max_retries(mut self, max_retries: Option<u32>) -> Self {
        self.max_retries = max_retries;
        self
    }

    /// Validates the configuration, acquires the cache-directory lock, cleans
    /// invalid local entries, and spawns the remote scan and
    /// `MirrorPolicy::run`.
    pub async fn build(self) -> object_store::Result<Arc<ObjectStoreMirror>> {
        let invalid = |message: &str| -> object_store::Error {
            MirrorError::InvalidConfig {
                message: message.to_string(),
            }
            .into()
        };
        if self.download_concurrency == 0 {
            return Err(invalid("download_concurrency must be greater than zero"));
        }
        if self.remote_scan_interval == Some(Duration::ZERO) {
            return Err(invalid("remote_scan_interval must be greater than zero"));
        }
        let runtime = tokio::runtime::Handle::try_current()
            .map_err(|_| invalid("the mirror must be built inside a Tokio runtime"))?;

        self.vfs
            .create_dir_all(&self.local_dir)
            .await
            .map_err(local_error)?;
        let lock = self
            .vfs
            .lock(&self.local_dir.join(LOCK_FILE))
            .await
            .map_err(local_error)?;
        let recovered = recover(self.vfs.as_ref(), &self.local_dir).await?;

        let inner = Arc::new(Inner::new(
            self.local_dir,
            self.remote,
            self.policy,
            self.vfs,
            Arc::clone(&self.clock),
            Retry::new(self.clock, self.max_retries),
            runtime,
            recovered.entries,
            recovered.prefixes,
            self.download_concurrency,
            lock,
        ));
        if let Some(interval) = self.remote_scan_interval {
            inner.spawn(scan::run(Arc::clone(&inner), interval));
        }
        let handle = MirrorHandle {
            inner: Arc::clone(&inner),
        };
        inner.spawn(Arc::clone(&inner.policy).run(handle));
        Ok(Arc::new(ObjectStoreMirror { inner }))
    }
}

/// Mirrors every object. For unit tests.
#[cfg(test)]
struct MirrorAll;

#[cfg(test)]
#[async_trait]
impl MirrorPolicy for MirrorAll {
    fn read_route(&self, _path: &Path, _options: &GetOptions) -> object_store::Result<ReadRoute> {
        Ok(ReadRoute::Local)
    }

    fn put_route(&self, _path: &Path, _options: &PutOptions) -> object_store::Result<WriteRoute> {
        Ok(WriteRoute::Mirror)
    }

    fn put_multipart_route(
        &self,
        _path: &Path,
        _options: &PutMultipartOptions,
    ) -> object_store::Result<MultipartRoute> {
        Ok(MultipartRoute::Mirror)
    }
}

#[cfg(test)]
impl ObjectStoreMirror {
    /// Builds a mirror of `remote` in `root` that mirrors every object and has
    /// no remote scan. For unit tests.
    pub(crate) async fn for_test(
        root: &std::path::Path,
        remote: Arc<dyn ObjectStore>,
    ) -> Arc<Self> {
        Self::builder(root, remote, Arc::new(MirrorAll))
            .with_remote_scan_interval(None)
            .build()
            .await
            .unwrap()
    }
}

#[cfg(test)]
mod tests {
    //! End-to-end tests of `ObjectStoreMirror` over an in-memory remote store.

    use std::collections::BTreeSet;
    use std::fmt;
    use std::path::{Path as StdPath, PathBuf};
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
    use std::sync::Arc;
    use std::time::Duration;

    use async_trait::async_trait;
    use bytes::Bytes;
    use futures::stream::BoxStream;
    use futures::TryStreamExt;
    use md5::{Digest, Md5};
    use object_store::memory::InMemory;
    use object_store::path::Path;
    use object_store::{
        CopyOptions, GetOptions, GetRange, GetResult, ListResult, MultipartUpload, ObjectMeta,
        ObjectStore, ObjectStoreExt, PutMode, PutMultipartOptions, PutOptions, PutPayload,
        PutResult,
    };
    use parking_lot::Mutex;
    use tokio::sync::Semaphore;

    use crate::{
        MirrorError, MirrorHandle, MirrorPolicy, MultipartRoute, ReadRoute, StdVfs, Vfs, VfsEntry,
        VfsLock, VfsWriter, WriteRoute,
    };

    use super::ObjectStoreMirror;

    /// An in-memory store that counts GETs and can fail or hold them.
    #[derive(Debug, Default)]
    struct TestStore {
        inner: InMemory,
        /// Full-object GETs (not HEADs).
        gets: AtomicUsize,
        in_flight: AtomicUsize,
        max_in_flight: AtomicUsize,
        /// Fail this many upcoming GETs with a transient error.
        fail_gets: AtomicUsize,
        /// When set, each GET takes a permit first.
        gate: Mutex<Option<Arc<Semaphore>>>,
        /// PUTs started.
        puts: AtomicUsize,
        /// When set, each PUT takes a permit first.
        put_gate: Mutex<Option<Arc<Semaphore>>>,
    }

    impl fmt::Display for TestStore {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "TestStore")
        }
    }

    #[async_trait]
    impl ObjectStore for TestStore {
        async fn put_opts(
            &self,
            location: &Path,
            payload: PutPayload,
            opts: PutOptions,
        ) -> object_store::Result<PutResult> {
            self.puts.fetch_add(1, Ordering::SeqCst);
            let gate = self.put_gate.lock().clone();
            if let Some(gate) = gate {
                gate.acquire().await.unwrap().forget();
            }
            self.inner.put_opts(location, payload, opts).await
        }

        async fn put_multipart_opts(
            &self,
            location: &Path,
            opts: PutMultipartOptions,
        ) -> object_store::Result<Box<dyn MultipartUpload>> {
            self.inner.put_multipart_opts(location, opts).await
        }

        async fn get_opts(
            &self,
            location: &Path,
            options: GetOptions,
        ) -> object_store::Result<GetResult> {
            if options.head {
                return self.inner.get_opts(location, options).await;
            }
            self.gets.fetch_add(1, Ordering::SeqCst);
            let in_flight = self.in_flight.fetch_add(1, Ordering::SeqCst) + 1;
            self.max_in_flight.fetch_max(in_flight, Ordering::SeqCst);
            let gate = self.gate.lock().clone();
            if let Some(gate) = gate {
                gate.acquire().await.unwrap().forget();
            }
            let result = if self
                .fail_gets
                .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |n| n.checked_sub(1))
                .is_ok()
            {
                Err(object_store::Error::Generic {
                    store: "TestStore",
                    source: "transient".into(),
                })
            } else {
                self.inner.get_opts(location, options).await
            };
            self.in_flight.fetch_sub(1, Ordering::SeqCst);
            result
        }

        fn delete_stream(
            &self,
            locations: BoxStream<'static, object_store::Result<Path>>,
        ) -> BoxStream<'static, object_store::Result<Path>> {
            self.inner.delete_stream(locations)
        }

        fn list(
            &self,
            prefix: Option<&Path>,
        ) -> BoxStream<'static, object_store::Result<ObjectMeta>> {
            self.inner.list(prefix)
        }

        async fn list_with_delimiter(
            &self,
            prefix: Option<&Path>,
        ) -> object_store::Result<ListResult> {
            self.inner.list_with_delimiter(prefix).await
        }

        async fn copy_opts(
            &self,
            from: &Path,
            to: &Path,
            options: CopyOptions,
        ) -> object_store::Result<()> {
            self.inner.copy_opts(from, to, options).await
        }
    }

    /// A [`StdVfs`] whose writes can be held.
    #[derive(Debug, Default)]
    struct GatedVfs {
        inner: StdVfs,
        /// When set, each write takes a permit first.
        write_gate: Arc<Mutex<Option<Arc<Semaphore>>>>,
        /// Writes that have waited on the gate.
        held_writes: Arc<AtomicUsize>,
    }

    #[async_trait]
    impl Vfs for GatedVfs {
        async fn create_dir_all(&self, path: &StdPath) -> std::io::Result<()> {
            self.inner.create_dir_all(path).await
        }

        async fn lock(&self, path: &StdPath) -> std::io::Result<Box<dyn VfsLock>> {
            self.inner.lock(path).await
        }

        async fn list(&self, dir: &StdPath) -> std::io::Result<Vec<VfsEntry>> {
            self.inner.list(dir).await
        }

        async fn read_range(
            &self,
            path: &StdPath,
            range: std::ops::Range<u64>,
        ) -> std::io::Result<Bytes> {
            self.inner.read_range(path, range).await
        }

        async fn read(&self, path: &StdPath) -> std::io::Result<Bytes> {
            self.inner.read(path).await
        }

        async fn create(&self, path: &StdPath) -> std::io::Result<Box<dyn VfsWriter>> {
            Ok(Box::new(GatedWriter {
                inner: self.inner.create(path).await?,
                gate: Arc::clone(&self.write_gate),
                held: Arc::clone(&self.held_writes),
            }))
        }

        async fn rename(&self, from: &StdPath, to: &StdPath) -> std::io::Result<()> {
            self.inner.rename(from, to).await
        }

        async fn remove(&self, path: &StdPath) -> std::io::Result<()> {
            self.inner.remove(path).await
        }
    }

    #[derive(Debug)]
    struct GatedWriter {
        inner: Box<dyn VfsWriter>,
        gate: Arc<Mutex<Option<Arc<Semaphore>>>>,
        held: Arc<AtomicUsize>,
    }

    #[async_trait]
    impl VfsWriter for GatedWriter {
        async fn write(&mut self, bytes: Bytes) -> std::io::Result<()> {
            let gate = self.gate.lock().clone();
            if let Some(gate) = gate {
                self.held.fetch_add(1, Ordering::SeqCst);
                gate.acquire().await.unwrap().forget();
            }
            self.inner.write(bytes).await
        }

        async fn finish(&mut self) -> std::io::Result<()> {
            self.inner.finish().await
        }
    }

    /// Routes `*.manifest` through `Observe` and `*.sst` to the mirror. A
    /// manifest's body is a newline-separated list of paths to fetch.
    struct TestPolicy {
        sst_read: Mutex<ReadRoute>,
        fail_observe: AtomicBool,
        observed: Mutex<Vec<(Path, Bytes)>>,
        run_started: AtomicBool,
        run_stopped: Arc<AtomicBool>,
    }

    impl TestPolicy {
        fn new() -> Arc<Self> {
            Arc::new(Self {
                sst_read: Mutex::new(ReadRoute::Local),
                fail_observe: AtomicBool::new(false),
                observed: Mutex::new(Vec::new()),
                run_started: AtomicBool::new(false),
                run_stopped: Arc::new(AtomicBool::new(false)),
            })
        }

        fn observed(&self) -> Vec<(Path, Bytes)> {
            self.observed.lock().clone()
        }
    }

    struct SetOnDrop(Arc<AtomicBool>);

    impl Drop for SetOnDrop {
        fn drop(&mut self) {
            self.0.store(true, Ordering::SeqCst);
        }
    }

    #[async_trait]
    impl MirrorPolicy for TestPolicy {
        fn read_route(
            &self,
            path: &Path,
            _options: &GetOptions,
        ) -> object_store::Result<ReadRoute> {
            Ok(match path.extension() {
                Some("manifest") => ReadRoute::Observe,
                Some("sst") => *self.sst_read.lock(),
                Some("bad") => {
                    return Err(object_store::Error::Generic {
                        store: "TestPolicy",
                        source: "bad route".into(),
                    })
                }
                _ => ReadRoute::Remote,
            })
        }

        fn put_route(
            &self,
            path: &Path,
            _options: &PutOptions,
        ) -> object_store::Result<WriteRoute> {
            Ok(match path.extension() {
                Some("manifest") => WriteRoute::Observe,
                Some("sst") | Some("123") | Some("meta") => WriteRoute::Mirror,
                _ => WriteRoute::Remote,
            })
        }

        fn put_multipart_route(
            &self,
            path: &Path,
            _options: &PutMultipartOptions,
        ) -> object_store::Result<MultipartRoute> {
            Ok(match path.extension() {
                Some("sst") => MultipartRoute::Mirror,
                _ => MultipartRoute::Remote,
            })
        }

        async fn observe(
            &self,
            path: &Path,
            bytes: &Bytes,
            mirror: &MirrorHandle,
        ) -> object_store::Result<()> {
            self.observed.lock().push((path.clone(), bytes.clone()));
            if self.fail_observe.load(Ordering::SeqCst) {
                return Err(object_store::Error::Generic {
                    store: "TestPolicy",
                    source: "observe failed".into(),
                });
            }
            let paths: Vec<Path> = std::str::from_utf8(bytes)
                .unwrap_or_default()
                .lines()
                .filter(|line| !line.is_empty())
                .map(Path::from)
                .collect();
            mirror.fetch(paths).await
        }

        async fn run(self: Arc<Self>, _mirror: MirrorHandle) {
            let _stopped = SetOnDrop(Arc::clone(&self.run_stopped));
            self.run_started.store(true, Ordering::SeqCst);
            std::future::pending::<()>().await;
        }
    }

    struct Fixture {
        _dir: tempfile::TempDir,
        root: PathBuf,
        store: Arc<TestStore>,
        policy: Arc<TestPolicy>,
        mirror: Arc<ObjectStoreMirror>,
    }

    impl Fixture {
        async fn new() -> Self {
            Self::with_concurrency(8).await
        }

        async fn with_concurrency(concurrency: usize) -> Self {
            let dir = tempfile::tempdir().unwrap();
            let root = dir.path().join("mirror");
            let store = Arc::new(TestStore::default());
            let policy = TestPolicy::new();
            let mirror = build(&root, &store, &policy, concurrency).await.unwrap();
            Self {
                _dir: dir,
                root,
                store,
                policy,
                mirror,
            }
        }

        fn handle(&self) -> MirrorHandle {
            self.mirror.handle()
        }

        fn files(&self) -> BTreeSet<String> {
            files(&self.root)
        }

        fn local_bytes(&self, path: &str) -> Vec<u8> {
            std::fs::read(self.root.join(data_file(path))).unwrap()
        }

        /// The files a set of complete local copies should leave behind.
        fn expected_files(&self, paths: &[&str]) -> BTreeSet<String> {
            let mut expected = BTreeSet::from(["LOCK".to_string()]);
            for path in paths {
                let data = data_file(path);
                expected.insert(format!("{data}.meta"));
                expected.insert(data);
            }
            expected
        }

        async fn put_remote(&self, path: &str, bytes: &'static [u8]) {
            self.store
                .put(&Path::from(path), PutPayload::from_static(bytes))
                .await
                .unwrap();
        }
    }

    async fn build(
        root: &StdPath,
        store: &Arc<TestStore>,
        policy: &Arc<TestPolicy>,
        concurrency: usize,
    ) -> object_store::Result<Arc<ObjectStoreMirror>> {
        ObjectStoreMirror::builder(
            root,
            Arc::clone(store) as Arc<dyn ObjectStore>,
            Arc::clone(policy) as Arc<dyn MirrorPolicy>,
        )
        .with_download_concurrency(concurrency)
        .with_remote_scan_interval(None)
        .build()
        .await
    }

    /// The local data file name for `path`: the hex MD5 of its parent, a `.`, and
    /// its file name. See `src/layout.rs`.
    fn data_file(path: &str) -> String {
        let (parent, name) = path.rsplit_once('/').unwrap_or(("", path));
        let prefix = hex::encode(Md5::digest(parent.as_bytes()));
        format!("{prefix}.{name}.obj")
    }

    fn files(root: &StdPath) -> BTreeSet<String> {
        std::fs::read_dir(root)
            .unwrap()
            .map(|entry| entry.unwrap().file_name().into_string().unwrap())
            .collect()
    }

    fn mirror_error(err: &object_store::Error) -> &MirrorError {
        MirrorError::find(err).unwrap_or_else(|| panic!("expected a MirrorError, got {err:?}"))
    }

    /// Polls `condition` until it holds, yielding to background tasks.
    async fn eventually(mut condition: impl FnMut() -> bool) {
        for _ in 0..1000 {
            if condition() {
                return;
            }
            tokio::time::sleep(Duration::from_millis(5)).await;
        }
        panic!("condition never held");
    }

    const SST: &str = "db/compacted/01A.sst";
    const SST2: &str = "db/compacted/01B.sst";
    const MANIFEST: &str = "db/manifest/00001.manifest";

    #[tokio::test]
    async fn should_reject_invalid_options() {
        let dir = tempfile::tempdir().unwrap();
        let store = Arc::new(TestStore::default());
        let policy = TestPolicy::new();

        let err = build(dir.path(), &store, &policy, 0).await.unwrap_err();
        assert!(matches!(
            mirror_error(&err),
            MirrorError::InvalidConfig { .. }
        ));

        let err = ObjectStoreMirror::builder(dir.path(), store, policy)
            .with_remote_scan_interval(Some(Duration::ZERO))
            .build()
            .await
            .unwrap_err();
        assert!(matches!(
            mirror_error(&err),
            MirrorError::InvalidConfig { .. }
        ));
    }

    #[tokio::test]
    async fn should_lock_directory_and_stop_background_tasks_on_drop() {
        let fixture = Fixture::new().await;
        eventually(|| fixture.policy.run_started.load(Ordering::SeqCst)).await;

        let err = build(&fixture.root, &fixture.store, &fixture.policy, 8)
            .await
            .unwrap_err();
        assert!(matches!(mirror_error(&err), MirrorError::Local { .. }));

        let Fixture {
            _dir,
            root,
            store,
            policy,
            mirror,
        } = fixture;
        drop(mirror);
        eventually(|| policy.run_stopped.load(Ordering::SeqCst)).await;

        // The lock is released once the background tasks let go of the state.
        let mut reopened = None;
        for _ in 0..100 {
            match build(&root, &store, &policy, 8).await {
                Ok(mirror) => {
                    reopened = Some(mirror);
                    break;
                }
                Err(_) => tokio::time::sleep(Duration::from_millis(5)).await,
            }
        }
        assert!(reopened.is_some());
    }

    #[tokio::test]
    async fn should_pass_remote_routes_through() {
        let fixture = Fixture::new().await;
        let path = Path::from("db/wal/00001.wal");
        fixture
            .mirror
            .put(&path, PutPayload::from_static(b"wal"))
            .await
            .unwrap();

        assert_eq!(
            fixture
                .mirror
                .get(&path)
                .await
                .unwrap()
                .bytes()
                .await
                .unwrap(),
            "wal"
        );
        assert!(!fixture.handle().contains(&path));
        assert_eq!(fixture.files(), fixture.expected_files(&[]));

        let listed: Vec<_> = fixture
            .mirror
            .list(Some(&Path::from("db")))
            .try_collect()
            .await
            .unwrap();
        assert_eq!(listed.len(), 1);
    }

    #[tokio::test]
    async fn should_wrap_route_errors_in_policy_error() {
        let fixture = Fixture::new().await;
        let err = fixture
            .mirror
            .get(&Path::from("a/b.bad"))
            .await
            .unwrap_err();
        assert!(matches!(mirror_error(&err), MirrorError::Policy { .. }));
    }

    #[tokio::test]
    async fn should_mirror_puts_and_serve_local_reads() {
        let fixture = Fixture::new().await;
        let path = Path::from(SST);
        let put = fixture
            .mirror
            .put(&path, PutPayload::from_static(b"0123456789"))
            .await
            .unwrap();

        assert!(fixture.handle().contains(&path));
        assert_eq!(fixture.local_bytes(SST), b"0123456789");
        assert_eq!(fixture.files(), fixture.expected_files(&[SST]));

        // Served locally, without touching the remote store.
        let result = fixture
            .mirror
            .get_opts(
                &path,
                GetOptions {
                    range: Some(GetRange::Bounded(2..5)),
                    ..Default::default()
                },
            )
            .await
            .unwrap();
        assert_eq!(result.range, 2..5);
        assert_eq!(result.bytes().await.unwrap(), "234");
        assert!(fixture.mirror.get_range(&path, 20..30).await.is_err());
        let head = fixture.mirror.head(&path).await.unwrap();
        assert_eq!(head.size, 10);
        assert_eq!(head.e_tag, put.e_tag);
        assert_eq!(fixture.store.gets.load(Ordering::SeqCst), 0);

        // Preconditions are checked against the local metadata.
        let err = fixture
            .mirror
            .get_opts(
                &path,
                GetOptions {
                    if_none_match: put.e_tag.clone(),
                    ..Default::default()
                },
            )
            .await
            .unwrap_err();
        assert!(matches!(err, object_store::Error::NotModified { .. }));
    }

    #[tokio::test]
    async fn should_return_not_local_on_local_miss() {
        let fixture = Fixture::new().await;
        fixture.put_remote(SST, b"remote only").await;

        let err = fixture.mirror.get(&Path::from(SST)).await.unwrap_err();
        assert!(matches!(mirror_error(&err), MirrorError::NotLocal { .. }));
        let err = fixture.mirror.head(&Path::from(SST)).await.unwrap_err();
        assert!(matches!(mirror_error(&err), MirrorError::NotLocal { .. }));
        assert_eq!(fixture.store.gets.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn should_not_write_remotely_when_put_fails_remotely() {
        let fixture = Fixture::new().await;
        fixture.put_remote(SST, b"first").await;

        let err = fixture
            .mirror
            .put_opts(
                &Path::from(SST),
                PutPayload::from_static(b"second"),
                PutOptions {
                    mode: PutMode::Create,
                    ..Default::default()
                },
            )
            .await
            .unwrap_err();
        assert!(matches!(err, object_store::Error::AlreadyExists { .. }));
        assert!(!fixture.handle().contains(&Path::from(SST)));
        assert_eq!(fixture.files(), fixture.expected_files(&[]));
    }

    #[tokio::test]
    async fn should_mirror_names_that_look_like_layout_suffixes() {
        let fixture = Fixture::new().await;
        eventually(|| fixture.policy.run_started.load(Ordering::SeqCst)).await;
        // `01A.sst.meta` and `01A.sst.123` look like the metadata and a temp
        // file of `01A.sst`.
        let objects: [(&str, &'static [u8]); 3] = [
            (SST, b"sst"),
            ("db/compacted/01A.sst.meta", b"meta"),
            ("db/compacted/01A.sst.123", b"temp"),
        ];
        for (path, bytes) in objects {
            fixture
                .mirror
                .put(&Path::from(path), PutPayload::from_static(bytes))
                .await
                .unwrap();
        }
        let paths = objects.map(|(path, _)| path);
        assert_eq!(fixture.files(), fixture.expected_files(&paths));
        for (path, bytes) in objects {
            assert_eq!(fixture.local_bytes(path), bytes, "{path}");
        }

        let Fixture {
            _dir,
            root,
            store,
            policy,
            mirror,
        } = fixture;
        drop(mirror);
        eventually(|| policy.run_stopped.load(Ordering::SeqCst)).await;
        let mut reopened = None;
        for _ in 0..100 {
            if let Ok(mirror) = build(&root, &store, &policy, 8).await {
                reopened = Some(mirror);
                break;
            }
            tokio::time::sleep(Duration::from_millis(5)).await;
        }
        let mirror = reopened.expect("mirror reopened");

        for path in paths {
            assert!(mirror.handle().contains(&Path::from(path)), "{path}");
        }
        assert_eq!(
            mirror
                .get(&Path::from(SST))
                .await
                .unwrap()
                .bytes()
                .await
                .unwrap(),
            "sst"
        );
        assert_eq!(store.gets.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn should_observe_whole_objects_on_ranged_reads() {
        let fixture = Fixture::new().await;
        fixture.put_remote(SST, b"sst").await;
        fixture
            .put_remote(MANIFEST, b"db/compacted/01A.sst\n")
            .await;
        let manifest = Path::from(MANIFEST);

        let result = fixture
            .mirror
            .get_opts(
                &manifest,
                GetOptions {
                    range: Some(GetRange::Bounded(0..2)),
                    ..Default::default()
                },
            )
            .await
            .unwrap();
        assert_eq!(result.range, 0..2);
        assert_eq!(result.bytes().await.unwrap(), "db");
        assert_eq!(
            fixture.policy.observed(),
            vec![(manifest.clone(), Bytes::from("db/compacted/01A.sst\n"))]
        );

        // `observe` fetched the SST it listed.
        assert!(fixture.handle().contains(&Path::from(SST)));
        assert_eq!(fixture.local_bytes(SST), b"sst");

        // HEAD and bodiless conditional GETs skip `observe`.
        let meta = fixture.mirror.head(&manifest).await.unwrap();
        let err = fixture
            .mirror
            .get_opts(
                &manifest,
                GetOptions {
                    if_none_match: meta.e_tag,
                    ..Default::default()
                },
            )
            .await
            .unwrap_err();
        assert!(matches!(err, object_store::Error::NotModified { .. }));
        assert_eq!(fixture.policy.observed().len(), 1);
    }

    #[tokio::test]
    async fn should_report_observe_failures() {
        let fixture = Fixture::new().await;
        fixture.policy.fail_observe.store(true, Ordering::SeqCst);
        let manifest = Path::from(MANIFEST);

        // Writes are durable, so the caller has to know.
        let err = fixture
            .mirror
            .put(&manifest, PutPayload::from_static(b""))
            .await
            .unwrap_err();
        assert!(matches!(
            mirror_error(&err),
            MirrorError::WriteCommitted { .. }
        ));
        assert!(fixture.store.head(&manifest).await.is_ok());

        // Reads return the policy's error unchanged.
        let err = fixture.mirror.get(&manifest).await.unwrap_err();
        assert!(MirrorError::find(&err).is_none());
        assert!(err.to_string().contains("observe failed"));
    }

    #[tokio::test]
    async fn should_refetch_and_replace_local_copy() {
        let fixture = Fixture::new().await;
        let path = Path::from(SST);
        fixture
            .mirror
            .put(&path, PutPayload::from_static(b"good bytes"))
            .await
            .unwrap();

        // Corrupt the local copy without changing its size.
        std::fs::write(fixture.root.join(data_file(SST)), b"bad bytes!").unwrap();

        *fixture.policy.sst_read.lock() = ReadRoute::Refetch;
        assert_eq!(fixture.mirror.get_range(&path, 0..4).await.unwrap(), "good");
        assert_eq!(fixture.local_bytes(SST), b"good bytes");
        assert_eq!(fixture.mirror.head(&path).await.unwrap().size, 10);
        assert_eq!(fixture.store.gets.load(Ordering::SeqCst), 2);
        assert_eq!(fixture.files(), fixture.expected_files(&[SST]));
    }

    #[tokio::test]
    async fn should_tee_multipart_uploads() {
        let fixture = Fixture::new().await;
        let path = Path::from(SST);
        let mut upload = fixture.mirror.put_multipart(&path).await.unwrap();

        // Complete the parts out of order; the local file still gets them in
        // order.
        let first = upload.put_part(PutPayload::from_static(b"aaaaa"));
        let second = upload.put_part(PutPayload::from_static(b"bbbbb"));
        let third = upload.put_part(PutPayload::from_static(b"ccccc"));
        third.await.unwrap();
        second.await.unwrap();
        first.await.unwrap();
        assert!(!fixture.handle().contains(&path));
        upload.complete().await.unwrap();

        assert!(fixture.handle().contains(&path));
        assert_eq!(fixture.local_bytes(SST), b"aaaaabbbbbccccc");
        assert_eq!(
            fixture
                .store
                .get(&path)
                .await
                .unwrap()
                .bytes()
                .await
                .unwrap(),
            "aaaaabbbbbccccc"
        );
        assert_eq!(fixture.mirror.head(&path).await.unwrap().size, 15);
        assert_eq!(fixture.files(), fixture.expected_files(&[SST]));
    }

    #[tokio::test]
    async fn should_remove_temp_file_when_multipart_upload_is_aborted_or_dropped() {
        let fixture = Fixture::new().await;
        let path = Path::from(SST);

        let mut upload = fixture.mirror.put_multipart(&path).await.unwrap();
        upload
            .put_part(PutPayload::from_static(b"part"))
            .await
            .unwrap();
        upload.abort().await.unwrap();
        assert_eq!(fixture.files(), fixture.expected_files(&[]));

        let mut upload = fixture.mirror.put_multipart(&path).await.unwrap();
        upload
            .put_part(PutPayload::from_static(b"part"))
            .await
            .unwrap();
        drop(upload);
        eventually(|| fixture.files() == fixture.expected_files(&[])).await;

        // The dropped upload released the path.
        fixture
            .mirror
            .put(&path, PutPayload::from_static(b"x"))
            .await
            .unwrap();
        assert!(fixture.handle().contains(&path));
    }

    #[tokio::test]
    async fn should_abort_multipart_upload_with_a_cancelled_part_mid_write() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path().join("mirror");
        let store = Arc::new(TestStore::default());
        let policy = TestPolicy::new();
        let vfs = Arc::new(GatedVfs::default());
        let mirror = ObjectStoreMirror::builder(
            &root,
            Arc::clone(&store) as Arc<dyn ObjectStore>,
            Arc::clone(&policy) as Arc<dyn MirrorPolicy>,
        )
        .with_remote_scan_interval(None)
        .with_vfs(Arc::clone(&vfs) as Arc<dyn Vfs>)
        .build()
        .await
        .unwrap();
        let path = Path::from(SST);
        let mut upload = mirror.put_multipart(&path).await.unwrap();

        // Cancel a part while its local write is held, the way
        // `WriteMultipart` drops its part tasks before aborting.
        let gate = Arc::new(Semaphore::new(0));
        *vfs.write_gate.lock() = Some(Arc::clone(&gate));
        let part = tokio::spawn(upload.put_part(PutPayload::from_static(b"part")));
        eventually(|| vfs.held_writes.load(Ordering::SeqCst) == 1).await;
        part.abort();
        let _ = part.await;

        tokio::time::timeout(Duration::from_secs(5), upload.abort())
            .await
            .expect("abort hung")
            .unwrap();
        eventually(|| files(&root) == BTreeSet::from(["LOCK".to_string()])).await;

        // The aborted upload released the path.
        *vfs.write_gate.lock() = None;
        mirror
            .put(&path, PutPayload::from_static(b"x"))
            .await
            .unwrap();
        assert!(mirror.handle().contains(&path));
    }

    #[tokio::test]
    async fn should_delete_local_and_remote_copies() {
        let fixture = Fixture::new().await;
        let path = Path::from(SST);
        fixture
            .mirror
            .put(&path, PutPayload::from_static(b"x"))
            .await
            .unwrap();

        fixture.mirror.delete(&path).await.unwrap();
        assert!(!fixture.handle().contains(&path));
        assert!(fixture.store.head(&path).await.is_err());
        assert_eq!(fixture.files(), fixture.expected_files(&[]));
    }

    #[tokio::test]
    async fn should_not_delete_remotely_before_earlier_mirrored_put() {
        let fixture = Fixture::new().await;
        let path = Path::from(SST);
        let gate = Arc::new(Semaphore::new(0));
        *fixture.store.put_gate.lock() = Some(Arc::clone(&gate));

        let mirror = Arc::clone(&fixture.mirror);
        let put_path = path.clone();
        let put =
            tokio::spawn(async move { mirror.put(&put_path, PutPayload::from_static(b"x")).await });
        eventually(|| fixture.store.puts.load(Ordering::SeqCst) == 1).await;

        let mirror = Arc::clone(&fixture.mirror);
        let delete_path = path.clone();
        let delete = tokio::spawn(async move { mirror.delete(&delete_path).await });
        tokio::time::sleep(Duration::from_millis(20)).await;
        assert!(!delete.is_finished());

        gate.add_permits(1);
        put.await.unwrap().unwrap();
        delete.await.unwrap().unwrap();
        assert!(fixture.store.head(&path).await.is_err());
        assert!(!fixture.handle().contains(&path));
        assert_eq!(fixture.files(), fixture.expected_files(&[]));
    }

    #[tokio::test]
    async fn should_reject_copy_and_rename() {
        let fixture = Fixture::new().await;
        fixture.put_remote("a/b", b"x").await;
        let (from, to) = (Path::from("a/b"), Path::from("a/c"));

        let err = fixture.mirror.copy(&from, &to).await.unwrap_err();
        assert!(matches!(
            mirror_error(&err),
            MirrorError::Unsupported { .. }
        ));
        let err = fixture.mirror.rename(&from, &to).await.unwrap_err();
        assert!(matches!(
            mirror_error(&err),
            MirrorError::Unsupported { .. }
        ));
        assert!(fixture.store.head(&to).await.is_err());
    }

    #[tokio::test]
    async fn should_restore_local_copies_after_restart() {
        let fixture = Fixture::new().await;
        eventually(|| fixture.policy.run_started.load(Ordering::SeqCst)).await;
        let path = Path::from(SST);
        let put = fixture
            .mirror
            .put(&path, PutPayload::from_static(b"persisted"))
            .await
            .unwrap();

        let Fixture {
            _dir,
            root,
            store,
            policy,
            mirror,
        } = fixture;
        drop(mirror);
        eventually(|| policy.run_stopped.load(Ordering::SeqCst)).await;
        let mut reopened = None;
        for _ in 0..100 {
            if let Ok(mirror) = build(&root, &store, &policy, 8).await {
                reopened = Some(mirror);
                break;
            }
            tokio::time::sleep(Duration::from_millis(5)).await;
        }
        let mirror = reopened.expect("mirror reopened");

        assert!(mirror.handle().contains(&path));
        assert_eq!(mirror.head(&path).await.unwrap().e_tag, put.e_tag);
        assert_eq!(
            mirror.get(&path).await.unwrap().bytes().await.unwrap(),
            "persisted"
        );
        assert_eq!(store.gets.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn should_refetch_corrupt_local_copy_after_restart() {
        let fixture = Fixture::new().await;
        // The policy task must create its shutdown guard before the mirror is dropped.
        eventually(|| fixture.policy.run_started.load(Ordering::SeqCst)).await;
        let path = Path::from(SST);
        fixture
            .mirror
            .put(&path, PutPayload::from_static(b"good bytes"))
            .await
            .unwrap();

        let Fixture {
            _dir,
            root,
            store,
            policy,
            mirror,
        } = fixture;
        drop(mirror);
        eventually(|| policy.run_stopped.load(Ordering::SeqCst)).await;

        // Model corruption that startup's file-size check cannot detect.
        std::fs::write(root.join(data_file(SST)), b"bad bytes!").unwrap();
        let mut reopened = None;
        for _ in 0..100 {
            if let Ok(mirror) = build(&root, &store, &policy, 8).await {
                reopened = Some(mirror);
                break;
            }
            tokio::time::sleep(Duration::from_millis(5)).await;
        }
        let mirror = reopened.expect("mirror reopened");

        // Local reads succeed with corrupt bytes, even when repeated.
        assert!(mirror.handle().contains(&path));
        for _ in 0..2 {
            assert_eq!(
                mirror.get(&path).await.unwrap().bytes().await.unwrap(),
                "bad bytes!"
            );
        }
        assert_eq!(store.gets.load(Ordering::SeqCst), 0);

        // The caller signals its policy to refetch, then checks the result.
        *policy.sst_read.lock() = ReadRoute::Refetch;
        assert_eq!(
            mirror.get(&path).await.unwrap().bytes().await.unwrap(),
            "good bytes"
        );
        *policy.sst_read.lock() = ReadRoute::Local;
        assert_eq!(
            mirror.get(&path).await.unwrap().bytes().await.unwrap(),
            "good bytes"
        );
        assert_eq!(store.gets.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn should_dedupe_concurrent_fetches() {
        let fixture = Fixture::new().await;
        fixture.put_remote(SST, b"x").await;
        let gate = Arc::new(Semaphore::new(0));
        *fixture.store.gate.lock() = Some(Arc::clone(&gate));

        let first = tokio::spawn(fixture.handle().fetch([Path::from(SST)]));
        let second = tokio::spawn(fixture.handle().fetch([Path::from(SST)]));
        eventually(|| fixture.store.gets.load(Ordering::SeqCst) == 1).await;
        gate.add_permits(10);
        first.await.unwrap().unwrap();
        second.await.unwrap().unwrap();

        assert_eq!(fixture.store.gets.load(Ordering::SeqCst), 1);
        assert!(fixture.handle().contains(&Path::from(SST)));

        // Already local, so no download.
        fixture.handle().fetch([Path::from(SST)]).await.unwrap();
        assert_eq!(fixture.store.gets.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn should_limit_download_concurrency() {
        let fixture = Fixture::with_concurrency(1).await;
        fixture.put_remote(SST, b"a").await;
        fixture.put_remote(SST2, b"b").await;
        let gate = Arc::new(Semaphore::new(0));
        *fixture.store.gate.lock() = Some(Arc::clone(&gate));

        let fetch = tokio::spawn(fixture.handle().fetch([Path::from(SST), Path::from(SST2)]));
        eventually(|| fixture.store.gets.load(Ordering::SeqCst) == 1).await;
        tokio::time::sleep(Duration::from_millis(20)).await;
        assert_eq!(fixture.store.gets.load(Ordering::SeqCst), 1);

        gate.add_permits(10);
        fetch.await.unwrap().unwrap();
        assert_eq!(fixture.store.max_in_flight.load(Ordering::SeqCst), 1);
        assert!(fixture.handle().contains(&Path::from(SST2)));
    }

    #[tokio::test]
    async fn should_retry_transient_download_errors() {
        let fixture = Fixture::new().await;
        fixture.put_remote(SST, b"x").await;
        fixture.store.fail_gets.store(2, Ordering::SeqCst);

        fixture.handle().fetch([Path::from(SST)]).await.unwrap();
        assert_eq!(fixture.store.gets.load(Ordering::SeqCst), 3);
        assert_eq!(fixture.files(), fixture.expected_files(&[SST]));
    }

    #[tokio::test]
    async fn should_fail_fetch_of_missing_object() {
        let fixture = Fixture::new().await;
        let err = fixture.handle().fetch([Path::from(SST)]).await.unwrap_err();
        assert!(matches!(err, object_store::Error::NotFound { .. }));
        assert_eq!(fixture.files(), fixture.expected_files(&[]));
    }

    #[tokio::test]
    async fn should_evict_local_copies() {
        let fixture = Fixture::new().await;
        let path = Path::from(SST);
        fixture
            .mirror
            .put(&path, PutPayload::from_static(b"x"))
            .await
            .unwrap();

        fixture.handle().evict([path.clone()]);
        eventually(|| !fixture.handle().contains(&path)).await;
        eventually(|| fixture.files() == fixture.expected_files(&[])).await;
        // The remote copy is untouched.
        assert!(fixture.store.head(&path).await.is_ok());
    }

    #[tokio::test]
    async fn should_run_remote_scan_in_background() {
        let dir = tempfile::tempdir().unwrap();
        let store = Arc::new(TestStore::default());
        let policy = TestPolicy::new();
        let mirror = ObjectStoreMirror::builder(
            dir.path(),
            Arc::clone(&store) as Arc<dyn ObjectStore>,
            policy,
        )
        .with_remote_scan_interval(Some(Duration::from_millis(10)))
        .build()
        .await
        .unwrap();
        let path = Path::from(SST);
        mirror
            .put(&path, PutPayload::from_static(b"x"))
            .await
            .unwrap();

        store.delete(&path).await.unwrap();
        eventually(|| !mirror.handle().contains(&path)).await;
    }
}