Line data Source code
1 : //! Describes the legacy now hopefully no longer modified per-timeline metadata.
2 : //!
3 : //! It is stored in `index_part.json` managed by [`remote_timeline_client`]. For many tenants and
4 : //! their timelines, this struct and its original serialization format is still needed because
5 : //! they were written a long time ago.
6 : //!
7 : //! Instead of changing and adding versioning to this, just change [`IndexPart`] with soft json
8 : //! versioning.
9 : //!
10 : //! To clean up this module we need to migrate all index_part.json files to a later version.
11 : //! While doing this, we need to be mindful about s3 based recovery as well, so it might take
12 : //! however long we keep the old versions to be able to delete the old code. After that, we can
13 : //! remove everything else than [`TimelineMetadataBodyV2`], rename it as `TimelineMetadata` and
14 : //! move it to `index.rs`. Before doing all of this, we need to keep the structures for backwards
15 : //! compatibility.
16 : //!
17 : //! [`remote_timeline_client`]: super::remote_timeline_client
18 : //! [`IndexPart`]: super::remote_timeline_client::index::IndexPart
19 :
20 : use anyhow::ensure;
21 : use serde::{Deserialize, Serialize};
22 : use utils::bin_ser::{BeSer, SerializeError};
23 : use utils::id::TimelineId;
24 : use utils::lsn::Lsn;
25 :
26 : /// Use special format number to enable backward compatibility.
27 : const METADATA_FORMAT_VERSION: u16 = 4;
28 :
29 : /// Previous supported format versions.
30 : ///
31 : /// In practice, none of these should remain, all are [`METADATA_FORMAT_VERSION`], but confirming
32 : /// that requires a scrubber run which is yet to be done.
33 : const METADATA_OLD_FORMAT_VERSION: u16 = 3;
34 :
35 : /// When the file existed on disk we assumed that a write of up to METADATA_MAX_SIZE bytes is atomic.
36 : ///
37 : /// This is the same assumption that PostgreSQL makes with the control file,
38 : ///
39 : /// see PG_CONTROL_MAX_SAFE_SIZE
40 : const METADATA_MAX_SIZE: usize = 512;
41 :
42 : /// Legacy metadata stored as a component of `index_part.json` per timeline.
43 : ///
44 : /// Do not make new changes to this type or the module. In production, we have two different kinds
45 : /// of serializations of this type: bincode and json. Bincode version reflects what used to be
46 : /// stored on disk in earlier versions and does internal crc32 checksumming.
47 : ///
48 : /// This type should not implement `serde::Serialize` or `serde::Deserialize` because there would
49 : /// be a confusion whether you want the old version ([`TimelineMetadata::from_bytes`]) or the modern
50 : /// as-exists in `index_part.json` ([`self::modern_serde`]).
51 : ///
52 : /// ```compile_fail
53 : /// #[derive(serde::Serialize)]
54 : /// struct DoNotDoThis(pageserver::tenant::metadata::TimelineMetadata);
55 : /// ```
56 : ///
57 : /// ```compile_fail
58 : /// #[derive(serde::Deserialize)]
59 : /// struct NeitherDoThis(pageserver::tenant::metadata::TimelineMetadata);
60 : /// ```
61 : #[derive(Debug, Clone, PartialEq, Eq)]
62 : pub struct TimelineMetadata {
63 : hdr: TimelineMetadataHeader,
64 : body: TimelineMetadataBodyV2,
65 : }
66 :
67 0 : #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
68 : struct TimelineMetadataHeader {
69 : checksum: u32, // CRC of serialized metadata body
70 : size: u16, // size of serialized metadata
71 : format_version: u16, // metadata format version (used for compatibility checks)
72 : }
73 :
74 : impl TryFrom<&TimelineMetadataBodyV2> for TimelineMetadataHeader {
75 : type Error = Crc32CalculationFailed;
76 :
77 96 : fn try_from(value: &TimelineMetadataBodyV2) -> Result<Self, Self::Error> {
78 : #[derive(Default)]
79 : struct Crc32Sink {
80 : crc: u32,
81 : count: usize,
82 : }
83 :
84 : impl std::io::Write for Crc32Sink {
85 1576 : fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
86 1576 : self.crc = crc32c::crc32c_append(self.crc, buf);
87 1576 : self.count += buf.len();
88 1576 : Ok(buf.len())
89 1576 : }
90 :
91 0 : fn flush(&mut self) -> std::io::Result<()> {
92 0 : Ok(())
93 0 : }
94 : }
95 :
96 : // jump through hoops to calculate the crc32 so that TimelineMetadata::ne works
97 : // across serialization versions
98 96 : let mut sink = Crc32Sink::default();
99 96 : <TimelineMetadataBodyV2 as utils::bin_ser::BeSer>::ser_into(value, &mut sink)
100 96 : .map_err(Crc32CalculationFailed)?;
101 :
102 96 : let size = METADATA_HDR_SIZE + sink.count;
103 96 :
104 96 : Ok(TimelineMetadataHeader {
105 96 : checksum: sink.crc,
106 96 : size: size as u16,
107 96 : format_version: METADATA_FORMAT_VERSION,
108 96 : })
109 96 : }
110 : }
111 :
112 : #[derive(thiserror::Error, Debug)]
113 : #[error("re-serializing for crc32 failed")]
114 : struct Crc32CalculationFailed(#[source] utils::bin_ser::SerializeError);
115 :
116 : const METADATA_HDR_SIZE: usize = size_of::<TimelineMetadataHeader>();
117 :
118 504 : #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
119 : struct TimelineMetadataBodyV2 {
120 : disk_consistent_lsn: Lsn,
121 : // This is only set if we know it. We track it in memory when the page
122 : // server is running, but we only track the value corresponding to
123 : // 'last_record_lsn', not 'disk_consistent_lsn' which can lag behind by a
124 : // lot. We only store it in the metadata file when we flush *all* the
125 : // in-memory data so that 'last_record_lsn' is the same as
126 : // 'disk_consistent_lsn'. That's OK, because after page server restart, as
127 : // soon as we reprocess at least one record, we will have a valid
128 : // 'prev_record_lsn' value in memory again. This is only really needed when
129 : // doing a clean shutdown, so that there is no more WAL beyond
130 : // 'disk_consistent_lsn'
131 : prev_record_lsn: Option<Lsn>,
132 : ancestor_timeline: Option<TimelineId>,
133 : ancestor_lsn: Lsn,
134 :
135 : // The LSN at which GC was last executed. Synonym of [`Timeline::applied_gc_cutoff_lsn`].
136 : latest_gc_cutoff_lsn: Lsn,
137 :
138 : initdb_lsn: Lsn,
139 : pg_version: u32,
140 : }
141 :
142 0 : #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
143 : struct TimelineMetadataBodyV1 {
144 : disk_consistent_lsn: Lsn,
145 : // This is only set if we know it. We track it in memory when the page
146 : // server is running, but we only track the value corresponding to
147 : // 'last_record_lsn', not 'disk_consistent_lsn' which can lag behind by a
148 : // lot. We only store it in the metadata file when we flush *all* the
149 : // in-memory data so that 'last_record_lsn' is the same as
150 : // 'disk_consistent_lsn'. That's OK, because after page server restart, as
151 : // soon as we reprocess at least one record, we will have a valid
152 : // 'prev_record_lsn' value in memory again. This is only really needed when
153 : // doing a clean shutdown, so that there is no more WAL beyond
154 : // 'disk_consistent_lsn'
155 : prev_record_lsn: Option<Lsn>,
156 : ancestor_timeline: Option<TimelineId>,
157 : ancestor_lsn: Lsn,
158 : latest_gc_cutoff_lsn: Lsn,
159 : initdb_lsn: Lsn,
160 : }
161 :
162 : impl TimelineMetadata {
163 1102 : pub fn new(
164 1102 : disk_consistent_lsn: Lsn,
165 1102 : prev_record_lsn: Option<Lsn>,
166 1102 : ancestor_timeline: Option<TimelineId>,
167 1102 : ancestor_lsn: Lsn,
168 1102 : latest_gc_cutoff_lsn: Lsn,
169 1102 : initdb_lsn: Lsn,
170 1102 : pg_version: u32,
171 1102 : ) -> Self {
172 1102 : Self {
173 1102 : hdr: TimelineMetadataHeader {
174 1102 : checksum: 0,
175 1102 : size: 0,
176 1102 : format_version: METADATA_FORMAT_VERSION,
177 1102 : },
178 1102 : body: TimelineMetadataBodyV2 {
179 1102 : disk_consistent_lsn,
180 1102 : prev_record_lsn,
181 1102 : ancestor_timeline,
182 1102 : ancestor_lsn,
183 1102 : latest_gc_cutoff_lsn,
184 1102 : initdb_lsn,
185 1102 : pg_version,
186 1102 : },
187 1102 : }
188 1102 : }
189 :
190 : #[cfg(test)]
191 24 : pub(crate) fn with_recalculated_checksum(mut self) -> anyhow::Result<Self> {
192 24 : self.hdr = TimelineMetadataHeader::try_from(&self.body)?;
193 24 : Ok(self)
194 24 : }
195 :
196 4 : fn upgrade_timeline_metadata(metadata_bytes: &[u8]) -> anyhow::Result<Self> {
197 4 : let mut hdr = TimelineMetadataHeader::des(&metadata_bytes[0..METADATA_HDR_SIZE])?;
198 :
199 : // backward compatible only up to this version
200 4 : ensure!(
201 4 : hdr.format_version == METADATA_OLD_FORMAT_VERSION,
202 0 : "unsupported metadata format version {}",
203 : hdr.format_version
204 : );
205 :
206 4 : let metadata_size = hdr.size as usize;
207 :
208 4 : let body: TimelineMetadataBodyV1 =
209 4 : TimelineMetadataBodyV1::des(&metadata_bytes[METADATA_HDR_SIZE..metadata_size])?;
210 :
211 4 : let body = TimelineMetadataBodyV2 {
212 4 : disk_consistent_lsn: body.disk_consistent_lsn,
213 4 : prev_record_lsn: body.prev_record_lsn,
214 4 : ancestor_timeline: body.ancestor_timeline,
215 4 : ancestor_lsn: body.ancestor_lsn,
216 4 : latest_gc_cutoff_lsn: body.latest_gc_cutoff_lsn,
217 4 : initdb_lsn: body.initdb_lsn,
218 4 : pg_version: 14, // All timelines created before this version had pg_version 14
219 4 : };
220 4 :
221 4 : hdr.format_version = METADATA_FORMAT_VERSION;
222 4 :
223 4 : Ok(Self { hdr, body })
224 4 : }
225 :
226 244 : pub fn from_bytes(metadata_bytes: &[u8]) -> anyhow::Result<Self> {
227 244 : ensure!(
228 244 : metadata_bytes.len() == METADATA_MAX_SIZE,
229 0 : "metadata bytes size is wrong"
230 : );
231 244 : let hdr = TimelineMetadataHeader::des(&metadata_bytes[0..METADATA_HDR_SIZE])?;
232 :
233 244 : let metadata_size = hdr.size as usize;
234 244 : ensure!(
235 244 : metadata_size <= METADATA_MAX_SIZE,
236 0 : "corrupted metadata file"
237 : );
238 244 : let calculated_checksum = crc32c::crc32c(&metadata_bytes[METADATA_HDR_SIZE..metadata_size]);
239 244 : ensure!(
240 244 : hdr.checksum == calculated_checksum,
241 0 : "metadata checksum mismatch"
242 : );
243 :
244 244 : if hdr.format_version != METADATA_FORMAT_VERSION {
245 : // If metadata has the old format,
246 : // upgrade it and return the result
247 4 : TimelineMetadata::upgrade_timeline_metadata(metadata_bytes)
248 : } else {
249 240 : let body =
250 240 : TimelineMetadataBodyV2::des(&metadata_bytes[METADATA_HDR_SIZE..metadata_size])?;
251 240 : ensure!(
252 240 : body.disk_consistent_lsn.is_aligned(),
253 0 : "disk_consistent_lsn is not aligned"
254 : );
255 240 : Ok(TimelineMetadata { hdr, body })
256 : }
257 244 : }
258 :
259 180 : pub fn to_bytes(&self) -> Result<Vec<u8>, SerializeError> {
260 180 : let body_bytes = self.body.ser()?;
261 180 : let metadata_size = METADATA_HDR_SIZE + body_bytes.len();
262 180 : let hdr = TimelineMetadataHeader {
263 180 : size: metadata_size as u16,
264 180 : format_version: METADATA_FORMAT_VERSION,
265 180 : checksum: crc32c::crc32c(&body_bytes),
266 180 : };
267 180 : let hdr_bytes = hdr.ser()?;
268 180 : let mut metadata_bytes = vec![0u8; METADATA_MAX_SIZE];
269 180 : metadata_bytes[0..METADATA_HDR_SIZE].copy_from_slice(&hdr_bytes);
270 180 : metadata_bytes[METADATA_HDR_SIZE..metadata_size].copy_from_slice(&body_bytes);
271 180 : Ok(metadata_bytes)
272 180 : }
273 :
274 : /// [`Lsn`] that corresponds to the corresponding timeline directory
275 : /// contents, stored locally in the pageserver workdir.
276 43746 : pub fn disk_consistent_lsn(&self) -> Lsn {
277 43746 : self.body.disk_consistent_lsn
278 43746 : }
279 :
280 906 : pub fn prev_record_lsn(&self) -> Option<Lsn> {
281 906 : self.body.prev_record_lsn
282 906 : }
283 :
284 6451 : pub fn ancestor_timeline(&self) -> Option<TimelineId> {
285 6451 : self.body.ancestor_timeline
286 6451 : }
287 :
288 1370 : pub fn ancestor_lsn(&self) -> Lsn {
289 1370 : self.body.ancestor_lsn
290 1370 : }
291 :
292 : /// When reparenting, the `ancestor_lsn` does not change.
293 : ///
294 : /// Returns true if anything was changed.
295 0 : pub fn reparent(&mut self, timeline: &TimelineId) {
296 0 : assert!(self.body.ancestor_timeline.is_some());
297 : // no assertion for redoing this: it's fine, we may have to repeat this multiple times over
298 0 : self.body.ancestor_timeline = Some(*timeline);
299 0 : }
300 :
301 : /// Returns true if anything was changed
302 0 : pub fn detach_from_ancestor(&mut self, branchpoint: &(TimelineId, Lsn)) {
303 0 : // Detaching from ancestor now doesn't always detach directly to the direct ancestor, but we
304 0 : // ensure the LSN is the same. So we don't check the timeline ID.
305 0 : if self.body.ancestor_lsn != Lsn(0) {
306 0 : assert_eq!(self.body.ancestor_lsn, branchpoint.1);
307 0 : }
308 0 : self.body.ancestor_timeline = None;
309 0 : self.body.ancestor_lsn = Lsn(0);
310 0 : }
311 :
312 906 : pub fn latest_gc_cutoff_lsn(&self) -> Lsn {
313 906 : self.body.latest_gc_cutoff_lsn
314 906 : }
315 :
316 906 : pub fn initdb_lsn(&self) -> Lsn {
317 906 : self.body.initdb_lsn
318 906 : }
319 :
320 914 : pub fn pg_version(&self) -> u32 {
321 914 : self.body.pg_version
322 914 : }
323 :
324 : // Checksums make it awkward to build a valid instance by hand. This helper
325 : // provides a TimelineMetadata with a valid checksum in its header.
326 168 : pub fn example() -> Self {
327 168 : let instance = Self::new(
328 168 : "0/16960E8".parse::<Lsn>().unwrap(),
329 168 : None,
330 168 : None,
331 168 : Lsn::from_hex("00000000").unwrap(),
332 168 : Lsn::from_hex("00000000").unwrap(),
333 168 : Lsn::from_hex("00000000").unwrap(),
334 168 : 0,
335 168 : );
336 168 : let bytes = instance.to_bytes().unwrap();
337 168 : Self::from_bytes(&bytes).unwrap()
338 168 : }
339 :
340 2488 : pub(crate) fn apply(&mut self, update: &MetadataUpdate) {
341 2488 : self.body.disk_consistent_lsn = update.disk_consistent_lsn;
342 2488 : self.body.prev_record_lsn = update.prev_record_lsn;
343 2488 : self.body.latest_gc_cutoff_lsn = update.latest_gc_cutoff_lsn;
344 2488 : }
345 : }
346 :
347 : pub(crate) mod modern_serde {
348 : use serde::{Deserialize, Serialize};
349 :
350 : use super::{TimelineMetadata, TimelineMetadataBodyV2, TimelineMetadataHeader};
351 :
352 104 : pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<TimelineMetadata, D::Error>
353 104 : where
354 104 : D: serde::de::Deserializer<'de>,
355 104 : {
356 : // for legacy reasons versions 1-5 had TimelineMetadata serialized as a Vec<u8> field with
357 : // BeSer.
358 : struct Visitor;
359 :
360 : impl<'d> serde::de::Visitor<'d> for Visitor {
361 : type Value = TimelineMetadata;
362 :
363 0 : fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
364 0 : f.write_str("BeSer bytes or json structure")
365 0 : }
366 :
367 32 : fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
368 32 : where
369 32 : A: serde::de::SeqAccess<'d>,
370 32 : {
371 : use serde::de::Error;
372 32 : let de = serde::de::value::SeqAccessDeserializer::new(seq);
373 32 : Vec::<u8>::deserialize(de)
374 32 : .map(|v| TimelineMetadata::from_bytes(&v).map_err(A::Error::custom))?
375 32 : }
376 :
377 72 : fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
378 72 : where
379 72 : A: serde::de::MapAccess<'d>,
380 72 : {
381 : use serde::de::Error;
382 :
383 72 : let de = serde::de::value::MapAccessDeserializer::new(map);
384 72 : let body = TimelineMetadataBodyV2::deserialize(de)?;
385 72 : let hdr = TimelineMetadataHeader::try_from(&body).map_err(A::Error::custom)?;
386 :
387 72 : Ok(TimelineMetadata { hdr, body })
388 72 : }
389 : }
390 :
391 104 : deserializer.deserialize_any(Visitor)
392 104 : }
393 :
394 6162 : pub(crate) fn serialize<S>(
395 6162 : metadata: &TimelineMetadata,
396 6162 : serializer: S,
397 6162 : ) -> Result<S::Ok, S::Error>
398 6162 : where
399 6162 : S: serde::Serializer,
400 6162 : {
401 6162 : // header is not needed, upon reading we've upgraded all v1 to v2
402 6162 : metadata.body.serialize(serializer)
403 6162 : }
404 :
405 : #[test]
406 4 : fn deserializes_bytes_as_well_as_equivalent_body_v2() {
407 4 : #[derive(serde::Deserialize, serde::Serialize)]
408 : struct Wrapper(
409 : #[serde(deserialize_with = "deserialize", serialize_with = "serialize")]
410 : TimelineMetadata,
411 : );
412 :
413 4 : let too_many_bytes = "[216,111,252,208,0,54,0,4,0,0,0,0,1,73,253,144,1,0,0,0,0,1,73,253,24,0,0,0,0,0,0,0,0,0,0,0,0,0,1,73,253,24,0,0,0,0,1,73,253,24,0,0,0,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]";
414 4 :
415 4 : let wrapper_from_bytes = serde_json::from_str::<Wrapper>(too_many_bytes).unwrap();
416 4 :
417 4 : let serialized = serde_json::to_value(&wrapper_from_bytes).unwrap();
418 4 :
419 4 : assert_eq!(
420 4 : serialized,
421 4 : serde_json::json! {{
422 4 : "disk_consistent_lsn": "0/149FD90",
423 4 : "prev_record_lsn": "0/149FD18",
424 4 : "ancestor_timeline": null,
425 4 : "ancestor_lsn": "0/0",
426 4 : "latest_gc_cutoff_lsn": "0/149FD18",
427 4 : "initdb_lsn": "0/149FD18",
428 4 : "pg_version": 15
429 4 : }}
430 4 : );
431 :
432 4 : let wrapper_from_json = serde_json::value::from_value::<Wrapper>(serialized).unwrap();
433 4 :
434 4 : assert_eq!(wrapper_from_bytes.0, wrapper_from_json.0);
435 4 : }
436 : }
437 :
438 : /// Parts of the metadata which are regularly modified.
439 : pub(crate) struct MetadataUpdate {
440 : disk_consistent_lsn: Lsn,
441 : prev_record_lsn: Option<Lsn>,
442 : latest_gc_cutoff_lsn: Lsn,
443 : }
444 :
445 : impl MetadataUpdate {
446 2488 : pub(crate) fn new(
447 2488 : disk_consistent_lsn: Lsn,
448 2488 : prev_record_lsn: Option<Lsn>,
449 2488 : latest_gc_cutoff_lsn: Lsn,
450 2488 : ) -> Self {
451 2488 : Self {
452 2488 : disk_consistent_lsn,
453 2488 : prev_record_lsn,
454 2488 : latest_gc_cutoff_lsn,
455 2488 : }
456 2488 : }
457 : }
458 :
459 : #[cfg(test)]
460 : mod tests {
461 : use super::*;
462 : use crate::tenant::harness::TIMELINE_ID;
463 :
464 : #[test]
465 4 : fn metadata_serializes_correctly() {
466 4 : let original_metadata = TimelineMetadata::new(
467 4 : Lsn(0x200),
468 4 : Some(Lsn(0x100)),
469 4 : Some(TIMELINE_ID),
470 4 : Lsn(0),
471 4 : Lsn(0),
472 4 : Lsn(0),
473 4 : // Any version will do here, so use the default
474 4 : crate::DEFAULT_PG_VERSION,
475 4 : );
476 4 :
477 4 : let metadata_bytes = original_metadata
478 4 : .to_bytes()
479 4 : .expect("Should serialize correct metadata to bytes");
480 4 :
481 4 : let deserialized_metadata = TimelineMetadata::from_bytes(&metadata_bytes)
482 4 : .expect("Should deserialize its own bytes");
483 4 :
484 4 : assert_eq!(
485 : deserialized_metadata.body, original_metadata.body,
486 0 : "Metadata that was serialized to bytes and deserialized back should not change"
487 : );
488 4 : }
489 :
490 : // Generate old version metadata and read it with current code.
491 : // Ensure that it is upgraded correctly
492 : #[test]
493 4 : fn test_metadata_upgrade() {
494 : #[derive(Debug, Clone, PartialEq, Eq)]
495 : struct TimelineMetadataV1 {
496 : hdr: TimelineMetadataHeader,
497 : body: TimelineMetadataBodyV1,
498 : }
499 :
500 4 : let metadata_v1 = TimelineMetadataV1 {
501 4 : hdr: TimelineMetadataHeader {
502 4 : checksum: 0,
503 4 : size: 0,
504 4 : format_version: METADATA_OLD_FORMAT_VERSION,
505 4 : },
506 4 : body: TimelineMetadataBodyV1 {
507 4 : disk_consistent_lsn: Lsn(0x200),
508 4 : prev_record_lsn: Some(Lsn(0x100)),
509 4 : ancestor_timeline: Some(TIMELINE_ID),
510 4 : ancestor_lsn: Lsn(0),
511 4 : latest_gc_cutoff_lsn: Lsn(0),
512 4 : initdb_lsn: Lsn(0),
513 4 : },
514 4 : };
515 :
516 : impl TimelineMetadataV1 {
517 4 : pub fn to_bytes(&self) -> anyhow::Result<Vec<u8>> {
518 4 : let body_bytes = self.body.ser()?;
519 4 : let metadata_size = METADATA_HDR_SIZE + body_bytes.len();
520 4 : let hdr = TimelineMetadataHeader {
521 4 : size: metadata_size as u16,
522 4 : format_version: METADATA_OLD_FORMAT_VERSION,
523 4 : checksum: crc32c::crc32c(&body_bytes),
524 4 : };
525 4 : let hdr_bytes = hdr.ser()?;
526 4 : let mut metadata_bytes = vec![0u8; METADATA_MAX_SIZE];
527 4 : metadata_bytes[0..METADATA_HDR_SIZE].copy_from_slice(&hdr_bytes);
528 4 : metadata_bytes[METADATA_HDR_SIZE..metadata_size].copy_from_slice(&body_bytes);
529 4 : Ok(metadata_bytes)
530 4 : }
531 : }
532 :
533 4 : let metadata_bytes = metadata_v1
534 4 : .to_bytes()
535 4 : .expect("Should serialize correct metadata to bytes");
536 4 :
537 4 : // This should deserialize to the latest version format
538 4 : let deserialized_metadata = TimelineMetadata::from_bytes(&metadata_bytes)
539 4 : .expect("Should deserialize its own bytes");
540 4 :
541 4 : let expected_metadata = TimelineMetadata::new(
542 4 : Lsn(0x200),
543 4 : Some(Lsn(0x100)),
544 4 : Some(TIMELINE_ID),
545 4 : Lsn(0),
546 4 : Lsn(0),
547 4 : Lsn(0),
548 4 : 14, // All timelines created before this version had pg_version 14
549 4 : );
550 4 :
551 4 : assert_eq!(
552 : deserialized_metadata.body, expected_metadata.body,
553 0 : "Metadata of the old version {} should be upgraded to the latest version {}",
554 : METADATA_OLD_FORMAT_VERSION, METADATA_FORMAT_VERSION
555 : );
556 4 : }
557 :
558 : #[test]
559 4 : fn test_metadata_bincode_serde_ensure_roundtrip() {
560 4 : let original_metadata = TimelineMetadata::new(
561 4 : Lsn(0x200),
562 4 : Some(Lsn(0x100)),
563 4 : Some(TIMELINE_ID),
564 4 : Lsn(0),
565 4 : Lsn(0),
566 4 : Lsn(0),
567 4 : // Any version will do here, so use the default
568 4 : crate::DEFAULT_PG_VERSION,
569 4 : );
570 4 : let expected_bytes = vec![
571 4 : /* TimelineMetadataHeader */
572 4 : 74, 104, 158, 105, 0, 70, 0, 4, // checksum, size, format_version (4 + 2 + 2)
573 4 : /* TimelineMetadataBodyV2 */
574 4 : 0, 0, 0, 0, 0, 0, 2, 0, // disk_consistent_lsn (8 bytes)
575 4 : 1, 0, 0, 0, 0, 0, 0, 1, 0, // prev_record_lsn (9 bytes)
576 4 : 1, 17, 34, 51, 68, 85, 102, 119, 136, 17, 34, 51, 68, 85, 102, 119,
577 4 : 136, // ancestor_timeline (17 bytes)
578 4 : 0, 0, 0, 0, 0, 0, 0, 0, // ancestor_lsn (8 bytes)
579 4 : 0, 0, 0, 0, 0, 0, 0, 0, // latest_gc_cutoff_lsn (8 bytes)
580 4 : 0, 0, 0, 0, 0, 0, 0, 0, // initdb_lsn (8 bytes)
581 4 : 0, 0, 0, 16, // pg_version (4 bytes)
582 4 : /* padding bytes */
583 4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
584 4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
585 4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
586 4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
587 4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
588 4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
589 4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
590 4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
591 4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
592 4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
593 4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
594 4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
595 4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
596 4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
597 4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
598 4 : 0, 0, 0, 0, 0, 0, 0,
599 4 : ];
600 4 : let metadata_ser_bytes = original_metadata.to_bytes().unwrap();
601 4 : assert_eq!(metadata_ser_bytes, expected_bytes);
602 :
603 4 : let expected_metadata = {
604 4 : let mut temp_metadata = original_metadata;
605 4 : let body_bytes = temp_metadata
606 4 : .body
607 4 : .ser()
608 4 : .expect("Cannot serialize the metadata body");
609 4 : let metadata_size = METADATA_HDR_SIZE + body_bytes.len();
610 4 : let hdr = TimelineMetadataHeader {
611 4 : size: metadata_size as u16,
612 4 : format_version: METADATA_FORMAT_VERSION,
613 4 : checksum: crc32c::crc32c(&body_bytes),
614 4 : };
615 4 : temp_metadata.hdr = hdr;
616 4 : temp_metadata
617 4 : };
618 4 : let des_metadata = TimelineMetadata::from_bytes(&metadata_ser_bytes).unwrap();
619 4 : assert_eq!(des_metadata, expected_metadata);
620 4 : }
621 : }
|