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
// SPDX-License-Identifier: Apache-2.0
//! Mirror catalog types shared between the catalog layer and the control plane.
//!
//! A mirror database is a continuously-updated read-only replica of a source
//! database in another cluster. Promotion to writable is one-way and permanent.
//! Exhaustive matches are required everywhere these enums are matched — no
//! `_ =>` arms.
use serde::{Deserialize, Serialize};
use crate::{DatabaseId, Lsn};
/// Identifies the source of a mirror database.
///
/// Stored on `DatabaseDescriptor.mirror_origin`; the read planner and write
/// rejector consult it to enforce mirror semantics.
#[derive(
Debug,
Clone,
PartialEq,
Eq,
Serialize,
Deserialize,
zerompk::ToMessagePack,
zerompk::FromMessagePack,
)]
#[msgpack(map)]
pub struct MirrorOrigin {
/// The cluster that the source database lives in.
pub source_cluster: String,
/// The source database being mirrored.
pub source_database: DatabaseId,
/// Replication mode: whether the source waits for mirror ack.
pub mode: MirrorMode,
/// WAL LSN last applied on this mirror.
pub last_applied: Lsn,
/// Current mirror lifecycle status.
pub status: MirrorStatus,
}
/// Replication mode for a mirror database.
///
/// Exhaustive matches are required everywhere this enum is matched — no
/// `_ =>` arms.
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
Serialize,
Deserialize,
zerompk::ToMessagePack,
zerompk::FromMessagePack,
)]
pub enum MirrorMode {
/// Source waits for mirror ack before commit. Strict latency cost;
/// not recommended cross-region.
Sync,
/// Mirror trails source; lag is observable via `MirrorStatus::Degraded`.
Async,
}
/// Lifecycle status of a mirror database.
///
/// Exhaustive matches are required everywhere this enum is matched — no
/// `_ =>` arms.
#[derive(
Debug,
Clone,
PartialEq,
Eq,
Serialize,
Deserialize,
zerompk::ToMessagePack,
zerompk::FromMessagePack,
)]
pub enum MirrorStatus {
/// Initial snapshot transfer is in progress.
Bootstrapping {
/// Bytes of snapshot data received so far.
bytes_done: u64,
/// Total snapshot size in bytes (0 = unknown).
bytes_total: u64,
},
/// Log replication is active; mirror is caught up within normal lag bounds.
Following,
/// Mirror is receiving entries but has fallen behind the lag threshold.
Degraded {
/// Observed replication lag in milliseconds.
lag_ms: u64,
},
/// Source is unreachable; mirror is serving stale reads with growing lag.
Disconnected,
/// Mirror was promoted to a writable database. Source link is severed.
/// `mirror_origin` is retained as a historical lineage record.
Promoted,
}
/// Per-mirror lag record persisted in `_system.mirror_lag`.
///
/// Read by the metrics collector and the `SHOW DATABASE MIRROR STATUS` handler
/// to produce the observable replication lag.
#[derive(
Debug,
Clone,
PartialEq,
Eq,
Serialize,
Deserialize,
zerompk::ToMessagePack,
zerompk::FromMessagePack,
)]
#[msgpack(map)]
pub struct MirrorLagRecord {
/// WAL LSN of the last entry successfully applied on this mirror.
pub last_applied_lsn: Lsn,
/// Wall-clock milliseconds (UNIX epoch) when `last_applied_lsn` was applied.
pub last_apply_ms: u64,
}
#[cfg(test)]
mod tests {
use super::*;
fn round_trip_msgpack<T>(val: &T) -> T
where
T: zerompk::ToMessagePack + for<'a> zerompk::FromMessagePack<'a>,
{
let bytes = zerompk::to_msgpack_vec(val).expect("serialize");
zerompk::from_msgpack(&bytes).expect("deserialize")
}
fn round_trip_serde<T>(val: &T) -> T
where
T: Serialize + for<'de> Deserialize<'de>,
{
let json = sonic_rs::to_string(val).expect("serde serialize");
sonic_rs::from_str(&json).expect("serde deserialize")
}
fn sample_origin() -> MirrorOrigin {
MirrorOrigin {
source_cluster: "prod-us".to_string(),
source_database: DatabaseId::DEFAULT,
mode: MirrorMode::Async,
last_applied: Lsn::new(12_345),
status: MirrorStatus::Following,
}
}
#[test]
fn mirror_mode_msgpack_roundtrip() {
assert_eq!(round_trip_msgpack(&MirrorMode::Sync), MirrorMode::Sync);
assert_eq!(round_trip_msgpack(&MirrorMode::Async), MirrorMode::Async);
}
#[test]
fn mirror_mode_serde_roundtrip() {
assert_eq!(round_trip_serde(&MirrorMode::Sync), MirrorMode::Sync);
assert_eq!(round_trip_serde(&MirrorMode::Async), MirrorMode::Async);
}
#[test]
fn mirror_status_following_msgpack() {
let s = MirrorStatus::Following;
assert_eq!(round_trip_msgpack(&s), s);
}
#[test]
fn mirror_status_bootstrapping_msgpack() {
let s = MirrorStatus::Bootstrapping {
bytes_done: 1024,
bytes_total: 4096,
};
assert_eq!(round_trip_msgpack(&s), s);
}
#[test]
fn mirror_status_degraded_msgpack() {
let s = MirrorStatus::Degraded { lag_ms: 7500 };
assert_eq!(round_trip_msgpack(&s), s);
}
#[test]
fn mirror_status_disconnected_msgpack() {
let s = MirrorStatus::Disconnected;
assert_eq!(round_trip_msgpack(&s), s);
}
#[test]
fn mirror_status_promoted_msgpack() {
let s = MirrorStatus::Promoted;
assert_eq!(round_trip_msgpack(&s), s);
}
#[test]
fn mirror_status_serde_roundtrip() {
for s in [
MirrorStatus::Following,
MirrorStatus::Bootstrapping {
bytes_done: 0,
bytes_total: 0,
},
MirrorStatus::Degraded { lag_ms: 5001 },
MirrorStatus::Disconnected,
MirrorStatus::Promoted,
] {
assert_eq!(round_trip_serde(&s), s);
}
}
#[test]
fn mirror_origin_msgpack_roundtrip() {
let o = sample_origin();
assert_eq!(round_trip_msgpack(&o), o);
}
#[test]
fn mirror_origin_serde_roundtrip() {
let o = sample_origin();
assert_eq!(round_trip_serde(&o), o);
}
}