google-cloud-spanner 0.34.5-preview

Google Cloud Client Libraries for Rust - Spanner
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
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! In-memory interval cache (`KeyRangeCache`) for Spanner location-aware routing.
//!
//! Stores table key range split boundaries and maps them to tablet replica groups.
//! Provides interval lookups for point keys and key ranges under `CoveringSplit`
//! and `PickRandom` routing modes.

#![allow(dead_code)]

use crate::model::{CacheUpdate, Group, Range, Tablet};
use bytes::Bytes;
#[cfg(test)]
use crc32c::crc32c;
use rand::random_range;
use std::collections::hash_map::Entry;
use std::collections::{BTreeMap, HashMap};
use std::mem::take;
use std::ops::Bound;
#[cfg(test)]
use std::sync::atomic::AtomicBool;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, RwLock};

/// Determines how to handle ranges that span multiple splits.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RangeMode {
    /// Consider it a cache miss if the whole range is not in a single split.
    CoveringSplit,
    /// If the range spans multiple splits, pick a random split when possible.
    PickRandom,
}

/// Maximum distance for a replica to be considered local (within the same region or metro).
pub(crate) const MAX_LOCAL_REPLICA_DISTANCE: u32 = 5;

/// An immutable, in-memory cache representation of a Spanner Paxos group.
///
/// # Thread-Safety & Immutability
/// This struct is completely immutable once constructed via [`CachedGroup::from_proto`].
/// Precomputed fields (`local_leader_index` and `eligible_replica_indices`) are evaluated once
/// during ingestion and remain fixed for the lifetime of the struct.
///
/// The cache manages group updates via a Read-Copy-Update (RCU) pattern wrapped in `Arc<CachedGroup>`:
/// background updates atomically replace the `Arc` pointer in the cache map under a write lock,
/// while concurrent in-flight requests safely read their immutable snapshot without locks or data races.
#[derive(Debug, Clone)]
pub(crate) struct CachedGroup {
    pub group_uid: u64,
    pub generation: Bytes,
    pub tablets: Vec<Tablet>,
    /// 0-based index into `tablets` representing the designated Paxos leader, or `None` if no leader is designated.
    ///
    /// In Spanner metadata protos, a negative `leader_index` (typically `-1`) denotes that no leader is designated
    /// or that leader routing is unknown/unspecified.
    pub leader_index: Option<usize>,
    /// Precomputed index into `tablets` of the local leader (if designated, routable, and distance <= 5).
    pub local_leader_index: Option<usize>,
    /// Precomputed indices into `tablets` for candidate replicas in the lowest available distance tier.
    pub eligible_replica_indices: Vec<usize>,
}

impl CachedGroup {
    pub(crate) fn from_proto(proto: Group) -> Self {
        let leader_index = Self::parse_leader_index(proto.leader_index, proto.tablets.len());
        let local_leader_index = Self::compute_local_leader_index(&proto.tablets, leader_index);
        let eligible_replica_indices = Self::compute_eligible_replica_indices(&proto.tablets);

        Self {
            group_uid: proto.group_uid,
            generation: proto.generation,
            tablets: proto.tablets,
            leader_index,
            local_leader_index,
            eligible_replica_indices,
        }
    }

    /// Returns `true` if this group has a designated leader index within valid bounds.
    pub(crate) fn has_leader(&self) -> bool {
        self.leader_index.is_some()
    }

    /// Returns a reference to the leader tablet if designated, non-skipped, and with a non-empty server address.
    pub(crate) fn leader(&self) -> Option<&Tablet> {
        let candidate = &self.tablets[self.leader_index?];
        if !Self::is_routable(candidate) {
            return None;
        }
        Some(candidate)
    }

    /// Returns a reference to the leader tablet if designated, routable, and local
    /// (`distance <= MAX_LOCAL_REPLICA_DISTANCE`).
    pub(crate) fn local_leader(&self) -> Option<&Tablet> {
        let index = self.local_leader_index?;
        Some(&self.tablets[index])
    }

    /// Returns candidate replica references in the lowest locality tier matching the minimum distance.
    ///
    /// If `prefer_leader` is `true` and a valid local leader is present, returns a single-element
    /// vector containing a reference to that leader.
    ///
    /// Otherwise, returns references to the precomputed candidate replicas in the lowest available distance tier.
    pub(crate) fn eligible_tablets(&self, prefer_leader: bool) -> Vec<&Tablet> {
        if prefer_leader && let Some(leader) = self.local_leader() {
            return vec![leader];
        }

        self.eligible_replica_indices
            .iter()
            .map(|&index| &self.tablets[index])
            .collect()
    }

    /// Parses the raw protobuf leader index, returning `None` if negative or out of bounds.
    fn parse_leader_index(leader_index: i32, tablets_count: usize) -> Option<usize> {
        if leader_index < 0 {
            return None;
        }
        let index = leader_index as usize;
        if index >= tablets_count {
            return None;
        }
        Some(index)
    }

    /// Precomputes the local leader index if the designated leader is routable and local.
    fn compute_local_leader_index(
        tablets: &[Tablet],
        leader_index: Option<usize>,
    ) -> Option<usize> {
        let index = leader_index?;
        let candidate = &tablets[index];
        if !Self::is_routable(candidate) || !Self::is_local_distance(candidate.distance) {
            return None;
        }
        Some(index)
    }

    /// Precomputes indices into `tablets` for candidate replicas in the lowest available distance tier.
    fn compute_eligible_replica_indices(tablets: &[Tablet]) -> Vec<usize> {
        let mut candidates = Vec::new();
        let mut minimum_distance = u32::MAX;
        let mut local_tier_active = false;

        for (index, tablet) in tablets.iter().enumerate() {
            if !Self::is_routable(tablet) {
                continue;
            }

            let is_local = Self::is_local_distance(tablet.distance);

            // When encountering a local replica for the first time, switch to local tier and clear remote candidates.
            if is_local && !local_tier_active {
                local_tier_active = true;
                minimum_distance = u32::MAX;
                candidates.clear();
            }

            // If a local replica was already found, ignore all remote replicas.
            if !is_local && local_tier_active {
                continue;
            }

            Self::collect_minimum_distance_index(
                &mut candidates,
                &mut minimum_distance,
                index,
                tablet.distance,
            );
        }

        candidates
    }

    /// Returns `true` if the tablet is non-skipped and has a non-empty server address.
    fn is_routable(tablet: &Tablet) -> bool {
        !tablet.skip && !tablet.server_address.is_empty()
    }

    /// Returns `true` if the distance metric is within the local region/metro threshold.
    fn is_local_distance(distance: u32) -> bool {
        distance <= MAX_LOCAL_REPLICA_DISTANCE
    }

    /// Appends the tablet index if distance matches current minimum, or resets the list
    /// if it establishes a strictly lower minimum distance.
    fn collect_minimum_distance_index(
        candidates: &mut Vec<usize>,
        minimum_distance: &mut u32,
        index: usize,
        distance: u32,
    ) {
        if distance < *minimum_distance {
            *minimum_distance = distance;
            candidates.clear();
            candidates.push(index);
            return;
        }
        if distance == *minimum_distance {
            candidates.push(index);
        }
    }
}

/// An in-memory cache representation of a Spanner split key range.
#[derive(Debug)]
pub(crate) struct CachedRange {
    pub start_key: Bytes,
    pub limit_key: Bytes,
    pub group_uid: u64,
    pub split_id: u64,
    pub generation: Bytes,
    pub last_access: AtomicU64,
}

impl CachedRange {
    pub(crate) fn new(
        start_key: Bytes,
        limit_key: Bytes,
        group_uid: u64,
        split_id: u64,
        generation: Bytes,
        last_access: u64,
    ) -> Self {
        Self {
            start_key,
            limit_key,
            group_uid,
            split_id,
            generation,
            last_access: AtomicU64::new(last_access),
        }
    }
}

#[derive(Default)]
struct CacheState {
    /// Maps `start_key` (start of range, inclusive) to the cached split range.
    ranges: BTreeMap<Bytes, Arc<CachedRange>>,
    /// Maps `group_uid` to the cached replica group.
    groups: HashMap<u64, Arc<CachedGroup>>,
}

/// Thread-safe in-memory key range cache mapping split boundaries to replica groups.
pub(crate) struct KeyRangeCache {
    state: RwLock<CacheState>,
    access_counter: AtomicU64,
    min_cache_entries_for_random_pick: AtomicUsize,
    #[cfg(test)]
    deterministic_random: AtomicBool,
}

impl KeyRangeCache {
    /// Creates a new empty key range cache.
    pub(crate) fn new() -> Self {
        Self {
            state: RwLock::new(CacheState::default()),
            access_counter: AtomicU64::new(0),
            min_cache_entries_for_random_pick: AtomicUsize::new(1000),
            #[cfg(test)]
            deterministic_random: AtomicBool::new(false),
        }
    }

    /// Enables deterministic pseudorandom selection for golden conformance testing.
    #[cfg(test)]
    pub(crate) fn use_deterministic_random(&self) {
        self.deterministic_random.store(true, Ordering::Relaxed);
    }

    #[cfg(test)]
    fn deterministic_uniform_random(
        &self,
        range_bound: usize,
        key_seed: &[u8],
        limit_seed: &[u8],
        start_key_seed: &[u8],
    ) -> usize {
        let combined = [key_seed, limit_seed, start_key_seed].concat();
        let hash = crc32c(&combined);
        (hash as usize) % range_bound
    }

    fn uniform_random(
        &self,
        range_bound: usize,
        key_seed: &[u8],
        limit_seed: &[u8],
        start_key_seed: &[u8],
    ) -> usize {
        #[cfg(test)]
        if self.deterministic_random.load(Ordering::Relaxed) {
            return self.deterministic_uniform_random(
                range_bound,
                key_seed,
                limit_seed,
                start_key_seed,
            );
        }
        let _ = (key_seed, limit_seed, start_key_seed);
        random_range(0..range_bound)
    }

    /// Returns the current logical access time counter value and increments it.
    ///
    /// Note: `fetch_add` returns the previous value, so adding `1` yields the new value
    /// after the increment (matching Java's `AtomicLong.incrementAndGet()`).
    pub(crate) fn access_time_now(&self) -> u64 {
        self.access_counter.fetch_add(1, Ordering::Relaxed) + 1
    }

    /// Returns `true` if the cache has no stored ranges.
    pub(crate) fn is_empty(&self) -> bool {
        self.state
            .read()
            .expect("lock cache state for is_empty")
            .ranges
            .is_empty()
    }

    /// Returns the number of cached split ranges.
    pub(crate) fn len(&self) -> usize {
        self.state
            .read()
            .expect("lock cache state for len")
            .ranges
            .len()
    }

    /// Clears all cached ranges and groups.
    pub(crate) fn clear(&self) {
        let old_state = {
            let mut state = self.state.write().expect("lock cache state for clear");
            take(&mut *state)
        };
        drop(old_state);
    }

    /// Returns the cached group for the given group UID, if present.
    pub(crate) fn get_group(&self, group_uid: u64) -> Option<Arc<CachedGroup>> {
        self.state
            .read()
            .expect("lock cache state for get_group")
            .groups
            .get(&group_uid)
            .map(Arc::clone)
    }

    /// Applies updates from a Spanner `CacheUpdate` message.
    pub(crate) fn add_ranges(&self, cache_update: &CacheUpdate) {
        if cache_update.group.is_empty() && cache_update.range.is_empty() {
            return;
        }
        let mut state = self.state.write().expect("lock cache state for add_ranges");

        for group_in in &cache_update.group {
            match state.groups.entry(group_in.group_uid) {
                Entry::Occupied(mut entry) if group_in.generation >= entry.get().generation => {
                    entry.insert(Arc::new(CachedGroup::from_proto(group_in.clone())));
                }
                Entry::Vacant(entry) => {
                    entry.insert(Arc::new(CachedGroup::from_proto(group_in.clone())));
                }
                _ => {}
            }
        }

        for range_in in &cache_update.range {
            self.replace_range_if_newer_locked(&mut state, range_in);
        }
    }

    fn replace_range_if_newer_locked(&self, state: &mut CacheState, range_in: &Range) {
        let start_key_query = if let Some((_, first_existing)) = state
            .ranges
            .range::<[u8], _>((
                Bound::Unbounded,
                Bound::Included(range_in.start_key.as_ref()),
            ))
            .next_back()
        {
            if first_existing.limit_key.is_empty()
                || first_existing.limit_key.as_ref() > range_in.start_key.as_ref()
            {
                first_existing.start_key.as_ref()
            } else {
                range_in.start_key.as_ref()
            }
        } else {
            range_in.start_key.as_ref()
        };

        let limit_bound = if range_in.limit_key.is_empty() {
            Bound::Unbounded
        } else {
            Bound::Excluded(range_in.limit_key.as_ref())
        };

        // Step 1: Check generation against existing overlapping ranges without allocating a Vec.
        let mut has_overlap = false;
        for (_, existing) in state
            .ranges
            .range::<[u8], _>((Bound::Included(start_key_query), limit_bound))
        {
            let overlaps = existing.limit_key.is_empty()
                || existing.limit_key.as_ref() > range_in.start_key.as_ref();
            if !overlaps {
                continue;
            }
            has_overlap = true;
            if range_in.generation < existing.generation
                || (range_in.generation == existing.generation
                    && range_in.start_key == existing.start_key
                    && range_in.limit_key == existing.limit_key)
            {
                return;
            }
        }

        // Step 2: If there is no overlap, insert directly with zero intermediate allocations.
        if !has_overlap {
            let start_key = range_in.start_key.clone();
            let new_range = Arc::new(CachedRange::new(
                start_key.clone(),
                range_in.limit_key.clone(),
                range_in.group_uid,
                range_in.split_id,
                range_in.generation.clone(),
                self.access_time_now(),
            ));
            state.ranges.insert(start_key, new_range);
            return;
        }

        // Step 3: Overlapping older ranges must be removed or split.
        let mut overlapping = Vec::new();
        for (_, existing) in state
            .ranges
            .range::<[u8], _>((Bound::Included(start_key_query), limit_bound))
        {
            let overlaps = existing.limit_key.is_empty()
                || existing.limit_key.as_ref() > range_in.start_key.as_ref();
            if overlaps {
                overlapping.push(Arc::clone(existing));
            }
        }

        for existing in &overlapping {
            state.ranges.remove(&existing.start_key);
        }

        let first = &overlapping[0];
        if first.start_key < range_in.start_key {
            let start_key = first.start_key.clone();
            let head = Arc::new(CachedRange::new(
                start_key.clone(),
                range_in.start_key.clone(),
                first.group_uid,
                first.split_id,
                first.generation.clone(),
                first.last_access.load(Ordering::Relaxed),
            ));
            state.ranges.insert(start_key, head);
        }

        let last = overlapping.last().expect("overlapping is not empty");
        if !range_in.limit_key.is_empty()
            && (last.limit_key.is_empty() || last.limit_key > range_in.limit_key)
        {
            let start_key = range_in.limit_key.clone();
            let tail = Arc::new(CachedRange::new(
                start_key.clone(),
                last.limit_key.clone(),
                last.group_uid,
                last.split_id,
                last.generation.clone(),
                last.last_access.load(Ordering::Relaxed),
            ));
            state.ranges.insert(start_key, tail);
        }

        let start_key = range_in.start_key.clone();
        let new_range = Arc::new(CachedRange::new(
            start_key.clone(),
            range_in.limit_key.clone(),
            range_in.group_uid,
            range_in.split_id,
            range_in.generation.clone(),
            self.access_time_now(),
        ));
        state.ranges.insert(start_key, new_range);
    }

    /// Finds a cached range covering the specified key or range.
    ///
    /// Uses zero-allocation slice borrowing (`Bound::Excluded(key)`) to query the B-tree map.
    pub(crate) fn find_range(
        &self,
        key: &[u8],
        limit: &[u8],
        mode: RangeMode,
    ) -> Option<Arc<CachedRange>> {
        let state = self.state.read().expect("lock cache state for find_range");
        let first_range_opt = state
            .ranges
            .range::<[u8], _>((Bound::Unbounded, Bound::Included(key)))
            .next_back()
            .map(|(_, r)| r);

        if let Some(first_range) = first_range_opt {
            let in_range = first_range.limit_key.is_empty() || key < first_range.limit_key.as_ref();

            if limit.is_empty() {
                if in_range {
                    first_range
                        .last_access
                        .store(self.access_time_now(), Ordering::Relaxed);
                    return Some(Arc::clone(first_range));
                }
                return None;
            }

            let limit_in_range =
                first_range.limit_key.is_empty() || limit <= first_range.limit_key.as_ref();
            if in_range && limit_in_range {
                first_range
                    .last_access
                    .store(self.access_time_now(), Ordering::Relaxed);
                return Some(Arc::clone(first_range));
            }
        }

        if limit.is_empty() || mode == RangeMode::CoveringSplit {
            return None;
        }

        let mut total = 0usize;
        let mut found_gap = false;
        let mut sampled: Option<&Arc<CachedRange>> = None;
        let mut last_limit: &[u8] = key;

        let scan_start = match first_range_opt {
            Some(first_range)
                if first_range.limit_key.is_empty() || key < first_range.limit_key.as_ref() =>
            {
                total = 1;
                sampled = Some(first_range);
                last_limit = first_range.limit_key.as_ref();
                Bound::Excluded(first_range.start_key.as_ref())
            }
            Some(first_range) => {
                found_gap = true;
                Bound::Excluded(first_range.start_key.as_ref())
            }
            None => {
                found_gap = true;
                Bound::Included(key)
            }
        };

        for (_, current) in state
            .ranges
            .range::<[u8], _>((scan_start, Bound::Unbounded))
        {
            if current.start_key.as_ref() >= limit {
                break;
            }
            if last_limit != current.start_key.as_ref() {
                found_gap = true;
            }
            total += 1;
            if total == 1 || self.uniform_random(total, key, limit, current.start_key.as_ref()) == 0
            {
                sampled = Some(current);
            }
            last_limit = current.limit_key.as_ref();
            if current.limit_key.is_empty()
                || last_limit >= limit
                || total
                    >= self
                        .min_cache_entries_for_random_pick
                        .load(Ordering::Relaxed)
            {
                break;
            }
        }

        if !last_limit.is_empty() && last_limit < limit {
            found_gap = true;
        }

        if let Some(sampled_range) = sampled
            && (!found_gap
                || total
                    >= self
                        .min_cache_entries_for_random_pick
                        .load(Ordering::Relaxed))
        {
            sampled_range
                .last_access
                .store(self.access_time_now(), Ordering::Relaxed);
            return Some(Arc::clone(sampled_range));
        }

        None
    }

    /// Returns all eligible candidate tablets in the lowest distance tier for the split range.
    ///
    /// If `prefer_leader` is `true` and a valid leader is present, returns a single-element
    /// vector containing that leader. Otherwise, returns candidate replicas in the lowest available
    /// distance tier.
    pub(crate) fn get_eligible_tablets(
        &self,
        range: &CachedRange,
        prefer_leader: bool,
    ) -> Option<Vec<Tablet>> {
        let group = self.get_group(range.group_uid)?;
        let eligible = group.eligible_tablets(prefer_leader);
        if eligible.is_empty() {
            return None;
        }
        Some(eligible.into_iter().cloned().collect())
    }

    /// Selects an appropriate tablet replica from the cached range's group.
    ///
    /// If `prefer_leader` is `true` and a valid local leader is present, selects that leader.
    /// Otherwise, selects uniformly among candidate replicas within the lowest available distance tier.
    pub(crate) fn select_tablet(&self, range: &CachedRange, prefer_leader: bool) -> Option<Tablet> {
        let group = self.get_group(range.group_uid)?;

        if prefer_leader && let Some(leader) = group.local_leader() {
            return Some(leader.clone());
        }

        let indices = &group.eligible_replica_indices;
        if indices.is_empty() {
            return None;
        }
        if indices.len() == 1 {
            return Some(group.tablets[indices[0]].clone());
        }
        let selected_index = random_range(0..indices.len());
        Some(group.tablets[indices[selected_index]].clone())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::tablet::Role;
    use static_assertions::assert_impl_all;
    use std::collections::HashSet;
    use std::fmt::Debug;

    #[test]
    fn key_range_cache_types_implement_expected_traits() {
        assert_impl_all!(KeyRangeCache: Send, Sync);
        assert_impl_all!(RangeMode: Send, Sync, Debug, Clone, Copy, PartialEq, Eq);
        assert_impl_all!(CachedRange: Send, Sync, Debug);
        assert_impl_all!(CachedGroup: Send, Sync, Debug, Clone);
    }

    fn make_range(
        start: &'static str,
        limit: &'static str,
        group_uid: u64,
        generation: &'static str,
    ) -> Range {
        Range {
            start_key: Bytes::from_static(start.as_bytes()),
            limit_key: Bytes::from_static(limit.as_bytes()),
            group_uid,
            split_id: group_uid,
            generation: Bytes::from_static(generation.as_bytes()),
            _unknown_fields: Default::default(),
        }
    }

    fn make_group(group_uid: u64, generation: &'static str, leader_index: i32) -> Group {
        Group {
            group_uid,
            generation: Bytes::from_static(generation.as_bytes()),
            tablets: vec![
                Tablet {
                    tablet_uid: 1,
                    server_address: "localhost:8001".to_string(),
                    location: "us-central1".to_string(),
                    role: Role::ReadWrite,
                    incarnation: Bytes::from_static(b"1"),
                    distance: 0,
                    skip: false,
                    _unknown_fields: Default::default(),
                },
                Tablet {
                    tablet_uid: 2,
                    server_address: "localhost:8002".to_string(),
                    location: "us-central1".to_string(),
                    role: Role::ReadWrite,
                    incarnation: Bytes::from_static(b"1"),
                    distance: 1,
                    skip: false,
                    _unknown_fields: Default::default(),
                },
            ],
            leader_index,
            _unknown_fields: Default::default(),
        }
    }

    #[test]
    fn lookup_empty_cache_returns_none() {
        let cache = KeyRangeCache::new();
        assert!(cache.is_empty());
        assert_eq!(cache.len(), 0);
        assert!(
            cache
                .find_range(b"a", b"", RangeMode::CoveringSplit)
                .is_none()
        );
        assert!(
            cache
                .find_range(b"a", b"z", RangeMode::CoveringSplit)
                .is_none()
        );
    }

    #[test]
    fn lookup_before_first_range_returns_none() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("m", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        assert!(
            cache
                .find_range(b"a", b"", RangeMode::CoveringSplit)
                .is_none()
        );
        assert!(
            cache
                .find_range(b"a", b"f", RangeMode::CoveringSplit)
                .is_none()
        );
    }

    #[test]
    fn lookup_after_last_range_returns_none() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "m", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        assert!(
            cache
                .find_range(b"z", b"", RangeMode::CoveringSplit)
                .is_none()
        );
        assert!(
            cache
                .find_range(b"p", b"z", RangeMode::CoveringSplit)
                .is_none()
        );
    }

    #[test]
    fn lookup_gap_between_ranges_returns_none() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "f", 1, "1"), make_range("m", "z", 2, "1")],
            group: vec![make_group(1, "1", 0), make_group(2, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        assert!(
            cache
                .find_range(b"g", b"", RangeMode::CoveringSplit)
                .is_none()
        );
        assert!(
            cache
                .find_range(b"g", b"k", RangeMode::CoveringSplit)
                .is_none()
        );
    }

    #[test]
    fn point_lookup_at_start_key_returns_range() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache
            .find_range(b"a", b"", RangeMode::CoveringSplit)
            .expect("start key is inclusive");
        assert_eq!(hit.split_id, 1);
    }

    #[test]
    fn point_lookup_at_limit_key_returns_none() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        assert!(
            cache
                .find_range(b"z", b"", RangeMode::CoveringSplit)
                .is_none(),
            "limit key is exclusive"
        );
    }

    #[test]
    fn point_lookup_in_middle_returns_range() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache
            .find_range(b"m", b"", RangeMode::CoveringSplit)
            .expect("m should be inside [a, z)");
        assert_eq!(hit.split_id, 1);
    }

    #[test]
    fn covering_split_exact_match_returns_range() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache
            .find_range(b"a", b"z", RangeMode::CoveringSplit)
            .expect("exact range match should return split");
        assert_eq!(hit.split_id, 1);
    }

    #[test]
    fn covering_split_subrange_returns_range() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache
            .find_range(b"c", b"k", RangeMode::CoveringSplit)
            .expect("subrange inside split should succeed");
        assert_eq!(hit.split_id, 1);
    }

    #[test]
    fn covering_split_spanning_multiple_ranges_returns_none() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "m", 1, "1"), make_range("m", "z", 2, "1")],
            group: vec![make_group(1, "1", 0), make_group(2, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        assert!(
            cache
                .find_range(b"c", b"p", RangeMode::CoveringSplit)
                .is_none(),
            "range spanning two splits should return None in CoveringSplit mode"
        );
    }

    #[test]
    fn pick_random_single_range_returns_range() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache
            .find_range(b"c", b"k", RangeMode::PickRandom)
            .expect("single range match in PickRandom");
        assert_eq!(hit.split_id, 1);
    }

    #[test]
    fn pick_random_spanning_multiple_contiguous_ranges_returns_one() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "m", 1, "1"), make_range("m", "z", 2, "1")],
            group: vec![make_group(1, "1", 0), make_group(2, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache
            .find_range(b"c", b"p", RangeMode::PickRandom)
            .expect("contiguous multi-split should sample one split in PickRandom");
        assert!(hit.split_id == 1 || hit.split_id == 2);
    }

    #[test]
    fn pick_random_with_gap_returns_none() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "f", 1, "1"), make_range("g", "z", 2, "1")],
            group: vec![make_group(1, "1", 0), make_group(2, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        assert!(
            cache
                .find_range(b"c", b"p", RangeMode::PickRandom)
                .is_none(),
            "gap between [a, f) and [g, z) should return None in PickRandom"
        );
    }

    #[test]
    fn newer_generation_replaces_entire_range() {
        let cache = KeyRangeCache::new();
        let update1 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update1);

        let update2 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 2, "2")],
            group: vec![make_group(2, "2", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update2);

        assert_eq!(cache.len(), 1);
        let hit = cache
            .find_range(b"m", b"", RangeMode::CoveringSplit)
            .expect("newer range");
        assert_eq!(hit.split_id, 2);
    }

    #[test]
    fn older_generation_is_ignored() {
        let cache = KeyRangeCache::new();
        let update1 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "2")],
            group: vec![make_group(1, "2", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update1);

        let update2 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 2, "1")],
            group: vec![make_group(2, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update2);

        assert_eq!(cache.len(), 1);
        let hit = cache
            .find_range(b"m", b"", RangeMode::CoveringSplit)
            .expect("original newer range should remain");
        assert_eq!(hit.split_id, 1);
    }

    #[test]
    fn equal_generation_is_ignored() {
        let cache = KeyRangeCache::new();
        let update1 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update1);

        let update2 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update2);

        assert_eq!(cache.len(), 1);
    }

    #[test]
    fn newer_generation_splitting_head_and_tail() {
        let cache = KeyRangeCache::new();
        let update1 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update1);

        let update2 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("g", "m", 2, "2")],
            group: vec![make_group(2, "2", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update2);

        assert_eq!(
            cache.len(),
            3,
            "range [a, z) should be split into [a, g), [g, m), and [m, z)"
        );

        let left = cache
            .find_range(b"c", b"", RangeMode::CoveringSplit)
            .expect("left head");
        assert_eq!(left.split_id, 1);
        assert_eq!(left.limit_key.as_ref(), b"g");

        let mid = cache
            .find_range(b"j", b"", RangeMode::CoveringSplit)
            .expect("middle newer range");
        assert_eq!(mid.split_id, 2);

        let right = cache
            .find_range(b"t", b"", RangeMode::CoveringSplit)
            .expect("right tail");
        assert_eq!(right.split_id, 1);
        assert_eq!(right.start_key.as_ref(), b"m");
    }

    #[test]
    fn newer_generation_overwriting_start_of_range() {
        let cache = KeyRangeCache::new();
        let update1 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update1);

        let update2 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "m", 2, "2")],
            group: vec![make_group(2, "2", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update2);

        assert_eq!(cache.len(), 2);
        let left = cache
            .find_range(b"c", b"", RangeMode::CoveringSplit)
            .expect("new left range");
        assert_eq!(left.split_id, 2);

        let right = cache
            .find_range(b"t", b"", RangeMode::CoveringSplit)
            .expect("remaining tail");
        assert_eq!(right.split_id, 1);
        assert_eq!(right.start_key.as_ref(), b"m");
    }

    #[test]
    fn newer_generation_overwriting_end_of_range() {
        let cache = KeyRangeCache::new();
        let update1 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update1);

        let update2 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("m", "z", 2, "2")],
            group: vec![make_group(2, "2", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update2);

        assert_eq!(cache.len(), 2);
        let left = cache
            .find_range(b"c", b"", RangeMode::CoveringSplit)
            .expect("remaining head");
        assert_eq!(left.split_id, 1);
        assert_eq!(left.limit_key.as_ref(), b"m");

        let right = cache
            .find_range(b"t", b"", RangeMode::CoveringSplit)
            .expect("new right range");
        assert_eq!(right.split_id, 2);
    }

    #[test]
    fn select_tablet_prefers_leader_when_requested() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 1)], // leader_index = 1
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache
            .find_range(b"m", b"", RangeMode::CoveringSplit)
            .expect("range hit");
        let tablet = cache
            .select_tablet(&hit, true)
            .expect("should select leader");
        assert_eq!(tablet.tablet_uid, 2);
    }

    #[test]
    fn select_tablet_falls_back_when_leader_skipped_or_empty() {
        let cache = KeyRangeCache::new();
        let mut group = make_group(1, "1", 1);
        group.tablets[1].skip = true; // Mark leader as skipped

        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![group],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache
            .find_range(b"m", b"", RangeMode::CoveringSplit)
            .expect("range hit");
        let tablet = cache
            .select_tablet(&hit, true)
            .expect("should fall back to non-leader tablet");
        assert_eq!(tablet.tablet_uid, 1);
    }

    #[test]
    fn select_tablet_prefers_distance_less_than_five() {
        let cache = KeyRangeCache::new();
        let mut group = make_group(1, "1", -1);
        group.tablets[0].distance = 10; // Far replica
        group.tablets[1].distance = 2; // Close replica

        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![group],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache
            .find_range(b"m", b"", RangeMode::CoveringSplit)
            .expect("range hit");
        let tablet = cache
            .select_tablet(&hit, false)
            .expect("should select close replica");
        assert_eq!(tablet.tablet_uid, 2);
    }

    #[test]
    fn select_tablet_prefers_minimum_distance() {
        let cache = KeyRangeCache::new();
        let mut group = make_group(1, "1", -1);
        group.tablets.push(Tablet {
            tablet_uid: 3,
            server_address: "localhost:8003".to_string(),
            location: "us-central1".to_string(),
            role: Role::ReadWrite,
            incarnation: Bytes::from_static(b"1"),
            distance: 10,
            skip: false,
            _unknown_fields: Default::default(),
        });
        group.tablets[0].distance = 4; // same region, different zone
        group.tablets[1].distance = 0; // same zone
        group.tablets[2].distance = 10; // remote region

        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![group],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache
            .find_range(b"m", b"", RangeMode::CoveringSplit)
            .expect("range hit");
        let tablet = cache
            .select_tablet(&hit, false)
            .expect("should select minimum distance replica");
        assert_eq!(
            tablet.tablet_uid, 2,
            "tablet uid 2 has distance 0, preferring it over distance 4 and 10"
        );
    }

    #[test]
    fn select_tablet_returns_none_for_empty_group() {
        let cache = KeyRangeCache::new();
        let mut group = make_group(1, "1", -1);
        group.tablets.clear();

        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![group],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache
            .find_range(b"m", b"", RangeMode::CoveringSplit)
            .expect("range hit");
        assert!(cache.select_tablet(&hit, true).is_none());
    }

    #[test]
    fn clear_removes_all_ranges_and_groups() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);
        assert_eq!(cache.len(), 1);

        cache.clear();
        assert!(cache.is_empty());
        assert_eq!(cache.len(), 0);
        assert!(cache.get_group(1).is_none());
    }

    #[test]
    fn access_time_updates_on_lookup() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit1 = cache
            .find_range(b"m", b"", RangeMode::CoveringSplit)
            .expect("hit 1");
        let t1 = hit1.last_access.load(Ordering::Relaxed);

        let hit2 = cache
            .find_range(b"m", b"", RangeMode::CoveringSplit)
            .expect("hit 2");
        let t2 = hit2.last_access.load(Ordering::Relaxed);

        assert!(t2 > t1, "access time should increment on each lookup");
    }

    #[test]
    fn unbounded_right_split_lookup() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("m", "", 1, "1")], // right-unbounded (+inf)
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        assert!(
            cache
                .find_range(b"a", b"", RangeMode::CoveringSplit)
                .is_none()
        );
        assert!(
            cache
                .find_range(b"m", b"", RangeMode::CoveringSplit)
                .is_some()
        );
        assert!(
            cache
                .find_range(b"z", b"", RangeMode::CoveringSplit)
                .is_some()
        );
        assert!(
            cache
                .find_range(b"z\x00", b"", RangeMode::CoveringSplit)
                .is_some()
        );
    }

    #[test]
    fn unbounded_left_split_lookup() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("", "m", 1, "1")], // left-unbounded (-inf)
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        assert!(
            cache
                .find_range(b"", b"", RangeMode::CoveringSplit)
                .is_some()
        );
        assert!(
            cache
                .find_range(b"\x00", b"", RangeMode::CoveringSplit)
                .is_some()
        );
        assert!(
            cache
                .find_range(b"a", b"", RangeMode::CoveringSplit)
                .is_some()
        );
        assert!(
            cache
                .find_range(b"m", b"", RangeMode::CoveringSplit)
                .is_none()
        );
    }

    #[test]
    fn full_table_unbounded_split_lookup() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("", "", 1, "1")], // entire table (-inf .. +inf)
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        assert!(
            cache
                .find_range(b"", b"", RangeMode::CoveringSplit)
                .is_some()
        );
        assert!(
            cache
                .find_range(b"a", b"", RangeMode::CoveringSplit)
                .is_some()
        );
        assert!(
            cache
                .find_range(b"z", b"", RangeMode::CoveringSplit)
                .is_some()
        );
    }

    #[test]
    fn newer_generation_replaces_unbounded_split() {
        let cache = KeyRangeCache::new();
        let update1 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("", "", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update1);

        // Replace entire unbounded split with a newer generation subrange
        let update2 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("f", "m", 2, "2")],
            group: vec![make_group(2, "2", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update2);

        // Check head (-inf .. f), middle (f .. m), tail (m .. +inf)
        let hit_head = cache
            .find_range(b"a", b"", RangeMode::CoveringSplit)
            .expect("head");
        assert_eq!(hit_head.group_uid, 1);
        assert_eq!(hit_head.limit_key, Bytes::from_static(b"f"));

        let hit_mid = cache
            .find_range(b"g", b"", RangeMode::CoveringSplit)
            .expect("mid");
        assert_eq!(hit_mid.group_uid, 2);

        let hit_tail = cache
            .find_range(b"z", b"", RangeMode::CoveringSplit)
            .expect("tail");
        assert_eq!(hit_tail.group_uid, 1);
        assert_eq!(hit_tail.start_key, Bytes::from_static(b"m"));
        assert_eq!(hit_tail.limit_key, Bytes::from_static(b""));
    }

    #[test]
    fn pick_random_with_no_initial_overlap_never_samples_preceding_range() {
        let cache = KeyRangeCache::new();
        // Set min entries to 1 so that random pick returns even with a gap
        cache
            .min_cache_entries_for_random_pick
            .store(1, Ordering::Relaxed);

        let update = CacheUpdate {
            database_id: 1,
            range: vec![
                make_range("a", "c", 1, "1"), // preceding range not covering "d"
                make_range("e", "g", 2, "1"), // range after gap
            ],
            group: vec![make_group(1, "1", 0), make_group(2, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache.find_range(b"d", b"g", RangeMode::PickRandom);
        assert!(hit.is_some());
        assert_eq!(
            hit.expect("hit").group_uid,
            2,
            "must never sample preceding non-overlapping range"
        );
    }

    #[test]
    fn pick_random_with_no_preceding_range_finds_subsequent_range_after_gap() {
        let cache = KeyRangeCache::new();
        cache
            .min_cache_entries_for_random_pick
            .store(1, Ordering::Relaxed);

        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("m", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache.find_range(b"a", b"p", RangeMode::PickRandom);
        assert!(hit.is_some());
        assert_eq!(hit.expect("hit").group_uid, 1);
    }

    #[test]
    fn equal_generation_with_different_bounds_overwrites_overlapping_portion() {
        let cache = KeyRangeCache::new();
        let update1 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update1);

        // Same generation "1", but subrange "m" .. "p" with a new group
        let update2 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("m", "p", 2, "1")],
            group: vec![make_group(2, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update2);

        let hit_mid = cache
            .find_range(b"n", b"", RangeMode::CoveringSplit)
            .expect("mid");
        assert_eq!(hit_mid.group_uid, 2);
    }

    #[test]
    fn newer_generation_spanning_multiple_ranges_splits_head_and_tail() {
        let cache = KeyRangeCache::new();
        let update1 = CacheUpdate {
            database_id: 1,
            range: vec![
                make_range("a", "d", 1, "1"),
                make_range("d", "g", 2, "1"),
                make_range("g", "j", 3, "1"),
            ],
            group: vec![
                make_group(1, "1", 0),
                make_group(2, "1", 0),
                make_group(3, "1", 0),
            ],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update1);

        let update2 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("b", "i", 4, "2")],
            group: vec![make_group(4, "2", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update2);

        assert_eq!(cache.len(), 3);
        let head = cache
            .find_range(b"a", b"", RangeMode::CoveringSplit)
            .expect("head");
        assert_eq!(head.group_uid, 1);
        assert_eq!(head.limit_key, Bytes::from_static(b"b"));

        let mid = cache
            .find_range(b"e", b"", RangeMode::CoveringSplit)
            .expect("mid");
        assert_eq!(mid.group_uid, 4);

        let tail = cache
            .find_range(b"i", b"", RangeMode::CoveringSplit)
            .expect("tail");
        assert_eq!(tail.group_uid, 3);
        assert_eq!(tail.start_key, Bytes::from_static(b"i"));
        assert_eq!(tail.limit_key, Bytes::from_static(b"j"));

        assert_eq!(
            cache
                .find_range(b"d", b"", RangeMode::CoveringSplit)
                .expect("range")
                .group_uid,
            4
        );
    }

    #[test]
    fn replace_range_log_n_start_key_query_cases() {
        let cache = KeyRangeCache::new();

        // Case 3: Empty cache insert ["m" .. "p")
        let update1 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("m", "p", 1, "1")],
            group: vec![make_group(1, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update1);
        assert_eq!(cache.len(), 1);

        // Case 3 (non-empty): Insert ["a" .. "c") before ["m" .. "p")
        let update2 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "c", 2, "1")],
            group: vec![make_group(2, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update2);
        assert_eq!(cache.len(), 2);

        // Case 2: Insert adjacent ["c" .. "e") right after ["a" .. "c")
        let update3 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("c", "e", 3, "1")],
            group: vec![make_group(3, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update3);
        assert_eq!(cache.len(), 3);

        // Case 1: Insert ["d" .. "n") with generation 2 overlapping both ["c".."e") and ["m".."p")
        let update4 = CacheUpdate {
            database_id: 1,
            range: vec![make_range("d", "n", 4, "2")],
            group: vec![make_group(4, "2", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update4);

        // Check resulting splits:
        // 1. ["a" .. "c") - group 2
        // 2. ["c" .. "d") - group 3 (split head of ["c" .. "e"))
        // 3. ["d" .. "n") - group 4 (new range)
        // 4. ["n" .. "p") - group 1 (split tail of ["m" .. "p"))
        assert_eq!(cache.len(), 4);

        let range_a = cache
            .find_range(b"a", b"", RangeMode::CoveringSplit)
            .expect("find range starting at 'a'");
        assert_eq!(range_a.group_uid, 2);
        assert_eq!(range_a.limit_key, Bytes::from_static(b"c"));

        let range_c = cache
            .find_range(b"c", b"", RangeMode::CoveringSplit)
            .expect("find range starting at 'c'");
        assert_eq!(range_c.group_uid, 3);
        assert_eq!(range_c.limit_key, Bytes::from_static(b"d"));

        let range_d = cache
            .find_range(b"d", b"", RangeMode::CoveringSplit)
            .expect("find range starting at 'd'");
        assert_eq!(range_d.group_uid, 4);
        assert_eq!(range_d.limit_key, Bytes::from_static(b"n"));

        let range_n = cache
            .find_range(b"n", b"", RangeMode::CoveringSplit)
            .expect("find range starting at 'n'");
        assert_eq!(range_n.group_uid, 1);
        assert_eq!(range_n.limit_key, Bytes::from_static(b"p"));
    }

    #[test]
    fn traits() {
        assert_impl_all!(KeyRangeCache: Send, Sync);
        assert_impl_all!(CachedGroup: Send, Sync, Debug, Clone);
        assert_impl_all!(CachedRange: Send, Sync, Debug);
    }

    #[test]
    fn cached_group_leader_accessors() {
        // 1. Group with no leader (leader_index = -1)
        let group_no_leader = CachedGroup::from_proto(make_group(1, "1", -1));
        assert!(!group_no_leader.has_leader());
        assert!(group_no_leader.leader().is_none());
        assert!(group_no_leader.local_leader().is_none());

        // 2. Group with valid local leader (leader_index = 0, distance = 0)
        let group_local_leader = CachedGroup::from_proto(make_group(1, "1", 0));
        assert!(group_local_leader.has_leader());
        assert_eq!(
            group_local_leader
                .leader()
                .expect("should find leader")
                .tablet_uid,
            1
        );
        assert_eq!(
            group_local_leader
                .local_leader()
                .expect("should find local leader")
                .tablet_uid,
            1
        );

        // 3. Group with remote leader (leader_index = 0, distance = 10)
        let mut proto_remote = make_group(1, "1", 0);
        proto_remote.tablets[0].distance = 10;
        let group_remote_leader = CachedGroup::from_proto(proto_remote);
        assert!(group_remote_leader.has_leader());
        assert_eq!(
            group_remote_leader
                .leader()
                .expect("should find leader")
                .tablet_uid,
            1
        );
        assert!(
            group_remote_leader.local_leader().is_none(),
            "remote leader with distance 10 must not be considered a local leader"
        );

        // 4. Group with skipped leader
        let mut proto_skipped = make_group(1, "1", 0);
        proto_skipped.tablets[0].skip = true;
        let group_skipped_leader = CachedGroup::from_proto(proto_skipped);
        assert!(group_skipped_leader.has_leader());
        assert!(group_skipped_leader.leader().is_none());
        assert!(group_skipped_leader.local_leader().is_none());

        // 5. Group with empty address leader
        let mut proto_empty_address = make_group(1, "1", 0);
        proto_empty_address.tablets[0].server_address.clear();
        let group_empty_address_leader = CachedGroup::from_proto(proto_empty_address);
        assert!(group_empty_address_leader.has_leader());
        assert!(group_empty_address_leader.leader().is_none());
        assert!(group_empty_address_leader.local_leader().is_none());
    }

    #[test]
    fn cached_group_eligible_tablets_partitions_local_and_remote() {
        let mut proto = make_group(1, "1", -1);
        proto.tablets.push(Tablet {
            tablet_uid: 3,
            server_address: "localhost:8003".to_string(),
            location: "us-central1".to_string(),
            role: Role::ReadWrite,
            incarnation: Bytes::from_static(b"1"),
            distance: 10,
            skip: false,
            _unknown_fields: Default::default(),
        });
        proto.tablets[0].distance = 4; // Local tier
        proto.tablets[1].distance = 2; // Local tier (minimum)
        proto.tablets[2].distance = 10; // Remote tier

        let group = CachedGroup::from_proto(proto.clone());
        let eligible = group.eligible_tablets(false);
        assert_eq!(
            eligible.len(),
            1,
            "should select single minimum distance local replica"
        );
        assert_eq!(eligible[0].tablet_uid, 2);

        // When multiple replicas tie at the minimum local distance
        let mut proto_tied = proto;
        proto_tied.tablets[0].distance = 2; // tied with tablet 2 at distance 2
        let group_tied = CachedGroup::from_proto(proto_tied);
        let eligible_tied = group_tied.eligible_tablets(false);
        assert_eq!(
            eligible_tied.len(),
            2,
            "both local candidates at distance 2 should be eligible"
        );

        // When only remote replicas exist (all distance > 5)
        let mut proto_remote_only = make_group(1, "1", -1);
        proto_remote_only.tablets[0].distance = 15;
        proto_remote_only.tablets[1].distance = 10; // lowest remote distance
        let group_remote_only = CachedGroup::from_proto(proto_remote_only);
        let eligible_remote = group_remote_only.eligible_tablets(false);
        assert_eq!(
            eligible_remote.len(),
            1,
            "should select lowest remote distance replica"
        );
        assert_eq!(eligible_remote[0].tablet_uid, 2);
    }

    #[test]
    fn get_group_and_get_eligible_tablets() {
        let cache = KeyRangeCache::new();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 100, "1")],
            group: vec![make_group(100, "1", 0)],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let group = cache.get_group(100);
        assert!(group.is_some(), "group 100 should be cached");
        assert_eq!(group.expect("group").group_uid, 100);

        assert!(
            cache.get_group(999).is_none(),
            "non-existent group should return None"
        );

        let range = cache
            .find_range(b"m", b"", RangeMode::CoveringSplit)
            .expect("range hit");

        let eligible_leader = cache.get_eligible_tablets(&range, true);
        assert!(eligible_leader.is_some());
        assert_eq!(eligible_leader.expect("eligible leader").len(), 1);

        let eligible_replicas = cache.get_eligible_tablets(&range, false);
        assert!(eligible_replicas.is_some());
        assert_eq!(
            eligible_replicas.expect("eligible replicas")[0].tablet_uid,
            1
        );
    }

    #[test]
    fn select_tablet_prefers_local_leader_and_falls_back_when_remote() {
        let cache = KeyRangeCache::new();
        let mut group = make_group(1, "1", 0);
        // Leader (tablet 1) is remote (distance = 20 > MAX_LOCAL_REPLICA_DISTANCE = 5)
        group.tablets[0].distance = 20;
        // Replica (tablet 2) is local (distance = 1)
        group.tablets[1].distance = 1;

        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![group],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache
            .find_range(b"m", b"", RangeMode::CoveringSplit)
            .expect("range hit");

        // When prefer_leader = true but the leader is remote, it falls back to the local replica
        // to avoid high cross-region latency.
        let selected_leader = cache
            .select_tablet(&hit, true)
            .expect("should select tablet");
        assert_eq!(
            selected_leader.tablet_uid, 2,
            "must fall back to local replica (UID 2) when leader is remote"
        );

        // When prefer_leader = false, the local replica tier (distance 1) is selected.
        let selected_replica = cache
            .select_tablet(&hit, false)
            .expect("should select local replica");
        assert_eq!(
            selected_replica.tablet_uid, 2,
            "must prefer local replica (UID 2) when prefer_leader is false"
        );
    }

    #[test]
    fn parse_leader_index_out_of_bounds() {
        // leader_index = 5 when tablets count = 2
        let group_out_of_bounds = CachedGroup::from_proto(make_group(1, "1", 5));
        assert!(!group_out_of_bounds.has_leader());
        assert!(group_out_of_bounds.leader().is_none());
        assert!(group_out_of_bounds.local_leader().is_none());
    }

    #[test]
    fn eligible_tablets_prefer_leader_falls_back_to_local_when_remote() {
        let mut proto = make_group(1, "1", 0);
        proto.tablets[0].distance = 20; // Remote leader
        proto.tablets[1].distance = 1; // Local replica
        let group = CachedGroup::from_proto(proto);

        // When prefer_leader is true but leader is remote, eligible_tablets falls back to local replica
        let eligible = group.eligible_tablets(true);
        assert_eq!(eligible.len(), 1);
        assert_eq!(eligible[0].tablet_uid, 2);

        // When prefer_leader is false, eligible_tablets returns the local replica
        let eligible_read = group.eligible_tablets(false);
        assert_eq!(eligible_read.len(), 1);
        assert_eq!(eligible_read[0].tablet_uid, 2);
    }

    #[test]
    fn compute_eligible_replica_indices_clears_prior_remote_when_local_discovered() {
        let mut proto = make_group(1, "1", -1);
        proto.tablets.clear();
        proto.tablets.push(Tablet {
            tablet_uid: 1,
            server_address: "localhost:8001".to_string(),
            location: "us-central1".to_string(),
            role: Role::ReadWrite,
            incarnation: Bytes::from_static(b"1"),
            distance: 10, // Remote seen first
            skip: false,
            _unknown_fields: Default::default(),
        });
        proto.tablets.push(Tablet {
            tablet_uid: 2,
            server_address: "localhost:8002".to_string(),
            location: "us-central1".to_string(),
            role: Role::ReadWrite,
            incarnation: Bytes::from_static(b"1"),
            distance: 3, // Local seen second (should clear remote)
            skip: false,
            _unknown_fields: Default::default(),
        });
        proto.tablets.push(Tablet {
            tablet_uid: 3,
            server_address: "localhost:8003".to_string(),
            location: "us-central1".to_string(),
            role: Role::ReadWrite,
            incarnation: Bytes::from_static(b"1"),
            distance: 3, // Local tied with tablet 2
            skip: false,
            _unknown_fields: Default::default(),
        });
        proto.tablets.push(Tablet {
            tablet_uid: 4,
            server_address: "localhost:8004".to_string(),
            location: "us-central1".to_string(),
            role: Role::ReadWrite,
            incarnation: Bytes::from_static(b"1"),
            distance: 1, // Strictly lower local distance (should clear tablets 2 and 3)
            skip: false,
            _unknown_fields: Default::default(),
        });
        proto.tablets.push(Tablet {
            tablet_uid: 5,
            server_address: "localhost:8005".to_string(),
            location: "us-central1".to_string(),
            role: Role::ReadWrite,
            incarnation: Bytes::from_static(b"1"),
            distance: 1, // Skipped replica with distance 1 (must be ignored)
            skip: true,
            _unknown_fields: Default::default(),
        });
        proto.tablets.push(Tablet {
            tablet_uid: 6,
            server_address: "".to_string(), // Empty address (must be ignored)
            location: "us-central1".to_string(),
            role: Role::ReadWrite,
            incarnation: Bytes::from_static(b"1"),
            distance: 1,
            skip: false,
            _unknown_fields: Default::default(),
        });

        let group = CachedGroup::from_proto(proto);
        let eligible = group.eligible_tablets(false);
        assert_eq!(eligible.len(), 1, "only tablet 4 should be eligible");
        assert_eq!(eligible[0].tablet_uid, 4);
    }

    #[test]
    fn get_eligible_tablets_and_select_tablet_missing_group_and_empty_tablets() {
        let cache = KeyRangeCache::new();
        let range = CachedRange::new(
            Bytes::from_static(b"a"),
            Bytes::from_static(b"z"),
            999, // Non-existent group UID
            1,
            Bytes::from_static(b"1"),
            0,
        );

        assert!(cache.get_eligible_tablets(&range, true).is_none());
        assert!(cache.get_eligible_tablets(&range, false).is_none());
        assert!(cache.select_tablet(&range, true).is_none());
        assert!(cache.select_tablet(&range, false).is_none());

        // Group exists but has zero routable tablets
        let mut group = make_group(100, "1", -1);
        group.tablets[0].skip = true;
        group.tablets[1].server_address.clear();
        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 100, "1")],
            group: vec![group],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache
            .find_range(b"m", b"", RangeMode::CoveringSplit)
            .expect("range hit");
        assert!(cache.get_eligible_tablets(&hit, true).is_none());
        assert!(cache.get_eligible_tablets(&hit, false).is_none());
        assert!(cache.select_tablet(&hit, true).is_none());
        assert!(cache.select_tablet(&hit, false).is_none());
    }

    #[test]
    fn select_tablet_random_sampling_over_tied_replicas() {
        let cache = KeyRangeCache::new();
        let mut group = make_group(1, "1", -1);
        // Add 3 replicas all with same distance
        group.tablets[0].distance = 1;
        group.tablets[1].distance = 1;
        group.tablets.push(Tablet {
            tablet_uid: 3,
            server_address: "localhost:8003".to_string(),
            location: "us-central1".to_string(),
            role: Role::ReadWrite,
            incarnation: Bytes::from_static(b"1"),
            distance: 1,
            skip: false,
            _unknown_fields: Default::default(),
        });

        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 1, "1")],
            group: vec![group],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let hit = cache
            .find_range(b"m", b"", RangeMode::CoveringSplit)
            .expect("range hit");

        let eligible = cache
            .get_eligible_tablets(&hit, false)
            .expect("eligible replicas");
        assert_eq!(eligible.len(), 3);
        let candidate_uids: HashSet<u64> =
            eligible.iter().map(|tablet| tablet.tablet_uid).collect();
        assert_eq!(candidate_uids, HashSet::from([1, 2, 3]));

        let selected = cache
            .select_tablet(&hit, false)
            .expect("should select tablet");
        assert!(
            candidate_uids.contains(&selected.tablet_uid),
            "selected tablet UID {} must be among candidate pool {:?}",
            selected.tablet_uid,
            candidate_uids
        );
    }

    #[test]
    fn deterministic_random_crc32c() {
        let non_deterministic_cache = KeyRangeCache::new();
        let default_selection =
            non_deterministic_cache.uniform_random(10, b"start_key", b"limit_key", b"current_key");
        assert!(
            default_selection < 10,
            "uniform_random with standard rand must produce index within bounds"
        );

        let cache = KeyRangeCache::new();
        cache.use_deterministic_random();

        let selection1 = cache.uniform_random(10, b"start_key", b"limit_key", b"current_key");
        let selection2 = cache.uniform_random(10, b"start_key", b"limit_key", b"current_key");
        assert_eq!(
            selection1, selection2,
            "deterministic random must produce identical results for identical seeds"
        );

        let different_selection =
            cache.uniform_random(10, b"other_start", b"limit_key", b"current_key");
        // Verify output is within [0, range_bound)
        assert!(selection1 < 10);
        assert!(different_selection < 10);
    }

    #[test]
    fn leader_distance_filtering() {
        let cache = KeyRangeCache::new();
        let mut group = make_group(10, "1", 1);
        // Leader tablet (index 1) is remote with distance 6 (> MAX_LOCAL_REPLICA_DISTANCE = 5)
        group.tablets[1].distance = 6;
        // Follower tablet (index 0) is local with distance 5
        group.tablets[0].distance = 5;

        let update = CacheUpdate {
            database_id: 1,
            range: vec![make_range("a", "z", 10, "1")],
            group: vec![group],
            key_recipes: None,
            _unknown_fields: Default::default(),
        };
        cache.add_ranges(&update);

        let cached_range = cache
            .find_range(b"m", b"", RangeMode::CoveringSplit)
            .expect("cached range must exist");

        // When prefer_leader is true, but leader distance > 5, it should fall back to the local replica (distance 5)
        let selected_tablet = cache
            .select_tablet(&cached_range, true)
            .expect("a routable tablet must be selected");
        assert_eq!(
            selected_tablet.tablet_uid, 1,
            "must fall back to local replica (UID 1) because leader (UID 2) distance is > 5"
        );
    }
}

#[cfg(test)]
mod golden_tests;