LCOV - code coverage report
Current view: top level - pageserver/src/tenant/storage_layer - layer.rs (source / functions) Coverage Total Hit
Test: aca806cab4756d7eb6a304846130f4a73a5d5393.info Lines: 77.5 % 1358 1053
Test Date: 2025-04-24 20:31:15 Functions: 76.2 % 151 115

            Line data    Source code
       1              : use std::ops::Range;
       2              : use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
       3              : use std::sync::{Arc, Weak};
       4              : use std::time::{Duration, SystemTime};
       5              : 
       6              : use crate::PERF_TRACE_TARGET;
       7              : use anyhow::Context;
       8              : use camino::{Utf8Path, Utf8PathBuf};
       9              : use pageserver_api::keyspace::KeySpace;
      10              : use pageserver_api::models::HistoricLayerInfo;
      11              : use pageserver_api::shard::{ShardIdentity, ShardIndex, TenantShardId};
      12              : use tracing::{Instrument, info_span};
      13              : use utils::generation::Generation;
      14              : use utils::id::TimelineId;
      15              : use utils::lsn::Lsn;
      16              : use utils::sync::{gate, heavier_once_cell};
      17              : 
      18              : use super::delta_layer::{self};
      19              : use super::image_layer::{self};
      20              : use super::{
      21              :     AsLayerDesc, ImageLayerWriter, LayerAccessStats, LayerAccessStatsReset, LayerName,
      22              :     LayerVisibilityHint, PerfInstrumentFutureExt, PersistentLayerDesc, ValuesReconstructState,
      23              : };
      24              : use crate::config::PageServerConf;
      25              : use crate::context::{DownloadBehavior, RequestContext, RequestContextBuilder};
      26              : use crate::span::debug_assert_current_span_has_tenant_and_timeline_id;
      27              : use crate::task_mgr::TaskKind;
      28              : use crate::tenant::Timeline;
      29              : use crate::tenant::remote_timeline_client::LayerFileMetadata;
      30              : use crate::tenant::timeline::{CompactionError, GetVectoredError};
      31              : 
      32              : #[cfg(test)]
      33              : mod tests;
      34              : 
      35              : #[cfg(test)]
      36              : mod failpoints;
      37              : 
      38              : pub const S3_UPLOAD_LIMIT: u64 = 4_500_000_000;
      39              : 
      40              : /// A Layer contains all data in a "rectangle" consisting of a range of keys and
      41              : /// range of LSNs.
      42              : ///
      43              : /// There are two kinds of layers, in-memory and on-disk layers. In-memory
      44              : /// layers are used to ingest incoming WAL, and provide fast access to the
      45              : /// recent page versions. On-disk layers are stored as files on disk, and are
      46              : /// immutable. This type represents the on-disk kind while in-memory kind are represented by
      47              : /// [`InMemoryLayer`].
      48              : ///
      49              : /// Furthermore, there are two kinds of on-disk layers: delta and image layers.
      50              : /// A delta layer contains all modifications within a range of LSNs and keys.
      51              : /// An image layer is a snapshot of all the data in a key-range, at a single
      52              : /// LSN.
      53              : ///
      54              : /// This type models the on-disk layers, which can be evicted and on-demand downloaded. As a
      55              : /// general goal, read accesses should always win eviction and eviction should not wait for
      56              : /// download.
      57              : ///
      58              : /// ### State transitions
      59              : ///
      60              : /// The internal state of `Layer` is composed of most importantly the on-filesystem state and the
      61              : /// [`ResidentOrWantedEvicted`] enum. On-filesystem state can be either present (fully downloaded,
      62              : /// right size) or deleted.
      63              : ///
      64              : /// Reads will always win requests to evict until `wait_for_turn_and_evict` has acquired the
      65              : /// `heavier_once_cell::InitPermit` and has started to `evict_blocking`. Before the
      66              : /// `heavier_once_cell::InitPermit` has been acquired, any read request
      67              : /// (`get_or_maybe_download`) can "re-initialize" using the existing downloaded file and thus
      68              : /// cancelling the eviction.
      69              : ///
      70              : /// ```text
      71              : ///  +-----------------+   get_or_maybe_download    +--------------------------------+
      72              : ///  | not initialized |--------------------------->| Resident(Arc<DownloadedLayer>) |
      73              : ///  |     ENOENT      |                         /->|                                |
      74              : ///  +-----------------+                         |  +--------------------------------+
      75              : ///                  ^                           |                         |       ^
      76              : ///                  |    get_or_maybe_download  |                         |       | get_or_maybe_download, either:
      77              : ///   evict_blocking | /-------------------------/                         |       | - upgrade weak to strong
      78              : ///                  | |                                                   |       | - re-initialize without download
      79              : ///                  | |                                    evict_and_wait |       |
      80              : ///  +-----------------+                                                   v       |
      81              : ///  | not initialized |  on_downloaded_layer_drop  +--------------------------------------+
      82              : ///  | file is present |<---------------------------| WantedEvicted(Weak<DownloadedLayer>) |
      83              : ///  +-----------------+                            +--------------------------------------+
      84              : /// ```
      85              : ///
      86              : /// ### Unsupported
      87              : ///
      88              : /// - Evicting by the operator deleting files from the filesystem
      89              : ///
      90              : /// [`InMemoryLayer`]: super::inmemory_layer::InMemoryLayer
      91              : #[derive(Clone)]
      92              : pub(crate) struct Layer(Arc<LayerInner>);
      93              : 
      94              : impl std::fmt::Display for Layer {
      95        13068 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
      96        13068 :         write!(
      97        13068 :             f,
      98        13068 :             "{}{}",
      99        13068 :             self.layer_desc().short_id(),
     100        13068 :             self.0.generation.get_suffix()
     101        13068 :         )
     102        13068 :     }
     103              : }
     104              : 
     105              : impl std::fmt::Debug for Layer {
     106           24 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
     107           24 :         write!(f, "{}", self)
     108           24 :     }
     109              : }
     110              : 
     111              : impl AsLayerDesc for Layer {
     112     12135085 :     fn layer_desc(&self) -> &PersistentLayerDesc {
     113     12135085 :         self.0.layer_desc()
     114     12135085 :     }
     115              : }
     116              : 
     117              : impl PartialEq for Layer {
     118           19 :     fn eq(&self, other: &Self) -> bool {
     119           19 :         Arc::as_ptr(&self.0) == Arc::as_ptr(&other.0)
     120           19 :     }
     121              : }
     122              : 
     123        11616 : pub(crate) fn local_layer_path(
     124        11616 :     conf: &PageServerConf,
     125        11616 :     tenant_shard_id: &TenantShardId,
     126        11616 :     timeline_id: &TimelineId,
     127        11616 :     layer_file_name: &LayerName,
     128        11616 :     generation: &Generation,
     129        11616 : ) -> Utf8PathBuf {
     130        11616 :     let timeline_path = conf.timeline_path(tenant_shard_id, timeline_id);
     131        11616 : 
     132        11616 :     if generation.is_none() {
     133              :         // Without a generation, we may only use legacy path style
     134            0 :         timeline_path.join(layer_file_name.to_string())
     135              :     } else {
     136        11616 :         timeline_path.join(format!("{}-v1{}", layer_file_name, generation.get_suffix()))
     137              :     }
     138        11616 : }
     139              : 
     140              : pub(crate) enum LastEviction {
     141              :     Never,
     142              :     At(std::time::Instant),
     143              :     Evicting,
     144              : }
     145              : 
     146              : impl LastEviction {
     147          156 :     pub(crate) fn happened_after(&self, timepoint: std::time::Instant) -> bool {
     148          156 :         match self {
     149            0 :             LastEviction::Never => false,
     150          156 :             LastEviction::At(evicted_at) => evicted_at > &timepoint,
     151            0 :             LastEviction::Evicting => true,
     152              :         }
     153          156 :     }
     154              : }
     155              : 
     156              : impl Layer {
     157              :     /// Creates a layer value for a file we know to not be resident.
     158            0 :     pub(crate) fn for_evicted(
     159            0 :         conf: &'static PageServerConf,
     160            0 :         timeline: &Arc<Timeline>,
     161            0 :         file_name: LayerName,
     162            0 :         metadata: LayerFileMetadata,
     163            0 :     ) -> Self {
     164            0 :         let local_path = local_layer_path(
     165            0 :             conf,
     166            0 :             &timeline.tenant_shard_id,
     167            0 :             &timeline.timeline_id,
     168            0 :             &file_name,
     169            0 :             &metadata.generation,
     170            0 :         );
     171            0 : 
     172            0 :         let desc = PersistentLayerDesc::from_filename(
     173            0 :             timeline.tenant_shard_id,
     174            0 :             timeline.timeline_id,
     175            0 :             file_name,
     176            0 :             metadata.file_size,
     177            0 :         );
     178            0 : 
     179            0 :         let owner = Layer(Arc::new(LayerInner::new(
     180            0 :             conf,
     181            0 :             timeline,
     182            0 :             local_path,
     183            0 :             desc,
     184            0 :             None,
     185            0 :             metadata.generation,
     186            0 :             metadata.shard,
     187            0 :         )));
     188            0 : 
     189            0 :         debug_assert!(owner.0.needs_download_blocking().unwrap().is_some());
     190              : 
     191            0 :         owner
     192            0 :     }
     193              : 
     194              :     /// Creates a Layer value for a file we know to be resident in timeline directory.
     195          732 :     pub(crate) fn for_resident(
     196          732 :         conf: &'static PageServerConf,
     197          732 :         timeline: &Arc<Timeline>,
     198          732 :         local_path: Utf8PathBuf,
     199          732 :         file_name: LayerName,
     200          732 :         metadata: LayerFileMetadata,
     201          732 :     ) -> ResidentLayer {
     202          732 :         let desc = PersistentLayerDesc::from_filename(
     203          732 :             timeline.tenant_shard_id,
     204          732 :             timeline.timeline_id,
     205          732 :             file_name,
     206          732 :             metadata.file_size,
     207          732 :         );
     208          732 : 
     209          732 :         let mut resident = None;
     210          732 : 
     211          732 :         let owner = Layer(Arc::new_cyclic(|owner| {
     212          732 :             let inner = Arc::new(DownloadedLayer {
     213          732 :                 owner: owner.clone(),
     214          732 :                 kind: tokio::sync::OnceCell::default(),
     215          732 :                 version: 0,
     216          732 :             });
     217          732 :             resident = Some(inner.clone());
     218          732 : 
     219          732 :             LayerInner::new(
     220          732 :                 conf,
     221          732 :                 timeline,
     222          732 :                 local_path,
     223          732 :                 desc,
     224          732 :                 Some(inner),
     225          732 :                 metadata.generation,
     226          732 :                 metadata.shard,
     227          732 :             )
     228          732 :         }));
     229          732 : 
     230          732 :         let downloaded = resident.expect("just initialized");
     231          732 : 
     232          732 :         debug_assert!(owner.0.needs_download_blocking().unwrap().is_none());
     233              : 
     234          732 :         timeline
     235          732 :             .metrics
     236          732 :             .resident_physical_size_add(metadata.file_size);
     237          732 : 
     238          732 :         ResidentLayer { downloaded, owner }
     239          732 :     }
     240              : 
     241              :     /// Creates a Layer value for freshly written out new layer file by renaming it from a
     242              :     /// temporary path.
     243        10980 :     pub(crate) fn finish_creating(
     244        10980 :         conf: &'static PageServerConf,
     245        10980 :         timeline: &Arc<Timeline>,
     246        10980 :         desc: PersistentLayerDesc,
     247        10980 :         temp_path: &Utf8Path,
     248        10980 :     ) -> anyhow::Result<ResidentLayer> {
     249        10980 :         let mut resident = None;
     250        10980 : 
     251        10980 :         let owner = Layer(Arc::new_cyclic(|owner| {
     252        10980 :             let inner = Arc::new(DownloadedLayer {
     253        10980 :                 owner: owner.clone(),
     254        10980 :                 kind: tokio::sync::OnceCell::default(),
     255        10980 :                 version: 0,
     256        10980 :             });
     257        10980 :             resident = Some(inner.clone());
     258        10980 : 
     259        10980 :             let local_path = local_layer_path(
     260        10980 :                 conf,
     261        10980 :                 &timeline.tenant_shard_id,
     262        10980 :                 &timeline.timeline_id,
     263        10980 :                 &desc.layer_name(),
     264        10980 :                 &timeline.generation,
     265        10980 :             );
     266        10980 : 
     267        10980 :             LayerInner::new(
     268        10980 :                 conf,
     269        10980 :                 timeline,
     270        10980 :                 local_path,
     271        10980 :                 desc,
     272        10980 :                 Some(inner),
     273        10980 :                 timeline.generation,
     274        10980 :                 timeline.get_shard_index(),
     275        10980 :             )
     276        10980 :         }));
     277        10980 : 
     278        10980 :         let downloaded = resident.expect("just initialized");
     279        10980 : 
     280        10980 :         // We never want to overwrite an existing file, so we use `RENAME_NOREPLACE`.
     281        10980 :         // TODO: this leaves the temp file in place if the rename fails, risking us running
     282        10980 :         // out of space. Should we clean it up here or does the calling context deal with this?
     283        10980 :         utils::fs_ext::rename_noreplace(temp_path.as_std_path(), owner.local_path().as_std_path())
     284        10980 :             .with_context(|| format!("rename temporary file as correct path for {owner}"))?;
     285              : 
     286        10980 :         Ok(ResidentLayer { downloaded, owner })
     287        10980 :     }
     288              : 
     289              :     /// Requests the layer to be evicted and waits for this to be done.
     290              :     ///
     291              :     /// If the file is not resident, an [`EvictionError::NotFound`] is returned.
     292              :     ///
     293              :     /// If for a bad luck or blocking of the executor, we miss the actual eviction and the layer is
     294              :     /// re-downloaded, [`EvictionError::Downloaded`] is returned.
     295              :     ///
     296              :     /// Timeout is mandatory, because waiting for eviction is only needed for our tests; eviction
     297              :     /// will happen regardless the future returned by this method completing unless there is a
     298              :     /// read access before eviction gets to complete.
     299              :     ///
     300              :     /// Technically cancellation safe, but cancelling might shift the viewpoint of what generation
     301              :     /// of download-evict cycle on retry.
     302          312 :     pub(crate) async fn evict_and_wait(&self, timeout: Duration) -> Result<(), EvictionError> {
     303          312 :         self.0.evict_and_wait(timeout).await
     304          288 :     }
     305              : 
     306              :     /// Delete the layer file when the `self` gets dropped, also try to schedule a remote index upload
     307              :     /// then.
     308              :     ///
     309              :     /// On drop, this will cause a call to [`crate::tenant::remote_timeline_client::RemoteTimelineClient::schedule_deletion_of_unlinked`].
     310              :     /// This means that the unlinking by [gc] or [compaction] must have happened strictly before
     311              :     /// the value this is called on gets dropped.
     312              :     ///
     313              :     /// This is ensured by both of those methods accepting references to Layer.
     314              :     ///
     315              :     /// [gc]: [`RemoteTimelineClient::schedule_gc_update`]
     316              :     /// [compaction]: [`RemoteTimelineClient::schedule_compaction_update`]
     317         3120 :     pub(crate) fn delete_on_drop(&self) {
     318         3120 :         self.0.delete_on_drop();
     319         3120 :     }
     320              : 
     321      1599487 :     pub(crate) async fn get_values_reconstruct_data(
     322      1599487 :         &self,
     323      1599487 :         keyspace: KeySpace,
     324      1599487 :         lsn_range: Range<Lsn>,
     325      1599487 :         reconstruct_data: &mut ValuesReconstructState,
     326      1599487 :         ctx: &RequestContext,
     327      1599487 :     ) -> Result<(), GetVectoredError> {
     328      1599487 :         let downloaded = {
     329      1599487 :             let ctx = RequestContextBuilder::from(ctx)
     330      1599487 :                 .perf_span(|crnt_perf_span| {
     331            0 :                     info_span!(
     332              :                         target: PERF_TRACE_TARGET,
     333            0 :                         parent: crnt_perf_span,
     334              :                         "GET_LAYER",
     335              :                     )
     336      1599487 :                 })
     337      1599487 :                 .attached_child();
     338      1599487 : 
     339      1599487 :             self.0
     340      1599487 :                 .get_or_maybe_download(true, &ctx)
     341      1599487 :                 .maybe_perf_instrument(&ctx, |crnt_perf_context| crnt_perf_context.clone())
     342      1599487 :                 .await
     343      1599487 :                 .map_err(|err| match err {
     344              :                     DownloadError::TimelineShutdown | DownloadError::DownloadCancelled => {
     345            0 :                         GetVectoredError::Cancelled
     346              :                     }
     347            0 :                     other => GetVectoredError::Other(anyhow::anyhow!(other)),
     348      1599487 :                 })?
     349              :         };
     350              : 
     351      1599487 :         let this = ResidentLayer {
     352      1599487 :             downloaded: downloaded.clone(),
     353      1599487 :             owner: self.clone(),
     354      1599487 :         };
     355      1599487 : 
     356      1599487 :         self.record_access(ctx);
     357      1599487 : 
     358      1599487 :         let ctx = RequestContextBuilder::from(ctx)
     359      1599487 :             .perf_span(|crnt_perf_span| {
     360            0 :                 info_span!(
     361              :                     target: PERF_TRACE_TARGET,
     362            0 :                     parent: crnt_perf_span,
     363              :                     "VISIT_LAYER",
     364              :                 )
     365      1599487 :             })
     366      1599487 :             .attached_child();
     367      1599487 : 
     368      1599487 :         downloaded
     369      1599487 :             .get_values_reconstruct_data(this, keyspace, lsn_range, reconstruct_data, &ctx)
     370      1599487 :             .instrument(tracing::debug_span!("get_values_reconstruct_data", layer=%self))
     371      1599487 :             .maybe_perf_instrument(&ctx, |crnt_perf_span| crnt_perf_span.clone())
     372      1599487 :             .await
     373      1599487 :             .map_err(|err| match err {
     374            0 :                 GetVectoredError::Other(err) => GetVectoredError::Other(
     375            0 :                     err.context(format!("get_values_reconstruct_data for layer {self}")),
     376            0 :                 ),
     377            0 :                 err => err,
     378      1599487 :             })
     379      1599487 :     }
     380              : 
     381              :     /// Download the layer if evicted.
     382              :     ///
     383              :     /// Will not error when the layer is already downloaded.
     384            0 :     pub(crate) async fn download(&self, ctx: &RequestContext) -> Result<(), DownloadError> {
     385            0 :         self.0.get_or_maybe_download(true, ctx).await?;
     386            0 :         Ok(())
     387            0 :     }
     388              : 
     389         1896 :     pub(crate) async fn needs_download(&self) -> Result<Option<NeedsDownload>, std::io::Error> {
     390         1896 :         self.0.needs_download().await
     391         1896 :     }
     392              : 
     393              :     /// Assuming the layer is already downloaded, returns a guard which will prohibit eviction
     394              :     /// while the guard exists.
     395              :     ///
     396              :     /// Returns None if the layer is currently evicted or becoming evicted.
     397          120 :     pub(crate) async fn keep_resident(&self) -> Option<ResidentLayer> {
     398          120 :         let downloaded = self.0.inner.get().and_then(|rowe| rowe.get())?;
     399              : 
     400           84 :         Some(ResidentLayer {
     401           84 :             downloaded,
     402           84 :             owner: self.clone(),
     403           84 :         })
     404          120 :     }
     405              : 
     406              :     /// Weak indicator of is the layer resident or not. Good enough for eviction, which can deal
     407              :     /// with `EvictionError::NotFound`.
     408              :     ///
     409              :     /// Returns `true` if this layer might be resident, or `false`, if it most likely evicted or
     410              :     /// will be unless a read happens soon.
     411         1075 :     pub(crate) fn is_likely_resident(&self) -> bool {
     412         1075 :         self.0
     413         1075 :             .inner
     414         1075 :             .get()
     415         1075 :             .map(|rowe| rowe.is_likely_resident())
     416         1075 :             .unwrap_or(false)
     417         1075 :     }
     418              : 
     419              :     /// Downloads if necessary and creates a guard, which will keep this layer from being evicted.
     420         3480 :     pub(crate) async fn download_and_keep_resident(
     421         3480 :         &self,
     422         3480 :         ctx: &RequestContext,
     423         3480 :     ) -> Result<ResidentLayer, DownloadError> {
     424         3480 :         let downloaded = self.0.get_or_maybe_download(true, ctx).await?;
     425              : 
     426         3480 :         Ok(ResidentLayer {
     427         3480 :             downloaded,
     428         3480 :             owner: self.clone(),
     429         3480 :         })
     430         3480 :     }
     431              : 
     432            0 :     pub(crate) fn info(&self, reset: LayerAccessStatsReset) -> HistoricLayerInfo {
     433            0 :         self.0.info(reset)
     434            0 :     }
     435              : 
     436          204 :     pub(crate) fn latest_activity(&self) -> SystemTime {
     437          204 :         self.0.access_stats.latest_activity()
     438          204 :     }
     439              : 
     440          552 :     pub(crate) fn visibility(&self) -> LayerVisibilityHint {
     441          552 :         self.0.access_stats.visibility()
     442          552 :     }
     443              : 
     444        10992 :     pub(crate) fn local_path(&self) -> &Utf8Path {
     445        10992 :         &self.0.path
     446        10992 :     }
     447              : 
     448        15960 :     pub(crate) fn metadata(&self) -> LayerFileMetadata {
     449        15960 :         self.0.metadata()
     450        15960 :     }
     451              : 
     452          156 :     pub(crate) fn last_evicted_at(&self) -> LastEviction {
     453          156 :         match self.0.last_evicted_at.try_lock() {
     454          156 :             Ok(lock) => match *lock {
     455            0 :                 None => LastEviction::Never,
     456          156 :                 Some(at) => LastEviction::At(at),
     457              :             },
     458            0 :             Err(std::sync::TryLockError::WouldBlock) => LastEviction::Evicting,
     459            0 :             Err(std::sync::TryLockError::Poisoned(p)) => panic!("Lock poisoned: {p}"),
     460              :         }
     461          156 :     }
     462              : 
     463            0 :     pub(crate) fn get_timeline_id(&self) -> Option<TimelineId> {
     464            0 :         self.0
     465            0 :             .timeline
     466            0 :             .upgrade()
     467            0 :             .map(|timeline| timeline.timeline_id)
     468            0 :     }
     469              : 
     470              :     /// Traditional debug dumping facility
     471              :     #[allow(unused)]
     472           24 :     pub(crate) async fn dump(&self, verbose: bool, ctx: &RequestContext) -> anyhow::Result<()> {
     473           24 :         self.0.desc.dump();
     474           24 : 
     475           24 :         if verbose {
     476              :             // for now, unconditionally download everything, even if that might not be wanted.
     477           24 :             let l = self.0.get_or_maybe_download(true, ctx).await?;
     478           24 :             l.dump(&self.0, ctx).await?
     479            0 :         }
     480              : 
     481           24 :         Ok(())
     482           24 :     }
     483              : 
     484              :     /// Waits until this layer has been dropped (and if needed, local file deletion and remote
     485              :     /// deletion scheduling has completed).
     486              :     ///
     487              :     /// Does not start local deletion, use [`Self::delete_on_drop`] for that
     488              :     /// separatedly.
     489              :     #[cfg(any(feature = "testing", test))]
     490           12 :     pub(crate) fn wait_drop(&self) -> impl std::future::Future<Output = ()> + 'static {
     491           12 :         let mut rx = self.0.status.as_ref().unwrap().subscribe();
     492              : 
     493           12 :         async move {
     494              :             loop {
     495           12 :                 if rx.changed().await.is_err() {
     496           12 :                     break;
     497            0 :                 }
     498              :             }
     499           12 :         }
     500           12 :     }
     501              : 
     502      1599487 :     fn record_access(&self, ctx: &RequestContext) {
     503      1599487 :         if self.0.access_stats.record_access(ctx) {
     504              :             // Visibility was modified to Visible: maybe log about this
     505            0 :             match ctx.task_kind() {
     506              :                 TaskKind::CalculateSyntheticSize
     507              :                 | TaskKind::OndemandLogicalSizeCalculation
     508              :                 | TaskKind::GarbageCollector
     509            0 :                 | TaskKind::MgmtRequest => {
     510            0 :                     // This situation is expected in code paths do binary searches of the LSN space to resolve
     511            0 :                     // an LSN to a timestamp, which happens during GC, during GC cutoff calculations in synthetic size,
     512            0 :                     // and on-demand for certain HTTP API requests. On-demand logical size calculation is also included
     513            0 :                     // because it is run as a sub-task of synthetic size.
     514            0 :                 }
     515              :                 _ => {
     516              :                     // In all other contexts, it is unusual to do I/O involving layers which are not visible at
     517              :                     // some branch tip, so we log the fact that we are accessing something that the visibility
     518              :                     // calculation thought should not be visible.
     519              :                     //
     520              :                     // This case is legal in brief time windows: for example an in-flight getpage request can hold on to a layer object
     521              :                     // which was covered by a concurrent compaction.
     522            0 :                     tracing::info!(
     523              :                         layer=%self,
     524            0 :                         "became visible as a result of access",
     525              :                     );
     526              :                 }
     527              :             }
     528              : 
     529              :             // Update the timeline's visible bytes count
     530            0 :             if let Some(tl) = self.0.timeline.upgrade() {
     531            0 :                 tl.metrics
     532            0 :                     .visible_physical_size_gauge
     533            0 :                     .add(self.0.desc.file_size)
     534            0 :             }
     535      1599487 :         }
     536      1599487 :     }
     537              : 
     538         2184 :     pub(crate) fn set_visibility(&self, visibility: LayerVisibilityHint) {
     539         2184 :         let old_visibility = self.0.access_stats.set_visibility(visibility.clone());
     540              :         use LayerVisibilityHint::*;
     541         2184 :         match (old_visibility, visibility) {
     542              :             (Visible, Covered) => {
     543              :                 // Subtract this layer's contribution to the visible size metric
     544          216 :                 if let Some(tl) = self.0.timeline.upgrade() {
     545          216 :                     debug_assert!(
     546          216 :                         tl.metrics.visible_physical_size_gauge.get() >= self.0.desc.file_size
     547              :                     );
     548          216 :                     tl.metrics
     549          216 :                         .visible_physical_size_gauge
     550          216 :                         .sub(self.0.desc.file_size)
     551            0 :                 }
     552              :             }
     553              :             (Covered, Visible) => {
     554              :                 // Add this layer's contribution to the visible size metric
     555            0 :                 if let Some(tl) = self.0.timeline.upgrade() {
     556            0 :                     tl.metrics
     557            0 :                         .visible_physical_size_gauge
     558            0 :                         .add(self.0.desc.file_size)
     559            0 :                 }
     560              :             }
     561         1968 :             (Covered, Covered) | (Visible, Visible) => {
     562         1968 :                 // no change
     563         1968 :             }
     564              :         }
     565         2184 :     }
     566              : }
     567              : 
     568              : /// The download-ness ([`DownloadedLayer`]) can be either resident or wanted evicted.
     569              : ///
     570              : /// However when we want something evicted, we cannot evict it right away as there might be current
     571              : /// reads happening on it. For example: it has been searched from [`LayerMap::search`] but not yet
     572              : /// read with [`Layer::get_values_reconstruct_data`].
     573              : ///
     574              : /// [`LayerMap::search`]: crate::tenant::layer_map::LayerMap::search
     575              : #[derive(Debug)]
     576              : enum ResidentOrWantedEvicted {
     577              :     Resident(Arc<DownloadedLayer>),
     578              :     WantedEvicted(Weak<DownloadedLayer>, usize),
     579              : }
     580              : 
     581              : impl ResidentOrWantedEvicted {
     582              :     /// Non-mutating access to the a DownloadedLayer, if possible.
     583              :     ///
     584              :     /// This is not used on the read path (anything that calls
     585              :     /// [`LayerInner::get_or_maybe_download`]) because it was decided that reads always win
     586              :     /// evictions, and part of that winning is using [`ResidentOrWantedEvicted::get_and_upgrade`].
     587           84 :     fn get(&self) -> Option<Arc<DownloadedLayer>> {
     588           84 :         match self {
     589           84 :             ResidentOrWantedEvicted::Resident(strong) => Some(strong.clone()),
     590            0 :             ResidentOrWantedEvicted::WantedEvicted(weak, _) => weak.upgrade(),
     591              :         }
     592           84 :     }
     593              : 
     594              :     /// Best-effort query for residency right now, not as strong guarantee as receiving a strong
     595              :     /// reference from `ResidentOrWantedEvicted::get`.
     596          667 :     fn is_likely_resident(&self) -> bool {
     597          667 :         match self {
     598          631 :             ResidentOrWantedEvicted::Resident(_) => true,
     599           36 :             ResidentOrWantedEvicted::WantedEvicted(weak, _) => weak.strong_count() > 0,
     600              :         }
     601          667 :     }
     602              : 
     603              :     /// Upgrades any weak to strong if possible.
     604              :     ///
     605              :     /// Returns a strong reference if possible, along with a boolean telling if an upgrade
     606              :     /// happened.
     607      1602967 :     fn get_and_upgrade(&mut self) -> Option<(Arc<DownloadedLayer>, bool)> {
     608      1602967 :         match self {
     609      1602919 :             ResidentOrWantedEvicted::Resident(strong) => Some((strong.clone(), false)),
     610           48 :             ResidentOrWantedEvicted::WantedEvicted(weak, _) => match weak.upgrade() {
     611            0 :                 Some(strong) => {
     612            0 :                     LAYER_IMPL_METRICS.inc_raced_wanted_evicted_accesses();
     613            0 : 
     614            0 :                     *self = ResidentOrWantedEvicted::Resident(strong.clone());
     615            0 : 
     616            0 :                     Some((strong, true))
     617              :                 }
     618           48 :                 None => None,
     619              :             },
     620              :         }
     621      1602967 :     }
     622              : 
     623              :     /// When eviction is first requested, drop down to holding a [`Weak`].
     624              :     ///
     625              :     /// Returns `Some` if this was the first time eviction was requested. Care should be taken to
     626              :     /// drop the possibly last strong reference outside of the mutex of
     627              :     /// [`heavier_once_cell::OnceCell`].
     628          276 :     fn downgrade(&mut self) -> Option<Arc<DownloadedLayer>> {
     629          276 :         match self {
     630          252 :             ResidentOrWantedEvicted::Resident(strong) => {
     631          252 :                 let weak = Arc::downgrade(strong);
     632          252 :                 let mut temp = ResidentOrWantedEvicted::WantedEvicted(weak, strong.version);
     633          252 :                 std::mem::swap(self, &mut temp);
     634          252 :                 match temp {
     635          252 :                     ResidentOrWantedEvicted::Resident(strong) => Some(strong),
     636            0 :                     ResidentOrWantedEvicted::WantedEvicted(..) => unreachable!("just swapped"),
     637              :                 }
     638              :             }
     639           24 :             ResidentOrWantedEvicted::WantedEvicted(..) => None,
     640              :         }
     641          276 :     }
     642              : }
     643              : 
     644              : struct LayerInner {
     645              :     /// Only needed to check ondemand_download_behavior_treat_error_as_warn and creation of
     646              :     /// [`Self::path`].
     647              :     conf: &'static PageServerConf,
     648              : 
     649              :     /// Full path to the file; unclear if this should exist anymore.
     650              :     path: Utf8PathBuf,
     651              : 
     652              :     desc: PersistentLayerDesc,
     653              : 
     654              :     /// Timeline access is needed for remote timeline client and metrics.
     655              :     ///
     656              :     /// There should not be an access to timeline for any reason without entering the
     657              :     /// [`Timeline::gate`] at the same time.
     658              :     timeline: Weak<Timeline>,
     659              : 
     660              :     access_stats: LayerAccessStats,
     661              : 
     662              :     /// This custom OnceCell is backed by std mutex, but only held for short time periods.
     663              :     ///
     664              :     /// Filesystem changes (download, evict) are only done while holding a permit which the
     665              :     /// `heavier_once_cell` provides.
     666              :     ///
     667              :     /// A number of fields in `Layer` are meant to only be updated when holding the InitPermit, but
     668              :     /// possibly read while not holding it.
     669              :     inner: heavier_once_cell::OnceCell<ResidentOrWantedEvicted>,
     670              : 
     671              :     /// Do we want to delete locally and remotely this when `LayerInner` is dropped
     672              :     wanted_deleted: AtomicBool,
     673              : 
     674              :     /// Version is to make sure we will only evict a specific initialization of the downloaded file.
     675              :     ///
     676              :     /// Incremented for each initialization, stored in `DownloadedLayer::version` or
     677              :     /// `ResidentOrWantedEvicted::WantedEvicted`.
     678              :     version: AtomicUsize,
     679              : 
     680              :     /// Allow subscribing to when the layer actually gets evicted, a non-cancellable download
     681              :     /// starts, or completes.
     682              :     ///
     683              :     /// Updates must only be posted while holding the InitPermit or the heavier_once_cell::Guard.
     684              :     /// Holding the InitPermit is the only time we can do state transitions, but we also need to
     685              :     /// cancel a pending eviction on upgrading a [`ResidentOrWantedEvicted::WantedEvicted`] back to
     686              :     /// [`ResidentOrWantedEvicted::Resident`] on access.
     687              :     ///
     688              :     /// The sender is wrapped in an Option to facilitate moving it out on [`LayerInner::drop`].
     689              :     status: Option<tokio::sync::watch::Sender<Status>>,
     690              : 
     691              :     /// Counter for exponential backoff with the download.
     692              :     ///
     693              :     /// This is atomic only for the purposes of having additional data only accessed while holding
     694              :     /// the InitPermit.
     695              :     consecutive_failures: AtomicUsize,
     696              : 
     697              :     /// The generation of this Layer.
     698              :     ///
     699              :     /// For loaded layers (resident or evicted) this comes from [`LayerFileMetadata::generation`],
     700              :     /// for created layers from [`Timeline::generation`].
     701              :     generation: Generation,
     702              : 
     703              :     /// The shard of this Layer.
     704              :     ///
     705              :     /// For layers created in this process, this will always be the [`ShardIndex`] of the
     706              :     /// current `ShardIdentity`` (TODO: add link once it's introduced).
     707              :     ///
     708              :     /// For loaded layers, this may be some other value if the tenant has undergone
     709              :     /// a shard split since the layer was originally written.
     710              :     shard: ShardIndex,
     711              : 
     712              :     /// When the Layer was last evicted but has not been downloaded since.
     713              :     ///
     714              :     /// This is used for skipping evicted layers from the previous heatmap (see
     715              :     /// `[Timeline::generate_heatmap]`) and for updating metrics
     716              :     /// (see [`LayerImplMetrics::redownload_after`]).
     717              :     last_evicted_at: std::sync::Mutex<Option<std::time::Instant>>,
     718              : 
     719              :     #[cfg(test)]
     720              :     failpoints: std::sync::Mutex<Vec<failpoints::Failpoint>>,
     721              : }
     722              : 
     723              : impl std::fmt::Display for LayerInner {
     724          324 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
     725          324 :         write!(f, "{}", self.layer_desc().short_id())
     726          324 :     }
     727              : }
     728              : 
     729              : impl AsLayerDesc for LayerInner {
     730     12150789 :     fn layer_desc(&self) -> &PersistentLayerDesc {
     731     12150789 :         &self.desc
     732     12150789 :     }
     733              : }
     734              : 
     735              : #[derive(Debug, Clone, Copy)]
     736              : enum Status {
     737              :     Resident,
     738              :     Evicted,
     739              :     Downloading,
     740              : }
     741              : 
     742              : impl Drop for LayerInner {
     743         4252 :     fn drop(&mut self) {
     744         4252 :         // if there was a pending eviction, mark it cancelled here to balance metrics
     745         4252 :         if let Some((ResidentOrWantedEvicted::WantedEvicted(..), _)) = self.inner.take_and_deinit()
     746           12 :         {
     747           12 :             // eviction has already been started
     748           12 :             LAYER_IMPL_METRICS.inc_eviction_cancelled(EvictionCancelled::LayerGone);
     749           12 : 
     750           12 :             // eviction request is intentionally not honored as no one is present to wait for it
     751           12 :             // and we could be delaying shutdown for nothing.
     752         4240 :         }
     753              : 
     754         4252 :         let timeline = self.timeline.upgrade();
     755              : 
     756         4252 :         if let Some(timeline) = timeline.as_ref() {
     757              :             // Only need to decrement metrics if the timeline still exists: otherwise
     758              :             // it will have already de-registered these metrics via TimelineMetrics::shutdown
     759         4156 :             timeline.metrics.dec_layer(&self.desc);
     760              : 
     761         4156 :             if matches!(self.access_stats.visibility(), LayerVisibilityHint::Visible) {
     762         4156 :                 debug_assert!(
     763         4156 :                     timeline.metrics.visible_physical_size_gauge.get() >= self.desc.file_size
     764              :                 );
     765         4156 :                 timeline
     766         4156 :                     .metrics
     767         4156 :                     .visible_physical_size_gauge
     768         4156 :                     .sub(self.desc.file_size);
     769            0 :             }
     770           96 :         }
     771              : 
     772         4252 :         if !*self.wanted_deleted.get_mut() {
     773         1176 :             return;
     774         3076 :         }
     775              : 
     776         3076 :         let span = tracing::info_span!(parent: None, "layer_delete", tenant_id = %self.layer_desc().tenant_shard_id.tenant_id, shard_id=%self.layer_desc().tenant_shard_id.shard_slug(), timeline_id = %self.layer_desc().timeline_id);
     777              : 
     778         3076 :         let path = std::mem::take(&mut self.path);
     779         3076 :         let file_name = self.layer_desc().layer_name();
     780         3076 :         let file_size = self.layer_desc().file_size;
     781         3076 :         let meta = self.metadata();
     782         3076 :         let status = self.status.take();
     783         3076 : 
     784         3076 :         Self::spawn_blocking(move || {
     785         3076 :             let _g = span.entered();
     786         3076 : 
     787         3076 :             // carry this until we are finished for [`Layer::wait_drop`] support
     788         3076 :             let _status = status;
     789              : 
     790         3076 :             let Some(timeline) = timeline else {
     791              :                 // no need to nag that timeline is gone: under normal situation on
     792              :                 // task_mgr::remove_tenant_from_memory the timeline is gone before we get dropped.
     793            0 :                 LAYER_IMPL_METRICS.inc_deletes_failed(DeleteFailed::TimelineGone);
     794            0 :                 return;
     795              :             };
     796              : 
     797         3076 :             let Ok(_guard) = timeline.gate.enter() else {
     798            0 :                 LAYER_IMPL_METRICS.inc_deletes_failed(DeleteFailed::TimelineGone);
     799            0 :                 return;
     800              :             };
     801              : 
     802         3076 :             let removed = match std::fs::remove_file(path) {
     803         3064 :                 Ok(()) => true,
     804           12 :                 Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
     805           12 :                     // until we no longer do detaches by removing all local files before removing the
     806           12 :                     // tenant from the global map, we will always get these errors even if we knew what
     807           12 :                     // is the latest state.
     808           12 :                     //
     809           12 :                     // we currently do not track the latest state, so we'll also end up here on evicted
     810           12 :                     // layers.
     811           12 :                     false
     812              :                 }
     813            0 :                 Err(e) => {
     814            0 :                     tracing::error!("failed to remove wanted deleted layer: {e}");
     815            0 :                     LAYER_IMPL_METRICS.inc_delete_removes_failed();
     816            0 :                     false
     817              :                 }
     818              :             };
     819              : 
     820         3076 :             if removed {
     821         3064 :                 timeline.metrics.resident_physical_size_sub(file_size);
     822         3064 :             }
     823         3076 :             let res = timeline
     824         3076 :                 .remote_client
     825         3076 :                 .schedule_deletion_of_unlinked(vec![(file_name, meta)]);
     826              : 
     827         3076 :             if let Err(e) = res {
     828              :                 // test_timeline_deletion_with_files_stuck_in_upload_queue is good at
     829              :                 // demonstrating this deadlock (without spawn_blocking): stop will drop
     830              :                 // queued items, which will have ResidentLayer's, and those drops would try
     831              :                 // to re-entrantly lock the RemoteTimelineClient inner state.
     832           12 :                 if !timeline.is_active() {
     833           12 :                     tracing::info!("scheduling deletion on drop failed: {e:#}");
     834              :                 } else {
     835            0 :                     tracing::warn!("scheduling deletion on drop failed: {e:#}");
     836              :                 }
     837           12 :                 LAYER_IMPL_METRICS.inc_deletes_failed(DeleteFailed::DeleteSchedulingFailed);
     838         3064 :             } else {
     839         3064 :                 LAYER_IMPL_METRICS.inc_completed_deletes();
     840         3064 :             }
     841         3076 :         });
     842         4252 :     }
     843              : }
     844              : 
     845              : impl LayerInner {
     846              :     #[allow(clippy::too_many_arguments)]
     847        11712 :     fn new(
     848        11712 :         conf: &'static PageServerConf,
     849        11712 :         timeline: &Arc<Timeline>,
     850        11712 :         local_path: Utf8PathBuf,
     851        11712 :         desc: PersistentLayerDesc,
     852        11712 :         downloaded: Option<Arc<DownloadedLayer>>,
     853        11712 :         generation: Generation,
     854        11712 :         shard: ShardIndex,
     855        11712 :     ) -> Self {
     856        11712 :         let (inner, version, init_status) = if let Some(inner) = downloaded {
     857        11712 :             let version = inner.version;
     858        11712 :             let resident = ResidentOrWantedEvicted::Resident(inner);
     859        11712 :             (
     860        11712 :                 heavier_once_cell::OnceCell::new(resident),
     861        11712 :                 version,
     862        11712 :                 Status::Resident,
     863        11712 :             )
     864              :         } else {
     865            0 :             (heavier_once_cell::OnceCell::default(), 0, Status::Evicted)
     866              :         };
     867              : 
     868              :         // This object acts as a RAII guard on these metrics: increment on construction
     869        11712 :         timeline.metrics.inc_layer(&desc);
     870        11712 : 
     871        11712 :         // New layers are visible by default. This metric is later updated on drop or in set_visibility
     872        11712 :         timeline
     873        11712 :             .metrics
     874        11712 :             .visible_physical_size_gauge
     875        11712 :             .add(desc.file_size);
     876        11712 : 
     877        11712 :         LayerInner {
     878        11712 :             conf,
     879        11712 :             path: local_path,
     880        11712 :             desc,
     881        11712 :             timeline: Arc::downgrade(timeline),
     882        11712 :             access_stats: Default::default(),
     883        11712 :             wanted_deleted: AtomicBool::new(false),
     884        11712 :             inner,
     885        11712 :             version: AtomicUsize::new(version),
     886        11712 :             status: Some(tokio::sync::watch::channel(init_status).0),
     887        11712 :             consecutive_failures: AtomicUsize::new(0),
     888        11712 :             generation,
     889        11712 :             shard,
     890        11712 :             last_evicted_at: std::sync::Mutex::default(),
     891        11712 :             #[cfg(test)]
     892        11712 :             failpoints: Default::default(),
     893        11712 :         }
     894        11712 :     }
     895              : 
     896         3120 :     fn delete_on_drop(&self) {
     897         3120 :         let res =
     898         3120 :             self.wanted_deleted
     899         3120 :                 .compare_exchange(false, true, Ordering::Release, Ordering::Relaxed);
     900         3120 : 
     901         3120 :         if res.is_ok() {
     902         3096 :             LAYER_IMPL_METRICS.inc_started_deletes();
     903         3096 :         }
     904         3120 :     }
     905              : 
     906              :     /// Cancellation safe, however dropping the future and calling this method again might result
     907              :     /// in a new attempt to evict OR join the previously started attempt.
     908          312 :     #[tracing::instrument(level = tracing::Level::DEBUG, skip_all, ret, err(level = tracing::Level::DEBUG), fields(layer=%self))]
     909              :     pub(crate) async fn evict_and_wait(&self, timeout: Duration) -> Result<(), EvictionError> {
     910              :         let mut rx = self.status.as_ref().unwrap().subscribe();
     911              : 
     912              :         {
     913              :             let current = rx.borrow_and_update();
     914              :             match &*current {
     915              :                 Status::Resident => {
     916              :                     // we might get lucky and evict this; continue
     917              :                 }
     918              :                 Status::Evicted | Status::Downloading => {
     919              :                     // it is already evicted
     920              :                     return Err(EvictionError::NotFound);
     921              :                 }
     922              :             }
     923              :         }
     924              : 
     925              :         let strong = {
     926              :             match self.inner.get() {
     927              :                 Some(mut either) => either.downgrade(),
     928              :                 None => {
     929              :                     // we already have a scheduled eviction, which just has not gotten to run yet.
     930              :                     // it might still race with a read access, but that could also get cancelled,
     931              :                     // so let's say this is not evictable.
     932              :                     return Err(EvictionError::NotFound);
     933              :                 }
     934              :             }
     935              :         };
     936              : 
     937              :         if strong.is_some() {
     938              :             // drop the DownloadedLayer outside of the holding the guard
     939              :             drop(strong);
     940              : 
     941              :             // idea here is that only one evicter should ever get to witness a strong reference,
     942              :             // which means whenever get_or_maybe_download upgrades a weak, it must mark up a
     943              :             // cancelled eviction and signal us, like it currently does.
     944              :             //
     945              :             // a second concurrent evict_and_wait will not see a strong reference.
     946              :             LAYER_IMPL_METRICS.inc_started_evictions();
     947              :         }
     948              : 
     949              :         let changed = rx.changed();
     950              :         let changed = tokio::time::timeout(timeout, changed).await;
     951              : 
     952              :         let Ok(changed) = changed else {
     953              :             return Err(EvictionError::Timeout);
     954              :         };
     955              : 
     956              :         let _: () = changed.expect("cannot be closed, because we are holding a strong reference");
     957              : 
     958              :         let current = rx.borrow_and_update();
     959              : 
     960              :         match &*current {
     961              :             // the easiest case
     962              :             Status::Evicted => Ok(()),
     963              :             // it surely was evicted in between, but then there was a new access now; we can't know
     964              :             // if it'll succeed so lets just call it evicted
     965              :             Status::Downloading => Ok(()),
     966              :             // either the download which was started after eviction completed already, or it was
     967              :             // never evicted
     968              :             Status::Resident => Err(EvictionError::Downloaded),
     969              :         }
     970              :     }
     971              : 
     972              :     /// Cancellation safe.
     973      1603063 :     async fn get_or_maybe_download(
     974      1603063 :         self: &Arc<Self>,
     975      1603063 :         allow_download: bool,
     976      1603063 :         ctx: &RequestContext,
     977      1603063 :     ) -> Result<Arc<DownloadedLayer>, DownloadError> {
     978      1603063 :         let mut wait_for_download_recorder =
     979      1603063 :             scopeguard::guard(utils::elapsed_accum::ElapsedAccum::default(), |accum| {
     980      1603063 :                 ctx.ondemand_download_wait_observe(accum.get());
     981      1603063 :             });
     982          144 :         let (weak, permit) = {
     983              :             // get_or_init_detached can:
     984              :             // - be fast (mutex lock) OR uncontested semaphore permit acquire
     985              :             // - be slow (wait for semaphore permit or closing)
     986      1603063 :             let init_cancelled = scopeguard::guard((), |_| LAYER_IMPL_METRICS.inc_init_cancelled());
     987              : 
     988      1603063 :             let locked = self
     989      1603063 :                 .inner
     990      1603063 :                 .get_or_init_detached_measured(Some(&mut wait_for_download_recorder))
     991      1603063 :                 .await
     992      1603063 :                 .map(|mut guard| guard.get_and_upgrade().ok_or(guard));
     993      1603063 : 
     994      1603063 :             scopeguard::ScopeGuard::into_inner(init_cancelled);
     995              : 
     996      1602919 :             match locked {
     997              :                 // this path could had been a RwLock::read
     998      1602919 :                 Ok(Ok((strong, upgraded))) if !upgraded => return Ok(strong),
     999            0 :                 Ok(Ok((strong, _))) => {
    1000            0 :                     // when upgraded back, the Arc<DownloadedLayer> is still available, but
    1001            0 :                     // previously a `evict_and_wait` was received. this is the only place when we
    1002            0 :                     // send out an update without holding the InitPermit.
    1003            0 :                     //
    1004            0 :                     // note that we also have dropped the Guard; this is fine, because we just made
    1005            0 :                     // a state change and are holding a strong reference to be returned.
    1006            0 :                     self.status.as_ref().unwrap().send_replace(Status::Resident);
    1007            0 :                     LAYER_IMPL_METRICS
    1008            0 :                         .inc_eviction_cancelled(EvictionCancelled::UpgradedBackOnAccess);
    1009            0 : 
    1010            0 :                     return Ok(strong);
    1011              :                 }
    1012           48 :                 Ok(Err(guard)) => {
    1013           48 :                     // path to here: we won the eviction, the file should still be on the disk.
    1014           48 :                     let (weak, permit) = guard.take_and_deinit();
    1015           48 :                     (Some(weak), permit)
    1016              :                 }
    1017           96 :                 Err(permit) => (None, permit),
    1018              :             }
    1019              :         };
    1020          144 :         let _guard = wait_for_download_recorder.guard();
    1021              : 
    1022          144 :         if let Some(weak) = weak {
    1023              :             // only drop the weak after dropping the heavier_once_cell guard
    1024           48 :             assert!(
    1025           48 :                 matches!(weak, ResidentOrWantedEvicted::WantedEvicted(..)),
    1026            0 :                 "unexpected {weak:?}, ResidentOrWantedEvicted::get_and_upgrade has a bug"
    1027              :             );
    1028           96 :         }
    1029              : 
    1030          144 :         let timeline = self
    1031          144 :             .timeline
    1032          144 :             .upgrade()
    1033          144 :             .ok_or(DownloadError::TimelineShutdown)?;
    1034              : 
    1035              :         // count cancellations, which currently remain largely unexpected
    1036          144 :         let init_cancelled = scopeguard::guard((), |_| LAYER_IMPL_METRICS.inc_init_cancelled());
    1037              : 
    1038              :         // check if we really need to be downloaded: this can happen if a read access won the
    1039              :         // semaphore before eviction.
    1040              :         //
    1041              :         // if we are cancelled while doing this `stat` the `self.inner` will be uninitialized. a
    1042              :         // pending eviction will try to evict even upon finding an uninitialized `self.inner`.
    1043          144 :         let needs_download = self
    1044          144 :             .needs_download()
    1045          144 :             .await
    1046          144 :             .map_err(DownloadError::PreStatFailed);
    1047          144 : 
    1048          144 :         scopeguard::ScopeGuard::into_inner(init_cancelled);
    1049              : 
    1050          144 :         let needs_download = needs_download?;
    1051              : 
    1052          144 :         let Some(reason) = needs_download else {
    1053              :             // the file is present locally because eviction has not had a chance to run yet
    1054              : 
    1055              :             #[cfg(test)]
    1056           48 :             self.failpoint(failpoints::FailpointKind::AfterDeterminingLayerNeedsNoDownload)
    1057           48 :                 .await?;
    1058              : 
    1059           36 :             LAYER_IMPL_METRICS.inc_init_needed_no_download();
    1060           36 : 
    1061           36 :             return Ok(self.initialize_after_layer_is_on_disk(permit));
    1062              :         };
    1063              : 
    1064              :         // we must download; getting cancelled before spawning the download is not an issue as
    1065              :         // any still running eviction would not find anything to evict.
    1066              : 
    1067           96 :         if let NeedsDownload::NotFile(ft) = reason {
    1068            0 :             return Err(DownloadError::NotFile(ft));
    1069           96 :         }
    1070           96 : 
    1071           96 :         self.check_expected_download(ctx)?;
    1072              : 
    1073           96 :         if !allow_download {
    1074              :             // this is only used from tests, but it is hard to test without the boolean
    1075           12 :             return Err(DownloadError::DownloadRequired);
    1076           84 :         }
    1077              : 
    1078           84 :         let ctx = if ctx.has_perf_span() {
    1079            0 :             let dl_ctx = RequestContextBuilder::from(ctx)
    1080            0 :                 .task_kind(TaskKind::LayerDownload)
    1081            0 :                 .download_behavior(DownloadBehavior::Download)
    1082            0 :                 .root_perf_span(|| {
    1083            0 :                     info_span!(
    1084            0 :                         target: PERF_TRACE_TARGET,
    1085            0 :                         "DOWNLOAD_LAYER",
    1086            0 :                         layer = %self,
    1087            0 :                         reason = %reason
    1088            0 :                     )
    1089            0 :                 })
    1090            0 :                 .detached_child();
    1091            0 :             ctx.perf_follows_from(&dl_ctx);
    1092            0 :             dl_ctx
    1093              :         } else {
    1094           84 :             ctx.attached_child()
    1095              :         };
    1096              : 
    1097           84 :         async move {
    1098           84 :             tracing::info!(%reason, "downloading on-demand");
    1099              : 
    1100           84 :             let init_cancelled = scopeguard::guard((), |_| LAYER_IMPL_METRICS.inc_init_cancelled());
    1101           84 :             let res = self
    1102           84 :                 .download_init_and_wait(timeline, permit, ctx.attached_child())
    1103           84 :                 .maybe_perf_instrument(&ctx, |crnt_perf_span| crnt_perf_span.clone())
    1104           84 :                 .await?;
    1105              : 
    1106           84 :             scopeguard::ScopeGuard::into_inner(init_cancelled);
    1107           84 :             Ok(res)
    1108           84 :         }
    1109           84 :         .instrument(tracing::info_span!("get_or_maybe_download", layer=%self))
    1110           84 :         .await
    1111      1603063 :     }
    1112              : 
    1113              :     /// Nag or fail per RequestContext policy
    1114           96 :     fn check_expected_download(&self, ctx: &RequestContext) -> Result<(), DownloadError> {
    1115              :         use crate::context::DownloadBehavior::*;
    1116           96 :         let b = ctx.download_behavior();
    1117           96 :         match b {
    1118           96 :             Download => Ok(()),
    1119              :             Warn | Error => {
    1120            0 :                 tracing::info!(
    1121            0 :                     "unexpectedly on-demand downloading for task kind {:?}",
    1122            0 :                     ctx.task_kind()
    1123              :                 );
    1124            0 :                 crate::metrics::UNEXPECTED_ONDEMAND_DOWNLOADS.inc();
    1125              : 
    1126            0 :                 let really_error =
    1127            0 :                     matches!(b, Error) && !self.conf.ondemand_download_behavior_treat_error_as_warn;
    1128              : 
    1129            0 :                 if really_error {
    1130              :                     // this check is only probablistic, seems like flakyness footgun
    1131            0 :                     Err(DownloadError::ContextAndConfigReallyDeniesDownloads)
    1132              :                 } else {
    1133            0 :                     Ok(())
    1134              :                 }
    1135              :             }
    1136              :         }
    1137           96 :     }
    1138              : 
    1139              :     /// Actual download, at most one is executed at the time.
    1140           84 :     async fn download_init_and_wait(
    1141           84 :         self: &Arc<Self>,
    1142           84 :         timeline: Arc<Timeline>,
    1143           84 :         permit: heavier_once_cell::InitPermit,
    1144           84 :         ctx: RequestContext,
    1145           84 :     ) -> Result<Arc<DownloadedLayer>, DownloadError> {
    1146           84 :         debug_assert_current_span_has_tenant_and_timeline_id();
    1147           84 : 
    1148           84 :         let (tx, rx) = tokio::sync::oneshot::channel();
    1149           84 : 
    1150           84 :         let this: Arc<Self> = self.clone();
    1151              : 
    1152           84 :         let guard = timeline
    1153           84 :             .gate
    1154           84 :             .enter()
    1155           84 :             .map_err(|_| DownloadError::DownloadCancelled)?;
    1156              : 
    1157           84 :         Self::spawn(
    1158           84 :             async move {
    1159            0 :                 let _guard = guard;
    1160            0 : 
    1161            0 :                 // now that we have commited to downloading, send out an update to:
    1162            0 :                 // - unhang any pending eviction
    1163            0 :                 // - break out of evict_and_wait
    1164            0 :                 this.status
    1165            0 :                     .as_ref()
    1166            0 :                     .unwrap()
    1167            0 :                     .send_replace(Status::Downloading);
    1168           84 : 
    1169           84 :                 #[cfg(test)]
    1170           84 :                 this.failpoint(failpoints::FailpointKind::WaitBeforeDownloading)
    1171           84 :                     .await
    1172           84 :                     .unwrap();
    1173              : 
    1174           84 :                 let res = this.download_and_init(timeline, permit, &ctx).await;
    1175              : 
    1176           84 :                 if let Err(res) = tx.send(res) {
    1177            0 :                     match res {
    1178            0 :                         Ok(_res) => {
    1179            0 :                             tracing::debug!("layer initialized, but caller has been cancelled");
    1180            0 :                             LAYER_IMPL_METRICS.inc_init_completed_without_requester();
    1181              :                         }
    1182            0 :                         Err(e) => {
    1183            0 :                             tracing::info!(
    1184            0 :                                 "layer file download failed, and caller has been cancelled: {e:?}"
    1185              :                             );
    1186            0 :                             LAYER_IMPL_METRICS.inc_download_failed_without_requester();
    1187              :                         }
    1188              :                     }
    1189           84 :                 }
    1190           84 :             }
    1191           84 :             .in_current_span(),
    1192           84 :         );
    1193           84 : 
    1194           84 :         match rx.await {
    1195           84 :             Ok(Ok(res)) => Ok(res),
    1196              :             Ok(Err(remote_storage::DownloadError::Cancelled)) => {
    1197            0 :                 Err(DownloadError::DownloadCancelled)
    1198              :             }
    1199            0 :             Ok(Err(_)) => Err(DownloadError::DownloadFailed),
    1200            0 :             Err(_gone) => Err(DownloadError::DownloadCancelled),
    1201              :         }
    1202           84 :     }
    1203              : 
    1204           84 :     async fn download_and_init(
    1205           84 :         self: &Arc<LayerInner>,
    1206           84 :         timeline: Arc<Timeline>,
    1207           84 :         permit: heavier_once_cell::InitPermit,
    1208           84 :         ctx: &RequestContext,
    1209           84 :     ) -> Result<Arc<DownloadedLayer>, remote_storage::DownloadError> {
    1210           84 :         let start = std::time::Instant::now();
    1211           84 :         let result = timeline
    1212           84 :             .remote_client
    1213           84 :             .download_layer_file(
    1214           84 :                 &self.desc.layer_name(),
    1215           84 :                 &self.metadata(),
    1216           84 :                 &self.path,
    1217           84 :                 &timeline.gate,
    1218           84 :                 &timeline.cancel,
    1219           84 :                 ctx,
    1220           84 :             )
    1221           84 :             .await;
    1222           84 :         let latency = start.elapsed();
    1223           84 :         let latency_millis = u64::try_from(latency.as_millis()).unwrap();
    1224           84 :         match result {
    1225           84 :             Ok(size) => {
    1226           84 :                 assert_eq!(size, self.desc.file_size);
    1227              : 
    1228           84 :                 match self.needs_download().await {
    1229            0 :                     Ok(Some(reason)) => {
    1230            0 :                         // this is really a bug in needs_download or remote timeline client
    1231            0 :                         panic!("post-condition failed: needs_download returned {reason:?}");
    1232              :                     }
    1233           84 :                     Ok(None) => {
    1234           84 :                         // as expected
    1235           84 :                     }
    1236            0 :                     Err(e) => {
    1237            0 :                         panic!("post-condition failed: needs_download errored: {e:?}");
    1238              :                     }
    1239              :                 };
    1240           84 :                 tracing::info!(size=%self.desc.file_size, %latency_millis, "on-demand download successful");
    1241           84 :                 timeline
    1242           84 :                     .metrics
    1243           84 :                     .resident_physical_size_add(self.desc.file_size);
    1244           84 :                 self.consecutive_failures.store(0, Ordering::Relaxed);
    1245           84 : 
    1246           84 :                 let since_last_eviction = self
    1247           84 :                     .last_evicted_at
    1248           84 :                     .lock()
    1249           84 :                     .unwrap()
    1250           84 :                     .take()
    1251           84 :                     .map(|ts| ts.elapsed());
    1252           84 :                 if let Some(since_last_eviction) = since_last_eviction {
    1253           84 :                     LAYER_IMPL_METRICS.record_redownloaded_after(since_last_eviction);
    1254           84 :                 }
    1255              : 
    1256           84 :                 self.access_stats.record_residence_event();
    1257           84 : 
    1258           84 :                 Ok(self.initialize_after_layer_is_on_disk(permit))
    1259              :             }
    1260            0 :             Err(e) => {
    1261            0 :                 let consecutive_failures =
    1262            0 :                     1 + self.consecutive_failures.fetch_add(1, Ordering::Relaxed);
    1263            0 : 
    1264            0 :                 if timeline.cancel.is_cancelled() {
    1265              :                     // If we're shutting down, drop out before logging the error
    1266            0 :                     return Err(e);
    1267            0 :                 }
    1268            0 : 
    1269            0 :                 tracing::error!(consecutive_failures, %latency_millis, "layer file download failed: {e:#}");
    1270              : 
    1271            0 :                 let backoff = utils::backoff::exponential_backoff_duration_seconds(
    1272            0 :                     consecutive_failures.min(u32::MAX as usize) as u32,
    1273            0 :                     1.5,
    1274            0 :                     60.0,
    1275            0 :                 );
    1276            0 : 
    1277            0 :                 let backoff = std::time::Duration::from_secs_f64(backoff);
    1278            0 : 
    1279            0 :                 tokio::select! {
    1280            0 :                     _ = tokio::time::sleep(backoff) => {},
    1281            0 :                     _ = timeline.cancel.cancelled() => {},
    1282              :                 };
    1283              : 
    1284            0 :                 Err(e)
    1285              :             }
    1286              :         }
    1287           84 :     }
    1288              : 
    1289              :     /// Initializes the `Self::inner` to a "resident" state.
    1290              :     ///
    1291              :     /// Callers are assumed to ensure that the file is actually on disk with `Self::needs_download`
    1292              :     /// before calling this method.
    1293              :     ///
    1294              :     /// If this method is ever made async, it needs to be cancellation safe so that no state
    1295              :     /// changes are made before we can write to the OnceCell in non-cancellable fashion.
    1296          120 :     fn initialize_after_layer_is_on_disk(
    1297          120 :         self: &Arc<LayerInner>,
    1298          120 :         permit: heavier_once_cell::InitPermit,
    1299          120 :     ) -> Arc<DownloadedLayer> {
    1300          120 :         debug_assert_current_span_has_tenant_and_timeline_id();
    1301          120 : 
    1302          120 :         // disable any scheduled but not yet running eviction deletions for this initialization
    1303          120 :         let next_version = 1 + self.version.fetch_add(1, Ordering::Relaxed);
    1304          120 :         self.status.as_ref().unwrap().send_replace(Status::Resident);
    1305          120 : 
    1306          120 :         let res = Arc::new(DownloadedLayer {
    1307          120 :             owner: Arc::downgrade(self),
    1308          120 :             kind: tokio::sync::OnceCell::default(),
    1309          120 :             version: next_version,
    1310          120 :         });
    1311          120 : 
    1312          120 :         let waiters = self.inner.initializer_count();
    1313          120 :         if waiters > 0 {
    1314            0 :             tracing::info!(waiters, "completing layer init for other tasks");
    1315          120 :         }
    1316              : 
    1317          120 :         let value = ResidentOrWantedEvicted::Resident(res.clone());
    1318          120 : 
    1319          120 :         self.inner.set(value, permit);
    1320          120 : 
    1321          120 :         res
    1322          120 :     }
    1323              : 
    1324         2136 :     async fn needs_download(&self) -> Result<Option<NeedsDownload>, std::io::Error> {
    1325         2136 :         match tokio::fs::metadata(&self.path).await {
    1326         2040 :             Ok(m) => Ok(self.is_file_present_and_good_size(&m).err()),
    1327           96 :             Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Some(NeedsDownload::NotFound)),
    1328            0 :             Err(e) => Err(e),
    1329              :         }
    1330         2136 :     }
    1331              : 
    1332          732 :     fn needs_download_blocking(&self) -> Result<Option<NeedsDownload>, std::io::Error> {
    1333          732 :         match self.path.metadata() {
    1334          732 :             Ok(m) => Ok(self.is_file_present_and_good_size(&m).err()),
    1335            0 :             Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Some(NeedsDownload::NotFound)),
    1336            0 :             Err(e) => Err(e),
    1337              :         }
    1338          732 :     }
    1339              : 
    1340         2772 :     fn is_file_present_and_good_size(&self, m: &std::fs::Metadata) -> Result<(), NeedsDownload> {
    1341         2772 :         // in future, this should include sha2-256 validation of the file.
    1342         2772 :         if !m.is_file() {
    1343            0 :             Err(NeedsDownload::NotFile(m.file_type()))
    1344         2772 :         } else if m.len() != self.desc.file_size {
    1345            0 :             Err(NeedsDownload::WrongSize {
    1346            0 :                 actual: m.len(),
    1347            0 :                 expected: self.desc.file_size,
    1348            0 :             })
    1349              :         } else {
    1350         2772 :             Ok(())
    1351              :         }
    1352         2772 :     }
    1353              : 
    1354            0 :     fn info(&self, reset: LayerAccessStatsReset) -> HistoricLayerInfo {
    1355            0 :         let layer_name = self.desc.layer_name().to_string();
    1356            0 : 
    1357            0 :         let resident = self
    1358            0 :             .inner
    1359            0 :             .get()
    1360            0 :             .map(|rowe| rowe.is_likely_resident())
    1361            0 :             .unwrap_or(false);
    1362            0 : 
    1363            0 :         let access_stats = self.access_stats.as_api_model(reset);
    1364            0 : 
    1365            0 :         if self.desc.is_delta {
    1366            0 :             let lsn_range = &self.desc.lsn_range;
    1367            0 : 
    1368            0 :             HistoricLayerInfo::Delta {
    1369            0 :                 layer_file_name: layer_name,
    1370            0 :                 layer_file_size: self.desc.file_size,
    1371            0 :                 lsn_start: lsn_range.start,
    1372            0 :                 lsn_end: lsn_range.end,
    1373            0 :                 remote: !resident,
    1374            0 :                 access_stats,
    1375            0 :                 l0: crate::tenant::layer_map::LayerMap::is_l0(
    1376            0 :                     &self.layer_desc().key_range,
    1377            0 :                     self.layer_desc().is_delta,
    1378            0 :                 ),
    1379            0 :             }
    1380              :         } else {
    1381            0 :             let lsn = self.desc.image_layer_lsn();
    1382            0 : 
    1383            0 :             HistoricLayerInfo::Image {
    1384            0 :                 layer_file_name: layer_name,
    1385            0 :                 layer_file_size: self.desc.file_size,
    1386            0 :                 lsn_start: lsn,
    1387            0 :                 remote: !resident,
    1388            0 :                 access_stats,
    1389            0 :             }
    1390              :         }
    1391            0 :     }
    1392              : 
    1393              :     /// `DownloadedLayer` is being dropped, so it calls this method.
    1394          240 :     fn on_downloaded_layer_drop(self: Arc<LayerInner>, only_version: usize) {
    1395              :         // we cannot know without inspecting LayerInner::inner if we should evict or not, even
    1396              :         // though here it is very likely
    1397          240 :         let span = tracing::info_span!(parent: None, "layer_evict", tenant_id = %self.desc.tenant_shard_id.tenant_id, shard_id = %self.desc.tenant_shard_id.shard_slug(), timeline_id = %self.desc.timeline_id, layer=%self, version=%only_version);
    1398              : 
    1399              :         // NOTE: this scope *must* never call `self.inner.get` because evict_and_wait might
    1400              :         // drop while the `self.inner` is being locked, leading to a deadlock.
    1401              : 
    1402          240 :         let start_evicting = async move {
    1403          240 :             #[cfg(test)]
    1404          240 :             self.failpoint(failpoints::FailpointKind::WaitBeforeStartingEvicting)
    1405          240 :                 .await
    1406          240 :                 .expect("failpoint should not have errored");
    1407          240 : 
    1408          240 :             tracing::debug!("eviction started");
    1409              : 
    1410          240 :             let res = self.wait_for_turn_and_evict(only_version).await;
    1411              :             // metrics: ignore the Ok branch, it is not done yet
    1412          240 :             if let Err(e) = res {
    1413           36 :                 tracing::debug!(res=?Err::<(), _>(&e), "eviction completed");
    1414           36 :                 LAYER_IMPL_METRICS.inc_eviction_cancelled(e);
    1415          204 :             }
    1416          240 :         };
    1417              : 
    1418          240 :         Self::spawn(start_evicting.instrument(span));
    1419          240 :     }
    1420              : 
    1421          240 :     async fn wait_for_turn_and_evict(
    1422          240 :         self: Arc<LayerInner>,
    1423          240 :         only_version: usize,
    1424          240 :     ) -> Result<(), EvictionCancelled> {
    1425          468 :         fn is_good_to_continue(status: &Status) -> Result<(), EvictionCancelled> {
    1426              :             use Status::*;
    1427          468 :             match status {
    1428          456 :                 Resident => Ok(()),
    1429           12 :                 Evicted => Err(EvictionCancelled::UnexpectedEvictedState),
    1430            0 :                 Downloading => Err(EvictionCancelled::LostToDownload),
    1431              :             }
    1432          468 :         }
    1433              : 
    1434          240 :         let timeline = self
    1435          240 :             .timeline
    1436          240 :             .upgrade()
    1437          240 :             .ok_or(EvictionCancelled::TimelineGone)?;
    1438              : 
    1439          240 :         let mut rx = self
    1440          240 :             .status
    1441          240 :             .as_ref()
    1442          240 :             .expect("LayerInner cannot be dropped, holding strong ref")
    1443          240 :             .subscribe();
    1444          240 : 
    1445          240 :         is_good_to_continue(&rx.borrow_and_update())?;
    1446              : 
    1447          228 :         let Ok(gate) = timeline.gate.enter() else {
    1448            0 :             return Err(EvictionCancelled::TimelineGone);
    1449              :         };
    1450              : 
    1451          204 :         let permit = {
    1452              :             // we cannot just `std::fs::remove_file` because there might already be an
    1453              :             // get_or_maybe_download which will inspect filesystem and reinitialize. filesystem
    1454              :             // operations must be done while holding the heavier_once_cell::InitPermit
    1455          228 :             let mut wait = std::pin::pin!(self.inner.get_or_init_detached());
    1456              : 
    1457          228 :             let waited = loop {
    1458              :                 // we must race to the Downloading starting, otherwise we would have to wait until the
    1459              :                 // completion of the download. waiting for download could be long and hinder our
    1460              :                 // efforts to alert on "hanging" evictions.
    1461          228 :                 tokio::select! {
    1462          228 :                     res = &mut wait => break res,
    1463          228 :                     _ = rx.changed() => {
    1464            0 :                         is_good_to_continue(&rx.borrow_and_update())?;
    1465              :                         // two possibilities for Status::Resident:
    1466              :                         // - the layer was found locally from disk by a read
    1467              :                         // - we missed a bunch of updates and now the layer is
    1468              :                         // again downloaded -- assume we'll fail later on with
    1469              :                         // version check or AlreadyReinitialized
    1470              :                     }
    1471              :                 }
    1472              :             };
    1473              : 
    1474              :             // re-check now that we have the guard or permit; all updates should have happened
    1475              :             // while holding the permit.
    1476          228 :             is_good_to_continue(&rx.borrow_and_update())?;
    1477              : 
    1478              :             // the term deinitialize is used here, because we clearing out the Weak will eventually
    1479              :             // lead to deallocating the reference counted value, and the value we
    1480              :             // `Guard::take_and_deinit` is likely to be the last because the Weak is never cloned.
    1481          228 :             let (_weak, permit) = match waited {
    1482          216 :                 Ok(guard) => {
    1483          216 :                     match &*guard {
    1484          204 :                         ResidentOrWantedEvicted::WantedEvicted(_weak, version)
    1485          204 :                             if *version == only_version =>
    1486          192 :                         {
    1487          192 :                             tracing::debug!(version, "deinitializing matching WantedEvicted");
    1488          192 :                             let (weak, permit) = guard.take_and_deinit();
    1489          192 :                             (Some(weak), permit)
    1490              :                         }
    1491           12 :                         ResidentOrWantedEvicted::WantedEvicted(_, version) => {
    1492           12 :                             // if we were not doing the version check, we would need to try to
    1493           12 :                             // upgrade the weak here to see if it really is dropped. version check
    1494           12 :                             // is done instead assuming that it is cheaper.
    1495           12 :                             tracing::debug!(
    1496              :                                 version,
    1497              :                                 only_version,
    1498            0 :                                 "version mismatch, not deinitializing"
    1499              :                             );
    1500           12 :                             return Err(EvictionCancelled::VersionCheckFailed);
    1501              :                         }
    1502              :                         ResidentOrWantedEvicted::Resident(_) => {
    1503           12 :                             return Err(EvictionCancelled::AlreadyReinitialized);
    1504              :                         }
    1505              :                     }
    1506              :                 }
    1507           12 :                 Err(permit) => {
    1508           12 :                     tracing::debug!("continuing after cancelled get_or_maybe_download or eviction");
    1509           12 :                     (None, permit)
    1510              :                 }
    1511              :             };
    1512              : 
    1513          204 :             permit
    1514          204 :         };
    1515          204 : 
    1516          204 :         let span = tracing::Span::current();
    1517          204 : 
    1518          204 :         let spawned_at = std::time::Instant::now();
    1519          204 : 
    1520          204 :         // this is on purpose a detached spawn; we don't need to wait for it
    1521          204 :         //
    1522          204 :         // eviction completion reporting is the only thing hinging on this, and it can be just as
    1523          204 :         // well from a spawn_blocking thread.
    1524          204 :         //
    1525          204 :         // important to note that now that we've acquired the permit we have made sure the evicted
    1526          204 :         // file is either the exact `WantedEvicted` we wanted to evict, or uninitialized in case
    1527          204 :         // there are multiple evictions. The rest is not cancellable, and we've now commited to
    1528          204 :         // evicting.
    1529          204 :         //
    1530          204 :         // If spawn_blocking has a queue and maximum number of threads are in use, we could stall
    1531          204 :         // reads. We will need to add cancellation for that if necessary.
    1532          204 :         Self::spawn_blocking(move || {
    1533          204 :             let _span = span.entered();
    1534          204 : 
    1535          204 :             let res = self.evict_blocking(&timeline, &gate, &permit);
    1536          204 : 
    1537          204 :             let waiters = self.inner.initializer_count();
    1538          204 : 
    1539          204 :             if waiters > 0 {
    1540            0 :                 LAYER_IMPL_METRICS.inc_evicted_with_waiters();
    1541          204 :             }
    1542              : 
    1543          204 :             let completed_in = spawned_at.elapsed();
    1544          204 :             LAYER_IMPL_METRICS.record_time_to_evict(completed_in);
    1545          204 : 
    1546          204 :             match res {
    1547          204 :                 Ok(()) => LAYER_IMPL_METRICS.inc_completed_evictions(),
    1548            0 :                 Err(e) => LAYER_IMPL_METRICS.inc_eviction_cancelled(e),
    1549              :             }
    1550              : 
    1551          204 :             tracing::debug!(?res, elapsed_ms=%completed_in.as_millis(), %waiters, "eviction completed");
    1552          204 :         });
    1553          204 : 
    1554          204 :         Ok(())
    1555          240 :     }
    1556              : 
    1557              :     /// This is blocking only to do just one spawn_blocking hop compared to multiple via tokio::fs.
    1558          204 :     fn evict_blocking(
    1559          204 :         &self,
    1560          204 :         timeline: &Timeline,
    1561          204 :         _gate: &gate::GateGuard,
    1562          204 :         _permit: &heavier_once_cell::InitPermit,
    1563          204 :     ) -> Result<(), EvictionCancelled> {
    1564          204 :         // now accesses to `self.inner.get_or_init*` wait on the semaphore or the `_permit`
    1565          204 : 
    1566          204 :         match capture_mtime_and_remove(&self.path) {
    1567          204 :             Ok(local_layer_mtime) => {
    1568          204 :                 let duration = SystemTime::now().duration_since(local_layer_mtime);
    1569          204 :                 match duration {
    1570          204 :                     Ok(elapsed) => {
    1571          204 :                         let accessed_and_visible = self.access_stats.accessed()
    1572           24 :                             && self.access_stats.visibility() == LayerVisibilityHint::Visible;
    1573          204 :                         if accessed_and_visible {
    1574           24 :                             // Only layers used for reads contribute to our "low residence" metric that is used
    1575           24 :                             // to detect thrashing.  Layers promoted for other reasons (e.g. compaction) are allowed
    1576           24 :                             // to be rapidly evicted without contributing to this metric.
    1577           24 :                             timeline
    1578           24 :                                 .metrics
    1579           24 :                                 .evictions_with_low_residence_duration
    1580           24 :                                 .read()
    1581           24 :                                 .unwrap()
    1582           24 :                                 .observe(elapsed);
    1583          180 :                         }
    1584              : 
    1585          204 :                         tracing::info!(
    1586            0 :                             residence_millis = elapsed.as_millis(),
    1587            0 :                             accessed_and_visible,
    1588            0 :                             "evicted layer after known residence period"
    1589              :                         );
    1590              :                     }
    1591              :                     Err(_) => {
    1592            0 :                         tracing::info!("evicted layer after unknown residence period");
    1593              :                     }
    1594              :                 }
    1595          204 :                 timeline.metrics.evictions.inc();
    1596          204 :                 timeline
    1597          204 :                     .metrics
    1598          204 :                     .resident_physical_size_sub(self.desc.file_size);
    1599              :             }
    1600            0 :             Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
    1601            0 :                 tracing::error!(
    1602              :                     layer_size = %self.desc.file_size,
    1603            0 :                     "failed to evict layer from disk, it was already gone"
    1604              :                 );
    1605            0 :                 return Err(EvictionCancelled::FileNotFound);
    1606              :             }
    1607            0 :             Err(e) => {
    1608            0 :                 // FIXME: this should probably be an abort
    1609            0 :                 tracing::error!("failed to evict file from disk: {e:#}");
    1610            0 :                 return Err(EvictionCancelled::RemoveFailed);
    1611              :             }
    1612              :         }
    1613              : 
    1614          204 :         self.access_stats.record_residence_event();
    1615          204 : 
    1616          204 :         *self.last_evicted_at.lock().unwrap() = Some(std::time::Instant::now());
    1617          204 : 
    1618          204 :         self.status.as_ref().unwrap().send_replace(Status::Evicted);
    1619          204 : 
    1620          204 :         Ok(())
    1621          204 :     }
    1622              : 
    1623        19120 :     fn metadata(&self) -> LayerFileMetadata {
    1624        19120 :         LayerFileMetadata::new(self.desc.file_size, self.generation, self.shard)
    1625        19120 :     }
    1626              : 
    1627              :     /// Needed to use entered runtime in tests, but otherwise use BACKGROUND_RUNTIME.
    1628              :     ///
    1629              :     /// Synchronizing with spawned tasks is very complicated otherwise.
    1630          324 :     fn spawn<F>(fut: F)
    1631          324 :     where
    1632          324 :         F: std::future::Future<Output = ()> + Send + 'static,
    1633          324 :     {
    1634          324 :         #[cfg(test)]
    1635          324 :         tokio::task::spawn(fut);
    1636          324 :         #[cfg(not(test))]
    1637          324 :         crate::task_mgr::BACKGROUND_RUNTIME.spawn(fut);
    1638          324 :     }
    1639              : 
    1640              :     /// Needed to use entered runtime in tests, but otherwise use BACKGROUND_RUNTIME.
    1641         3280 :     fn spawn_blocking<F>(f: F)
    1642         3280 :     where
    1643         3280 :         F: FnOnce() + Send + 'static,
    1644         3280 :     {
    1645         3280 :         #[cfg(test)]
    1646         3280 :         tokio::task::spawn_blocking(f);
    1647         3280 :         #[cfg(not(test))]
    1648         3280 :         crate::task_mgr::BACKGROUND_RUNTIME.spawn_blocking(f);
    1649         3280 :     }
    1650              : }
    1651              : 
    1652          204 : fn capture_mtime_and_remove(path: &Utf8Path) -> Result<SystemTime, std::io::Error> {
    1653          204 :     let m = path.metadata()?;
    1654          204 :     let local_layer_mtime = m.modified()?;
    1655          204 :     std::fs::remove_file(path)?;
    1656          204 :     Ok(local_layer_mtime)
    1657          204 : }
    1658              : 
    1659              : #[derive(Debug, thiserror::Error)]
    1660              : pub(crate) enum EvictionError {
    1661              :     #[error("layer was already evicted")]
    1662              :     NotFound,
    1663              : 
    1664              :     /// Evictions must always lose to downloads in races, and this time it happened.
    1665              :     #[error("layer was downloaded instead")]
    1666              :     Downloaded,
    1667              : 
    1668              :     #[error("eviction did not happen within timeout")]
    1669              :     Timeout,
    1670              : }
    1671              : 
    1672              : /// Error internal to the [`LayerInner::get_or_maybe_download`]
    1673              : #[derive(Debug, thiserror::Error)]
    1674              : pub(crate) enum DownloadError {
    1675              :     #[error("timeline has already shutdown")]
    1676              :     TimelineShutdown,
    1677              :     #[error("context denies downloading")]
    1678              :     ContextAndConfigReallyDeniesDownloads,
    1679              :     #[error("downloading is really required but not allowed by this method")]
    1680              :     DownloadRequired,
    1681              :     #[error("layer path exists, but it is not a file: {0:?}")]
    1682              :     NotFile(std::fs::FileType),
    1683              :     /// Why no error here? Because it will be reported by page_service. We should had also done
    1684              :     /// retries already.
    1685              :     #[error("downloading evicted layer file failed")]
    1686              :     DownloadFailed,
    1687              :     #[error("downloading failed, possibly for shutdown")]
    1688              :     DownloadCancelled,
    1689              :     #[error("pre-condition: stat before download failed")]
    1690              :     PreStatFailed(#[source] std::io::Error),
    1691              : 
    1692              :     #[cfg(test)]
    1693              :     #[error("failpoint: {0:?}")]
    1694              :     Failpoint(failpoints::FailpointKind),
    1695              : }
    1696              : 
    1697              : impl DownloadError {
    1698            0 :     pub(crate) fn is_cancelled(&self) -> bool {
    1699            0 :         matches!(self, DownloadError::DownloadCancelled)
    1700            0 :     }
    1701              : }
    1702              : 
    1703              : #[derive(Debug, PartialEq)]
    1704              : pub(crate) enum NeedsDownload {
    1705              :     NotFound,
    1706              :     NotFile(std::fs::FileType),
    1707              :     WrongSize { actual: u64, expected: u64 },
    1708              : }
    1709              : 
    1710              : impl std::fmt::Display for NeedsDownload {
    1711           84 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    1712           84 :         match self {
    1713           84 :             NeedsDownload::NotFound => write!(f, "file was not found"),
    1714            0 :             NeedsDownload::NotFile(ft) => write!(f, "path is not a file; {ft:?}"),
    1715            0 :             NeedsDownload::WrongSize { actual, expected } => {
    1716            0 :                 write!(f, "file size mismatch {actual} vs. {expected}")
    1717              :             }
    1718              :         }
    1719           84 :     }
    1720              : }
    1721              : 
    1722              : /// Existence of `DownloadedLayer` means that we have the file locally, and can later evict it.
    1723              : pub(crate) struct DownloadedLayer {
    1724              :     owner: Weak<LayerInner>,
    1725              :     // Use tokio OnceCell as we do not need to deinitialize this, it'll just get dropped with the
    1726              :     // DownloadedLayer
    1727              :     kind: tokio::sync::OnceCell<anyhow::Result<LayerKind>>,
    1728              :     version: usize,
    1729              : }
    1730              : 
    1731              : impl std::fmt::Debug for DownloadedLayer {
    1732            0 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    1733            0 :         f.debug_struct("DownloadedLayer")
    1734            0 :             // owner omitted because it is always "Weak"
    1735            0 :             .field("kind", &self.kind)
    1736            0 :             .field("version", &self.version)
    1737            0 :             .finish()
    1738            0 :     }
    1739              : }
    1740              : 
    1741              : impl Drop for DownloadedLayer {
    1742         4480 :     fn drop(&mut self) {
    1743         4480 :         if let Some(owner) = self.owner.upgrade() {
    1744          240 :             owner.on_downloaded_layer_drop(self.version);
    1745         4240 :         } else {
    1746         4240 :             // Layer::drop will handle cancelling the eviction; because of drop order and
    1747         4240 :             // `DownloadedLayer` never leaking, we cannot know here if eviction was requested.
    1748         4240 :         }
    1749         4480 :     }
    1750              : }
    1751              : 
    1752              : impl DownloadedLayer {
    1753              :     /// Initializes the `DeltaLayerInner` or `ImageLayerInner` within [`LayerKind`].
    1754              :     /// Failure to load the layer is sticky, i.e., future `get()` calls will return
    1755              :     /// the initial load failure immediately.
    1756              :     ///
    1757              :     /// `owner` parameter is a strong reference at the same `LayerInner` as the
    1758              :     /// `DownloadedLayer::owner` would be when upgraded. Given how this method ends up called,
    1759              :     /// we will always have the LayerInner on the callstack, so we can just use it.
    1760      1605787 :     async fn get<'a>(
    1761      1605787 :         &'a self,
    1762      1605787 :         owner: &Arc<LayerInner>,
    1763      1605787 :         ctx: &RequestContext,
    1764      1605787 :     ) -> anyhow::Result<&'a LayerKind> {
    1765      1605787 :         let init = || async {
    1766         7512 :             assert_eq!(
    1767         7512 :                 Weak::as_ptr(&self.owner),
    1768         7512 :                 Arc::as_ptr(owner),
    1769            0 :                 "these are the same, just avoiding the upgrade"
    1770              :             );
    1771              : 
    1772         7512 :             let res = if owner.desc.is_delta {
    1773         6612 :                 let ctx = RequestContextBuilder::from(ctx)
    1774         6612 :                     .page_content_kind(crate::context::PageContentKind::DeltaLayerSummary)
    1775         6612 :                     .attached_child();
    1776         6612 :                 let summary = Some(delta_layer::Summary::expected(
    1777         6612 :                     owner.desc.tenant_shard_id.tenant_id,
    1778         6612 :                     owner.desc.timeline_id,
    1779         6612 :                     owner.desc.key_range.clone(),
    1780         6612 :                     owner.desc.lsn_range.clone(),
    1781         6612 :                 ));
    1782         6612 :                 delta_layer::DeltaLayerInner::load(
    1783         6612 :                     &owner.path,
    1784         6612 :                     summary,
    1785         6612 :                     Some(owner.conf.max_vectored_read_bytes),
    1786         6612 :                     &ctx,
    1787         6612 :                 )
    1788         6612 :                 .await
    1789         6612 :                 .map(LayerKind::Delta)
    1790              :             } else {
    1791          900 :                 let ctx = RequestContextBuilder::from(ctx)
    1792          900 :                     .page_content_kind(crate::context::PageContentKind::ImageLayerSummary)
    1793          900 :                     .attached_child();
    1794          900 :                 let lsn = owner.desc.image_layer_lsn();
    1795          900 :                 let summary = Some(image_layer::Summary::expected(
    1796          900 :                     owner.desc.tenant_shard_id.tenant_id,
    1797          900 :                     owner.desc.timeline_id,
    1798          900 :                     owner.desc.key_range.clone(),
    1799          900 :                     lsn,
    1800          900 :                 ));
    1801          900 :                 image_layer::ImageLayerInner::load(
    1802          900 :                     &owner.path,
    1803          900 :                     lsn,
    1804          900 :                     summary,
    1805          900 :                     Some(owner.conf.max_vectored_read_bytes),
    1806          900 :                     &ctx,
    1807          900 :                 )
    1808          900 :                 .await
    1809          900 :                 .map(LayerKind::Image)
    1810              :             };
    1811              : 
    1812         7512 :             match res {
    1813         7512 :                 Ok(layer) => Ok(layer),
    1814            0 :                 Err(err) => {
    1815            0 :                     LAYER_IMPL_METRICS.inc_permanent_loading_failures();
    1816            0 :                     // We log this message once over the lifetime of `Self`
    1817            0 :                     // => Ok and good to log backtrace and path here.
    1818            0 :                     tracing::error!(
    1819            0 :                         "layer load failed, assuming permanent failure: {}: {err:?}",
    1820            0 :                         owner.path
    1821              :                     );
    1822            0 :                     Err(err)
    1823              :                 }
    1824              :             }
    1825        15024 :         };
    1826      1605787 :         self.kind
    1827      1605787 :             .get_or_init(init)
    1828      1605787 :             .await
    1829      1605787 :             .as_ref()
    1830      1605787 :             // We already logged the full backtrace above, once. Don't repeat that here.
    1831      1605787 :             .map_err(|e| anyhow::anyhow!("layer load failed earlier: {e}"))
    1832      1605787 :     }
    1833              : 
    1834      1599487 :     async fn get_values_reconstruct_data(
    1835      1599487 :         &self,
    1836      1599487 :         this: ResidentLayer,
    1837      1599487 :         keyspace: KeySpace,
    1838      1599487 :         lsn_range: Range<Lsn>,
    1839      1599487 :         reconstruct_data: &mut ValuesReconstructState,
    1840      1599487 :         ctx: &RequestContext,
    1841      1599487 :     ) -> Result<(), GetVectoredError> {
    1842              :         use LayerKind::*;
    1843              : 
    1844      1599487 :         match self
    1845      1599487 :             .get(&this.owner.0, ctx)
    1846      1599487 :             .await
    1847      1599487 :             .map_err(GetVectoredError::Other)?
    1848              :         {
    1849      1417975 :             Delta(d) => {
    1850      1417975 :                 d.get_values_reconstruct_data(this, keyspace, lsn_range, reconstruct_data, ctx)
    1851      1417975 :                     .await
    1852              :             }
    1853       181512 :             Image(i) => {
    1854       181512 :                 i.get_values_reconstruct_data(this, keyspace, reconstruct_data, ctx)
    1855       181512 :                     .await
    1856              :             }
    1857              :         }
    1858      1599487 :     }
    1859              : 
    1860           24 :     async fn dump(&self, owner: &Arc<LayerInner>, ctx: &RequestContext) -> anyhow::Result<()> {
    1861              :         use LayerKind::*;
    1862           24 :         match self.get(owner, ctx).await? {
    1863           24 :             Delta(d) => d.dump(ctx).await?,
    1864            0 :             Image(i) => i.dump(ctx).await?,
    1865              :         }
    1866              : 
    1867           24 :         Ok(())
    1868           24 :     }
    1869              : }
    1870              : 
    1871              : /// Wrapper around an actual layer implementation.
    1872              : #[derive(Debug)]
    1873              : enum LayerKind {
    1874              :     Delta(delta_layer::DeltaLayerInner),
    1875              :     Image(image_layer::ImageLayerInner),
    1876              : }
    1877              : 
    1878              : /// Guard for forcing a layer be resident while it exists.
    1879              : #[derive(Clone)]
    1880              : pub struct ResidentLayer {
    1881              :     owner: Layer,
    1882              :     downloaded: Arc<DownloadedLayer>,
    1883              : }
    1884              : 
    1885              : impl std::fmt::Display for ResidentLayer {
    1886        12996 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    1887        12996 :         write!(f, "{}", self.owner)
    1888        12996 :     }
    1889              : }
    1890              : 
    1891              : impl std::fmt::Debug for ResidentLayer {
    1892            0 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    1893            0 :         write!(f, "{}", self.owner)
    1894            0 :     }
    1895              : }
    1896              : 
    1897              : impl ResidentLayer {
    1898              :     /// Release the eviction guard, converting back into a plain [`Layer`].
    1899              :     ///
    1900              :     /// You can access the [`Layer`] also by using `as_ref`.
    1901         2520 :     pub(crate) fn drop_eviction_guard(self) -> Layer {
    1902         2520 :         self.into()
    1903         2520 :     }
    1904              : 
    1905              :     /// Loads all keys stored in the layer. Returns key, lsn and value size.
    1906            0 :     #[tracing::instrument(level = tracing::Level::DEBUG, skip_all, fields(layer=%self))]
    1907              :     pub(crate) async fn load_keys<'a>(
    1908              :         &'a self,
    1909              :         ctx: &RequestContext,
    1910              :     ) -> anyhow::Result<Vec<pageserver_api::key::Key>> {
    1911              :         use LayerKind::*;
    1912              : 
    1913              :         let owner = &self.owner.0;
    1914              :         let inner = self.downloaded.get(owner, ctx).await?;
    1915              : 
    1916              :         // this is valid because the DownloadedLayer::kind is a OnceCell, not a
    1917              :         // Mutex<OnceCell>, so we cannot go and deinitialize the value with OnceCell::take
    1918              :         // while it's being held.
    1919              :         self.owner.record_access(ctx);
    1920              : 
    1921              :         let res = match inner {
    1922              :             Delta(d) => delta_layer::DeltaLayerInner::load_keys(d, ctx).await,
    1923              :             Image(i) => image_layer::ImageLayerInner::load_keys(i, ctx).await,
    1924              :         };
    1925            0 :         res.with_context(|| format!("Layer index is corrupted for {self}"))
    1926              :     }
    1927              : 
    1928              :     /// Read all they keys in this layer which match the ShardIdentity, and write them all to
    1929              :     /// the provided writer.  Return the number of keys written.
    1930           48 :     #[tracing::instrument(level = tracing::Level::DEBUG, skip_all, fields(layer=%self))]
    1931              :     pub(crate) async fn filter(
    1932              :         &self,
    1933              :         shard_identity: &ShardIdentity,
    1934              :         writer: &mut ImageLayerWriter,
    1935              :         ctx: &RequestContext,
    1936              :     ) -> Result<usize, CompactionError> {
    1937              :         use LayerKind::*;
    1938              : 
    1939              :         match self
    1940              :             .downloaded
    1941              :             .get(&self.owner.0, ctx)
    1942              :             .await
    1943              :             .map_err(CompactionError::Other)?
    1944              :         {
    1945              :             Delta(_) => {
    1946              :                 return Err(CompactionError::Other(anyhow::anyhow!(format!(
    1947              :                     "cannot filter() on a delta layer {self}"
    1948              :                 ))));
    1949              :             }
    1950              :             Image(i) => i
    1951              :                 .filter(shard_identity, writer, ctx)
    1952              :                 .await
    1953              :                 .map_err(CompactionError::Other),
    1954              :         }
    1955              :     }
    1956              : 
    1957              :     /// Returns the amount of keys and values written to the writer.
    1958           60 :     pub(crate) async fn copy_delta_prefix(
    1959           60 :         &self,
    1960           60 :         writer: &mut super::delta_layer::DeltaLayerWriter,
    1961           60 :         until: Lsn,
    1962           60 :         ctx: &RequestContext,
    1963           60 :     ) -> anyhow::Result<usize> {
    1964              :         use LayerKind::*;
    1965              : 
    1966           60 :         let owner = &self.owner.0;
    1967           60 : 
    1968           60 :         match self.downloaded.get(owner, ctx).await? {
    1969           60 :             Delta(d) => d
    1970           60 :                 .copy_prefix(writer, until, ctx)
    1971           60 :                 .await
    1972           60 :                 .with_context(|| format!("copy_delta_prefix until {until} of {self}")),
    1973            0 :             Image(_) => anyhow::bail!(format!("cannot copy_lsn_prefix of image layer {self}")),
    1974              :         }
    1975           60 :     }
    1976              : 
    1977        11260 :     pub(crate) fn local_path(&self) -> &Utf8Path {
    1978        11260 :         &self.owner.0.path
    1979        11260 :     }
    1980              : 
    1981        13368 :     pub(crate) fn metadata(&self) -> LayerFileMetadata {
    1982        13368 :         self.owner.metadata()
    1983        13368 :     }
    1984              : 
    1985              :     /// Cast the layer to a delta, return an error if it is an image layer.
    1986         5736 :     pub(crate) async fn get_as_delta(
    1987         5736 :         &self,
    1988         5736 :         ctx: &RequestContext,
    1989         5736 :     ) -> anyhow::Result<&delta_layer::DeltaLayerInner> {
    1990              :         use LayerKind::*;
    1991         5736 :         match self.downloaded.get(&self.owner.0, ctx).await? {
    1992         5736 :             Delta(d) => Ok(d),
    1993            0 :             Image(_) => Err(anyhow::anyhow!("image layer")),
    1994              :         }
    1995         5736 :     }
    1996              : 
    1997              :     /// Cast the layer to an image, return an error if it is a delta layer.
    1998          432 :     pub(crate) async fn get_as_image(
    1999          432 :         &self,
    2000          432 :         ctx: &RequestContext,
    2001          432 :     ) -> anyhow::Result<&image_layer::ImageLayerInner> {
    2002              :         use LayerKind::*;
    2003          432 :         match self.downloaded.get(&self.owner.0, ctx).await? {
    2004          432 :             Image(d) => Ok(d),
    2005            0 :             Delta(_) => Err(anyhow::anyhow!("delta layer")),
    2006              :         }
    2007          432 :     }
    2008              : }
    2009              : 
    2010              : impl AsLayerDesc for ResidentLayer {
    2011      7145356 :     fn layer_desc(&self) -> &PersistentLayerDesc {
    2012      7145356 :         self.owner.layer_desc()
    2013      7145356 :     }
    2014              : }
    2015              : 
    2016              : impl AsRef<Layer> for ResidentLayer {
    2017        12384 :     fn as_ref(&self) -> &Layer {
    2018        12384 :         &self.owner
    2019        12384 :     }
    2020              : }
    2021              : 
    2022              : /// Drop the eviction guard.
    2023              : impl From<ResidentLayer> for Layer {
    2024         2520 :     fn from(value: ResidentLayer) -> Self {
    2025         2520 :         value.owner
    2026         2520 :     }
    2027              : }
    2028              : 
    2029              : use metrics::IntCounter;
    2030              : 
    2031              : pub(crate) struct LayerImplMetrics {
    2032              :     started_evictions: IntCounter,
    2033              :     completed_evictions: IntCounter,
    2034              :     cancelled_evictions: enum_map::EnumMap<EvictionCancelled, IntCounter>,
    2035              : 
    2036              :     started_deletes: IntCounter,
    2037              :     completed_deletes: IntCounter,
    2038              :     failed_deletes: enum_map::EnumMap<DeleteFailed, IntCounter>,
    2039              : 
    2040              :     rare_counters: enum_map::EnumMap<RareEvent, IntCounter>,
    2041              :     inits_cancelled: metrics::core::GenericCounter<metrics::core::AtomicU64>,
    2042              :     redownload_after: metrics::Histogram,
    2043              :     time_to_evict: metrics::Histogram,
    2044              : }
    2045              : 
    2046              : impl Default for LayerImplMetrics {
    2047          324 :     fn default() -> Self {
    2048              :         use enum_map::Enum;
    2049              : 
    2050              :         // reminder: these will be pageserver_layer_* with "_total" suffix
    2051              : 
    2052          324 :         let started_evictions = metrics::register_int_counter!(
    2053          324 :             "pageserver_layer_started_evictions",
    2054          324 :             "Evictions started in the Layer implementation"
    2055          324 :         )
    2056          324 :         .unwrap();
    2057          324 :         let completed_evictions = metrics::register_int_counter!(
    2058          324 :             "pageserver_layer_completed_evictions",
    2059          324 :             "Evictions completed in the Layer implementation"
    2060          324 :         )
    2061          324 :         .unwrap();
    2062          324 : 
    2063          324 :         let cancelled_evictions = metrics::register_int_counter_vec!(
    2064          324 :             "pageserver_layer_cancelled_evictions_count",
    2065          324 :             "Different reasons for evictions to have been cancelled or failed",
    2066          324 :             &["reason"]
    2067          324 :         )
    2068          324 :         .unwrap();
    2069          324 : 
    2070         2916 :         let cancelled_evictions = enum_map::EnumMap::from_array(std::array::from_fn(|i| {
    2071         2916 :             let reason = EvictionCancelled::from_usize(i);
    2072         2916 :             let s = reason.as_str();
    2073         2916 :             cancelled_evictions.with_label_values(&[s])
    2074         2916 :         }));
    2075          324 : 
    2076          324 :         let started_deletes = metrics::register_int_counter!(
    2077          324 :             "pageserver_layer_started_deletes",
    2078          324 :             "Deletions on drop pending in the Layer implementation"
    2079          324 :         )
    2080          324 :         .unwrap();
    2081          324 :         let completed_deletes = metrics::register_int_counter!(
    2082          324 :             "pageserver_layer_completed_deletes",
    2083          324 :             "Deletions on drop completed in the Layer implementation"
    2084          324 :         )
    2085          324 :         .unwrap();
    2086          324 : 
    2087          324 :         let failed_deletes = metrics::register_int_counter_vec!(
    2088          324 :             "pageserver_layer_failed_deletes_count",
    2089          324 :             "Different reasons for deletions on drop to have failed",
    2090          324 :             &["reason"]
    2091          324 :         )
    2092          324 :         .unwrap();
    2093          324 : 
    2094          648 :         let failed_deletes = enum_map::EnumMap::from_array(std::array::from_fn(|i| {
    2095          648 :             let reason = DeleteFailed::from_usize(i);
    2096          648 :             let s = reason.as_str();
    2097          648 :             failed_deletes.with_label_values(&[s])
    2098          648 :         }));
    2099          324 : 
    2100          324 :         let rare_counters = metrics::register_int_counter_vec!(
    2101          324 :             "pageserver_layer_assumed_rare_count",
    2102          324 :             "Times unexpected or assumed rare event happened",
    2103          324 :             &["event"]
    2104          324 :         )
    2105          324 :         .unwrap();
    2106          324 : 
    2107         2268 :         let rare_counters = enum_map::EnumMap::from_array(std::array::from_fn(|i| {
    2108         2268 :             let event = RareEvent::from_usize(i);
    2109         2268 :             let s = event.as_str();
    2110         2268 :             rare_counters.with_label_values(&[s])
    2111         2268 :         }));
    2112          324 : 
    2113          324 :         let inits_cancelled = metrics::register_int_counter!(
    2114          324 :             "pageserver_layer_inits_cancelled_count",
    2115          324 :             "Times Layer initialization was cancelled",
    2116          324 :         )
    2117          324 :         .unwrap();
    2118          324 : 
    2119          324 :         let redownload_after = {
    2120          324 :             let minute = 60.0;
    2121          324 :             let hour = 60.0 * minute;
    2122          324 :             metrics::register_histogram!(
    2123          324 :                 "pageserver_layer_redownloaded_after",
    2124          324 :                 "Time between evicting and re-downloading.",
    2125          324 :                 vec![
    2126          324 :                     10.0,
    2127          324 :                     30.0,
    2128          324 :                     minute,
    2129          324 :                     5.0 * minute,
    2130          324 :                     15.0 * minute,
    2131          324 :                     30.0 * minute,
    2132          324 :                     hour,
    2133          324 :                     12.0 * hour,
    2134          324 :                 ]
    2135          324 :             )
    2136          324 :             .unwrap()
    2137          324 :         };
    2138          324 : 
    2139          324 :         let time_to_evict = metrics::register_histogram!(
    2140          324 :             "pageserver_layer_eviction_held_permit_seconds",
    2141          324 :             "Time eviction held the permit.",
    2142          324 :             vec![0.001, 0.010, 0.100, 0.500, 1.000, 5.000]
    2143          324 :         )
    2144          324 :         .unwrap();
    2145          324 : 
    2146          324 :         Self {
    2147          324 :             started_evictions,
    2148          324 :             completed_evictions,
    2149          324 :             cancelled_evictions,
    2150          324 : 
    2151          324 :             started_deletes,
    2152          324 :             completed_deletes,
    2153          324 :             failed_deletes,
    2154          324 : 
    2155          324 :             rare_counters,
    2156          324 :             inits_cancelled,
    2157          324 :             redownload_after,
    2158          324 :             time_to_evict,
    2159          324 :         }
    2160          324 :     }
    2161              : }
    2162              : 
    2163              : impl LayerImplMetrics {
    2164          252 :     fn inc_started_evictions(&self) {
    2165          252 :         self.started_evictions.inc();
    2166          252 :     }
    2167          204 :     fn inc_completed_evictions(&self) {
    2168          204 :         self.completed_evictions.inc();
    2169          204 :     }
    2170           48 :     fn inc_eviction_cancelled(&self, reason: EvictionCancelled) {
    2171           48 :         self.cancelled_evictions[reason].inc()
    2172           48 :     }
    2173              : 
    2174         3096 :     fn inc_started_deletes(&self) {
    2175         3096 :         self.started_deletes.inc();
    2176         3096 :     }
    2177         3064 :     fn inc_completed_deletes(&self) {
    2178         3064 :         self.completed_deletes.inc();
    2179         3064 :     }
    2180            0 :     fn inc_deletes_failed(&self, reason: DeleteFailed) {
    2181            0 :         self.failed_deletes[reason].inc();
    2182            0 :     }
    2183              : 
    2184              :     /// Counted separatedly from failed layer deletes because we will complete the layer deletion
    2185              :     /// attempt regardless of failure to delete local file.
    2186            0 :     fn inc_delete_removes_failed(&self) {
    2187            0 :         self.rare_counters[RareEvent::RemoveOnDropFailed].inc();
    2188            0 :     }
    2189              : 
    2190              :     /// Expected rare just as cancellations are rare, but we could have cancellations separate from
    2191              :     /// the single caller which can start the download, so use this counter to separte them.
    2192            0 :     fn inc_init_completed_without_requester(&self) {
    2193            0 :         self.rare_counters[RareEvent::InitCompletedWithoutRequester].inc();
    2194            0 :     }
    2195              : 
    2196              :     /// Expected rare because cancellations are unexpected, and failures are unexpected
    2197            0 :     fn inc_download_failed_without_requester(&self) {
    2198            0 :         self.rare_counters[RareEvent::DownloadFailedWithoutRequester].inc();
    2199            0 :     }
    2200              : 
    2201              :     /// The Weak in ResidentOrWantedEvicted::WantedEvicted was successfully upgraded.
    2202              :     ///
    2203              :     /// If this counter is always zero, we should replace ResidentOrWantedEvicted type with an
    2204              :     /// Option.
    2205            0 :     fn inc_raced_wanted_evicted_accesses(&self) {
    2206            0 :         self.rare_counters[RareEvent::UpgradedWantedEvicted].inc();
    2207            0 :     }
    2208              : 
    2209              :     /// These are only expected for [`Self::inc_init_cancelled`] amount when
    2210              :     /// running with remote storage.
    2211           36 :     fn inc_init_needed_no_download(&self) {
    2212           36 :         self.rare_counters[RareEvent::InitWithoutDownload].inc();
    2213           36 :     }
    2214              : 
    2215              :     /// Expected rare because all layer files should be readable and good
    2216            0 :     fn inc_permanent_loading_failures(&self) {
    2217            0 :         self.rare_counters[RareEvent::PermanentLoadingFailure].inc();
    2218            0 :     }
    2219              : 
    2220            0 :     fn inc_init_cancelled(&self) {
    2221            0 :         self.inits_cancelled.inc()
    2222            0 :     }
    2223              : 
    2224           84 :     fn record_redownloaded_after(&self, duration: std::time::Duration) {
    2225           84 :         self.redownload_after.observe(duration.as_secs_f64())
    2226           84 :     }
    2227              : 
    2228              :     /// This would be bad if it ever happened, or mean extreme disk pressure. We should probably
    2229              :     /// instead cancel eviction if we would have read waiters. We cannot however separate reads
    2230              :     /// from other evictions, so this could have noise as well.
    2231            0 :     fn inc_evicted_with_waiters(&self) {
    2232            0 :         self.rare_counters[RareEvent::EvictedWithWaiters].inc();
    2233            0 :     }
    2234              : 
    2235              :     /// Recorded at least initially as the permit is now acquired in async context before
    2236              :     /// spawn_blocking action.
    2237          204 :     fn record_time_to_evict(&self, duration: std::time::Duration) {
    2238          204 :         self.time_to_evict.observe(duration.as_secs_f64())
    2239          204 :     }
    2240              : }
    2241              : 
    2242              : #[derive(Debug, Clone, Copy, enum_map::Enum)]
    2243              : enum EvictionCancelled {
    2244              :     LayerGone,
    2245              :     TimelineGone,
    2246              :     VersionCheckFailed,
    2247              :     FileNotFound,
    2248              :     RemoveFailed,
    2249              :     AlreadyReinitialized,
    2250              :     /// Not evicted because of a pending reinitialization
    2251              :     LostToDownload,
    2252              :     /// After eviction, there was a new layer access which cancelled the eviction.
    2253              :     UpgradedBackOnAccess,
    2254              :     UnexpectedEvictedState,
    2255              : }
    2256              : 
    2257              : impl EvictionCancelled {
    2258         2916 :     fn as_str(&self) -> &'static str {
    2259         2916 :         match self {
    2260          324 :             EvictionCancelled::LayerGone => "layer_gone",
    2261          324 :             EvictionCancelled::TimelineGone => "timeline_gone",
    2262          324 :             EvictionCancelled::VersionCheckFailed => "version_check_fail",
    2263          324 :             EvictionCancelled::FileNotFound => "file_not_found",
    2264          324 :             EvictionCancelled::RemoveFailed => "remove_failed",
    2265          324 :             EvictionCancelled::AlreadyReinitialized => "already_reinitialized",
    2266          324 :             EvictionCancelled::LostToDownload => "lost_to_download",
    2267          324 :             EvictionCancelled::UpgradedBackOnAccess => "upgraded_back_on_access",
    2268          324 :             EvictionCancelled::UnexpectedEvictedState => "unexpected_evicted_state",
    2269              :         }
    2270         2916 :     }
    2271              : }
    2272              : 
    2273              : #[derive(enum_map::Enum)]
    2274              : enum DeleteFailed {
    2275              :     TimelineGone,
    2276              :     DeleteSchedulingFailed,
    2277              : }
    2278              : 
    2279              : impl DeleteFailed {
    2280          648 :     fn as_str(&self) -> &'static str {
    2281          648 :         match self {
    2282          324 :             DeleteFailed::TimelineGone => "timeline_gone",
    2283          324 :             DeleteFailed::DeleteSchedulingFailed => "delete_scheduling_failed",
    2284              :         }
    2285          648 :     }
    2286              : }
    2287              : 
    2288              : #[derive(enum_map::Enum)]
    2289              : enum RareEvent {
    2290              :     RemoveOnDropFailed,
    2291              :     InitCompletedWithoutRequester,
    2292              :     DownloadFailedWithoutRequester,
    2293              :     UpgradedWantedEvicted,
    2294              :     InitWithoutDownload,
    2295              :     PermanentLoadingFailure,
    2296              :     EvictedWithWaiters,
    2297              : }
    2298              : 
    2299              : impl RareEvent {
    2300         2268 :     fn as_str(&self) -> &'static str {
    2301              :         use RareEvent::*;
    2302              : 
    2303         2268 :         match self {
    2304          324 :             RemoveOnDropFailed => "remove_on_drop_failed",
    2305          324 :             InitCompletedWithoutRequester => "init_completed_without",
    2306          324 :             DownloadFailedWithoutRequester => "download_failed_without",
    2307          324 :             UpgradedWantedEvicted => "raced_wanted_evicted",
    2308          324 :             InitWithoutDownload => "init_needed_no_download",
    2309          324 :             PermanentLoadingFailure => "permanent_loading_failure",
    2310          324 :             EvictedWithWaiters => "evicted_with_waiters",
    2311              :         }
    2312         2268 :     }
    2313              : }
    2314              : 
    2315              : pub(crate) static LAYER_IMPL_METRICS: once_cell::sync::Lazy<LayerImplMetrics> =
    2316              :     once_cell::sync::Lazy::new(LayerImplMetrics::default);
        

Generated by: LCOV version 2.1-beta