Line data Source code
1 : //! In-memory index to track the tenant files on the remote storage.
2 : //! Able to restore itself from the storage index parts, that are located in every timeline's remote directory and contain all data about
3 : //! remote timeline layers and its metadata.
4 :
5 : use std::collections::HashMap;
6 :
7 : use chrono::NaiveDateTime;
8 : use pageserver_api::models::AuxFilePolicy;
9 : use serde::{Deserialize, Serialize};
10 : use utils::id::TimelineId;
11 :
12 : use crate::tenant::metadata::TimelineMetadata;
13 : use crate::tenant::storage_layer::LayerName;
14 : use crate::tenant::Generation;
15 : use pageserver_api::shard::ShardIndex;
16 :
17 : use utils::lsn::Lsn;
18 :
19 : /// In-memory representation of an `index_part.json` file
20 : ///
21 : /// Contains the data about all files in the timeline, present remotely and its metadata.
22 : ///
23 : /// This type needs to be backwards and forwards compatible. When changing the fields,
24 : /// remember to add a test case for the changed version.
25 2805 : #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
26 : pub struct IndexPart {
27 : /// Debugging aid describing the version of this type.
28 : #[serde(default)]
29 : version: usize,
30 :
31 : #[serde(default)]
32 : #[serde(skip_serializing_if = "Option::is_none")]
33 : pub deleted_at: Option<NaiveDateTime>,
34 :
35 : /// Per layer file name metadata, which can be present for a present or missing layer file.
36 : ///
37 : /// Older versions of `IndexPart` will not have this property or have only a part of metadata
38 : /// that latest version stores.
39 : pub layer_metadata: HashMap<LayerName, LayerFileMetadata>,
40 :
41 : /// Because of the trouble of eyeballing the legacy "metadata" field, we copied the
42 : /// "disk_consistent_lsn" out. After version 7 this is no longer needed, but the name cannot be
43 : /// reused.
44 : pub(super) disk_consistent_lsn: Lsn,
45 :
46 : // TODO: rename as "metadata" next week, keep the alias = "metadata_bytes", bump version Adding
47 : // the "alias = metadata" was forgotten in #7693, so we have to use "rewrite = metadata_bytes"
48 : // for backwards compatibility.
49 : #[serde(
50 : rename = "metadata_bytes",
51 : alias = "metadata",
52 : with = "crate::tenant::metadata::modern_serde"
53 : )]
54 : pub metadata: TimelineMetadata,
55 :
56 : #[serde(default)]
57 : pub(crate) lineage: Lineage,
58 :
59 : /// Describes the kind of aux files stored in the timeline.
60 : ///
61 : /// The value is modified during file ingestion when the latest wanted value communicated via tenant config is applied if it is acceptable.
62 : /// A V1 setting after V2 files have been committed is not accepted.
63 : ///
64 : /// None means no aux files have been written to the storage before the point
65 : /// when this flag is introduced.
66 : #[serde(skip_serializing_if = "Option::is_none", default)]
67 : pub(crate) last_aux_file_policy: Option<AuxFilePolicy>,
68 : }
69 :
70 : impl IndexPart {
71 : /// When adding or modifying any parts of `IndexPart`, increment the version so that it can be
72 : /// used to understand later versions.
73 : ///
74 : /// Version is currently informative only.
75 : /// Version history
76 : /// - 2: added `deleted_at`
77 : /// - 3: no longer deserialize `timeline_layers` (serialized format is the same, but timeline_layers
78 : /// is always generated from the keys of `layer_metadata`)
79 : /// - 4: timeline_layers is fully removed.
80 : /// - 5: lineage was added
81 : /// - 6: last_aux_file_policy is added.
82 : /// - 7: metadata_bytes is no longer written, but still read
83 : const LATEST_VERSION: usize = 7;
84 :
85 : // Versions we may see when reading from a bucket.
86 : pub const KNOWN_VERSIONS: &'static [usize] = &[1, 2, 3, 4, 5, 6, 7];
87 :
88 : pub const FILE_NAME: &'static str = "index_part.json";
89 :
90 396 : pub(crate) fn empty(metadata: TimelineMetadata) -> Self {
91 396 : IndexPart {
92 396 : version: Self::LATEST_VERSION,
93 396 : layer_metadata: Default::default(),
94 396 : disk_consistent_lsn: metadata.disk_consistent_lsn(),
95 396 : metadata,
96 396 : deleted_at: None,
97 396 : lineage: Default::default(),
98 396 : last_aux_file_policy: None,
99 396 : }
100 396 : }
101 :
102 0 : pub fn version(&self) -> usize {
103 0 : self.version
104 0 : }
105 :
106 : /// If you want this under normal operations, read it from self.metadata:
107 : /// this method is just for the scrubber to use when validating an index.
108 0 : pub fn duplicated_disk_consistent_lsn(&self) -> Lsn {
109 0 : self.disk_consistent_lsn
110 0 : }
111 :
112 16 : pub fn from_s3_bytes(bytes: &[u8]) -> Result<Self, serde_json::Error> {
113 16 : serde_json::from_slice::<IndexPart>(bytes)
114 16 : }
115 :
116 1371 : pub fn to_s3_bytes(&self) -> serde_json::Result<Vec<u8>> {
117 1371 : serde_json::to_vec(self)
118 1371 : }
119 :
120 : #[cfg(test)]
121 12 : pub(crate) fn example() -> Self {
122 12 : Self::empty(TimelineMetadata::example())
123 12 : }
124 :
125 12 : pub(crate) fn last_aux_file_policy(&self) -> Option<AuxFilePolicy> {
126 12 : self.last_aux_file_policy
127 12 : }
128 : }
129 :
130 : /// Metadata gathered for each of the layer files.
131 : ///
132 : /// Fields have to be `Option`s because remote [`IndexPart`]'s can be from different version, which
133 : /// might have less or more metadata depending if upgrading or rolling back an upgrade.
134 132 : #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
135 : pub struct LayerFileMetadata {
136 : pub file_size: u64,
137 :
138 : #[serde(default = "Generation::none")]
139 : #[serde(skip_serializing_if = "Generation::is_none")]
140 : pub generation: Generation,
141 :
142 : #[serde(default = "ShardIndex::unsharded")]
143 : #[serde(skip_serializing_if = "ShardIndex::is_unsharded")]
144 : pub shard: ShardIndex,
145 : }
146 :
147 : impl LayerFileMetadata {
148 2332 : pub fn new(file_size: u64, generation: Generation, shard: ShardIndex) -> Self {
149 2332 : LayerFileMetadata {
150 2332 : file_size,
151 2332 : generation,
152 2332 : shard,
153 2332 : }
154 2332 : }
155 : }
156 :
157 : /// Limited history of earlier ancestors.
158 : ///
159 : /// A timeline can have more than 1 earlier ancestor, in the rare case that it was repeatedly
160 : /// reparented by having an later timeline be detached from it's ancestor.
161 32 : #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Default)]
162 : pub(crate) struct Lineage {
163 : /// Has the `reparenting_history` been truncated to [`Lineage::REMEMBER_AT_MOST`].
164 : #[serde(skip_serializing_if = "is_false", default)]
165 : reparenting_history_truncated: bool,
166 :
167 : /// Earlier ancestors, truncated when [`Self::reparenting_history_truncated`]
168 : ///
169 : /// These are stored in case we want to support WAL based DR on the timeline. There can be many
170 : /// of these and at most one [`Self::original_ancestor`]. There cannot be more reparentings
171 : /// after [`Self::original_ancestor`] has been set.
172 : #[serde(skip_serializing_if = "Vec::is_empty", default)]
173 : reparenting_history: Vec<TimelineId>,
174 :
175 : /// The ancestor from which this timeline has been detached from and when.
176 : ///
177 : /// If you are adding support for detaching from a hierarchy, consider changing the ancestry
178 : /// into a `Vec<(TimelineId, Lsn)>` to be a path instead.
179 : // FIXME: this is insufficient even for path of two timelines for future wal recovery
180 : // purposes:
181 : //
182 : // assuming a "old main" which has received most of the WAL, and has a branch "new main",
183 : // starting a bit before "old main" last_record_lsn. the current version works fine,
184 : // because we will know to replay wal and branch at the recorded Lsn to do wal recovery.
185 : //
186 : // then assuming "new main" would similarly receive a branch right before its last_record_lsn,
187 : // "new new main". the current implementation would just store ("new main", ancestor_lsn, _)
188 : // here. however, we cannot recover from WAL using only that information, we would need the
189 : // whole ancestry here:
190 : //
191 : // ```json
192 : // [
193 : // ["old main", ancestor_lsn("new main"), _],
194 : // ["new main", ancestor_lsn("new new main"), _]
195 : // ]
196 : // ```
197 : #[serde(skip_serializing_if = "Option::is_none", default)]
198 : original_ancestor: Option<(TimelineId, Lsn, NaiveDateTime)>,
199 : }
200 :
201 5610 : fn is_false(b: &bool) -> bool {
202 5610 : !b
203 5610 : }
204 :
205 : impl Lineage {
206 : const REMEMBER_AT_MOST: usize = 100;
207 :
208 0 : pub(crate) fn record_previous_ancestor(&mut self, old_ancestor: &TimelineId) {
209 0 : if self.reparenting_history.last() == Some(old_ancestor) {
210 : // do not re-record it
211 0 : return;
212 0 : }
213 0 :
214 0 : let drop_oldest = self.reparenting_history.len() + 1 >= Self::REMEMBER_AT_MOST;
215 0 :
216 0 : self.reparenting_history_truncated |= drop_oldest;
217 0 : if drop_oldest {
218 0 : self.reparenting_history.remove(0);
219 0 : }
220 0 : self.reparenting_history.push(*old_ancestor);
221 0 : }
222 :
223 0 : pub(crate) fn record_detaching(&mut self, branchpoint: &(TimelineId, Lsn)) {
224 0 : assert!(self.original_ancestor.is_none());
225 :
226 0 : self.original_ancestor =
227 0 : Some((branchpoint.0, branchpoint.1, chrono::Utc::now().naive_utc()));
228 0 : }
229 :
230 : /// The queried lsn is most likely the basebackup lsn, and this answers question "is it allowed
231 : /// to start a read/write primary at this lsn".
232 : ///
233 : /// Returns true if the Lsn was previously our branch point.
234 0 : pub(crate) fn is_previous_ancestor_lsn(&self, lsn: Lsn) -> bool {
235 0 : self.original_ancestor
236 0 : .is_some_and(|(_, ancestor_lsn, _)| ancestor_lsn == lsn)
237 0 : }
238 :
239 0 : pub(crate) fn is_detached_from_original_ancestor(&self) -> bool {
240 0 : self.original_ancestor.is_some()
241 0 : }
242 :
243 0 : pub(crate) fn is_reparented(&self) -> bool {
244 0 : !self.reparenting_history.is_empty()
245 0 : }
246 : }
247 :
248 : #[cfg(test)]
249 : mod tests {
250 : use super::*;
251 : use std::str::FromStr;
252 : use utils::id::TimelineId;
253 :
254 : #[test]
255 2 : fn v1_indexpart_is_parsed() {
256 2 : let example = r#"{
257 2 : "version":1,
258 2 : "timeline_layers":["000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__0000000001696070-00000000016960E9"],
259 2 : "layer_metadata":{
260 2 : "000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__0000000001696070-00000000016960E9": { "file_size": 25600000 },
261 2 : "000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000016B59D8-00000000016B5A51": { "file_size": 9007199254741001 }
262 2 : },
263 2 : "disk_consistent_lsn":"0/16960E8",
264 2 : "metadata_bytes":[113,11,159,210,0,54,0,4,0,0,0,0,1,105,96,232,1,0,0,0,0,1,105,96,112,0,0,0,0,0,0,0,0,0,0,0,0,0,1,105,96,112,0,0,0,0,1,105,96,112,0,0,0,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
265 2 : }"#;
266 2 :
267 2 : let expected = IndexPart {
268 2 : // note this is not verified, could be anything, but exists for humans debugging.. could be the git version instead?
269 2 : version: 1,
270 2 : layer_metadata: HashMap::from([
271 2 : ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__0000000001696070-00000000016960E9".parse().unwrap(), LayerFileMetadata {
272 2 : file_size: 25600000,
273 2 : generation: Generation::none(),
274 2 : shard: ShardIndex::unsharded()
275 2 : }),
276 2 : ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000016B59D8-00000000016B5A51".parse().unwrap(), LayerFileMetadata {
277 2 : // serde_json should always parse this but this might be a double with jq for
278 2 : // example.
279 2 : file_size: 9007199254741001,
280 2 : generation: Generation::none(),
281 2 : shard: ShardIndex::unsharded()
282 2 : })
283 2 : ]),
284 2 : disk_consistent_lsn: "0/16960E8".parse::<Lsn>().unwrap(),
285 2 : metadata: TimelineMetadata::from_bytes(&[113,11,159,210,0,54,0,4,0,0,0,0,1,105,96,232,1,0,0,0,0,1,105,96,112,0,0,0,0,0,0,0,0,0,0,0,0,0,1,105,96,112,0,0,0,0,1,105,96,112,0,0,0,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]).unwrap(),
286 2 : deleted_at: None,
287 2 : lineage: Lineage::default(),
288 2 : last_aux_file_policy: None,
289 2 : };
290 2 :
291 2 : let part = IndexPart::from_s3_bytes(example.as_bytes()).unwrap();
292 2 : assert_eq!(part, expected);
293 2 : }
294 :
295 : #[test]
296 2 : fn v1_indexpart_is_parsed_with_optional_missing_layers() {
297 2 : let example = r#"{
298 2 : "version":1,
299 2 : "timeline_layers":["000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__0000000001696070-00000000016960E9"],
300 2 : "missing_layers":["This shouldn't fail deserialization"],
301 2 : "layer_metadata":{
302 2 : "000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__0000000001696070-00000000016960E9": { "file_size": 25600000 },
303 2 : "000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000016B59D8-00000000016B5A51": { "file_size": 9007199254741001 }
304 2 : },
305 2 : "disk_consistent_lsn":"0/16960E8",
306 2 : "metadata_bytes":[113,11,159,210,0,54,0,4,0,0,0,0,1,105,96,232,1,0,0,0,0,1,105,96,112,0,0,0,0,0,0,0,0,0,0,0,0,0,1,105,96,112,0,0,0,0,1,105,96,112,0,0,0,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
307 2 : }"#;
308 2 :
309 2 : let expected = IndexPart {
310 2 : // note this is not verified, could be anything, but exists for humans debugging.. could be the git version instead?
311 2 : version: 1,
312 2 : layer_metadata: HashMap::from([
313 2 : ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__0000000001696070-00000000016960E9".parse().unwrap(), LayerFileMetadata {
314 2 : file_size: 25600000,
315 2 : generation: Generation::none(),
316 2 : shard: ShardIndex::unsharded()
317 2 : }),
318 2 : ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000016B59D8-00000000016B5A51".parse().unwrap(), LayerFileMetadata {
319 2 : // serde_json should always parse this but this might be a double with jq for
320 2 : // example.
321 2 : file_size: 9007199254741001,
322 2 : generation: Generation::none(),
323 2 : shard: ShardIndex::unsharded()
324 2 : })
325 2 : ]),
326 2 : disk_consistent_lsn: "0/16960E8".parse::<Lsn>().unwrap(),
327 2 : metadata: TimelineMetadata::from_bytes(&[113,11,159,210,0,54,0,4,0,0,0,0,1,105,96,232,1,0,0,0,0,1,105,96,112,0,0,0,0,0,0,0,0,0,0,0,0,0,1,105,96,112,0,0,0,0,1,105,96,112,0,0,0,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]).unwrap(),
328 2 : deleted_at: None,
329 2 : lineage: Lineage::default(),
330 2 : last_aux_file_policy: None,
331 2 : };
332 2 :
333 2 : let part = IndexPart::from_s3_bytes(example.as_bytes()).unwrap();
334 2 : assert_eq!(part, expected);
335 2 : }
336 :
337 : #[test]
338 2 : fn v2_indexpart_is_parsed_with_deleted_at() {
339 2 : let example = r#"{
340 2 : "version":2,
341 2 : "timeline_layers":["000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__0000000001696070-00000000016960E9"],
342 2 : "missing_layers":["This shouldn't fail deserialization"],
343 2 : "layer_metadata":{
344 2 : "000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__0000000001696070-00000000016960E9": { "file_size": 25600000 },
345 2 : "000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000016B59D8-00000000016B5A51": { "file_size": 9007199254741001 }
346 2 : },
347 2 : "disk_consistent_lsn":"0/16960E8",
348 2 : "metadata_bytes":[113,11,159,210,0,54,0,4,0,0,0,0,1,105,96,232,1,0,0,0,0,1,105,96,112,0,0,0,0,0,0,0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 2 : // note this is not verified, could be anything, but exists for humans debugging.. could be the git version instead?
354 2 : version: 2,
355 2 : layer_metadata: HashMap::from([
356 2 : ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__0000000001696070-00000000016960E9".parse().unwrap(), LayerFileMetadata {
357 2 : file_size: 25600000,
358 2 : generation: Generation::none(),
359 2 : shard: ShardIndex::unsharded()
360 2 : }),
361 2 : ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000016B59D8-00000000016B5A51".parse().unwrap(), LayerFileMetadata {
362 2 : // serde_json should always parse this but this might be a double with jq for
363 2 : // example.
364 2 : file_size: 9007199254741001,
365 2 : generation: Generation::none(),
366 2 : shard: ShardIndex::unsharded()
367 2 : })
368 2 : ]),
369 2 : disk_consistent_lsn: "0/16960E8".parse::<Lsn>().unwrap(),
370 2 : metadata: TimelineMetadata::from_bytes(&[113,11,159,210,0,54,0,4,0,0,0,0,1,105,96,232,1,0,0,0,0,1,105,96,112,0,0,0,0,0,0,0,0,0,0,0,0,0,1,105,96,112,0,0,0,0,1,105,96,112,0,0,0,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]).unwrap(),
371 2 : deleted_at: Some(parse_naive_datetime("2023-07-31T09:00:00.123000000")),
372 2 : lineage: Lineage::default(),
373 2 : last_aux_file_policy: None,
374 2 : };
375 2 :
376 2 : let part = IndexPart::from_s3_bytes(example.as_bytes()).unwrap();
377 2 : assert_eq!(part, expected);
378 2 : }
379 :
380 : #[test]
381 2 : fn empty_layers_are_parsed() {
382 2 : let empty_layers_json = r#"{
383 2 : "version":1,
384 2 : "timeline_layers":[],
385 2 : "layer_metadata":{},
386 2 : "disk_consistent_lsn":"0/2532648",
387 2 : "metadata_bytes":[136,151,49,208,0,70,0,4,0,0,0,0,2,83,38,72,1,0,0,0,0,2,83,38,32,1,87,198,240,135,97,119,45,125,38,29,155,161,140,141,255,210,0,0,0,0,2,83,38,72,0,0,0,0,1,73,240,192,0,0,0,0,1,73,240,192,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]
388 2 : }"#;
389 2 :
390 2 : let expected = IndexPart {
391 2 : version: 1,
392 2 : layer_metadata: HashMap::new(),
393 2 : disk_consistent_lsn: "0/2532648".parse::<Lsn>().unwrap(),
394 2 : metadata: TimelineMetadata::from_bytes(&[
395 2 : 136, 151, 49, 208, 0, 70, 0, 4, 0, 0, 0, 0, 2, 83, 38, 72, 1, 0, 0, 0, 0, 2, 83,
396 2 : 38, 32, 1, 87, 198, 240, 135, 97, 119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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
409 2 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
410 2 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
411 2 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
412 2 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
413 2 : 0, 0, 0, 0, 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 2 : 0, 0,
415 2 : ])
416 2 : .unwrap(),
417 2 : deleted_at: None,
418 2 : lineage: Lineage::default(),
419 2 : last_aux_file_policy: None,
420 2 : };
421 2 :
422 2 : let empty_layers_parsed = IndexPart::from_s3_bytes(empty_layers_json.as_bytes()).unwrap();
423 2 :
424 2 : assert_eq!(empty_layers_parsed, expected);
425 2 : }
426 :
427 : #[test]
428 2 : fn v4_indexpart_is_parsed() {
429 2 : let example = r#"{
430 2 : "version":4,
431 2 : "layer_metadata":{
432 2 : "000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__0000000001696070-00000000016960E9": { "file_size": 25600000 },
433 2 : "000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000016B59D8-00000000016B5A51": { "file_size": 9007199254741001 }
434 2 : },
435 2 : "disk_consistent_lsn":"0/16960E8",
436 2 : "metadata_bytes":[113,11,159,210,0,54,0,4,0,0,0,0,1,105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123"
438 2 : }"#;
439 2 :
440 2 : let expected = IndexPart {
441 2 : version: 4,
442 2 : layer_metadata: HashMap::from([
443 2 : ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__0000000001696070-00000000016960E9".parse().unwrap(), LayerFileMetadata {
444 2 : file_size: 25600000,
445 2 : generation: Generation::none(),
446 2 : shard: ShardIndex::unsharded()
447 2 : }),
448 2 : ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000016B59D8-00000000016B5A51".parse().unwrap(), LayerFileMetadata {
449 2 : // serde_json should always parse this but this might be a double with jq for
450 2 : // example.
451 2 : file_size: 9007199254741001,
452 2 : generation: Generation::none(),
453 2 : shard: ShardIndex::unsharded()
454 2 : })
455 2 : ]),
456 2 : disk_consistent_lsn: "0/16960E8".parse::<Lsn>().unwrap(),
457 2 : metadata: TimelineMetadata::from_bytes(&[113,11,159,210,0,54,0,4,0,0,0,0,1,105,96,232,1,0,0,0,0,1,105,96,112,0,0,0,0,0,0,0,0,0,0,0,0,0,1,105,96,112,0,0,0,0,1,105,96,112,0,0,0,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]).unwrap(),
458 2 : deleted_at: Some(parse_naive_datetime("2023-07-31T09:00:00.123000000")),
459 2 : lineage: Lineage::default(),
460 2 : last_aux_file_policy: None,
461 2 : };
462 2 :
463 2 : let part = IndexPart::from_s3_bytes(example.as_bytes()).unwrap();
464 2 : assert_eq!(part, expected);
465 2 : }
466 :
467 : #[test]
468 2 : fn v5_indexpart_is_parsed() {
469 2 : let example = r#"{
470 2 : "version":5,
471 2 : "layer_metadata":{
472 2 : "000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000014EF420-00000000014EF499":{"file_size":23289856,"generation":1},
473 2 : "000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000014EF499-00000000015A7619":{"file_size":1015808,"generation":1}},
474 2 : "disk_consistent_lsn":"0/15A7618",
475 2 : "metadata_bytes":[226,88,25,241,0,46,0,4,0,0,0,0,1,90,118,24,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,78,244,32,0,0,0,0,1,78,244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2 : "reparenting_history":["e1bfd8c633d713d279e6fcd2bcc15b6d"]
479 2 : }
480 2 : }"#;
481 2 :
482 2 : let expected = IndexPart {
483 2 : version: 5,
484 2 : layer_metadata: HashMap::from([
485 2 : ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000014EF420-00000000014EF499".parse().unwrap(), LayerFileMetadata {
486 2 : file_size: 23289856,
487 2 : generation: Generation::new(1),
488 2 : shard: ShardIndex::unsharded(),
489 2 : }),
490 2 : ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000014EF499-00000000015A7619".parse().unwrap(), LayerFileMetadata {
491 2 : file_size: 1015808,
492 2 : generation: Generation::new(1),
493 2 : shard: ShardIndex::unsharded(),
494 2 : })
495 2 : ]),
496 2 : disk_consistent_lsn: Lsn::from_str("0/15A7618").unwrap(),
497 2 : metadata: TimelineMetadata::from_bytes(&[226,88,25,241,0,46,0,4,0,0,0,0,1,90,118,24,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,78,244,32,0,0,0,0,1,78,244,32,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]).unwrap(),
498 2 : deleted_at: None,
499 2 : lineage: Lineage {
500 2 : reparenting_history_truncated: false,
501 2 : reparenting_history: vec![TimelineId::from_str("e1bfd8c633d713d279e6fcd2bcc15b6d").unwrap()],
502 2 : original_ancestor: Some((TimelineId::from_str("e2bfd8c633d713d279e6fcd2bcc15b6d").unwrap(), Lsn::from_str("0/15A7618").unwrap(), parse_naive_datetime("2024-05-07T18:52:36.322426563"))),
503 2 : },
504 2 : last_aux_file_policy: None,
505 2 : };
506 2 :
507 2 : let part = IndexPart::from_s3_bytes(example.as_bytes()).unwrap();
508 2 : assert_eq!(part, expected);
509 2 : }
510 :
511 : #[test]
512 2 : fn v6_indexpart_is_parsed() {
513 2 : let example = r#"{
514 2 : "version":6,
515 2 : "layer_metadata":{
516 2 : "000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__0000000001696070-00000000016960E9": { "file_size": 25600000 },
517 2 : "000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
521 2 : "deleted_at": "2023-07-31T09:00:00.123",
522 2 : "lineage":{
523 2 : "original_ancestor":["e2bfd8c633d713d279e6fcd2bcc15b6d","0/15A7618","2024-05-07T18:52:36.322426563"],
524 2 : "reparenting_history":["e1bfd8c633d713d279e6fcd2bcc15b6d"]
525 2 : },
526 2 : "last_aux_file_policy": "V2"
527 2 : }"#;
528 2 :
529 2 : let expected = IndexPart {
530 2 : version: 6,
531 2 : layer_metadata: HashMap::from([
532 2 : ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__0000000001696070-00000000016960E9".parse().unwrap(), LayerFileMetadata {
533 2 : file_size: 25600000,
534 2 : generation: Generation::none(),
535 2 : shard: ShardIndex::unsharded()
536 2 : }),
537 2 : ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000016B59D8-00000000016B5A51".parse().unwrap(), LayerFileMetadata {
538 2 : // serde_json should always parse this but this might be a double with jq for
539 2 : // example.
540 2 : file_size: 9007199254741001,
541 2 : generation: Generation::none(),
542 2 : shard: ShardIndex::unsharded()
543 2 : })
544 2 : ]),
545 2 : disk_consistent_lsn: "0/16960E8".parse::<Lsn>().unwrap(),
546 2 : metadata: TimelineMetadata::from_bytes(&[113,11,159,210,0,54,0,4,0,0,0,0,1,105,96,232,1,0,0,0,0,1,105,96,112,0,0,0,0,0,0,0,0,0,0,0,0,0,1,105,96,112,0,0,0,0,1,105,96,112,0,0,0,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]).unwrap(),
547 2 : deleted_at: Some(parse_naive_datetime("2023-07-31T09:00:00.123000000")),
548 2 : lineage: Lineage {
549 2 : reparenting_history_truncated: false,
550 2 : reparenting_history: vec![TimelineId::from_str("e1bfd8c633d713d279e6fcd2bcc15b6d").unwrap()],
551 2 : original_ancestor: Some((TimelineId::from_str("e2bfd8c633d713d279e6fcd2bcc15b6d").unwrap(), Lsn::from_str("0/15A7618").unwrap(), parse_naive_datetime("2024-05-07T18:52:36.322426563"))),
552 2 : },
553 2 : last_aux_file_policy: Some(AuxFilePolicy::V2),
554 2 : };
555 2 :
556 2 : let part = IndexPart::from_s3_bytes(example.as_bytes()).unwrap();
557 2 : assert_eq!(part, expected);
558 2 : }
559 :
560 : #[test]
561 2 : fn v7_indexpart_is_parsed() {
562 2 : let example = r#"{
563 2 : "version": 7,
564 2 : "layer_metadata":{
565 2 : "000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__0000000001696070-00000000016960E9": { "file_size": 25600000 },
566 2 : "000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000016B59D8-00000000016B5A51": { "file_size": 9007199254741001 }
567 2 : },
568 2 : "disk_consistent_lsn":"0/16960E8",
569 2 : "metadata": {
570 2 : "disk_consistent_lsn": "0/16960E8",
571 2 : "prev_record_lsn": "0/1696070",
572 2 : "ancestor_timeline": "e45a7f37d3ee2ff17dc14bf4f4e3f52e",
573 2 : "ancestor_lsn": "0/0",
574 2 : "latest_gc_cutoff_lsn": "0/1696070",
575 2 : "initdb_lsn": "0/1696070",
576 2 : "pg_version": 14
577 2 : },
578 2 : "deleted_at": "2023-07-31T09:00:00.123"
579 2 : }"#;
580 2 :
581 2 : let expected = IndexPart {
582 2 : version: 7,
583 2 : layer_metadata: HashMap::from([
584 2 : ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__0000000001696070-00000000016960E9".parse().unwrap(), LayerFileMetadata {
585 2 : file_size: 25600000,
586 2 : generation: Generation::none(),
587 2 : shard: ShardIndex::unsharded()
588 2 : }),
589 2 : ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000016B59D8-00000000016B5A51".parse().unwrap(), LayerFileMetadata {
590 2 : file_size: 9007199254741001,
591 2 : generation: Generation::none(),
592 2 : shard: ShardIndex::unsharded()
593 2 : })
594 2 : ]),
595 2 : disk_consistent_lsn: "0/16960E8".parse::<Lsn>().unwrap(),
596 2 : metadata: TimelineMetadata::new(
597 2 : Lsn::from_str("0/16960E8").unwrap(),
598 2 : Some(Lsn::from_str("0/1696070").unwrap()),
599 2 : Some(TimelineId::from_str("e45a7f37d3ee2ff17dc14bf4f4e3f52e").unwrap()),
600 2 : Lsn::INVALID,
601 2 : Lsn::from_str("0/1696070").unwrap(),
602 2 : Lsn::from_str("0/1696070").unwrap(),
603 2 : 14,
604 2 : ).with_recalculated_checksum().unwrap(),
605 2 : deleted_at: Some(parse_naive_datetime("2023-07-31T09:00:00.123000000")),
606 2 : lineage: Default::default(),
607 2 : last_aux_file_policy: Default::default(),
608 2 : };
609 2 :
610 2 : let part = IndexPart::from_s3_bytes(example.as_bytes()).unwrap();
611 2 : assert_eq!(part, expected);
612 2 : }
613 :
614 12 : fn parse_naive_datetime(s: &str) -> NaiveDateTime {
615 12 : chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S.%f").unwrap()
616 12 : }
617 : }
|