LCOV - code coverage report
Current view: top level - pageserver/src/tenant/storage_layer - layer_desc.rs (source / functions) Coverage Total Hit
Test: 75747cdbffeb0b6d2a2a311584368de68cd9aadc.info Lines: 95.1 % 142 135
Test Date: 2024-06-24 06:52:57 Functions: 61.5 % 26 16

            Line data    Source code
       1              : use core::fmt::Display;
       2              : use pageserver_api::shard::TenantShardId;
       3              : use std::ops::Range;
       4              : use utils::{id::TimelineId, lsn::Lsn};
       5              : 
       6              : use crate::repository::Key;
       7              : 
       8              : use super::{DeltaLayerName, ImageLayerName, LayerName};
       9              : 
      10              : use serde::{Deserialize, Serialize};
      11              : 
      12              : #[cfg(test)]
      13              : use utils::id::TenantId;
      14              : 
      15              : /// A unique identifier of a persistent layer. This is different from `LayerDescriptor`, which is only used in the
      16              : /// benchmarks. This struct contains all necessary information to find the image / delta layer. It also provides
      17              : /// a unified way to generate layer information like file name.
      18            0 : #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Hash)]
      19              : pub struct PersistentLayerDesc {
      20              :     pub tenant_shard_id: TenantShardId,
      21              :     pub timeline_id: TimelineId,
      22              :     /// Range of keys that this layer covers
      23              :     pub key_range: Range<Key>,
      24              :     /// Inclusive start, exclusive end of the LSN range that this layer holds.
      25              :     ///
      26              :     /// - For an open in-memory layer, the end bound is MAX_LSN
      27              :     /// - For a frozen in-memory layer or a delta layer, the end bound is a valid lsn after the
      28              :     /// range start
      29              :     /// - An image layer represents snapshot at one LSN, so end_lsn is always the snapshot LSN + 1
      30              :     pub lsn_range: Range<Lsn>,
      31              :     /// Whether this is a delta layer, and also, is this incremental.
      32              :     pub is_delta: bool,
      33              :     pub file_size: u64,
      34              : }
      35              : 
      36              : /// A unique identifier of a persistent layer within the context of one timeline.
      37              : #[derive(Debug, PartialEq, Eq, Clone, Hash)]
      38              : pub struct PersistentLayerKey {
      39              :     pub key_range: Range<Key>,
      40              :     pub lsn_range: Range<Lsn>,
      41              :     pub is_delta: bool,
      42              : }
      43              : 
      44              : impl PersistentLayerDesc {
      45       348070 :     pub fn key(&self) -> PersistentLayerKey {
      46       348070 :         PersistentLayerKey {
      47       348070 :             key_range: self.key_range.clone(),
      48       348070 :             lsn_range: self.lsn_range.clone(),
      49       348070 :             is_delta: self.is_delta,
      50       348070 :         }
      51       348070 :     }
      52              : 
      53         1896 :     pub fn short_id(&self) -> impl Display {
      54         1896 :         self.layer_name()
      55         1896 :     }
      56              : 
      57              :     #[cfg(test)]
      58           10 :     pub fn new_test(key_range: Range<Key>, lsn_range: Range<Lsn>, is_delta: bool) -> Self {
      59           10 :         Self {
      60           10 :             tenant_shard_id: TenantShardId::unsharded(TenantId::generate()),
      61           10 :             timeline_id: TimelineId::generate(),
      62           10 :             key_range,
      63           10 :             lsn_range,
      64           10 :             is_delta,
      65           10 :             file_size: 0,
      66           10 :         }
      67           10 :     }
      68              : 
      69          230 :     pub fn new_img(
      70          230 :         tenant_shard_id: TenantShardId,
      71          230 :         timeline_id: TimelineId,
      72          230 :         key_range: Range<Key>,
      73          230 :         lsn: Lsn,
      74          230 :         file_size: u64,
      75          230 :     ) -> Self {
      76          230 :         Self {
      77          230 :             tenant_shard_id,
      78          230 :             timeline_id,
      79          230 :             key_range,
      80          230 :             lsn_range: Self::image_layer_lsn_range(lsn),
      81          230 :             is_delta: false,
      82          230 :             file_size,
      83          230 :         }
      84          230 :     }
      85              : 
      86         1338 :     pub fn new_delta(
      87         1338 :         tenant_shard_id: TenantShardId,
      88         1338 :         timeline_id: TimelineId,
      89         1338 :         key_range: Range<Key>,
      90         1338 :         lsn_range: Range<Lsn>,
      91         1338 :         file_size: u64,
      92         1338 :     ) -> Self {
      93         1338 :         Self {
      94         1338 :             tenant_shard_id,
      95         1338 :             timeline_id,
      96         1338 :             key_range,
      97         1338 :             lsn_range,
      98         1338 :             is_delta: true,
      99         1338 :             file_size,
     100         1338 :         }
     101         1338 :     }
     102              : 
     103           24 :     pub fn from_filename(
     104           24 :         tenant_shard_id: TenantShardId,
     105           24 :         timeline_id: TimelineId,
     106           24 :         filename: LayerName,
     107           24 :         file_size: u64,
     108           24 :     ) -> Self {
     109           24 :         match filename {
     110            4 :             LayerName::Image(i) => {
     111            4 :                 Self::new_img(tenant_shard_id, timeline_id, i.key_range, i.lsn, file_size)
     112              :             }
     113           20 :             LayerName::Delta(d) => Self::new_delta(
     114           20 :                 tenant_shard_id,
     115           20 :                 timeline_id,
     116           20 :                 d.key_range,
     117           20 :                 d.lsn_range,
     118           20 :                 file_size,
     119           20 :             ),
     120              :         }
     121           24 :     }
     122              : 
     123              :     /// Get the LSN that the image layer covers.
     124        14142 :     pub fn image_layer_lsn(&self) -> Lsn {
     125        14142 :         assert!(!self.is_delta);
     126        14142 :         assert!(self.lsn_range.start + 1 == self.lsn_range.end);
     127        14142 :         self.lsn_range.start
     128        14142 :     }
     129              : 
     130              :     /// Get the LSN range corresponding to a single image layer LSN.
     131          230 :     pub fn image_layer_lsn_range(lsn: Lsn) -> Range<Lsn> {
     132          230 :         lsn..(lsn + 1)
     133          230 :     }
     134              : 
     135              :     /// Get a delta layer name for this layer.
     136              :     ///
     137              :     /// Panic: if this is not a delta layer.
     138         7591 :     pub fn delta_layer_name(&self) -> DeltaLayerName {
     139         7591 :         assert!(self.is_delta);
     140         7591 :         DeltaLayerName {
     141         7591 :             key_range: self.key_range.clone(),
     142         7591 :             lsn_range: self.lsn_range.clone(),
     143         7591 :         }
     144         7591 :     }
     145              : 
     146              :     /// Get a image layer name for this layer.
     147              :     ///
     148              :     /// Panic: if this is not an image layer, or the lsn range is invalid
     149         1053 :     pub fn image_layer_name(&self) -> ImageLayerName {
     150         1053 :         assert!(!self.is_delta);
     151         1053 :         assert!(self.lsn_range.start + 1 == self.lsn_range.end);
     152         1053 :         ImageLayerName {
     153         1053 :             key_range: self.key_range.clone(),
     154         1053 :             lsn: self.lsn_range.start,
     155         1053 :         }
     156         1053 :     }
     157              : 
     158         8644 :     pub fn layer_name(&self) -> LayerName {
     159         8644 :         if self.is_delta {
     160         7591 :             self.delta_layer_name().into()
     161              :         } else {
     162         1053 :             self.image_layer_name().into()
     163              :         }
     164         8644 :     }
     165              : 
     166              :     // TODO: remove this in the future once we refactor timeline APIs.
     167              : 
     168      1190503 :     pub fn get_lsn_range(&self) -> Range<Lsn> {
     169      1190503 :         self.lsn_range.clone()
     170      1190503 :     }
     171              : 
     172        15910 :     pub fn get_key_range(&self) -> Range<Key> {
     173        15910 :         self.key_range.clone()
     174        15910 :     }
     175              : 
     176            0 :     pub fn get_timeline_id(&self) -> TimelineId {
     177            0 :         self.timeline_id
     178            0 :     }
     179              : 
     180              :     /// Does this layer only contain some data for the key-range (incremental),
     181              :     /// or does it contain a version of every page? This is important to know
     182              :     /// for garbage collecting old layers: an incremental layer depends on
     183              :     /// the previous non-incremental layer.
     184         1992 :     pub fn is_incremental(&self) -> bool {
     185         1992 :         self.is_delta
     186         1992 :     }
     187              : 
     188       563088 :     pub fn is_delta(&self) -> bool {
     189       563088 :         self.is_delta
     190       563088 :     }
     191              : 
     192           16 :     pub fn dump(&self) {
     193           16 :         if self.is_delta {
     194           14 :             println!(
     195           14 :                 "----- delta layer for ten {} tli {} keys {}-{} lsn {}-{} is_incremental {} size {} ----",
     196           14 :                 self.tenant_shard_id,
     197           14 :                 self.timeline_id,
     198           14 :                 self.key_range.start,
     199           14 :                 self.key_range.end,
     200           14 :                 self.lsn_range.start,
     201           14 :                 self.lsn_range.end,
     202           14 :                 self.is_incremental(),
     203           14 :                 self.file_size,
     204           14 :             );
     205           14 :         } else {
     206            2 :             println!(
     207            2 :                 "----- image layer for ten {} tli {} key {}-{} at {} is_incremental {} size {} ----",
     208            2 :                 self.tenant_shard_id,
     209            2 :                 self.timeline_id,
     210            2 :                 self.key_range.start,
     211            2 :                 self.key_range.end,
     212            2 :                 self.image_layer_lsn(),
     213            2 :                 self.is_incremental(),
     214            2 :                 self.file_size
     215            2 :             );
     216            2 :         }
     217           16 :     }
     218              : 
     219            0 :     pub fn file_size(&self) -> u64 {
     220            0 :         self.file_size
     221            0 :     }
     222              : }
        

Generated by: LCOV version 2.1-beta