Line data Source code
1 : //!
2 : //! Helper functions for dealing with filenames of the image and delta layer files.
3 : //!
4 : use crate::repository::Key;
5 : use std::borrow::Cow;
6 : use std::cmp::Ordering;
7 : use std::fmt;
8 : use std::ops::Range;
9 : use std::str::FromStr;
10 :
11 : use regex::Regex;
12 : use utils::lsn::Lsn;
13 :
14 : use super::PersistentLayerDesc;
15 :
16 : // Note: Timeline::load_layer_map() relies on this sort order
17 : #[derive(PartialEq, Eq, Clone, Hash)]
18 : pub struct DeltaLayerName {
19 : pub key_range: Range<Key>,
20 : pub lsn_range: Range<Lsn>,
21 : }
22 :
23 : impl std::fmt::Debug for DeltaLayerName {
24 0 : fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 : use super::RangeDisplayDebug;
26 :
27 0 : f.debug_struct("DeltaLayerName")
28 0 : .field("key_range", &RangeDisplayDebug(&self.key_range))
29 0 : .field("lsn_range", &self.lsn_range)
30 0 : .finish()
31 0 : }
32 : }
33 :
34 : impl PartialOrd for DeltaLayerName {
35 0 : fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
36 0 : Some(self.cmp(other))
37 0 : }
38 : }
39 :
40 : impl Ord for DeltaLayerName {
41 0 : fn cmp(&self, other: &Self) -> Ordering {
42 0 : let mut cmp = self.key_range.start.cmp(&other.key_range.start);
43 0 : if cmp != Ordering::Equal {
44 0 : return cmp;
45 0 : }
46 0 : cmp = self.key_range.end.cmp(&other.key_range.end);
47 0 : if cmp != Ordering::Equal {
48 0 : return cmp;
49 0 : }
50 0 : cmp = self.lsn_range.start.cmp(&other.lsn_range.start);
51 0 : if cmp != Ordering::Equal {
52 0 : return cmp;
53 0 : }
54 0 : cmp = self.lsn_range.end.cmp(&other.lsn_range.end);
55 0 :
56 0 : cmp
57 0 : }
58 : }
59 :
60 : /// Represents the region of the LSN-Key space covered by a DeltaLayer
61 : ///
62 : /// ```text
63 : /// <key start>-<key end>__<LSN start>-<LSN end>
64 : /// ```
65 : impl DeltaLayerName {
66 : /// Parse the part of a delta layer's file name that represents the LayerName. Returns None
67 : /// if the filename does not match the expected pattern.
68 9774 : pub fn parse_str(fname: &str) -> Option<Self> {
69 9774 : let mut parts = fname.split("__");
70 9774 : let mut key_parts = parts.next()?.split('-');
71 9774 : let mut lsn_parts = parts.next()?.split('-');
72 :
73 9774 : let key_start_str = key_parts.next()?;
74 9774 : let key_end_str = key_parts.next()?;
75 9774 : let lsn_start_str = lsn_parts.next()?;
76 9774 : let lsn_end_str = lsn_parts.next()?;
77 :
78 4914 : if parts.next().is_some() || key_parts.next().is_some() || key_parts.next().is_some() {
79 0 : return None;
80 4914 : }
81 4914 :
82 4914 : if key_start_str.len() != 36
83 4914 : || key_end_str.len() != 36
84 4914 : || lsn_start_str.len() != 16
85 4914 : || lsn_end_str.len() != 16
86 : {
87 0 : return None;
88 4914 : }
89 :
90 4914 : let key_start = Key::from_hex(key_start_str).ok()?;
91 4914 : let key_end = Key::from_hex(key_end_str).ok()?;
92 :
93 4914 : let start_lsn = Lsn::from_hex(lsn_start_str).ok()?;
94 4914 : let end_lsn = Lsn::from_hex(lsn_end_str).ok()?;
95 :
96 4914 : if start_lsn >= end_lsn {
97 0 : return None;
98 : // or panic?
99 4914 : }
100 4914 :
101 4914 : if key_start >= key_end {
102 0 : return None;
103 : // or panic?
104 4914 : }
105 4914 :
106 4914 : Some(DeltaLayerName {
107 4914 : key_range: key_start..key_end,
108 4914 : lsn_range: start_lsn..end_lsn,
109 4914 : })
110 9774 : }
111 : }
112 :
113 : impl fmt::Display for DeltaLayerName {
114 113970 : fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115 113970 : write!(
116 113970 : f,
117 113970 : "{}-{}__{:016X}-{:016X}",
118 113970 : self.key_range.start,
119 113970 : self.key_range.end,
120 113970 : u64::from(self.lsn_range.start),
121 113970 : u64::from(self.lsn_range.end),
122 113970 : )
123 113970 : }
124 : }
125 :
126 : #[derive(PartialEq, Eq, Clone, Hash)]
127 : pub struct ImageLayerName {
128 : pub key_range: Range<Key>,
129 : pub lsn: Lsn,
130 : }
131 :
132 : impl std::fmt::Debug for ImageLayerName {
133 0 : fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134 : use super::RangeDisplayDebug;
135 :
136 0 : f.debug_struct("ImageLayerName")
137 0 : .field("key_range", &RangeDisplayDebug(&self.key_range))
138 0 : .field("lsn", &self.lsn)
139 0 : .finish()
140 0 : }
141 : }
142 :
143 : impl PartialOrd for ImageLayerName {
144 0 : fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
145 0 : Some(self.cmp(other))
146 0 : }
147 : }
148 :
149 : impl Ord for ImageLayerName {
150 0 : fn cmp(&self, other: &Self) -> Ordering {
151 0 : let mut cmp = self.key_range.start.cmp(&other.key_range.start);
152 0 : if cmp != Ordering::Equal {
153 0 : return cmp;
154 0 : }
155 0 : cmp = self.key_range.end.cmp(&other.key_range.end);
156 0 : if cmp != Ordering::Equal {
157 0 : return cmp;
158 0 : }
159 0 : cmp = self.lsn.cmp(&other.lsn);
160 0 :
161 0 : cmp
162 0 : }
163 : }
164 :
165 : impl ImageLayerName {
166 4812 : pub fn lsn_as_range(&self) -> Range<Lsn> {
167 4812 : // Saves from having to copypaste this all over
168 4812 : PersistentLayerDesc::image_layer_lsn_range(self.lsn)
169 4812 : }
170 : }
171 :
172 : ///
173 : /// Represents the part of the Key-LSN space covered by an ImageLayer
174 : ///
175 : /// ```text
176 : /// <key start>-<key end>__<LSN>
177 : /// ```
178 : impl ImageLayerName {
179 : /// Parse a string as then LayerName part of an image layer file name. Returns None if the
180 : /// filename does not match the expected pattern.
181 9774 : pub fn parse_str(fname: &str) -> Option<Self> {
182 9774 : let mut parts = fname.split("__");
183 9774 : let mut key_parts = parts.next()?.split('-');
184 :
185 9774 : let key_start_str = key_parts.next()?;
186 9774 : let key_end_str = key_parts.next()?;
187 9774 : let lsn_str = parts.next()?;
188 9774 : if parts.next().is_some() || key_parts.next().is_some() {
189 0 : return None;
190 9774 : }
191 9774 :
192 9774 : if key_start_str.len() != 36 || key_end_str.len() != 36 || lsn_str.len() != 16 {
193 4914 : return None;
194 4860 : }
195 :
196 4860 : let key_start = Key::from_hex(key_start_str).ok()?;
197 4860 : let key_end = Key::from_hex(key_end_str).ok()?;
198 :
199 4860 : let lsn = Lsn::from_hex(lsn_str).ok()?;
200 :
201 4860 : Some(ImageLayerName {
202 4860 : key_range: key_start..key_end,
203 4860 : lsn,
204 4860 : })
205 9774 : }
206 : }
207 :
208 : impl fmt::Display for ImageLayerName {
209 10530 : fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
210 10530 : write!(
211 10530 : f,
212 10530 : "{}-{}__{:016X}",
213 10530 : self.key_range.start,
214 10530 : self.key_range.end,
215 10530 : u64::from(self.lsn),
216 10530 : )
217 10530 : }
218 : }
219 :
220 : /// LayerName is the logical identity of a layer within a LayerMap at a moment in time.
221 : ///
222 : /// The LayerName is not a unique filename, as the same LayerName may have multiple physical incarnations
223 : /// over time (e.g. across shard splits or compression). The physical filenames of layers in local
224 : /// storage and object names in remote storage consist of the LayerName plus some extra qualifiers
225 : /// that uniquely identify the physical incarnation of a layer (see [crate::tenant::remote_timeline_client::remote_layer_path])
226 : /// and [`crate::tenant::storage_layer::layer::local_layer_path`])
227 : #[derive(Debug, PartialEq, Eq, Hash, Clone)]
228 : pub enum LayerName {
229 : Image(ImageLayerName),
230 : Delta(DeltaLayerName),
231 : }
232 :
233 : impl LayerName {
234 : /// Determines if this layer file is considered to be in future meaning we will discard these
235 : /// layers during timeline initialization from the given disk_consistent_lsn.
236 52511 : pub(crate) fn is_in_future(&self, disk_consistent_lsn: Lsn) -> bool {
237 : use LayerName::*;
238 3151 : match self {
239 3151 : Image(file_name) if file_name.lsn > disk_consistent_lsn => true,
240 49360 : Delta(file_name) if file_name.lsn_range.end > disk_consistent_lsn + 1 => true,
241 52478 : _ => false,
242 : }
243 52511 : }
244 :
245 0 : pub(crate) fn kind(&self) -> &'static str {
246 : use LayerName::*;
247 0 : match self {
248 0 : Delta(_) => "delta",
249 0 : Image(_) => "image",
250 : }
251 0 : }
252 :
253 : /// Gets the key range encoded in the layer name.
254 24 : pub fn key_range(&self) -> &Range<Key> {
255 24 : match &self {
256 12 : LayerName::Image(layer) => &layer.key_range,
257 12 : LayerName::Delta(layer) => &layer.key_range,
258 : }
259 24 : }
260 :
261 24 : pub fn is_delta(&self) -> bool {
262 24 : matches!(self, LayerName::Delta(_))
263 24 : }
264 : }
265 :
266 : impl fmt::Display for LayerName {
267 16881 : fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
268 16881 : match self {
269 2535 : Self::Image(fname) => write!(f, "{fname}"),
270 14346 : Self::Delta(fname) => write!(f, "{fname}"),
271 : }
272 16881 : }
273 : }
274 :
275 : impl From<ImageLayerName> for LayerName {
276 3213 : fn from(fname: ImageLayerName) -> Self {
277 3213 : Self::Image(fname)
278 3213 : }
279 : }
280 : impl From<DeltaLayerName> for LayerName {
281 19266 : fn from(fname: DeltaLayerName) -> Self {
282 19266 : Self::Delta(fname)
283 19266 : }
284 : }
285 :
286 : impl FromStr for LayerName {
287 : type Err = String;
288 :
289 : /// Conversion from either a physical layer filename, or the string-ization of
290 : /// Self. When loading a physical layer filename, we drop any extra information
291 : /// not needed to build Self.
292 9774 : fn from_str(value: &str) -> Result<Self, Self::Err> {
293 9774 : let gen_suffix_regex = Regex::new("^(?<base>.+)(?<gen>-v1-[0-9a-f]{8})$").unwrap();
294 9774 : let file_name: Cow<str> = match gen_suffix_regex.captures(value) {
295 60 : Some(captures) => captures
296 60 : .name("base")
297 60 : .expect("Non-optional group")
298 60 : .as_str()
299 60 : .into(),
300 9714 : None => value.into(),
301 : };
302 :
303 9774 : let delta = DeltaLayerName::parse_str(&file_name);
304 9774 : let image = ImageLayerName::parse_str(&file_name);
305 9774 : let ok = match (delta, image) {
306 : (None, None) => {
307 0 : return Err(format!(
308 0 : "neither delta nor image layer file name: {value:?}"
309 0 : ))
310 : }
311 4914 : (Some(delta), None) => Self::Delta(delta),
312 4860 : (None, Some(image)) => Self::Image(image),
313 0 : (Some(_), Some(_)) => unreachable!(),
314 : };
315 9774 : Ok(ok)
316 9774 : }
317 : }
318 :
319 : impl serde::Serialize for LayerName {
320 106125 : fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
321 106125 : where
322 106125 : S: serde::Serializer,
323 106125 : {
324 106125 : match self {
325 6501 : Self::Image(fname) => serializer.collect_str(fname),
326 99624 : Self::Delta(fname) => serializer.collect_str(fname),
327 : }
328 106125 : }
329 : }
330 :
331 : impl<'de> serde::Deserialize<'de> for LayerName {
332 9564 : fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
333 9564 : where
334 9564 : D: serde::Deserializer<'de>,
335 9564 : {
336 9564 : deserializer.deserialize_string(LayerNameVisitor)
337 9564 : }
338 : }
339 :
340 : struct LayerNameVisitor;
341 :
342 : impl<'de> serde::de::Visitor<'de> for LayerNameVisitor {
343 : type Value = LayerName;
344 :
345 0 : fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
346 0 : write!(
347 0 : formatter,
348 0 : "a string that is a valid image or delta layer file name"
349 0 : )
350 0 : }
351 9564 : fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
352 9564 : where
353 9564 : E: serde::de::Error,
354 9564 : {
355 9564 : v.parse().map_err(|e| E::custom(e))
356 9564 : }
357 : }
358 :
359 : #[cfg(test)]
360 : mod test {
361 : use super::*;
362 : #[test]
363 6 : fn image_layer_parse() {
364 6 : let expected = LayerName::Image(ImageLayerName {
365 6 : key_range: Key::from_i128(0)
366 6 : ..Key::from_hex("000000067F00000001000004DF0000000006").unwrap(),
367 6 : lsn: Lsn::from_hex("00000000014FED58").unwrap(),
368 6 : });
369 6 : let parsed = LayerName::from_str("000000000000000000000000000000000000-000000067F00000001000004DF0000000006__00000000014FED58-v1-00000001").unwrap();
370 6 : assert_eq!(parsed, expected,);
371 :
372 : // Omitting generation suffix is valid
373 6 : let parsed = LayerName::from_str("000000000000000000000000000000000000-000000067F00000001000004DF0000000006__00000000014FED58").unwrap();
374 6 : assert_eq!(parsed, expected,);
375 6 : }
376 :
377 : #[test]
378 6 : fn delta_layer_parse() {
379 6 : let expected = LayerName::Delta(DeltaLayerName {
380 6 : key_range: Key::from_i128(0)
381 6 : ..Key::from_hex("000000067F00000001000004DF0000000006").unwrap(),
382 6 : lsn_range: Lsn::from_hex("00000000014FED58").unwrap()
383 6 : ..Lsn::from_hex("000000000154C481").unwrap(),
384 6 : });
385 6 : let parsed = LayerName::from_str("000000000000000000000000000000000000-000000067F00000001000004DF0000000006__00000000014FED58-000000000154C481-v1-00000001").unwrap();
386 6 : assert_eq!(parsed, expected);
387 :
388 : // Omitting generation suffix is valid
389 6 : let parsed = LayerName::from_str("000000000000000000000000000000000000-000000067F00000001000004DF0000000006__00000000014FED58-000000000154C481").unwrap();
390 6 : assert_eq!(parsed, expected);
391 6 : }
392 : }
|