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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

use super::{
    get_substatus_code_from_error, get_substatus_code_from_response, is_non_retryable_status_code,
    resource_throttle_retry_policy::ResourceThrottleRetryPolicy, RequestSentExt, RequestSentStatus,
    RetryResult,
};
use crate::constants::{self, SubStatusCode};
use crate::cosmos_request::CosmosRequest;
use crate::operation_context::OperationType;
use crate::regions::Region;
use crate::routing::global_endpoint_manager::GlobalEndpointManager;
use crate::routing::global_partition_endpoint_manager::GlobalPartitionEndpointManager;
use azure_core::error::ErrorKind;
use azure_core::http::{RawResponse, StatusCode};
use azure_core::time::Duration;
use std::sync::Arc;
use tracing::error;
use url::Url;

/// An integer indicating the default retry intervals between two retry attempts.
const RETRY_INTERVAL_MS: i64 = 1000;

/// An integer indicating the maximum retry count on endpoint failures.
const MAX_RETRY_COUNT_ON_ENDPOINT_FAILURE: usize = 120;

/// An integer indicating the maximum retry count on connection failures before marking
/// the endpoint unavailable.
const MAX_RETRY_COUNT_ON_CONNECTION_FAILURE: usize = 3;

/// Context information for routing retry attempts to specific endpoints.
#[derive(Clone, Debug)]
struct RetryContext {
    /// Index of the location to route the retry request to
    retry_location_index: usize,

    /// Whether to retry on preferred locations only (true) or all available locations (false)
    retry_request_on_preferred_locations: bool,

    /// Whether to route directly to the hub endpoint instead of using location-based routing
    route_to_hub: bool,
}

/// Retry policy for handling data plane request failures.
#[derive(Debug)]
pub(crate) struct ClientRetryPolicy {
    /// Manages multi-region endpoint routing and failover logic
    global_endpoint_manager: Arc<GlobalEndpointManager>,

    /// An instance of GlobalPartitionEndpointManager that manages partition key range to endpoint mapping
    partition_key_range_location_cache: Arc<GlobalPartitionEndpointManager>,

    /// Whether automatic endpoint discovery is enabled for failover scenarios
    enable_endpoint_discovery: bool,

    /// Counter tracking the number of endpoint failover retry attempts
    failover_retry_count: usize,

    /// Counter tracking the number of session token unavailability retry attempts
    session_token_retry_count: usize,

    /// Counter tracking the number of service unavailable (503) retry attempts
    service_unavailable_retry_count: usize,

    /// Counter tracking the number of consecutive connection failure retry attempts
    /// on the current endpoint before marking it unavailable.
    connection_retry_count: usize,

    /// Whether the current request is a read operation (true) or write operation (false)
    operation_type: Option<OperationType>,

    /// The Cosmos request being processed by the retry policy.
    request: Option<CosmosRequest>,

    /// Whether the account supports writing to multiple locations simultaneously
    can_use_multiple_write_locations: bool,

    /// The resolved endpoint URL for the current or next request attempt
    location_endpoint: Option<Url>,

    /// Context information for routing the next retry attempt to a specific location
    retry_context: Option<RetryContext>,

    /// Regions excluded from routing for the current request
    excluded_regions: Option<Vec<Region>>,

    /// Underlying policy for handling resource throttling (429) with exponential backoff
    throttling_retry: ResourceThrottleRetryPolicy,
}

impl ClientRetryPolicy {
    /// Creates a new ClientRetryPolicy instance.
    ///
    /// # Summary
    /// Initializes a retry policy that handles various failure scenarios including session token
    /// mismatches, endpoint failures, service unavailability, and resource throttling. The policy
    /// manages automatic endpoint discovery, multi-region failover, and coordinates with the
    /// GlobalEndpointManager for routing decisions. It wraps a ResourceThrottleRetryPolicy for
    /// handling 429 (TooManyRequests) responses with exponential backoff.
    ///
    /// # Arguments
    /// * `global_endpoint_manager` - The endpoint manager for handling multi-region routing and failover
    ///
    /// # Returns
    /// A new `ClientRetryPolicy` instance configured with default retry limits and throttling behavior
    pub fn new(
        global_endpoint_manager: Arc<GlobalEndpointManager>,
        partition_key_range_location_cache: Arc<GlobalPartitionEndpointManager>,
        excluded_regions: Option<Vec<Region>>,
    ) -> Self {
        Self {
            global_endpoint_manager,
            partition_key_range_location_cache,
            enable_endpoint_discovery: true,
            failover_retry_count: 0,
            session_token_retry_count: 0,
            service_unavailable_retry_count: 0,
            connection_retry_count: 0,
            operation_type: None,
            request: None,
            can_use_multiple_write_locations: false,
            location_endpoint: None,
            retry_context: None,
            excluded_regions,
            throttling_retry: ResourceThrottleRetryPolicy::new(5, 200, 10),
        }
    }

    /// Returns whether the current operation is read-only.
    ///
    /// Defaults to `true` if the operation type has not been set, which is the
    /// conservative choice: reads are always safe to retry.
    fn is_read_only(&self) -> bool {
        self.operation_type.is_none_or(|op| op.is_read_only())
    }

    /// Prepares a request before it is sent, configuring routing and endpoint selection.
    ///
    /// # Summary
    /// Performs pre-flight setup for each request attempt by refreshing location cache,
    /// determining request characteristics (read vs write, multi-master support), and
    /// resolving the target endpoint based on retry context. Handles location-based routing
    /// directives, including retry attempts that target specific location indices or the hub
    /// endpoint. Clears previous routing context and configures the request with the
    /// appropriate endpoint URL for the current attempt.
    ///
    /// # Arguments
    /// * `request` - The mutable request to configure before sending
    pub(crate) async fn before_send_request(&mut self, request: &mut CosmosRequest) {
        // Ideally, any request flow should not be blocked by the outcome of refresh_location.
        // There can be three possible cases:
        // a) The refresh_location succeeds when TTL expires.
        // b) The refresh_location is bypassed when TTL hasn't expired.
        // c) The refresh_location operation has failed. In the event of a failure,
        //    the error is logged and the request should not be blocked.
        // Hence, the outcome of the operation is ignored here.
        _ = self.global_endpoint_manager.refresh_location(false).await;
        self.operation_type = Some(request.operation_type);
        self.excluded_regions = request.excluded_regions.clone().map(|e| e.0);
        self.can_use_multiple_write_locations = self
            .global_endpoint_manager
            .can_use_multiple_write_locations(request);

        if self.can_use_multiple_write_locations {
            request
                .headers
                .insert(constants::ALLOW_TENTATIVE_WRITES, "true");
        } else {
            request.headers.remove(constants::ALLOW_TENTATIVE_WRITES);
        }

        // Clear previous location-based routing directive
        request.request_context.clear_route_to_location();

        if let Some(ref ctx) = self.retry_context {
            let mut req_ctx = request.request_context.clone();
            if ctx.route_to_hub {
                req_ctx.route_to_location_endpoint(
                    request
                        .resource_link
                        .url(self.global_endpoint_manager.hub_uri()),
                );
            } else {
                req_ctx.route_to_location_index(
                    ctx.retry_location_index,
                    ctx.retry_request_on_preferred_locations,
                );
            }
            request.request_context = req_ctx;
        }

        // Resolve the endpoint for the request
        self.location_endpoint = Some(
            self.global_endpoint_manager
                .resolve_service_endpoint(request),
        );

        tracing::trace!(
            ?self.location_endpoint,
            "routed request to endpoint"
        );

        if let Some(ref endpoint) = self.location_endpoint {
            request
                .request_context
                .route_to_location_endpoint(endpoint.clone());
        }

        if self
            .partition_key_range_location_cache
            .partition_level_failover_enabled()
            && request.resource_type.is_partitioned()
        {
            self.partition_key_range_location_cache
                .try_add_partition_level_location_override(request);
        }

        self.request = Some(request.clone());
    }

    /// Determines whether a Data Plane request should be retried based on the response or error
    ///
    /// # Summary
    /// Evaluates the result of a request attempt to determine if it should be retried.
    /// Distinguishes between successful responses (2xx), client/server error responses
    /// (4xx/5xx), and transport/network errors. Delegates error responses to
    /// `should_retry_response` and exceptions to `should_retry_error` for detailed
    /// evaluation. Non-error responses (2xx, 3xx) are not retried. This method is
    /// called by the retry framework after each request attempt.
    ///
    /// # Arguments
    /// * `response` - The result of the request attempt (Ok with response or Err with error)
    ///
    /// # Returns
    /// A `RetryResult`:
    /// - `Retry { after: Duration }` if the request should be retried with specified delay
    /// - `DoNotRetry` for successful responses or non-retryable failures
    pub(crate) async fn should_retry(
        &mut self,
        response: &azure_core::Result<RawResponse>,
    ) -> RetryResult {
        match response {
            Ok(resp) if resp.status().is_server_error() || resp.status().is_client_error() => {
                self.should_retry_response(resp).await
            }
            Ok(_) => RetryResult::DoNotRetry,
            Err(err) => self.should_retry_error(err).await,
        }
    }

    /// Determines if a request should be retried when session token is unavailable.
    ///
    /// # Summary
    /// Handles 404.1022 (READ_SESSION_NOT_AVAILABLE) errors by attempting to retry on different
    /// endpoints. For multi-write scenarios, tries all available endpoints before giving up.
    /// For single-write scenarios, retries once on the primary write location. Increments the
    /// session token retry counter and configures retry context for endpoint routing.
    ///
    /// # Arguments
    /// * `cosmos_request` - The original request that failed with session token unavailable
    ///
    /// # Returns
    /// A `RetryResult`:
    /// - `Retry { after: Duration::ZERO }` if retry is allowed on a different endpoint
    /// - `DoNotRetry` if endpoint discovery is disabled or all endpoints have been tried
    fn should_retry_on_session_not_available(&mut self) -> RetryResult {
        self.session_token_retry_count += 1;

        // If endpoint discovery is disabled, the request cannot be retried anywhere else
        if !self.enable_endpoint_discovery {
            return RetryResult::DoNotRetry;
        }

        if self.can_use_multiple_write_locations {
            let endpoints = self.global_endpoint_manager.applicable_endpoints(
                self.operation_type.unwrap_or(OperationType::Read),
                self.excluded_regions.as_ref(),
            );
            if self.session_token_retry_count > endpoints.len() {
                // When use multiple write locations is true and the request has been tried on all locations, then don't retry the request.
                RetryResult::DoNotRetry
            } else {
                self.retry_context = Some(RetryContext {
                    retry_location_index: self.session_token_retry_count,
                    retry_request_on_preferred_locations: true,
                    route_to_hub: false,
                });

                RetryResult::Retry {
                    after: Duration::ZERO,
                }
            }
        } else if self.session_token_retry_count > 1 {
            // When cannot use multiple write locations, then don't retry the request if
            // we have already tried this request on the write location
            RetryResult::DoNotRetry
        } else {
            self.retry_context = Some(RetryContext {
                retry_location_index: 0,
                retry_request_on_preferred_locations: false,
                route_to_hub: false,
            });

            RetryResult::Retry {
                after: Duration::ZERO,
            }
        }
    }

    /// Determines if a request should be retried after a connection failure.
    ///
    /// Connection failures mean the request was never sent to the server, so both
    /// reads and writes are safe to retry. The strategy is:
    ///
    /// 1. Retry up to [`MAX_RETRY_COUNT_ON_CONNECTION_FAILURE`] times on the same
    ///    endpoint with a delay — the failure may be transient.
    /// 2. After exhausting local retries, mark the endpoint unavailable for both
    ///    reads and writes, refresh the location cache, and fail over to the next
    ///    available endpoint.
    async fn should_retry_on_connection_failure(&mut self) -> RetryResult {
        self.connection_retry_count += 1;

        if self.connection_retry_count <= MAX_RETRY_COUNT_ON_CONNECTION_FAILURE {
            // Retry on the same endpoint — the connection failure may be transient.
            return RetryResult::Retry {
                after: Duration::milliseconds(RETRY_INTERVAL_MS),
            };
        }

        // Exhausted local retries — mark endpoint unavailable and fail over.
        if let Some(ref endpoint) = self.location_endpoint {
            self.global_endpoint_manager
                .mark_endpoint_unavailable_for_read(endpoint);
            self.global_endpoint_manager
                .mark_endpoint_unavailable_for_write(endpoint);
        }

        self.failover_retry_count += 1;
        if self.failover_retry_count > MAX_RETRY_COUNT_ON_ENDPOINT_FAILURE
            || !self.enable_endpoint_discovery
        {
            return RetryResult::DoNotRetry;
        }

        _ = self.global_endpoint_manager.refresh_location(true).await;

        // Reset connection retry counter for the new endpoint.
        self.connection_retry_count = 0;

        self.retry_context = Some(RetryContext {
            retry_location_index: 0,
            retry_request_on_preferred_locations: true,
            route_to_hub: false,
        });

        RetryResult::Retry {
            after: Duration::ZERO,
        }
    }

    /// Determines if a request should be retried when an endpoint fails.
    ///
    /// # Summary
    /// Handles endpoint failures by marking failed endpoints as unavailable and attempting retry
    /// on alternative endpoints. Refreshes the location cache to get updated endpoint information
    /// and configures retry delays based on request type (write requests get longer delays).
    /// Respects maximum retry limits and endpoint discovery settings. Can mark endpoints as
    /// unavailable for reads, writes, or both depending on the failure scenario.
    ///
    /// # Arguments
    /// * `is_read_request` - Whether this is a read operation
    /// * `mark_both_read_and_write_as_unavailable` - Whether to mark the endpoint unavailable for both operations
    /// * `force_refresh` - Whether to force refresh of the location cache
    /// * `retry_on_preferred_locations` - Whether to retry on preferred locations first
    /// * `overwrite_endpoint_discovery` - Whether to bypass endpoint discovery checks
    ///
    /// # Returns
    /// A `RetryResult`:
    /// - `Retry { after: Duration }` with appropriate delay if retry is allowed
    /// - `DoNotRetry` if max retry count exceeded or endpoint discovery disabled
    async fn should_retry_on_endpoint_failure(
        &mut self,
        is_read_request: bool,
        mark_both_read_and_write_as_unavailable: bool,
        force_refresh: bool,
        retry_on_preferred_locations: bool,
        overwrite_endpoint_discovery: bool,
    ) -> RetryResult {
        if self.failover_retry_count > MAX_RETRY_COUNT_ON_ENDPOINT_FAILURE
            || (!self.enable_endpoint_discovery && !overwrite_endpoint_discovery)
        {
            return RetryResult::DoNotRetry;
        }

        self.failover_retry_count += 1;

        if let Some(ref endpoint) = self.location_endpoint {
            if !overwrite_endpoint_discovery {
                if is_read_request || mark_both_read_and_write_as_unavailable {
                    self.global_endpoint_manager
                        .mark_endpoint_unavailable_for_read(endpoint);
                }
                if !is_read_request || mark_both_read_and_write_as_unavailable {
                    self.global_endpoint_manager
                        .mark_endpoint_unavailable_for_write(endpoint);
                }
            }
        }

        let retry_delay = if !is_read_request {
            if self.failover_retry_count > 1 {
                Duration::milliseconds(RETRY_INTERVAL_MS)
            } else {
                Duration::ZERO
            }
        } else {
            Duration::milliseconds(RETRY_INTERVAL_MS)
        };

        // Ideally, any request flow should not be blocked by the outcome of refresh_location.
        // There can be three possible cases:
        // a) The refresh_location succeeds when TTL expires.
        // b) The refresh_location is bypassed when TTL hasn't expired.
        // c) The refresh_location operation has failed. In the event of a failure,
        //    the error is logged and the request should not be blocked.
        // Hence, the outcome of the operation is ignored here.
        _ = self
            .global_endpoint_manager
            .refresh_location(force_refresh)
            .await;
        let retry_location_index = if retry_on_preferred_locations {
            0
        } else {
            self.failover_retry_count
        };

        self.retry_context = Some(RetryContext {
            retry_location_index,
            retry_request_on_preferred_locations: retry_on_preferred_locations,
            route_to_hub: false,
        });

        RetryResult::Retry { after: retry_delay }
    }

    /// Determines if a request should be retried for service unavailable status codes.
    ///
    /// # Summary
    /// Handles 503 (ServiceUnavailable), 500 (InternalServerError for reads), and 410 with
    /// LeaseNotFound errors by attempting retry on all applicable endpoints (all regions minus
    /// excluded regions, in preference of preferred regions). Requires multi-write support for
    /// write operations. Configures retry context to route to the next preferred location.
    ///
    /// # Returns
    /// A `RetryResult`:
    /// - `Retry { after: Duration::ZERO }` if retry conditions are met
    /// - `DoNotRetry` if all endpoints tried or write without multi-write support
    fn should_retry_on_unavailable_endpoint_status_codes(&mut self) -> RetryResult {
        self.service_unavailable_retry_count += 1;

        if !self.can_use_multiple_write_locations
            && !self
                .operation_type
                .as_ref()
                .is_some_and(|op| op.is_read_only())
        {
            return RetryResult::DoNotRetry;
        }

        // automatic failover support needed to be plugged in.
        if !self.can_use_multiple_write_locations
            && !self.is_read_only()
            && !self
                .partition_key_range_location_cache
                .partition_level_automatic_failover_enabled()
        {
            return RetryResult::DoNotRetry;
        }

        let endpoints = self
            .global_endpoint_manager
            .applicable_endpoints(self.operation_type.unwrap(), self.excluded_regions.as_ref());

        if self.service_unavailable_retry_count > endpoints.len() {
            return RetryResult::DoNotRetry;
        }

        self.retry_context = Some(RetryContext {
            retry_location_index: self.service_unavailable_retry_count,
            retry_request_on_preferred_locations: true,
            route_to_hub: false,
        });

        RetryResult::Retry {
            after: Duration::ZERO,
        }
    }

    /// Routes HTTP status codes to appropriate retry handling logic.
    ///
    /// # Summary
    /// Evaluates HTTP status code and Cosmos DB sub-status code combinations to determine
    /// the appropriate retry strategy. Handles specific scenarios: 403.3 (WriteForbidden)
    /// triggers endpoint failover with cache refresh, 404.1022 (READ_SESSION_NOT_AVAILABLE)
    /// retries on different endpoints, 503 (ServiceUnavailable) attempts preferred location
    /// failover, and 500/408/410 with LeaseNotFound retry on alternative endpoints for reads.
    ///
    /// For read operations, any status code that is not considered non-retryable by
    /// [`is_non_retryable_status_code`] is retried on an alternative endpoint. For write
    /// operations, unhandled status codes are delegated to the throttling policy.
    ///
    /// # Arguments
    /// * `status_code` - The HTTP status code from the response
    /// * `sub_status_code` - The Cosmos DB specific sub-status code
    ///
    /// # Returns
    /// An `Option<RetryResult>`:
    /// - `Some(RetryResult)` if the status code requires special retry handling
    /// - `None` if the status code should be delegated to the throttling policy
    async fn should_retry_on_http_status(
        &mut self,
        status_code: StatusCode,
        sub_status_code: Option<SubStatusCode>,
    ) -> Option<RetryResult> {
        // Forbidden - Write forbidden (403.3)
        if status_code == StatusCode::Forbidden
            && sub_status_code == Some(SubStatusCode::WRITE_FORBIDDEN)
        {
            if self.request.is_some()
                && (self.is_request_eligible_for_per_partition_automatic_failover()
                    || self.increment_failure_counter_and_check_circuit_breaker_eligibility())
                && self
                    .partition_key_range_location_cache
                    .try_mark_endpoint_unavailable_for_partition_key_range(
                        self.request.as_ref().unwrap(),
                    )
            {
                return Some(RetryResult::Retry {
                    after: Duration::ZERO,
                });
            }

            return Some(
                self.should_retry_on_endpoint_failure(false, false, true, false, false)
                    .await,
            );
        }

        // Read Session Not Available (404.1022)
        if status_code == StatusCode::NotFound
            && sub_status_code == Some(SubStatusCode::READ_SESSION_NOT_AVAILABLE)
        {
            return Some(self.should_retry_on_session_not_available());
        }

        if self.should_mark_endpoint_unavailable_on_system_resource_unavailable_for_write(
            Some(status_code),
            sub_status_code,
        ) {
            error!(
                "Operation will NOT be retried on local region. \
                     Treating SystemResourceUnavailable (429/3092) as ServiceUnavailable (503). \
                     Status code: 429, sub status code: 3092"
            );

            return Some(
                self.try_mark_endpoint_unavailable_for_pk_range_and_retry_on_service_unavailable(
                    true,
                ),
            );
        }

        // Service unavailable (503)
        if status_code == StatusCode::ServiceUnavailable {
            return Some(
                self.try_mark_endpoint_unavailable_for_pk_range_and_retry_on_service_unavailable(
                    false,
                ),
            );
        }

        // Gone - Lease not found (410.1022) applies to both reads and writes
        if status_code == StatusCode::Gone
            && sub_status_code == Some(SubStatusCode::LEASE_NOT_FOUND)
        {
            return Some(self.should_retry_on_unavailable_endpoint_status_codes());
        }

        // For read operations, retry on any status code that is not explicitly non-retryable.
        // This ensures transient server errors are retried on alternative endpoints.
        if self.is_read_only() && !is_non_retryable_status_code(status_code, sub_status_code) {
            return Some(self.should_retry_on_unavailable_endpoint_status_codes());
        }

        None
    }

    /// Marks endpoint unavailable for partition key range and retries on service unavailable.
    fn try_mark_endpoint_unavailable_for_pk_range_and_retry_on_service_unavailable(
        &mut self,
        is_system_resource_unavailable_for_write: bool,
    ) -> RetryResult {
        self.try_mark_endpoint_unavailable_for_pk_range(is_system_resource_unavailable_for_write);
        self.should_retry_on_unavailable_endpoint_status_codes()
    }

    /// Attempts to mark the endpoint unavailable for the partition key range.
    fn try_mark_endpoint_unavailable_for_pk_range(
        &self,
        is_system_resource_unavailable_for_write: bool,
    ) -> bool {
        if let Some(request) = self.request.as_ref() {
            if is_system_resource_unavailable_for_write
                || self.is_request_eligible_for_per_partition_automatic_failover()
                || self.increment_failure_counter_and_check_circuit_breaker_eligibility()
            {
                return self
                    .partition_key_range_location_cache
                    .try_mark_endpoint_unavailable_for_partition_key_range(request);
            }
        }
        false
    }

    /// Checks if endpoint should be marked unavailable on system resource unavailable for write.
    fn should_mark_endpoint_unavailable_on_system_resource_unavailable_for_write(
        &self,
        status_code: Option<StatusCode>,
        sub_status_code: Option<SubStatusCode>,
    ) -> bool {
        self.can_use_multiple_write_locations
            && status_code == Some(StatusCode::TooManyRequests)
            && sub_status_code == Some(SubStatusCode::SYSTEM_RESOURCE_NOT_AVAILABLE)
    }

    /// Checks if request is eligible for per-partition automatic failover.
    fn is_request_eligible_for_per_partition_automatic_failover(&self) -> bool {
        if let Some(request) = self.request.as_ref() {
            return self
                .partition_key_range_location_cache
                .is_request_eligible_for_per_partition_automatic_failover(request);
        }
        false
    }

    /// Increments failure counter and checks if request is eligible for partition-level circuit breaker.
    fn increment_failure_counter_and_check_circuit_breaker_eligibility(&self) -> bool {
        if let Some(request) = self.request.as_ref() {
            return self
                .partition_key_range_location_cache
                .is_request_eligible_for_partition_level_circuit_breaker(request)
                && self
                    .partition_key_range_location_cache
                    .increment_request_failure_counter_and_check_if_partition_can_failover(
                        request,
                    );
        }
        false
    }

    /// Evaluates an error to determine if the request should be retried.
    ///
    /// # Summary
    /// First checks the [`RequestSentStatus`] to handle transport-level errors:
    /// - `NotSent`: retries reads and writes (request never reached server).
    /// - `Sent`/`Unknown` with transport errors (`Timeout`, `Io`): retries reads only.
    ///
    /// For HTTP-level errors, delegates to `should_retry_on_http_status` for
    /// scenario-specific retry logic (403.3, 404.1022, 503, 500, 410), then falls
    /// back to the throttling retry policy for 429 (TooManyRequests).
    ///
    /// # Arguments
    /// * `err` - The error that occurred during the request
    ///
    /// # Returns
    /// A `RetryResult` indicating whether to retry and with what delay
    async fn should_retry_error(&mut self, err: &azure_core::Error) -> RetryResult {
        // Determine whether the request was actually sent to the server.
        // This drives the retry decision for transport-level errors:
        // - NotSent: safe to retry reads and writes (request never reached server)
        // - Sent/Unknown: only retry reads (write may have been applied)
        match err.request_sent_status() {
            RequestSentStatus::NotSent => {
                return self.should_retry_on_connection_failure().await;
            }
            RequestSentStatus::Sent | RequestSentStatus::Unknown => {
                if matches!(err.kind(), ErrorKind::Io) {
                    if self.is_read_only() {
                        return self.should_retry_on_unavailable_endpoint_status_codes();
                    }
                    return RetryResult::DoNotRetry;
                }
            }
        }

        let status_code = err.http_status().unwrap_or(StatusCode::UnknownValue(0));
        let sub_status_code = get_substatus_code_from_error(err);

        if let Some(result) = self
            .should_retry_on_http_status(status_code, sub_status_code)
            .await
        {
            return result;
        }

        self.throttling_retry.should_retry_error(err)
    }

    /// Evaluates an HTTP response to determine if the request should be retried.
    ///
    /// # Summary
    /// Extracts HTTP status code and sub-status code from the response and delegates to
    /// `should_retry_on_http_status` for scenario-specific retry logic. If the response
    /// doesn't match any special retry cases (403.3, 404.1022, 503, 500, 410), falls
    /// back to the throttling retry policy which handles 429 (TooManyRequests) responses
    /// with exponential backoff.
    ///
    /// # Arguments
    /// * `response` - The HTTP response received from the service
    ///
    /// # Returns
    /// A `RetryResult` indicating whether to retry and with what delay
    async fn should_retry_response(&mut self, response: &RawResponse) -> RetryResult {
        let status_code = response.status();
        let sub_status_code = get_substatus_code_from_response(response);

        if let Some(result) = self
            .should_retry_on_http_status(status_code, sub_status_code)
            .await
        {
            return result;
        }

        self.throttling_retry.should_retry_response(response)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::AccountRegion;
    use crate::operation_context::OperationType;
    use crate::partition_key::PartitionKey;
    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::headers::Headers;
    use azure_core::http::ClientOptions;
    use azure_core::Bytes;
    use std::sync::Arc;

    fn create_test_endpoint_manager() -> Arc<GlobalEndpointManager> {
        let pipeline = azure_core::http::Pipeline::new(
            option_env!("CARGO_PKG_NAME"),
            option_env!("CARGO_PKG_VERSION"),
            ClientOptions::default(),
            Vec::new(),
            Vec::new(),
            None,
        );

        GlobalEndpointManager::new(
            "https://test.documents.azure.com".parse().unwrap(),
            vec![Region::from("West US"), Region::from("East US")],
            vec![],
            pipeline,
        )
    }

    fn create_test_endpoint_manager_no_locations() -> Arc<GlobalEndpointManager> {
        let pipeline = azure_core::http::Pipeline::new(
            option_env!("CARGO_PKG_NAME"),
            option_env!("CARGO_PKG_VERSION"),
            ClientOptions::default(),
            Vec::new(),
            Vec::new(),
            None,
        );

        GlobalEndpointManager::new(
            "https://test.documents.azure.com".parse().unwrap(),
            vec![],
            vec![],
            pipeline,
        )
    }

    fn create_test_endpoint_manager_with_preferred_locations() -> Arc<GlobalEndpointManager> {
        let pipeline = azure_core::http::Pipeline::new(
            option_env!("CARGO_PKG_NAME"),
            option_env!("CARGO_PKG_VERSION"),
            ClientOptions::default(),
            Vec::new(),
            Vec::new(),
            None,
        );

        GlobalEndpointManager::new(
            "https://test.documents.azure.com".parse().unwrap(),
            vec![Region::EAST_ASIA, Region::WEST_US, Region::NORTH_CENTRAL_US],
            vec![],
            pipeline,
        )
    }

    fn create_test_policy() -> ClientRetryPolicy {
        let manager = create_test_endpoint_manager();
        let partition_manager = GlobalPartitionEndpointManager::new(manager.clone(), false, false);
        ClientRetryPolicy::new(manager, partition_manager, None)
    }

    fn create_test_policy_no_locations() -> ClientRetryPolicy {
        let manager = create_test_endpoint_manager_no_locations();
        let partition_manager = GlobalPartitionEndpointManager::new(manager.clone(), false, false);
        ClientRetryPolicy::new(manager, partition_manager, None)
    }

    fn create_test_policy_with_preferred_locations() -> ClientRetryPolicy {
        let manager = create_test_endpoint_manager_with_preferred_locations();
        let partition_manager = GlobalPartitionEndpointManager::new(manager.clone(), false, false);
        ClientRetryPolicy::new(manager, partition_manager, None)
    }

    fn create_test_request() -> CosmosRequest {
        let resource_link = ResourceLink::root(ResourceType::Documents);
        CosmosRequest::builder(OperationType::Read, resource_link.clone())
            .partition_key(PartitionKey::from("test"))
            .build()
            .unwrap()
    }

    fn create_write_request() -> CosmosRequest {
        let resource_link = ResourceLink::root(ResourceType::Documents);
        CosmosRequest::builder(OperationType::Create, resource_link.clone())
            .partition_key(PartitionKey::from("test"))
            .build()
            .unwrap()
    }

    fn create_raw_response(status_code: StatusCode) -> RawResponse {
        let headers = Headers::new();
        RawResponse::from_bytes(status_code, headers, Bytes::new())
    }

    fn create_raw_response_with_substatus(status_code: StatusCode, substatus: u32) -> RawResponse {
        let mut headers = Headers::new();
        headers.insert("x-ms-substatus", substatus.to_string());
        RawResponse::from_bytes(status_code, headers, Bytes::new())
    }

    fn create_error_with_status(status: StatusCode) -> azure_core::Error {
        let response = create_raw_response(status);
        azure_core::Error::new(
            azure_core::error::ErrorKind::HttpResponse {
                status: response.status(),
                error_code: None,
                raw_response: Some(Box::new(response)),
            },
            "Test error",
        )
    }

    fn create_error_with_substatus(status: StatusCode, substatus: u32) -> azure_core::Error {
        let response = create_raw_response_with_substatus(status, substatus);
        azure_core::Error::new(
            azure_core::error::ErrorKind::HttpResponse {
                status: response.status(),
                error_code: None,
                raw_response: Some(Box::new(response)),
            },
            "Test error with substatus",
        )
    }

    /// Creates a multi-master endpoint manager with two regions (West US + East US),
    /// both configured as write AND read endpoints.
    fn create_multi_master_endpoint_manager() -> Arc<GlobalEndpointManager> {
        let pipeline = azure_core::http::Pipeline::new(
            option_env!("CARGO_PKG_NAME"),
            option_env!("CARGO_PKG_VERSION"),
            ClientOptions::default(),
            Vec::new(),
            Vec::new(),
            None,
        );

        let manager = GlobalEndpointManager::new(
            "https://test.documents.azure.com".parse().unwrap(),
            vec![Region::from("West US"), Region::from("East US")],
            vec![],
            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(),
        };

        // Both regions as write + read → multi-master account
        manager.update_location_cache(vec![west.clone(), east.clone()], vec![west, east]);
        manager
    }

    /// Creates a policy configured for multi-master with PPCB enabled.
    fn create_multi_master_ppcb_policy() -> ClientRetryPolicy {
        let manager = create_multi_master_endpoint_manager();
        // PPAF = false, PPCB = true
        let partition_manager = GlobalPartitionEndpointManager::new(manager.clone(), false, true);
        ClientRetryPolicy::new(manager, partition_manager, None)
    }

    /// Creates a write request with a PartitionKeyRange and routed endpoint,
    /// as the PPCB code path requires both to be present.
    fn create_ppcb_write_request() -> CosmosRequest {
        let resource_link = ResourceLink::root(ResourceType::Documents);
        let mut request = CosmosRequest::builder(OperationType::Create, resource_link)
            .partition_key(PartitionKey::from("test"))
            .build()
            .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.request_context.location_endpoint_to_route =
            Some("https://test-westus.documents.azure.com/".parse().unwrap());
        request
    }

    #[tokio::test]
    async fn test_new_policy_initialization() {
        let policy = create_test_policy();
        assert!(policy.enable_endpoint_discovery);
        assert_eq!(policy.failover_retry_count, 0);
        assert_eq!(policy.session_token_retry_count, 0);
        assert_eq!(policy.service_unavailable_retry_count, 0);
        assert!(!policy.can_use_multiple_write_locations);
        assert!(policy.location_endpoint.is_none());
        assert!(policy.retry_context.is_none());
        assert!(policy.operation_type.is_none());
    }

    #[tokio::test]
    async fn test_retry_context_none_initially() {
        let policy = create_test_policy();
        assert!(policy.retry_context.is_none());
    }

    #[tokio::test]
    async fn test_should_retry_service_unavailable_with_preferred_locations() {
        let mut policy = create_test_policy_with_preferred_locations();
        policy.operation_type = Some(OperationType::Read);
        let error = create_error_with_status(StatusCode::ServiceUnavailable);

        let result = policy.should_retry_error(&error).await;

        match result {
            RetryResult::Retry { after } => {
                assert_eq!(after, Duration::ZERO);
                assert_eq!(policy.service_unavailable_retry_count, 1);
                assert!(policy.retry_context.is_some());
            }
            _ => panic!("Expected retry for ServiceUnavailable with preferred locations"),
        }
    }

    #[tokio::test]
    async fn test_should_retry_service_unavailable_without_preferred_locations() {
        // Even with no preferred locations, applicable_endpoints returns the default endpoint
        let mut policy = create_test_policy_no_locations();
        policy.operation_type = Some(OperationType::Read);
        let error = create_error_with_status(StatusCode::ServiceUnavailable);

        let result = policy.should_retry_error(&error).await;

        match result {
            RetryResult::Retry { after } => {
                assert_eq!(after, Duration::ZERO);
                assert_eq!(policy.service_unavailable_retry_count, 1);
            }
            _ => panic!("Expected retry for ServiceUnavailable (default endpoint available)"),
        }

        // Second attempt should stop — only the default endpoint was available
        let result = policy.should_retry_error(&error).await;
        assert!(
            !result.is_retry(),
            "Expected DoNotRetry after exhausting the single default endpoint"
        );
    }

    #[tokio::test]
    async fn test_should_retry_internal_server_error_for_read() {
        let mut policy = create_test_policy_with_preferred_locations();
        policy.operation_type = Some(OperationType::Read);
        let error = create_error_with_status(StatusCode::InternalServerError);

        let result = policy.should_retry_error(&error).await;

        match result {
            RetryResult::Retry { after } => {
                assert_eq!(after, Duration::ZERO);
                assert_eq!(policy.service_unavailable_retry_count, 1);
            }
            _ => panic!("Expected retry for InternalServerError on read request"),
        }
    }

    #[tokio::test]
    async fn test_should_not_retry_internal_server_error_for_write() {
        let mut policy = create_test_policy_with_preferred_locations();
        policy.operation_type = Some(OperationType::Create);
        let error = create_error_with_status(StatusCode::InternalServerError);

        let result = policy.should_retry_error(&error).await;

        match result {
            RetryResult::DoNotRetry => {}
            _ => panic!("Expected DoNotRetry for InternalServerError on write request"),
        }
    }

    #[tokio::test]
    async fn test_should_retry_gone_with_lease_not_found() {
        let mut policy = create_test_policy_with_preferred_locations();
        policy.operation_type = Some(OperationType::Read);
        let error = create_error_with_substatus(
            StatusCode::Gone,
            SubStatusCode::LEASE_NOT_FOUND.value() as u32,
        );

        let result = policy.should_retry_error(&error).await;

        match result {
            RetryResult::Retry { after } => {
                assert_eq!(after, Duration::ZERO);
                assert_eq!(policy.service_unavailable_retry_count, 1);
            }
            _ => panic!("Expected retry for Gone with LeaseNotFound"),
        }
    }

    #[tokio::test]
    async fn test_should_retry_gone_with_lease_not_found_for_write() {
        let mut policy = create_test_policy_with_preferred_locations();
        policy.operation_type = Some(OperationType::Create);
        policy.can_use_multiple_write_locations = true;
        let error = create_error_with_substatus(
            StatusCode::Gone,
            SubStatusCode::LEASE_NOT_FOUND.value() as u32,
        );

        let result = policy.should_retry_error(&error).await;

        match result {
            RetryResult::Retry { after } => {
                assert_eq!(after, Duration::ZERO);
                assert_eq!(policy.service_unavailable_retry_count, 1);
            }
            _ => panic!("Expected retry for Gone with LeaseNotFound on write"),
        }
    }

    #[tokio::test]
    async fn test_should_retry_write_forbidden() {
        let mut policy = create_test_policy();
        policy.operation_type = Some(OperationType::Create);
        policy.location_endpoint = Some("https://test.documents.azure.com".parse().unwrap());
        let error = create_error_with_substatus(
            StatusCode::Forbidden,
            SubStatusCode::WRITE_FORBIDDEN.value() as u32,
        );

        let result = policy.should_retry_error(&error).await;

        match result {
            RetryResult::Retry { after: _ } => {
                assert_eq!(policy.failover_retry_count, 1);
            }
            _ => panic!("Expected retry for WriteForbidden"),
        }
    }

    #[tokio::test]
    async fn test_should_retry_session_not_available_single_write() {
        let mut policy = create_test_policy();
        policy.enable_endpoint_discovery = true;
        policy.can_use_multiple_write_locations = false;

        let error = create_error_with_substatus(
            StatusCode::NotFound,
            SubStatusCode::READ_SESSION_NOT_AVAILABLE.value() as u32,
        );

        let result = policy.should_retry_error(&error).await;

        match result {
            RetryResult::Retry { after } => {
                assert_eq!(after, Duration::ZERO);
                assert_eq!(policy.session_token_retry_count, 1);
                assert!(policy.retry_context.is_some());
            }
            _ => panic!("Expected retry for READ_SESSION_NOT_AVAILABLE"),
        }
    }

    #[tokio::test]
    async fn test_should_not_retry_session_not_available_when_discovery_disabled() {
        let mut policy = create_test_policy();
        policy.enable_endpoint_discovery = false;

        let error = create_error_with_substatus(
            StatusCode::NotFound,
            SubStatusCode::READ_SESSION_NOT_AVAILABLE.value() as u32,
        );

        let result = policy.should_retry_error(&error).await;

        match result {
            RetryResult::DoNotRetry => {
                assert_eq!(policy.session_token_retry_count, 1);
            }
            _ => panic!("Expected DoNotRetry when endpoint discovery disabled"),
        }
    }

    #[tokio::test]
    async fn test_should_not_retry_session_not_available_after_all_endpoints_tried() {
        let mut policy = create_test_policy();
        policy.enable_endpoint_discovery = true;
        policy.can_use_multiple_write_locations = false;
        policy.operation_type = Some(OperationType::Read);
        // create_test_policy has 2 preferred locations, so set count to 2
        // to simulate all endpoints already tried
        policy.session_token_retry_count = 2;

        let error = create_error_with_substatus(
            StatusCode::NotFound,
            SubStatusCode::READ_SESSION_NOT_AVAILABLE.value() as u32,
        );

        let result = policy.should_retry_error(&error).await;
        match result {
            RetryResult::DoNotRetry => {
                assert_eq!(policy.session_token_retry_count, 3);
            }
            _ => panic!("Expected DoNotRetry after all endpoints tried"),
        }
    }

    #[tokio::test]
    async fn test_should_not_retry_service_unavailable_after_all_endpoints_tried() {
        let mut policy = create_test_policy_with_preferred_locations();
        policy.operation_type = Some(OperationType::Read);
        // applicable_endpoints returns 1 (default endpoint) in test setup,
        // so set count to 1 to simulate exhaustion
        policy.service_unavailable_retry_count = 1;

        let error = create_error_with_status(StatusCode::ServiceUnavailable);

        let result = policy.should_retry_error(&error).await;

        match result {
            RetryResult::DoNotRetry => {
                assert_eq!(policy.service_unavailable_retry_count, 2);
            }
            _ => panic!("Expected DoNotRetry after all endpoints tried"),
        }
    }

    #[tokio::test]
    async fn test_should_not_retry_service_unavailable_for_write_without_multi_write() {
        let mut policy = create_test_policy_with_preferred_locations();
        policy.operation_type = Some(OperationType::Create);
        policy.can_use_multiple_write_locations = false;

        let error = create_error_with_status(StatusCode::ServiceUnavailable);

        let result = policy.should_retry_error(&error).await;

        match result {
            RetryResult::DoNotRetry => {}
            _ => panic!("Expected DoNotRetry for write without multi-write support"),
        }
    }

    #[tokio::test]
    async fn test_should_retry_too_many_requests() {
        let mut policy = create_test_policy();
        let error = create_error_with_status(StatusCode::TooManyRequests);

        let result = policy.should_retry_error(&error).await;

        // TooManyRequests should be delegated to throttling policy
        match result {
            RetryResult::Retry { after: _ } => {}
            _ => panic!("Expected retry for TooManyRequests (throttling)"),
        }
    }

    #[tokio::test]
    async fn test_should_retry_response_service_unavailable() {
        let mut policy = create_test_policy_with_preferred_locations();
        policy.operation_type = Some(OperationType::Read);
        let response = create_raw_response(StatusCode::ServiceUnavailable);

        let result = policy.should_retry_response(&response).await;

        match result {
            RetryResult::Retry { after } => {
                assert_eq!(after, Duration::ZERO);
                assert_eq!(policy.service_unavailable_retry_count, 1);
            }
            _ => panic!("Expected retry for ServiceUnavailable response"),
        }
    }

    #[tokio::test]
    async fn test_should_retry_response_too_many_requests() {
        let mut policy = create_test_policy();
        let response = create_raw_response(StatusCode::TooManyRequests);

        let result = policy.should_retry_response(&response).await;

        // Should be delegated to throttling policy
        match result {
            RetryResult::Retry { after: _ } => {}
            _ => panic!("Expected retry for TooManyRequests response"),
        }
    }

    #[tokio::test]
    async fn test_should_retry_for_error_response() {
        let mut policy = create_test_policy_with_preferred_locations();
        policy.operation_type = Some(OperationType::Read);
        let response = create_raw_response(StatusCode::ServiceUnavailable);
        let result_with_response: azure_core::Result<RawResponse> = Ok(response);

        let retry_result = policy.should_retry(&result_with_response).await;

        match retry_result {
            RetryResult::Retry { after } => {
                assert_eq!(after, Duration::ZERO);
            }
            _ => panic!("Expected retry for error response"),
        }
    }

    #[tokio::test]
    async fn test_should_not_retry_for_success_response() {
        let mut policy = create_test_policy();
        let response = create_raw_response(StatusCode::Ok);
        let result_with_response: azure_core::Result<RawResponse> = Ok(response);

        let retry_result = policy.should_retry(&result_with_response).await;

        match retry_result {
            RetryResult::DoNotRetry => {}
            _ => panic!("Expected DoNotRetry for success response"),
        }
    }

    #[tokio::test]
    async fn test_should_retry_for_transport_error() {
        let mut policy = create_test_policy_with_preferred_locations();
        policy.operation_type = Some(OperationType::Read);
        let error = create_error_with_status(StatusCode::ServiceUnavailable);
        let result_with_error: azure_core::Result<RawResponse> = Err(error);

        let retry_result = policy.should_retry(&result_with_error).await;

        match retry_result {
            RetryResult::Retry { after: _ } => {}
            _ => panic!("Expected retry for transport error"),
        }
    }

    #[tokio::test]
    async fn test_endpoint_failover_increments_count() {
        let mut policy = create_test_policy();
        policy.location_endpoint = Some("https://test.documents.azure.com".parse().unwrap());

        let result = policy
            .should_retry_on_endpoint_failure(true, false, false, false, false)
            .await;

        match result {
            RetryResult::Retry { after: _ } => {
                assert_eq!(policy.failover_retry_count, 1);
                assert!(policy.retry_context.is_some());
            }
            _ => panic!("Expected retry for endpoint failure"),
        }
    }

    #[tokio::test]
    async fn test_endpoint_failover_respects_max_retry_count() {
        let mut policy = create_test_policy();
        policy.failover_retry_count = MAX_RETRY_COUNT_ON_ENDPOINT_FAILURE + 1;

        let result = policy
            .should_retry_on_endpoint_failure(true, false, false, false, false)
            .await;

        match result {
            RetryResult::DoNotRetry => {}
            _ => panic!("Expected DoNotRetry after max failover retries"),
        }
    }

    #[tokio::test]
    async fn test_endpoint_failover_respects_endpoint_discovery_disabled() {
        let mut policy = create_test_policy();
        policy.enable_endpoint_discovery = false;

        let result = policy
            .should_retry_on_endpoint_failure(true, false, false, false, false)
            .await;

        match result {
            RetryResult::DoNotRetry => {}
            _ => panic!("Expected DoNotRetry when endpoint discovery disabled"),
        }
    }

    #[tokio::test]
    async fn test_endpoint_failover_with_overwrite_discovery() {
        let mut policy = create_test_policy();
        policy.enable_endpoint_discovery = false;
        policy.location_endpoint = Some("https://test.documents.azure.com".parse().unwrap());

        let result = policy
            .should_retry_on_endpoint_failure(true, false, false, false, true)
            .await;

        match result {
            RetryResult::Retry { after: _ } => {
                assert_eq!(policy.failover_retry_count, 1);
            }
            _ => panic!("Expected retry when overwrite_endpoint_discovery is true"),
        }
    }

    #[tokio::test]
    async fn test_endpoint_failover_write_delay() {
        let mut policy = create_test_policy();
        policy.location_endpoint = Some("https://test.documents.azure.com".parse().unwrap());
        policy.failover_retry_count = 1;

        let result = policy
            .should_retry_on_endpoint_failure(false, false, false, false, false)
            .await;

        match result {
            RetryResult::Retry { after } => {
                assert_eq!(after, Duration::milliseconds(RETRY_INTERVAL_MS));
                assert_eq!(policy.failover_retry_count, 2);
            }
            _ => panic!("Expected retry with delay for write request"),
        }
    }

    #[tokio::test]
    async fn test_endpoint_failover_first_write_no_delay() {
        let mut policy = create_test_policy();
        policy.location_endpoint = Some("https://test.documents.azure.com".parse().unwrap());

        let result = policy
            .should_retry_on_endpoint_failure(false, false, false, false, false)
            .await;

        match result {
            RetryResult::Retry { after } => {
                assert_eq!(after, Duration::ZERO);
                assert_eq!(policy.failover_retry_count, 1);
            }
            _ => panic!("Expected retry with zero delay for first write failover"),
        }
    }

    #[tokio::test]
    async fn test_endpoint_failover_read_always_has_delay() {
        let mut policy = create_test_policy();
        policy.location_endpoint = Some("https://test.documents.azure.com".parse().unwrap());

        let result = policy
            .should_retry_on_endpoint_failure(true, false, false, false, false)
            .await;

        match result {
            RetryResult::Retry { after } => {
                assert_eq!(after, Duration::milliseconds(RETRY_INTERVAL_MS));
            }
            _ => panic!("Expected retry with delay for read request"),
        }
    }

    #[tokio::test]
    async fn test_before_send_request_sets_read_flag() {
        let mut policy = create_test_policy();
        let mut request = create_test_request();

        policy.before_send_request(&mut request).await;

        assert!(policy.operation_type.is_some());
        assert!(policy.operation_type.unwrap().is_read_only());
    }

    #[tokio::test]
    async fn test_before_send_request_sets_write_flag() {
        let mut policy = create_test_policy();
        let mut request = create_write_request();

        policy.before_send_request(&mut request).await;

        assert!(policy.operation_type.is_some());
        assert!(!policy.operation_type.unwrap().is_read_only());
    }

    #[tokio::test]
    async fn test_retry_context_applied_to_request() {
        let mut policy = create_test_policy();
        policy.retry_context = Some(RetryContext {
            retry_location_index: 1,
            retry_request_on_preferred_locations: true,
            route_to_hub: false,
        });
        let mut request = create_test_request();

        policy.before_send_request(&mut request).await;

        // The retry context should be applied to the request
        assert!(policy.location_endpoint.is_some());
    }

    #[test]
    fn test_retry_context_creation() {
        let ctx = RetryContext {
            retry_location_index: 2,
            retry_request_on_preferred_locations: true,
            route_to_hub: false,
        };

        assert_eq!(ctx.retry_location_index, 2);
        assert!(ctx.retry_request_on_preferred_locations);
        assert!(!ctx.route_to_hub);
    }

    #[test]
    fn test_constants_values() {
        assert_eq!(RETRY_INTERVAL_MS, 1000);
        assert_eq!(MAX_RETRY_COUNT_ON_ENDPOINT_FAILURE, 120);
    }

    #[tokio::test]
    async fn read_retries_on_unknown_server_error() {
        let mut policy = create_test_policy_with_preferred_locations();
        policy.operation_type = Some(OperationType::Read);

        // A non-specific server error (e.g., 502 BadGateway) should be retried for reads
        let error = create_error_with_status(StatusCode::BadGateway);
        let result = policy.should_retry_error(&error).await;

        assert!(
            result.is_retry(),
            "Expected retry for BadGateway on read request"
        );
    }

    #[tokio::test]
    async fn read_does_not_retry_non_retryable_status_codes() {
        for status in [
            StatusCode::BadRequest,
            StatusCode::Unauthorized,
            StatusCode::NotFound,
            StatusCode::MethodNotAllowed,
            StatusCode::Conflict,
            StatusCode::PreconditionFailed,
            StatusCode::PayloadTooLarge,
            StatusCode::Locked,
            constants::RETRY_WITH,
        ] {
            let mut policy = create_test_policy_with_preferred_locations();
            policy.operation_type = Some(OperationType::Read);

            let error = create_error_with_status(status);
            let result = policy.should_retry_error(&error).await;

            assert!(
                !result.is_retry(),
                "Expected DoNotRetry for {status:?} on read request"
            );
        }
    }

    #[tokio::test]
    async fn write_does_not_retry_unknown_server_error() {
        let mut policy = create_test_policy_with_preferred_locations();
        policy.operation_type = Some(OperationType::Create);

        // A non-specific server error should NOT be retried for writes
        let error = create_error_with_status(StatusCode::BadGateway);
        let result = policy.should_retry_error(&error).await;

        assert!(
            !result.is_retry(),
            "Expected DoNotRetry for BadGateway on write request"
        );
    }

    #[tokio::test]
    async fn read_retries_on_forbidden_without_write_forbidden_substatus() {
        let mut policy = create_test_policy_with_preferred_locations();
        policy.operation_type = Some(OperationType::Read);

        // Forbidden without WRITE_FORBIDDEN substatus should be retried for reads
        let error = create_error_with_status(StatusCode::Forbidden);
        let result = policy.should_retry_error(&error).await;

        assert!(
            result.is_retry(),
            "Expected retry for Forbidden (no substatus) on read request"
        );
    }

    fn create_connection_error(message: &str) -> azure_core::Error {
        azure_core::Error::with_message(
            azure_core::error::ErrorKind::Connection,
            message.to_string(),
        )
    }

    fn create_timeout_error(message: &str) -> azure_core::Error {
        azure_core::Error::with_message(azure_core::error::ErrorKind::Io, message.to_string())
    }

    fn create_io_error(message: &str) -> azure_core::Error {
        azure_core::Error::with_message(azure_core::error::ErrorKind::Io, message.to_string())
    }

    #[tokio::test]
    async fn connection_error_retries_read() {
        let mut policy = create_test_policy();
        let mut request = create_test_request();
        policy.before_send_request(&mut request).await;

        let err = create_connection_error("connection refused");
        let result = policy.should_retry(&Err(err)).await;
        assert!(
            result.is_retry(),
            "connection error should retry read requests"
        );
    }

    #[tokio::test]
    async fn connection_error_retries_write() {
        let mut policy = create_test_policy();
        let mut request = create_write_request();
        policy.before_send_request(&mut request).await;

        let err = create_connection_error("connection refused");
        let result = policy.should_retry(&Err(err)).await;
        assert!(
            result.is_retry(),
            "connection error should retry write requests"
        );
    }

    #[tokio::test]
    async fn connection_error_retries_on_same_endpoint() {
        let mut policy = create_test_policy();
        let mut request = create_test_request();
        policy.before_send_request(&mut request).await;

        // First 3 connection errors should retry on the same endpoint.
        for i in 1..=3 {
            let err = create_connection_error("connection refused");
            let result = policy.should_retry(&Err(err)).await;
            assert!(result.is_retry(), "connection attempt {i} should retry");
            assert_eq!(policy.connection_retry_count, i);
            assert_eq!(
                policy.failover_retry_count, 0,
                "should not failover during local retries"
            );
        }
    }

    #[tokio::test]
    async fn connection_error_fails_over_after_max_retries() {
        let mut policy = create_test_policy();
        let mut request = create_test_request();
        policy.before_send_request(&mut request).await;

        // Exhaust local retries.
        for _ in 0..3 {
            let err = create_connection_error("connection refused");
            policy.should_retry(&Err(err)).await;
        }

        // Next connection error should trigger failover.
        let err = create_connection_error("connection refused");
        let result = policy.should_retry(&Err(err)).await;
        assert!(result.is_retry(), "should failover to next endpoint");
        assert_eq!(
            policy.failover_retry_count, 1,
            "failover_retry_count should increment after local retries exhausted"
        );
        assert_eq!(
            policy.connection_retry_count, 0,
            "connection_retry_count should reset for new endpoint"
        );
    }

    #[tokio::test]
    async fn response_timeout_retries_read() {
        let mut policy = create_test_policy();
        let mut request = create_test_request();
        policy.before_send_request(&mut request).await;

        let err = create_timeout_error("response timeout");
        let result = policy.should_retry(&Err(err)).await;
        assert!(
            result.is_retry(),
            "response timeout should retry read requests"
        );
    }

    #[tokio::test]
    async fn response_timeout_does_not_retry_write() {
        let mut policy = create_test_policy();
        let mut request = create_write_request();
        policy.before_send_request(&mut request).await;

        let err = create_timeout_error("response timeout");
        let result = policy.should_retry(&Err(err)).await;
        assert_eq!(
            result,
            RetryResult::DoNotRetry,
            "response timeout should NOT retry write requests"
        );
    }

    #[tokio::test]
    async fn response_timeout_read_uses_service_unavailable_counter() {
        let mut policy = create_test_policy();
        let mut request = create_test_request();
        policy.before_send_request(&mut request).await;

        let err = create_timeout_error("response timeout");
        let result = policy.should_retry(&Err(err)).await;
        assert!(result.is_retry());
        assert_eq!(
            policy.service_unavailable_retry_count, 1,
            "service_unavailable_retry_count should increment on response timeout for reads"
        );
    }

    #[tokio::test]
    async fn unknown_io_error_retries_read() {
        let mut policy = create_test_policy();
        let mut request = create_test_request();
        policy.before_send_request(&mut request).await;

        let err = create_io_error("some unrelated IO error");
        let result = policy.should_retry(&Err(err)).await;
        assert!(
            result.is_retry(),
            "unknown IO errors should retry read requests"
        );
    }

    #[tokio::test]
    async fn unknown_io_error_does_not_retry_write() {
        let mut policy = create_test_policy();
        let mut request = create_write_request();
        policy.before_send_request(&mut request).await;

        let err = create_io_error("some unrelated IO error");
        let result = policy.should_retry(&Err(err)).await;
        assert_eq!(
            result,
            RetryResult::DoNotRetry,
            "unknown IO errors should not retry write requests"
        );
    }

    /// **Core bug-fix test.** On a multi-master account with PPCB enabled, a write
    /// receiving 403/3 must reach `is_request_eligible_for_partition_level_circuit_breaker`,
    /// which internally calls `increment_request_failure_counter_and_check_if_partition_can_failover`.
    ///
    /// Before the fix, the outer `partition_level_failover_enabled()` guard short-circuited
    /// on multi-master accounts, so the counter was never incremented and partition-level
    /// failover could never trigger. After the fix, each 403/3 increments the counter.
    #[tokio::test]
    async fn write_forbidden_on_multi_master_increments_ppcb_counter() {
        let mut policy = create_multi_master_ppcb_policy();
        let request = create_ppcb_write_request();

        policy.operation_type = Some(OperationType::Create);
        policy.can_use_multiple_write_locations = true;
        policy.location_endpoint = Some("https://test-westus.documents.azure.com".parse().unwrap());
        policy.request = Some(request);

        // Call the method that the 403/3 handler now invokes.
        // Default write threshold = 5, so each of the first 5 calls should return false
        // (counter not yet exceeded), but still increment the counter.
        for i in 1..=5 {
            let result = policy.increment_failure_counter_and_check_circuit_breaker_eligibility();
            assert!(
                !result,
                "Attempt {i}: Expected false (counter {i} <= threshold 5)"
            );
        }

        // The 6th call should exceed the threshold (6 > 5) and return true
        let result = policy.increment_failure_counter_and_check_circuit_breaker_eligibility();
        assert!(
            result,
            "Expected true after exceeding write failure threshold (6 > 5)"
        );
    }

    /// On a multi-master account with PPCB enabled, once the write failure count
    /// exceeds the threshold (default: 5), the next 403/3 should trigger partition-level
    /// failover: the partition is marked unavailable and the retry is immediate
    /// (Duration::ZERO) without going through account-level failover.
    #[tokio::test]
    async fn write_forbidden_on_multi_master_triggers_partition_failover_after_threshold() {
        let mut policy = create_multi_master_ppcb_policy();
        let request = create_ppcb_write_request();

        policy.operation_type = Some(OperationType::Create);
        policy.can_use_multiple_write_locations = true;
        policy.location_endpoint = Some("https://test-westus.documents.azure.com".parse().unwrap());
        policy.request = Some(request);

        // Pre-pump the PPCB counter to just below threshold (5 increments).
        // Each call to increment_failure_counter_and_check_circuit_breaker_eligibility()
        // increments the counter via increment_request_failure_counter_and_check_if_partition_can_failover.
        for _ in 1..=5 {
            let _ = policy.increment_failure_counter_and_check_circuit_breaker_eligibility();
        }

        let failover_count_before = policy.failover_retry_count;

        // Now call should_retry_error with a 403/3 error.
        // The 6th increment will exceed the threshold → the PPCB path returns
        // Retry { after: ZERO } immediately, without calling should_retry_on_endpoint_failure
        // (and thus without any HTTP calls).
        let error = create_error_with_substatus(
            StatusCode::Forbidden,
            SubStatusCode::WRITE_FORBIDDEN.value() as u32,
        );

        let result = policy.should_retry_error(&error).await;

        assert!(
            result.is_retry(),
            "Expected retry after PPCB threshold exceeded"
        );

        // The partition-level path returns Retry { after: ZERO } directly,
        // WITHOUT going through should_retry_on_endpoint_failure, so
        // failover_retry_count should NOT have increased.
        assert_eq!(
            policy.failover_retry_count, failover_count_before,
            "failover_retry_count should NOT increment when partition-level failover succeeds \
             (partition path bypasses account-level retry)"
        );
    }

    /// Without PPCB enabled, `is_request_eligible_for_partition_level_circuit_breaker`
    /// should always return false, meaning partition-level failover never triggers
    /// for 403/3 on multi-master accounts.
    #[tokio::test]
    async fn write_forbidden_on_multi_master_without_ppcb_returns_ineligible() {
        let manager = create_multi_master_endpoint_manager();
        // PPAF = false, PPCB = false
        let partition_manager = GlobalPartitionEndpointManager::new(manager.clone(), false, false);
        let mut policy = ClientRetryPolicy::new(manager, partition_manager, None);

        let request = create_ppcb_write_request();
        policy.operation_type = Some(OperationType::Create);
        policy.can_use_multiple_write_locations = true;
        policy.location_endpoint = Some("https://test-westus.documents.azure.com".parse().unwrap());
        policy.request = Some(request);

        // Even after many calls, should always return false (PPCB disabled)
        for i in 1..=8 {
            let result = policy.increment_failure_counter_and_check_circuit_breaker_eligibility();
            assert!(!result, "Attempt {i}: Expected false when PPCB is disabled");
        }
    }
}