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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//! Partition-level endpoint routing state for PPAF and PPCB.
use std::{
collections::{HashMap, HashSet},
time::{Duration, Instant},
};
use crate::options::{OperationOptionsView, Region};
use super::{partition_key_range_id::PartitionKeyRangeId, CosmosEndpoint};
/// Immutable partition-level endpoint routing state.
///
/// Managed via CAS in `LocationStateStore` alongside `AccountEndpointState`.
/// Mutations create a new instance and swap it atomically via `crossbeam_epoch`.
#[derive(Clone, Debug)]
pub(crate) struct PartitionEndpointState {
/// PPAF map: writes on single-master accounts.
/// Key: partition key range ID.
pub failover_overrides: HashMap<PartitionKeyRangeId, PartitionFailoverEntry>,
/// PPCB map: reads (any account) + writes on multi-master.
/// Key: partition key range ID.
pub circuit_breaker_overrides: HashMap<PartitionKeyRangeId, PartitionFailoverEntry>,
/// PPAF enabled (from `AccountProperties.enable_per_partition_failover_behavior`).
pub per_partition_automatic_failover_enabled: bool,
/// PPCB enabled (from env var + account property).
pub per_partition_circuit_breaker_enabled: bool,
/// Per-`(partition, primary_region)` count of consecutive alternate-region
/// hedge wins: incremented when the alternate
/// hedge attempt finishes before the primary, reset on a direct primary-region
/// win. When the count reaches [`PartitionFailoverConfig::consecutive_hedge_win_threshold`]
/// the partition is tripped by installing an [`HealthStatus::Unhealthy`]
/// entry in [`Self::circuit_breaker_overrides`] (same shape PPCB uses for hard
/// failures), so subsequent reads route away from the degraded primary region.
/// The trip is recovered by the existing PPCB failback sweep — primary wins
/// only reset the counter, never the trip itself.
///
/// `Option<Region>` accommodates default-endpoint accounts whose snapshots
/// do not carry a named region (the counter key is
/// `(partition, primary_region)` with `primary_region` allowed to be
/// absent).
pub consecutive_hedge_wins: HashMap<(PartitionKeyRangeId, Option<Region>), u32>,
/// Configuration read from env vars at construction time.
pub config: PartitionFailoverConfig,
/// Test-only liveness canary. Set by tests that want to observe whether a
/// particular `PartitionEndpointState` instance is dropped (e.g., the
/// `apply_partition` use-after-free regression test). Production code never
/// reads or writes this field; it is `None` in all non-test constructions.
#[cfg(test)]
pub(crate) _test_canary: Option<std::sync::Arc<()>>,
}
impl PartitionEndpointState {
/// Creates a new `PartitionEndpointState` from the given partition failover config.
pub fn new(config: PartitionFailoverConfig) -> Self {
Self {
per_partition_circuit_breaker_enabled: config.circuit_breaker_option_enabled,
failover_overrides: HashMap::new(),
circuit_breaker_overrides: HashMap::new(),
consecutive_hedge_wins: HashMap::new(),
per_partition_automatic_failover_enabled: false,
config,
#[cfg(test)]
_test_canary: None,
}
}
}
impl Default for PartitionEndpointState {
fn default() -> Self {
Self::new(PartitionFailoverConfig::default())
}
}
/// Health status of a partition failover entry.
///
/// Tracks the lifecycle of a failed-over partition through recovery.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum HealthStatus {
/// Partition is failed-over to an alternate region.
/// All requests route to the override endpoint.
Unhealthy,
/// Unavailability window has elapsed. The next single request for this
/// partition is tentatively routed back to the original region as a probe.
ProbeCandidate,
}
/// Per-partition failover entry.
///
/// Immutable value — mutations produce a new instance via CAS.
#[derive(Clone, Debug)]
pub(crate) struct PartitionFailoverEntry {
/// Current endpoint this partition is routed to.
pub current_endpoint: CosmosEndpoint,
/// Original endpoint that first failed (used for failback probing).
pub first_failed_endpoint: CosmosEndpoint,
/// Set of endpoints already tried.
pub failed_endpoints: HashSet<CosmosEndpoint>,
/// Read failure count (not necessarily consecutive — see §13.2).
pub read_failure_count: i32,
/// Write failure count (not necessarily consecutive — see §13.2).
pub write_failure_count: i32,
/// When the first failure occurred (for failback eligibility).
pub first_failure_time: Instant,
/// When the most recent failure occurred (for counter reset).
pub last_failure_time: Instant,
/// Health status for gradual failback (probe-based recovery).
pub health_status: HealthStatus,
/// Per-entry random delay added to `partition_unavailability_duration` before
/// this entry becomes a `ProbeCandidate`. Spreads simultaneously-failed
/// partitions across the failback window so they don't all stampede the
/// recovering region on the same sweep tick (thundering-herd mitigation).
/// Sampled once when the entry is created (and re-sampled on probe failure)
/// from `[0, partition_unavailability_duration / 2]`. PPAF entries always
/// use `Duration::ZERO` since they don't participate in background failback.
pub failback_jitter: Duration,
}
/// Configuration for partition-level failover, read once at construction.
#[derive(Clone, Debug)]
pub(crate) struct PartitionFailoverConfig {
/// Read failures before circuit trips (default: 10).
pub read_failure_threshold: i32,
/// Write failures before circuit trips (default: 5).
pub write_failure_threshold: i32,
/// Window after which failure counters reset (default: 5 minutes).
pub counter_reset_window: Duration,
/// Duration a partition must remain unavailable before failback (default: 5s).
pub partition_unavailability_duration: Duration,
/// Interval for the background failback sweep (default: 300s).
pub failback_sweep_interval: Duration,
/// Whether PPCB is enabled via options (default: false).
pub circuit_breaker_option_enabled: bool,
/// Consecutive alternate-region hedge wins on the same
/// `(partition, primary_region)` pair before PPCB trips the partition.
/// Default `5` matches the .NET v3 SDK convention; override via
/// [`OperationOptions::consecutive_hedge_win_threshold`] or
/// `AZURE_COSMOS_CONSECUTIVE_HEDGE_WIN_THRESHOLD`.
///
/// Cross-region hedging surfaces a steady
/// signal of primary-region degradation when the alternate consistently
/// beats the primary. Tripping the partition routes subsequent requests
/// away from the degraded region until the failback loop allows a probe.
pub consecutive_hedge_win_threshold: u32,
}
impl Default for PartitionFailoverConfig {
fn default() -> Self {
Self {
read_failure_threshold: 10,
write_failure_threshold: 5,
counter_reset_window: Duration::from_secs(5 * 60),
partition_unavailability_duration: Duration::from_secs(5),
failback_sweep_interval: Duration::from_secs(300),
circuit_breaker_option_enabled: false,
consecutive_hedge_win_threshold: 5,
}
}
}
impl PartitionFailoverConfig {
/// Creates a `PartitionFailoverConfig` by resolving values from the
/// layered [`OperationOptionsView`], falling back to compile-time defaults.
///
/// Called once at driver construction time.
pub fn from_options(view: &OperationOptionsView<'_>) -> Self {
let defaults = Self::default();
let read_failure_threshold = view
.circuit_breaker_failure_count_for_reads()
.map(|v| i32::try_from(*v).unwrap_or(i32::MAX))
.unwrap_or(defaults.read_failure_threshold);
let write_failure_threshold = view
.circuit_breaker_failure_count_for_writes()
.map(|v| i32::try_from(*v).unwrap_or(i32::MAX))
.unwrap_or(defaults.write_failure_threshold);
let counter_reset_window_minutes = view
.circuit_breaker_timeout_counter_reset_window_in_minutes()
.map(|v| u64::from(*v))
.unwrap_or(5);
let partition_unavailability_secs = view
.allowed_partition_unavailability_duration_in_seconds()
.map(|v| u64::from(*v))
.unwrap_or(5);
let failback_sweep_secs = view
.ppcb_stale_partition_unavailability_refresh_interval_in_seconds()
.map(|v| u64::from(*v))
.unwrap_or(300);
let circuit_breaker_option_enabled = view
.per_partition_circuit_breaker_enabled()
.copied()
.unwrap_or(false);
let consecutive_hedge_win_threshold = view
.consecutive_hedge_win_threshold()
.copied()
.unwrap_or(defaults.consecutive_hedge_win_threshold);
Self {
read_failure_threshold,
write_failure_threshold,
counter_reset_window: Duration::from_secs(counter_reset_window_minutes.max(1) * 60),
partition_unavailability_duration: Duration::from_secs(
partition_unavailability_secs.max(1),
),
failback_sweep_interval: Duration::from_secs(failback_sweep_secs.max(1)),
circuit_breaker_option_enabled,
consecutive_hedge_win_threshold,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_config_values() {
let config = PartitionFailoverConfig::default();
assert_eq!(config.read_failure_threshold, 10);
assert_eq!(config.write_failure_threshold, 5);
assert_eq!(config.counter_reset_window, Duration::from_secs(300));
assert_eq!(
config.partition_unavailability_duration,
Duration::from_secs(5)
);
assert_eq!(config.failback_sweep_interval, Duration::from_secs(300));
assert!(!config.circuit_breaker_option_enabled);
assert_eq!(config.consecutive_hedge_win_threshold, 5);
}
#[test]
fn default_partition_state() {
let state = PartitionEndpointState::default();
assert!(state.failover_overrides.is_empty());
assert!(state.circuit_breaker_overrides.is_empty());
assert!(state.consecutive_hedge_wins.is_empty());
assert!(!state.per_partition_automatic_failover_enabled);
assert!(!state.per_partition_circuit_breaker_enabled);
assert!(!state.config.circuit_breaker_option_enabled);
}
#[test]
fn from_options_uses_user_supplied_consecutive_hedge_win_threshold() {
use crate::options::{OperationOptions, OperationOptionsView};
// User opts into a more aggressive trip threshold (2 wins instead
// of the default 5).
let op = OperationOptions {
consecutive_hedge_win_threshold: Some(2),
..Default::default()
};
let view = OperationOptionsView::new(None, None, None, Some(&op));
let config = PartitionFailoverConfig::from_options(&view);
assert_eq!(
config.consecutive_hedge_win_threshold, 2,
"consecutive_hedge_win_threshold must be honored from OperationOptions",
);
}
#[test]
fn from_options_falls_back_to_default_consecutive_hedge_win_threshold() {
use crate::options::{OperationOptions, OperationOptionsView};
let op = OperationOptions::default();
let view = OperationOptionsView::new(None, None, None, Some(&op));
let config = PartitionFailoverConfig::from_options(&view);
assert_eq!(
config.consecutive_hedge_win_threshold,
PartitionFailoverConfig::default().consecutive_hedge_win_threshold,
"absent option must fall back to the compile-time default",
);
}
}