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
// SPDX-License-Identifier: Apache-2.0
//! Clone catalog types shared between the catalog layer and the control plane.
use serde::{Deserialize, Serialize};
use crate::{DatabaseId, Lsn};
/// Maximum clone depth (source → clone → clone-of-clone …).
///
/// A clone at depth 9 is rejected with `CLONE_DEPTH_EXCEEDED`.
pub const MAX_CLONE_DEPTH: u32 = 8;
/// Identifies the source of a copy-on-write database clone.
///
/// Stored on each cloned `StoredCollection`; the read and write planners
/// consult it to decide whether source delegation is needed.
#[derive(
Debug,
Clone,
PartialEq,
Eq,
Serialize,
Deserialize,
zerompk::ToMessagePack,
zerompk::FromMessagePack,
rkyv::Archive,
rkyv::Serialize,
rkyv::Deserialize,
)]
#[msgpack(map)]
pub struct CloneOrigin {
/// The source database this collection was cloned from.
pub source_database: DatabaseId,
/// Collection name in the source database (scoped to `source_database`).
pub source_collection: String,
/// WAL LSN up to which source rows are delegated on reads.
/// Rows in the source with LSN > `as_of_lsn` are invisible through
/// this clone regardless of query time.
pub as_of_lsn: Lsn,
/// WAL LSN at the moment this clone was created. Used to detect
/// bitemporal queries that pre-date the clone.
pub clone_created_at: Lsn,
/// Surrogate high-water captured from the source's `SurrogateAssigner`
/// at clone-create time. KV bindings allocated AFTER this value
/// belong strictly to source-side writes that must not be visible
/// from the resulting clone — the lazy KV read path uses this
/// ceiling to filter source-delegated rows. `None` on legacy clones
/// created before this field existed (no isolation enforced —
/// matches the prior behaviour).
#[serde(default)]
#[msgpack(default)]
pub kv_surrogate_ceiling: Option<u32>,
}
/// Materialization state of a copy-on-write clone.
///
/// Exhaustive matches are required everywhere this enum is matched — no
/// `_ =>` arms.
#[derive(
Debug,
Clone,
PartialEq,
Eq,
Serialize,
Deserialize,
zerompk::ToMessagePack,
zerompk::FromMessagePack,
rkyv::Archive,
rkyv::Serialize,
rkyv::Deserialize,
Default,
)]
pub enum CloneStatus {
/// Reads delegate to source up to `as_of_lsn`; writes go to target.
/// This is the initial state immediately after a `CLONE DATABASE`.
#[default]
Shadowed,
/// Background materializer is copying source rows into target storage.
/// Reads still delegate to source for rows not yet copied.
Materializing {
/// LSN watermark of the materializer's current position in the source.
progress_lsn: Lsn,
/// Bytes materialised so far (best-effort estimate).
bytes_done: u64,
/// Total bytes to materialise (best-effort estimate; 0 = unknown).
bytes_total: u64,
},
/// All source rows are physically present in target storage.
/// Source delegation is no longer needed.
Materialized,
}
#[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() -> CloneOrigin {
CloneOrigin {
source_database: DatabaseId::DEFAULT,
source_collection: "users".to_string(),
as_of_lsn: Lsn::new(42_000),
clone_created_at: Lsn::new(42_100),
kv_surrogate_ceiling: Some(7),
}
}
#[test]
fn clone_status_shadowed_msgpack() {
let s = CloneStatus::Shadowed;
assert_eq!(round_trip_msgpack(&s), s);
}
#[test]
fn clone_status_materializing_msgpack() {
let s = CloneStatus::Materializing {
progress_lsn: Lsn::new(1_000),
bytes_done: 512,
bytes_total: 1_024,
};
assert_eq!(round_trip_msgpack(&s), s);
}
#[test]
fn clone_status_materialized_msgpack() {
let s = CloneStatus::Materialized;
assert_eq!(round_trip_msgpack(&s), s);
}
#[test]
fn clone_status_shadowed_serde() {
let s = CloneStatus::Shadowed;
assert_eq!(round_trip_serde(&s), s);
}
#[test]
fn clone_status_materializing_serde() {
let s = CloneStatus::Materializing {
progress_lsn: Lsn::new(1_000),
bytes_done: 512,
bytes_total: 1_024,
};
assert_eq!(round_trip_serde(&s), s);
}
#[test]
fn clone_status_materialized_serde() {
let s = CloneStatus::Materialized;
assert_eq!(round_trip_serde(&s), s);
}
#[test]
fn clone_origin_msgpack() {
let o = sample_origin();
assert_eq!(round_trip_msgpack(&o), o);
}
#[test]
fn clone_origin_serde() {
let o = sample_origin();
assert_eq!(round_trip_serde(&o), o);
}
#[test]
fn max_clone_depth_value() {
assert_eq!(MAX_CLONE_DEPTH, 8);
}
#[test]
fn clone_status_default_is_shadowed() {
assert_eq!(CloneStatus::default(), CloneStatus::Shadowed);
}
}