@feltdb/core 0.7.2 → 0.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/application-development.d.ts +76 -0
- package/dist/application-development.d.ts.map +1 -0
- package/dist/application-development.js +137 -0
- package/dist/application-manifest.d.ts +5 -1
- package/dist/application-manifest.d.ts.map +1 -1
- package/dist/application-manifest.js +10 -3
- package/dist/canonical-application.d.ts +26 -0
- package/dist/canonical-application.d.ts.map +1 -0
- package/dist/canonical-application.js +45 -0
- package/dist/cli/ai-model-config.js +97 -0
- package/dist/cli/commands.js +485 -30
- package/dist/cli/index.js +1 -1
- package/dist/cli/proposal-client.js +54 -0
- package/dist/cli/publish-engine.js +162 -0
- package/dist/cli/source-sync.js +139 -0
- package/dist/create/cli.js +1 -1
- package/dist/create/create.js +30 -6
- package/dist/create/managed-account.js +14 -7
- package/dist/create/package-versions.js +1 -1
- package/dist/create/server-source/crates/feltdb/src/application.rs +31 -1
- package/dist/create/server-source/crates/feltdb/src/authority_failover.rs +847 -0
- package/dist/create/server-source/crates/feltdb/src/bin/feltdb_node.rs +338 -29
- package/dist/create/server-source/crates/feltdb/src/distributed_transactions.rs +72 -34
- package/dist/create/server-source/crates/feltdb/src/lib.rs +1 -0
- package/dist/create/server-source/crates/feltdb-server/src/identity.rs +46 -3
- package/dist/create/server-source/crates/feltdb-server/src/main.rs +315 -16
- package/dist/create/server-source/crates/feltdb-server/src/tenancy.rs +20 -0
- package/dist/create/template/default-project/README.md +57 -0
- package/dist/create/template/default-project/agents/activity-assistant.ts +18 -0
- package/dist/create/template/default-project/agents/project-assistant.ts +19 -0
- package/dist/create/template/default-project/capabilities/activity-summary.ts +14 -0
- package/dist/create/template/default-project/capabilities/project-search.ts +16 -0
- package/dist/create/template/default-project/capabilities/project-summary.ts +15 -0
- package/dist/create/template/default-project/feltdb.flow +169 -0
- package/dist/create/template/default-project/src/App.tsx +20 -0
- package/dist/create/template/default-project/src/context/AuthContext.tsx +27 -0
- package/dist/create/template/default-project/src/feltdb.ts +87 -0
- package/dist/create/template/default-project/src/index.tsx +14 -0
- package/dist/create/template/default-project/src/pages/Activity.tsx +7 -0
- package/dist/create/template/default-project/src/pages/Agents.tsx +15 -0
- package/dist/create/template/default-project/src/pages/Dashboard.tsx +17 -0
- package/dist/create/template/default-project/src/pages/Invitations.tsx +11 -0
- package/dist/create/template/default-project/src/pages/Projects.tsx +11 -0
- package/dist/create/template/default-project/src/pages/SignIn.tsx +8 -0
- package/dist/create/template/default-project/src/pages/SignUp.tsx +8 -0
- package/dist/create/template/default-project/src/styles-application.css +21 -0
- package/dist/create/template/default-project/src/styles.css +1 -0
- package/dist/create/template/default-project/workflows/agent-assisted-summary.ts +1 -0
- package/dist/create/template/default-project/workflows/invitation.ts +30 -0
- package/dist/create/template/default-project/workflows/project-created.ts +2 -0
- package/dist/db.d.ts +49 -1
- package/dist/db.d.ts.map +1 -1
- package/dist/db.js +77 -0
- package/dist/error-codes.d.ts +20 -1
- package/dist/error-codes.d.ts.map +1 -1
- package/dist/error-codes.js +25 -0
- package/dist/flowspec.d.ts.map +1 -1
- package/dist/flowspec.js +27 -6
- package/dist/http-db.d.ts +15 -2
- package/dist/http-db.d.ts.map +1 -1
- package/dist/http-db.js +113 -5
- package/dist/index-core.d.ts +3 -0
- package/dist/index-core.d.ts.map +1 -1
- package/dist/index-core.js +3 -0
- package/dist/proposal.d.ts +85 -0
- package/dist/proposal.d.ts.map +1 -0
- package/dist/proposal.js +105 -0
- package/dist/studio/app.d.ts +5 -1
- package/dist/studio/app.d.ts.map +1 -1
- package/dist/studio/components/ApplicationDesigner.d.ts +10 -1
- package/dist/studio/components/ApplicationDesigner.d.ts.map +1 -1
- package/dist/studio/components/AskFeltDB.d.ts +20 -0
- package/dist/studio/components/AskFeltDB.d.ts.map +1 -0
- package/dist/studio/components/ProposalBrowser.d.ts +3 -0
- package/dist/studio/components/ProposalBrowser.d.ts.map +1 -0
- package/dist/studio/components/index.d.ts +2 -0
- package/dist/studio/components/index.d.ts.map +1 -1
- package/dist/studio/components/index.js +4 -4
- package/dist/studio/{components-C5p2TfIU.js → components-Bm4sjhcX.js} +531 -192
- package/dist/studio/index.js +184 -172
- package/dist/studio/studio.css +1 -1
- package/dist/studio-app/assets/dist-DA1MYSzp.js +1 -0
- package/dist/studio-app/assets/{feltdb_wasm-DVKsw75S.js → feltdb_wasm-B-Ccgccb.js} +1 -1
- package/dist/studio-app/assets/feltdb_wasm_bg-wH3enJIA.wasm +0 -0
- package/dist/studio-app/assets/{index-C1GWyazR.css → index-BRrmvaKw.css} +1 -1
- package/dist/studio-app/assets/index-CEIqowCh.js +42 -0
- package/dist/studio-app/assets/lib-B2_7fcn7.js +69 -0
- package/dist/studio-app/assets/worker-CARZ5g7K.js +69 -0
- package/dist/studio-app/index.html +2 -2
- package/dist/wasm/feltdb_wasm_bg.wasm +0 -0
- package/dist/workspace/local-development-authority.d.ts +3 -0
- package/dist/workspace/local-development-authority.d.ts.map +1 -1
- package/dist/workspace/local-development-authority.js +62 -1
- package/package.json +1 -1
- package/dist/studio-app/assets/feltdb_wasm_bg-DNyNf0yy.wasm +0 -0
- package/dist/studio-app/assets/index-C71X92EK.js +0 -28
|
@@ -0,0 +1,847 @@
|
|
|
1
|
+
//! Authority Failover Protocol
|
|
2
|
+
//!
|
|
3
|
+
//! This module implements automatic authority election, failover, and recovery
|
|
4
|
+
//! for distributed FeltDB clusters. The design follows these principles:
|
|
5
|
+
//!
|
|
6
|
+
//! 1. **Single Authority**: At any time, exactly one node holds authority.
|
|
7
|
+
//! Operations flow through the authority, which coordinates replication.
|
|
8
|
+
//!
|
|
9
|
+
//! 2. **Fencing**: A stale authority cannot accept writes after a new authority
|
|
10
|
+
//! is elected. Fencing tokens prevent split-brain scenarios.
|
|
11
|
+
//!
|
|
12
|
+
//! 3. **Automatic Failover**: When the authority fails, surviving nodes detect
|
|
13
|
+
//! the failure and elect a new authority without application intervention.
|
|
14
|
+
//!
|
|
15
|
+
//! 4. **Recovery**: A previously-failed authority can rejoin as a replica and
|
|
16
|
+
//! catch up to the current state.
|
|
17
|
+
//!
|
|
18
|
+
//! ## Consistency Contract
|
|
19
|
+
//!
|
|
20
|
+
//! - A write is **committed** when the authority has persisted it durably
|
|
21
|
+
//! and at least one other active replica has acknowledged receipt.
|
|
22
|
+
//! - A committed write survives any single node failure.
|
|
23
|
+
//! - During failover, a brief period of unavailability occurs while
|
|
24
|
+
//! the new authority is elected.
|
|
25
|
+
//! - The protocol prevents two nodes from believing they are authority
|
|
26
|
+
//! simultaneously through monotonic fencing tokens.
|
|
27
|
+
//!
|
|
28
|
+
//! ## Failover Sequence
|
|
29
|
+
//!
|
|
30
|
+
//! ```text
|
|
31
|
+
//! Authority A fails
|
|
32
|
+
//! │
|
|
33
|
+
//! ▼
|
|
34
|
+
//! Replicas detect failure (heartbeat timeout)
|
|
35
|
+
//! │
|
|
36
|
+
//! ▼
|
|
37
|
+
//! Election: highest-priority surviving replica
|
|
38
|
+
//! │
|
|
39
|
+
//! ▼
|
|
40
|
+
//! New authority B claims with fencing token
|
|
41
|
+
//! │
|
|
42
|
+
//! ▼
|
|
43
|
+
//! B broadcasts authority change
|
|
44
|
+
//! │
|
|
45
|
+
//! ▼
|
|
46
|
+
//! Replicas acknowledge new authority
|
|
47
|
+
//! │
|
|
48
|
+
//! ▼
|
|
49
|
+
//! B begins accepting writes
|
|
50
|
+
//! │
|
|
51
|
+
//! ▼
|
|
52
|
+
//! Old authority A rejoins as replica
|
|
53
|
+
//! ```
|
|
54
|
+
|
|
55
|
+
use serde::{Deserialize, Serialize};
|
|
56
|
+
use std::collections::{BTreeMap, HashSet};
|
|
57
|
+
use std::path::{Path, PathBuf};
|
|
58
|
+
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
|
59
|
+
|
|
60
|
+
fn now_ms() -> u64 {
|
|
61
|
+
SystemTime::now()
|
|
62
|
+
.duration_since(UNIX_EPOCH)
|
|
63
|
+
.map(|elapsed| elapsed.as_millis() as u64)
|
|
64
|
+
.unwrap_or(0)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/// Authority role of a node in the cluster.
|
|
68
|
+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
69
|
+
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
70
|
+
pub enum AuthorityRole {
|
|
71
|
+
/// This node is the authority and accepts writes.
|
|
72
|
+
Authority,
|
|
73
|
+
/// This node is a replica and forwards writes to the authority.
|
|
74
|
+
Replica,
|
|
75
|
+
/// This node is a candidate seeking to become authority.
|
|
76
|
+
Candidate,
|
|
77
|
+
/// This node has been fenced (stale authority).
|
|
78
|
+
Fenced,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/// A fencing token that prevents stale authorities from operating.
|
|
82
|
+
///
|
|
83
|
+
/// Monotonically increasing: a higher token always supersedes a lower one.
|
|
84
|
+
/// The fencing token is durably stored and survives restarts.
|
|
85
|
+
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
|
|
86
|
+
pub struct FencingToken(pub u64);
|
|
87
|
+
|
|
88
|
+
impl FencingToken {
|
|
89
|
+
pub fn initial() -> Self {
|
|
90
|
+
Self(1)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
pub fn next(&self) -> Self {
|
|
94
|
+
Self(self.0.saturating_add(1))
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
pub fn supersedes(&self, other: &FencingToken) -> bool {
|
|
98
|
+
self.0 > other.0
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
impl std::fmt::Display for FencingToken {
|
|
103
|
+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
104
|
+
write!(f, "FT:{}", self.0)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/// Record of an authority election.
|
|
109
|
+
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
110
|
+
pub struct AuthorityRecord {
|
|
111
|
+
/// The node ID of the authority.
|
|
112
|
+
pub node_id: String,
|
|
113
|
+
/// The fencing token for this authority term.
|
|
114
|
+
pub fencing_token: FencingToken,
|
|
115
|
+
/// When this authority was elected.
|
|
116
|
+
pub elected_at_ms: u64,
|
|
117
|
+
/// The previous authority (if any).
|
|
118
|
+
pub previous_authority: Option<String>,
|
|
119
|
+
/// Nodes that acknowledged this authority.
|
|
120
|
+
pub acknowledgements: HashSet<String>,
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
impl AuthorityRecord {
|
|
124
|
+
pub fn new(node_id: String, fencing_token: FencingToken, previous: Option<String>) -> Self {
|
|
125
|
+
Self {
|
|
126
|
+
node_id,
|
|
127
|
+
fencing_token,
|
|
128
|
+
elected_at_ms: now_ms(),
|
|
129
|
+
previous_authority: previous,
|
|
130
|
+
acknowledgements: HashSet::new(),
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/// Heartbeat tracking for failure detection.
|
|
136
|
+
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
137
|
+
pub struct HeartbeatState {
|
|
138
|
+
/// Last heartbeat received from each peer.
|
|
139
|
+
pub last_seen: BTreeMap<String, u64>,
|
|
140
|
+
/// Heartbeat interval in milliseconds.
|
|
141
|
+
pub interval_ms: u64,
|
|
142
|
+
/// How many missed heartbeats before a node is considered failed.
|
|
143
|
+
pub failure_threshold: u32,
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
impl HeartbeatState {
|
|
147
|
+
pub fn new(interval_ms: u64, failure_threshold: u32) -> Self {
|
|
148
|
+
Self {
|
|
149
|
+
last_seen: BTreeMap::new(),
|
|
150
|
+
interval_ms,
|
|
151
|
+
failure_threshold,
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/// Record a heartbeat from a peer.
|
|
156
|
+
pub fn record_heartbeat(&mut self, node_id: &str) {
|
|
157
|
+
self.last_seen.insert(node_id.to_string(), now_ms());
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/// Check if a node appears to have failed.
|
|
161
|
+
pub fn is_failed(&self, node_id: &str) -> bool {
|
|
162
|
+
let Some(last) = self.last_seen.get(node_id) else {
|
|
163
|
+
// Never seen = not yet known, not necessarily failed
|
|
164
|
+
return false;
|
|
165
|
+
};
|
|
166
|
+
let deadline = self.interval_ms * self.failure_threshold as u64;
|
|
167
|
+
now_ms().saturating_sub(*last) > deadline
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/// Get all nodes that appear to have failed.
|
|
171
|
+
pub fn failed_nodes(&self) -> Vec<String> {
|
|
172
|
+
self.last_seen
|
|
173
|
+
.keys()
|
|
174
|
+
.filter(|node_id| self.is_failed(node_id))
|
|
175
|
+
.cloned()
|
|
176
|
+
.collect()
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/// Election priority for a node.
|
|
181
|
+
/// Higher priority wins the election when multiple candidates exist.
|
|
182
|
+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
|
183
|
+
pub struct ElectionPriority {
|
|
184
|
+
/// Operations applied (higher is better - more up-to-date).
|
|
185
|
+
pub operations_applied: u64,
|
|
186
|
+
/// Static priority assigned to the node (for tie-breaking).
|
|
187
|
+
pub static_priority: u32,
|
|
188
|
+
/// Node ID (for deterministic tie-breaking).
|
|
189
|
+
pub node_id: String,
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
impl ElectionPriority {
|
|
193
|
+
pub fn new(operations_applied: u64, static_priority: u32, node_id: String) -> Self {
|
|
194
|
+
Self {
|
|
195
|
+
operations_applied,
|
|
196
|
+
static_priority,
|
|
197
|
+
node_id,
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/// The result of an authority claim attempt.
|
|
203
|
+
#[derive(Clone, Debug)]
|
|
204
|
+
pub enum ClaimResult {
|
|
205
|
+
/// Successfully claimed authority with this fencing token.
|
|
206
|
+
Granted { token: FencingToken },
|
|
207
|
+
/// Another node already has a higher fencing token.
|
|
208
|
+
Rejected {
|
|
209
|
+
current_authority: String,
|
|
210
|
+
current_token: FencingToken,
|
|
211
|
+
},
|
|
212
|
+
/// This node is fenced and cannot claim authority.
|
|
213
|
+
Fenced { token: FencingToken },
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/// Authority state that is durably persisted.
|
|
217
|
+
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
218
|
+
pub struct AuthorityState {
|
|
219
|
+
/// The cluster ID this authority state belongs to.
|
|
220
|
+
pub cluster_id: String,
|
|
221
|
+
/// Current authority record, if any.
|
|
222
|
+
pub current_authority: Option<AuthorityRecord>,
|
|
223
|
+
/// This node's role.
|
|
224
|
+
pub local_role: AuthorityRole,
|
|
225
|
+
/// This node's ID.
|
|
226
|
+
pub local_node_id: String,
|
|
227
|
+
/// The highest fencing token this node has seen.
|
|
228
|
+
pub highest_seen_token: FencingToken,
|
|
229
|
+
/// Election priority for this node.
|
|
230
|
+
pub local_priority: u32,
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
impl AuthorityState {
|
|
234
|
+
pub fn new(cluster_id: String, local_node_id: String, local_priority: u32) -> Self {
|
|
235
|
+
Self {
|
|
236
|
+
cluster_id,
|
|
237
|
+
current_authority: None,
|
|
238
|
+
local_role: AuthorityRole::Replica,
|
|
239
|
+
local_node_id,
|
|
240
|
+
highest_seen_token: FencingToken::initial(),
|
|
241
|
+
local_priority,
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/// Check if this node is currently the authority.
|
|
246
|
+
pub fn is_authority(&self) -> bool {
|
|
247
|
+
self.local_role == AuthorityRole::Authority
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/// Check if this node is fenced.
|
|
251
|
+
pub fn is_fenced(&self) -> bool {
|
|
252
|
+
self.local_role == AuthorityRole::Fenced
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/// Get the current authority node ID.
|
|
256
|
+
pub fn authority_node(&self) -> Option<&str> {
|
|
257
|
+
self.current_authority.as_ref().map(|r| r.node_id.as_str())
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/// Get the current fencing token.
|
|
261
|
+
pub fn current_token(&self) -> FencingToken {
|
|
262
|
+
self.current_authority
|
|
263
|
+
.as_ref()
|
|
264
|
+
.map(|r| r.fencing_token)
|
|
265
|
+
.unwrap_or(FencingToken::initial())
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/// Persistent authority store.
|
|
270
|
+
pub struct AuthorityStore {
|
|
271
|
+
path: PathBuf,
|
|
272
|
+
state: AuthorityState,
|
|
273
|
+
heartbeats: HeartbeatState,
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
#[derive(Debug)]
|
|
277
|
+
pub enum AuthorityError {
|
|
278
|
+
NoCluster,
|
|
279
|
+
AlreadyAuthority(String),
|
|
280
|
+
NotAuthority,
|
|
281
|
+
Fenced(FencingToken),
|
|
282
|
+
StaleToken {
|
|
283
|
+
provided: FencingToken,
|
|
284
|
+
current: FencingToken,
|
|
285
|
+
},
|
|
286
|
+
QuorumUnavailable {
|
|
287
|
+
live: usize,
|
|
288
|
+
required: usize,
|
|
289
|
+
},
|
|
290
|
+
Io(String),
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
impl std::fmt::Display for AuthorityError {
|
|
294
|
+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
295
|
+
match self {
|
|
296
|
+
Self::NoCluster => write!(f, "no cluster has been created"),
|
|
297
|
+
Self::AlreadyAuthority(id) => write!(f, "node {id} is already authority"),
|
|
298
|
+
Self::NotAuthority => write!(f, "this node is not the authority"),
|
|
299
|
+
Self::Fenced(token) => write!(f, "this node is fenced at {token}"),
|
|
300
|
+
Self::StaleToken { provided, current } => {
|
|
301
|
+
write!(f, "stale token {provided}, current is {current}")
|
|
302
|
+
}
|
|
303
|
+
Self::QuorumUnavailable { live, required } => {
|
|
304
|
+
write!(
|
|
305
|
+
f,
|
|
306
|
+
"authority quorum unavailable: {live} live, {required} required"
|
|
307
|
+
)
|
|
308
|
+
}
|
|
309
|
+
Self::Io(msg) => write!(f, "authority store: {msg}"),
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
impl AuthorityStore {
|
|
315
|
+
/// Open or create an authority store.
|
|
316
|
+
pub fn open<P: AsRef<Path>>(
|
|
317
|
+
path: P,
|
|
318
|
+
cluster_id: &str,
|
|
319
|
+
local_node_id: &str,
|
|
320
|
+
local_priority: u32,
|
|
321
|
+
) -> Result<Self, AuthorityError> {
|
|
322
|
+
Self::open_with_failure_detector(path, cluster_id, local_node_id, local_priority, 1_000, 5)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/// Open with an explicitly configured failure detector. Production keeps
|
|
326
|
+
/// the conservative default above; process-level acceptance tests use a
|
|
327
|
+
/// shorter interval so failure is event-driven without multi-second sleeps.
|
|
328
|
+
pub fn open_with_failure_detector<P: AsRef<Path>>(
|
|
329
|
+
path: P,
|
|
330
|
+
cluster_id: &str,
|
|
331
|
+
local_node_id: &str,
|
|
332
|
+
local_priority: u32,
|
|
333
|
+
heartbeat_interval_ms: u64,
|
|
334
|
+
failure_threshold: u32,
|
|
335
|
+
) -> Result<Self, AuthorityError> {
|
|
336
|
+
let path = path.as_ref().to_path_buf();
|
|
337
|
+
let state = if path.exists() {
|
|
338
|
+
let bytes = std::fs::read(&path).map_err(|e| AuthorityError::Io(e.to_string()))?;
|
|
339
|
+
serde_json::from_slice(&bytes).map_err(|e| AuthorityError::Io(e.to_string()))?
|
|
340
|
+
} else {
|
|
341
|
+
AuthorityState::new(
|
|
342
|
+
cluster_id.to_string(),
|
|
343
|
+
local_node_id.to_string(),
|
|
344
|
+
local_priority,
|
|
345
|
+
)
|
|
346
|
+
};
|
|
347
|
+
Ok(Self {
|
|
348
|
+
path,
|
|
349
|
+
state,
|
|
350
|
+
heartbeats: HeartbeatState::new(heartbeat_interval_ms, failure_threshold),
|
|
351
|
+
})
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/// Persist the authority state durably.
|
|
355
|
+
fn persist(&self) -> Result<(), AuthorityError> {
|
|
356
|
+
if let Some(parent) = self.path.parent() {
|
|
357
|
+
std::fs::create_dir_all(parent).map_err(|e| AuthorityError::Io(e.to_string()))?;
|
|
358
|
+
}
|
|
359
|
+
let temp = self.path.with_extension("tmp");
|
|
360
|
+
let bytes = serde_json::to_vec_pretty(&self.state)
|
|
361
|
+
.map_err(|e| AuthorityError::Io(e.to_string()))?;
|
|
362
|
+
{
|
|
363
|
+
use std::io::Write;
|
|
364
|
+
let mut file =
|
|
365
|
+
std::fs::File::create(&temp).map_err(|e| AuthorityError::Io(e.to_string()))?;
|
|
366
|
+
file.write_all(&bytes)
|
|
367
|
+
.map_err(|e| AuthorityError::Io(e.to_string()))?;
|
|
368
|
+
file.sync_all()
|
|
369
|
+
.map_err(|e| AuthorityError::Io(e.to_string()))?;
|
|
370
|
+
}
|
|
371
|
+
std::fs::rename(&temp, &self.path).map_err(|e| AuthorityError::Io(e.to_string()))?;
|
|
372
|
+
if let Some(parent) = self.path.parent() {
|
|
373
|
+
if let Ok(dir) = std::fs::File::open(parent) {
|
|
374
|
+
let _ = dir.sync_all();
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
Ok(())
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/// Get the current authority state.
|
|
381
|
+
pub fn state(&self) -> &AuthorityState {
|
|
382
|
+
&self.state
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/// Record a heartbeat from a peer.
|
|
386
|
+
pub fn heartbeat(&mut self, node_id: &str) {
|
|
387
|
+
self.heartbeats.record_heartbeat(node_id);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/// Check if the current authority has failed.
|
|
391
|
+
pub fn authority_failed(&self) -> bool {
|
|
392
|
+
if let Some(ref authority) = self.state.current_authority {
|
|
393
|
+
if authority.node_id == self.state.local_node_id {
|
|
394
|
+
return false; // We are authority, we haven't failed
|
|
395
|
+
}
|
|
396
|
+
return self.heartbeats.is_failed(&authority.node_id);
|
|
397
|
+
}
|
|
398
|
+
false
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/// Get all nodes that appear to have failed.
|
|
402
|
+
pub fn failed_nodes(&self) -> Vec<String> {
|
|
403
|
+
self.heartbeats.failed_nodes()
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/// Claim authority with a new fencing token.
|
|
407
|
+
///
|
|
408
|
+
/// This node attempts to become the authority. If successful:
|
|
409
|
+
/// - A new fencing token is assigned (one higher than any seen)
|
|
410
|
+
/// - This node becomes authority
|
|
411
|
+
/// - The previous authority (if any) is recorded
|
|
412
|
+
///
|
|
413
|
+
/// If another node has a higher token, the claim is rejected.
|
|
414
|
+
pub fn claim_authority(&mut self) -> Result<ClaimResult, AuthorityError> {
|
|
415
|
+
if self.state.is_fenced() {
|
|
416
|
+
return Ok(ClaimResult::Fenced {
|
|
417
|
+
token: self.state.highest_seen_token,
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// If there's already an authority with a higher token, reject
|
|
422
|
+
if let Some(ref current) = self.state.current_authority {
|
|
423
|
+
if current.node_id != self.state.local_node_id
|
|
424
|
+
&& !self.heartbeats.is_failed(¤t.node_id)
|
|
425
|
+
{
|
|
426
|
+
return Ok(ClaimResult::Rejected {
|
|
427
|
+
current_authority: current.node_id.clone(),
|
|
428
|
+
current_token: current.fencing_token,
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Claim with a new token
|
|
434
|
+
let new_token = self.state.highest_seen_token.next();
|
|
435
|
+
let previous = self
|
|
436
|
+
.state
|
|
437
|
+
.current_authority
|
|
438
|
+
.as_ref()
|
|
439
|
+
.map(|r| r.node_id.clone());
|
|
440
|
+
|
|
441
|
+
let mut record =
|
|
442
|
+
AuthorityRecord::new(self.state.local_node_id.clone(), new_token, previous);
|
|
443
|
+
record
|
|
444
|
+
.acknowledgements
|
|
445
|
+
.insert(self.state.local_node_id.clone());
|
|
446
|
+
|
|
447
|
+
self.state.current_authority = Some(record);
|
|
448
|
+
self.state.local_role = AuthorityRole::Authority;
|
|
449
|
+
self.state.highest_seen_token = new_token;
|
|
450
|
+
|
|
451
|
+
self.persist()?;
|
|
452
|
+
|
|
453
|
+
Ok(ClaimResult::Granted { token: new_token })
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/// Accept a new authority announcement.
|
|
457
|
+
///
|
|
458
|
+
/// If the fencing token is higher than any we've seen, accept the new
|
|
459
|
+
/// authority and update our role accordingly.
|
|
460
|
+
pub fn accept_authority(
|
|
461
|
+
&mut self,
|
|
462
|
+
authority_node: &str,
|
|
463
|
+
token: FencingToken,
|
|
464
|
+
) -> Result<(), AuthorityError> {
|
|
465
|
+
// Check if token is valid
|
|
466
|
+
if !token.supersedes(&self.state.highest_seen_token)
|
|
467
|
+
&& token != self.state.highest_seen_token
|
|
468
|
+
{
|
|
469
|
+
return Err(AuthorityError::StaleToken {
|
|
470
|
+
provided: token,
|
|
471
|
+
current: self.state.highest_seen_token,
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// Update our state
|
|
476
|
+
self.state.highest_seen_token = token;
|
|
477
|
+
|
|
478
|
+
// If we were authority with a lower token, we are now fenced
|
|
479
|
+
if self.state.local_role == AuthorityRole::Authority
|
|
480
|
+
&& authority_node != self.state.local_node_id
|
|
481
|
+
{
|
|
482
|
+
self.state.local_role = AuthorityRole::Fenced;
|
|
483
|
+
} else if authority_node != self.state.local_node_id {
|
|
484
|
+
self.state.local_role = AuthorityRole::Replica;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// Record the new authority
|
|
488
|
+
self.state.current_authority = Some(AuthorityRecord::new(
|
|
489
|
+
authority_node.to_string(),
|
|
490
|
+
token,
|
|
491
|
+
self.state
|
|
492
|
+
.current_authority
|
|
493
|
+
.as_ref()
|
|
494
|
+
.map(|r| r.node_id.clone()),
|
|
495
|
+
));
|
|
496
|
+
|
|
497
|
+
// Start tracking heartbeats from the new authority.
|
|
498
|
+
// This allows failure detection to work even if we've never received
|
|
499
|
+
// a heartbeat yet - the clock starts now.
|
|
500
|
+
if authority_node != self.state.local_node_id {
|
|
501
|
+
self.heartbeats.record_heartbeat(authority_node);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
self.persist()
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/// Acknowledge the current authority.
|
|
508
|
+
pub fn acknowledge_authority(&mut self, from_node: &str) -> Result<(), AuthorityError> {
|
|
509
|
+
if let Some(ref mut authority) = self.state.current_authority {
|
|
510
|
+
authority.acknowledgements.insert(from_node.to_string());
|
|
511
|
+
self.persist()?;
|
|
512
|
+
}
|
|
513
|
+
Ok(())
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/// Validate a write operation against the current fencing token.
|
|
517
|
+
///
|
|
518
|
+
/// Returns Ok if the token is valid (current or not provided for local writes).
|
|
519
|
+
/// Returns Err if the token is stale.
|
|
520
|
+
pub fn validate_write(&self, token: Option<FencingToken>) -> Result<(), AuthorityError> {
|
|
521
|
+
if self.state.is_fenced() {
|
|
522
|
+
return Err(AuthorityError::Fenced(self.state.highest_seen_token));
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
if !self.state.is_authority() {
|
|
526
|
+
return Err(AuthorityError::NotAuthority);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
if let Some(provided) = token {
|
|
530
|
+
if provided != self.state.current_token() {
|
|
531
|
+
return Err(AuthorityError::StaleToken {
|
|
532
|
+
provided,
|
|
533
|
+
current: self.state.current_token(),
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
Ok(())
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/// Validate an authoritative write against both its fencing term and a
|
|
542
|
+
/// live quorum. A partitioned former authority may not have observed the
|
|
543
|
+
/// replacement's newer token yet, so fencing alone is insufficient until
|
|
544
|
+
/// healing; expiry of its quorum evidence closes that split-brain window.
|
|
545
|
+
pub fn validate_quorum_write(
|
|
546
|
+
&self,
|
|
547
|
+
token: FencingToken,
|
|
548
|
+
required_quorum: usize,
|
|
549
|
+
) -> Result<(), AuthorityError> {
|
|
550
|
+
self.validate_write(Some(token))?;
|
|
551
|
+
let live = 1 + self
|
|
552
|
+
.heartbeats
|
|
553
|
+
.last_seen
|
|
554
|
+
.keys()
|
|
555
|
+
.filter(|node_id| !self.heartbeats.is_failed(node_id))
|
|
556
|
+
.count();
|
|
557
|
+
if live < required_quorum {
|
|
558
|
+
return Err(AuthorityError::QuorumUnavailable {
|
|
559
|
+
live,
|
|
560
|
+
required: required_quorum,
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
Ok(())
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/// Step down from authority role (voluntary).
|
|
567
|
+
pub fn step_down(&mut self) -> Result<(), AuthorityError> {
|
|
568
|
+
if self.state.local_role == AuthorityRole::Authority {
|
|
569
|
+
self.state.local_role = AuthorityRole::Replica;
|
|
570
|
+
self.persist()?;
|
|
571
|
+
}
|
|
572
|
+
Ok(())
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/// Recover from fenced state (after catching up).
|
|
576
|
+
pub fn recover_from_fenced(&mut self) -> Result<(), AuthorityError> {
|
|
577
|
+
if self.state.local_role == AuthorityRole::Fenced {
|
|
578
|
+
self.state.local_role = AuthorityRole::Replica;
|
|
579
|
+
self.persist()?;
|
|
580
|
+
}
|
|
581
|
+
Ok(())
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/// Compute election priority for this node.
|
|
585
|
+
pub fn election_priority(&self, operations_applied: u64) -> ElectionPriority {
|
|
586
|
+
ElectionPriority::new(
|
|
587
|
+
operations_applied,
|
|
588
|
+
self.state.local_priority,
|
|
589
|
+
self.state.local_node_id.clone(),
|
|
590
|
+
)
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/// Determine the winner of an election given a set of candidates.
|
|
594
|
+
pub fn elect_authority(candidates: &[ElectionPriority]) -> Option<&ElectionPriority> {
|
|
595
|
+
candidates.iter().max()
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/// Authority announcement message.
|
|
600
|
+
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
601
|
+
pub struct AuthorityAnnouncement {
|
|
602
|
+
pub authority_node: String,
|
|
603
|
+
pub fencing_token: FencingToken,
|
|
604
|
+
pub elected_at_ms: u64,
|
|
605
|
+
pub cluster_id: String,
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/// Heartbeat message.
|
|
609
|
+
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
610
|
+
pub struct Heartbeat {
|
|
611
|
+
pub from_node: String,
|
|
612
|
+
pub is_authority: bool,
|
|
613
|
+
pub fencing_token: FencingToken,
|
|
614
|
+
pub operations_applied: u64,
|
|
615
|
+
pub timestamp_ms: u64,
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
impl Heartbeat {
|
|
619
|
+
pub fn new(
|
|
620
|
+
from_node: String,
|
|
621
|
+
is_authority: bool,
|
|
622
|
+
fencing_token: FencingToken,
|
|
623
|
+
operations_applied: u64,
|
|
624
|
+
) -> Self {
|
|
625
|
+
Self {
|
|
626
|
+
from_node,
|
|
627
|
+
is_authority,
|
|
628
|
+
fencing_token,
|
|
629
|
+
operations_applied,
|
|
630
|
+
timestamp_ms: now_ms(),
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/// Election request message.
|
|
636
|
+
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
637
|
+
pub struct ElectionRequest {
|
|
638
|
+
pub candidate_node: String,
|
|
639
|
+
pub priority: ElectionPriority,
|
|
640
|
+
pub proposed_token: FencingToken,
|
|
641
|
+
pub timestamp_ms: u64,
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/// Election response message.
|
|
645
|
+
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
646
|
+
pub struct ElectionResponse {
|
|
647
|
+
pub from_node: String,
|
|
648
|
+
pub vote_granted: bool,
|
|
649
|
+
pub reason: String,
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
#[cfg(test)]
|
|
653
|
+
mod tests {
|
|
654
|
+
use super::*;
|
|
655
|
+
|
|
656
|
+
#[test]
|
|
657
|
+
fn fencing_token_ordering() {
|
|
658
|
+
let t1 = FencingToken::initial();
|
|
659
|
+
let t2 = t1.next();
|
|
660
|
+
let t3 = t2.next();
|
|
661
|
+
|
|
662
|
+
assert!(t2.supersedes(&t1));
|
|
663
|
+
assert!(t3.supersedes(&t2));
|
|
664
|
+
assert!(t3.supersedes(&t1));
|
|
665
|
+
assert!(!t1.supersedes(&t2));
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
#[test]
|
|
669
|
+
fn authority_claim_and_fencing() {
|
|
670
|
+
let dir = tempfile::tempdir().unwrap();
|
|
671
|
+
let path = dir.path().join("authority.json");
|
|
672
|
+
|
|
673
|
+
// Node A claims authority
|
|
674
|
+
let mut store_a =
|
|
675
|
+
AuthorityStore::open(&path, "test-cluster", "A", 10).expect("open store A");
|
|
676
|
+
let result = store_a.claim_authority().expect("claim");
|
|
677
|
+
match result {
|
|
678
|
+
ClaimResult::Granted { token } => {
|
|
679
|
+
assert_eq!(token, FencingToken(2)); // initial is 1, next is 2
|
|
680
|
+
}
|
|
681
|
+
_ => panic!("expected Granted"),
|
|
682
|
+
}
|
|
683
|
+
assert!(store_a.state().is_authority());
|
|
684
|
+
|
|
685
|
+
// Node B sees A's authority
|
|
686
|
+
let path_b = dir.path().join("authority_b.json");
|
|
687
|
+
let mut store_b =
|
|
688
|
+
AuthorityStore::open(&path_b, "test-cluster", "B", 5).expect("open store B");
|
|
689
|
+
store_b
|
|
690
|
+
.accept_authority("A", FencingToken(2))
|
|
691
|
+
.expect("accept");
|
|
692
|
+
assert!(!store_b.state().is_authority());
|
|
693
|
+
assert_eq!(store_b.state().authority_node(), Some("A"));
|
|
694
|
+
|
|
695
|
+
// Node B tries to claim - should be rejected since A is still up
|
|
696
|
+
let result_b = store_b.claim_authority().expect("claim B");
|
|
697
|
+
match result_b {
|
|
698
|
+
ClaimResult::Rejected {
|
|
699
|
+
current_authority,
|
|
700
|
+
current_token,
|
|
701
|
+
} => {
|
|
702
|
+
assert_eq!(current_authority, "A");
|
|
703
|
+
assert_eq!(current_token, FencingToken(2));
|
|
704
|
+
}
|
|
705
|
+
_ => panic!("expected Rejected"),
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
#[test]
|
|
710
|
+
fn failover_when_authority_dies() {
|
|
711
|
+
let dir = tempfile::tempdir().unwrap();
|
|
712
|
+
|
|
713
|
+
// Node A is authority
|
|
714
|
+
let path_a = dir.path().join("authority_a.json");
|
|
715
|
+
let mut store_a =
|
|
716
|
+
AuthorityStore::open(&path_a, "test-cluster", "A", 10).expect("open store A");
|
|
717
|
+
store_a.claim_authority().expect("claim A");
|
|
718
|
+
|
|
719
|
+
// Node B knows about A
|
|
720
|
+
let path_b = dir.path().join("authority_b.json");
|
|
721
|
+
let mut store_b =
|
|
722
|
+
AuthorityStore::open(&path_b, "test-cluster", "B", 5).expect("open store B");
|
|
723
|
+
store_b
|
|
724
|
+
.accept_authority("A", FencingToken(2))
|
|
725
|
+
.expect("accept A");
|
|
726
|
+
|
|
727
|
+
// Simulate A dying - mark heartbeat as very old
|
|
728
|
+
store_b.heartbeats.last_seen.insert("A".to_string(), 0);
|
|
729
|
+
|
|
730
|
+
// Now B should be able to claim
|
|
731
|
+
assert!(store_b.authority_failed());
|
|
732
|
+
let result_b = store_b.claim_authority().expect("claim B after failover");
|
|
733
|
+
match result_b {
|
|
734
|
+
ClaimResult::Granted { token } => {
|
|
735
|
+
assert_eq!(token, FencingToken(3)); // higher than A's token
|
|
736
|
+
}
|
|
737
|
+
_ => panic!("expected Granted after failover"),
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
#[test]
|
|
742
|
+
fn stale_authority_is_fenced() {
|
|
743
|
+
let dir = tempfile::tempdir().unwrap();
|
|
744
|
+
|
|
745
|
+
// A is authority with token 2
|
|
746
|
+
let path_a = dir.path().join("authority_a.json");
|
|
747
|
+
let mut store_a =
|
|
748
|
+
AuthorityStore::open(&path_a, "test-cluster", "A", 10).expect("open store A");
|
|
749
|
+
store_a.claim_authority().expect("claim A");
|
|
750
|
+
assert!(store_a.state().is_authority());
|
|
751
|
+
|
|
752
|
+
// A receives announcement that B is now authority with token 3
|
|
753
|
+
store_a
|
|
754
|
+
.accept_authority("B", FencingToken(3))
|
|
755
|
+
.expect("accept B");
|
|
756
|
+
|
|
757
|
+
// A should now be fenced
|
|
758
|
+
assert!(store_a.state().is_fenced());
|
|
759
|
+
assert!(!store_a.state().is_authority());
|
|
760
|
+
|
|
761
|
+
// A's writes should be rejected
|
|
762
|
+
let result = store_a.validate_write(None);
|
|
763
|
+
assert!(matches!(result, Err(AuthorityError::Fenced(_))));
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
#[test]
|
|
767
|
+
fn election_priority_ordering() {
|
|
768
|
+
let p1 = ElectionPriority::new(100, 5, "A".to_string());
|
|
769
|
+
let p2 = ElectionPriority::new(100, 10, "B".to_string());
|
|
770
|
+
let p3 = ElectionPriority::new(150, 5, "C".to_string());
|
|
771
|
+
|
|
772
|
+
// Higher operations wins
|
|
773
|
+
assert!(p3 > p1);
|
|
774
|
+
assert!(p3 > p2);
|
|
775
|
+
|
|
776
|
+
// Same operations, higher priority wins
|
|
777
|
+
assert!(p2 > p1);
|
|
778
|
+
|
|
779
|
+
// Election selects highest priority
|
|
780
|
+
let candidates = vec![p1.clone(), p2.clone(), p3.clone()];
|
|
781
|
+
let winner = AuthorityStore::elect_authority(&candidates);
|
|
782
|
+
assert_eq!(winner.map(|w| &w.node_id), Some(&"C".to_string()));
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
#[test]
|
|
786
|
+
fn heartbeat_failure_detection() {
|
|
787
|
+
let mut heartbeats = HeartbeatState::new(1000, 3); // 1s interval, 3 missed = failure
|
|
788
|
+
|
|
789
|
+
// Record a heartbeat
|
|
790
|
+
heartbeats.record_heartbeat("A");
|
|
791
|
+
assert!(!heartbeats.is_failed("A"));
|
|
792
|
+
|
|
793
|
+
// Manually set an old timestamp
|
|
794
|
+
heartbeats.last_seen.insert("A".to_string(), 0);
|
|
795
|
+
|
|
796
|
+
// Should now be detected as failed (current time is way past 3 intervals)
|
|
797
|
+
assert!(heartbeats.is_failed("A"));
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
#[test]
|
|
801
|
+
fn partitioned_authority_loses_write_quorum() {
|
|
802
|
+
let dir = tempfile::tempdir().unwrap();
|
|
803
|
+
let mut store = AuthorityStore::open_with_failure_detector(
|
|
804
|
+
dir.path().join("authority.json"),
|
|
805
|
+
"test-cluster",
|
|
806
|
+
"A",
|
|
807
|
+
10,
|
|
808
|
+
1,
|
|
809
|
+
1,
|
|
810
|
+
)
|
|
811
|
+
.expect("open authority store");
|
|
812
|
+
let token = match store.claim_authority().expect("claim") {
|
|
813
|
+
ClaimResult::Granted { token } => token,
|
|
814
|
+
_ => panic!("authority claim was not granted"),
|
|
815
|
+
};
|
|
816
|
+
store.heartbeat("B");
|
|
817
|
+
assert!(store.validate_quorum_write(token, 2).is_ok());
|
|
818
|
+
store.heartbeats.last_seen.insert("B".to_string(), 0);
|
|
819
|
+
assert!(matches!(
|
|
820
|
+
store.validate_quorum_write(token, 2),
|
|
821
|
+
Err(AuthorityError::QuorumUnavailable {
|
|
822
|
+
live: 1,
|
|
823
|
+
required: 2
|
|
824
|
+
})
|
|
825
|
+
));
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
#[test]
|
|
829
|
+
fn recovery_from_fenced() {
|
|
830
|
+
let dir = tempfile::tempdir().unwrap();
|
|
831
|
+
let path = dir.path().join("authority.json");
|
|
832
|
+
|
|
833
|
+
let mut store = AuthorityStore::open(&path, "test-cluster", "A", 10).expect("open store");
|
|
834
|
+
store.claim_authority().expect("claim");
|
|
835
|
+
|
|
836
|
+
// Get fenced
|
|
837
|
+
store
|
|
838
|
+
.accept_authority("B", FencingToken(3))
|
|
839
|
+
.expect("accept B");
|
|
840
|
+
assert!(store.state().is_fenced());
|
|
841
|
+
|
|
842
|
+
// Recover
|
|
843
|
+
store.recover_from_fenced().expect("recover");
|
|
844
|
+
assert!(!store.state().is_fenced());
|
|
845
|
+
assert_eq!(store.state().local_role, AuthorityRole::Replica);
|
|
846
|
+
}
|
|
847
|
+
}
|