LCOV - code coverage report
Current view: top level - pageserver/src/tenant - metadata.rs (source / functions) Coverage Total Hit
Test: 1b0a6a0c05cee5a7de360813c8034804e105ce1c.info Lines: 92.1 % 369 340
Test Date: 2025-03-12 00:01:28 Functions: 58.2 % 67 39

            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         1100 :     pub fn new(
     164         1100 :         disk_consistent_lsn: Lsn,
     165         1100 :         prev_record_lsn: Option<Lsn>,
     166         1100 :         ancestor_timeline: Option<TimelineId>,
     167         1100 :         ancestor_lsn: Lsn,
     168         1100 :         latest_gc_cutoff_lsn: Lsn,
     169         1100 :         initdb_lsn: Lsn,
     170         1100 :         pg_version: u32,
     171         1100 :     ) -> Self {
     172         1100 :         Self {
     173         1100 :             hdr: TimelineMetadataHeader {
     174         1100 :                 checksum: 0,
     175         1100 :                 size: 0,
     176         1100 :                 format_version: METADATA_FORMAT_VERSION,
     177         1100 :             },
     178         1100 :             body: TimelineMetadataBodyV2 {
     179         1100 :                 disk_consistent_lsn,
     180         1100 :                 prev_record_lsn,
     181         1100 :                 ancestor_timeline,
     182         1100 :                 ancestor_lsn,
     183         1100 :                 latest_gc_cutoff_lsn,
     184         1100 :                 initdb_lsn,
     185         1100 :                 pg_version,
     186         1100 :             },
     187         1100 :         }
     188         1100 :     }
     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        43724 :     pub fn disk_consistent_lsn(&self) -> Lsn {
     277        43724 :         self.body.disk_consistent_lsn
     278        43724 :     }
     279              : 
     280          904 :     pub fn prev_record_lsn(&self) -> Option<Lsn> {
     281          904 :         self.body.prev_record_lsn
     282          904 :     }
     283              : 
     284         6401 :     pub fn ancestor_timeline(&self) -> Option<TimelineId> {
     285         6401 :         self.body.ancestor_timeline
     286         6401 :     }
     287              : 
     288         1368 :     pub fn ancestor_lsn(&self) -> Lsn {
     289         1368 :         self.body.ancestor_lsn
     290         1368 :     }
     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 :         if let Some(ancestor) = self.body.ancestor_timeline {
     304            0 :             assert_eq!(ancestor, branchpoint.0);
     305            0 :         }
     306            0 :         if self.body.ancestor_lsn != Lsn(0) {
     307            0 :             assert_eq!(self.body.ancestor_lsn, branchpoint.1);
     308            0 :         }
     309            0 :         self.body.ancestor_timeline = None;
     310            0 :         self.body.ancestor_lsn = Lsn(0);
     311            0 :     }
     312              : 
     313          904 :     pub fn latest_gc_cutoff_lsn(&self) -> Lsn {
     314          904 :         self.body.latest_gc_cutoff_lsn
     315          904 :     }
     316              : 
     317          904 :     pub fn initdb_lsn(&self) -> Lsn {
     318          904 :         self.body.initdb_lsn
     319          904 :     }
     320              : 
     321          912 :     pub fn pg_version(&self) -> u32 {
     322          912 :         self.body.pg_version
     323          912 :     }
     324              : 
     325              :     // Checksums make it awkward to build a valid instance by hand.  This helper
     326              :     // provides a TimelineMetadata with a valid checksum in its header.
     327          168 :     pub fn example() -> Self {
     328          168 :         let instance = Self::new(
     329          168 :             "0/16960E8".parse::<Lsn>().unwrap(),
     330          168 :             None,
     331          168 :             None,
     332          168 :             Lsn::from_hex("00000000").unwrap(),
     333          168 :             Lsn::from_hex("00000000").unwrap(),
     334          168 :             Lsn::from_hex("00000000").unwrap(),
     335          168 :             0,
     336          168 :         );
     337          168 :         let bytes = instance.to_bytes().unwrap();
     338          168 :         Self::from_bytes(&bytes).unwrap()
     339          168 :     }
     340              : 
     341         2456 :     pub(crate) fn apply(&mut self, update: &MetadataUpdate) {
     342         2456 :         self.body.disk_consistent_lsn = update.disk_consistent_lsn;
     343         2456 :         self.body.prev_record_lsn = update.prev_record_lsn;
     344         2456 :         self.body.latest_gc_cutoff_lsn = update.latest_gc_cutoff_lsn;
     345         2456 :     }
     346              : }
     347              : 
     348              : pub(crate) mod modern_serde {
     349              :     use serde::{Deserialize, Serialize};
     350              : 
     351              :     use super::{TimelineMetadata, TimelineMetadataBodyV2, TimelineMetadataHeader};
     352              : 
     353          104 :     pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<TimelineMetadata, D::Error>
     354          104 :     where
     355          104 :         D: serde::de::Deserializer<'de>,
     356          104 :     {
     357              :         // for legacy reasons versions 1-5 had TimelineMetadata serialized as a Vec<u8> field with
     358              :         // BeSer.
     359              :         struct Visitor;
     360              : 
     361              :         impl<'d> serde::de::Visitor<'d> for Visitor {
     362              :             type Value = TimelineMetadata;
     363              : 
     364            0 :             fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
     365            0 :                 f.write_str("BeSer bytes or json structure")
     366            0 :             }
     367              : 
     368           32 :             fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
     369           32 :             where
     370           32 :                 A: serde::de::SeqAccess<'d>,
     371           32 :             {
     372              :                 use serde::de::Error;
     373           32 :                 let de = serde::de::value::SeqAccessDeserializer::new(seq);
     374           32 :                 Vec::<u8>::deserialize(de)
     375           32 :                     .map(|v| TimelineMetadata::from_bytes(&v).map_err(A::Error::custom))?
     376           32 :             }
     377              : 
     378           72 :             fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
     379           72 :             where
     380           72 :                 A: serde::de::MapAccess<'d>,
     381           72 :             {
     382              :                 use serde::de::Error;
     383              : 
     384           72 :                 let de = serde::de::value::MapAccessDeserializer::new(map);
     385           72 :                 let body = TimelineMetadataBodyV2::deserialize(de)?;
     386           72 :                 let hdr = TimelineMetadataHeader::try_from(&body).map_err(A::Error::custom)?;
     387              : 
     388           72 :                 Ok(TimelineMetadata { hdr, body })
     389           72 :             }
     390              :         }
     391              : 
     392          104 :         deserializer.deserialize_any(Visitor)
     393          104 :     }
     394              : 
     395         6121 :     pub(crate) fn serialize<S>(
     396         6121 :         metadata: &TimelineMetadata,
     397         6121 :         serializer: S,
     398         6121 :     ) -> Result<S::Ok, S::Error>
     399         6121 :     where
     400         6121 :         S: serde::Serializer,
     401         6121 :     {
     402         6121 :         // header is not needed, upon reading we've upgraded all v1 to v2
     403         6121 :         metadata.body.serialize(serializer)
     404         6121 :     }
     405              : 
     406              :     #[test]
     407            4 :     fn deserializes_bytes_as_well_as_equivalent_body_v2() {
     408            4 :         #[derive(serde::Deserialize, serde::Serialize)]
     409              :         struct Wrapper(
     410              :             #[serde(deserialize_with = "deserialize", serialize_with = "serialize")]
     411              :             TimelineMetadata,
     412              :         );
     413              : 
     414            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]";
     415            4 : 
     416            4 :         let wrapper_from_bytes = serde_json::from_str::<Wrapper>(too_many_bytes).unwrap();
     417            4 : 
     418            4 :         let serialized = serde_json::to_value(&wrapper_from_bytes).unwrap();
     419            4 : 
     420            4 :         assert_eq!(
     421            4 :             serialized,
     422            4 :             serde_json::json! {{
     423            4 :                 "disk_consistent_lsn": "0/149FD90",
     424            4 :                 "prev_record_lsn": "0/149FD18",
     425            4 :                 "ancestor_timeline": null,
     426            4 :                 "ancestor_lsn": "0/0",
     427            4 :                 "latest_gc_cutoff_lsn": "0/149FD18",
     428            4 :                 "initdb_lsn": "0/149FD18",
     429            4 :                 "pg_version": 15
     430            4 :             }}
     431            4 :         );
     432              : 
     433            4 :         let wrapper_from_json = serde_json::value::from_value::<Wrapper>(serialized).unwrap();
     434            4 : 
     435            4 :         assert_eq!(wrapper_from_bytes.0, wrapper_from_json.0);
     436            4 :     }
     437              : }
     438              : 
     439              : /// Parts of the metadata which are regularly modified.
     440              : pub(crate) struct MetadataUpdate {
     441              :     disk_consistent_lsn: Lsn,
     442              :     prev_record_lsn: Option<Lsn>,
     443              :     latest_gc_cutoff_lsn: Lsn,
     444              : }
     445              : 
     446              : impl MetadataUpdate {
     447         2456 :     pub(crate) fn new(
     448         2456 :         disk_consistent_lsn: Lsn,
     449         2456 :         prev_record_lsn: Option<Lsn>,
     450         2456 :         latest_gc_cutoff_lsn: Lsn,
     451         2456 :     ) -> Self {
     452         2456 :         Self {
     453         2456 :             disk_consistent_lsn,
     454         2456 :             prev_record_lsn,
     455         2456 :             latest_gc_cutoff_lsn,
     456         2456 :         }
     457         2456 :     }
     458              : }
     459              : 
     460              : #[cfg(test)]
     461              : mod tests {
     462              :     use super::*;
     463              :     use crate::tenant::harness::TIMELINE_ID;
     464              : 
     465              :     #[test]
     466            4 :     fn metadata_serializes_correctly() {
     467            4 :         let original_metadata = TimelineMetadata::new(
     468            4 :             Lsn(0x200),
     469            4 :             Some(Lsn(0x100)),
     470            4 :             Some(TIMELINE_ID),
     471            4 :             Lsn(0),
     472            4 :             Lsn(0),
     473            4 :             Lsn(0),
     474            4 :             // Any version will do here, so use the default
     475            4 :             crate::DEFAULT_PG_VERSION,
     476            4 :         );
     477            4 : 
     478            4 :         let metadata_bytes = original_metadata
     479            4 :             .to_bytes()
     480            4 :             .expect("Should serialize correct metadata to bytes");
     481            4 : 
     482            4 :         let deserialized_metadata = TimelineMetadata::from_bytes(&metadata_bytes)
     483            4 :             .expect("Should deserialize its own bytes");
     484            4 : 
     485            4 :         assert_eq!(
     486              :             deserialized_metadata.body, original_metadata.body,
     487            0 :             "Metadata that was serialized to bytes and deserialized back should not change"
     488              :         );
     489            4 :     }
     490              : 
     491              :     // Generate old version metadata and read it with current code.
     492              :     // Ensure that it is upgraded correctly
     493              :     #[test]
     494            4 :     fn test_metadata_upgrade() {
     495              :         #[derive(Debug, Clone, PartialEq, Eq)]
     496              :         struct TimelineMetadataV1 {
     497              :             hdr: TimelineMetadataHeader,
     498              :             body: TimelineMetadataBodyV1,
     499              :         }
     500              : 
     501            4 :         let metadata_v1 = TimelineMetadataV1 {
     502            4 :             hdr: TimelineMetadataHeader {
     503            4 :                 checksum: 0,
     504            4 :                 size: 0,
     505            4 :                 format_version: METADATA_OLD_FORMAT_VERSION,
     506            4 :             },
     507            4 :             body: TimelineMetadataBodyV1 {
     508            4 :                 disk_consistent_lsn: Lsn(0x200),
     509            4 :                 prev_record_lsn: Some(Lsn(0x100)),
     510            4 :                 ancestor_timeline: Some(TIMELINE_ID),
     511            4 :                 ancestor_lsn: Lsn(0),
     512            4 :                 latest_gc_cutoff_lsn: Lsn(0),
     513            4 :                 initdb_lsn: Lsn(0),
     514            4 :             },
     515            4 :         };
     516              : 
     517              :         impl TimelineMetadataV1 {
     518            4 :             pub fn to_bytes(&self) -> anyhow::Result<Vec<u8>> {
     519            4 :                 let body_bytes = self.body.ser()?;
     520            4 :                 let metadata_size = METADATA_HDR_SIZE + body_bytes.len();
     521            4 :                 let hdr = TimelineMetadataHeader {
     522            4 :                     size: metadata_size as u16,
     523            4 :                     format_version: METADATA_OLD_FORMAT_VERSION,
     524            4 :                     checksum: crc32c::crc32c(&body_bytes),
     525            4 :                 };
     526            4 :                 let hdr_bytes = hdr.ser()?;
     527            4 :                 let mut metadata_bytes = vec![0u8; METADATA_MAX_SIZE];
     528            4 :                 metadata_bytes[0..METADATA_HDR_SIZE].copy_from_slice(&hdr_bytes);
     529            4 :                 metadata_bytes[METADATA_HDR_SIZE..metadata_size].copy_from_slice(&body_bytes);
     530            4 :                 Ok(metadata_bytes)
     531            4 :             }
     532              :         }
     533              : 
     534            4 :         let metadata_bytes = metadata_v1
     535            4 :             .to_bytes()
     536            4 :             .expect("Should serialize correct metadata to bytes");
     537            4 : 
     538            4 :         // This should deserialize to the latest version format
     539            4 :         let deserialized_metadata = TimelineMetadata::from_bytes(&metadata_bytes)
     540            4 :             .expect("Should deserialize its own bytes");
     541            4 : 
     542            4 :         let expected_metadata = TimelineMetadata::new(
     543            4 :             Lsn(0x200),
     544            4 :             Some(Lsn(0x100)),
     545            4 :             Some(TIMELINE_ID),
     546            4 :             Lsn(0),
     547            4 :             Lsn(0),
     548            4 :             Lsn(0),
     549            4 :             14, // All timelines created before this version had pg_version 14
     550            4 :         );
     551            4 : 
     552            4 :         assert_eq!(
     553              :             deserialized_metadata.body, expected_metadata.body,
     554            0 :             "Metadata of the old version {} should be upgraded to the latest version {}",
     555              :             METADATA_OLD_FORMAT_VERSION, METADATA_FORMAT_VERSION
     556              :         );
     557            4 :     }
     558              : 
     559              :     #[test]
     560            4 :     fn test_metadata_bincode_serde_ensure_roundtrip() {
     561            4 :         let original_metadata = TimelineMetadata::new(
     562            4 :             Lsn(0x200),
     563            4 :             Some(Lsn(0x100)),
     564            4 :             Some(TIMELINE_ID),
     565            4 :             Lsn(0),
     566            4 :             Lsn(0),
     567            4 :             Lsn(0),
     568            4 :             // Any version will do here, so use the default
     569            4 :             crate::DEFAULT_PG_VERSION,
     570            4 :         );
     571            4 :         let expected_bytes = vec![
     572            4 :             /* TimelineMetadataHeader */
     573            4 :             74, 104, 158, 105, 0, 70, 0, 4, // checksum, size, format_version (4 + 2 + 2)
     574            4 :             /* TimelineMetadataBodyV2 */
     575            4 :             0, 0, 0, 0, 0, 0, 2, 0, // disk_consistent_lsn (8 bytes)
     576            4 :             1, 0, 0, 0, 0, 0, 0, 1, 0, // prev_record_lsn (9 bytes)
     577            4 :             1, 17, 34, 51, 68, 85, 102, 119, 136, 17, 34, 51, 68, 85, 102, 119,
     578            4 :             136, // ancestor_timeline (17 bytes)
     579            4 :             0, 0, 0, 0, 0, 0, 0, 0, // ancestor_lsn (8 bytes)
     580            4 :             0, 0, 0, 0, 0, 0, 0, 0, // latest_gc_cutoff_lsn (8 bytes)
     581            4 :             0, 0, 0, 0, 0, 0, 0, 0, // initdb_lsn (8 bytes)
     582            4 :             0, 0, 0, 16, // pg_version (4 bytes)
     583            4 :             /* padding bytes */
     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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     599            4 :             0, 0, 0, 0, 0, 0, 0,
     600            4 :         ];
     601            4 :         let metadata_ser_bytes = original_metadata.to_bytes().unwrap();
     602            4 :         assert_eq!(metadata_ser_bytes, expected_bytes);
     603              : 
     604            4 :         let expected_metadata = {
     605            4 :             let mut temp_metadata = original_metadata;
     606            4 :             let body_bytes = temp_metadata
     607            4 :                 .body
     608            4 :                 .ser()
     609            4 :                 .expect("Cannot serialize the metadata body");
     610            4 :             let metadata_size = METADATA_HDR_SIZE + body_bytes.len();
     611            4 :             let hdr = TimelineMetadataHeader {
     612            4 :                 size: metadata_size as u16,
     613            4 :                 format_version: METADATA_FORMAT_VERSION,
     614            4 :                 checksum: crc32c::crc32c(&body_bytes),
     615            4 :             };
     616            4 :             temp_metadata.hdr = hdr;
     617            4 :             temp_metadata
     618            4 :         };
     619            4 :         let des_metadata = TimelineMetadata::from_bytes(&metadata_ser_bytes).unwrap();
     620            4 :         assert_eq!(des_metadata, expected_metadata);
     621            4 :     }
     622              : }
        

Generated by: LCOV version 2.1-beta