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
// Copyright 2024 foundationdb-rs developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
//! Key management utilities for leader election
//!
//! This module defines the key structure and namespace organization for
//! storing leader election data in FoundationDB. All keys are scoped
//! within a user-provided subspace to allow multiple elections to coexist.
//!
//! # Key Schema
//!
//! ```text
//! <subspace>/config - Election configuration
//! <subspace>/leader - Current leader state (single key, O(1) access)
//! <subspace>/candidates/<id> - Per-candidate registration and heartbeat
//! ```
use crateSubspace;
/// Key prefixes for different data types
pub const CONFIG_PREFIX: &str = "config";
pub const LEADER_PREFIX: &str = "leader";
pub const CANDIDATES_PREFIX: &str = "candidates";
/// Generate the key for global configuration
///
/// Returns the key where election configuration is stored.
///
/// # Key Structure
/// `<subspace>/config`
/// Generate the key for leader state
///
/// Returns the key where current leader information is stored.
/// This is the core "active disk" key for O(1) leadership operations.
///
/// # Key Structure
/// `<subspace>/leader`
///
/// # Contents
/// Stores the current leader's ballot, ID, priority, lease expiry, and versionstamp
/// Generate the key for a specific candidate
///
/// Returns the key for storing a candidate's registration and heartbeat info.
///
/// # Key Structure
/// `<subspace>/candidates/<process_id>`
///
/// # Arguments
/// * `process_id` - Unique identifier for the candidate process
/// Generate the key range for all candidates
///
/// Returns the start and end keys for scanning all registered candidates.
///
/// # Key Structure
/// Range: `<subspace>/candidates/` to `<subspace>/candidates/\xff`
///
/// # Usage
/// Used for listing candidates, evicting dead candidates, or monitoring.
/// Note: Leadership determination is O(1) and doesn't require scanning.