Line data Source code
1 : //! Code to deal with safekeeper control file upgrades
2 : use crate::{
3 : safekeeper::{AcceptorState, PgUuid, ServerInfo, Term, TermHistory, TermLsn},
4 : state::{EvictionState, PersistedPeers, TimelinePersistentState},
5 : wal_backup_partial,
6 : };
7 : use anyhow::{bail, Result};
8 : use pq_proto::SystemId;
9 : use serde::{Deserialize, Serialize};
10 : use tracing::*;
11 : use utils::{
12 : bin_ser::LeSer,
13 : id::{TenantId, TimelineId},
14 : lsn::Lsn,
15 : };
16 :
17 : /// Persistent consensus state of the acceptor.
18 6 : #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
19 : struct AcceptorStateV1 {
20 : /// acceptor's last term it voted for (advanced in 1 phase)
21 : term: Term,
22 : /// acceptor's epoch (advanced, i.e. bumped to 'term' when VCL is reached).
23 : epoch: Term,
24 : }
25 :
26 6 : #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
27 : struct SafeKeeperStateV1 {
28 : /// persistent acceptor state
29 : acceptor_state: AcceptorStateV1,
30 : /// information about server
31 : server: ServerInfoV2,
32 : /// Unique id of the last *elected* proposer we dealt with. Not needed
33 : /// for correctness, exists for monitoring purposes.
34 : proposer_uuid: PgUuid,
35 : /// part of WAL acknowledged by quorum and available locally
36 : commit_lsn: Lsn,
37 : /// minimal LSN which may be needed for recovery of some safekeeper (end_lsn
38 : /// of last record streamed to everyone)
39 : truncate_lsn: Lsn,
40 : // Safekeeper starts receiving WAL from this LSN, zeros before it ought to
41 : // be skipped during decoding.
42 : wal_start_lsn: Lsn,
43 : }
44 :
45 12 : #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
46 : pub struct ServerInfoV2 {
47 : /// Postgres server version
48 : pub pg_version: u32,
49 : pub system_id: SystemId,
50 : pub tenant_id: TenantId,
51 : pub timeline_id: TimelineId,
52 : pub wal_seg_size: u32,
53 : }
54 :
55 6 : #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
56 : pub struct SafeKeeperStateV2 {
57 : /// persistent acceptor state
58 : pub acceptor_state: AcceptorState,
59 : /// information about server
60 : pub server: ServerInfoV2,
61 : /// Unique id of the last *elected* proposer we dealt with. Not needed
62 : /// for correctness, exists for monitoring purposes.
63 : pub proposer_uuid: PgUuid,
64 : /// part of WAL acknowledged by quorum and available locally
65 : pub commit_lsn: Lsn,
66 : /// minimal LSN which may be needed for recovery of some safekeeper (end_lsn
67 : /// of last record streamed to everyone)
68 : pub truncate_lsn: Lsn,
69 : // Safekeeper starts receiving WAL from this LSN, zeros before it ought to
70 : // be skipped during decoding.
71 : pub wal_start_lsn: Lsn,
72 : }
73 :
74 24 : #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
75 : pub struct ServerInfoV3 {
76 : /// Postgres server version
77 : pub pg_version: u32,
78 : pub system_id: SystemId,
79 : #[serde(with = "hex")]
80 : pub tenant_id: TenantId,
81 : #[serde(with = "hex")]
82 : pub timeline_id: TimelineId,
83 : pub wal_seg_size: u32,
84 : }
85 :
86 12 : #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
87 : pub struct SafeKeeperStateV3 {
88 : /// persistent acceptor state
89 : pub acceptor_state: AcceptorState,
90 : /// information about server
91 : pub server: ServerInfoV3,
92 : /// Unique id of the last *elected* proposer we dealt with. Not needed
93 : /// for correctness, exists for monitoring purposes.
94 : #[serde(with = "hex")]
95 : pub proposer_uuid: PgUuid,
96 : /// part of WAL acknowledged by quorum and available locally
97 : pub commit_lsn: Lsn,
98 : /// minimal LSN which may be needed for recovery of some safekeeper (end_lsn
99 : /// of last record streamed to everyone)
100 : pub truncate_lsn: Lsn,
101 : // Safekeeper starts receiving WAL from this LSN, zeros before it ought to
102 : // be skipped during decoding.
103 : pub wal_start_lsn: Lsn,
104 : }
105 :
106 36 : #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
107 : pub struct SafeKeeperStateV4 {
108 : #[serde(with = "hex")]
109 : pub tenant_id: TenantId,
110 : #[serde(with = "hex")]
111 : pub timeline_id: TimelineId,
112 : /// persistent acceptor state
113 : pub acceptor_state: AcceptorState,
114 : /// information about server
115 : pub server: ServerInfo,
116 : /// Unique id of the last *elected* proposer we dealt with. Not needed
117 : /// for correctness, exists for monitoring purposes.
118 : #[serde(with = "hex")]
119 : pub proposer_uuid: PgUuid,
120 : /// Part of WAL acknowledged by quorum and available locally. Always points
121 : /// to record boundary.
122 : pub commit_lsn: Lsn,
123 : /// First LSN not yet offloaded to s3. Useful to persist to avoid finding
124 : /// out offloading progress on boot.
125 : pub s3_wal_lsn: Lsn,
126 : /// Minimal LSN which may be needed for recovery of some safekeeper (end_lsn
127 : /// of last record streamed to everyone). Persisting it helps skipping
128 : /// recovery in walproposer, generally we compute it from peers. In
129 : /// walproposer proto called 'truncate_lsn'.
130 : pub peer_horizon_lsn: Lsn,
131 : /// LSN of the oldest known checkpoint made by pageserver and successfully
132 : /// pushed to s3. We don't remove WAL beyond it. Persisted only for
133 : /// informational purposes, we receive it from pageserver (or broker).
134 : pub remote_consistent_lsn: Lsn,
135 : // Peers and their state as we remember it. Knowing peers themselves is
136 : // fundamental; but state is saved here only for informational purposes and
137 : // obviously can be stale. (Currently not saved at all, but let's provision
138 : // place to have less file version upgrades).
139 : pub peers: PersistedPeers,
140 : }
141 :
142 0 : #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
143 : pub struct SafeKeeperStateV7 {
144 : #[serde(with = "hex")]
145 : pub tenant_id: TenantId,
146 : #[serde(with = "hex")]
147 : pub timeline_id: TimelineId,
148 : /// persistent acceptor state
149 : pub acceptor_state: AcceptorState,
150 : /// information about server
151 : pub server: ServerInfo,
152 : /// Unique id of the last *elected* proposer we dealt with. Not needed
153 : /// for correctness, exists for monitoring purposes.
154 : #[serde(with = "hex")]
155 : pub proposer_uuid: PgUuid,
156 : /// Since which LSN this timeline generally starts. Safekeeper might have
157 : /// joined later.
158 : pub timeline_start_lsn: Lsn,
159 : /// Since which LSN safekeeper has (had) WAL for this timeline.
160 : /// All WAL segments next to one containing local_start_lsn are
161 : /// filled with data from the beginning.
162 : pub local_start_lsn: Lsn,
163 : /// Part of WAL acknowledged by quorum *and available locally*. Always points
164 : /// to record boundary.
165 : pub commit_lsn: Lsn,
166 : /// LSN that points to the end of the last backed up segment. Useful to
167 : /// persist to avoid finding out offloading progress on boot.
168 : pub backup_lsn: Lsn,
169 : /// Minimal LSN which may be needed for recovery of some safekeeper (end_lsn
170 : /// of last record streamed to everyone). Persisting it helps skipping
171 : /// recovery in walproposer, generally we compute it from peers. In
172 : /// walproposer proto called 'truncate_lsn'. Updates are currently drived
173 : /// only by walproposer.
174 : pub peer_horizon_lsn: Lsn,
175 : /// LSN of the oldest known checkpoint made by pageserver and successfully
176 : /// pushed to s3. We don't remove WAL beyond it. Persisted only for
177 : /// informational purposes, we receive it from pageserver (or broker).
178 : pub remote_consistent_lsn: Lsn,
179 : // Peers and their state as we remember it. Knowing peers themselves is
180 : // fundamental; but state is saved here only for informational purposes and
181 : // obviously can be stale. (Currently not saved at all, but let's provision
182 : // place to have less file version upgrades).
183 : pub peers: PersistedPeers,
184 : }
185 :
186 : /// Persistent information stored on safekeeper node about timeline.
187 : /// On disk data is prefixed by magic and format version and followed by checksum.
188 48 : #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
189 : pub struct SafeKeeperStateV8 {
190 : #[serde(with = "hex")]
191 : pub tenant_id: TenantId,
192 : #[serde(with = "hex")]
193 : pub timeline_id: TimelineId,
194 : /// persistent acceptor state
195 : pub acceptor_state: AcceptorState,
196 : /// information about server
197 : pub server: ServerInfo,
198 : /// Unique id of the last *elected* proposer we dealt with. Not needed
199 : /// for correctness, exists for monitoring purposes.
200 : #[serde(with = "hex")]
201 : pub proposer_uuid: PgUuid,
202 : /// Since which LSN this timeline generally starts. Safekeeper might have
203 : /// joined later.
204 : pub timeline_start_lsn: Lsn,
205 : /// Since which LSN safekeeper has (had) WAL for this timeline.
206 : /// All WAL segments next to one containing local_start_lsn are
207 : /// filled with data from the beginning.
208 : pub local_start_lsn: Lsn,
209 : /// Part of WAL acknowledged by quorum *and available locally*. Always points
210 : /// to record boundary.
211 : pub commit_lsn: Lsn,
212 : /// LSN that points to the end of the last backed up segment. Useful to
213 : /// persist to avoid finding out offloading progress on boot.
214 : pub backup_lsn: Lsn,
215 : /// Minimal LSN which may be needed for recovery of some safekeeper (end_lsn
216 : /// of last record streamed to everyone). Persisting it helps skipping
217 : /// recovery in walproposer, generally we compute it from peers. In
218 : /// walproposer proto called 'truncate_lsn'. Updates are currently drived
219 : /// only by walproposer.
220 : pub peer_horizon_lsn: Lsn,
221 : /// LSN of the oldest known checkpoint made by pageserver and successfully
222 : /// pushed to s3. We don't remove WAL beyond it. Persisted only for
223 : /// informational purposes, we receive it from pageserver (or broker).
224 : pub remote_consistent_lsn: Lsn,
225 : /// Peers and their state as we remember it. Knowing peers themselves is
226 : /// fundamental; but state is saved here only for informational purposes and
227 : /// obviously can be stale. (Currently not saved at all, but let's provision
228 : /// place to have less file version upgrades).
229 : pub peers: PersistedPeers,
230 : /// Holds names of partial segments uploaded to remote storage. Used to
231 : /// clean up old objects without leaving garbage in remote storage.
232 : pub partial_backup: wal_backup_partial::State,
233 : }
234 :
235 12 : pub fn upgrade_control_file(buf: &[u8], version: u32) -> Result<TimelinePersistentState> {
236 12 : // migrate to storing full term history
237 12 : if version == 1 {
238 0 : info!("reading safekeeper control file version {}", version);
239 0 : let oldstate = SafeKeeperStateV1::des(&buf[..buf.len()])?;
240 0 : let ac = AcceptorState {
241 0 : term: oldstate.acceptor_state.term,
242 0 : term_history: TermHistory(vec![TermLsn {
243 0 : term: oldstate.acceptor_state.epoch,
244 0 : lsn: Lsn(0),
245 0 : }]),
246 0 : };
247 0 : return Ok(TimelinePersistentState {
248 0 : tenant_id: oldstate.server.tenant_id,
249 0 : timeline_id: oldstate.server.timeline_id,
250 0 : acceptor_state: ac,
251 0 : server: ServerInfo {
252 0 : pg_version: oldstate.server.pg_version,
253 0 : system_id: oldstate.server.system_id,
254 0 : wal_seg_size: oldstate.server.wal_seg_size,
255 0 : },
256 0 : proposer_uuid: oldstate.proposer_uuid,
257 0 : timeline_start_lsn: Lsn(0),
258 0 : local_start_lsn: Lsn(0),
259 0 : commit_lsn: oldstate.commit_lsn,
260 0 : backup_lsn: Lsn(0),
261 0 : peer_horizon_lsn: oldstate.truncate_lsn,
262 0 : remote_consistent_lsn: Lsn(0),
263 0 : peers: PersistedPeers(vec![]),
264 0 : partial_backup: wal_backup_partial::State::default(),
265 0 : eviction_state: EvictionState::Present,
266 0 : });
267 : // migrate to hexing some ids
268 12 : } else if version == 2 {
269 0 : info!("reading safekeeper control file version {}", version);
270 0 : let oldstate = SafeKeeperStateV2::des(&buf[..buf.len()])?;
271 0 : let server = ServerInfo {
272 0 : pg_version: oldstate.server.pg_version,
273 0 : system_id: oldstate.server.system_id,
274 0 : wal_seg_size: oldstate.server.wal_seg_size,
275 0 : };
276 0 : return Ok(TimelinePersistentState {
277 0 : tenant_id: oldstate.server.tenant_id,
278 0 : timeline_id: oldstate.server.timeline_id,
279 0 : acceptor_state: oldstate.acceptor_state,
280 0 : server,
281 0 : proposer_uuid: oldstate.proposer_uuid,
282 0 : timeline_start_lsn: Lsn(0),
283 0 : local_start_lsn: Lsn(0),
284 0 : commit_lsn: oldstate.commit_lsn,
285 0 : backup_lsn: Lsn(0),
286 0 : peer_horizon_lsn: oldstate.truncate_lsn,
287 0 : remote_consistent_lsn: Lsn(0),
288 0 : peers: PersistedPeers(vec![]),
289 0 : partial_backup: wal_backup_partial::State::default(),
290 0 : eviction_state: EvictionState::Present,
291 0 : });
292 : // migrate to moving tenant_id/timeline_id to the top and adding some lsns
293 12 : } else if version == 3 {
294 0 : info!("reading safekeeper control file version {version}");
295 0 : let oldstate = SafeKeeperStateV3::des(&buf[..buf.len()])?;
296 0 : let server = ServerInfo {
297 0 : pg_version: oldstate.server.pg_version,
298 0 : system_id: oldstate.server.system_id,
299 0 : wal_seg_size: oldstate.server.wal_seg_size,
300 0 : };
301 0 : return Ok(TimelinePersistentState {
302 0 : tenant_id: oldstate.server.tenant_id,
303 0 : timeline_id: oldstate.server.timeline_id,
304 0 : acceptor_state: oldstate.acceptor_state,
305 0 : server,
306 0 : proposer_uuid: oldstate.proposer_uuid,
307 0 : timeline_start_lsn: Lsn(0),
308 0 : local_start_lsn: Lsn(0),
309 0 : commit_lsn: oldstate.commit_lsn,
310 0 : backup_lsn: Lsn(0),
311 0 : peer_horizon_lsn: oldstate.truncate_lsn,
312 0 : remote_consistent_lsn: Lsn(0),
313 0 : peers: PersistedPeers(vec![]),
314 0 : partial_backup: wal_backup_partial::State::default(),
315 0 : eviction_state: EvictionState::Present,
316 0 : });
317 : // migrate to having timeline_start_lsn
318 12 : } else if version == 4 {
319 0 : info!("reading safekeeper control file version {}", version);
320 0 : let oldstate = SafeKeeperStateV4::des(&buf[..buf.len()])?;
321 0 : let server = ServerInfo {
322 0 : pg_version: oldstate.server.pg_version,
323 0 : system_id: oldstate.server.system_id,
324 0 : wal_seg_size: oldstate.server.wal_seg_size,
325 0 : };
326 0 : return Ok(TimelinePersistentState {
327 0 : tenant_id: oldstate.tenant_id,
328 0 : timeline_id: oldstate.timeline_id,
329 0 : acceptor_state: oldstate.acceptor_state,
330 0 : server,
331 0 : proposer_uuid: oldstate.proposer_uuid,
332 0 : timeline_start_lsn: Lsn(0),
333 0 : local_start_lsn: Lsn(0),
334 0 : commit_lsn: oldstate.commit_lsn,
335 0 : backup_lsn: Lsn::INVALID,
336 0 : peer_horizon_lsn: oldstate.peer_horizon_lsn,
337 0 : remote_consistent_lsn: Lsn(0),
338 0 : peers: PersistedPeers(vec![]),
339 0 : partial_backup: wal_backup_partial::State::default(),
340 0 : eviction_state: EvictionState::Present,
341 0 : });
342 12 : } else if version == 5 {
343 0 : info!("reading safekeeper control file version {}", version);
344 0 : let mut oldstate = TimelinePersistentState::des(&buf[..buf.len()])?;
345 0 : if oldstate.timeline_start_lsn != Lsn(0) {
346 0 : return Ok(oldstate);
347 0 : }
348 0 :
349 0 : // set special timeline_start_lsn because we don't know the real one
350 0 : info!("setting timeline_start_lsn and local_start_lsn to Lsn(1)");
351 0 : oldstate.timeline_start_lsn = Lsn(1);
352 0 : oldstate.local_start_lsn = Lsn(1);
353 0 :
354 0 : return Ok(oldstate);
355 12 : } else if version == 6 {
356 0 : info!("reading safekeeper control file version {}", version);
357 0 : let mut oldstate = TimelinePersistentState::des(&buf[..buf.len()])?;
358 0 : if oldstate.server.pg_version != 0 {
359 0 : return Ok(oldstate);
360 0 : }
361 0 :
362 0 : // set pg_version to the default v14
363 0 : info!("setting pg_version to 140005");
364 0 : oldstate.server.pg_version = 140005;
365 0 :
366 0 : return Ok(oldstate);
367 12 : } else if version == 7 {
368 0 : info!("reading safekeeper control file version {}", version);
369 0 : let oldstate = SafeKeeperStateV7::des(&buf[..buf.len()])?;
370 :
371 0 : return Ok(TimelinePersistentState {
372 0 : tenant_id: oldstate.tenant_id,
373 0 : timeline_id: oldstate.timeline_id,
374 0 : acceptor_state: oldstate.acceptor_state,
375 0 : server: oldstate.server,
376 0 : proposer_uuid: oldstate.proposer_uuid,
377 0 : timeline_start_lsn: oldstate.timeline_start_lsn,
378 0 : local_start_lsn: oldstate.local_start_lsn,
379 0 : commit_lsn: oldstate.commit_lsn,
380 0 : backup_lsn: oldstate.backup_lsn,
381 0 : peer_horizon_lsn: oldstate.peer_horizon_lsn,
382 0 : remote_consistent_lsn: oldstate.remote_consistent_lsn,
383 0 : peers: oldstate.peers,
384 0 : partial_backup: wal_backup_partial::State::default(),
385 0 : eviction_state: EvictionState::Present,
386 0 : });
387 12 : } else if version == 8 {
388 12 : let oldstate = SafeKeeperStateV8::des(&buf[..buf.len()])?;
389 :
390 12 : return Ok(TimelinePersistentState {
391 12 : tenant_id: oldstate.tenant_id,
392 12 : timeline_id: oldstate.timeline_id,
393 12 : acceptor_state: oldstate.acceptor_state,
394 12 : server: oldstate.server,
395 12 : proposer_uuid: oldstate.proposer_uuid,
396 12 : timeline_start_lsn: oldstate.timeline_start_lsn,
397 12 : local_start_lsn: oldstate.local_start_lsn,
398 12 : commit_lsn: oldstate.commit_lsn,
399 12 : backup_lsn: oldstate.backup_lsn,
400 12 : peer_horizon_lsn: oldstate.peer_horizon_lsn,
401 12 : remote_consistent_lsn: oldstate.remote_consistent_lsn,
402 12 : peers: oldstate.peers,
403 12 : partial_backup: oldstate.partial_backup,
404 12 : eviction_state: EvictionState::Present,
405 12 : });
406 0 : }
407 0 :
408 0 : // TODO: persist the file back to the disk after upgrade
409 0 : // TODO: think about backward compatibility and rollbacks
410 0 :
411 0 : bail!("unsupported safekeeper control file version {}", version)
412 12 : }
413 :
414 12 : pub fn downgrade_v9_to_v8(state: &TimelinePersistentState) -> SafeKeeperStateV8 {
415 12 : assert!(state.eviction_state == EvictionState::Present);
416 12 : SafeKeeperStateV8 {
417 12 : tenant_id: state.tenant_id,
418 12 : timeline_id: state.timeline_id,
419 12 : acceptor_state: state.acceptor_state.clone(),
420 12 : server: state.server.clone(),
421 12 : proposer_uuid: state.proposer_uuid,
422 12 : timeline_start_lsn: state.timeline_start_lsn,
423 12 : local_start_lsn: state.local_start_lsn,
424 12 : commit_lsn: state.commit_lsn,
425 12 : backup_lsn: state.backup_lsn,
426 12 : peer_horizon_lsn: state.peer_horizon_lsn,
427 12 : remote_consistent_lsn: state.remote_consistent_lsn,
428 12 : peers: state.peers.clone(),
429 12 : partial_backup: state.partial_backup.clone(),
430 12 : }
431 12 : }
432 :
433 : #[cfg(test)]
434 : mod tests {
435 : use std::str::FromStr;
436 :
437 : use utils::{id::NodeId, Hex};
438 :
439 : use crate::safekeeper::PersistedPeerInfo;
440 :
441 : use super::*;
442 :
443 : #[test]
444 6 : fn roundtrip_v1() {
445 6 : let tenant_id = TenantId::from_str("cf0480929707ee75372337efaa5ecf96").unwrap();
446 6 : let timeline_id = TimelineId::from_str("112ded66422aa5e953e5440fa5427ac4").unwrap();
447 6 : let state = SafeKeeperStateV1 {
448 6 : acceptor_state: AcceptorStateV1 {
449 6 : term: 42,
450 6 : epoch: 43,
451 6 : },
452 6 : server: ServerInfoV2 {
453 6 : pg_version: 14,
454 6 : system_id: 0x1234567887654321,
455 6 : tenant_id,
456 6 : timeline_id,
457 6 : wal_seg_size: 0x12345678,
458 6 : },
459 6 : proposer_uuid: {
460 6 : let mut arr = timeline_id.as_arr();
461 6 : arr.reverse();
462 6 : arr
463 6 : },
464 6 : commit_lsn: Lsn(1234567800),
465 6 : truncate_lsn: Lsn(123456780),
466 6 : wal_start_lsn: Lsn(1234567800 - 8),
467 6 : };
468 6 :
469 6 : let ser = state.ser().unwrap();
470 6 : #[rustfmt::skip]
471 6 : let expected = [
472 6 : // term
473 6 : 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
474 6 : // epoch
475 6 : 0x2b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
476 6 : // pg_version
477 6 : 0x0e, 0x00, 0x00, 0x00,
478 6 : // system_id
479 6 : 0x21, 0x43, 0x65, 0x87, 0x78, 0x56, 0x34, 0x12,
480 6 : // tenant_id
481 6 : 0xcf, 0x04, 0x80, 0x92, 0x97, 0x07, 0xee, 0x75, 0x37, 0x23, 0x37, 0xef, 0xaa, 0x5e, 0xcf, 0x96,
482 6 : // timeline_id
483 6 : 0x11, 0x2d, 0xed, 0x66, 0x42, 0x2a, 0xa5, 0xe9, 0x53, 0xe5, 0x44, 0x0f, 0xa5, 0x42, 0x7a, 0xc4,
484 6 : // wal_seg_size
485 6 : 0x78, 0x56, 0x34, 0x12,
486 6 : // proposer_uuid
487 6 : 0xc4, 0x7a, 0x42, 0xa5, 0x0f, 0x44, 0xe5, 0x53, 0xe9, 0xa5, 0x2a, 0x42, 0x66, 0xed, 0x2d, 0x11,
488 6 : // commit_lsn
489 6 : 0x78, 0x02, 0x96, 0x49, 0x00, 0x00, 0x00, 0x00,
490 6 : // truncate_lsn
491 6 : 0x0c, 0xcd, 0x5b, 0x07, 0x00, 0x00, 0x00, 0x00,
492 6 : // wal_start_lsn
493 6 : 0x70, 0x02, 0x96, 0x49, 0x00, 0x00, 0x00, 0x00,
494 6 : ];
495 6 :
496 6 : assert_eq!(Hex(&ser), Hex(&expected));
497 :
498 6 : let deser = SafeKeeperStateV1::des(&ser).unwrap();
499 6 :
500 6 : assert_eq!(state, deser);
501 6 : }
502 :
503 : #[test]
504 6 : fn roundtrip_v2() {
505 6 : let tenant_id = TenantId::from_str("cf0480929707ee75372337efaa5ecf96").unwrap();
506 6 : let timeline_id = TimelineId::from_str("112ded66422aa5e953e5440fa5427ac4").unwrap();
507 6 : let state = SafeKeeperStateV2 {
508 6 : acceptor_state: AcceptorState {
509 6 : term: 42,
510 6 : term_history: TermHistory(vec![TermLsn {
511 6 : lsn: Lsn(0x1),
512 6 : term: 41,
513 6 : }]),
514 6 : },
515 6 : server: ServerInfoV2 {
516 6 : pg_version: 14,
517 6 : system_id: 0x1234567887654321,
518 6 : tenant_id,
519 6 : timeline_id,
520 6 : wal_seg_size: 0x12345678,
521 6 : },
522 6 : proposer_uuid: {
523 6 : let mut arr = timeline_id.as_arr();
524 6 : arr.reverse();
525 6 : arr
526 6 : },
527 6 : commit_lsn: Lsn(1234567800),
528 6 : truncate_lsn: Lsn(123456780),
529 6 : wal_start_lsn: Lsn(1234567800 - 8),
530 6 : };
531 6 :
532 6 : let ser = state.ser().unwrap();
533 6 : let expected = [
534 6 : 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
535 6 : 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
536 6 : 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x21, 0x43, 0x65, 0x87, 0x78, 0x56,
537 6 : 0x34, 0x12, 0xcf, 0x04, 0x80, 0x92, 0x97, 0x07, 0xee, 0x75, 0x37, 0x23, 0x37, 0xef,
538 6 : 0xaa, 0x5e, 0xcf, 0x96, 0x11, 0x2d, 0xed, 0x66, 0x42, 0x2a, 0xa5, 0xe9, 0x53, 0xe5,
539 6 : 0x44, 0x0f, 0xa5, 0x42, 0x7a, 0xc4, 0x78, 0x56, 0x34, 0x12, 0xc4, 0x7a, 0x42, 0xa5,
540 6 : 0x0f, 0x44, 0xe5, 0x53, 0xe9, 0xa5, 0x2a, 0x42, 0x66, 0xed, 0x2d, 0x11, 0x78, 0x02,
541 6 : 0x96, 0x49, 0x00, 0x00, 0x00, 0x00, 0x0c, 0xcd, 0x5b, 0x07, 0x00, 0x00, 0x00, 0x00,
542 6 : 0x70, 0x02, 0x96, 0x49, 0x00, 0x00, 0x00, 0x00,
543 6 : ];
544 6 :
545 6 : assert_eq!(Hex(&ser), Hex(&expected));
546 :
547 6 : let deser = SafeKeeperStateV2::des(&ser).unwrap();
548 6 :
549 6 : assert_eq!(state, deser);
550 6 : }
551 :
552 : #[test]
553 6 : fn roundtrip_v3() {
554 6 : let tenant_id = TenantId::from_str("cf0480929707ee75372337efaa5ecf96").unwrap();
555 6 : let timeline_id = TimelineId::from_str("112ded66422aa5e953e5440fa5427ac4").unwrap();
556 6 : let state = SafeKeeperStateV3 {
557 6 : acceptor_state: AcceptorState {
558 6 : term: 42,
559 6 : term_history: TermHistory(vec![TermLsn {
560 6 : lsn: Lsn(0x1),
561 6 : term: 41,
562 6 : }]),
563 6 : },
564 6 : server: ServerInfoV3 {
565 6 : pg_version: 14,
566 6 : system_id: 0x1234567887654321,
567 6 : tenant_id,
568 6 : timeline_id,
569 6 : wal_seg_size: 0x12345678,
570 6 : },
571 6 : proposer_uuid: {
572 6 : let mut arr = timeline_id.as_arr();
573 6 : arr.reverse();
574 6 : arr
575 6 : },
576 6 : commit_lsn: Lsn(1234567800),
577 6 : truncate_lsn: Lsn(123456780),
578 6 : wal_start_lsn: Lsn(1234567800 - 8),
579 6 : };
580 6 :
581 6 : let ser = state.ser().unwrap();
582 6 : let expected = [
583 6 : 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
584 6 : 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
585 6 : 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x21, 0x43, 0x65, 0x87, 0x78, 0x56,
586 6 : 0x34, 0x12, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x66, 0x30, 0x34,
587 6 : 0x38, 0x30, 0x39, 0x32, 0x39, 0x37, 0x30, 0x37, 0x65, 0x65, 0x37, 0x35, 0x33, 0x37,
588 6 : 0x32, 0x33, 0x33, 0x37, 0x65, 0x66, 0x61, 0x61, 0x35, 0x65, 0x63, 0x66, 0x39, 0x36,
589 6 : 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x31, 0x32, 0x64, 0x65, 0x64,
590 6 : 0x36, 0x36, 0x34, 0x32, 0x32, 0x61, 0x61, 0x35, 0x65, 0x39, 0x35, 0x33, 0x65, 0x35,
591 6 : 0x34, 0x34, 0x30, 0x66, 0x61, 0x35, 0x34, 0x32, 0x37, 0x61, 0x63, 0x34, 0x78, 0x56,
592 6 : 0x34, 0x12, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x34, 0x37, 0x61,
593 6 : 0x34, 0x32, 0x61, 0x35, 0x30, 0x66, 0x34, 0x34, 0x65, 0x35, 0x35, 0x33, 0x65, 0x39,
594 6 : 0x61, 0x35, 0x32, 0x61, 0x34, 0x32, 0x36, 0x36, 0x65, 0x64, 0x32, 0x64, 0x31, 0x31,
595 6 : 0x78, 0x02, 0x96, 0x49, 0x00, 0x00, 0x00, 0x00, 0x0c, 0xcd, 0x5b, 0x07, 0x00, 0x00,
596 6 : 0x00, 0x00, 0x70, 0x02, 0x96, 0x49, 0x00, 0x00, 0x00, 0x00,
597 6 : ];
598 6 :
599 6 : assert_eq!(Hex(&ser), Hex(&expected));
600 :
601 6 : let deser = SafeKeeperStateV3::des(&ser).unwrap();
602 6 :
603 6 : assert_eq!(state, deser);
604 6 : }
605 :
606 : #[test]
607 6 : fn roundtrip_v4() {
608 6 : let tenant_id = TenantId::from_str("cf0480929707ee75372337efaa5ecf96").unwrap();
609 6 : let timeline_id = TimelineId::from_str("112ded66422aa5e953e5440fa5427ac4").unwrap();
610 6 : let state = SafeKeeperStateV4 {
611 6 : tenant_id,
612 6 : timeline_id,
613 6 : acceptor_state: AcceptorState {
614 6 : term: 42,
615 6 : term_history: TermHistory(vec![TermLsn {
616 6 : lsn: Lsn(0x1),
617 6 : term: 41,
618 6 : }]),
619 6 : },
620 6 : server: ServerInfo {
621 6 : pg_version: 14,
622 6 : system_id: 0x1234567887654321,
623 6 : wal_seg_size: 0x12345678,
624 6 : },
625 6 : proposer_uuid: {
626 6 : let mut arr = timeline_id.as_arr();
627 6 : arr.reverse();
628 6 : arr
629 6 : },
630 6 : peers: PersistedPeers(vec![(
631 6 : NodeId(1),
632 6 : PersistedPeerInfo {
633 6 : backup_lsn: Lsn(1234567000),
634 6 : term: 42,
635 6 : flush_lsn: Lsn(1234567800 - 8),
636 6 : commit_lsn: Lsn(1234567600),
637 6 : },
638 6 : )]),
639 6 : commit_lsn: Lsn(1234567800),
640 6 : s3_wal_lsn: Lsn(1234567300),
641 6 : peer_horizon_lsn: Lsn(9999999),
642 6 : remote_consistent_lsn: Lsn(1234560000),
643 6 : };
644 6 :
645 6 : let ser = state.ser().unwrap();
646 6 : let expected = [
647 6 : 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x66, 0x30, 0x34, 0x38, 0x30,
648 6 : 0x39, 0x32, 0x39, 0x37, 0x30, 0x37, 0x65, 0x65, 0x37, 0x35, 0x33, 0x37, 0x32, 0x33,
649 6 : 0x33, 0x37, 0x65, 0x66, 0x61, 0x61, 0x35, 0x65, 0x63, 0x66, 0x39, 0x36, 0x20, 0x00,
650 6 : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x31, 0x32, 0x64, 0x65, 0x64, 0x36, 0x36,
651 6 : 0x34, 0x32, 0x32, 0x61, 0x61, 0x35, 0x65, 0x39, 0x35, 0x33, 0x65, 0x35, 0x34, 0x34,
652 6 : 0x30, 0x66, 0x61, 0x35, 0x34, 0x32, 0x37, 0x61, 0x63, 0x34, 0x2a, 0x00, 0x00, 0x00,
653 6 : 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00,
654 6 : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
655 6 : 0x0e, 0x00, 0x00, 0x00, 0x21, 0x43, 0x65, 0x87, 0x78, 0x56, 0x34, 0x12, 0x78, 0x56,
656 6 : 0x34, 0x12, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x34, 0x37, 0x61,
657 6 : 0x34, 0x32, 0x61, 0x35, 0x30, 0x66, 0x34, 0x34, 0x65, 0x35, 0x35, 0x33, 0x65, 0x39,
658 6 : 0x61, 0x35, 0x32, 0x61, 0x34, 0x32, 0x36, 0x36, 0x65, 0x64, 0x32, 0x64, 0x31, 0x31,
659 6 : 0x78, 0x02, 0x96, 0x49, 0x00, 0x00, 0x00, 0x00, 0x84, 0x00, 0x96, 0x49, 0x00, 0x00,
660 6 : 0x00, 0x00, 0x7f, 0x96, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe4, 0x95, 0x49,
661 6 : 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
662 6 : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0xff, 0x95, 0x49, 0x00, 0x00, 0x00, 0x00,
663 6 : 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x02, 0x96, 0x49, 0x00, 0x00,
664 6 : 0x00, 0x00, 0xb0, 0x01, 0x96, 0x49, 0x00, 0x00, 0x00, 0x00,
665 6 : ];
666 6 :
667 6 : assert_eq!(Hex(&ser), Hex(&expected));
668 :
669 6 : let deser = SafeKeeperStateV4::des(&ser).unwrap();
670 6 :
671 6 : assert_eq!(state, deser);
672 6 : }
673 : }
|