azure_data_cosmos 0.32.0

Rust wrappers around Microsoft Azure REST APIs - Azure Cosmos DB
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
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------

use azure_core::time::Duration;
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicI32, Ordering};
use std::sync::{Arc, Mutex, RwLock, Weak};
use std::time::Instant;

use crate::background_task_manager::BackgroundTaskManager;
use crate::constants;
use crate::cosmos_request::CosmosRequest;
use crate::operation_context::OperationType;
use crate::resource_context::ResourceType;
use crate::routing::global_endpoint_manager::GlobalEndpointManager;
use crate::routing::partition_key_range::PartitionKeyRange;
use tracing::info;
use url::Url;

/// Default duration (in seconds) a partition must remain marked unavailable before
/// the background failback loop considers it eligible for health re-evaluation.
const DEFAULT_ALLOWED_PARTITION_UNAVAILABILITY_DURATION_SECS: i64 = 5;

/// Default interval (in seconds) at which the background failback loop runs to check
/// whether previously failed partitions can be restored to healthy status.
const DEFAULT_STALE_PARTITION_UNAVAILABILITY_REFRESH_INTERVAL_SECS: i64 = 300;

/// Default threshold of consecutive read failures before the circuit breaker trips.
const DEFAULT_CIRCUIT_BREAKER_CONSECUTIVE_FAILURE_COUNT_FOR_READS: i32 = 2;

/// Default threshold of consecutive write failures before the circuit breaker trips.
const DEFAULT_CIRCUIT_BREAKER_CONSECUTIVE_FAILURE_COUNT_FOR_WRITES: i32 = 5;

/// Default window (in minutes) after which the circuit breaker resets its failure
/// counters if no new failure has been recorded.
const DEFAULT_CIRCUIT_BREAKER_TIMEOUT_COUNTER_RESET_WINDOW_MINS: i64 = 5;

/// Represents the health status of a transport address.
/// The numeric values indicate priority for partition selection (lower = healthier).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum PartitionHealthStatus {
    /// The partition is marked as healthy.
    Healthy = 100,

    /// The partition is confirmed unhealthy.
    Unhealthy = 200,
}

/// This struct is used to failover single partitions to different regions.
/// The client retry policy will mark a partition as down. The PartitionKeyRangeToLocationForReadAndWrite
/// will add an override to the next read region. When the request is retried it will
/// override the default location with the new region from the PartitionKeyRangeToLocationForReadAndWrite.
#[derive(Debug)]
pub struct GlobalPartitionEndpointManager {
    /// An instance of GlobalEndpointManager.
    global_endpoint_manager: Arc<GlobalEndpointManager>,

    /// Partition unavailability duration in seconds, before it can be considered for a refresh.
    partition_unavailability_duration_secs: i64,

    /// Partition failback refresh interval in seconds. Default is 5 minutes.
    background_connection_init_interval_secs: i64,

    /// Partition key range to failover info mapping for writes in a single master account.
    partition_key_range_to_location_for_write:
        Arc<RwLock<HashMap<PartitionKeyRange, PartitionKeyRangeFailoverInfo>>>,

    /// Partition key range to failover info mapping for reads in single master,
    /// and both reads and writes in multi master account.
    partition_key_range_to_location_for_read_and_write:
        Arc<RwLock<HashMap<PartitionKeyRange, PartitionKeyRangeFailoverInfo>>>,

    /// Flag indicating if the background connection initialization task is active.
    background_connection_init_active: AtomicBool,

    /// Flag to determine if partition level failover is enabled.
    partition_level_automatic_failover_enabled: AtomicBool,

    /// Flag to determine if partition level circuit breaker is enabled.
    partition_level_circuit_breaker_enabled: AtomicBool,

    /// Manages background tasks and signals them to stop when dropped.
    background_task_manager: BackgroundTaskManager,
}

impl GlobalPartitionEndpointManager {
    /// Creates a new instance of [`GlobalPartitionEndpointManager`].
    ///
    /// Initializes partition-level failover maps, reads environment variable overrides
    /// for partition unavailability duration and refresh intervals, and spawns the
    /// background circuit-breaker failback loop if partition-level failover is enabled.
    ///
    /// # Arguments
    /// * `global_endpoint_manager` - Shared reference to the [`GlobalEndpointManager`]
    ///   used for resolving read/write endpoints and multi-write support.
    /// * `partition_level_failover_enabled` - Whether per-partition automatic failover
    ///   (for single-master write accounts) should be enabled.
    /// * `partition_level_circuit_breaker_enabled` - Whether the partition-level circuit
    ///   breaker (for multi-master accounts and reads) should be enabled.
    ///
    /// # Returns
    /// An `Arc<Self>` that can be shared across threads and async tasks.
    pub fn new(
        global_endpoint_manager: Arc<GlobalEndpointManager>,
        partition_level_failover_enabled: bool,
        partition_level_circuit_breaker_enabled: bool,
    ) -> Arc<Self> {
        let instance = Arc::new(Self {
            global_endpoint_manager,
            partition_unavailability_duration_secs:
                Self::allowed_partition_unavailability_duration_secs(
                    DEFAULT_ALLOWED_PARTITION_UNAVAILABILITY_DURATION_SECS,
                ),
            background_connection_init_interval_secs:
                Self::stale_partition_unavailability_refresh_interval_secs(
                    DEFAULT_STALE_PARTITION_UNAVAILABILITY_REFRESH_INTERVAL_SECS,
                ),
            partition_key_range_to_location_for_write: Arc::new(RwLock::new(HashMap::new())),
            partition_key_range_to_location_for_read_and_write: Arc::new(RwLock::new(
                HashMap::new(),
            )),
            background_connection_init_active: AtomicBool::new(false),
            partition_level_automatic_failover_enabled: AtomicBool::new(
                partition_level_failover_enabled,
            ),
            partition_level_circuit_breaker_enabled: AtomicBool::new(
                partition_level_circuit_breaker_enabled,
            ),
            background_task_manager: BackgroundTaskManager::new(),
        });

        instance.initialize_and_start_circuit_breaker_failback_background_refresh();
        instance
    }

    /// Returns the allowed partition unavailability duration in seconds.
    ///
    /// This value controls how long a partition must remain marked unavailable before
    /// the background failback loop considers it eligible for health re-evaluation.
    /// Reads from the `AZURE_COSMOS_ALLOWED_PARTITION_UNAVAILABILITY_DURATION_IN_SECONDS`
    /// environment variable, falling back to `default` if the variable is unset or not parseable.
    fn allowed_partition_unavailability_duration_secs(default: i64) -> i64 {
        std::env::var(constants::AZURE_COSMOS_ALLOWED_PARTITION_UNAVAILABILITY_DURATION_IN_SECONDS)
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(default)
    }

    /// Returns the stale partition unavailability refresh interval in seconds.
    ///
    /// This determines how frequently the background failback loop runs to check
    /// whether previously failed partitions can be restored to healthy status.
    /// Reads from the `AZURE_COSMOS_PPCB_STALE_PARTITION_UNAVAILABILITY_REFRESH_INTERVAL_IN_SECONDS`
    /// environment variable, falling back to `default` if the variable is unset or not parseable.
    fn stale_partition_unavailability_refresh_interval_secs(default: i64) -> i64 {
        std::env::var(
            constants::AZURE_COSMOS_PPCB_STALE_PARTITION_UNAVAILABILITY_REFRESH_INTERVAL_IN_SECONDS,
        )
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(default)
    }

    /// Initializes and starts the background circuit-breaker failback periodic refresh task.
    ///
    /// Uses an atomic `compare_exchange` on [`background_connection_init_active`] to ensure
    /// only one background task is ever spawned, regardless of how many times this method
    /// is called. The spawned task runs [`initiate_circuit_breaker_failback_loop`] indefinitely
    /// to periodically re-evaluate whether failed partitions can be marked healthy again.
    fn initialize_and_start_circuit_breaker_failback_background_refresh(self: &Arc<Self>) {
        // Atomically try to set from false to true.
        // If it was already true, another thread already started the task.
        if self
            .background_connection_init_active
            .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
            .is_err()
        {
            return;
        }

        let weak_self = Arc::downgrade(self);
        // Spawn via BackgroundTaskManager so the task is tracked and will be
        // canceled when the manager (and thus the client) is dropped.
        // We capture a Weak<Self> (not Arc<Self>) to avoid a reference cycle
        // that would prevent the GlobalPartitionEndpointManager from ever
        // being dropped.
        self.background_task_manager.spawn(Box::pin(async move {
            Self::initiate_circuit_breaker_failback_loop(weak_self).await;
        }));
    }

    /// Runs a loop that periodically attempts to fail back partitions to their
    /// original (previously failed) endpoints.
    ///
    /// On each iteration the loop sleeps for [`background_connection_init_interval_secs`]
    /// seconds and then calls [`initiate_failback_to_unhealthy_endpoints`]. Any errors
    /// during the failback attempt are logged but do not terminate the loop.
    ///
    /// The loop exits when `weak_self.upgrade()` returns `None` (all strong
    /// `Arc` references are gone), which happens when the owning client is
    /// dropped. Dropping the client drops the [`BackgroundTaskManager`], which
    /// drops the stored future, cancelling this task.
    async fn initiate_circuit_breaker_failback_loop(weak_self: Weak<Self>) {
        // Briefly upgrade to read the interval, then release the strong ref
        // so it does not keep Self alive across the sleep.
        let interval = match weak_self.upgrade() {
            Some(strong) => Duration::seconds(strong.background_connection_init_interval_secs),
            None => return,
        };

        loop {
            // Use the runtime-agnostic sleep from azure_core
            azure_core::async_runtime::get_async_runtime()
                .sleep(interval)
                .await;

            // Upgrade the Weak ref for this iteration only. If it fails, the
            // manager has been dropped and we should exit.
            let strong = match weak_self.upgrade() {
                Some(s) => s,
                None => {
                    info!("GlobalPartitionEndpointManager: background failback loop exiting because the client has been dropped.");
                    return;
                }
            };

            info!("GlobalPartitionEndpointManager: initiate_circuit_breaker_failback_loop() un-deterministically marking the failed partitions back to healthy.");

            if let Err(e) = strong.initiate_failback_to_unhealthy_endpoints().await {
                tracing::error!("GlobalPartitionEndpointManager: initiate_circuit_breaker_failback_loop() - failed to mark the failed partitions back to healthy. Exception: {}", e);
            }
            // `strong` is dropped here, releasing the temporary strong ref
            // before the next sleep.
        }
    }

    /// Attempts to initiate failback to previously failed endpoints non-deterministically.
    ///
    /// Scans `partition_key_range_to_location_for_read_and_write` for partitions whose
    /// first failure occurred more than [`partition_unavailability_duration_secs`] ago.
    /// Eligible partitions are marked healthy via [`mark_endpoints_to_healthy`], and their
    /// override entries are removed, allowing future requests to be routed back to the
    /// original endpoint.
    ///
    /// # Errors
    /// Returns an error if the read lock on the partition map is poisoned.
    async fn initiate_failback_to_unhealthy_endpoints(
        &self,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        info!("GlobalPartitionEndpointManager: initiate_circuit_breaker_failback_loop() - Attempting to mark the failed partitions back to healthy and initiate failback.");

        let mut pk_range_to_endpoint_mappings: HashMap<
            PartitionKeyRange,
            (String, String, PartitionHealthStatus),
        > = HashMap::new();

        // Scope the guard so it's dropped before any async operations
        {
            let guard = self
                .partition_key_range_to_location_for_read_and_write
                .read()
                .map_err(|e| e.to_string())?;
            for (pk_range, partition_failover) in guard.iter() {
                let pk_range = pk_range.clone();

                let (first_request_failure_time, _) =
                    partition_failover.snapshot_partition_failover_timestamps();

                if Instant::now().duration_since(first_request_failure_time)
                    > Duration::seconds(self.partition_unavailability_duration_secs)
                {
                    let original_failed_location = partition_failover.first_failed_location.clone();

                    pk_range_to_endpoint_mappings.insert(
                        pk_range,
                        (
                            partition_failover.collection_rid.clone(),
                            original_failed_location,
                            PartitionHealthStatus::Unhealthy,
                        ),
                    );
                }
            }
        } // guard is dropped here

        if !pk_range_to_endpoint_mappings.is_empty() {
            // Mark endpoints as healthy directly
            Self::mark_endpoints_to_healthy(&mut pk_range_to_endpoint_mappings);

            for (pk_range, (_, original_failed_location, current_health_state)) in
                pk_range_to_endpoint_mappings
            {
                if current_health_state == PartitionHealthStatus::Healthy {
                    info!(
                        "Initiating failback to endpoint: {}, for partition key range: {:?}",
                        original_failed_location, pk_range
                    );
                    self.partition_key_range_to_location_for_read_and_write
                        .write()
                        .unwrap()
                        .remove(&pk_range);
                }
            }
        }

        Ok(())
    }

    /// Marks all partition key range endpoint mappings in the provided map as [`PartitionHealthStatus::Healthy`].
    ///
    /// This is a non-deterministic health restoration: once the unavailability window
    /// has elapsed, the endpoint is optimistically assumed to be healthy again.
    /// The actual verification happens when subsequent requests are routed back to
    /// the original endpoint.
    ///
    /// # Arguments
    /// * `pk_range_uri_mappings` - Mutable reference to the map of partition key ranges
    ///   to their `(collection_rid, original_failed_location, health_status)` tuples.
    fn mark_endpoints_to_healthy(
        pk_range_uri_mappings: &mut HashMap<
            PartitionKeyRange,
            (String, String, PartitionHealthStatus),
        >,
    ) {
        for (pk_range, mapping) in pk_range_uri_mappings.iter_mut() {
            info!(
                "Un-deterministically marking the original failed endpoint: {}, for the PkRange: {}, collectionRid: {} back to healthy.",
                mapping.1,
                pk_range.id,
                mapping.0
            );

            mapping.2 = PartitionHealthStatus::Healthy;
        }
    }

    /// Determines if a request is eligible for per-partition automatic failover.
    ///
    /// A request qualifies when **all** of the following are true:
    /// 1. Partition-level automatic failover is enabled.
    /// 2. The request is a **write** operation (not read-only).
    /// 3. The account is a **single-master** account (does not support multiple write locations).
    ///
    /// This path handles failover of write requests on single-master accounts to read regions.
    ///
    /// # Arguments
    /// * `request` - The Cosmos request to evaluate.
    ///
    /// # Returns
    /// `true` if the request is eligible for per-partition automatic failover, `false` otherwise.
    pub fn is_request_eligible_for_per_partition_automatic_failover(
        &self,
        request: &CosmosRequest,
    ) -> bool {
        self.partition_level_automatic_failover_enabled
            .load(Ordering::SeqCst)
            && !request.is_read_only_request()
            && !self
                .global_endpoint_manager
                .can_support_multiple_write_locations(request.resource_type, request.operation_type)
    }

    /// Determines if a request is eligible for partition-level circuit breaker.
    ///
    /// A request qualifies when **all** of the following are true:
    /// 1. Partition-level circuit breaker is enabled.
    /// 2. The request is **either** a read-only operation, **or** a write operation on a
    ///    **multi-master** account that supports multiple write locations.
    ///
    /// This path handles failover of reads (any account type) and writes on multi-master
    /// accounts to alternate preferred read regions.
    ///
    /// # Arguments
    /// * `request` - The Cosmos request to evaluate.
    ///
    /// # Returns
    /// `true` if the request is eligible for partition-level circuit breaker, `false` otherwise.
    pub fn is_request_eligible_for_partition_level_circuit_breaker(
        &self,
        request: &CosmosRequest,
    ) -> bool {
        self.partition_level_circuit_breaker_enabled
            .load(Ordering::SeqCst)
            && (request.resource_type == ResourceType::Documents
                || (request.resource_type == ResourceType::StoredProcedures
                    && request.operation_type == OperationType::Execute))
            && (request.is_read_only_request()
                || (!request.is_read_only_request()
                    && self
                        .global_endpoint_manager
                        .can_support_multiple_write_locations(
                            request.resource_type,
                            request.operation_type,
                        )))
    }

    /// Validates whether the given request is eligible for any form of partition-level failover.
    ///
    /// Performs the following checks:
    /// 1. At least one partition-level failover mode (automatic failover or circuit breaker)
    ///    must be enabled.
    /// 2. The request must target a resource type that supports partition-level failover
    ///    (see [`can_use_partition_level_failover_locations`]).
    /// 3. A resolved partition key range must exist on the request context.
    /// 4. If `should_validate_failed_location` is `true`, a valid failed location endpoint
    ///    must be present on the request context.
    ///
    /// # Arguments
    /// * `request` - The Cosmos request to validate.
    /// * `should_validate_failed_location` - When `true`, the method also extracts and
    ///   returns the failed location URL from the request context.
    ///
    /// # Returns
    /// `Some((partition_key_range, optional_failed_location))` if eligible, `None` otherwise.
    fn is_request_eligible_for_partition_failover(
        &self,
        request: &CosmosRequest,
        should_validate_failed_location: bool,
    ) -> Option<(PartitionKeyRange, Option<Url>)> {
        if !self.partition_level_automatic_failover_enabled()
            && !self.partition_level_circuit_breaker_enabled()
        {
            return None;
        }

        let request_context = &request.request_context;

        if !self.can_use_partition_level_failover_locations(request) {
            return None;
        }

        let partition_key_range = request_context.resolved_partition_key_range.clone()?;

        let failed_location = if should_validate_failed_location {
            let location = request_context.location_endpoint_to_route.clone()?;
            Some(location)
        } else {
            None
        };

        Some((partition_key_range, failed_location))
    }

    /// Determines if partition-level failover locations can be applied to the given request.
    ///
    /// Partition-level failover only makes sense when there are multiple read endpoints
    /// available to fail over to. The request must also target one of the supported
    /// resource types:
    /// - [`ResourceType::Documents`] (CRUD on items)
    /// - [`ResourceType::StoredProcedures`] with [`OperationType::Execute`]
    ///
    /// # Arguments
    /// * `request` - The Cosmos request to check.
    ///
    /// # Returns
    /// `true` if the request can leverage partition-level failover locations, `false` otherwise.
    fn can_use_partition_level_failover_locations(&self, request: &CosmosRequest) -> bool {
        if self.global_endpoint_manager.read_endpoints().len() <= 1 {
            return false;
        }

        matches!(request.resource_type, ResourceType::Documents)
            || (request.resource_type == ResourceType::StoredProcedures
                && request.operation_type == OperationType::Execute)
    }

    /// Attempts to route the request to a partition-level override location if one exists.
    ///
    /// Looks up the partition key range in `partition_key_range_to_location_mapping`. If an
    /// override entry is found:
    /// - For circuit-breaker eligible requests, it additionally verifies that the failure
    ///   counters have exceeded the threshold before applying the override.
    /// - Updates [`request.request_context`] to route to the overridden location.
    ///
    /// # Arguments
    /// * `partition_key_range` - The partition key range to look up.
    /// * `request` - The Cosmos request whose routing will be overridden.
    /// * `partition_key_range_to_location_mapping` - The mapping to consult for override info.
    ///
    /// # Returns
    /// `true` if the request was successfully routed to an override location, `false` otherwise.
    fn try_route_request_for_partition_level_override(
        &self,
        partition_key_range: &PartitionKeyRange,
        request: &mut CosmosRequest,
        partition_key_range_to_location_mapping: &Arc<
            RwLock<HashMap<PartitionKeyRange, PartitionKeyRangeFailoverInfo>>,
        >,
    ) -> bool {
        if let Some(partition_key_range_failover) = partition_key_range_to_location_mapping
            .read()
            .unwrap()
            .get(partition_key_range)
        {
            if self.is_request_eligible_for_partition_level_circuit_breaker(request)
                && !partition_key_range_failover
                    .can_circuit_breaker_trigger_partition_failover(request.is_read_only_request())
            {
                return false;
            }

            // TODO - Move this to new type and capture in DiagnosticsContext when porting to driver
            let triggered_by = if self
                .partition_level_automatic_failover_enabled
                .load(Ordering::SeqCst)
            {
                "Automatic Failover"
            } else {
                "Circuit Breaker"
            };

            info!(
                "Attempting to route request for partition level override triggered by {}, for operation type: {:?}. URI: {}, PartitionKeyRange: {:?}",
                triggered_by,
                request.operation_type,
                partition_key_range_failover.current,
                partition_key_range.id
            );

            if let Ok(endpoint) = partition_key_range_failover.current.parse() {
                request.request_context.route_to_location_endpoint(endpoint);

                return true;
            } else {
                info!(
                    "Skipping partition level override due to invalid URI in failover info: {}",
                    partition_key_range_failover.current
                );
            }
        }

        false
    }

    /// Returns whether partition level automatic failover is enabled.
    pub fn partition_level_automatic_failover_enabled(&self) -> bool {
        self.partition_level_automatic_failover_enabled
            .load(Ordering::SeqCst)
    }

    /// Returns whether partition level circuit breaker is enabled.
    pub fn partition_level_circuit_breaker_enabled(&self) -> bool {
        self.partition_level_circuit_breaker_enabled
            .load(Ordering::SeqCst)
    }

    /// Checks if partition level failover is enabled.
    ///
    /// Returns `true` if either partition level circuit breaker or partition level
    /// automatic failover is enabled.
    pub fn partition_level_failover_enabled(&self) -> bool {
        self.partition_level_circuit_breaker_enabled()
            || self.partition_level_automatic_failover_enabled()
    }

    /// Attempts to apply a partition-level location override to the request before it is sent.
    ///
    /// This is the main entry point called by the retry pipeline to check whether an
    /// existing partition failover override exists for the request's partition key range.
    /// Depending on the request's eligibility, it delegates to
    /// [`try_route_request_for_partition_level_override`] using either:
    /// - `partition_key_range_to_location_for_read_and_write` (circuit breaker path), or
    /// - `partition_key_range_to_location_for_write` (automatic failover path).
    ///
    /// # Arguments
    /// * `request` - The mutable Cosmos request whose routing may be overridden.
    ///
    /// # Returns
    /// `true` if an override was applied and the request will be routed to an alternate
    /// location, `false` if no override exists or the request is ineligible.
    pub fn try_add_partition_level_location_override(&self, request: &mut CosmosRequest) -> bool {
        let Some((partition_key_range, _)) =
            self.is_request_eligible_for_partition_failover(request, false)
        else {
            return false;
        };

        if self.is_request_eligible_for_partition_level_circuit_breaker(request) {
            return self.try_route_request_for_partition_level_override(
                &partition_key_range,
                request,
                &self.partition_key_range_to_location_for_read_and_write,
            );
        } else if self.is_request_eligible_for_per_partition_automatic_failover(request) {
            return self.try_route_request_for_partition_level_override(
                &partition_key_range,
                request,
                &self.partition_key_range_to_location_for_write,
            );
        }

        false
    }

    /// Marks the current location unavailable for write. Future requests will be routed
    /// to the next location if available.
    ///
    /// # Arguments
    /// * `request` - The document service request to process.
    ///
    /// # Returns
    /// `true` if the endpoint was successfully marked as unavailable and a new location was set,
    /// `false` otherwise.
    pub fn try_mark_endpoint_unavailable_for_partition_key_range(
        &self,
        request: &CosmosRequest,
    ) -> bool {
        // Validate request eligibility and extract partition key range and failed location
        let Some((partition_key_range, failed_location)) =
            self.is_request_eligible_for_partition_failover(request, true)
        else {
            return false;
        };

        // Ensure we have a valid failed location (required when shouldValidateFailedLocation is true)
        let Some(failed_location) = failed_location else {
            return false;
        };

        let failed_location_str = failed_location.as_str();

        if self.is_request_eligible_for_partition_level_circuit_breaker(request) {
            // For multi master write accounts, since all the regions are treated as write regions,
            // the next locations to fail over will be the preferred read regions that are configured
            // in the application preferred regions in the CosmosClientOptions.
            let next_locations: Vec<String> = self
                .global_endpoint_manager
                .read_endpoints()
                .iter()
                .map(|u| u.to_string())
                .collect();

            return self.try_add_or_update_partition_failover_info_and_move_to_next_location(
                &partition_key_range,
                failed_location_str,
                &next_locations,
                request,
                &self.partition_key_range_to_location_for_read_and_write,
            );
        } else if self.is_request_eligible_for_per_partition_automatic_failover(request) {
            // For any single master write accounts, the next locations to fail over will be
            // the read regions configured at the account level.
            let next_locations: Vec<String> = self
                .global_endpoint_manager
                .account_read_endpoints()
                .iter()
                .map(|u| u.to_string())
                .collect();

            return self.try_add_or_update_partition_failover_info_and_move_to_next_location(
                &partition_key_range,
                failed_location_str,
                &next_locations,
                request,
                &self.partition_key_range_to_location_for_write,
            );
        }

        tracing::info!(
        "Partition level override was skipped since the request did not meet the minimum requirements."
    );
        false
    }

    /// Attempts to add or update the partition failover information and move to the next available location.
    ///
    /// This method checks if the current location for the partition key range has failed and updates
    /// the failover information to route the request to the next available location. If all locations
    /// have been tried, it removes the failover information for the partition key range.
    ///
    /// # Arguments
    /// * `partition_key_range` - The partition key range for which the failover information is being updated.
    /// * `failed_location` - The URI of the failed location.
    /// * `next_locations` - A slice of URIs representing the next available locations.
    /// * `request` - The document service request being routed.
    /// * `partition_key_range_to_location_mapping` - The mapping of partition key ranges to their failover information.
    ///
    /// # Returns
    /// `true` if the failover information was successfully updated and the request was routed to a new location,
    /// `false` otherwise.
    fn try_add_or_update_partition_failover_info_and_move_to_next_location(
        &self,
        partition_key_range: &PartitionKeyRange,
        failed_location: &str,
        next_locations: &[String],
        request: &CosmosRequest,
        partition_key_range_to_location_mapping: &Arc<
            RwLock<HashMap<PartitionKeyRange, PartitionKeyRangeFailoverInfo>>,
        >,
    ) -> bool {
        if request.request_context.resolved_collection_rid.is_none() {
            return false;
        }

        let triggered_by = if self
            .partition_level_automatic_failover_enabled
            .load(Ordering::SeqCst)
        {
            "Automatic Failover"
        } else {
            "Circuit Breaker"
        };

        // Get the resolved collection RID from the request context, if available
        let collection_rid = request
            .request_context
            .resolved_collection_rid
            .clone()
            .unwrap();

        // Get or insert the partition failover info and try to move to next location
        let mut guard = partition_key_range_to_location_mapping.write().unwrap();
        let partition_failover = guard.entry(partition_key_range.clone()).or_insert_with(|| {
            PartitionKeyRangeFailoverInfo::new(collection_rid, failed_location.to_string())
        });

        // Will return true if it was able to update to a new region
        if partition_failover.try_move_next_location(next_locations, failed_location) {
            tracing::info!(
                "Partition level override triggered by {}, added to new location for {:?}. \
             PartitionKeyRange: {:?}, failedLocation: {}, new location: {}",
                triggered_by,
                request.operation_type,
                partition_key_range,
                failed_location,
                partition_failover.current
            );

            return true;
        }

        // All the locations have been tried. Remove the override information
        tracing::info!(
        "Partition level override removed for {:?}. PartitionKeyRange: {:?}, failedLocation: {}",
        request.operation_type,
        partition_key_range,
        failed_location
    );

        // Remove while still holding the write guard (no need to drop and re-acquire)
        guard.remove(partition_key_range);

        false
    }

    /// Increments the request failure counter for the partition and checks whether the
    /// circuit breaker threshold has been exceeded to trigger partition-level failover.
    ///
    /// This method is called after a request to a specific partition key range fails.
    /// It records the failure in the appropriate failover info map (write-only for
    /// automatic failover, read-and-write for circuit breaker) and then evaluates
    /// whether the consecutive failure count has crossed the configured threshold.
    ///
    /// # Arguments
    /// * `request` - The failed Cosmos request, used to extract the partition key range,
    ///   collection RID, failed location, and operation type.
    ///
    /// # Returns
    /// `true` if the failure counters indicate the partition should be failed over
    /// to an alternate region, `false` otherwise.
    pub(crate) fn increment_request_failure_counter_and_check_if_partition_can_failover(
        &self,
        request: &CosmosRequest,
    ) -> bool {
        let Some((partition_key_range, Some(failed_location))) =
            self.is_request_eligible_for_partition_failover(request, true)
        else {
            return false;
        };

        if request.request_context.resolved_collection_rid.is_none() {
            return false;
        }

        let collection_rid = request
            .request_context
            .resolved_collection_rid
            .clone()
            .unwrap();

        let is_read_only = request.is_read_only_request();

        if self.is_request_eligible_for_per_partition_automatic_failover(request) {
            let mut guard = self
                .partition_key_range_to_location_for_write
                .write()
                .unwrap();
            let partition_failover = guard.entry(partition_key_range).or_insert_with(|| {
                PartitionKeyRangeFailoverInfo::new(
                    collection_rid.clone(),
                    failed_location.to_string(),
                )
            });
            partition_failover.increment_request_failure_counts(is_read_only, Instant::now());
            partition_failover.can_circuit_breaker_trigger_partition_failover(is_read_only)
        } else {
            let mut guard = self
                .partition_key_range_to_location_for_read_and_write
                .write()
                .unwrap();
            let partition_failover = guard.entry(partition_key_range).or_insert_with(|| {
                PartitionKeyRangeFailoverInfo::new(collection_rid, failed_location.to_string())
            });
            partition_failover.increment_request_failure_counts(is_read_only, Instant::now());
            partition_failover.can_circuit_breaker_trigger_partition_failover(is_read_only)
        }
    }

    /// Sets whether per partition automatic failover is enabled.
    ///
    /// Only logs when the value actually changes to avoid noisy repeated logs
    /// during periodic account refresh.
    pub fn configure_partition_level_automatic_failover(&self, is_enabled: bool) {
        let previous = self
            .partition_level_automatic_failover_enabled
            .swap(is_enabled, Ordering::SeqCst);
        if previous != is_enabled {
            info!(
                "Per partition automatic failover enablement flag changed: {} -> {}",
                previous, is_enabled
            );
        }
    }

    /// Sets whether per partition circuit breaker is enabled.
    ///
    /// Only logs when the value actually changes to avoid noisy repeated logs
    /// during periodic account refresh.
    pub fn configure_per_partition_circuit_breaker(&self, is_enabled: bool) {
        let previous = self
            .partition_level_circuit_breaker_enabled
            .swap(is_enabled, Ordering::SeqCst);
        if previous != is_enabled {
            info!(
                "Per partition circuit breaker enablement flag changed: {} -> {}",
                previous, is_enabled
            );
        }
    }
}

/// Contains failover tracking information for a single partition key range.
///
/// Each instance tracks which endpoint the partition is currently routed to, which
/// locations have already been tried, and consecutive failure counts for both reads
/// and writes. The circuit breaker uses these counters to decide when to trigger
/// a partition-level failover to the next available region.
#[derive(Debug)]
pub struct PartitionKeyRangeFailoverInfo {
    /// Set of locations that have already been tried and failed, along with the
    /// timestamp when each location was marked as failed. Protected by a [`Mutex`]
    /// because it is mutated during failover transitions.
    failed_locations: Mutex<HashMap<String, Instant>>,

    /// Duration window after which consecutive failure counters are reset to zero.
    /// If the time between the current failure and the last recorded failure exceeds
    /// this window, the counters restart from zero.
    timeout_counter_reset_window: Duration,

    /// The consecutive read failure count threshold that must be exceeded
    /// before the circuit breaker triggers a partition failover for read requests.
    read_request_failure_counter_threshold: i32,

    /// The consecutive write failure count threshold that must be exceeded
    /// before the circuit breaker triggers a partition failover for write requests.
    write_request_failure_counter_threshold: i32,

    /// Timestamp of the most recent request failure for this partition key range.
    /// Protected by [`RwLock`] because it is read frequently but only written on failure.
    last_request_failure_time: RwLock<Instant>,

    /// Running count of consecutive read request failures. Atomic for lock-free
    /// incrementing across concurrent request threads.
    consecutive_read_request_failure_count: AtomicI32,

    /// Running count of consecutive write request failures. Atomic for lock-free
    /// incrementing across concurrent request threads.
    consecutive_write_request_failure_count: AtomicI32,

    /// The endpoint URI that this partition key range is currently routed to.
    pub current: String,

    /// The endpoint URI that originally failed, triggering the failover chain.
    /// Used by the failback loop to know which endpoint to restore.
    pub first_failed_location: String,

    /// The collection resource ID (RID) associated with this partition key range.
    pub collection_rid: String,

    /// Timestamp of the very first request failure that initiated this failover entry.
    /// Used by the failback loop to determine if enough time has passed for health re-evaluation.
    pub first_request_failure_time: Instant,
}

impl PartitionKeyRangeFailoverInfo {
    /// Creates a new [`PartitionKeyRangeFailoverInfo`] for the given collection and location.
    ///
    /// Initializes all failure counters to zero, reads environment variable overrides
    /// for threshold and window configurations, and records the current instant as
    /// both the first and last failure timestamps.
    ///
    /// # Arguments
    /// * `collection_rid` - The resource ID of the collection this partition belongs to.
    /// * `current_location` - The endpoint URI of the location that is currently being
    ///   used (and has just failed).
    pub fn new(collection_rid: String, current_location: String) -> Self {
        Self {
            collection_rid,
            current: current_location.clone(),
            first_failed_location: current_location,
            failed_locations: Mutex::new(HashMap::new()),
            consecutive_read_request_failure_count: AtomicI32::new(0),
            consecutive_write_request_failure_count: AtomicI32::new(0),
            read_request_failure_counter_threshold:
                Self::circuit_breaker_consecutive_failure_count_for_reads(
                    DEFAULT_CIRCUIT_BREAKER_CONSECUTIVE_FAILURE_COUNT_FOR_READS,
                ),
            write_request_failure_counter_threshold:
                Self::circuit_breaker_consecutive_failure_count_for_writes(
                    DEFAULT_CIRCUIT_BREAKER_CONSECUTIVE_FAILURE_COUNT_FOR_WRITES,
                ),
            timeout_counter_reset_window: Duration::seconds(
                Self::circuit_breaker_timeout_counter_reset_window_mins(
                    DEFAULT_CIRCUIT_BREAKER_TIMEOUT_COUNTER_RESET_WINDOW_MINS,
                ) * 60,
            ),
            first_request_failure_time: Instant::now(),
            last_request_failure_time: RwLock::new(Instant::now()),
        }
    }

    /// Returns the consecutive read failure count threshold for the circuit breaker.
    ///
    /// Reads from the `AZURE_COSMOS_CIRCUIT_BREAKER_CONSECUTIVE_FAILURE_COUNT_FOR_READS`
    /// environment variable, falling back to `default` if the variable is unset or not parseable.
    fn circuit_breaker_consecutive_failure_count_for_reads(default: i32) -> i32 {
        std::env::var(constants::AZURE_COSMOS_CIRCUIT_BREAKER_CONSECUTIVE_FAILURE_COUNT_FOR_READS)
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(default)
    }

    /// Returns the consecutive write failure count threshold for the circuit breaker.
    ///
    /// Reads from the `AZURE_COSMOS_CIRCUIT_BREAKER_CONSECUTIVE_FAILURE_COUNT_FOR_WRITES`
    /// environment variable, falling back to `default` if the variable is unset or not parseable.
    fn circuit_breaker_consecutive_failure_count_for_writes(default: i32) -> i32 {
        std::env::var(constants::AZURE_COSMOS_CIRCUIT_BREAKER_CONSECUTIVE_FAILURE_COUNT_FOR_WRITES)
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(default)
    }

    /// Returns the timeout counter reset window in minutes for the circuit breaker.
    ///
    /// If the elapsed time between two consecutive failures exceeds this window,
    /// the read and write failure counters are reset to zero. Reads from the
    /// `AZURE_COSMOS_CIRCUIT_BREAKER_TIMEOUT_COUNTER_RESET_WINDOW_IN_MINUTES`
    /// environment variable, falling back to `default` if unset or not parseable.
    fn circuit_breaker_timeout_counter_reset_window_mins(default: i64) -> i64 {
        std::env::var(
            constants::AZURE_COSMOS_CIRCUIT_BREAKER_TIMEOUT_COUNTER_RESET_WINDOW_IN_MINUTES,
        )
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(default)
    }

    /// Attempts to move this partition's routing to the next available location.
    ///
    /// If `failed_location` no longer matches `self.current` (another thread already
    /// moved it), returns `true` immediately. Otherwise, iterates through `locations`
    /// and picks the first one that:
    /// - Is not the current location.
    /// - Has not already been tried (not in `failed_locations`).
    ///
    /// Records `failed_location` in the `failed_locations` set and updates `self.current`
    /// to the new location.
    ///
    /// # Arguments
    /// * `locations` - Ordered list of candidate endpoint URIs to try.
    /// * `failed_location` - The endpoint URI that just failed.
    ///
    /// # Returns
    /// `true` if a new location was selected (or someone else already moved), `false` if
    /// all locations have been exhausted.
    pub fn try_move_next_location(&mut self, locations: &[String], failed_location: &str) -> bool {
        if failed_location != self.current {
            return true;
        }

        let mut guard = self.failed_locations.lock().unwrap();

        if failed_location != self.current {
            return true;
        }

        for location in locations {
            if self.current == *location {
                continue;
            }

            if guard.contains_key(location) {
                continue;
            }

            guard.insert(failed_location.to_string(), Instant::now());
            self.current = location.clone();
            return true;
        }

        false
    }

    /// Checks whether the circuit breaker should trigger a partition-level failover.
    ///
    /// Compares the current consecutive failure counts against the configured thresholds.
    /// For read-only requests, checks the read counter; for write requests, checks the
    /// write counter.
    ///
    /// # Arguments
    /// * `is_read_only_request` - Whether the triggering request is read-only.
    ///
    /// # Returns
    /// `true` if the consecutive failure count exceeds the threshold, indicating that
    /// a failover should be triggered, `false` otherwise.
    pub fn can_circuit_breaker_trigger_partition_failover(
        &self,
        is_read_only_request: bool,
    ) -> bool {
        let (read_count, write_count) = self.snapshot_consecutive_request_failure_count();

        if is_read_only_request {
            read_count > self.read_request_failure_counter_threshold
        } else {
            write_count > self.write_request_failure_counter_threshold
        }
    }

    /// Increments the consecutive request failure counter for this partition.
    ///
    /// If the time since the last recorded failure exceeds [`timeout_counter_reset_window`],
    /// both read and write counters are reset to zero before incrementing. This prevents
    /// stale failures from accumulating across long idle periods.
    ///
    /// # Arguments
    /// * `is_read_only_request` - Whether the failed request was read-only. Determines
    ///   which counter (read or write) is incremented.
    /// * `current_time` - The timestamp of the current failure.
    pub fn increment_request_failure_counts(
        &self,
        is_read_only_request: bool,
        current_time: Instant,
    ) {
        let (_, last_failure_time) = self.snapshot_partition_failover_timestamps();

        if current_time.duration_since(last_failure_time) > self.timeout_counter_reset_window {
            self.consecutive_read_request_failure_count
                .store(0, Ordering::SeqCst);
            self.consecutive_write_request_failure_count
                .store(0, Ordering::SeqCst);
        }

        if is_read_only_request {
            self.consecutive_read_request_failure_count
                .fetch_add(1, Ordering::SeqCst);
        } else {
            self.consecutive_write_request_failure_count
                .fetch_add(1, Ordering::SeqCst);
        }

        *self.last_request_failure_time.write().unwrap() = current_time;
    }

    /// Returns a snapshot of the partition failover timestamps.
    ///
    /// # Returns
    /// A tuple of `(first_request_failure_time, last_request_failure_time)` capturing
    /// when the first and most recent failures occurred for this partition key range.
    pub fn snapshot_partition_failover_timestamps(&self) -> (Instant, Instant) {
        (
            self.first_request_failure_time,
            *self.last_request_failure_time.read().unwrap(),
        )
    }

    /// Returns a snapshot of the consecutive request failure counters.
    ///
    /// # Returns
    /// A tuple of `(read_failure_count, write_failure_count)` representing the current
    /// consecutive failure counts for read and write requests respectively.
    pub fn snapshot_consecutive_request_failure_count(&self) -> (i32, i32) {
        (
            self.consecutive_read_request_failure_count
                .load(Ordering::SeqCst),
            self.consecutive_write_request_failure_count
                .load(Ordering::SeqCst),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cosmos_request::CosmosRequest;
    use crate::models::AccountRegion;
    use crate::operation_context::OperationType;
    use crate::regions::Region;
    use crate::resource_context::{ResourceLink, ResourceType};
    use crate::routing::global_endpoint_manager::GlobalEndpointManager;
    use crate::routing::partition_key_range::PartitionKeyRange;
    use azure_core::http::Pipeline;
    use std::sync::Arc;
    use std::time::Instant;

    // -----------------------------------------------------------------------
    // Helper functions
    // -----------------------------------------------------------------------

    fn create_test_pipeline() -> Pipeline {
        Pipeline::new(
            option_env!("CARGO_PKG_NAME"),
            option_env!("CARGO_PKG_VERSION"),
            azure_core::http::ClientOptions::default(),
            Vec::new(),
            Vec::new(),
            None,
        )
    }

    fn create_single_region_manager() -> Arc<GlobalEndpointManager> {
        GlobalEndpointManager::new(
            "https://test.documents.azure.com".parse().unwrap(),
            vec![Region::from("West US")],
            vec![],
            create_test_pipeline(),
        )
    }

    fn create_multi_region_manager() -> Arc<GlobalEndpointManager> {
        let manager = GlobalEndpointManager::new(
            "https://test.documents.azure.com".parse().unwrap(),
            vec![Region::from("West US"), Region::from("East US")],
            vec![],
            create_test_pipeline(),
        );

        let west = AccountRegion {
            name: Region::from("West US"),
            database_account_endpoint: "https://test-westus.documents.azure.com".parse().unwrap(),
        };
        let east = AccountRegion {
            name: Region::from("East US"),
            database_account_endpoint: "https://test-eastus.documents.azure.com".parse().unwrap(),
        };

        manager.update_location_cache(vec![west.clone(), east.clone()], vec![west, east]);
        manager
    }

    fn create_three_region_manager() -> Arc<GlobalEndpointManager> {
        let manager = GlobalEndpointManager::new(
            "https://test.documents.azure.com".parse().unwrap(),
            vec![
                Region::from("West US"),
                Region::from("East US"),
                Region::from("Central US"),
            ],
            vec![],
            create_test_pipeline(),
        );

        let west = AccountRegion {
            name: Region::from("West US"),
            database_account_endpoint: "https://test-westus.documents.azure.com".parse().unwrap(),
        };
        let east = AccountRegion {
            name: Region::from("East US"),
            database_account_endpoint: "https://test-eastus.documents.azure.com".parse().unwrap(),
        };
        let central = AccountRegion {
            name: Region::from("Central US"),
            database_account_endpoint: "https://test-centralus.documents.azure.com"
                .parse()
                .unwrap(),
        };

        manager.update_location_cache(
            vec![west.clone(), east.clone(), central.clone()],
            vec![west, east, central],
        );
        manager
    }

    /// Creates a multi-region manager that simulates a single-master account:
    /// one write endpoint (West US) and two read endpoints (West US + East US).
    fn create_single_master_multi_region_manager() -> Arc<GlobalEndpointManager> {
        let manager = GlobalEndpointManager::new(
            "https://test.documents.azure.com".parse().unwrap(),
            vec![Region::from("West US"), Region::from("East US")],
            vec![],
            create_test_pipeline(),
        );

        let west = AccountRegion {
            name: Region::from("West US"),
            database_account_endpoint: "https://test-westus.documents.azure.com".parse().unwrap(),
        };
        let east = AccountRegion {
            name: Region::from("East US"),
            database_account_endpoint: "https://test-eastus.documents.azure.com".parse().unwrap(),
        };

        // Single write location, multiple read locations
        manager.update_location_cache(vec![west.clone()], vec![west, east]);
        manager
    }

    fn create_read_request() -> CosmosRequest {
        let resource_link = ResourceLink::root(ResourceType::Documents);
        let mut request = CosmosRequest::builder(OperationType::Read, resource_link)
            .build()
            .unwrap();
        request.request_context.location_endpoint_to_route =
            Some("https://test-westus.documents.azure.com/".parse().unwrap());
        request.request_context.resolved_partition_key_range =
            Some(PartitionKeyRange::new("0".into(), "".into(), "FF".into()));
        request.request_context.resolved_collection_rid = Some("dbs/db1/colls/coll1".into());
        request
    }

    fn create_write_request() -> CosmosRequest {
        let resource_link = ResourceLink::root(ResourceType::Documents);
        let mut request = CosmosRequest::builder(OperationType::Create, resource_link)
            .build()
            .unwrap();
        request.request_context.location_endpoint_to_route =
            Some("https://test-westus.documents.azure.com/".parse().unwrap());
        request.request_context.resolved_partition_key_range =
            Some(PartitionKeyRange::new("0".into(), "".into(), "FF".into()));
        request.request_context.resolved_collection_rid = Some("dbs/db1/colls/coll1".into());
        request
    }

    fn create_stored_procedure_execute_request() -> CosmosRequest {
        let resource_link = ResourceLink::root(ResourceType::StoredProcedures);
        let mut request = CosmosRequest::builder(OperationType::Execute, resource_link)
            .build()
            .unwrap();
        request.request_context.location_endpoint_to_route =
            Some("https://test-westus.documents.azure.com/".parse().unwrap());
        request.request_context.resolved_partition_key_range =
            Some(PartitionKeyRange::new("0".into(), "".into(), "FF".into()));
        request.request_context.resolved_collection_rid = Some("dbs/db1/colls/coll1".into());
        request
    }

    fn create_database_request() -> CosmosRequest {
        let resource_link = ResourceLink::root(ResourceType::Databases);
        let mut request = CosmosRequest::builder(OperationType::Read, resource_link)
            .build()
            .unwrap();
        request.request_context.location_endpoint_to_route =
            Some("https://test-westus.documents.azure.com/".parse().unwrap());
        request.request_context.resolved_partition_key_range =
            Some(PartitionKeyRange::new("0".into(), "".into(), "FF".into()));
        request.request_context.resolved_collection_rid = Some("dbs/db1/colls/coll1".into());
        request
    }

    // -----------------------------------------------------------------------
    // PartitionHealthStatus tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_health_status_values() {
        assert_eq!(PartitionHealthStatus::Healthy as i32, 100);
        assert_eq!(PartitionHealthStatus::Unhealthy as i32, 200);
    }

    #[tokio::test]
    async fn test_health_status_equality() {
        assert_eq!(
            PartitionHealthStatus::Healthy,
            PartitionHealthStatus::Healthy
        );
        assert_ne!(
            PartitionHealthStatus::Healthy,
            PartitionHealthStatus::Unhealthy
        );
    }

    // -----------------------------------------------------------------------
    // PartitionKeyRangeFailoverInfo tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_failover_info_new_initializes_correctly() {
        let info = PartitionKeyRangeFailoverInfo::new(
            "rid1".to_string(),
            "https://loc1.documents.azure.com/".to_string(),
        );

        assert_eq!(info.collection_rid, "rid1");
        assert_eq!(info.current, "https://loc1.documents.azure.com/");
        assert_eq!(
            info.first_failed_location,
            "https://loc1.documents.azure.com/"
        );

        let (read_count, write_count) = info.snapshot_consecutive_request_failure_count();
        assert_eq!(read_count, 0);
        assert_eq!(write_count, 0);
    }

    #[tokio::test]
    async fn test_failover_info_timestamps_initialized_to_now() {
        let before = Instant::now();
        let info = PartitionKeyRangeFailoverInfo::new("rid".into(), "https://loc.com/".into());
        let after = Instant::now();

        let (first, last) = info.snapshot_partition_failover_timestamps();
        assert!(first >= before && first <= after);
        assert!(last >= before && last <= after);
    }

    #[tokio::test]
    async fn test_try_move_next_location_moves_to_first_available() {
        let mut info =
            PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        let locations = vec![
            "https://loc1.com/".to_string(),
            "https://loc2.com/".to_string(),
            "https://loc3.com/".to_string(),
        ];

        let result = info.try_move_next_location(&locations, "https://loc1.com/");
        assert!(result);
        assert_eq!(info.current, "https://loc2.com/");
    }

    #[tokio::test]
    async fn test_try_move_next_location_skips_current_location() {
        let mut info =
            PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        // Only the current location is available
        let locations = vec!["https://loc1.com/".to_string()];

        let result = info.try_move_next_location(&locations, "https://loc1.com/");
        assert!(!result);
        assert_eq!(info.current, "https://loc1.com/");
    }

    #[tokio::test]
    async fn test_try_move_next_location_returns_true_if_already_moved() {
        let mut info =
            PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        // Move to loc2 first
        let locations = vec![
            "https://loc1.com/".to_string(),
            "https://loc2.com/".to_string(),
        ];
        info.try_move_next_location(&locations, "https://loc1.com/");
        assert_eq!(info.current, "https://loc2.com/");

        // Now try to move again with loc1 as failed — but current is already loc2
        let result = info.try_move_next_location(&locations, "https://loc1.com/");
        assert!(result);
        // Current should remain loc2 (already moved away from loc1)
        assert_eq!(info.current, "https://loc2.com/");
    }

    #[tokio::test]
    async fn test_try_move_next_location_sequential_failover() {
        let mut info =
            PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        let locations = vec![
            "https://loc1.com/".to_string(),
            "https://loc2.com/".to_string(),
            "https://loc3.com/".to_string(),
        ];

        // First failover: loc1 -> loc2
        assert!(info.try_move_next_location(&locations, "https://loc1.com/"));
        assert_eq!(info.current, "https://loc2.com/");

        // Second failover: loc2 -> loc3
        assert!(info.try_move_next_location(&locations, "https://loc2.com/"));
        assert_eq!(info.current, "https://loc3.com/");

        // Third failover: loc3 -> no more locations
        assert!(!info.try_move_next_location(&locations, "https://loc3.com/"));
        assert_eq!(info.current, "https://loc3.com/");
    }

    #[tokio::test]
    async fn test_try_move_next_location_empty_locations() {
        let mut info =
            PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        let result = info.try_move_next_location(&[], "https://loc1.com/");
        assert!(!result);
    }

    #[tokio::test]
    async fn test_can_circuit_breaker_trigger_reads_below_threshold() {
        let info = PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        // Default read threshold is 2, counter starts at 0
        assert!(!info.can_circuit_breaker_trigger_partition_failover(true));
    }

    #[tokio::test]
    async fn test_can_circuit_breaker_trigger_reads_at_threshold() {
        let info = PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        // Increment read counter to threshold (2)
        info.consecutive_read_request_failure_count
            .store(2, Ordering::SeqCst);

        // At threshold, not above — should not trigger
        assert!(!info.can_circuit_breaker_trigger_partition_failover(true));
    }

    #[tokio::test]
    async fn test_can_circuit_breaker_trigger_reads_above_threshold() {
        let info = PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        // Default read threshold is 10; store 11 so that 11 > 10 triggers the breaker
        info.consecutive_read_request_failure_count
            .store(11, Ordering::SeqCst);

        assert!(info.can_circuit_breaker_trigger_partition_failover(true));
    }

    #[tokio::test]
    async fn test_can_circuit_breaker_trigger_writes_below_threshold() {
        let info = PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        // Default write threshold is 5, counter starts at 0
        assert!(!info.can_circuit_breaker_trigger_partition_failover(false));
    }

    #[tokio::test]
    async fn test_can_circuit_breaker_trigger_writes_at_threshold() {
        let info = PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        // Increment write counter to threshold (5)
        info.consecutive_write_request_failure_count
            .store(5, Ordering::SeqCst);

        // At threshold, not above — should not trigger
        assert!(!info.can_circuit_breaker_trigger_partition_failover(false));
    }

    #[tokio::test]
    async fn test_can_circuit_breaker_trigger_writes_above_threshold() {
        let info = PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        // Increment write counter above threshold (> 5)
        info.consecutive_write_request_failure_count
            .store(6, Ordering::SeqCst);

        assert!(info.can_circuit_breaker_trigger_partition_failover(false));
    }

    #[tokio::test]
    async fn test_can_circuit_breaker_read_count_does_not_affect_write_check() {
        let info = PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        // High read count should not trigger write failover
        info.consecutive_read_request_failure_count
            .store(100, Ordering::SeqCst);
        assert!(!info.can_circuit_breaker_trigger_partition_failover(false));

        // High write count should not trigger read failover
        info.consecutive_write_request_failure_count
            .store(100, Ordering::SeqCst);
        info.consecutive_read_request_failure_count
            .store(0, Ordering::SeqCst);
        assert!(!info.can_circuit_breaker_trigger_partition_failover(true));
    }

    #[tokio::test]
    async fn test_increment_read_failure_count() {
        let info = PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());
        let now = Instant::now();

        info.increment_request_failure_counts(true, now);
        info.increment_request_failure_counts(true, now);
        info.increment_request_failure_counts(true, now);

        let (read_count, write_count) = info.snapshot_consecutive_request_failure_count();
        assert_eq!(read_count, 3);
        assert_eq!(write_count, 0);
    }

    #[tokio::test]
    async fn test_increment_write_failure_count() {
        let info = PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());
        let now = Instant::now();

        info.increment_request_failure_counts(false, now);
        info.increment_request_failure_counts(false, now);

        let (read_count, write_count) = info.snapshot_consecutive_request_failure_count();
        assert_eq!(read_count, 0);
        assert_eq!(write_count, 2);
    }

    #[tokio::test]
    async fn test_increment_mixed_read_and_write_failures() {
        let info = PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());
        let now = Instant::now();

        info.increment_request_failure_counts(true, now);
        info.increment_request_failure_counts(false, now);
        info.increment_request_failure_counts(true, now);
        info.increment_request_failure_counts(false, now);
        info.increment_request_failure_counts(false, now);

        let (read_count, write_count) = info.snapshot_consecutive_request_failure_count();
        assert_eq!(read_count, 2);
        assert_eq!(write_count, 3);
    }

    #[tokio::test]
    async fn test_increment_updates_last_failure_time() {
        let info = PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        let (_, time_before) = info.snapshot_partition_failover_timestamps();

        // Sleep briefly to ensure time advances
        std::thread::sleep(std::time::Duration::from_millis(10));
        let later = Instant::now();
        info.increment_request_failure_counts(true, later);

        let (_, time_after) = info.snapshot_partition_failover_timestamps();
        assert!(time_after > time_before);
    }

    #[tokio::test]
    async fn test_increment_resets_counters_when_timeout_window_exceeded() {
        let info = PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        // Add some failures at current time
        let now = Instant::now();
        info.increment_request_failure_counts(true, now);
        info.increment_request_failure_counts(true, now);
        info.increment_request_failure_counts(false, now);

        let (read_count, write_count) = info.snapshot_consecutive_request_failure_count();
        assert_eq!(read_count, 2);
        assert_eq!(write_count, 1);

        // Simulate a time far in the future (beyond the 5 min default window)
        // The timeout_counter_reset_window is 5 * 60 = 300 seconds
        let far_future = now + std::time::Duration::from_secs(400);
        info.increment_request_failure_counts(true, far_future);

        // After reset + 1 new read failure
        let (read_count, write_count) = info.snapshot_consecutive_request_failure_count();
        assert_eq!(read_count, 1);
        assert_eq!(write_count, 0);
    }

    #[tokio::test]
    async fn test_increment_does_not_reset_within_timeout_window() {
        let info = PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        let now = Instant::now();
        info.increment_request_failure_counts(true, now);
        info.increment_request_failure_counts(true, now);

        // Within the window (< 300 seconds)
        let soon = now + std::time::Duration::from_secs(100);
        info.increment_request_failure_counts(true, soon);

        let (read_count, _) = info.snapshot_consecutive_request_failure_count();
        assert_eq!(read_count, 3);
    }

    #[tokio::test]
    async fn test_snapshot_consecutive_count_returns_current_values() {
        let info = PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        info.consecutive_read_request_failure_count
            .store(7, Ordering::SeqCst);
        info.consecutive_write_request_failure_count
            .store(3, Ordering::SeqCst);

        let (r, w) = info.snapshot_consecutive_request_failure_count();
        assert_eq!(r, 7);
        assert_eq!(w, 3);
    }

    // -----------------------------------------------------------------------
    // mark_endpoints_to_healthy tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_mark_endpoints_to_healthy_marks_all_as_healthy() {
        let pk1 = PartitionKeyRange::new("0".into(), "".into(), "AA".into());
        let pk2 = PartitionKeyRange::new("1".into(), "AA".into(), "FF".into());

        let mut mappings = HashMap::new();
        mappings.insert(
            pk1.clone(),
            (
                "rid1".to_string(),
                "https://loc1.com/".to_string(),
                PartitionHealthStatus::Unhealthy,
            ),
        );
        mappings.insert(
            pk2.clone(),
            (
                "rid2".to_string(),
                "https://loc2.com/".to_string(),
                PartitionHealthStatus::Unhealthy,
            ),
        );

        GlobalPartitionEndpointManager::mark_endpoints_to_healthy(&mut mappings);

        assert_eq!(mappings[&pk1].2, PartitionHealthStatus::Healthy);
        assert_eq!(mappings[&pk2].2, PartitionHealthStatus::Healthy);
    }

    #[tokio::test]
    async fn test_mark_endpoints_to_healthy_empty_map() {
        let mut mappings: HashMap<PartitionKeyRange, (String, String, PartitionHealthStatus)> =
            HashMap::new();

        GlobalPartitionEndpointManager::mark_endpoints_to_healthy(&mut mappings);

        assert!(mappings.is_empty());
    }

    #[tokio::test]
    async fn test_mark_endpoints_to_healthy_already_healthy() {
        let pk = PartitionKeyRange::new("0".into(), "".into(), "FF".into());

        let mut mappings = HashMap::new();
        mappings.insert(
            pk.clone(),
            (
                "rid1".to_string(),
                "https://loc1.com/".to_string(),
                PartitionHealthStatus::Healthy,
            ),
        );

        GlobalPartitionEndpointManager::mark_endpoints_to_healthy(&mut mappings);

        assert_eq!(mappings[&pk].2, PartitionHealthStatus::Healthy);
    }

    // -----------------------------------------------------------------------
    // GlobalPartitionEndpointManager flag tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_new_both_flags_disabled() {
        let gem = create_single_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, false);

        assert!(!manager.partition_level_automatic_failover_enabled());
        assert!(!manager.partition_level_circuit_breaker_enabled());
        assert!(!manager.partition_level_failover_enabled());
    }

    #[tokio::test]
    async fn test_new_auto_failover_enabled_only() {
        let gem = create_single_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, false);

        assert!(manager.partition_level_automatic_failover_enabled());
        assert!(!manager.partition_level_circuit_breaker_enabled());
        assert!(manager.partition_level_failover_enabled());
    }

    #[tokio::test]
    async fn test_new_circuit_breaker_enabled_only() {
        let gem = create_single_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        assert!(!manager.partition_level_automatic_failover_enabled());
        assert!(manager.partition_level_circuit_breaker_enabled());
        assert!(manager.partition_level_failover_enabled());
    }

    #[tokio::test]
    async fn test_new_both_flags_enabled() {
        let gem = create_single_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, true);

        assert!(manager.partition_level_automatic_failover_enabled());
        assert!(manager.partition_level_circuit_breaker_enabled());
        assert!(manager.partition_level_failover_enabled());
    }

    // -----------------------------------------------------------------------
    // can_use_partition_level_failover_locations tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_can_use_failover_locations_with_single_endpoint() {
        let gem = create_single_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, true);

        let request = create_read_request();
        // Single region: read_endpoints().len() <= 1 → false
        assert!(!manager.can_use_partition_level_failover_locations(&request));
    }

    #[tokio::test]
    async fn test_can_use_failover_locations_with_multiple_endpoints_documents() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, true);

        let request = create_read_request();
        assert!(manager.can_use_partition_level_failover_locations(&request));
    }

    #[tokio::test]
    async fn test_can_use_failover_locations_with_stored_procedure_execute() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, true);

        let request = create_stored_procedure_execute_request();
        assert!(manager.can_use_partition_level_failover_locations(&request));
    }

    #[tokio::test]
    async fn test_can_use_failover_locations_with_database_resource() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, true);

        let request = create_database_request();
        // Databases are not eligible for partition-level failover
        assert!(!manager.can_use_partition_level_failover_locations(&request));
    }

    // -----------------------------------------------------------------------
    // is_request_eligible_for_per_partition_automatic_failover tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_auto_failover_eligible_write_on_single_master() {
        // Single master: can_support_multiple_write_locations returns false
        let gem = create_single_master_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, false);

        let request = create_write_request();
        // auto failover enabled + write request + single master = eligible
        assert!(manager.is_request_eligible_for_per_partition_automatic_failover(&request));
    }

    #[tokio::test]
    async fn test_auto_failover_not_eligible_when_disabled() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, false);

        let request = create_write_request();
        assert!(!manager.is_request_eligible_for_per_partition_automatic_failover(&request));
    }

    #[tokio::test]
    async fn test_auto_failover_not_eligible_for_read_request() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, false);

        let request = create_read_request();
        // Read-only requests are not eligible for automatic failover
        assert!(!manager.is_request_eligible_for_per_partition_automatic_failover(&request));
    }

    // -----------------------------------------------------------------------
    // is_request_eligible_for_partition_level_circuit_breaker tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_circuit_breaker_eligible_for_read_request() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let request = create_read_request();
        assert!(manager.is_request_eligible_for_partition_level_circuit_breaker(&request));
    }

    #[tokio::test]
    async fn test_circuit_breaker_not_eligible_when_disabled() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, false);

        let request = create_read_request();
        assert!(!manager.is_request_eligible_for_partition_level_circuit_breaker(&request));
    }

    #[tokio::test]
    async fn test_circuit_breaker_not_eligible_write_on_single_master() {
        // Single-master: can_support_multiple_write_locations returns false for writes
        let gem = create_single_master_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let request = create_write_request();
        // Write on single-master: circuit breaker should not be eligible
        // (can_support_multiple_write_locations is false for the default manager config)
        assert!(!manager.is_request_eligible_for_partition_level_circuit_breaker(&request));
    }

    // -----------------------------------------------------------------------
    // is_request_eligible_for_partition_failover tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_partition_failover_returns_none_when_both_disabled() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, false);

        let request = create_read_request();
        assert!(manager
            .is_request_eligible_for_partition_failover(&request, false)
            .is_none());
    }

    #[tokio::test]
    async fn test_partition_failover_returns_some_when_eligible() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let request = create_read_request();
        let result = manager.is_request_eligible_for_partition_failover(&request, false);
        assert!(result.is_some());

        let (pk_range, failed_loc) = result.unwrap();
        assert_eq!(pk_range.id, "0");
        assert!(failed_loc.is_none()); // Not validating failed location
    }

    #[tokio::test]
    async fn test_partition_failover_validates_failed_location() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let request = create_read_request();
        let result = manager.is_request_eligible_for_partition_failover(&request, true);
        assert!(result.is_some());

        let (pk_range, failed_loc) = result.unwrap();
        assert_eq!(pk_range.id, "0");
        assert!(failed_loc.is_some());
        assert_eq!(
            failed_loc.unwrap().as_str(),
            "https://test-westus.documents.azure.com/"
        );
    }

    #[tokio::test]
    async fn test_partition_failover_returns_none_without_partition_key_range() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let mut request = create_read_request();
        request.request_context.resolved_partition_key_range = None;

        assert!(manager
            .is_request_eligible_for_partition_failover(&request, false)
            .is_none());
    }

    #[tokio::test]
    async fn test_partition_failover_returns_none_for_ineligible_resource_type() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let request = create_database_request();
        assert!(manager
            .is_request_eligible_for_partition_failover(&request, false)
            .is_none());
    }

    #[tokio::test]
    async fn test_partition_failover_returns_none_without_failed_location() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let mut request = create_read_request();
        request.request_context.location_endpoint_to_route = None;

        // With should_validate_failed_location=true, missing location → None
        assert!(manager
            .is_request_eligible_for_partition_failover(&request, true)
            .is_none());
    }

    // -----------------------------------------------------------------------
    // try_mark_endpoint_unavailable_for_partition_key_range tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_mark_endpoint_unavailable_circuit_breaker_path() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let request = create_read_request();
        let result = manager.try_mark_endpoint_unavailable_for_partition_key_range(&request);
        assert!(result);

        // Verify the override was added to the read_and_write map
        let guard = manager
            .partition_key_range_to_location_for_read_and_write
            .read()
            .unwrap();
        let pk = PartitionKeyRange::new("0".into(), "".into(), "FF".into());
        assert!(guard.contains_key(&pk));

        let failover_info = guard.get(&pk).unwrap();
        assert_ne!(
            failover_info.current,
            "https://test-westus.documents.azure.com/"
        );
    }

    #[tokio::test]
    async fn test_mark_endpoint_unavailable_auto_failover_path() {
        let gem = create_single_master_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, false);

        let request = create_write_request();
        let result = manager.try_mark_endpoint_unavailable_for_partition_key_range(&request);
        assert!(result);

        // Verify the override was added to the write map
        let guard = manager
            .partition_key_range_to_location_for_write
            .read()
            .unwrap();
        let pk = PartitionKeyRange::new("0".into(), "".into(), "FF".into());
        assert!(guard.contains_key(&pk));
    }

    #[tokio::test]
    async fn test_mark_endpoint_unavailable_returns_false_when_disabled() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, false);

        let request = create_read_request();
        assert!(!manager.try_mark_endpoint_unavailable_for_partition_key_range(&request));
    }

    #[tokio::test]
    async fn test_mark_endpoint_unavailable_returns_false_without_failed_location() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let mut request = create_read_request();
        request.request_context.location_endpoint_to_route = None;

        assert!(!manager.try_mark_endpoint_unavailable_for_partition_key_range(&request));
    }

    #[tokio::test]
    async fn test_mark_endpoint_unavailable_sequential_failover_removes_on_exhaust() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let pk = PartitionKeyRange::new("0".into(), "".into(), "FF".into());

        // First failure at West US → should move to East US
        let request = create_read_request();
        assert!(manager.try_mark_endpoint_unavailable_for_partition_key_range(&request));

        {
            let guard = manager
                .partition_key_range_to_location_for_read_and_write
                .read()
                .unwrap();
            let info = guard.get(&pk).unwrap();
            assert_eq!(info.current, "https://test-eastus.documents.azure.com/");
        }

        // Second failure at East US → all exhausted, should remove override
        let mut request2 = create_read_request();
        request2.request_context.location_endpoint_to_route =
            Some("https://test-eastus.documents.azure.com/".parse().unwrap());
        let result = manager.try_mark_endpoint_unavailable_for_partition_key_range(&request2);
        assert!(!result);

        // Override should be removed
        let guard = manager
            .partition_key_range_to_location_for_read_and_write
            .read()
            .unwrap();
        assert!(!guard.contains_key(&pk));
    }

    // -----------------------------------------------------------------------
    // try_add_partition_level_location_override tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_add_override_returns_false_when_no_override_exists() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let mut request = create_read_request();
        assert!(!manager.try_add_partition_level_location_override(&mut request));
    }

    #[tokio::test]
    async fn test_add_override_routes_to_override_location() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        // First, mark endpoint as unavailable to create an override
        let request = create_read_request();
        manager.try_mark_endpoint_unavailable_for_partition_key_range(&request);

        // Bump failure counts above threshold so circuit breaker applies.
        // Default read threshold is 10; store 11 so that 11 > 10 is true.
        {
            let guard = manager
                .partition_key_range_to_location_for_read_and_write
                .read()
                .unwrap();
            let pk = PartitionKeyRange::new("0".into(), "".into(), "FF".into());
            let info = guard.get(&pk).unwrap();
            info.consecutive_read_request_failure_count
                .store(11, Ordering::SeqCst);
        }

        // Now try to add override — should route to the new location
        let mut request2 = create_read_request();
        let result = manager.try_add_partition_level_location_override(&mut request2);
        assert!(result);

        assert_eq!(
            request2
                .request_context
                .location_endpoint_to_route
                .unwrap()
                .as_str(),
            "https://test-eastus.documents.azure.com/"
        );
    }

    #[tokio::test]
    async fn test_add_override_returns_false_when_disabled() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, false);

        let mut request = create_read_request();
        assert!(!manager.try_add_partition_level_location_override(&mut request));
    }

    #[tokio::test]
    async fn test_add_override_circuit_breaker_below_threshold_returns_false() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        // Mark endpoint unavailable to create override
        let request = create_read_request();
        manager.try_mark_endpoint_unavailable_for_partition_key_range(&request);

        // Don't bump failure counts — below threshold
        let mut request2 = create_read_request();
        let result = manager.try_add_partition_level_location_override(&mut request2);
        assert!(!result);
    }

    #[tokio::test]
    async fn test_add_override_auto_failover_path() {
        let gem = create_single_master_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, false);

        // Mark write endpoint as unavailable
        let request = create_write_request();
        manager.try_mark_endpoint_unavailable_for_partition_key_range(&request);

        // Now try to add override for a write request
        let mut request2 = create_write_request();
        let result = manager.try_add_partition_level_location_override(&mut request2);
        assert!(result);
    }

    // -----------------------------------------------------------------------
    // increment_request_failure_counter_and_check_if_partition_can_failover tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_increment_failure_counter_returns_false_when_disabled() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, false);

        let request = create_read_request();
        assert!(!manager
            .increment_request_failure_counter_and_check_if_partition_can_failover(&request));
    }

    #[tokio::test]
    async fn test_increment_failure_counter_creates_entry_on_first_call() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let request = create_read_request();
        let _ =
            manager.increment_request_failure_counter_and_check_if_partition_can_failover(&request);

        let guard = manager
            .partition_key_range_to_location_for_read_and_write
            .read()
            .unwrap();
        let pk = PartitionKeyRange::new("0".into(), "".into(), "FF".into());
        assert!(guard.contains_key(&pk));
    }

    #[tokio::test]
    async fn test_increment_failure_counter_below_threshold_returns_false() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let request = create_read_request();
        // Default read threshold is 2, so 1 failure should not trigger
        let result =
            manager.increment_request_failure_counter_and_check_if_partition_can_failover(&request);
        assert!(!result);
    }

    #[tokio::test]
    async fn test_increment_failure_counter_above_threshold_returns_true() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let request = create_read_request();

        // Default read threshold is 10; we need > 10 increments so the
        // counter exceeds the threshold (strict greater-than comparison).
        for _ in 0..11 {
            manager.increment_request_failure_counter_and_check_if_partition_can_failover(&request);
        }

        let result =
            manager.increment_request_failure_counter_and_check_if_partition_can_failover(&request);
        assert!(result);
    }

    #[tokio::test]
    async fn test_increment_failure_counter_auto_failover_path_for_writes() {
        let gem = create_single_master_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, false);

        let request = create_write_request();
        let _ =
            manager.increment_request_failure_counter_and_check_if_partition_can_failover(&request);

        // Verify it went to the write map
        let guard = manager
            .partition_key_range_to_location_for_write
            .read()
            .unwrap();
        let pk = PartitionKeyRange::new("0".into(), "".into(), "FF".into());
        assert!(guard.contains_key(&pk));

        // And not in the read_and_write map
        let guard2 = manager
            .partition_key_range_to_location_for_read_and_write
            .read()
            .unwrap();
        assert!(!guard2.contains_key(&pk));
    }

    // -----------------------------------------------------------------------
    // try_add_or_update_partition_failover_info_and_move_to_next_location tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_add_or_update_moves_to_next_location() {
        let gem = create_three_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let pk = PartitionKeyRange::new("0".into(), "".into(), "FF".into());
        let request = create_read_request();
        let map = Arc::new(RwLock::new(HashMap::new()));

        let next_locations = vec![
            "https://test-westus.documents.azure.com/".to_string(),
            "https://test-eastus.documents.azure.com/".to_string(),
            "https://test-centralus.documents.azure.com/".to_string(),
        ];

        let result = manager.try_add_or_update_partition_failover_info_and_move_to_next_location(
            &pk,
            "https://test-westus.documents.azure.com/",
            &next_locations,
            &request,
            &map,
        );
        assert!(result);

        let guard = map.read().unwrap();
        let info = guard.get(&pk).unwrap();
        assert_eq!(info.current, "https://test-eastus.documents.azure.com/");
    }

    #[tokio::test]
    async fn test_add_or_update_removes_on_all_exhausted() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let pk = PartitionKeyRange::new("0".into(), "".into(), "FF".into());
        let request = create_read_request();
        let map = Arc::new(RwLock::new(HashMap::new()));

        let next_locations = vec![
            "https://test-westus.documents.azure.com/".to_string(),
            "https://test-eastus.documents.azure.com/".to_string(),
        ];

        // First move: west -> east
        let result = manager.try_add_or_update_partition_failover_info_and_move_to_next_location(
            &pk,
            "https://test-westus.documents.azure.com/",
            &next_locations,
            &request,
            &map,
        );
        assert!(result);

        // Second move: east -> exhausted
        let result2 = manager.try_add_or_update_partition_failover_info_and_move_to_next_location(
            &pk,
            "https://test-eastus.documents.azure.com/",
            &next_locations,
            &request,
            &map,
        );
        assert!(!result2);

        // Entry should be removed
        let guard = map.read().unwrap();
        assert!(!guard.contains_key(&pk));
    }

    // -----------------------------------------------------------------------
    // Multiple partition key range tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_different_partition_key_ranges_tracked_independently() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        // Create two requests with different partition key ranges
        let mut request1 = create_read_request();
        request1.request_context.resolved_partition_key_range =
            Some(PartitionKeyRange::new("0".into(), "".into(), "AA".into()));

        let mut request2 = create_read_request();
        request2.request_context.resolved_partition_key_range =
            Some(PartitionKeyRange::new("1".into(), "AA".into(), "FF".into()));

        // Mark both as unavailable
        assert!(manager.try_mark_endpoint_unavailable_for_partition_key_range(&request1));
        assert!(manager.try_mark_endpoint_unavailable_for_partition_key_range(&request2));

        // Both should have entries
        let guard = manager
            .partition_key_range_to_location_for_read_and_write
            .read()
            .unwrap();
        assert_eq!(guard.len(), 2);
    }

    // -----------------------------------------------------------------------
    // Debug trait tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_debug_formatting() {
        let gem = create_single_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, false);

        let debug_str = format!("{:?}", manager);
        assert!(debug_str.contains("GlobalPartitionEndpointManager"));
        assert!(debug_str.contains("partition_level_automatic_failover_enabled"));
        assert!(debug_str.contains("partition_level_circuit_breaker_enabled"));
    }

    #[tokio::test]
    async fn test_failover_info_debug_formatting() {
        let info = PartitionKeyRangeFailoverInfo::new("rid1".into(), "https://loc1.com/".into());

        let debug_str = format!("{:?}", info);
        assert!(debug_str.contains("PartitionKeyRangeFailoverInfo"));
        assert!(debug_str.contains("rid1"));
    }

    // -----------------------------------------------------------------------
    // Three-region failover tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_three_region_sequential_failover() {
        let gem = create_three_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        let pk = PartitionKeyRange::new("0".into(), "".into(), "FF".into());

        // First failure at West US
        let request1 = create_read_request();
        assert!(manager.try_mark_endpoint_unavailable_for_partition_key_range(&request1));

        {
            let guard = manager
                .partition_key_range_to_location_for_read_and_write
                .read()
                .unwrap();
            assert_eq!(
                guard.get(&pk).unwrap().current,
                "https://test-eastus.documents.azure.com/"
            );
        }

        // Second failure at East US
        let mut request2 = create_read_request();
        request2.request_context.location_endpoint_to_route =
            Some("https://test-eastus.documents.azure.com/".parse().unwrap());
        assert!(manager.try_mark_endpoint_unavailable_for_partition_key_range(&request2));

        {
            let guard = manager
                .partition_key_range_to_location_for_read_and_write
                .read()
                .unwrap();
            assert_eq!(
                guard.get(&pk).unwrap().current,
                "https://test-centralus.documents.azure.com/"
            );
        }

        // Third failure at Central US → all exhausted, override removed
        let mut request3 = create_read_request();
        request3.request_context.location_endpoint_to_route = Some(
            "https://test-centralus.documents.azure.com/"
                .parse()
                .unwrap(),
        );
        assert!(!manager.try_mark_endpoint_unavailable_for_partition_key_range(&request3));

        let guard = manager
            .partition_key_range_to_location_for_read_and_write
            .read()
            .unwrap();
        assert!(!guard.contains_key(&pk));
    }

    // -----------------------------------------------------------------------
    // Background initialization tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_background_init_flag_set_on_construction() {
        let gem = create_single_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, true);

        assert!(manager
            .background_connection_init_active
            .load(Ordering::SeqCst));
    }

    #[tokio::test]
    async fn test_second_background_init_is_noop() {
        let gem = create_single_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, true);

        // Flag is already true after construction
        assert!(manager
            .background_connection_init_active
            .load(Ordering::SeqCst));

        // Calling again should be a no-op (no panic, flag stays true)
        manager.initialize_and_start_circuit_breaker_failback_background_refresh();
        assert!(manager
            .background_connection_init_active
            .load(Ordering::SeqCst));
    }

    // -----------------------------------------------------------------------
    // End-to-end: mark unavailable → add override → verify routing
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_end_to_end_failover_and_override_routing() {
        let gem = create_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, true);

        // Step 1: A read request fails at West US
        let request = create_read_request();
        assert!(manager.try_mark_endpoint_unavailable_for_partition_key_range(&request));

        // Step 2: Bump failure count above threshold.
        // Default read threshold is 10; store 11 so that 11 > 10 triggers override.
        {
            let guard = manager
                .partition_key_range_to_location_for_read_and_write
                .read()
                .unwrap();
            let pk = PartitionKeyRange::new("0".into(), "".into(), "FF".into());
            let info = guard.get(&pk).unwrap();
            info.consecutive_read_request_failure_count
                .store(11, Ordering::SeqCst);
        }

        // Step 3: New request should be routed to East US
        let mut new_request = create_read_request();
        assert!(manager.try_add_partition_level_location_override(&mut new_request));

        assert_eq!(
            new_request
                .request_context
                .location_endpoint_to_route
                .unwrap()
                .as_str(),
            "https://test-eastus.documents.azure.com/"
        );
    }

    #[tokio::test]
    async fn test_end_to_end_auto_failover_write_request() {
        let gem = create_single_master_multi_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, false);

        // Step 1: A write request fails at West US
        let request = create_write_request();
        assert!(manager.try_mark_endpoint_unavailable_for_partition_key_range(&request));

        // Step 2: New write request should be routed to East US
        // (auto failover path doesn't check circuit breaker thresholds)
        let mut new_request = create_write_request();
        assert!(manager.try_add_partition_level_location_override(&mut new_request));

        assert_eq!(
            new_request
                .request_context
                .location_endpoint_to_route
                .unwrap()
                .as_str(),
            "https://test-eastus.documents.azure.com/"
        );
    }

    // -----------------------------------------------------------------------
    // Dynamic configure_* method tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_configure_partition_level_automatic_failover_toggles_flag() {
        let gem = create_single_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, false);

        // Initially disabled
        assert!(!manager.partition_level_automatic_failover_enabled());

        // Enable it
        manager.configure_partition_level_automatic_failover(true);
        assert!(manager.partition_level_automatic_failover_enabled());

        // Disable it again
        manager.configure_partition_level_automatic_failover(false);
        assert!(!manager.partition_level_automatic_failover_enabled());
    }

    #[tokio::test]
    async fn test_configure_per_partition_circuit_breaker_toggles_flag() {
        let gem = create_single_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, false, false);

        // Initially disabled
        assert!(!manager.partition_level_circuit_breaker_enabled());

        // Enable it
        manager.configure_per_partition_circuit_breaker(true);
        assert!(manager.partition_level_circuit_breaker_enabled());

        // Disable it again
        manager.configure_per_partition_circuit_breaker(false);
        assert!(!manager.partition_level_circuit_breaker_enabled());
    }

    #[tokio::test]
    async fn test_configure_idempotent_same_value() {
        let gem = create_single_region_manager();
        let manager = GlobalPartitionEndpointManager::new(gem, true, true);

        // Setting the same value should not panic or change the flag
        assert!(manager.partition_level_automatic_failover_enabled());
        manager.configure_partition_level_automatic_failover(true);
        assert!(manager.partition_level_automatic_failover_enabled());

        assert!(manager.partition_level_circuit_breaker_enabled());
        manager.configure_per_partition_circuit_breaker(true);
        assert!(manager.partition_level_circuit_breaker_enabled());
    }
}