LCOV - code coverage report
Current view: top level - pageserver/src/tenant - timeline.rs (source / functions) Coverage Total Hit
Test: a43a77853355b937a79c57b07a8f05607cf29e6c.info Lines: 64.5 % 3355 2165
Test Date: 2024-09-19 12:04:32 Functions: 58.8 % 323 190

            Line data    Source code
       1              : pub(crate) mod analysis;
       2              : pub(crate) mod compaction;
       3              : pub mod delete;
       4              : pub(crate) mod detach_ancestor;
       5              : mod eviction_task;
       6              : pub(crate) mod handle;
       7              : mod init;
       8              : pub mod layer_manager;
       9              : pub(crate) mod logical_size;
      10              : pub mod span;
      11              : pub mod uninit;
      12              : mod walreceiver;
      13              : 
      14              : use anyhow::{anyhow, bail, ensure, Context, Result};
      15              : use arc_swap::ArcSwap;
      16              : use bytes::Bytes;
      17              : use camino::Utf8Path;
      18              : use chrono::{DateTime, Utc};
      19              : use enumset::EnumSet;
      20              : use fail::fail_point;
      21              : use handle::ShardTimelineId;
      22              : use once_cell::sync::Lazy;
      23              : use pageserver_api::{
      24              :     key::{
      25              :         CompactKey, KEY_SIZE, METADATA_KEY_BEGIN_PREFIX, METADATA_KEY_END_PREFIX,
      26              :         NON_INHERITED_RANGE, NON_INHERITED_SPARSE_RANGE,
      27              :     },
      28              :     keyspace::{KeySpaceAccum, KeySpaceRandomAccum, SparseKeyPartitioning},
      29              :     models::{
      30              :         AtomicAuxFilePolicy, AuxFilePolicy, CompactionAlgorithm, CompactionAlgorithmSettings,
      31              :         DownloadRemoteLayersTaskInfo, DownloadRemoteLayersTaskSpawnRequest, EvictionPolicy,
      32              :         InMemoryLayerInfo, LayerMapInfo, LsnLease, TimelineState,
      33              :     },
      34              :     reltag::BlockNumber,
      35              :     shard::{ShardIdentity, ShardNumber, TenantShardId},
      36              : };
      37              : use rand::Rng;
      38              : use serde_with::serde_as;
      39              : use storage_broker::BrokerClientChannel;
      40              : use tokio::{
      41              :     runtime::Handle,
      42              :     sync::{oneshot, watch},
      43              : };
      44              : use tokio_util::sync::CancellationToken;
      45              : use tracing::*;
      46              : use utils::{
      47              :     fs_ext, pausable_failpoint,
      48              :     sync::gate::{Gate, GateGuard},
      49              : };
      50              : 
      51              : use std::pin::pin;
      52              : use std::sync::atomic::Ordering as AtomicOrdering;
      53              : use std::sync::{Arc, Mutex, RwLock, Weak};
      54              : use std::time::{Duration, Instant, SystemTime};
      55              : use std::{
      56              :     array,
      57              :     collections::{BTreeMap, HashMap, HashSet},
      58              :     sync::atomic::AtomicU64,
      59              : };
      60              : use std::{cmp::min, ops::ControlFlow};
      61              : use std::{
      62              :     collections::btree_map::Entry,
      63              :     ops::{Deref, Range},
      64              : };
      65              : 
      66              : use crate::{
      67              :     aux_file::AuxFileSizeEstimator,
      68              :     tenant::{
      69              :         layer_map::{LayerMap, SearchResult},
      70              :         metadata::TimelineMetadata,
      71              :         storage_layer::{inmemory_layer::IndexEntry, PersistentLayerDesc},
      72              :     },
      73              :     walredo,
      74              : };
      75              : use crate::{
      76              :     context::{DownloadBehavior, RequestContext},
      77              :     disk_usage_eviction_task::DiskUsageEvictionInfo,
      78              :     pgdatadir_mapping::CollectKeySpaceError,
      79              : };
      80              : use crate::{
      81              :     disk_usage_eviction_task::finite_f32,
      82              :     tenant::storage_layer::{
      83              :         AsLayerDesc, DeltaLayerWriter, EvictionError, ImageLayerWriter, InMemoryLayer, Layer,
      84              :         LayerAccessStatsReset, LayerName, ResidentLayer, ValueReconstructState,
      85              :         ValuesReconstructState,
      86              :     },
      87              : };
      88              : use crate::{
      89              :     disk_usage_eviction_task::EvictionCandidate, tenant::storage_layer::delta_layer::DeltaEntry,
      90              : };
      91              : use crate::{
      92              :     l0_flush::{self, L0FlushGlobalState},
      93              :     metrics::GetKind,
      94              : };
      95              : use crate::{
      96              :     metrics::ScanLatencyOngoingRecording, tenant::timeline::logical_size::CurrentLogicalSize,
      97              : };
      98              : use crate::{pgdatadir_mapping::LsnForTimestamp, tenant::tasks::BackgroundLoopKind};
      99              : use crate::{pgdatadir_mapping::MAX_AUX_FILE_V2_DELTAS, tenant::storage_layer::PersistentLayerKey};
     100              : use crate::{
     101              :     pgdatadir_mapping::{AuxFilesDirectory, DirectoryKind},
     102              :     virtual_file::{MaybeFatalIo, VirtualFile},
     103              : };
     104              : use pageserver_api::config::tenant_conf_defaults::DEFAULT_PITR_INTERVAL;
     105              : 
     106              : use crate::config::PageServerConf;
     107              : use crate::keyspace::{KeyPartitioning, KeySpace};
     108              : use crate::metrics::TimelineMetrics;
     109              : use crate::pgdatadir_mapping::CalculateLogicalSizeError;
     110              : use crate::tenant::config::TenantConfOpt;
     111              : use pageserver_api::reltag::RelTag;
     112              : use pageserver_api::shard::ShardIndex;
     113              : 
     114              : use postgres_connection::PgConnectionConfig;
     115              : use postgres_ffi::to_pg_timestamp;
     116              : use utils::{
     117              :     completion,
     118              :     generation::Generation,
     119              :     id::TimelineId,
     120              :     lsn::{AtomicLsn, Lsn, RecordLsn},
     121              :     seqwait::SeqWait,
     122              :     simple_rcu::{Rcu, RcuReadGuard},
     123              : };
     124              : 
     125              : use crate::repository::GcResult;
     126              : use crate::repository::{Key, Value};
     127              : use crate::task_mgr;
     128              : use crate::task_mgr::TaskKind;
     129              : use crate::ZERO_PAGE;
     130              : 
     131              : use self::delete::DeleteTimelineFlow;
     132              : pub(super) use self::eviction_task::EvictionTaskTenantState;
     133              : use self::eviction_task::EvictionTaskTimelineState;
     134              : use self::layer_manager::LayerManager;
     135              : use self::logical_size::LogicalSize;
     136              : use self::walreceiver::{WalReceiver, WalReceiverConf};
     137              : 
     138              : use super::{
     139              :     config::TenantConf, storage_layer::inmemory_layer, storage_layer::LayerVisibilityHint,
     140              :     upload_queue::NotInitialized,
     141              : };
     142              : use super::{debug_assert_current_span_has_tenant_and_timeline_id, AttachedTenantConf};
     143              : use super::{remote_timeline_client::index::IndexPart, storage_layer::LayerFringe};
     144              : use super::{
     145              :     remote_timeline_client::RemoteTimelineClient, remote_timeline_client::WaitCompletionError,
     146              :     storage_layer::ReadableLayer,
     147              : };
     148              : use super::{
     149              :     secondary::heatmap::{HeatMapLayer, HeatMapTimeline},
     150              :     GcError,
     151              : };
     152              : 
     153              : #[derive(Debug, PartialEq, Eq, Clone, Copy)]
     154              : pub(crate) enum FlushLoopState {
     155              :     NotStarted,
     156              :     Running {
     157              :         #[cfg(test)]
     158              :         expect_initdb_optimization: bool,
     159              :         #[cfg(test)]
     160              :         initdb_optimization_count: usize,
     161              :     },
     162              :     Exited,
     163              : }
     164              : 
     165              : #[derive(Debug, Copy, Clone, PartialEq, Eq)]
     166              : pub enum ImageLayerCreationMode {
     167              :     /// Try to create image layers based on `time_for_new_image_layer`. Used in compaction code path.
     168              :     Try,
     169              :     /// Force creating the image layers if possible. For now, no image layers will be created
     170              :     /// for metadata keys. Used in compaction code path with force flag enabled.
     171              :     Force,
     172              :     /// Initial ingestion of the data, and no data should be dropped in this function. This
     173              :     /// means that no metadata keys should be included in the partitions. Used in flush frozen layer
     174              :     /// code path.
     175              :     Initial,
     176              : }
     177              : 
     178              : impl std::fmt::Display for ImageLayerCreationMode {
     179         2124 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
     180         2124 :         write!(f, "{:?}", self)
     181         2124 :     }
     182              : }
     183              : 
     184              : /// Temporary function for immutable storage state refactor, ensures we are dropping mutex guard instead of other things.
     185              : /// Can be removed after all refactors are done.
     186           84 : fn drop_rlock<T>(rlock: tokio::sync::RwLockReadGuard<T>) {
     187           84 :     drop(rlock)
     188           84 : }
     189              : 
     190              : /// Temporary function for immutable storage state refactor, ensures we are dropping mutex guard instead of other things.
     191              : /// Can be removed after all refactors are done.
     192         2208 : fn drop_wlock<T>(rlock: tokio::sync::RwLockWriteGuard<'_, T>) {
     193         2208 :     drop(rlock)
     194         2208 : }
     195              : 
     196              : /// The outward-facing resources required to build a Timeline
     197              : pub struct TimelineResources {
     198              :     pub remote_client: RemoteTimelineClient,
     199              :     pub timeline_get_throttle: Arc<
     200              :         crate::tenant::throttle::Throttle<&'static crate::metrics::tenant_throttling::TimelineGet>,
     201              :     >,
     202              :     pub l0_flush_global_state: l0_flush::L0FlushGlobalState,
     203              : }
     204              : 
     205              : pub(crate) struct AuxFilesState {
     206              :     pub(crate) dir: Option<AuxFilesDirectory>,
     207              :     pub(crate) n_deltas: usize,
     208              : }
     209              : 
     210              : /// The relation size cache caches relation sizes at the end of the timeline. It speeds up WAL
     211              : /// ingestion considerably, because WAL ingestion needs to check on most records if the record
     212              : /// implicitly extends the relation.  At startup, `complete_as_of` is initialized to the current end
     213              : /// of the timeline (disk_consistent_lsn).  It's used on reads of relation sizes to check if the
     214              : /// value can be used to also update the cache, see [`Timeline::update_cached_rel_size`].
     215              : pub(crate) struct RelSizeCache {
     216              :     pub(crate) complete_as_of: Lsn,
     217              :     pub(crate) map: HashMap<RelTag, (Lsn, BlockNumber)>,
     218              : }
     219              : 
     220              : pub struct Timeline {
     221              :     pub(crate) conf: &'static PageServerConf,
     222              :     tenant_conf: Arc<ArcSwap<AttachedTenantConf>>,
     223              : 
     224              :     myself: Weak<Self>,
     225              : 
     226              :     pub(crate) tenant_shard_id: TenantShardId,
     227              :     pub timeline_id: TimelineId,
     228              : 
     229              :     /// The generation of the tenant that instantiated us: this is used for safety when writing remote objects.
     230              :     /// Never changes for the lifetime of this [`Timeline`] object.
     231              :     ///
     232              :     /// This duplicates the generation stored in LocationConf, but that structure is mutable:
     233              :     /// this copy enforces the invariant that generatio doesn't change during a Tenant's lifetime.
     234              :     pub(crate) generation: Generation,
     235              : 
     236              :     /// The detailed sharding information from our parent Tenant.  This enables us to map keys
     237              :     /// to shards, and is constant through the lifetime of this Timeline.
     238              :     shard_identity: ShardIdentity,
     239              : 
     240              :     pub pg_version: u32,
     241              : 
     242              :     /// The tuple has two elements.
     243              :     /// 1. `LayerFileManager` keeps track of the various physical representations of the layer files (inmem, local, remote).
     244              :     /// 2. `LayerMap`, the acceleration data structure for `get_reconstruct_data`.
     245              :     ///
     246              :     /// `LayerMap` maps out the `(PAGE,LSN) / (KEY,LSN)` space, which is composed of `(KeyRange, LsnRange)` rectangles.
     247              :     /// We describe these rectangles through the `PersistentLayerDesc` struct.
     248              :     ///
     249              :     /// When we want to reconstruct a page, we first find the `PersistentLayerDesc`'s that we need for page reconstruction,
     250              :     /// using `LayerMap`. Then, we use `LayerFileManager` to get the `PersistentLayer`'s that correspond to the
     251              :     /// `PersistentLayerDesc`'s.
     252              :     ///
     253              :     /// Hence, it's important to keep things coherent. The `LayerFileManager` must always have an entry for all
     254              :     /// `PersistentLayerDesc`'s in the `LayerMap`. If it doesn't, `LayerFileManager::get_from_desc` will panic at
     255              :     /// runtime, e.g., during page reconstruction.
     256              :     ///
     257              :     /// In the future, we'll be able to split up the tuple of LayerMap and `LayerFileManager`,
     258              :     /// so that e.g. on-demand-download/eviction, and layer spreading, can operate just on `LayerFileManager`.
     259              :     pub(crate) layers: tokio::sync::RwLock<LayerManager>,
     260              : 
     261              :     last_freeze_at: AtomicLsn,
     262              :     // Atomic would be more appropriate here.
     263              :     last_freeze_ts: RwLock<Instant>,
     264              : 
     265              :     pub(crate) standby_horizon: AtomicLsn,
     266              : 
     267              :     // WAL redo manager. `None` only for broken tenants.
     268              :     walredo_mgr: Option<Arc<super::WalRedoManager>>,
     269              : 
     270              :     /// Remote storage client.
     271              :     /// See [`remote_timeline_client`](super::remote_timeline_client) module comment for details.
     272              :     pub remote_client: Arc<RemoteTimelineClient>,
     273              : 
     274              :     // What page versions do we hold in the repository? If we get a
     275              :     // request > last_record_lsn, we need to wait until we receive all
     276              :     // the WAL up to the request. The SeqWait provides functions for
     277              :     // that. TODO: If we get a request for an old LSN, such that the
     278              :     // versions have already been garbage collected away, we should
     279              :     // throw an error, but we don't track that currently.
     280              :     //
     281              :     // last_record_lsn.load().last points to the end of last processed WAL record.
     282              :     //
     283              :     // We also remember the starting point of the previous record in
     284              :     // 'last_record_lsn.load().prev'. It's used to set the xl_prev pointer of the
     285              :     // first WAL record when the node is started up. But here, we just
     286              :     // keep track of it.
     287              :     last_record_lsn: SeqWait<RecordLsn, Lsn>,
     288              : 
     289              :     // All WAL records have been processed and stored durably on files on
     290              :     // local disk, up to this LSN. On crash and restart, we need to re-process
     291              :     // the WAL starting from this point.
     292              :     //
     293              :     // Some later WAL records might have been processed and also flushed to disk
     294              :     // already, so don't be surprised to see some, but there's no guarantee on
     295              :     // them yet.
     296              :     disk_consistent_lsn: AtomicLsn,
     297              : 
     298              :     // Parent timeline that this timeline was branched from, and the LSN
     299              :     // of the branch point.
     300              :     ancestor_timeline: Option<Arc<Timeline>>,
     301              :     ancestor_lsn: Lsn,
     302              : 
     303              :     pub(super) metrics: TimelineMetrics,
     304              : 
     305              :     // `Timeline` doesn't write these metrics itself, but it manages the lifetime.  Code
     306              :     // in `crate::page_service` writes these metrics.
     307              :     pub(crate) query_metrics: crate::metrics::SmgrQueryTimePerTimeline,
     308              : 
     309              :     directory_metrics: [AtomicU64; DirectoryKind::KINDS_NUM],
     310              : 
     311              :     /// Ensures layers aren't frozen by checkpointer between
     312              :     /// [`Timeline::get_layer_for_write`] and layer reads.
     313              :     /// Locked automatically by [`TimelineWriter`] and checkpointer.
     314              :     /// Must always be acquired before the layer map/individual layer lock
     315              :     /// to avoid deadlock.
     316              :     ///
     317              :     /// The state is cleared upon freezing.
     318              :     write_lock: tokio::sync::Mutex<Option<TimelineWriterState>>,
     319              : 
     320              :     /// Used to avoid multiple `flush_loop` tasks running
     321              :     pub(super) flush_loop_state: Mutex<FlushLoopState>,
     322              : 
     323              :     /// layer_flush_start_tx can be used to wake up the layer-flushing task.
     324              :     /// - The u64 value is a counter, incremented every time a new flush cycle is requested.
     325              :     ///   The flush cycle counter is sent back on the layer_flush_done channel when
     326              :     ///   the flush finishes. You can use that to wait for the flush to finish.
     327              :     /// - The LSN is updated to max() of its current value and the latest disk_consistent_lsn
     328              :     ///   read by whoever sends an update
     329              :     layer_flush_start_tx: tokio::sync::watch::Sender<(u64, Lsn)>,
     330              :     /// to be notified when layer flushing has finished, subscribe to the layer_flush_done channel
     331              :     layer_flush_done_tx: tokio::sync::watch::Sender<(u64, Result<(), FlushLayerError>)>,
     332              : 
     333              :     // Needed to ensure that we can't create a branch at a point that was already garbage collected
     334              :     pub latest_gc_cutoff_lsn: Rcu<Lsn>,
     335              : 
     336              :     // List of child timelines and their branch points. This is needed to avoid
     337              :     // garbage collecting data that is still needed by the child timelines.
     338              :     pub(crate) gc_info: std::sync::RwLock<GcInfo>,
     339              : 
     340              :     // It may change across major versions so for simplicity
     341              :     // keep it after running initdb for a timeline.
     342              :     // It is needed in checks when we want to error on some operations
     343              :     // when they are requested for pre-initdb lsn.
     344              :     // It can be unified with latest_gc_cutoff_lsn under some "first_valid_lsn",
     345              :     // though let's keep them both for better error visibility.
     346              :     pub initdb_lsn: Lsn,
     347              : 
     348              :     /// When did we last calculate the partitioning? Make it pub to test cases.
     349              :     pub(super) partitioning: tokio::sync::Mutex<((KeyPartitioning, SparseKeyPartitioning), Lsn)>,
     350              : 
     351              :     /// Configuration: how often should the partitioning be recalculated.
     352              :     repartition_threshold: u64,
     353              : 
     354              :     last_image_layer_creation_check_at: AtomicLsn,
     355              :     last_image_layer_creation_check_instant: std::sync::Mutex<Option<Instant>>,
     356              : 
     357              :     /// Current logical size of the "datadir", at the last LSN.
     358              :     current_logical_size: LogicalSize,
     359              : 
     360              :     /// Information about the last processed message by the WAL receiver,
     361              :     /// or None if WAL receiver has not received anything for this timeline
     362              :     /// yet.
     363              :     pub last_received_wal: Mutex<Option<WalReceiverInfo>>,
     364              :     pub walreceiver: Mutex<Option<WalReceiver>>,
     365              : 
     366              :     /// Relation size cache
     367              :     pub(crate) rel_size_cache: RwLock<RelSizeCache>,
     368              : 
     369              :     download_all_remote_layers_task_info: RwLock<Option<DownloadRemoteLayersTaskInfo>>,
     370              : 
     371              :     state: watch::Sender<TimelineState>,
     372              : 
     373              :     /// Prevent two tasks from deleting the timeline at the same time. If held, the
     374              :     /// timeline is being deleted. If 'true', the timeline has already been deleted.
     375              :     pub delete_progress: Arc<tokio::sync::Mutex<DeleteTimelineFlow>>,
     376              : 
     377              :     eviction_task_timeline_state: tokio::sync::Mutex<EvictionTaskTimelineState>,
     378              : 
     379              :     /// Load or creation time information about the disk_consistent_lsn and when the loading
     380              :     /// happened. Used for consumption metrics.
     381              :     pub(crate) loaded_at: (Lsn, SystemTime),
     382              : 
     383              :     /// Gate to prevent shutdown completing while I/O is still happening to this timeline's data
     384              :     pub(crate) gate: Gate,
     385              : 
     386              :     /// Cancellation token scoped to this timeline: anything doing long-running work relating
     387              :     /// to the timeline should drop out when this token fires.
     388              :     pub(crate) cancel: CancellationToken,
     389              : 
     390              :     /// Make sure we only have one running compaction at a time in tests.
     391              :     ///
     392              :     /// Must only be taken in two places:
     393              :     /// - [`Timeline::compact`] (this file)
     394              :     /// - [`delete::delete_local_timeline_directory`]
     395              :     ///
     396              :     /// Timeline deletion will acquire both compaction and gc locks in whatever order.
     397              :     compaction_lock: tokio::sync::Mutex<()>,
     398              : 
     399              :     /// Make sure we only have one running gc at a time.
     400              :     ///
     401              :     /// Must only be taken in two places:
     402              :     /// - [`Timeline::gc`] (this file)
     403              :     /// - [`delete::delete_local_timeline_directory`]
     404              :     ///
     405              :     /// Timeline deletion will acquire both compaction and gc locks in whatever order.
     406              :     gc_lock: tokio::sync::Mutex<()>,
     407              : 
     408              :     /// Cloned from [`super::Tenant::timeline_get_throttle`] on construction.
     409              :     timeline_get_throttle: Arc<
     410              :         crate::tenant::throttle::Throttle<&'static crate::metrics::tenant_throttling::TimelineGet>,
     411              :     >,
     412              : 
     413              :     /// Keep aux directory cache to avoid it's reconstruction on each update
     414              :     pub(crate) aux_files: tokio::sync::Mutex<AuxFilesState>,
     415              : 
     416              :     /// Size estimator for aux file v2
     417              :     pub(crate) aux_file_size_estimator: AuxFileSizeEstimator,
     418              : 
     419              :     /// Indicate whether aux file v2 storage is enabled.
     420              :     pub(crate) last_aux_file_policy: AtomicAuxFilePolicy,
     421              : 
     422              :     /// Some test cases directly place keys into the timeline without actually modifying the directory
     423              :     /// keys (i.e., DB_DIR). The test cases creating such keys will put the keyspaces here, so that
     424              :     /// these keys won't get garbage-collected during compaction/GC. This field only modifies the dense
     425              :     /// keyspace return value of `collect_keyspace`. For sparse keyspaces, use AUX keys for testing, and
     426              :     /// in the future, add `extra_test_sparse_keyspace` if necessary.
     427              :     #[cfg(test)]
     428              :     pub(crate) extra_test_dense_keyspace: ArcSwap<KeySpace>,
     429              : 
     430              :     pub(crate) l0_flush_global_state: L0FlushGlobalState,
     431              : 
     432              :     pub(crate) handles: handle::PerTimelineState<crate::page_service::TenantManagerTypes>,
     433              : }
     434              : 
     435              : pub struct WalReceiverInfo {
     436              :     pub wal_source_connconf: PgConnectionConfig,
     437              :     pub last_received_msg_lsn: Lsn,
     438              :     pub last_received_msg_ts: u128,
     439              : }
     440              : 
     441              : /// Information about how much history needs to be retained, needed by
     442              : /// Garbage Collection.
     443              : #[derive(Default)]
     444              : pub(crate) struct GcInfo {
     445              :     /// Specific LSNs that are needed.
     446              :     ///
     447              :     /// Currently, this includes all points where child branches have
     448              :     /// been forked off from. In the future, could also include
     449              :     /// explicit user-defined snapshot points.
     450              :     pub(crate) retain_lsns: Vec<(Lsn, TimelineId)>,
     451              : 
     452              :     /// The cutoff coordinates, which are combined by selecting the minimum.
     453              :     pub(crate) cutoffs: GcCutoffs,
     454              : 
     455              :     /// Leases granted to particular LSNs.
     456              :     pub(crate) leases: BTreeMap<Lsn, LsnLease>,
     457              : 
     458              :     /// Whether our branch point is within our ancestor's PITR interval (for cost estimation)
     459              :     pub(crate) within_ancestor_pitr: bool,
     460              : }
     461              : 
     462              : impl GcInfo {
     463          678 :     pub(crate) fn min_cutoff(&self) -> Lsn {
     464          678 :         self.cutoffs.select_min()
     465          678 :     }
     466              : 
     467          684 :     pub(super) fn insert_child(&mut self, child_id: TimelineId, child_lsn: Lsn) {
     468          684 :         self.retain_lsns.push((child_lsn, child_id));
     469          684 :         self.retain_lsns.sort_by_key(|i| i.0);
     470          684 :     }
     471              : 
     472            6 :     pub(super) fn remove_child(&mut self, child_id: TimelineId) {
     473            6 :         self.retain_lsns.retain(|i| i.1 != child_id);
     474            6 :     }
     475              : }
     476              : 
     477              : /// The `GcInfo` component describing which Lsns need to be retained.  Functionally, this
     478              : /// is a single number (the oldest LSN which we must retain), but it internally distinguishes
     479              : /// between time-based and space-based retention for observability and consumption metrics purposes.
     480              : #[derive(Debug, Clone)]
     481              : pub(crate) struct GcCutoffs {
     482              :     /// Calculated from the [`TenantConf::gc_horizon`], this LSN indicates how much
     483              :     /// history we must keep to retain a specified number of bytes of WAL.
     484              :     pub(crate) space: Lsn,
     485              : 
     486              :     /// Calculated from [`TenantConf::pitr_interval`], this LSN indicates how much
     487              :     /// history we must keep to enable reading back at least the PITR interval duration.
     488              :     pub(crate) time: Lsn,
     489              : }
     490              : 
     491              : impl Default for GcCutoffs {
     492         1236 :     fn default() -> Self {
     493         1236 :         Self {
     494         1236 :             space: Lsn::INVALID,
     495         1236 :             time: Lsn::INVALID,
     496         1236 :         }
     497         1236 :     }
     498              : }
     499              : 
     500              : impl GcCutoffs {
     501          756 :     fn select_min(&self) -> Lsn {
     502          756 :         std::cmp::min(self.space, self.time)
     503          756 :     }
     504              : }
     505              : 
     506              : pub(crate) struct TimelineVisitOutcome {
     507              :     completed_keyspace: KeySpace,
     508              :     image_covered_keyspace: KeySpace,
     509              : }
     510              : 
     511              : /// An error happened in a get() operation.
     512            6 : #[derive(thiserror::Error, Debug)]
     513              : pub(crate) enum PageReconstructError {
     514              :     #[error(transparent)]
     515              :     Other(anyhow::Error),
     516              : 
     517              :     #[error("Ancestor LSN wait error: {0}")]
     518              :     AncestorLsnTimeout(WaitLsnError),
     519              : 
     520              :     #[error("timeline shutting down")]
     521              :     Cancelled,
     522              : 
     523              :     /// An error happened replaying WAL records
     524              :     #[error(transparent)]
     525              :     WalRedo(anyhow::Error),
     526              : 
     527              :     #[error("{0}")]
     528              :     MissingKey(MissingKeyError),
     529              : }
     530              : 
     531              : impl From<anyhow::Error> for PageReconstructError {
     532            0 :     fn from(value: anyhow::Error) -> Self {
     533            0 :         // with walingest.rs many PageReconstructError are wrapped in as anyhow::Error
     534            0 :         match value.downcast::<PageReconstructError>() {
     535            0 :             Ok(pre) => pre,
     536            0 :             Err(other) => PageReconstructError::Other(other),
     537              :         }
     538            0 :     }
     539              : }
     540              : 
     541              : impl From<utils::bin_ser::DeserializeError> for PageReconstructError {
     542            0 :     fn from(value: utils::bin_ser::DeserializeError) -> Self {
     543            0 :         PageReconstructError::Other(anyhow::Error::new(value).context("deserialization failure"))
     544            0 :     }
     545              : }
     546              : 
     547              : impl From<layer_manager::Shutdown> for PageReconstructError {
     548            0 :     fn from(_: layer_manager::Shutdown) -> Self {
     549            0 :         PageReconstructError::Cancelled
     550            0 :     }
     551              : }
     552              : 
     553              : impl GetVectoredError {
     554              :     #[cfg(test)]
     555           18 :     pub(crate) fn is_missing_key_error(&self) -> bool {
     556           18 :         matches!(self, Self::MissingKey(_))
     557           18 :     }
     558              : }
     559              : 
     560              : impl From<layer_manager::Shutdown> for GetVectoredError {
     561            0 :     fn from(_: layer_manager::Shutdown) -> Self {
     562            0 :         GetVectoredError::Cancelled
     563            0 :     }
     564              : }
     565              : 
     566              : #[derive(thiserror::Error)]
     567              : pub struct MissingKeyError {
     568              :     key: Key,
     569              :     shard: ShardNumber,
     570              :     cont_lsn: Lsn,
     571              :     request_lsn: Lsn,
     572              :     ancestor_lsn: Option<Lsn>,
     573              :     backtrace: Option<std::backtrace::Backtrace>,
     574              : }
     575              : 
     576              : impl std::fmt::Debug for MissingKeyError {
     577            0 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
     578            0 :         write!(f, "{}", self)
     579            0 :     }
     580              : }
     581              : 
     582              : impl std::fmt::Display for MissingKeyError {
     583            0 :     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
     584            0 :         write!(
     585            0 :             f,
     586            0 :             "could not find data for key {} (shard {:?}) at LSN {}, request LSN {}",
     587            0 :             self.key, self.shard, self.cont_lsn, self.request_lsn
     588            0 :         )?;
     589            0 :         if let Some(ref ancestor_lsn) = self.ancestor_lsn {
     590            0 :             write!(f, ", ancestor {}", ancestor_lsn)?;
     591            0 :         }
     592              : 
     593            0 :         if let Some(ref backtrace) = self.backtrace {
     594            0 :             write!(f, "\n{}", backtrace)?;
     595            0 :         }
     596              : 
     597            0 :         Ok(())
     598            0 :     }
     599              : }
     600              : 
     601              : impl PageReconstructError {
     602              :     /// Returns true if this error indicates a tenant/timeline shutdown alike situation
     603            0 :     pub(crate) fn is_stopping(&self) -> bool {
     604              :         use PageReconstructError::*;
     605            0 :         match self {
     606            0 :             Cancelled => true,
     607            0 :             Other(_) | AncestorLsnTimeout(_) | WalRedo(_) | MissingKey(_) => false,
     608              :         }
     609            0 :     }
     610              : }
     611              : 
     612            0 : #[derive(thiserror::Error, Debug)]
     613              : pub(crate) enum CreateImageLayersError {
     614              :     #[error("timeline shutting down")]
     615              :     Cancelled,
     616              : 
     617              :     #[error("read failed")]
     618              :     GetVectoredError(#[source] GetVectoredError),
     619              : 
     620              :     #[error("reconstruction failed")]
     621              :     PageReconstructError(#[source] PageReconstructError),
     622              : 
     623              :     #[error(transparent)]
     624              :     Other(#[from] anyhow::Error),
     625              : }
     626              : 
     627              : impl From<layer_manager::Shutdown> for CreateImageLayersError {
     628            0 :     fn from(_: layer_manager::Shutdown) -> Self {
     629            0 :         CreateImageLayersError::Cancelled
     630            0 :     }
     631              : }
     632              : 
     633            0 : #[derive(thiserror::Error, Debug, Clone)]
     634              : pub(crate) enum FlushLayerError {
     635              :     /// Timeline cancellation token was cancelled
     636              :     #[error("timeline shutting down")]
     637              :     Cancelled,
     638              : 
     639              :     /// We tried to flush a layer while the Timeline is in an unexpected state
     640              :     #[error("cannot flush frozen layers when flush_loop is not running, state is {0:?}")]
     641              :     NotRunning(FlushLoopState),
     642              : 
     643              :     // Arc<> the following non-clonable error types: we must be Clone-able because the flush error is propagated from the flush
     644              :     // loop via a watch channel, where we can only borrow it.
     645              :     #[error("create image layers (shared)")]
     646              :     CreateImageLayersError(Arc<CreateImageLayersError>),
     647              : 
     648              :     #[error("other (shared)")]
     649              :     Other(#[from] Arc<anyhow::Error>),
     650              : }
     651              : 
     652              : impl FlushLayerError {
     653              :     // When crossing from generic anyhow errors to this error type, we explicitly check
     654              :     // for timeline cancellation to avoid logging inoffensive shutdown errors as warn/err.
     655            0 :     fn from_anyhow(timeline: &Timeline, err: anyhow::Error) -> Self {
     656            0 :         let cancelled = timeline.cancel.is_cancelled()
     657              :             // The upload queue might have been shut down before the official cancellation of the timeline.
     658            0 :             || err
     659            0 :                 .downcast_ref::<NotInitialized>()
     660            0 :                 .map(NotInitialized::is_stopping)
     661            0 :                 .unwrap_or_default();
     662            0 :         if cancelled {
     663            0 :             Self::Cancelled
     664              :         } else {
     665            0 :             Self::Other(Arc::new(err))
     666              :         }
     667            0 :     }
     668              : }
     669              : 
     670              : impl From<layer_manager::Shutdown> for FlushLayerError {
     671            0 :     fn from(_: layer_manager::Shutdown) -> Self {
     672            0 :         FlushLayerError::Cancelled
     673            0 :     }
     674              : }
     675              : 
     676            0 : #[derive(thiserror::Error, Debug)]
     677              : pub(crate) enum GetVectoredError {
     678              :     #[error("timeline shutting down")]
     679              :     Cancelled,
     680              : 
     681              :     #[error("requested too many keys: {0} > {}", Timeline::MAX_GET_VECTORED_KEYS)]
     682              :     Oversized(u64),
     683              : 
     684              :     #[error("requested at invalid LSN: {0}")]
     685              :     InvalidLsn(Lsn),
     686              : 
     687              :     #[error("requested key not found: {0}")]
     688              :     MissingKey(MissingKeyError),
     689              : 
     690              :     #[error("ancestry walk")]
     691              :     GetReadyAncestorError(#[source] GetReadyAncestorError),
     692              : 
     693              :     #[error(transparent)]
     694              :     Other(#[from] anyhow::Error),
     695              : }
     696              : 
     697              : impl From<GetReadyAncestorError> for GetVectoredError {
     698            6 :     fn from(value: GetReadyAncestorError) -> Self {
     699              :         use GetReadyAncestorError::*;
     700            6 :         match value {
     701            0 :             Cancelled => GetVectoredError::Cancelled,
     702              :             AncestorLsnTimeout(_) | BadState { .. } => {
     703            6 :                 GetVectoredError::GetReadyAncestorError(value)
     704              :             }
     705              :         }
     706            6 :     }
     707              : }
     708              : 
     709            6 : #[derive(thiserror::Error, Debug)]
     710              : pub(crate) enum GetReadyAncestorError {
     711              :     #[error("ancestor LSN wait error")]
     712              :     AncestorLsnTimeout(#[from] WaitLsnError),
     713              : 
     714              :     #[error("bad state on timeline {timeline_id}: {state:?}")]
     715              :     BadState {
     716              :         timeline_id: TimelineId,
     717              :         state: TimelineState,
     718              :     },
     719              : 
     720              :     #[error("cancelled")]
     721              :     Cancelled,
     722              : }
     723              : 
     724              : #[derive(Clone, Copy)]
     725              : pub enum LogicalSizeCalculationCause {
     726              :     Initial,
     727              :     ConsumptionMetricsSyntheticSize,
     728              :     EvictionTaskImitation,
     729              :     TenantSizeHandler,
     730              : }
     731              : 
     732              : pub enum GetLogicalSizePriority {
     733              :     User,
     734              :     Background,
     735              : }
     736              : 
     737            0 : #[derive(enumset::EnumSetType)]
     738              : pub(crate) enum CompactFlags {
     739              :     ForceRepartition,
     740              :     ForceImageLayerCreation,
     741              :     EnhancedGcBottomMostCompaction,
     742              :     DryRun,
     743              : }
     744              : 
     745              : impl std::fmt::Debug for Timeline {
     746            0 :     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
     747            0 :         write!(f, "Timeline<{}>", self.timeline_id)
     748            0 :     }
     749              : }
     750              : 
     751            0 : #[derive(thiserror::Error, Debug)]
     752              : pub(crate) enum WaitLsnError {
     753              :     // Called on a timeline which is shutting down
     754              :     #[error("Shutdown")]
     755              :     Shutdown,
     756              : 
     757              :     // Called on an timeline not in active state or shutting down
     758              :     #[error("Bad timeline state: {0:?}")]
     759              :     BadState(TimelineState),
     760              : 
     761              :     // Timeout expired while waiting for LSN to catch up with goal.
     762              :     #[error("{0}")]
     763              :     Timeout(String),
     764              : }
     765              : 
     766              : // The impls below achieve cancellation mapping for errors.
     767              : // Perhaps there's a way of achieving this with less cruft.
     768              : 
     769              : impl From<CreateImageLayersError> for CompactionError {
     770            0 :     fn from(e: CreateImageLayersError) -> Self {
     771            0 :         match e {
     772            0 :             CreateImageLayersError::Cancelled => CompactionError::ShuttingDown,
     773            0 :             CreateImageLayersError::Other(e) => {
     774            0 :                 CompactionError::Other(e.context("create image layers"))
     775              :             }
     776            0 :             _ => CompactionError::Other(e.into()),
     777              :         }
     778            0 :     }
     779              : }
     780              : 
     781              : impl From<CreateImageLayersError> for FlushLayerError {
     782            0 :     fn from(e: CreateImageLayersError) -> Self {
     783            0 :         match e {
     784            0 :             CreateImageLayersError::Cancelled => FlushLayerError::Cancelled,
     785            0 :             any => FlushLayerError::CreateImageLayersError(Arc::new(any)),
     786              :         }
     787            0 :     }
     788              : }
     789              : 
     790              : impl From<PageReconstructError> for CreateImageLayersError {
     791            0 :     fn from(e: PageReconstructError) -> Self {
     792            0 :         match e {
     793            0 :             PageReconstructError::Cancelled => CreateImageLayersError::Cancelled,
     794            0 :             _ => CreateImageLayersError::PageReconstructError(e),
     795              :         }
     796            0 :     }
     797              : }
     798              : 
     799              : impl From<GetVectoredError> for CreateImageLayersError {
     800            0 :     fn from(e: GetVectoredError) -> Self {
     801            0 :         match e {
     802            0 :             GetVectoredError::Cancelled => CreateImageLayersError::Cancelled,
     803            0 :             _ => CreateImageLayersError::GetVectoredError(e),
     804              :         }
     805            0 :     }
     806              : }
     807              : 
     808              : impl From<GetVectoredError> for PageReconstructError {
     809           18 :     fn from(e: GetVectoredError) -> Self {
     810           18 :         match e {
     811            0 :             GetVectoredError::Cancelled => PageReconstructError::Cancelled,
     812            0 :             GetVectoredError::InvalidLsn(_) => PageReconstructError::Other(anyhow!("Invalid LSN")),
     813            0 :             err @ GetVectoredError::Oversized(_) => PageReconstructError::Other(err.into()),
     814           12 :             GetVectoredError::MissingKey(err) => PageReconstructError::MissingKey(err),
     815            6 :             GetVectoredError::GetReadyAncestorError(err) => PageReconstructError::from(err),
     816            0 :             GetVectoredError::Other(err) => PageReconstructError::Other(err),
     817              :         }
     818           18 :     }
     819              : }
     820              : 
     821              : impl From<GetReadyAncestorError> for PageReconstructError {
     822            6 :     fn from(e: GetReadyAncestorError) -> Self {
     823              :         use GetReadyAncestorError::*;
     824            6 :         match e {
     825            0 :             AncestorLsnTimeout(wait_err) => PageReconstructError::AncestorLsnTimeout(wait_err),
     826            6 :             bad_state @ BadState { .. } => PageReconstructError::Other(anyhow::anyhow!(bad_state)),
     827            0 :             Cancelled => PageReconstructError::Cancelled,
     828              :         }
     829            6 :     }
     830              : }
     831              : 
     832              : pub(crate) enum WaitLsnWaiter<'a> {
     833              :     Timeline(&'a Timeline),
     834              :     Tenant,
     835              :     PageService,
     836              : }
     837              : 
     838              : /// Argument to [`Timeline::shutdown`].
     839              : #[derive(Debug, Clone, Copy)]
     840              : pub(crate) enum ShutdownMode {
     841              :     /// Graceful shutdown, may do a lot of I/O as we flush any open layers to disk and then
     842              :     /// also to remote storage.  This method can easily take multiple seconds for a busy timeline.
     843              :     ///
     844              :     /// While we are flushing, we continue to accept read I/O for LSNs ingested before
     845              :     /// the call to [`Timeline::shutdown`].
     846              :     FreezeAndFlush,
     847              :     /// Shut down immediately, without waiting for any open layers to flush.
     848              :     Hard,
     849              : }
     850              : 
     851              : struct ImageLayerCreationOutcome {
     852              :     image: Option<ResidentLayer>,
     853              :     next_start_key: Key,
     854              : }
     855              : 
     856              : /// Public interface functions
     857              : impl Timeline {
     858              :     /// Get the LSN where this branch was created
     859            6 :     pub(crate) fn get_ancestor_lsn(&self) -> Lsn {
     860            6 :         self.ancestor_lsn
     861            6 :     }
     862              : 
     863              :     /// Get the ancestor's timeline id
     864         2262 :     pub(crate) fn get_ancestor_timeline_id(&self) -> Option<TimelineId> {
     865         2262 :         self.ancestor_timeline
     866         2262 :             .as_ref()
     867         2262 :             .map(|ancestor| ancestor.timeline_id)
     868         2262 :     }
     869              : 
     870              :     /// Get the ancestor timeline
     871            0 :     pub(crate) fn ancestor_timeline(&self) -> Option<&Arc<Timeline>> {
     872            0 :         self.ancestor_timeline.as_ref()
     873            0 :     }
     874              : 
     875              :     /// Get the bytes written since the PITR cutoff on this branch, and
     876              :     /// whether this branch's ancestor_lsn is within its parent's PITR.
     877            0 :     pub(crate) fn get_pitr_history_stats(&self) -> (u64, bool) {
     878            0 :         let gc_info = self.gc_info.read().unwrap();
     879            0 :         let history = self
     880            0 :             .get_last_record_lsn()
     881            0 :             .checked_sub(gc_info.cutoffs.time)
     882            0 :             .unwrap_or(Lsn(0))
     883            0 :             .0;
     884            0 :         (history, gc_info.within_ancestor_pitr)
     885            0 :     }
     886              : 
     887              :     /// Lock and get timeline's GC cutoff
     888         2986 :     pub(crate) fn get_latest_gc_cutoff_lsn(&self) -> RcuReadGuard<Lsn> {
     889         2986 :         self.latest_gc_cutoff_lsn.read()
     890         2986 :     }
     891              : 
     892              :     /// Look up given page version.
     893              :     ///
     894              :     /// If a remote layer file is needed, it is downloaded as part of this
     895              :     /// call.
     896              :     ///
     897              :     /// This method enforces [`Self::timeline_get_throttle`] internally.
     898              :     ///
     899              :     /// NOTE: It is considered an error to 'get' a key that doesn't exist. The
     900              :     /// abstraction above this needs to store suitable metadata to track what
     901              :     /// data exists with what keys, in separate metadata entries. If a
     902              :     /// non-existent key is requested, we may incorrectly return a value from
     903              :     /// an ancestor branch, for example, or waste a lot of cycles chasing the
     904              :     /// non-existing key.
     905              :     ///
     906              :     /// # Cancel-Safety
     907              :     ///
     908              :     /// This method is cancellation-safe.
     909              :     #[inline(always)]
     910      1876068 :     pub(crate) async fn get(
     911      1876068 :         &self,
     912      1876068 :         key: Key,
     913      1876068 :         lsn: Lsn,
     914      1876068 :         ctx: &RequestContext,
     915      1876068 :     ) -> Result<Bytes, PageReconstructError> {
     916      1876068 :         if !lsn.is_valid() {
     917            0 :             return Err(PageReconstructError::Other(anyhow::anyhow!("Invalid LSN")));
     918      1876068 :         }
     919      1876068 : 
     920      1876068 :         // This check is debug-only because of the cost of hashing, and because it's a double-check: we
     921      1876068 :         // already checked the key against the shard_identity when looking up the Timeline from
     922      1876068 :         // page_service.
     923      1876068 :         debug_assert!(!self.shard_identity.is_key_disposable(&key));
     924              : 
     925      1876068 :         self.timeline_get_throttle.throttle(ctx, 1).await;
     926              : 
     927      1876068 :         let keyspace = KeySpace {
     928      1876068 :             ranges: vec![key..key.next()],
     929      1876068 :         };
     930      1876068 : 
     931      1876068 :         // Initialise the reconstruct state for the key with the cache
     932      1876068 :         // entry returned above.
     933      1876068 :         let mut reconstruct_state = ValuesReconstructState::new();
     934              : 
     935      1876068 :         let vectored_res = self
     936      1876068 :             .get_vectored_impl(keyspace.clone(), lsn, &mut reconstruct_state, ctx)
     937       547860 :             .await;
     938              : 
     939      1876068 :         let key_value = vectored_res?.pop_first();
     940      1876050 :         match key_value {
     941      1875120 :             Some((got_key, value)) => {
     942      1875120 :                 if got_key != key {
     943            0 :                     error!(
     944            0 :                         "Expected {}, but singular vectored get returned {}",
     945              :                         key, got_key
     946              :                     );
     947            0 :                     Err(PageReconstructError::Other(anyhow!(
     948            0 :                         "Singular vectored get returned wrong key"
     949            0 :                     )))
     950              :                 } else {
     951      1875120 :                     value
     952              :                 }
     953              :             }
     954          930 :             None => Err(PageReconstructError::MissingKey(MissingKeyError {
     955          930 :                 key,
     956          930 :                 shard: self.shard_identity.get_shard_number(&key),
     957          930 :                 cont_lsn: Lsn(0),
     958          930 :                 request_lsn: lsn,
     959          930 :                 ancestor_lsn: None,
     960          930 :                 backtrace: None,
     961          930 :             })),
     962              :         }
     963      1876068 :     }
     964              : 
     965              :     pub(crate) const MAX_GET_VECTORED_KEYS: u64 = 32;
     966              :     pub(crate) const VEC_GET_LAYERS_VISITED_WARN_THRESH: f64 = 512.0;
     967              : 
     968              :     /// Look up multiple page versions at a given LSN
     969              :     ///
     970              :     /// This naive implementation will be replaced with a more efficient one
     971              :     /// which actually vectorizes the read path.
     972         3354 :     pub(crate) async fn get_vectored(
     973         3354 :         &self,
     974         3354 :         keyspace: KeySpace,
     975         3354 :         lsn: Lsn,
     976         3354 :         ctx: &RequestContext,
     977         3354 :     ) -> Result<BTreeMap<Key, Result<Bytes, PageReconstructError>>, GetVectoredError> {
     978         3354 :         if !lsn.is_valid() {
     979            0 :             return Err(GetVectoredError::InvalidLsn(lsn));
     980         3354 :         }
     981         3354 : 
     982         3354 :         let key_count = keyspace.total_raw_size().try_into().unwrap();
     983         3354 :         if key_count > Timeline::MAX_GET_VECTORED_KEYS {
     984            0 :             return Err(GetVectoredError::Oversized(key_count));
     985         3354 :         }
     986              : 
     987         6708 :         for range in &keyspace.ranges {
     988         3354 :             let mut key = range.start;
     989         7272 :             while key != range.end {
     990         3918 :                 assert!(!self.shard_identity.is_key_disposable(&key));
     991         3918 :                 key = key.next();
     992              :             }
     993              :         }
     994              : 
     995         3354 :         trace!(
     996            0 :             "get vectored request for {:?}@{} from task kind {:?}",
     997            0 :             keyspace,
     998            0 :             lsn,
     999            0 :             ctx.task_kind(),
    1000              :         );
    1001              : 
    1002         3354 :         let start = crate::metrics::GET_VECTORED_LATENCY
    1003         3354 :             .for_task_kind(ctx.task_kind())
    1004         3354 :             .map(|metric| (metric, Instant::now()));
    1005              : 
    1006              :         // start counting after throttle so that throttle time
    1007              :         // is always less than observation time
    1008         3354 :         let throttled = self
    1009         3354 :             .timeline_get_throttle
    1010         3354 :             .throttle(ctx, key_count as usize)
    1011            0 :             .await;
    1012              : 
    1013         3354 :         let res = self
    1014         3354 :             .get_vectored_impl(
    1015         3354 :                 keyspace.clone(),
    1016         3354 :                 lsn,
    1017         3354 :                 &mut ValuesReconstructState::new(),
    1018         3354 :                 ctx,
    1019         3354 :             )
    1020          144 :             .await;
    1021              : 
    1022         3354 :         if let Some((metric, start)) = start {
    1023            0 :             let elapsed = start.elapsed();
    1024            0 :             let ex_throttled = if let Some(throttled) = throttled {
    1025            0 :                 elapsed.checked_sub(throttled)
    1026              :             } else {
    1027            0 :                 Some(elapsed)
    1028              :             };
    1029              : 
    1030            0 :             if let Some(ex_throttled) = ex_throttled {
    1031            0 :                 metric.observe(ex_throttled.as_secs_f64());
    1032            0 :             } else {
    1033            0 :                 use utils::rate_limit::RateLimit;
    1034            0 :                 static LOGGED: Lazy<Mutex<RateLimit>> =
    1035            0 :                     Lazy::new(|| Mutex::new(RateLimit::new(Duration::from_secs(10))));
    1036            0 :                 let mut rate_limit = LOGGED.lock().unwrap();
    1037            0 :                 rate_limit.call(|| {
    1038            0 :                     warn!("error deducting time spent throttled; this message is logged at a global rate limit");
    1039            0 :                 });
    1040            0 :             }
    1041         3354 :         }
    1042              : 
    1043         3354 :         res
    1044         3354 :     }
    1045              : 
    1046              :     /// Scan the keyspace and return all existing key-values in the keyspace. This currently uses vectored
    1047              :     /// get underlying. Normal vectored get would throw an error when a key in the keyspace is not found
    1048              :     /// during the search, but for the scan interface, it returns all existing key-value pairs, and does
    1049              :     /// not expect each single key in the key space will be found. The semantics is closer to the RocksDB
    1050              :     /// scan iterator interface. We could optimize this interface later to avoid some checks in the vectored
    1051              :     /// get path to maintain and split the probing and to-be-probe keyspace. We also need to ensure that
    1052              :     /// the scan operation will not cause OOM in the future.
    1053           72 :     pub(crate) async fn scan(
    1054           72 :         &self,
    1055           72 :         keyspace: KeySpace,
    1056           72 :         lsn: Lsn,
    1057           72 :         ctx: &RequestContext,
    1058           72 :     ) -> Result<BTreeMap<Key, Result<Bytes, PageReconstructError>>, GetVectoredError> {
    1059           72 :         if !lsn.is_valid() {
    1060            0 :             return Err(GetVectoredError::InvalidLsn(lsn));
    1061           72 :         }
    1062           72 : 
    1063           72 :         trace!(
    1064            0 :             "key-value scan request for {:?}@{} from task kind {:?}",
    1065            0 :             keyspace,
    1066            0 :             lsn,
    1067            0 :             ctx.task_kind()
    1068              :         );
    1069              : 
    1070              :         // We should generalize this into Keyspace::contains in the future.
    1071          144 :         for range in &keyspace.ranges {
    1072           72 :             if range.start.field1 < METADATA_KEY_BEGIN_PREFIX
    1073           72 :                 || range.end.field1 > METADATA_KEY_END_PREFIX
    1074              :             {
    1075            0 :                 return Err(GetVectoredError::Other(anyhow::anyhow!(
    1076            0 :                     "only metadata keyspace can be scanned"
    1077            0 :                 )));
    1078           72 :             }
    1079              :         }
    1080              : 
    1081           72 :         let start = crate::metrics::SCAN_LATENCY
    1082           72 :             .for_task_kind(ctx.task_kind())
    1083           72 :             .map(ScanLatencyOngoingRecording::start_recording);
    1084              : 
    1085              :         // start counting after throttle so that throttle time
    1086              :         // is always less than observation time
    1087           72 :         let throttled = self
    1088           72 :             .timeline_get_throttle
    1089           72 :             // assume scan = 1 quota for now until we find a better way to process this
    1090           72 :             .throttle(ctx, 1)
    1091            0 :             .await;
    1092              : 
    1093           72 :         let vectored_res = self
    1094           72 :             .get_vectored_impl(
    1095           72 :                 keyspace.clone(),
    1096           72 :                 lsn,
    1097           72 :                 &mut ValuesReconstructState::default(),
    1098           72 :                 ctx,
    1099           72 :             )
    1100            0 :             .await;
    1101              : 
    1102           72 :         if let Some(recording) = start {
    1103            0 :             recording.observe(throttled);
    1104           72 :         }
    1105              : 
    1106           72 :         vectored_res
    1107           72 :     }
    1108              : 
    1109      1880388 :     pub(super) async fn get_vectored_impl(
    1110      1880388 :         &self,
    1111      1880388 :         keyspace: KeySpace,
    1112      1880388 :         lsn: Lsn,
    1113      1880388 :         reconstruct_state: &mut ValuesReconstructState,
    1114      1880388 :         ctx: &RequestContext,
    1115      1880388 :     ) -> Result<BTreeMap<Key, Result<Bytes, PageReconstructError>>, GetVectoredError> {
    1116      1880388 :         let get_kind = if keyspace.total_raw_size() == 1 {
    1117      1879074 :             GetKind::Singular
    1118              :         } else {
    1119         1314 :             GetKind::Vectored
    1120              :         };
    1121              : 
    1122      1880388 :         let get_data_timer = crate::metrics::GET_RECONSTRUCT_DATA_TIME
    1123      1880388 :             .for_get_kind(get_kind)
    1124      1880388 :             .start_timer();
    1125      1880388 :         self.get_vectored_reconstruct_data(keyspace.clone(), lsn, reconstruct_state, ctx)
    1126       574741 :             .await?;
    1127      1880340 :         get_data_timer.stop_and_record();
    1128      1880340 : 
    1129      1880340 :         let reconstruct_timer = crate::metrics::RECONSTRUCT_TIME
    1130      1880340 :             .for_get_kind(get_kind)
    1131      1880340 :             .start_timer();
    1132      1880340 :         let mut results: BTreeMap<Key, Result<Bytes, PageReconstructError>> = BTreeMap::new();
    1133      1880340 :         let layers_visited = reconstruct_state.get_layers_visited();
    1134              : 
    1135      2000400 :         for (key, res) in std::mem::take(&mut reconstruct_state.keys) {
    1136      2000400 :             match res {
    1137            0 :                 Err(err) => {
    1138            0 :                     results.insert(key, Err(err));
    1139            0 :                 }
    1140      2000400 :                 Ok(state) => {
    1141      2000400 :                     let state = ValueReconstructState::from(state);
    1142              : 
    1143      2000400 :                     let reconstruct_res = self.reconstruct_value(key, lsn, state).await;
    1144      2000400 :                     results.insert(key, reconstruct_res);
    1145              :                 }
    1146              :             }
    1147              :         }
    1148      1880340 :         reconstruct_timer.stop_and_record();
    1149      1880340 : 
    1150      1880340 :         // For aux file keys (v1 or v2) the vectored read path does not return an error
    1151      1880340 :         // when they're missing. Instead they are omitted from the resulting btree
    1152      1880340 :         // (this is a requirement, not a bug). Skip updating the metric in these cases
    1153      1880340 :         // to avoid infinite results.
    1154      1880340 :         if !results.is_empty() {
    1155      1878834 :             let avg = layers_visited as f64 / results.len() as f64;
    1156      1878834 :             if avg >= Self::VEC_GET_LAYERS_VISITED_WARN_THRESH {
    1157            0 :                 use utils::rate_limit::RateLimit;
    1158            0 :                 static LOGGED: Lazy<Mutex<RateLimit>> =
    1159            0 :                     Lazy::new(|| Mutex::new(RateLimit::new(Duration::from_secs(60))));
    1160            0 :                 let mut rate_limit = LOGGED.lock().unwrap();
    1161            0 :                 rate_limit.call(|| {
    1162            0 :                     tracing::info!(
    1163            0 :                       shard_id = %self.tenant_shard_id.shard_slug(),
    1164            0 :                       lsn = %lsn,
    1165            0 :                       "Vectored read for {} visited {} layers on average per key and {} in total. {}/{} pages were returned",
    1166            0 :                       keyspace, avg, layers_visited, results.len(), keyspace.total_raw_size());
    1167            0 :                 });
    1168      1878834 :             }
    1169              : 
    1170              :             // Note that this is an approximation. Tracking the exact number of layers visited
    1171              :             // per key requires virtually unbounded memory usage and is inefficient
    1172              :             // (i.e. segment tree tracking each range queried from a layer)
    1173      1878834 :             crate::metrics::VEC_READ_NUM_LAYERS_VISITED.observe(avg);
    1174         1506 :         }
    1175              : 
    1176      1880340 :         Ok(results)
    1177      1880388 :     }
    1178              : 
    1179              :     /// Get last or prev record separately. Same as get_last_record_rlsn().last/prev.
    1180       831174 :     pub(crate) fn get_last_record_lsn(&self) -> Lsn {
    1181       831174 :         self.last_record_lsn.load().last
    1182       831174 :     }
    1183              : 
    1184            0 :     pub(crate) fn get_prev_record_lsn(&self) -> Lsn {
    1185            0 :         self.last_record_lsn.load().prev
    1186            0 :     }
    1187              : 
    1188              :     /// Atomically get both last and prev.
    1189          678 :     pub(crate) fn get_last_record_rlsn(&self) -> RecordLsn {
    1190          678 :         self.last_record_lsn.load()
    1191          678 :     }
    1192              : 
    1193              :     /// Subscribe to callers of wait_lsn(). The value of the channel is None if there are no
    1194              :     /// wait_lsn() calls in progress, and Some(Lsn) if there is an active waiter for wait_lsn().
    1195            0 :     pub(crate) fn subscribe_for_wait_lsn_updates(&self) -> watch::Receiver<Option<Lsn>> {
    1196            0 :         self.last_record_lsn.status_receiver()
    1197            0 :     }
    1198              : 
    1199         3472 :     pub(crate) fn get_disk_consistent_lsn(&self) -> Lsn {
    1200         3472 :         self.disk_consistent_lsn.load()
    1201         3472 :     }
    1202              : 
    1203              :     /// remote_consistent_lsn from the perspective of the tenant's current generation,
    1204              :     /// not validated with control plane yet.
    1205              :     /// See [`Self::get_remote_consistent_lsn_visible`].
    1206            0 :     pub(crate) fn get_remote_consistent_lsn_projected(&self) -> Option<Lsn> {
    1207            0 :         self.remote_client.remote_consistent_lsn_projected()
    1208            0 :     }
    1209              : 
    1210              :     /// remote_consistent_lsn which the tenant is guaranteed not to go backward from,
    1211              :     /// i.e. a value of remote_consistent_lsn_projected which has undergone
    1212              :     /// generation validation in the deletion queue.
    1213            0 :     pub(crate) fn get_remote_consistent_lsn_visible(&self) -> Option<Lsn> {
    1214            0 :         self.remote_client.remote_consistent_lsn_visible()
    1215            0 :     }
    1216              : 
    1217              :     /// The sum of the file size of all historic layers in the layer map.
    1218              :     /// This method makes no distinction between local and remote layers.
    1219              :     /// Hence, the result **does not represent local filesystem usage**.
    1220            0 :     pub(crate) async fn layer_size_sum(&self) -> u64 {
    1221            0 :         let guard = self.layers.read().await;
    1222            0 :         guard.layer_size_sum()
    1223            0 :     }
    1224              : 
    1225            0 :     pub(crate) fn resident_physical_size(&self) -> u64 {
    1226            0 :         self.metrics.resident_physical_size_get()
    1227            0 :     }
    1228              : 
    1229            0 :     pub(crate) fn get_directory_metrics(&self) -> [u64; DirectoryKind::KINDS_NUM] {
    1230            0 :         array::from_fn(|idx| self.directory_metrics[idx].load(AtomicOrdering::Relaxed))
    1231            0 :     }
    1232              : 
    1233              :     ///
    1234              :     /// Wait until WAL has been received and processed up to this LSN.
    1235              :     ///
    1236              :     /// You should call this before any of the other get_* or list_* functions. Calling
    1237              :     /// those functions with an LSN that has been processed yet is an error.
    1238              :     ///
    1239       678418 :     pub(crate) async fn wait_lsn(
    1240       678418 :         &self,
    1241       678418 :         lsn: Lsn,
    1242       678418 :         who_is_waiting: WaitLsnWaiter<'_>,
    1243       678418 :         ctx: &RequestContext, /* Prepare for use by cancellation */
    1244       678418 :     ) -> Result<(), WaitLsnError> {
    1245       678418 :         let state = self.current_state();
    1246       678418 :         if self.cancel.is_cancelled() || matches!(state, TimelineState::Stopping) {
    1247            0 :             return Err(WaitLsnError::Shutdown);
    1248       678418 :         } else if !matches!(state, TimelineState::Active) {
    1249            0 :             return Err(WaitLsnError::BadState(state));
    1250       678418 :         }
    1251       678418 : 
    1252       678418 :         if cfg!(debug_assertions) {
    1253       678418 :             match ctx.task_kind() {
    1254              :                 TaskKind::WalReceiverManager
    1255              :                 | TaskKind::WalReceiverConnectionHandler
    1256              :                 | TaskKind::WalReceiverConnectionPoller => {
    1257            0 :                     let is_myself = match who_is_waiting {
    1258            0 :                         WaitLsnWaiter::Timeline(waiter) => Weak::ptr_eq(&waiter.myself, &self.myself),
    1259            0 :                         WaitLsnWaiter::Tenant | WaitLsnWaiter::PageService => unreachable!("tenant or page_service context are not expected to have task kind {:?}", ctx.task_kind()),
    1260              :                     };
    1261            0 :                     if is_myself {
    1262            0 :                         if let Err(current) = self.last_record_lsn.would_wait_for(lsn) {
    1263              :                             // walingest is the only one that can advance last_record_lsn; it should make sure to never reach here
    1264            0 :                             panic!("this timeline's walingest task is calling wait_lsn({lsn}) but we only have last_record_lsn={current}; would deadlock");
    1265            0 :                         }
    1266            0 :                     } else {
    1267            0 :                         // if another  timeline's  is waiting for us, there's no deadlock risk because
    1268            0 :                         // our walreceiver task can make progress independent of theirs
    1269            0 :                     }
    1270              :                 }
    1271       678418 :                 _ => {}
    1272              :             }
    1273            0 :         }
    1274              : 
    1275       678418 :         let _timer = crate::metrics::WAIT_LSN_TIME.start_timer();
    1276       678418 : 
    1277       678418 :         match self
    1278       678418 :             .last_record_lsn
    1279       678418 :             .wait_for_timeout(lsn, self.conf.wait_lsn_timeout)
    1280            0 :             .await
    1281              :         {
    1282       678418 :             Ok(()) => Ok(()),
    1283            0 :             Err(e) => {
    1284              :                 use utils::seqwait::SeqWaitError::*;
    1285            0 :                 match e {
    1286            0 :                     Shutdown => Err(WaitLsnError::Shutdown),
    1287              :                     Timeout => {
    1288              :                         // don't count the time spent waiting for lock below, and also in walreceiver.status(), towards the wait_lsn_time_histo
    1289            0 :                         drop(_timer);
    1290            0 :                         let walreceiver_status = self.walreceiver_status();
    1291            0 :                         Err(WaitLsnError::Timeout(format!(
    1292            0 :                         "Timed out while waiting for WAL record at LSN {} to arrive, last_record_lsn {} disk consistent LSN={}, WalReceiver status: {}",
    1293            0 :                         lsn,
    1294            0 :                         self.get_last_record_lsn(),
    1295            0 :                         self.get_disk_consistent_lsn(),
    1296            0 :                         walreceiver_status,
    1297            0 :                     )))
    1298              :                     }
    1299              :                 }
    1300              :             }
    1301              :         }
    1302       678418 :     }
    1303              : 
    1304            0 :     pub(crate) fn walreceiver_status(&self) -> String {
    1305            0 :         match &*self.walreceiver.lock().unwrap() {
    1306            0 :             None => "stopping or stopped".to_string(),
    1307            0 :             Some(walreceiver) => match walreceiver.status() {
    1308            0 :                 Some(status) => status.to_human_readable_string(),
    1309            0 :                 None => "Not active".to_string(),
    1310              :             },
    1311              :         }
    1312            0 :     }
    1313              : 
    1314              :     /// Check that it is valid to request operations with that lsn.
    1315          690 :     pub(crate) fn check_lsn_is_in_scope(
    1316          690 :         &self,
    1317          690 :         lsn: Lsn,
    1318          690 :         latest_gc_cutoff_lsn: &RcuReadGuard<Lsn>,
    1319          690 :     ) -> anyhow::Result<()> {
    1320          690 :         ensure!(
    1321          690 :             lsn >= **latest_gc_cutoff_lsn,
    1322           12 :             "LSN {} is earlier than latest GC cutoff {} (we might've already garbage collected needed data)",
    1323           12 :             lsn,
    1324           12 :             **latest_gc_cutoff_lsn,
    1325              :         );
    1326          678 :         Ok(())
    1327          690 :     }
    1328              : 
    1329              :     /// Obtains a temporary lease blocking garbage collection for the given LSN.
    1330              :     ///
    1331              :     /// This function will error if the requesting LSN is less than the `latest_gc_cutoff_lsn` and there is also
    1332              :     /// no existing lease to renew. If there is an existing lease in the map, the lease will be renewed only if
    1333              :     /// the request extends the lease. The returned lease is therefore the maximum between the existing lease and
    1334              :     /// the requesting lease.
    1335           42 :     pub(crate) fn make_lsn_lease(
    1336           42 :         &self,
    1337           42 :         lsn: Lsn,
    1338           42 :         length: Duration,
    1339           42 :         _ctx: &RequestContext,
    1340           42 :     ) -> anyhow::Result<LsnLease> {
    1341           36 :         let lease = {
    1342           42 :             let mut gc_info = self.gc_info.write().unwrap();
    1343           42 : 
    1344           42 :             let valid_until = SystemTime::now() + length;
    1345           42 : 
    1346           42 :             let entry = gc_info.leases.entry(lsn);
    1347              : 
    1348           36 :             let lease = {
    1349           42 :                 if let Entry::Occupied(mut occupied) = entry {
    1350           18 :                     let existing_lease = occupied.get_mut();
    1351           18 :                     if valid_until > existing_lease.valid_until {
    1352            6 :                         existing_lease.valid_until = valid_until;
    1353            6 :                         let dt: DateTime<Utc> = valid_until.into();
    1354            6 :                         info!("lease extended to {}", dt);
    1355              :                     } else {
    1356           12 :                         let dt: DateTime<Utc> = existing_lease.valid_until.into();
    1357           12 :                         info!("existing lease covers greater length, valid until {}", dt);
    1358              :                     }
    1359              : 
    1360           18 :                     existing_lease.clone()
    1361              :                 } else {
    1362              :                     // Reject already GC-ed LSN (lsn < latest_gc_cutoff)
    1363           24 :                     let latest_gc_cutoff_lsn = self.get_latest_gc_cutoff_lsn();
    1364           24 :                     if lsn < *latest_gc_cutoff_lsn {
    1365            6 :                         bail!("tried to request a page version that was garbage collected. requested at {} gc cutoff {}", lsn, *latest_gc_cutoff_lsn);
    1366           18 :                     }
    1367           18 : 
    1368           18 :                     let dt: DateTime<Utc> = valid_until.into();
    1369           18 :                     info!("lease created, valid until {}", dt);
    1370           18 :                     entry.or_insert(LsnLease { valid_until }).clone()
    1371              :                 }
    1372              :             };
    1373              : 
    1374           36 :             lease
    1375           36 :         };
    1376           36 : 
    1377           36 :         Ok(lease)
    1378           42 :     }
    1379              : 
    1380              :     /// Flush to disk all data that was written with the put_* functions
    1381         3264 :     #[instrument(skip(self), fields(tenant_id=%self.tenant_shard_id.tenant_id, shard_id=%self.tenant_shard_id.shard_slug(), timeline_id=%self.timeline_id))]
    1382              :     pub(crate) async fn freeze_and_flush(&self) -> Result<(), FlushLayerError> {
    1383              :         self.freeze_and_flush0().await
    1384              :     }
    1385              : 
    1386              :     // This exists to provide a non-span creating version of `freeze_and_flush` we can call without
    1387              :     // polluting the span hierarchy.
    1388         3264 :     pub(crate) async fn freeze_and_flush0(&self) -> Result<(), FlushLayerError> {
    1389         3264 :         let token = {
    1390              :             // Freeze the current open in-memory layer. It will be written to disk on next
    1391              :             // iteration.
    1392         3264 :             let mut g = self.write_lock.lock().await;
    1393              : 
    1394         3264 :             let to_lsn = self.get_last_record_lsn();
    1395         3264 :             self.freeze_inmem_layer_at(to_lsn, &mut g).await?
    1396              :         };
    1397         3264 :         self.wait_flush_completion(token).await
    1398         3264 :     }
    1399              : 
    1400              :     // Check if an open ephemeral layer should be closed: this provides
    1401              :     // background enforcement of checkpoint interval if there is no active WAL receiver, to avoid keeping
    1402              :     // an ephemeral layer open forever when idle.  It also freezes layers if the global limit on
    1403              :     // ephemeral layer bytes has been breached.
    1404            0 :     pub(super) async fn maybe_freeze_ephemeral_layer(&self) {
    1405            0 :         let Ok(mut write_guard) = self.write_lock.try_lock() else {
    1406              :             // If the write lock is held, there is an active wal receiver: rolling open layers
    1407              :             // is their responsibility while they hold this lock.
    1408            0 :             return;
    1409              :         };
    1410              : 
    1411              :         // FIXME: why not early exit? because before #7927 the state would had been cleared every
    1412              :         // time, and this was missed.
    1413              :         // if write_guard.is_none() { return; }
    1414              : 
    1415            0 :         let Ok(layers_guard) = self.layers.try_read() else {
    1416              :             // Don't block if the layer lock is busy
    1417            0 :             return;
    1418              :         };
    1419              : 
    1420            0 :         let Ok(lm) = layers_guard.layer_map() else {
    1421            0 :             return;
    1422              :         };
    1423              : 
    1424            0 :         let Some(open_layer) = &lm.open_layer else {
    1425              :             // If there is no open layer, we have no layer freezing to do.  However, we might need to generate
    1426              :             // some updates to disk_consistent_lsn and remote_consistent_lsn, in case we ingested some WAL regions
    1427              :             // that didn't result in writes to this shard.
    1428              : 
    1429              :             // Must not hold the layers lock while waiting for a flush.
    1430            0 :             drop(layers_guard);
    1431            0 : 
    1432            0 :             let last_record_lsn = self.get_last_record_lsn();
    1433            0 :             let disk_consistent_lsn = self.get_disk_consistent_lsn();
    1434            0 :             if last_record_lsn > disk_consistent_lsn {
    1435              :                 // We have no open layer, but disk_consistent_lsn is behind the last record: this indicates
    1436              :                 // we are a sharded tenant and have skipped some WAL
    1437            0 :                 let last_freeze_ts = *self.last_freeze_ts.read().unwrap();
    1438            0 :                 if last_freeze_ts.elapsed() >= self.get_checkpoint_timeout() {
    1439              :                     // Only do this if have been layer-less longer than get_checkpoint_timeout, so that a shard
    1440              :                     // without any data ingested (yet) doesn't write a remote index as soon as it
    1441              :                     // sees its LSN advance: we only do this if we've been layer-less
    1442              :                     // for some time.
    1443            0 :                     tracing::debug!(
    1444            0 :                         "Advancing disk_consistent_lsn past WAL ingest gap {} -> {}",
    1445              :                         disk_consistent_lsn,
    1446              :                         last_record_lsn
    1447              :                     );
    1448              : 
    1449              :                     // The flush loop will update remote consistent LSN as well as disk consistent LSN.
    1450              :                     // We know there is no open layer, so we can request freezing without actually
    1451              :                     // freezing anything. This is true even if we have dropped the layers_guard, we
    1452              :                     // still hold the write_guard.
    1453            0 :                     let _ = async {
    1454            0 :                         let token = self
    1455            0 :                             .freeze_inmem_layer_at(last_record_lsn, &mut write_guard)
    1456            0 :                             .await?;
    1457            0 :                         self.wait_flush_completion(token).await
    1458            0 :                     }
    1459            0 :                     .await;
    1460            0 :                 }
    1461            0 :             }
    1462              : 
    1463            0 :             return;
    1464              :         };
    1465              : 
    1466            0 :         let Some(current_size) = open_layer.try_len() else {
    1467              :             // Unexpected: since we hold the write guard, nobody else should be writing to this layer, so
    1468              :             // read lock to get size should always succeed.
    1469            0 :             tracing::warn!("Lock conflict while reading size of open layer");
    1470            0 :             return;
    1471              :         };
    1472              : 
    1473            0 :         let current_lsn = self.get_last_record_lsn();
    1474              : 
    1475            0 :         let checkpoint_distance_override = open_layer.tick().await;
    1476              : 
    1477            0 :         if let Some(size_override) = checkpoint_distance_override {
    1478            0 :             if current_size > size_override {
    1479              :                 // This is not harmful, but it only happens in relatively rare cases where
    1480              :                 // time-based checkpoints are not happening fast enough to keep the amount of
    1481              :                 // ephemeral data within configured limits.  It's a sign of stress on the system.
    1482            0 :                 tracing::info!("Early-rolling open layer at size {current_size} (limit {size_override}) due to dirty data pressure");
    1483            0 :             }
    1484            0 :         }
    1485              : 
    1486            0 :         let checkpoint_distance =
    1487            0 :             checkpoint_distance_override.unwrap_or(self.get_checkpoint_distance());
    1488            0 : 
    1489            0 :         if self.should_roll(
    1490            0 :             current_size,
    1491            0 :             current_size,
    1492            0 :             checkpoint_distance,
    1493            0 :             self.get_last_record_lsn(),
    1494            0 :             self.last_freeze_at.load(),
    1495            0 :             open_layer.get_opened_at(),
    1496            0 :         ) {
    1497            0 :             match open_layer.info() {
    1498            0 :                 InMemoryLayerInfo::Frozen { lsn_start, lsn_end } => {
    1499            0 :                     // We may reach this point if the layer was already frozen by not yet flushed: flushing
    1500            0 :                     // happens asynchronously in the background.
    1501            0 :                     tracing::debug!(
    1502            0 :                         "Not freezing open layer, it's already frozen ({lsn_start}..{lsn_end})"
    1503              :                     );
    1504              :                 }
    1505              :                 InMemoryLayerInfo::Open { .. } => {
    1506              :                     // Upgrade to a write lock and freeze the layer
    1507            0 :                     drop(layers_guard);
    1508            0 :                     let res = self
    1509            0 :                         .freeze_inmem_layer_at(current_lsn, &mut write_guard)
    1510            0 :                         .await;
    1511              : 
    1512            0 :                     if let Err(e) = res {
    1513            0 :                         tracing::info!(
    1514            0 :                             "failed to flush frozen layer after background freeze: {e:#}"
    1515              :                         );
    1516            0 :                     }
    1517              :                 }
    1518              :             }
    1519            0 :         }
    1520            0 :     }
    1521              : 
    1522              :     /// Outermost timeline compaction operation; downloads needed layers. Returns whether we have pending
    1523              :     /// compaction tasks.
    1524         1092 :     pub(crate) async fn compact(
    1525         1092 :         self: &Arc<Self>,
    1526         1092 :         cancel: &CancellationToken,
    1527         1092 :         flags: EnumSet<CompactFlags>,
    1528         1092 :         ctx: &RequestContext,
    1529         1092 :     ) -> Result<bool, CompactionError> {
    1530         1092 :         // most likely the cancellation token is from background task, but in tests it could be the
    1531         1092 :         // request task as well.
    1532         1092 : 
    1533         1092 :         let prepare = async move {
    1534         1092 :             let guard = self.compaction_lock.lock().await;
    1535              : 
    1536         1092 :             let permit = super::tasks::concurrent_background_tasks_rate_limit_permit(
    1537         1092 :                 BackgroundLoopKind::Compaction,
    1538         1092 :                 ctx,
    1539         1092 :             )
    1540            0 :             .await;
    1541              : 
    1542         1092 :             (guard, permit)
    1543         1092 :         };
    1544              : 
    1545              :         // this wait probably never needs any "long time spent" logging, because we already nag if
    1546              :         // compaction task goes over it's period (20s) which is quite often in production.
    1547         1092 :         let (_guard, _permit) = tokio::select! {
    1548         1092 :             tuple = prepare => { tuple },
    1549         1092 :             _ = self.cancel.cancelled() => return Ok(false),
    1550         1092 :             _ = cancel.cancelled() => return Ok(false),
    1551              :         };
    1552              : 
    1553         1092 :         let last_record_lsn = self.get_last_record_lsn();
    1554         1092 : 
    1555         1092 :         // Last record Lsn could be zero in case the timeline was just created
    1556         1092 :         if !last_record_lsn.is_valid() {
    1557            0 :             warn!("Skipping compaction for potentially just initialized timeline, it has invalid last record lsn: {last_record_lsn}");
    1558            0 :             return Ok(false);
    1559         1092 :         }
    1560         1092 : 
    1561         1092 :         match self.get_compaction_algorithm_settings().kind {
    1562              :             CompactionAlgorithm::Tiered => {
    1563            0 :                 self.compact_tiered(cancel, ctx).await?;
    1564            0 :                 Ok(false)
    1565              :             }
    1566       118487 :             CompactionAlgorithm::Legacy => self.compact_legacy(cancel, flags, ctx).await,
    1567              :         }
    1568         1092 :     }
    1569              : 
    1570              :     /// Mutate the timeline with a [`TimelineWriter`].
    1571     15399522 :     pub(crate) async fn writer(&self) -> TimelineWriter<'_> {
    1572     15399522 :         TimelineWriter {
    1573     15399522 :             tl: self,
    1574     15399522 :             write_guard: self.write_lock.lock().await,
    1575              :         }
    1576     15399522 :     }
    1577              : 
    1578            0 :     pub(crate) fn activate(
    1579            0 :         self: &Arc<Self>,
    1580            0 :         parent: Arc<crate::tenant::Tenant>,
    1581            0 :         broker_client: BrokerClientChannel,
    1582            0 :         background_jobs_can_start: Option<&completion::Barrier>,
    1583            0 :         ctx: &RequestContext,
    1584            0 :     ) {
    1585            0 :         if self.tenant_shard_id.is_shard_zero() {
    1586            0 :             // Logical size is only maintained accurately on shard zero.
    1587            0 :             self.spawn_initial_logical_size_computation_task(ctx);
    1588            0 :         }
    1589            0 :         self.launch_wal_receiver(ctx, broker_client);
    1590            0 :         self.set_state(TimelineState::Active);
    1591            0 :         self.launch_eviction_task(parent, background_jobs_can_start);
    1592            0 :     }
    1593              : 
    1594              :     /// After this function returns, there are no timeline-scoped tasks are left running.
    1595              :     ///
    1596              :     /// The preferred pattern for is:
    1597              :     /// - in any spawned tasks, keep Timeline::guard open + Timeline::cancel / child token
    1598              :     /// - if early shutdown (not just cancellation) of a sub-tree of tasks is required,
    1599              :     ///   go the extra mile and keep track of JoinHandles
    1600              :     /// - Keep track of JoinHandles using a passed-down `Arc<Mutex<Option<JoinSet>>>` or similar,
    1601              :     ///   instead of spawning directly on a runtime. It is a more composable / testable pattern.
    1602              :     ///
    1603              :     /// For legacy reasons, we still have multiple tasks spawned using
    1604              :     /// `task_mgr::spawn(X, Some(tenant_id), Some(timeline_id))`.
    1605              :     /// We refer to these as "timeline-scoped task_mgr tasks".
    1606              :     /// Some of these tasks are already sensitive to Timeline::cancel while others are
    1607              :     /// not sensitive to Timeline::cancel and instead respect [`task_mgr::shutdown_token`]
    1608              :     /// or [`task_mgr::shutdown_watcher`].
    1609              :     /// We want to gradually convert the code base away from these.
    1610              :     ///
    1611              :     /// Here is an inventory of timeline-scoped task_mgr tasks that are still sensitive to
    1612              :     /// `task_mgr::shutdown_{token,watcher}` (there are also tenant-scoped and global-scoped
    1613              :     /// ones that aren't mentioned here):
    1614              :     /// - [`TaskKind::TimelineDeletionWorker`]
    1615              :     ///    - NB: also used for tenant deletion
    1616              :     /// - [`TaskKind::RemoteUploadTask`]`
    1617              :     /// - [`TaskKind::InitialLogicalSizeCalculation`]
    1618              :     /// - [`TaskKind::DownloadAllRemoteLayers`] (can we get rid of it?)
    1619              :     // Inventory of timeline-scoped task_mgr tasks that use spawn but aren't sensitive:
    1620              :     /// - [`TaskKind::Eviction`]
    1621              :     /// - [`TaskKind::LayerFlushTask`]
    1622              :     /// - [`TaskKind::OndemandLogicalSizeCalculation`]
    1623              :     /// - [`TaskKind::GarbageCollector`] (immediate_gc is timeline-scoped)
    1624           24 :     pub(crate) async fn shutdown(&self, mode: ShutdownMode) {
    1625           24 :         debug_assert_current_span_has_tenant_and_timeline_id();
    1626              : 
    1627           24 :         let try_freeze_and_flush = match mode {
    1628           18 :             ShutdownMode::FreezeAndFlush => true,
    1629            6 :             ShutdownMode::Hard => false,
    1630              :         };
    1631              : 
    1632              :         // Regardless of whether we're going to try_freeze_and_flush
    1633              :         // or not, stop ingesting any more data. Walreceiver only provides
    1634              :         // cancellation but no "wait until gone", because it uses the Timeline::gate.
    1635              :         // So, only after the self.gate.close() below will we know for sure that
    1636              :         // no walreceiver tasks are left.
    1637              :         // For `try_freeze_and_flush=true`, this means that we might still be ingesting
    1638              :         // data during the call to `self.freeze_and_flush()` below.
    1639              :         // That's not ideal, but, we don't have the concept of a ChildGuard,
    1640              :         // which is what we'd need to properly model early shutdown of the walreceiver
    1641              :         // task sub-tree before the other Timeline task sub-trees.
    1642           24 :         let walreceiver = self.walreceiver.lock().unwrap().take();
    1643           24 :         tracing::debug!(
    1644            0 :             is_some = walreceiver.is_some(),
    1645            0 :             "Waiting for WalReceiverManager..."
    1646              :         );
    1647           24 :         if let Some(walreceiver) = walreceiver {
    1648            0 :             walreceiver.cancel();
    1649           24 :         }
    1650              :         // ... and inform any waiters for newer LSNs that there won't be any.
    1651           24 :         self.last_record_lsn.shutdown();
    1652           24 : 
    1653           24 :         if try_freeze_and_flush {
    1654           18 :             if let Some((open, frozen)) = self
    1655           18 :                 .layers
    1656           18 :                 .read()
    1657            0 :                 .await
    1658           18 :                 .layer_map()
    1659           18 :                 .map(|lm| (lm.open_layer.is_some(), lm.frozen_layers.len()))
    1660           18 :                 .ok()
    1661           18 :                 .filter(|(open, frozen)| *open || *frozen > 0)
    1662              :             {
    1663            0 :                 tracing::info!(?open, frozen, "flushing and freezing on shutdown");
    1664           18 :             } else {
    1665           18 :                 // this is double-shutdown, ignore it
    1666           18 :             }
    1667              : 
    1668              :             // we shut down walreceiver above, so, we won't add anything more
    1669              :             // to the InMemoryLayer; freeze it and wait for all frozen layers
    1670              :             // to reach the disk & upload queue, then shut the upload queue and
    1671              :             // wait for it to drain.
    1672           18 :             match self.freeze_and_flush().await {
    1673              :                 Ok(_) => {
    1674              :                     // drain the upload queue
    1675              :                     // if we did not wait for completion here, it might be our shutdown process
    1676              :                     // didn't wait for remote uploads to complete at all, as new tasks can forever
    1677              :                     // be spawned.
    1678              :                     //
    1679              :                     // what is problematic is the shutting down of RemoteTimelineClient, because
    1680              :                     // obviously it does not make sense to stop while we wait for it, but what
    1681              :                     // about corner cases like s3 suddenly hanging up?
    1682           18 :                     self.remote_client.shutdown().await;
    1683              :                 }
    1684              :                 Err(FlushLayerError::Cancelled) => {
    1685              :                     // this is likely the second shutdown, ignore silently.
    1686              :                     // TODO: this can be removed once https://github.com/neondatabase/neon/issues/5080
    1687            0 :                     debug_assert!(self.cancel.is_cancelled());
    1688              :                 }
    1689            0 :                 Err(e) => {
    1690            0 :                     // Non-fatal.  Shutdown is infallible.  Failures to flush just mean that
    1691            0 :                     // we have some extra WAL replay to do next time the timeline starts.
    1692            0 :                     warn!("failed to freeze and flush: {e:#}");
    1693              :                 }
    1694              :             }
    1695            6 :         }
    1696              : 
    1697              :         // Signal any subscribers to our cancellation token to drop out
    1698           24 :         tracing::debug!("Cancelling CancellationToken");
    1699           24 :         self.cancel.cancel();
    1700           24 : 
    1701           24 :         // Ensure Prevent new page service requests from starting.
    1702           24 :         self.handles.shutdown();
    1703           24 : 
    1704           24 :         // Transition the remote_client into a state where it's only useful for timeline deletion.
    1705           24 :         // (The deletion use case is why we can't just hook up remote_client to Self::cancel).)
    1706           24 :         self.remote_client.stop();
    1707           24 : 
    1708           24 :         // As documented in remote_client.stop()'s doc comment, it's our responsibility
    1709           24 :         // to shut down the upload queue tasks.
    1710           24 :         // TODO: fix that, task management should be encapsulated inside remote_client.
    1711           24 :         task_mgr::shutdown_tasks(
    1712           24 :             Some(TaskKind::RemoteUploadTask),
    1713           24 :             Some(self.tenant_shard_id),
    1714           24 :             Some(self.timeline_id),
    1715           24 :         )
    1716            0 :         .await;
    1717              : 
    1718              :         // TODO: work toward making this a no-op. See this function's doc comment for more context.
    1719           24 :         tracing::debug!("Waiting for tasks...");
    1720           24 :         task_mgr::shutdown_tasks(None, Some(self.tenant_shard_id), Some(self.timeline_id)).await;
    1721              : 
    1722              :         {
    1723              :             // Allow any remaining in-memory layers to do cleanup -- until that, they hold the gate
    1724              :             // open.
    1725           24 :             let mut write_guard = self.write_lock.lock().await;
    1726           24 :             self.layers.write().await.shutdown(&mut write_guard);
    1727           24 :         }
    1728           24 : 
    1729           24 :         // Finally wait until any gate-holders are complete.
    1730           24 :         //
    1731           24 :         // TODO: once above shutdown_tasks is a no-op, we can close the gate before calling shutdown_tasks
    1732           24 :         // and use a TBD variant of shutdown_tasks that asserts that there were no tasks left.
    1733           24 :         self.gate.close().await;
    1734              : 
    1735           24 :         self.metrics.shutdown();
    1736           24 :     }
    1737              : 
    1738         1236 :     pub(crate) fn set_state(&self, new_state: TimelineState) {
    1739         1236 :         match (self.current_state(), new_state) {
    1740         1236 :             (equal_state_1, equal_state_2) if equal_state_1 == equal_state_2 => {
    1741            6 :                 info!("Ignoring new state, equal to the existing one: {equal_state_2:?}");
    1742              :             }
    1743            0 :             (st, TimelineState::Loading) => {
    1744            0 :                 error!("ignoring transition from {st:?} into Loading state");
    1745              :             }
    1746            0 :             (TimelineState::Broken { .. }, new_state) => {
    1747            0 :                 error!("Ignoring state update {new_state:?} for broken timeline");
    1748              :             }
    1749              :             (TimelineState::Stopping, TimelineState::Active) => {
    1750            0 :                 error!("Not activating a Stopping timeline");
    1751              :             }
    1752         1230 :             (_, new_state) => {
    1753         1230 :                 self.state.send_replace(new_state);
    1754         1230 :             }
    1755              :         }
    1756         1236 :     }
    1757              : 
    1758            6 :     pub(crate) fn set_broken(&self, reason: String) {
    1759            6 :         let backtrace_str: String = format!("{}", std::backtrace::Backtrace::force_capture());
    1760            6 :         let broken_state = TimelineState::Broken {
    1761            6 :             reason,
    1762            6 :             backtrace: backtrace_str,
    1763            6 :         };
    1764            6 :         self.set_state(broken_state);
    1765            6 : 
    1766            6 :         // Although the Broken state is not equivalent to shutdown() (shutdown will be called
    1767            6 :         // later when this tenant is detach or the process shuts down), firing the cancellation token
    1768            6 :         // here avoids the need for other tasks to watch for the Broken state explicitly.
    1769            6 :         self.cancel.cancel();
    1770            6 :     }
    1771              : 
    1772       683672 :     pub(crate) fn current_state(&self) -> TimelineState {
    1773       683672 :         self.state.borrow().clone()
    1774       683672 :     }
    1775              : 
    1776           18 :     pub(crate) fn is_broken(&self) -> bool {
    1777           18 :         matches!(&*self.state.borrow(), TimelineState::Broken { .. })
    1778           18 :     }
    1779              : 
    1780          666 :     pub(crate) fn is_active(&self) -> bool {
    1781          666 :         self.current_state() == TimelineState::Active
    1782          666 :     }
    1783              : 
    1784              :     #[allow(unused)]
    1785            0 :     pub(crate) fn is_archived(&self) -> Option<bool> {
    1786            0 :         self.remote_client.is_archived()
    1787            0 :     }
    1788              : 
    1789         3352 :     pub(crate) fn is_stopping(&self) -> bool {
    1790         3352 :         self.current_state() == TimelineState::Stopping
    1791         3352 :     }
    1792              : 
    1793            0 :     pub(crate) fn subscribe_for_state_updates(&self) -> watch::Receiver<TimelineState> {
    1794            0 :         self.state.subscribe()
    1795            0 :     }
    1796              : 
    1797       678424 :     pub(crate) async fn wait_to_become_active(
    1798       678424 :         &self,
    1799       678424 :         _ctx: &RequestContext, // Prepare for use by cancellation
    1800       678424 :     ) -> Result<(), TimelineState> {
    1801       678424 :         let mut receiver = self.state.subscribe();
    1802              :         loop {
    1803       678424 :             let current_state = receiver.borrow().clone();
    1804       678424 :             match current_state {
    1805              :                 TimelineState::Loading => {
    1806            0 :                     receiver
    1807            0 :                         .changed()
    1808            0 :                         .await
    1809            0 :                         .expect("holding a reference to self");
    1810              :                 }
    1811              :                 TimelineState::Active { .. } => {
    1812       678418 :                     return Ok(());
    1813              :                 }
    1814              :                 TimelineState::Broken { .. } | TimelineState::Stopping => {
    1815              :                     // There's no chance the timeline can transition back into ::Active
    1816            6 :                     return Err(current_state);
    1817              :                 }
    1818              :             }
    1819              :         }
    1820       678424 :     }
    1821              : 
    1822            0 :     pub(crate) async fn layer_map_info(
    1823            0 :         &self,
    1824            0 :         reset: LayerAccessStatsReset,
    1825            0 :     ) -> Result<LayerMapInfo, layer_manager::Shutdown> {
    1826            0 :         let guard = self.layers.read().await;
    1827            0 :         let layer_map = guard.layer_map()?;
    1828            0 :         let mut in_memory_layers = Vec::with_capacity(layer_map.frozen_layers.len() + 1);
    1829            0 :         if let Some(open_layer) = &layer_map.open_layer {
    1830            0 :             in_memory_layers.push(open_layer.info());
    1831            0 :         }
    1832            0 :         for frozen_layer in &layer_map.frozen_layers {
    1833            0 :             in_memory_layers.push(frozen_layer.info());
    1834            0 :         }
    1835              : 
    1836            0 :         let historic_layers = layer_map
    1837            0 :             .iter_historic_layers()
    1838            0 :             .map(|desc| guard.get_from_desc(&desc).info(reset))
    1839            0 :             .collect();
    1840            0 : 
    1841            0 :         Ok(LayerMapInfo {
    1842            0 :             in_memory_layers,
    1843            0 :             historic_layers,
    1844            0 :         })
    1845            0 :     }
    1846              : 
    1847            0 :     #[instrument(skip_all, fields(tenant_id = %self.tenant_shard_id.tenant_id, shard_id = %self.tenant_shard_id.shard_slug(), timeline_id = %self.timeline_id))]
    1848              :     pub(crate) async fn download_layer(
    1849              :         &self,
    1850              :         layer_file_name: &LayerName,
    1851              :     ) -> anyhow::Result<Option<bool>> {
    1852              :         let Some(layer) = self.find_layer(layer_file_name).await? else {
    1853              :             return Ok(None);
    1854              :         };
    1855              : 
    1856              :         layer.download().await?;
    1857              : 
    1858              :         Ok(Some(true))
    1859              :     }
    1860              : 
    1861              :     /// Evict just one layer.
    1862              :     ///
    1863              :     /// Returns `Ok(None)` in the case where the layer could not be found by its `layer_file_name`.
    1864            0 :     pub(crate) async fn evict_layer(
    1865            0 :         &self,
    1866            0 :         layer_file_name: &LayerName,
    1867            0 :     ) -> anyhow::Result<Option<bool>> {
    1868            0 :         let _gate = self
    1869            0 :             .gate
    1870            0 :             .enter()
    1871            0 :             .map_err(|_| anyhow::anyhow!("Shutting down"))?;
    1872              : 
    1873            0 :         let Some(local_layer) = self.find_layer(layer_file_name).await? else {
    1874            0 :             return Ok(None);
    1875              :         };
    1876              : 
    1877              :         // curl has this by default
    1878            0 :         let timeout = std::time::Duration::from_secs(120);
    1879            0 : 
    1880            0 :         match local_layer.evict_and_wait(timeout).await {
    1881            0 :             Ok(()) => Ok(Some(true)),
    1882            0 :             Err(EvictionError::NotFound) => Ok(Some(false)),
    1883            0 :             Err(EvictionError::Downloaded) => Ok(Some(false)),
    1884            0 :             Err(EvictionError::Timeout) => Ok(Some(false)),
    1885              :         }
    1886            0 :     }
    1887              : 
    1888     14409060 :     fn should_roll(
    1889     14409060 :         &self,
    1890     14409060 :         layer_size: u64,
    1891     14409060 :         projected_layer_size: u64,
    1892     14409060 :         checkpoint_distance: u64,
    1893     14409060 :         projected_lsn: Lsn,
    1894     14409060 :         last_freeze_at: Lsn,
    1895     14409060 :         opened_at: Instant,
    1896     14409060 :     ) -> bool {
    1897     14409060 :         let distance = projected_lsn.widening_sub(last_freeze_at);
    1898     14409060 : 
    1899     14409060 :         // Rolling the open layer can be triggered by:
    1900     14409060 :         // 1. The distance from the last LSN we rolled at. This bounds the amount of WAL that
    1901     14409060 :         //    the safekeepers need to store.  For sharded tenants, we multiply by shard count to
    1902     14409060 :         //    account for how writes are distributed across shards: we expect each node to consume
    1903     14409060 :         //    1/count of the LSN on average.
    1904     14409060 :         // 2. The size of the currently open layer.
    1905     14409060 :         // 3. The time since the last roll. It helps safekeepers to regard pageserver as caught
    1906     14409060 :         //    up and suspend activity.
    1907     14409060 :         if distance >= checkpoint_distance as i128 * self.shard_identity.count.count() as i128 {
    1908            0 :             info!(
    1909            0 :                 "Will roll layer at {} with layer size {} due to LSN distance ({})",
    1910              :                 projected_lsn, layer_size, distance
    1911              :             );
    1912              : 
    1913            0 :             true
    1914     14409060 :         } else if projected_layer_size >= checkpoint_distance {
    1915              :             // NB: this check is relied upon by:
    1916          240 :             let _ = IndexEntry::validate_checkpoint_distance;
    1917          240 :             info!(
    1918            0 :                 "Will roll layer at {} with layer size {} due to layer size ({})",
    1919              :                 projected_lsn, layer_size, projected_layer_size
    1920              :             );
    1921              : 
    1922          240 :             true
    1923     14408820 :         } else if distance > 0 && opened_at.elapsed() >= self.get_checkpoint_timeout() {
    1924            0 :             info!(
    1925            0 :                 "Will roll layer at {} with layer size {} due to time since first write to the layer ({:?})",
    1926            0 :                 projected_lsn,
    1927            0 :                 layer_size,
    1928            0 :                 opened_at.elapsed()
    1929              :             );
    1930              : 
    1931            0 :             true
    1932              :         } else {
    1933     14408820 :             false
    1934              :         }
    1935     14409060 :     }
    1936              : }
    1937              : 
    1938              : /// Number of times we will compute partition within a checkpoint distance.
    1939              : const REPARTITION_FREQ_IN_CHECKPOINT_DISTANCE: u64 = 10;
    1940              : 
    1941              : // Private functions
    1942              : impl Timeline {
    1943           36 :     pub(crate) fn get_lsn_lease_length(&self) -> Duration {
    1944           36 :         let tenant_conf = self.tenant_conf.load();
    1945           36 :         tenant_conf
    1946           36 :             .tenant_conf
    1947           36 :             .lsn_lease_length
    1948           36 :             .unwrap_or(self.conf.default_tenant_conf.lsn_lease_length)
    1949           36 :     }
    1950              : 
    1951              :     // TODO(yuchen): remove unused flag after implementing https://github.com/neondatabase/neon/issues/8072
    1952              :     #[allow(unused)]
    1953            0 :     pub(crate) fn get_lsn_lease_length_for_ts(&self) -> Duration {
    1954            0 :         let tenant_conf = self.tenant_conf.load();
    1955            0 :         tenant_conf
    1956            0 :             .tenant_conf
    1957            0 :             .lsn_lease_length_for_ts
    1958            0 :             .unwrap_or(self.conf.default_tenant_conf.lsn_lease_length_for_ts)
    1959            0 :     }
    1960              : 
    1961          690 :     pub(crate) fn get_switch_aux_file_policy(&self) -> AuxFilePolicy {
    1962          690 :         let tenant_conf = self.tenant_conf.load();
    1963          690 :         tenant_conf
    1964          690 :             .tenant_conf
    1965          690 :             .switch_aux_file_policy
    1966          690 :             .unwrap_or(self.conf.default_tenant_conf.switch_aux_file_policy)
    1967          690 :     }
    1968              : 
    1969            0 :     pub(crate) fn get_lazy_slru_download(&self) -> bool {
    1970            0 :         let tenant_conf = self.tenant_conf.load();
    1971            0 :         tenant_conf
    1972            0 :             .tenant_conf
    1973            0 :             .lazy_slru_download
    1974            0 :             .unwrap_or(self.conf.default_tenant_conf.lazy_slru_download)
    1975            0 :     }
    1976              : 
    1977     14413836 :     fn get_checkpoint_distance(&self) -> u64 {
    1978     14413836 :         let tenant_conf = self.tenant_conf.load();
    1979     14413836 :         tenant_conf
    1980     14413836 :             .tenant_conf
    1981     14413836 :             .checkpoint_distance
    1982     14413836 :             .unwrap_or(self.conf.default_tenant_conf.checkpoint_distance)
    1983     14413836 :     }
    1984              : 
    1985     14408820 :     fn get_checkpoint_timeout(&self) -> Duration {
    1986     14408820 :         let tenant_conf = self.tenant_conf.load();
    1987     14408820 :         tenant_conf
    1988     14408820 :             .tenant_conf
    1989     14408820 :             .checkpoint_timeout
    1990     14408820 :             .unwrap_or(self.conf.default_tenant_conf.checkpoint_timeout)
    1991     14408820 :     }
    1992              : 
    1993         1758 :     fn get_compaction_target_size(&self) -> u64 {
    1994         1758 :         let tenant_conf = self.tenant_conf.load();
    1995         1758 :         tenant_conf
    1996         1758 :             .tenant_conf
    1997         1758 :             .compaction_target_size
    1998         1758 :             .unwrap_or(self.conf.default_tenant_conf.compaction_target_size)
    1999         1758 :     }
    2000              : 
    2001         1176 :     fn get_compaction_threshold(&self) -> usize {
    2002         1176 :         let tenant_conf = self.tenant_conf.load();
    2003         1176 :         tenant_conf
    2004         1176 :             .tenant_conf
    2005         1176 :             .compaction_threshold
    2006         1176 :             .unwrap_or(self.conf.default_tenant_conf.compaction_threshold)
    2007         1176 :     }
    2008              : 
    2009           42 :     fn get_image_creation_threshold(&self) -> usize {
    2010           42 :         let tenant_conf = self.tenant_conf.load();
    2011           42 :         tenant_conf
    2012           42 :             .tenant_conf
    2013           42 :             .image_creation_threshold
    2014           42 :             .unwrap_or(self.conf.default_tenant_conf.image_creation_threshold)
    2015           42 :     }
    2016              : 
    2017         1092 :     fn get_compaction_algorithm_settings(&self) -> CompactionAlgorithmSettings {
    2018         1092 :         let tenant_conf = &self.tenant_conf.load();
    2019         1092 :         tenant_conf
    2020         1092 :             .tenant_conf
    2021         1092 :             .compaction_algorithm
    2022         1092 :             .as_ref()
    2023         1092 :             .unwrap_or(&self.conf.default_tenant_conf.compaction_algorithm)
    2024         1092 :             .clone()
    2025         1092 :     }
    2026              : 
    2027            0 :     fn get_eviction_policy(&self) -> EvictionPolicy {
    2028            0 :         let tenant_conf = self.tenant_conf.load();
    2029            0 :         tenant_conf
    2030            0 :             .tenant_conf
    2031            0 :             .eviction_policy
    2032            0 :             .unwrap_or(self.conf.default_tenant_conf.eviction_policy)
    2033            0 :     }
    2034              : 
    2035         1260 :     fn get_evictions_low_residence_duration_metric_threshold(
    2036         1260 :         tenant_conf: &TenantConfOpt,
    2037         1260 :         default_tenant_conf: &TenantConf,
    2038         1260 :     ) -> Duration {
    2039         1260 :         tenant_conf
    2040         1260 :             .evictions_low_residence_duration_metric_threshold
    2041         1260 :             .unwrap_or(default_tenant_conf.evictions_low_residence_duration_metric_threshold)
    2042         1260 :     }
    2043              : 
    2044         2124 :     fn get_image_layer_creation_check_threshold(&self) -> u8 {
    2045         2124 :         let tenant_conf = self.tenant_conf.load();
    2046         2124 :         tenant_conf
    2047         2124 :             .tenant_conf
    2048         2124 :             .image_layer_creation_check_threshold
    2049         2124 :             .unwrap_or(
    2050         2124 :                 self.conf
    2051         2124 :                     .default_tenant_conf
    2052         2124 :                     .image_layer_creation_check_threshold,
    2053         2124 :             )
    2054         2124 :     }
    2055              : 
    2056           24 :     pub(super) fn tenant_conf_updated(&self, new_conf: &TenantConfOpt) {
    2057           24 :         // NB: Most tenant conf options are read by background loops, so,
    2058           24 :         // changes will automatically be picked up.
    2059           24 : 
    2060           24 :         // The threshold is embedded in the metric. So, we need to update it.
    2061           24 :         {
    2062           24 :             let new_threshold = Self::get_evictions_low_residence_duration_metric_threshold(
    2063           24 :                 new_conf,
    2064           24 :                 &self.conf.default_tenant_conf,
    2065           24 :             );
    2066           24 : 
    2067           24 :             let tenant_id_str = self.tenant_shard_id.tenant_id.to_string();
    2068           24 :             let shard_id_str = format!("{}", self.tenant_shard_id.shard_slug());
    2069           24 : 
    2070           24 :             let timeline_id_str = self.timeline_id.to_string();
    2071           24 :             self.metrics
    2072           24 :                 .evictions_with_low_residence_duration
    2073           24 :                 .write()
    2074           24 :                 .unwrap()
    2075           24 :                 .change_threshold(
    2076           24 :                     &tenant_id_str,
    2077           24 :                     &shard_id_str,
    2078           24 :                     &timeline_id_str,
    2079           24 :                     new_threshold,
    2080           24 :                 );
    2081           24 :         }
    2082           24 :     }
    2083              : 
    2084              :     /// Open a Timeline handle.
    2085              :     ///
    2086              :     /// Loads the metadata for the timeline into memory, but not the layer map.
    2087              :     #[allow(clippy::too_many_arguments)]
    2088         1236 :     pub(super) fn new(
    2089         1236 :         conf: &'static PageServerConf,
    2090         1236 :         tenant_conf: Arc<ArcSwap<AttachedTenantConf>>,
    2091         1236 :         metadata: &TimelineMetadata,
    2092         1236 :         ancestor: Option<Arc<Timeline>>,
    2093         1236 :         timeline_id: TimelineId,
    2094         1236 :         tenant_shard_id: TenantShardId,
    2095         1236 :         generation: Generation,
    2096         1236 :         shard_identity: ShardIdentity,
    2097         1236 :         walredo_mgr: Option<Arc<super::WalRedoManager>>,
    2098         1236 :         resources: TimelineResources,
    2099         1236 :         pg_version: u32,
    2100         1236 :         state: TimelineState,
    2101         1236 :         aux_file_policy: Option<AuxFilePolicy>,
    2102         1236 :         cancel: CancellationToken,
    2103         1236 :     ) -> Arc<Self> {
    2104         1236 :         let disk_consistent_lsn = metadata.disk_consistent_lsn();
    2105         1236 :         let (state, _) = watch::channel(state);
    2106         1236 : 
    2107         1236 :         let (layer_flush_start_tx, _) = tokio::sync::watch::channel((0, disk_consistent_lsn));
    2108         1236 :         let (layer_flush_done_tx, _) = tokio::sync::watch::channel((0, Ok(())));
    2109         1236 : 
    2110         1236 :         let evictions_low_residence_duration_metric_threshold = {
    2111         1236 :             let loaded_tenant_conf = tenant_conf.load();
    2112         1236 :             Self::get_evictions_low_residence_duration_metric_threshold(
    2113         1236 :                 &loaded_tenant_conf.tenant_conf,
    2114         1236 :                 &conf.default_tenant_conf,
    2115         1236 :             )
    2116              :         };
    2117              : 
    2118         1236 :         if let Some(ancestor) = &ancestor {
    2119          684 :             let mut ancestor_gc_info = ancestor.gc_info.write().unwrap();
    2120          684 :             ancestor_gc_info.insert_child(timeline_id, metadata.ancestor_lsn());
    2121          684 :         }
    2122              : 
    2123         1236 :         Arc::new_cyclic(|myself| {
    2124         1236 :             let metrics = TimelineMetrics::new(
    2125         1236 :                 &tenant_shard_id,
    2126         1236 :                 &timeline_id,
    2127         1236 :                 crate::metrics::EvictionsWithLowResidenceDurationBuilder::new(
    2128         1236 :                     "mtime",
    2129         1236 :                     evictions_low_residence_duration_metric_threshold,
    2130         1236 :                 ),
    2131         1236 :             );
    2132         1236 :             let aux_file_metrics = metrics.aux_file_size_gauge.clone();
    2133              : 
    2134         1236 :             let mut result = Timeline {
    2135         1236 :                 conf,
    2136         1236 :                 tenant_conf,
    2137         1236 :                 myself: myself.clone(),
    2138         1236 :                 timeline_id,
    2139         1236 :                 tenant_shard_id,
    2140         1236 :                 generation,
    2141         1236 :                 shard_identity,
    2142         1236 :                 pg_version,
    2143         1236 :                 layers: Default::default(),
    2144         1236 : 
    2145         1236 :                 walredo_mgr,
    2146         1236 :                 walreceiver: Mutex::new(None),
    2147         1236 : 
    2148         1236 :                 remote_client: Arc::new(resources.remote_client),
    2149         1236 : 
    2150         1236 :                 // initialize in-memory 'last_record_lsn' from 'disk_consistent_lsn'.
    2151         1236 :                 last_record_lsn: SeqWait::new(RecordLsn {
    2152         1236 :                     last: disk_consistent_lsn,
    2153         1236 :                     prev: metadata.prev_record_lsn().unwrap_or(Lsn(0)),
    2154         1236 :                 }),
    2155         1236 :                 disk_consistent_lsn: AtomicLsn::new(disk_consistent_lsn.0),
    2156         1236 : 
    2157         1236 :                 last_freeze_at: AtomicLsn::new(disk_consistent_lsn.0),
    2158         1236 :                 last_freeze_ts: RwLock::new(Instant::now()),
    2159         1236 : 
    2160         1236 :                 loaded_at: (disk_consistent_lsn, SystemTime::now()),
    2161         1236 : 
    2162         1236 :                 ancestor_timeline: ancestor,
    2163         1236 :                 ancestor_lsn: metadata.ancestor_lsn(),
    2164         1236 : 
    2165         1236 :                 metrics,
    2166         1236 : 
    2167         1236 :                 query_metrics: crate::metrics::SmgrQueryTimePerTimeline::new(
    2168         1236 :                     &tenant_shard_id,
    2169         1236 :                     &timeline_id,
    2170         1236 :                 ),
    2171         1236 : 
    2172         8652 :                 directory_metrics: array::from_fn(|_| AtomicU64::new(0)),
    2173         1236 : 
    2174         1236 :                 flush_loop_state: Mutex::new(FlushLoopState::NotStarted),
    2175         1236 : 
    2176         1236 :                 layer_flush_start_tx,
    2177         1236 :                 layer_flush_done_tx,
    2178         1236 : 
    2179         1236 :                 write_lock: tokio::sync::Mutex::new(None),
    2180         1236 : 
    2181         1236 :                 gc_info: std::sync::RwLock::new(GcInfo::default()),
    2182         1236 : 
    2183         1236 :                 latest_gc_cutoff_lsn: Rcu::new(metadata.latest_gc_cutoff_lsn()),
    2184         1236 :                 initdb_lsn: metadata.initdb_lsn(),
    2185         1236 : 
    2186         1236 :                 current_logical_size: if disk_consistent_lsn.is_valid() {
    2187              :                     // we're creating timeline data with some layer files existing locally,
    2188              :                     // need to recalculate timeline's logical size based on data in the layers.
    2189          696 :                     LogicalSize::deferred_initial(disk_consistent_lsn)
    2190              :                 } else {
    2191              :                     // we're creating timeline data without any layers existing locally,
    2192              :                     // initial logical size is 0.
    2193          540 :                     LogicalSize::empty_initial()
    2194              :                 },
    2195         1236 :                 partitioning: tokio::sync::Mutex::new((
    2196         1236 :                     (KeyPartitioning::new(), KeyPartitioning::new().into_sparse()),
    2197         1236 :                     Lsn(0),
    2198         1236 :                 )),
    2199         1236 :                 repartition_threshold: 0,
    2200         1236 :                 last_image_layer_creation_check_at: AtomicLsn::new(0),
    2201         1236 :                 last_image_layer_creation_check_instant: Mutex::new(None),
    2202         1236 : 
    2203         1236 :                 last_received_wal: Mutex::new(None),
    2204         1236 :                 rel_size_cache: RwLock::new(RelSizeCache {
    2205         1236 :                     complete_as_of: disk_consistent_lsn,
    2206         1236 :                     map: HashMap::new(),
    2207         1236 :                 }),
    2208         1236 : 
    2209         1236 :                 download_all_remote_layers_task_info: RwLock::new(None),
    2210         1236 : 
    2211         1236 :                 state,
    2212         1236 : 
    2213         1236 :                 eviction_task_timeline_state: tokio::sync::Mutex::new(
    2214         1236 :                     EvictionTaskTimelineState::default(),
    2215         1236 :                 ),
    2216         1236 :                 delete_progress: Arc::new(tokio::sync::Mutex::new(DeleteTimelineFlow::default())),
    2217         1236 : 
    2218         1236 :                 cancel,
    2219         1236 :                 gate: Gate::default(),
    2220         1236 : 
    2221         1236 :                 compaction_lock: tokio::sync::Mutex::default(),
    2222         1236 :                 gc_lock: tokio::sync::Mutex::default(),
    2223         1236 : 
    2224         1236 :                 standby_horizon: AtomicLsn::new(0),
    2225         1236 : 
    2226         1236 :                 timeline_get_throttle: resources.timeline_get_throttle,
    2227         1236 : 
    2228         1236 :                 aux_files: tokio::sync::Mutex::new(AuxFilesState {
    2229         1236 :                     dir: None,
    2230         1236 :                     n_deltas: 0,
    2231         1236 :                 }),
    2232         1236 : 
    2233         1236 :                 aux_file_size_estimator: AuxFileSizeEstimator::new(aux_file_metrics),
    2234         1236 : 
    2235         1236 :                 last_aux_file_policy: AtomicAuxFilePolicy::new(aux_file_policy),
    2236         1236 : 
    2237         1236 :                 #[cfg(test)]
    2238         1236 :                 extra_test_dense_keyspace: ArcSwap::new(Arc::new(KeySpace::default())),
    2239         1236 : 
    2240         1236 :                 l0_flush_global_state: resources.l0_flush_global_state,
    2241         1236 : 
    2242         1236 :                 handles: Default::default(),
    2243         1236 :             };
    2244         1236 : 
    2245         1236 :             if aux_file_policy == Some(AuxFilePolicy::V1) {
    2246            0 :                 warn!("this timeline is using deprecated aux file policy V1 (when loading the timeline)");
    2247         1236 :             }
    2248              : 
    2249         1236 :             result.repartition_threshold =
    2250         1236 :                 result.get_checkpoint_distance() / REPARTITION_FREQ_IN_CHECKPOINT_DISTANCE;
    2251         1236 : 
    2252         1236 :             result
    2253         1236 :                 .metrics
    2254         1236 :                 .last_record_gauge
    2255         1236 :                 .set(disk_consistent_lsn.0 as i64);
    2256         1236 :             result
    2257         1236 :         })
    2258         1236 :     }
    2259              : 
    2260         1734 :     pub(super) fn maybe_spawn_flush_loop(self: &Arc<Self>) {
    2261         1734 :         let Ok(guard) = self.gate.enter() else {
    2262            0 :             info!("cannot start flush loop when the timeline gate has already been closed");
    2263            0 :             return;
    2264              :         };
    2265         1734 :         let mut flush_loop_state = self.flush_loop_state.lock().unwrap();
    2266         1734 :         match *flush_loop_state {
    2267         1218 :             FlushLoopState::NotStarted => (),
    2268              :             FlushLoopState::Running { .. } => {
    2269          516 :                 info!(
    2270            0 :                     "skipping attempt to start flush_loop twice {}/{}",
    2271            0 :                     self.tenant_shard_id, self.timeline_id
    2272              :                 );
    2273          516 :                 return;
    2274              :             }
    2275              :             FlushLoopState::Exited => {
    2276            0 :                 warn!(
    2277            0 :                     "ignoring attempt to restart exited flush_loop {}/{}",
    2278            0 :                     self.tenant_shard_id, self.timeline_id
    2279              :                 );
    2280            0 :                 return;
    2281              :             }
    2282              :         }
    2283              : 
    2284         1218 :         let layer_flush_start_rx = self.layer_flush_start_tx.subscribe();
    2285         1218 :         let self_clone = Arc::clone(self);
    2286         1218 : 
    2287         1218 :         debug!("spawning flush loop");
    2288         1218 :         *flush_loop_state = FlushLoopState::Running {
    2289         1218 :             #[cfg(test)]
    2290         1218 :             expect_initdb_optimization: false,
    2291         1218 :             #[cfg(test)]
    2292         1218 :             initdb_optimization_count: 0,
    2293         1218 :         };
    2294         1218 :         task_mgr::spawn(
    2295         1218 :             task_mgr::BACKGROUND_RUNTIME.handle(),
    2296         1218 :             task_mgr::TaskKind::LayerFlushTask,
    2297         1218 :             self.tenant_shard_id,
    2298         1218 :             Some(self.timeline_id),
    2299         1218 :             "layer flush task",
    2300         1218 :             async move {
    2301         1218 :                 let _guard = guard;
    2302         1218 :                 let background_ctx = RequestContext::todo_child(TaskKind::LayerFlushTask, DownloadBehavior::Error);
    2303        54073 :                 self_clone.flush_loop(layer_flush_start_rx, &background_ctx).await;
    2304           24 :                 let mut flush_loop_state = self_clone.flush_loop_state.lock().unwrap();
    2305           24 :                 assert!(matches!(*flush_loop_state, FlushLoopState::Running{..}));
    2306           24 :                 *flush_loop_state  = FlushLoopState::Exited;
    2307           24 :                 Ok(())
    2308           24 :             }
    2309         1218 :             .instrument(info_span!(parent: None, "layer flush task", tenant_id = %self.tenant_shard_id.tenant_id, shard_id = %self.tenant_shard_id.shard_slug(), timeline_id = %self.timeline_id))
    2310              :         );
    2311         1734 :     }
    2312              : 
    2313              :     /// Creates and starts the wal receiver.
    2314              :     ///
    2315              :     /// This function is expected to be called at most once per Timeline's lifecycle
    2316              :     /// when the timeline is activated.
    2317            0 :     fn launch_wal_receiver(
    2318            0 :         self: &Arc<Self>,
    2319            0 :         ctx: &RequestContext,
    2320            0 :         broker_client: BrokerClientChannel,
    2321            0 :     ) {
    2322            0 :         info!(
    2323            0 :             "launching WAL receiver for timeline {} of tenant {}",
    2324            0 :             self.timeline_id, self.tenant_shard_id
    2325              :         );
    2326              : 
    2327            0 :         let tenant_conf = self.tenant_conf.load();
    2328            0 :         let wal_connect_timeout = tenant_conf
    2329            0 :             .tenant_conf
    2330            0 :             .walreceiver_connect_timeout
    2331            0 :             .unwrap_or(self.conf.default_tenant_conf.walreceiver_connect_timeout);
    2332            0 :         let lagging_wal_timeout = tenant_conf
    2333            0 :             .tenant_conf
    2334            0 :             .lagging_wal_timeout
    2335            0 :             .unwrap_or(self.conf.default_tenant_conf.lagging_wal_timeout);
    2336            0 :         let max_lsn_wal_lag = tenant_conf
    2337            0 :             .tenant_conf
    2338            0 :             .max_lsn_wal_lag
    2339            0 :             .unwrap_or(self.conf.default_tenant_conf.max_lsn_wal_lag);
    2340            0 : 
    2341            0 :         let mut guard = self.walreceiver.lock().unwrap();
    2342            0 :         assert!(
    2343            0 :             guard.is_none(),
    2344            0 :             "multiple launches / re-launches of WAL receiver are not supported"
    2345              :         );
    2346            0 :         *guard = Some(WalReceiver::start(
    2347            0 :             Arc::clone(self),
    2348            0 :             WalReceiverConf {
    2349            0 :                 wal_connect_timeout,
    2350            0 :                 lagging_wal_timeout,
    2351            0 :                 max_lsn_wal_lag,
    2352            0 :                 auth_token: crate::config::SAFEKEEPER_AUTH_TOKEN.get().cloned(),
    2353            0 :                 availability_zone: self.conf.availability_zone.clone(),
    2354            0 :                 ingest_batch_size: self.conf.ingest_batch_size,
    2355            0 :             },
    2356            0 :             broker_client,
    2357            0 :             ctx,
    2358            0 :         ));
    2359            0 :     }
    2360              : 
    2361              :     /// Initialize with an empty layer map. Used when creating a new timeline.
    2362         1218 :     pub(super) fn init_empty_layer_map(&self, start_lsn: Lsn) {
    2363         1218 :         let mut layers = self.layers.try_write().expect(
    2364         1218 :             "in the context where we call this function, no other task has access to the object",
    2365         1218 :         );
    2366         1218 :         layers
    2367         1218 :             .open_mut()
    2368         1218 :             .expect("in this context the LayerManager must still be open")
    2369         1218 :             .initialize_empty(Lsn(start_lsn.0));
    2370         1218 :     }
    2371              : 
    2372              :     /// Scan the timeline directory, cleanup, populate the layer map, and schedule uploads for local-only
    2373              :     /// files.
    2374           18 :     pub(super) async fn load_layer_map(
    2375           18 :         &self,
    2376           18 :         disk_consistent_lsn: Lsn,
    2377           18 :         index_part: Option<IndexPart>,
    2378           18 :     ) -> anyhow::Result<()> {
    2379              :         use init::{Decision::*, Discovered, DismissedLayer};
    2380              :         use LayerName::*;
    2381              : 
    2382           18 :         let mut guard = self.layers.write().await;
    2383              : 
    2384           18 :         let timer = self.metrics.load_layer_map_histo.start_timer();
    2385           18 : 
    2386           18 :         // Scan timeline directory and create ImageLayerName and DeltaFilename
    2387           18 :         // structs representing all files on disk
    2388           18 :         let timeline_path = self
    2389           18 :             .conf
    2390           18 :             .timeline_path(&self.tenant_shard_id, &self.timeline_id);
    2391           18 :         let conf = self.conf;
    2392           18 :         let span = tracing::Span::current();
    2393           18 : 
    2394           18 :         // Copy to move into the task we're about to spawn
    2395           18 :         let this = self.myself.upgrade().expect("&self method holds the arc");
    2396              : 
    2397           18 :         let (loaded_layers, needs_cleanup, total_physical_size) = tokio::task::spawn_blocking({
    2398           18 :             move || {
    2399           18 :                 let _g = span.entered();
    2400           18 :                 let discovered = init::scan_timeline_dir(&timeline_path)?;
    2401           18 :                 let mut discovered_layers = Vec::with_capacity(discovered.len());
    2402           18 :                 let mut unrecognized_files = Vec::new();
    2403           18 : 
    2404           18 :                 let mut path = timeline_path;
    2405              : 
    2406           66 :                 for discovered in discovered {
    2407           48 :                     let (name, kind) = match discovered {
    2408           48 :                         Discovered::Layer(layer_file_name, local_metadata) => {
    2409           48 :                             discovered_layers.push((layer_file_name, local_metadata));
    2410           48 :                             continue;
    2411              :                         }
    2412            0 :                         Discovered::IgnoredBackup(path) => {
    2413            0 :                             std::fs::remove_file(path)
    2414            0 :                                 .or_else(fs_ext::ignore_not_found)
    2415            0 :                                 .fatal_err("Removing .old file");
    2416            0 :                             continue;
    2417              :                         }
    2418            0 :                         Discovered::Unknown(file_name) => {
    2419            0 :                             // we will later error if there are any
    2420            0 :                             unrecognized_files.push(file_name);
    2421            0 :                             continue;
    2422              :                         }
    2423            0 :                         Discovered::Ephemeral(name) => (name, "old ephemeral file"),
    2424            0 :                         Discovered::Temporary(name) => (name, "temporary timeline file"),
    2425            0 :                         Discovered::TemporaryDownload(name) => (name, "temporary download"),
    2426              :                     };
    2427            0 :                     path.push(Utf8Path::new(&name));
    2428            0 :                     init::cleanup(&path, kind)?;
    2429            0 :                     path.pop();
    2430              :                 }
    2431              : 
    2432           18 :                 if !unrecognized_files.is_empty() {
    2433              :                     // assume that if there are any there are many many.
    2434            0 :                     let n = unrecognized_files.len();
    2435            0 :                     let first = &unrecognized_files[..n.min(10)];
    2436            0 :                     anyhow::bail!(
    2437            0 :                         "unrecognized files in timeline dir (total {n}), first 10: {first:?}"
    2438            0 :                     );
    2439           18 :                 }
    2440           18 : 
    2441           18 :                 let decided =
    2442           18 :                     init::reconcile(discovered_layers, index_part.as_ref(), disk_consistent_lsn);
    2443           18 : 
    2444           18 :                 let mut loaded_layers = Vec::new();
    2445           18 :                 let mut needs_cleanup = Vec::new();
    2446           18 :                 let mut total_physical_size = 0;
    2447              : 
    2448           66 :                 for (name, decision) in decided {
    2449           48 :                     let decision = match decision {
    2450           48 :                         Ok(decision) => decision,
    2451            0 :                         Err(DismissedLayer::Future { local }) => {
    2452            0 :                             if let Some(local) = local {
    2453            0 :                                 init::cleanup_future_layer(
    2454            0 :                                     &local.local_path,
    2455            0 :                                     &name,
    2456            0 :                                     disk_consistent_lsn,
    2457            0 :                                 )?;
    2458            0 :                             }
    2459            0 :                             needs_cleanup.push(name);
    2460            0 :                             continue;
    2461              :                         }
    2462            0 :                         Err(DismissedLayer::LocalOnly(local)) => {
    2463            0 :                             init::cleanup_local_only_file(&name, &local)?;
    2464              :                             // this file never existed remotely, we will have to do rework
    2465            0 :                             continue;
    2466              :                         }
    2467            0 :                         Err(DismissedLayer::BadMetadata(local)) => {
    2468            0 :                             init::cleanup_local_file_for_remote(&local)?;
    2469              :                             // this file never existed remotely, we will have to do rework
    2470            0 :                             continue;
    2471              :                         }
    2472              :                     };
    2473              : 
    2474           48 :                     match &name {
    2475           36 :                         Delta(d) => assert!(d.lsn_range.end <= disk_consistent_lsn + 1),
    2476           12 :                         Image(i) => assert!(i.lsn <= disk_consistent_lsn),
    2477              :                     }
    2478              : 
    2479           48 :                     tracing::debug!(layer=%name, ?decision, "applied");
    2480              : 
    2481           48 :                     let layer = match decision {
    2482           48 :                         Resident { local, remote } => {
    2483           48 :                             total_physical_size += local.file_size;
    2484           48 :                             Layer::for_resident(conf, &this, local.local_path, name, remote)
    2485           48 :                                 .drop_eviction_guard()
    2486              :                         }
    2487            0 :                         Evicted(remote) => Layer::for_evicted(conf, &this, name, remote),
    2488              :                     };
    2489              : 
    2490           48 :                     loaded_layers.push(layer);
    2491              :                 }
    2492           18 :                 Ok((loaded_layers, needs_cleanup, total_physical_size))
    2493           18 :             }
    2494           18 :         })
    2495           16 :         .await
    2496           18 :         .map_err(anyhow::Error::new)
    2497           18 :         .and_then(|x| x)?;
    2498              : 
    2499           18 :         let num_layers = loaded_layers.len();
    2500           18 : 
    2501           18 :         guard
    2502           18 :             .open_mut()
    2503           18 :             .expect("layermanager must be open during init")
    2504           18 :             .initialize_local_layers(loaded_layers, disk_consistent_lsn + 1);
    2505           18 : 
    2506           18 :         self.remote_client
    2507           18 :             .schedule_layer_file_deletion(&needs_cleanup)?;
    2508           18 :         self.remote_client
    2509           18 :             .schedule_index_upload_for_file_changes()?;
    2510              :         // This barrier orders above DELETEs before any later operations.
    2511              :         // This is critical because code executing after the barrier might
    2512              :         // create again objects with the same key that we just scheduled for deletion.
    2513              :         // For example, if we just scheduled deletion of an image layer "from the future",
    2514              :         // later compaction might run again and re-create the same image layer.
    2515              :         // "from the future" here means an image layer whose LSN is > IndexPart::disk_consistent_lsn.
    2516              :         // "same" here means same key range and LSN.
    2517              :         //
    2518              :         // Without a barrier between above DELETEs and the re-creation's PUTs,
    2519              :         // the upload queue may execute the PUT first, then the DELETE.
    2520              :         // In our example, we will end up with an IndexPart referencing a non-existent object.
    2521              :         //
    2522              :         // 1. a future image layer is created and uploaded
    2523              :         // 2. ps restart
    2524              :         // 3. the future layer from (1) is deleted during load layer map
    2525              :         // 4. image layer is re-created and uploaded
    2526              :         // 5. deletion queue would like to delete (1) but actually deletes (4)
    2527              :         // 6. delete by name works as expected, but it now deletes the wrong (later) version
    2528              :         //
    2529              :         // See https://github.com/neondatabase/neon/issues/5878
    2530              :         //
    2531              :         // NB: generation numbers naturally protect against this because they disambiguate
    2532              :         //     (1) and (4)
    2533           18 :         self.remote_client.schedule_barrier()?;
    2534              :         // Tenant::create_timeline will wait for these uploads to happen before returning, or
    2535              :         // on retry.
    2536              : 
    2537              :         // Now that we have the full layer map, we may calculate the visibility of layers within it (a global scan)
    2538           18 :         drop(guard); // drop write lock, update_layer_visibility will take a read lock.
    2539           18 :         self.update_layer_visibility().await?;
    2540              : 
    2541           18 :         info!(
    2542            0 :             "loaded layer map with {} layers at {}, total physical size: {}",
    2543              :             num_layers, disk_consistent_lsn, total_physical_size
    2544              :         );
    2545              : 
    2546           18 :         timer.stop_and_record();
    2547           18 :         Ok(())
    2548           18 :     }
    2549              : 
    2550              :     /// Retrieve current logical size of the timeline.
    2551              :     ///
    2552              :     /// The size could be lagging behind the actual number, in case
    2553              :     /// the initial size calculation has not been run (gets triggered on the first size access).
    2554              :     ///
    2555              :     /// return size and boolean flag that shows if the size is exact
    2556            0 :     pub(crate) fn get_current_logical_size(
    2557            0 :         self: &Arc<Self>,
    2558            0 :         priority: GetLogicalSizePriority,
    2559            0 :         ctx: &RequestContext,
    2560            0 :     ) -> logical_size::CurrentLogicalSize {
    2561            0 :         if !self.tenant_shard_id.is_shard_zero() {
    2562              :             // Logical size is only accurately maintained on shard zero: when called elsewhere, for example
    2563              :             // when HTTP API is serving a GET for timeline zero, return zero
    2564            0 :             return logical_size::CurrentLogicalSize::Approximate(logical_size::Approximate::zero());
    2565            0 :         }
    2566            0 : 
    2567            0 :         let current_size = self.current_logical_size.current_size();
    2568            0 :         debug!("Current size: {current_size:?}");
    2569              : 
    2570            0 :         match (current_size.accuracy(), priority) {
    2571            0 :             (logical_size::Accuracy::Exact, _) => (), // nothing to do
    2572            0 :             (logical_size::Accuracy::Approximate, GetLogicalSizePriority::Background) => {
    2573            0 :                 // background task will eventually deliver an exact value, we're in no rush
    2574            0 :             }
    2575              :             (logical_size::Accuracy::Approximate, GetLogicalSizePriority::User) => {
    2576              :                 // background task is not ready, but user is asking for it now;
    2577              :                 // => make the background task skip the line
    2578              :                 // (The alternative would be to calculate the size here, but,
    2579              :                 //  it can actually take a long time if the user has a lot of rels.
    2580              :                 //  And we'll inevitable need it again; So, let the background task do the work.)
    2581            0 :                 match self
    2582            0 :                     .current_logical_size
    2583            0 :                     .cancel_wait_for_background_loop_concurrency_limit_semaphore
    2584            0 :                     .get()
    2585              :                 {
    2586            0 :                     Some(cancel) => cancel.cancel(),
    2587              :                     None => {
    2588            0 :                         let state = self.current_state();
    2589            0 :                         if matches!(
    2590            0 :                             state,
    2591              :                             TimelineState::Broken { .. } | TimelineState::Stopping
    2592            0 :                         ) {
    2593            0 : 
    2594            0 :                             // Can happen when timeline detail endpoint is used when deletion is ongoing (or its broken).
    2595            0 :                             // Don't make noise.
    2596            0 :                         } else {
    2597            0 :                             warn!("unexpected: cancel_wait_for_background_loop_concurrency_limit_semaphore not set, priority-boosting of logical size calculation will not work");
    2598            0 :                             debug_assert!(false);
    2599              :                         }
    2600              :                     }
    2601              :                 };
    2602              :             }
    2603              :         }
    2604              : 
    2605            0 :         if let CurrentLogicalSize::Approximate(_) = &current_size {
    2606            0 :             if ctx.task_kind() == TaskKind::WalReceiverConnectionHandler {
    2607            0 :                 let first = self
    2608            0 :                     .current_logical_size
    2609            0 :                     .did_return_approximate_to_walreceiver
    2610            0 :                     .compare_exchange(
    2611            0 :                         false,
    2612            0 :                         true,
    2613            0 :                         AtomicOrdering::Relaxed,
    2614            0 :                         AtomicOrdering::Relaxed,
    2615            0 :                     )
    2616            0 :                     .is_ok();
    2617            0 :                 if first {
    2618            0 :                     crate::metrics::initial_logical_size::TIMELINES_WHERE_WALRECEIVER_GOT_APPROXIMATE_SIZE.inc();
    2619            0 :                 }
    2620            0 :             }
    2621            0 :         }
    2622              : 
    2623            0 :         current_size
    2624            0 :     }
    2625              : 
    2626            0 :     fn spawn_initial_logical_size_computation_task(self: &Arc<Self>, ctx: &RequestContext) {
    2627            0 :         let Some(initial_part_end) = self.current_logical_size.initial_part_end else {
    2628              :             // nothing to do for freshly created timelines;
    2629            0 :             assert_eq!(
    2630            0 :                 self.current_logical_size.current_size().accuracy(),
    2631            0 :                 logical_size::Accuracy::Exact,
    2632            0 :             );
    2633            0 :             self.current_logical_size.initialized.add_permits(1);
    2634            0 :             return;
    2635              :         };
    2636              : 
    2637            0 :         let cancel_wait_for_background_loop_concurrency_limit_semaphore = CancellationToken::new();
    2638            0 :         let token = cancel_wait_for_background_loop_concurrency_limit_semaphore.clone();
    2639            0 :         self.current_logical_size
    2640            0 :             .cancel_wait_for_background_loop_concurrency_limit_semaphore.set(token)
    2641            0 :             .expect("initial logical size calculation task must be spawned exactly once per Timeline object");
    2642            0 : 
    2643            0 :         let self_clone = Arc::clone(self);
    2644            0 :         let background_ctx = ctx.detached_child(
    2645            0 :             TaskKind::InitialLogicalSizeCalculation,
    2646            0 :             DownloadBehavior::Download,
    2647            0 :         );
    2648            0 :         task_mgr::spawn(
    2649            0 :             task_mgr::BACKGROUND_RUNTIME.handle(),
    2650            0 :             task_mgr::TaskKind::InitialLogicalSizeCalculation,
    2651            0 :             self.tenant_shard_id,
    2652            0 :             Some(self.timeline_id),
    2653            0 :             "initial size calculation",
    2654              :             // NB: don't log errors here, task_mgr will do that.
    2655            0 :             async move {
    2656            0 :                 let cancel = task_mgr::shutdown_token();
    2657            0 :                 self_clone
    2658            0 :                     .initial_logical_size_calculation_task(
    2659            0 :                         initial_part_end,
    2660            0 :                         cancel_wait_for_background_loop_concurrency_limit_semaphore,
    2661            0 :                         cancel,
    2662            0 :                         background_ctx,
    2663            0 :                     )
    2664            0 :                     .await;
    2665            0 :                 Ok(())
    2666            0 :             }
    2667            0 :             .instrument(info_span!(parent: None, "initial_size_calculation", tenant_id=%self.tenant_shard_id.tenant_id, shard_id=%self.tenant_shard_id.shard_slug(), timeline_id=%self.timeline_id)),
    2668              :         );
    2669            0 :     }
    2670              : 
    2671            0 :     async fn initial_logical_size_calculation_task(
    2672            0 :         self: Arc<Self>,
    2673            0 :         initial_part_end: Lsn,
    2674            0 :         skip_concurrency_limiter: CancellationToken,
    2675            0 :         cancel: CancellationToken,
    2676            0 :         background_ctx: RequestContext,
    2677            0 :     ) {
    2678            0 :         scopeguard::defer! {
    2679            0 :             // Irrespective of the outcome of this operation, we should unblock anyone waiting for it.
    2680            0 :             self.current_logical_size.initialized.add_permits(1);
    2681            0 :         }
    2682            0 : 
    2683            0 :         let try_once = |attempt: usize| {
    2684            0 :             let background_ctx = &background_ctx;
    2685            0 :             let self_ref = &self;
    2686            0 :             let skip_concurrency_limiter = &skip_concurrency_limiter;
    2687            0 :             async move {
    2688            0 :                 let cancel = task_mgr::shutdown_token();
    2689            0 :                 let wait_for_permit = super::tasks::concurrent_background_tasks_rate_limit_permit(
    2690            0 :                     BackgroundLoopKind::InitialLogicalSizeCalculation,
    2691            0 :                     background_ctx,
    2692            0 :                 );
    2693              : 
    2694              :                 use crate::metrics::initial_logical_size::StartCircumstances;
    2695            0 :                 let (_maybe_permit, circumstances) = tokio::select! {
    2696            0 :                     permit = wait_for_permit => {
    2697            0 :                         (Some(permit), StartCircumstances::AfterBackgroundTasksRateLimit)
    2698              :                     }
    2699            0 :                     _ = self_ref.cancel.cancelled() => {
    2700            0 :                         return Err(CalculateLogicalSizeError::Cancelled);
    2701              :                     }
    2702            0 :                     _ = cancel.cancelled() => {
    2703            0 :                         return Err(CalculateLogicalSizeError::Cancelled);
    2704              :                     },
    2705            0 :                     () = skip_concurrency_limiter.cancelled() => {
    2706              :                         // Some action that is part of a end user interaction requested logical size
    2707              :                         // => break out of the rate limit
    2708              :                         // TODO: ideally we'd not run on BackgroundRuntime but the requester's runtime;
    2709              :                         // but then again what happens if they cancel; also, we should just be using
    2710              :                         // one runtime across the entire process, so, let's leave this for now.
    2711            0 :                         (None, StartCircumstances::SkippedConcurrencyLimiter)
    2712              :                     }
    2713              :                 };
    2714              : 
    2715            0 :                 let metrics_guard = if attempt == 1 {
    2716            0 :                     crate::metrics::initial_logical_size::START_CALCULATION.first(circumstances)
    2717              :                 } else {
    2718            0 :                     crate::metrics::initial_logical_size::START_CALCULATION.retry(circumstances)
    2719              :                 };
    2720              : 
    2721            0 :                 let calculated_size = self_ref
    2722            0 :                     .logical_size_calculation_task(
    2723            0 :                         initial_part_end,
    2724            0 :                         LogicalSizeCalculationCause::Initial,
    2725            0 :                         background_ctx,
    2726            0 :                     )
    2727            0 :                     .await?;
    2728              : 
    2729            0 :                 self_ref
    2730            0 :                     .trigger_aux_file_size_computation(initial_part_end, background_ctx)
    2731            0 :                     .await?;
    2732              : 
    2733              :                 // TODO: add aux file size to logical size
    2734              : 
    2735            0 :                 Ok((calculated_size, metrics_guard))
    2736            0 :             }
    2737            0 :         };
    2738              : 
    2739            0 :         let retrying = async {
    2740            0 :             let mut attempt = 0;
    2741              :             loop {
    2742            0 :                 attempt += 1;
    2743            0 : 
    2744            0 :                 match try_once(attempt).await {
    2745            0 :                     Ok(res) => return ControlFlow::Continue(res),
    2746            0 :                     Err(CalculateLogicalSizeError::Cancelled) => return ControlFlow::Break(()),
    2747              :                     Err(
    2748            0 :                         e @ (CalculateLogicalSizeError::Decode(_)
    2749            0 :                         | CalculateLogicalSizeError::PageRead(_)),
    2750            0 :                     ) => {
    2751            0 :                         warn!(attempt, "initial size calculation failed: {e:?}");
    2752              :                         // exponential back-off doesn't make sense at these long intervals;
    2753              :                         // use fixed retry interval with generous jitter instead
    2754            0 :                         let sleep_duration = Duration::from_secs(
    2755            0 :                             u64::try_from(
    2756            0 :                                 // 1hour base
    2757            0 :                                 (60_i64 * 60_i64)
    2758            0 :                                     // 10min jitter
    2759            0 :                                     + rand::thread_rng().gen_range(-10 * 60..10 * 60),
    2760            0 :                             )
    2761            0 :                             .expect("10min < 1hour"),
    2762            0 :                         );
    2763            0 :                         tokio::time::sleep(sleep_duration).await;
    2764              :                     }
    2765              :                 }
    2766              :             }
    2767            0 :         };
    2768              : 
    2769            0 :         let (calculated_size, metrics_guard) = tokio::select! {
    2770            0 :             res = retrying  => {
    2771            0 :                 match res {
    2772            0 :                     ControlFlow::Continue(calculated_size) => calculated_size,
    2773            0 :                     ControlFlow::Break(()) => return,
    2774              :                 }
    2775              :             }
    2776            0 :             _ = cancel.cancelled() => {
    2777            0 :                 return;
    2778              :             }
    2779              :         };
    2780              : 
    2781              :         // we cannot query current_logical_size.current_size() to know the current
    2782              :         // *negative* value, only truncated to u64.
    2783            0 :         let added = self
    2784            0 :             .current_logical_size
    2785            0 :             .size_added_after_initial
    2786            0 :             .load(AtomicOrdering::Relaxed);
    2787            0 : 
    2788            0 :         let sum = calculated_size.saturating_add_signed(added);
    2789            0 : 
    2790            0 :         // set the gauge value before it can be set in `update_current_logical_size`.
    2791            0 :         self.metrics.current_logical_size_gauge.set(sum);
    2792            0 : 
    2793            0 :         self.current_logical_size
    2794            0 :             .initial_logical_size
    2795            0 :             .set((calculated_size, metrics_guard.calculation_result_saved()))
    2796            0 :             .ok()
    2797            0 :             .expect("only this task sets it");
    2798            0 :     }
    2799              : 
    2800            0 :     pub(crate) fn spawn_ondemand_logical_size_calculation(
    2801            0 :         self: &Arc<Self>,
    2802            0 :         lsn: Lsn,
    2803            0 :         cause: LogicalSizeCalculationCause,
    2804            0 :         ctx: RequestContext,
    2805            0 :     ) -> oneshot::Receiver<Result<u64, CalculateLogicalSizeError>> {
    2806            0 :         let (sender, receiver) = oneshot::channel();
    2807            0 :         let self_clone = Arc::clone(self);
    2808            0 :         // XXX if our caller loses interest, i.e., ctx is cancelled,
    2809            0 :         // we should stop the size calculation work and return an error.
    2810            0 :         // That would require restructuring this function's API to
    2811            0 :         // return the result directly, instead of a Receiver for the result.
    2812            0 :         let ctx = ctx.detached_child(
    2813            0 :             TaskKind::OndemandLogicalSizeCalculation,
    2814            0 :             DownloadBehavior::Download,
    2815            0 :         );
    2816            0 :         task_mgr::spawn(
    2817            0 :             task_mgr::BACKGROUND_RUNTIME.handle(),
    2818            0 :             task_mgr::TaskKind::OndemandLogicalSizeCalculation,
    2819            0 :             self.tenant_shard_id,
    2820            0 :             Some(self.timeline_id),
    2821            0 :             "ondemand logical size calculation",
    2822            0 :             async move {
    2823            0 :                 let res = self_clone
    2824            0 :                     .logical_size_calculation_task(lsn, cause, &ctx)
    2825            0 :                     .await;
    2826            0 :                 let _ = sender.send(res).ok();
    2827            0 :                 Ok(()) // Receiver is responsible for handling errors
    2828            0 :             }
    2829            0 :             .in_current_span(),
    2830            0 :         );
    2831            0 :         receiver
    2832            0 :     }
    2833              : 
    2834              :     /// # Cancel-Safety
    2835              :     ///
    2836              :     /// This method is cancellation-safe.
    2837            0 :     #[instrument(skip_all)]
    2838              :     async fn logical_size_calculation_task(
    2839              :         self: &Arc<Self>,
    2840              :         lsn: Lsn,
    2841              :         cause: LogicalSizeCalculationCause,
    2842              :         ctx: &RequestContext,
    2843              :     ) -> Result<u64, CalculateLogicalSizeError> {
    2844              :         crate::span::debug_assert_current_span_has_tenant_and_timeline_id();
    2845              :         // We should never be calculating logical sizes on shard !=0, because these shards do not have
    2846              :         // accurate relation sizes, and they do not emit consumption metrics.
    2847              :         debug_assert!(self.tenant_shard_id.is_shard_zero());
    2848              : 
    2849              :         let guard = self
    2850              :             .gate
    2851              :             .enter()
    2852            0 :             .map_err(|_| CalculateLogicalSizeError::Cancelled)?;
    2853              : 
    2854              :         let self_calculation = Arc::clone(self);
    2855              : 
    2856            0 :         let mut calculation = pin!(async {
    2857            0 :             let ctx = ctx.attached_child();
    2858            0 :             self_calculation
    2859            0 :                 .calculate_logical_size(lsn, cause, &guard, &ctx)
    2860            0 :                 .await
    2861            0 :         });
    2862              : 
    2863              :         tokio::select! {
    2864              :             res = &mut calculation => { res }
    2865              :             _ = self.cancel.cancelled() => {
    2866              :                 debug!("cancelling logical size calculation for timeline shutdown");
    2867              :                 calculation.await
    2868              :             }
    2869              :         }
    2870              :     }
    2871              : 
    2872              :     /// Calculate the logical size of the database at the latest LSN.
    2873              :     ///
    2874              :     /// NOTE: counted incrementally, includes ancestors. This can be a slow operation,
    2875              :     /// especially if we need to download remote layers.
    2876              :     ///
    2877              :     /// # Cancel-Safety
    2878              :     ///
    2879              :     /// This method is cancellation-safe.
    2880            0 :     async fn calculate_logical_size(
    2881            0 :         &self,
    2882            0 :         up_to_lsn: Lsn,
    2883            0 :         cause: LogicalSizeCalculationCause,
    2884            0 :         _guard: &GateGuard,
    2885            0 :         ctx: &RequestContext,
    2886            0 :     ) -> Result<u64, CalculateLogicalSizeError> {
    2887            0 :         info!(
    2888            0 :             "Calculating logical size for timeline {} at {}",
    2889              :             self.timeline_id, up_to_lsn
    2890              :         );
    2891              : 
    2892            0 :         pausable_failpoint!("timeline-calculate-logical-size-pause");
    2893              : 
    2894              :         // See if we've already done the work for initial size calculation.
    2895              :         // This is a short-cut for timelines that are mostly unused.
    2896            0 :         if let Some(size) = self.current_logical_size.initialized_size(up_to_lsn) {
    2897            0 :             return Ok(size);
    2898            0 :         }
    2899            0 :         let storage_time_metrics = match cause {
    2900              :             LogicalSizeCalculationCause::Initial
    2901              :             | LogicalSizeCalculationCause::ConsumptionMetricsSyntheticSize
    2902            0 :             | LogicalSizeCalculationCause::TenantSizeHandler => &self.metrics.logical_size_histo,
    2903              :             LogicalSizeCalculationCause::EvictionTaskImitation => {
    2904            0 :                 &self.metrics.imitate_logical_size_histo
    2905              :             }
    2906              :         };
    2907            0 :         let timer = storage_time_metrics.start_timer();
    2908            0 :         let logical_size = self
    2909            0 :             .get_current_logical_size_non_incremental(up_to_lsn, ctx)
    2910            0 :             .await?;
    2911            0 :         debug!("calculated logical size: {logical_size}");
    2912            0 :         timer.stop_and_record();
    2913            0 :         Ok(logical_size)
    2914            0 :     }
    2915              : 
    2916              :     /// Update current logical size, adding `delta' to the old value.
    2917       811710 :     fn update_current_logical_size(&self, delta: i64) {
    2918       811710 :         let logical_size = &self.current_logical_size;
    2919       811710 :         logical_size.increment_size(delta);
    2920       811710 : 
    2921       811710 :         // Also set the value in the prometheus gauge. Note that
    2922       811710 :         // there is a race condition here: if this is is called by two
    2923       811710 :         // threads concurrently, the prometheus gauge might be set to
    2924       811710 :         // one value while current_logical_size is set to the
    2925       811710 :         // other.
    2926       811710 :         match logical_size.current_size() {
    2927       811710 :             CurrentLogicalSize::Exact(ref new_current_size) => self
    2928       811710 :                 .metrics
    2929       811710 :                 .current_logical_size_gauge
    2930       811710 :                 .set(new_current_size.into()),
    2931            0 :             CurrentLogicalSize::Approximate(_) => {
    2932            0 :                 // don't update the gauge yet, this allows us not to update the gauge back and
    2933            0 :                 // forth between the initial size calculation task.
    2934            0 :             }
    2935              :         }
    2936       811710 :     }
    2937              : 
    2938         8472 :     pub(crate) fn update_directory_entries_count(&self, kind: DirectoryKind, count: u64) {
    2939         8472 :         self.directory_metrics[kind.offset()].store(count, AtomicOrdering::Relaxed);
    2940         8472 :         let aux_metric =
    2941         8472 :             self.directory_metrics[DirectoryKind::AuxFiles.offset()].load(AtomicOrdering::Relaxed);
    2942         8472 : 
    2943         8472 :         let sum_of_entries = self
    2944         8472 :             .directory_metrics
    2945         8472 :             .iter()
    2946        59304 :             .map(|v| v.load(AtomicOrdering::Relaxed))
    2947         8472 :             .sum();
    2948              :         // Set a high general threshold and a lower threshold for the auxiliary files,
    2949              :         // as we can have large numbers of relations in the db directory.
    2950              :         const SUM_THRESHOLD: u64 = 5000;
    2951              :         const AUX_THRESHOLD: u64 = 1000;
    2952         8472 :         if sum_of_entries >= SUM_THRESHOLD || aux_metric >= AUX_THRESHOLD {
    2953            0 :             self.metrics
    2954            0 :                 .directory_entries_count_gauge
    2955            0 :                 .set(sum_of_entries);
    2956         8472 :         } else if let Some(metric) = Lazy::get(&self.metrics.directory_entries_count_gauge) {
    2957            0 :             metric.set(sum_of_entries);
    2958         8472 :         }
    2959         8472 :     }
    2960              : 
    2961            0 :     async fn find_layer(
    2962            0 :         &self,
    2963            0 :         layer_name: &LayerName,
    2964            0 :     ) -> Result<Option<Layer>, layer_manager::Shutdown> {
    2965            0 :         let guard = self.layers.read().await;
    2966            0 :         let layer = guard
    2967            0 :             .layer_map()?
    2968            0 :             .iter_historic_layers()
    2969            0 :             .find(|l| &l.layer_name() == layer_name)
    2970            0 :             .map(|found| guard.get_from_desc(&found));
    2971            0 :         Ok(layer)
    2972            0 :     }
    2973              : 
    2974              :     /// The timeline heatmap is a hint to secondary locations from the primary location,
    2975              :     /// indicating which layers are currently on-disk on the primary.
    2976              :     ///
    2977              :     /// None is returned if the Timeline is in a state where uploading a heatmap
    2978              :     /// doesn't make sense, such as shutting down or initializing.  The caller
    2979              :     /// should treat this as a cue to simply skip doing any heatmap uploading
    2980              :     /// for this timeline.
    2981            6 :     pub(crate) async fn generate_heatmap(&self) -> Option<HeatMapTimeline> {
    2982            6 :         if !self.is_active() {
    2983            0 :             return None;
    2984            6 :         }
    2985              : 
    2986            6 :         let guard = self.layers.read().await;
    2987              : 
    2988           30 :         let resident = guard.likely_resident_layers().filter_map(|layer| {
    2989           30 :             match layer.visibility() {
    2990              :                 LayerVisibilityHint::Visible => {
    2991              :                     // Layer is visible to one or more read LSNs: elegible for inclusion in layer map
    2992           24 :                     let last_activity_ts = layer.latest_activity();
    2993           24 :                     Some((layer.layer_desc(), layer.metadata(), last_activity_ts))
    2994              :                 }
    2995              :                 LayerVisibilityHint::Covered => {
    2996              :                     // Layer is resident but unlikely to be read: not elegible for inclusion in heatmap.
    2997            6 :                     None
    2998              :                 }
    2999              :             }
    3000           30 :         });
    3001            6 : 
    3002            6 :         let mut layers = resident.collect::<Vec<_>>();
    3003            6 : 
    3004            6 :         // Sort layers in order of which to download first.  For a large set of layers to download, we
    3005            6 :         // want to prioritize those layers which are most likely to still be in the resident many minutes
    3006            6 :         // or hours later:
    3007            6 :         // - Download L0s last, because they churn the fastest: L0s on a fast-writing tenant might
    3008            6 :         //   only exist for a few minutes before being compacted into L1s.
    3009            6 :         // - For L1 & image layers, download most recent LSNs first: the older the LSN, the sooner
    3010            6 :         //   the layer is likely to be covered by an image layer during compaction.
    3011           64 :         layers.sort_by_key(|(desc, _meta, _atime)| {
    3012           64 :             std::cmp::Reverse((
    3013           64 :                 !LayerMap::is_l0(&desc.key_range, desc.is_delta),
    3014           64 :                 desc.lsn_range.end,
    3015           64 :             ))
    3016           64 :         });
    3017            6 : 
    3018            6 :         let layers = layers
    3019            6 :             .into_iter()
    3020           24 :             .map(|(desc, meta, atime)| HeatMapLayer::new(desc.layer_name(), meta, atime))
    3021            6 :             .collect();
    3022            6 : 
    3023            6 :         Some(HeatMapTimeline::new(self.timeline_id, layers))
    3024            6 :     }
    3025              : 
    3026              :     /// Returns true if the given lsn is or was an ancestor branchpoint.
    3027            0 :     pub(crate) fn is_ancestor_lsn(&self, lsn: Lsn) -> bool {
    3028            0 :         // upon timeline detach, we set the ancestor_lsn to Lsn::INVALID and the store the original
    3029            0 :         // branchpoint in the value in IndexPart::lineage
    3030            0 :         self.ancestor_lsn == lsn
    3031            0 :             || (self.ancestor_lsn == Lsn::INVALID
    3032            0 :                 && self.remote_client.is_previous_ancestor_lsn(lsn))
    3033            0 :     }
    3034              : }
    3035              : 
    3036              : impl Timeline {
    3037              :     #[allow(unknown_lints)] // doc_lazy_continuation is still a new lint
    3038              :     #[allow(clippy::doc_lazy_continuation)]
    3039              :     /// Get the data needed to reconstruct all keys in the provided keyspace
    3040              :     ///
    3041              :     /// The algorithm is as follows:
    3042              :     /// 1.   While some keys are still not done and there's a timeline to visit:
    3043              :     /// 2.   Visit the timeline (see [`Timeline::get_vectored_reconstruct_data_timeline`]:
    3044              :     /// 2.1: Build the fringe for the current keyspace
    3045              :     /// 2.2  Visit the newest layer from the fringe to collect all values for the range it
    3046              :     ///      intersects
    3047              :     /// 2.3. Pop the timeline from the fringe
    3048              :     /// 2.4. If the fringe is empty, go back to 1
    3049      1880388 :     async fn get_vectored_reconstruct_data(
    3050      1880388 :         &self,
    3051      1880388 :         mut keyspace: KeySpace,
    3052      1880388 :         request_lsn: Lsn,
    3053      1880388 :         reconstruct_state: &mut ValuesReconstructState,
    3054      1880388 :         ctx: &RequestContext,
    3055      1880388 :     ) -> Result<(), GetVectoredError> {
    3056      1880388 :         let mut timeline_owned: Arc<Timeline>;
    3057      1880388 :         let mut timeline = self;
    3058      1880388 : 
    3059      1880388 :         let mut cont_lsn = Lsn(request_lsn.0 + 1);
    3060              : 
    3061      1880382 :         let missing_keyspace = loop {
    3062      2558806 :             if self.cancel.is_cancelled() {
    3063            0 :                 return Err(GetVectoredError::Cancelled);
    3064      2558806 :             }
    3065              : 
    3066              :             let TimelineVisitOutcome {
    3067      2558806 :                 completed_keyspace: completed,
    3068      2558806 :                 image_covered_keyspace,
    3069      2558806 :             } = Self::get_vectored_reconstruct_data_timeline(
    3070      2558806 :                 timeline,
    3071      2558806 :                 keyspace.clone(),
    3072      2558806 :                 cont_lsn,
    3073      2558806 :                 reconstruct_state,
    3074      2558806 :                 &self.cancel,
    3075      2558806 :                 ctx,
    3076      2558806 :             )
    3077       574741 :             .await?;
    3078              : 
    3079      2558806 :             keyspace.remove_overlapping_with(&completed);
    3080      2558806 : 
    3081      2558806 :             // Do not descend into the ancestor timeline for aux files.
    3082      2558806 :             // We don't return a blanket [`GetVectoredError::MissingKey`] to avoid
    3083      2558806 :             // stalling compaction.
    3084      2558806 :             keyspace.remove_overlapping_with(&KeySpace {
    3085      2558806 :                 ranges: vec![NON_INHERITED_RANGE, NON_INHERITED_SPARSE_RANGE],
    3086      2558806 :             });
    3087      2558806 : 
    3088      2558806 :             // Keyspace is fully retrieved
    3089      2558806 :             if keyspace.is_empty() {
    3090      1880340 :                 break None;
    3091       678466 :             }
    3092              : 
    3093       678466 :             let Some(ancestor_timeline) = timeline.ancestor_timeline.as_ref() else {
    3094              :                 // Not fully retrieved but no ancestor timeline.
    3095           42 :                 break Some(keyspace);
    3096              :             };
    3097              : 
    3098              :             // Now we see if there are keys covered by the image layer but does not exist in the
    3099              :             // image layer, which means that the key does not exist.
    3100              : 
    3101              :             // The block below will stop the vectored search if any of the keys encountered an image layer
    3102              :             // which did not contain a snapshot for said key. Since we have already removed all completed
    3103              :             // keys from `keyspace`, we expect there to be no overlap between it and the image covered key
    3104              :             // space. If that's not the case, we had at least one key encounter a gap in the image layer
    3105              :             // and stop the search as a result of that.
    3106       678424 :             let removed = keyspace.remove_overlapping_with(&image_covered_keyspace);
    3107       678424 :             if !removed.is_empty() {
    3108            0 :                 break Some(removed);
    3109       678424 :             }
    3110       678424 :             // If we reached this point, `remove_overlapping_with` should not have made any change to the
    3111       678424 :             // keyspace.
    3112       678424 : 
    3113       678424 :             // Take the min to avoid reconstructing a page with data newer than request Lsn.
    3114       678424 :             cont_lsn = std::cmp::min(Lsn(request_lsn.0 + 1), Lsn(timeline.ancestor_lsn.0 + 1));
    3115       678424 :             timeline_owned = timeline
    3116       678424 :                 .get_ready_ancestor_timeline(ancestor_timeline, ctx)
    3117            6 :                 .await?;
    3118       678418 :             timeline = &*timeline_owned;
    3119              :         };
    3120              : 
    3121      1880382 :         if let Some(missing_keyspace) = missing_keyspace {
    3122           42 :             return Err(GetVectoredError::MissingKey(MissingKeyError {
    3123           42 :                 key: missing_keyspace.start().unwrap(), /* better if we can store the full keyspace */
    3124           42 :                 shard: self
    3125           42 :                     .shard_identity
    3126           42 :                     .get_shard_number(&missing_keyspace.start().unwrap()),
    3127           42 :                 cont_lsn,
    3128           42 :                 request_lsn,
    3129           42 :                 ancestor_lsn: Some(timeline.ancestor_lsn),
    3130           42 :                 backtrace: None,
    3131           42 :             }));
    3132      1880340 :         }
    3133      1880340 : 
    3134      1880340 :         Ok(())
    3135      1880388 :     }
    3136              : 
    3137              :     /// Collect the reconstruct data for a keyspace from the specified timeline.
    3138              :     ///
    3139              :     /// Maintain a fringe [`LayerFringe`] which tracks all the layers that intersect
    3140              :     /// the current keyspace. The current keyspace of the search at any given timeline
    3141              :     /// is the original keyspace minus all the keys that have been completed minus
    3142              :     /// any keys for which we couldn't find an intersecting layer. It's not tracked explicitly,
    3143              :     /// but if you merge all the keyspaces in the fringe, you get the "current keyspace".
    3144              :     ///
    3145              :     /// This is basically a depth-first search visitor implementation where a vertex
    3146              :     /// is the (layer, lsn range, key space) tuple. The fringe acts as the stack.
    3147              :     ///
    3148              :     /// At each iteration pop the top of the fringe (the layer with the highest Lsn)
    3149              :     /// and get all the required reconstruct data from the layer in one go.
    3150              :     ///
    3151              :     /// Returns the completed keyspace and the keyspaces with image coverage. The caller
    3152              :     /// decides how to deal with these two keyspaces.
    3153      2558806 :     async fn get_vectored_reconstruct_data_timeline(
    3154      2558806 :         timeline: &Timeline,
    3155      2558806 :         keyspace: KeySpace,
    3156      2558806 :         mut cont_lsn: Lsn,
    3157      2558806 :         reconstruct_state: &mut ValuesReconstructState,
    3158      2558806 :         cancel: &CancellationToken,
    3159      2558806 :         ctx: &RequestContext,
    3160      2558806 :     ) -> Result<TimelineVisitOutcome, GetVectoredError> {
    3161      2558806 :         let mut unmapped_keyspace = keyspace.clone();
    3162      2558806 :         let mut fringe = LayerFringe::new();
    3163      2558806 : 
    3164      2558806 :         let mut completed_keyspace = KeySpace::default();
    3165      2558806 :         let mut image_covered_keyspace = KeySpaceRandomAccum::new();
    3166              : 
    3167              :         loop {
    3168      5017840 :             if cancel.is_cancelled() {
    3169            0 :                 return Err(GetVectoredError::Cancelled);
    3170      5017840 :             }
    3171      5017840 : 
    3172      5017840 :             let (keys_done_last_step, keys_with_image_coverage) =
    3173      5017840 :                 reconstruct_state.consume_done_keys();
    3174      5017840 :             unmapped_keyspace.remove_overlapping_with(&keys_done_last_step);
    3175      5017840 :             completed_keyspace.merge(&keys_done_last_step);
    3176      5017840 :             if let Some(keys_with_image_coverage) = keys_with_image_coverage {
    3177        24025 :                 unmapped_keyspace
    3178        24025 :                     .remove_overlapping_with(&KeySpace::single(keys_with_image_coverage.clone()));
    3179        24025 :                 image_covered_keyspace.add_range(keys_with_image_coverage);
    3180      4993815 :             }
    3181              : 
    3182              :             // Do not descent any further if the last layer we visited
    3183              :             // completed all keys in the keyspace it inspected. This is not
    3184              :             // required for correctness, but avoids visiting extra layers
    3185              :             // which turns out to be a perf bottleneck in some cases.
    3186      5017840 :             if !unmapped_keyspace.is_empty() {
    3187      3138976 :                 let guard = timeline.layers.read().await;
    3188      3138976 :                 let layers = guard.layer_map()?;
    3189              : 
    3190      3138976 :                 let in_memory_layer = layers.find_in_memory_layer(|l| {
    3191      2738353 :                     let start_lsn = l.get_lsn_range().start;
    3192      2738353 :                     cont_lsn > start_lsn
    3193      3138976 :                 });
    3194      3138976 : 
    3195      3138976 :                 match in_memory_layer {
    3196      1819412 :                     Some(l) => {
    3197      1819412 :                         let lsn_range = l.get_lsn_range().start..cont_lsn;
    3198      1819412 :                         fringe.update(
    3199      1819412 :                             ReadableLayer::InMemoryLayer(l),
    3200      1819412 :                             unmapped_keyspace.clone(),
    3201      1819412 :                             lsn_range,
    3202      1819412 :                         );
    3203      1819412 :                     }
    3204              :                     None => {
    3205      1535059 :                         for range in unmapped_keyspace.ranges.iter() {
    3206      1535059 :                             let results = layers.range_search(range.clone(), cont_lsn);
    3207      1535059 : 
    3208      1535059 :                             results
    3209      1535059 :                                 .found
    3210      1535059 :                                 .into_iter()
    3211      1535059 :                                 .map(|(SearchResult { layer, lsn_floor }, keyspace_accum)| {
    3212       831000 :                                     (
    3213       831000 :                                         ReadableLayer::PersistentLayer(guard.get_from_desc(&layer)),
    3214       831000 :                                         keyspace_accum.to_keyspace(),
    3215       831000 :                                         lsn_floor..cont_lsn,
    3216       831000 :                                     )
    3217      1535059 :                                 })
    3218      1535059 :                                 .for_each(|(layer, keyspace, lsn_range)| {
    3219       831000 :                                     fringe.update(layer, keyspace, lsn_range)
    3220      1535059 :                                 });
    3221      1535059 :                         }
    3222              :                     }
    3223              :                 }
    3224              : 
    3225              :                 // It's safe to drop the layer map lock after planning the next round of reads.
    3226              :                 // The fringe keeps readable handles for the layers which are safe to read even
    3227              :                 // if layers were compacted or flushed.
    3228              :                 //
    3229              :                 // The more interesting consideration is: "Why is the read algorithm still correct
    3230              :                 // if the layer map changes while it is operating?". Doing a vectored read on a
    3231              :                 // timeline boils down to pushing an imaginary lsn boundary downwards for each range
    3232              :                 // covered by the read. The layer map tells us how to move the lsn downwards for a
    3233              :                 // range at *a particular point in time*. It is fine for the answer to be different
    3234              :                 // at two different time points.
    3235      3138976 :                 drop(guard);
    3236      1878864 :             }
    3237              : 
    3238      5017840 :             if let Some((layer_to_read, keyspace_to_read, lsn_range)) = fringe.next_layer() {
    3239      2459034 :                 let next_cont_lsn = lsn_range.start;
    3240      2459034 :                 layer_to_read
    3241      2459034 :                     .get_values_reconstruct_data(
    3242      2459034 :                         keyspace_to_read.clone(),
    3243      2459034 :                         lsn_range,
    3244      2459034 :                         reconstruct_state,
    3245      2459034 :                         ctx,
    3246      2459034 :                     )
    3247       547268 :                     .await?;
    3248              : 
    3249      2459034 :                 unmapped_keyspace = keyspace_to_read;
    3250      2459034 :                 cont_lsn = next_cont_lsn;
    3251      2459034 : 
    3252      2459034 :                 reconstruct_state.on_layer_visited(&layer_to_read);
    3253              :             } else {
    3254      2558806 :                 break;
    3255      2558806 :             }
    3256      2558806 :         }
    3257      2558806 : 
    3258      2558806 :         Ok(TimelineVisitOutcome {
    3259      2558806 :             completed_keyspace,
    3260      2558806 :             image_covered_keyspace: image_covered_keyspace.consume_keyspace(),
    3261      2558806 :         })
    3262      2558806 :     }
    3263              : 
    3264       678424 :     async fn get_ready_ancestor_timeline(
    3265       678424 :         &self,
    3266       678424 :         ancestor: &Arc<Timeline>,
    3267       678424 :         ctx: &RequestContext,
    3268       678424 :     ) -> Result<Arc<Timeline>, GetReadyAncestorError> {
    3269       678424 :         // It's possible that the ancestor timeline isn't active yet, or
    3270       678424 :         // is active but hasn't yet caught up to the branch point. Wait
    3271       678424 :         // for it.
    3272       678424 :         //
    3273       678424 :         // This cannot happen while the pageserver is running normally,
    3274       678424 :         // because you cannot create a branch from a point that isn't
    3275       678424 :         // present in the pageserver yet. However, we don't wait for the
    3276       678424 :         // branch point to be uploaded to cloud storage before creating
    3277       678424 :         // a branch. I.e., the branch LSN need not be remote consistent
    3278       678424 :         // for the branching operation to succeed.
    3279       678424 :         //
    3280       678424 :         // Hence, if we try to load a tenant in such a state where
    3281       678424 :         // 1. the existence of the branch was persisted (in IndexPart and/or locally)
    3282       678424 :         // 2. but the ancestor state is behind branch_lsn because it was not yet persisted
    3283       678424 :         // then we will need to wait for the ancestor timeline to
    3284       678424 :         // re-stream WAL up to branch_lsn before we access it.
    3285       678424 :         //
    3286       678424 :         // How can a tenant get in such a state?
    3287       678424 :         // - ungraceful pageserver process exit
    3288       678424 :         // - detach+attach => this is a bug, https://github.com/neondatabase/neon/issues/4219
    3289       678424 :         //
    3290       678424 :         // NB: this could be avoided by requiring
    3291       678424 :         //   branch_lsn >= remote_consistent_lsn
    3292       678424 :         // during branch creation.
    3293       678424 :         match ancestor.wait_to_become_active(ctx).await {
    3294       678418 :             Ok(()) => {}
    3295              :             Err(TimelineState::Stopping) => {
    3296              :                 // If an ancestor is stopping, it means the tenant is stopping: handle this the same as if this timeline was stopping.
    3297            0 :                 return Err(GetReadyAncestorError::Cancelled);
    3298              :             }
    3299            6 :             Err(state) => {
    3300            6 :                 return Err(GetReadyAncestorError::BadState {
    3301            6 :                     timeline_id: ancestor.timeline_id,
    3302            6 :                     state,
    3303            6 :                 });
    3304              :             }
    3305              :         }
    3306       678418 :         ancestor
    3307       678418 :             .wait_lsn(self.ancestor_lsn, WaitLsnWaiter::Timeline(self), ctx)
    3308            0 :             .await
    3309       678418 :             .map_err(|e| match e {
    3310            0 :                 e @ WaitLsnError::Timeout(_) => GetReadyAncestorError::AncestorLsnTimeout(e),
    3311            0 :                 WaitLsnError::Shutdown => GetReadyAncestorError::Cancelled,
    3312            0 :                 WaitLsnError::BadState(state) => GetReadyAncestorError::BadState {
    3313            0 :                     timeline_id: ancestor.timeline_id,
    3314            0 :                     state,
    3315            0 :                 },
    3316       678418 :             })?;
    3317              : 
    3318       678418 :         Ok(ancestor.clone())
    3319       678424 :     }
    3320              : 
    3321        16356 :     pub(crate) fn get_shard_identity(&self) -> &ShardIdentity {
    3322        16356 :         &self.shard_identity
    3323        16356 :     }
    3324              : 
    3325              :     #[inline(always)]
    3326            0 :     pub(crate) fn shard_timeline_id(&self) -> ShardTimelineId {
    3327            0 :         ShardTimelineId {
    3328            0 :             shard_index: ShardIndex {
    3329            0 :                 shard_number: self.shard_identity.number,
    3330            0 :                 shard_count: self.shard_identity.count,
    3331            0 :             },
    3332            0 :             timeline_id: self.timeline_id,
    3333            0 :         }
    3334            0 :     }
    3335              : 
    3336              :     /// Returns a non-frozen open in-memory layer for ingestion.
    3337              :     ///
    3338              :     /// Takes a witness of timeline writer state lock being held, because it makes no sense to call
    3339              :     /// this function without holding the mutex.
    3340         3810 :     async fn get_layer_for_write(
    3341         3810 :         &self,
    3342         3810 :         lsn: Lsn,
    3343         3810 :         _guard: &tokio::sync::MutexGuard<'_, Option<TimelineWriterState>>,
    3344         3810 :         ctx: &RequestContext,
    3345         3810 :     ) -> anyhow::Result<Arc<InMemoryLayer>> {
    3346         3810 :         let mut guard = self.layers.write().await;
    3347         3810 :         let gate_guard = self.gate.enter().context("enter gate for inmem layer")?;
    3348              : 
    3349         3810 :         let last_record_lsn = self.get_last_record_lsn();
    3350         3810 :         ensure!(
    3351         3810 :             lsn > last_record_lsn,
    3352            0 :             "cannot modify relation after advancing last_record_lsn (incoming_lsn={}, last_record_lsn={})",
    3353              :             lsn,
    3354              :             last_record_lsn,
    3355              :         );
    3356              : 
    3357         3810 :         let layer = guard
    3358         3810 :             .open_mut()?
    3359         3810 :             .get_layer_for_write(
    3360         3810 :                 lsn,
    3361         3810 :                 self.conf,
    3362         3810 :                 self.timeline_id,
    3363         3810 :                 self.tenant_shard_id,
    3364         3810 :                 gate_guard,
    3365         3810 :                 ctx,
    3366         3810 :             )
    3367         2154 :             .await?;
    3368         3810 :         Ok(layer)
    3369         3810 :     }
    3370              : 
    3371     15837228 :     pub(crate) fn finish_write(&self, new_lsn: Lsn) {
    3372     15837228 :         assert!(new_lsn.is_aligned());
    3373              : 
    3374     15837228 :         self.metrics.last_record_gauge.set(new_lsn.0 as i64);
    3375     15837228 :         self.last_record_lsn.advance(new_lsn);
    3376     15837228 :     }
    3377              : 
    3378              :     /// Freeze any existing open in-memory layer and unconditionally notify the flush loop.
    3379              :     ///
    3380              :     /// Unconditional flush loop notification is given because in sharded cases we will want to
    3381              :     /// leave an Lsn gap. Unsharded tenants do not have Lsn gaps.
    3382         3504 :     async fn freeze_inmem_layer_at(
    3383         3504 :         &self,
    3384         3504 :         at: Lsn,
    3385         3504 :         write_lock: &mut tokio::sync::MutexGuard<'_, Option<TimelineWriterState>>,
    3386         3504 :     ) -> Result<u64, FlushLayerError> {
    3387         3504 :         let frozen = {
    3388         3504 :             let mut guard = self.layers.write().await;
    3389         3504 :             guard
    3390         3504 :                 .open_mut()?
    3391         3504 :                 .try_freeze_in_memory_layer(at, &self.last_freeze_at, write_lock)
    3392            6 :                 .await
    3393              :         };
    3394              : 
    3395         3504 :         if frozen {
    3396         3420 :             let now = Instant::now();
    3397         3420 :             *(self.last_freeze_ts.write().unwrap()) = now;
    3398         3420 :         }
    3399              : 
    3400              :         // Increment the flush cycle counter and wake up the flush task.
    3401              :         // Remember the new value, so that when we listen for the flush
    3402              :         // to finish, we know when the flush that we initiated has
    3403              :         // finished, instead of some other flush that was started earlier.
    3404         3504 :         let mut my_flush_request = 0;
    3405         3504 : 
    3406         3504 :         let flush_loop_state = { *self.flush_loop_state.lock().unwrap() };
    3407         3504 :         if !matches!(flush_loop_state, FlushLoopState::Running { .. }) {
    3408            0 :             return Err(FlushLayerError::NotRunning(flush_loop_state));
    3409         3504 :         }
    3410         3504 : 
    3411         3504 :         self.layer_flush_start_tx.send_modify(|(counter, lsn)| {
    3412         3504 :             my_flush_request = *counter + 1;
    3413         3504 :             *counter = my_flush_request;
    3414         3504 :             *lsn = std::cmp::max(at, *lsn);
    3415         3504 :         });
    3416         3504 : 
    3417         3504 :         assert_ne!(my_flush_request, 0);
    3418              : 
    3419         3504 :         Ok(my_flush_request)
    3420         3504 :     }
    3421              : 
    3422              :     /// Layer flusher task's main loop.
    3423         1218 :     async fn flush_loop(
    3424         1218 :         self: &Arc<Self>,
    3425         1218 :         mut layer_flush_start_rx: tokio::sync::watch::Receiver<(u64, Lsn)>,
    3426         1218 :         ctx: &RequestContext,
    3427         1218 :     ) {
    3428         1218 :         info!("started flush loop");
    3429              :         loop {
    3430         4602 :             tokio::select! {
    3431         4601 :                 _ = self.cancel.cancelled() => {
    3432           24 :                     info!("shutting down layer flush task due to Timeline::cancel");
    3433           24 :                     break;
    3434              :                 },
    3435         4601 :                 _ = layer_flush_start_rx.changed() => {}
    3436         3384 :             }
    3437         3384 :             trace!("waking up");
    3438         3384 :             let (flush_counter, frozen_to_lsn) = *layer_flush_start_rx.borrow();
    3439         3384 : 
    3440         3384 :             // The highest LSN to which we flushed in the loop over frozen layers
    3441         3384 :             let mut flushed_to_lsn = Lsn(0);
    3442              : 
    3443         3384 :             let result = loop {
    3444         6804 :                 if self.cancel.is_cancelled() {
    3445            0 :                     info!("dropping out of flush loop for timeline shutdown");
    3446              :                     // Note: we do not bother transmitting into [`layer_flush_done_tx`], because
    3447              :                     // anyone waiting on that will respect self.cancel as well: they will stop
    3448              :                     // waiting at the same time we as drop out of this loop.
    3449            0 :                     return;
    3450         6804 :                 }
    3451         6804 : 
    3452         6804 :                 let timer = self.metrics.flush_time_histo.start_timer();
    3453              : 
    3454         6804 :                 let layer_to_flush = {
    3455         6804 :                     let guard = self.layers.read().await;
    3456         6804 :                     let Ok(lm) = guard.layer_map() else {
    3457            0 :                         info!("dropping out of flush loop for timeline shutdown");
    3458            0 :                         return;
    3459              :                     };
    3460         6804 :                     lm.frozen_layers.front().cloned()
    3461              :                     // drop 'layers' lock to allow concurrent reads and writes
    3462              :                 };
    3463         6804 :                 let Some(layer_to_flush) = layer_to_flush else {
    3464         3384 :                     break Ok(());
    3465              :                 };
    3466        51199 :                 match self.flush_frozen_layer(layer_to_flush, ctx).await {
    3467         3420 :                     Ok(this_layer_to_lsn) => {
    3468         3420 :                         flushed_to_lsn = std::cmp::max(flushed_to_lsn, this_layer_to_lsn);
    3469         3420 :                     }
    3470              :                     Err(FlushLayerError::Cancelled) => {
    3471            0 :                         info!("dropping out of flush loop for timeline shutdown");
    3472            0 :                         return;
    3473              :                     }
    3474            0 :                     err @ Err(
    3475            0 :                         FlushLayerError::NotRunning(_)
    3476            0 :                         | FlushLayerError::Other(_)
    3477            0 :                         | FlushLayerError::CreateImageLayersError(_),
    3478            0 :                     ) => {
    3479            0 :                         error!("could not flush frozen layer: {err:?}");
    3480            0 :                         break err.map(|_| ());
    3481              :                     }
    3482              :                 }
    3483         3420 :                 timer.stop_and_record();
    3484              :             };
    3485              : 
    3486              :             // Unsharded tenants should never advance their LSN beyond the end of the
    3487              :             // highest layer they write: such gaps between layer data and the frozen LSN
    3488              :             // are only legal on sharded tenants.
    3489         3384 :             debug_assert!(
    3490         3384 :                 self.shard_identity.count.count() > 1
    3491         3384 :                     || flushed_to_lsn >= frozen_to_lsn
    3492          204 :                     || !flushed_to_lsn.is_valid()
    3493              :             );
    3494              : 
    3495         3384 :             if flushed_to_lsn < frozen_to_lsn && self.shard_identity.count.count() > 1 {
    3496              :                 // If our layer flushes didn't carry disk_consistent_lsn up to the `to_lsn` advertised
    3497              :                 // to us via layer_flush_start_rx, then advance it here.
    3498              :                 //
    3499              :                 // This path is only taken for tenants with multiple shards: single sharded tenants should
    3500              :                 // never encounter a gap in the wal.
    3501            0 :                 let old_disk_consistent_lsn = self.disk_consistent_lsn.load();
    3502            0 :                 tracing::debug!("Advancing disk_consistent_lsn across layer gap {old_disk_consistent_lsn}->{frozen_to_lsn}");
    3503            0 :                 if self.set_disk_consistent_lsn(frozen_to_lsn) {
    3504            0 :                     if let Err(e) = self.schedule_uploads(frozen_to_lsn, vec![]) {
    3505            0 :                         tracing::warn!("Failed to schedule metadata upload after updating disk_consistent_lsn: {e}");
    3506            0 :                     }
    3507            0 :                 }
    3508         3384 :             }
    3509              : 
    3510              :             // Notify any listeners that we're done
    3511         3384 :             let _ = self
    3512         3384 :                 .layer_flush_done_tx
    3513         3384 :                 .send_replace((flush_counter, result));
    3514              :         }
    3515           24 :     }
    3516              : 
    3517              :     /// Waits any flush request created by [`Self::freeze_inmem_layer_at`] to complete.
    3518         3264 :     async fn wait_flush_completion(&self, request: u64) -> Result<(), FlushLayerError> {
    3519         3264 :         let mut rx = self.layer_flush_done_tx.subscribe();
    3520              :         loop {
    3521              :             {
    3522         6525 :                 let (last_result_counter, last_result) = &*rx.borrow();
    3523         6525 :                 if *last_result_counter >= request {
    3524         3264 :                     if let Err(err) = last_result {
    3525              :                         // We already logged the original error in
    3526              :                         // flush_loop. We cannot propagate it to the caller
    3527              :                         // here, because it might not be Cloneable
    3528            0 :                         return Err(err.clone());
    3529              :                     } else {
    3530         3264 :                         return Ok(());
    3531              :                     }
    3532         3261 :                 }
    3533         3261 :             }
    3534         3261 :             trace!("waiting for flush to complete");
    3535         3261 :             tokio::select! {
    3536         3261 :                 rx_e = rx.changed() => {
    3537         3261 :                     rx_e.map_err(|_| FlushLayerError::NotRunning(*self.flush_loop_state.lock().unwrap()))?;
    3538              :                 },
    3539              :                 // Cancellation safety: we are not leaving an I/O in-flight for the flush, we're just ignoring
    3540              :                 // the notification from [`flush_loop`] that it completed.
    3541         3261 :                 _ = self.cancel.cancelled() => {
    3542            0 :                     tracing::info!("Cancelled layer flush due on timeline shutdown");
    3543            0 :                     return Ok(())
    3544              :                 }
    3545              :             };
    3546         3261 :             trace!("done")
    3547              :         }
    3548         3264 :     }
    3549              : 
    3550              :     /// Flush one frozen in-memory layer to disk, as a new delta layer.
    3551              :     ///
    3552              :     /// Return value is the last lsn (inclusive) of the layer that was frozen.
    3553         3420 :     #[instrument(skip_all, fields(layer=%frozen_layer))]
    3554              :     async fn flush_frozen_layer(
    3555              :         self: &Arc<Self>,
    3556              :         frozen_layer: Arc<InMemoryLayer>,
    3557              :         ctx: &RequestContext,
    3558              :     ) -> Result<Lsn, FlushLayerError> {
    3559              :         debug_assert_current_span_has_tenant_and_timeline_id();
    3560              : 
    3561              :         // As a special case, when we have just imported an image into the repository,
    3562              :         // instead of writing out a L0 delta layer, we directly write out image layer
    3563              :         // files instead. This is possible as long as *all* the data imported into the
    3564              :         // repository have the same LSN.
    3565              :         let lsn_range = frozen_layer.get_lsn_range();
    3566              : 
    3567              :         // Whether to directly create image layers for this flush, or flush them as delta layers
    3568              :         let create_image_layer =
    3569              :             lsn_range.start == self.initdb_lsn && lsn_range.end == Lsn(self.initdb_lsn.0 + 1);
    3570              : 
    3571              :         #[cfg(test)]
    3572              :         {
    3573              :             match &mut *self.flush_loop_state.lock().unwrap() {
    3574              :                 FlushLoopState::NotStarted | FlushLoopState::Exited => {
    3575              :                     panic!("flush loop not running")
    3576              :                 }
    3577              :                 FlushLoopState::Running {
    3578              :                     expect_initdb_optimization,
    3579              :                     initdb_optimization_count,
    3580              :                     ..
    3581              :                 } => {
    3582              :                     if create_image_layer {
    3583              :                         *initdb_optimization_count += 1;
    3584              :                     } else {
    3585              :                         assert!(!*expect_initdb_optimization, "expected initdb optimization");
    3586              :                     }
    3587              :                 }
    3588              :             }
    3589              :         }
    3590              : 
    3591              :         let (layers_to_upload, delta_layer_to_add) = if create_image_layer {
    3592              :             // Note: The 'ctx' in use here has DownloadBehavior::Error. We should not
    3593              :             // require downloading anything during initial import.
    3594              :             let ((rel_partition, metadata_partition), _lsn) = self
    3595              :                 .repartition(
    3596              :                     self.initdb_lsn,
    3597              :                     self.get_compaction_target_size(),
    3598              :                     EnumSet::empty(),
    3599              :                     ctx,
    3600              :                 )
    3601              :                 .await
    3602            0 :                 .map_err(|e| FlushLayerError::from_anyhow(self, e))?;
    3603              : 
    3604              :             if self.cancel.is_cancelled() {
    3605              :                 return Err(FlushLayerError::Cancelled);
    3606              :             }
    3607              : 
    3608              :             let mut layers_to_upload = Vec::new();
    3609              :             layers_to_upload.extend(
    3610              :                 self.create_image_layers(
    3611              :                     &rel_partition,
    3612              :                     self.initdb_lsn,
    3613              :                     ImageLayerCreationMode::Initial,
    3614              :                     ctx,
    3615              :                 )
    3616              :                 .await?,
    3617              :             );
    3618              :             if !metadata_partition.parts.is_empty() {
    3619              :                 assert_eq!(
    3620              :                     metadata_partition.parts.len(),
    3621              :                     1,
    3622              :                     "currently sparse keyspace should only contain a single metadata keyspace"
    3623              :                 );
    3624              :                 layers_to_upload.extend(
    3625              :                     self.create_image_layers(
    3626              :                         // Safety: create_image_layers treat sparse keyspaces differently that it does not scan
    3627              :                         // every single key within the keyspace, and therefore, it's safe to force converting it
    3628              :                         // into a dense keyspace before calling this function.
    3629              :                         &metadata_partition.into_dense(),
    3630              :                         self.initdb_lsn,
    3631              :                         ImageLayerCreationMode::Initial,
    3632              :                         ctx,
    3633              :                     )
    3634              :                     .await?,
    3635              :                 );
    3636              :             }
    3637              : 
    3638              :             (layers_to_upload, None)
    3639              :         } else {
    3640              :             // Normal case, write out a L0 delta layer file.
    3641              :             // `create_delta_layer` will not modify the layer map.
    3642              :             // We will remove frozen layer and add delta layer in one atomic operation later.
    3643              :             let Some(layer) = self
    3644              :                 .create_delta_layer(&frozen_layer, None, ctx)
    3645              :                 .await
    3646            0 :                 .map_err(|e| FlushLayerError::from_anyhow(self, e))?
    3647              :             else {
    3648              :                 panic!("delta layer cannot be empty if no filter is applied");
    3649              :             };
    3650              :             (
    3651              :                 // FIXME: even though we have a single image and single delta layer assumption
    3652              :                 // we push them to vec
    3653              :                 vec![layer.clone()],
    3654              :                 Some(layer),
    3655              :             )
    3656              :         };
    3657              : 
    3658              :         pausable_failpoint!("flush-layer-cancel-after-writing-layer-out-pausable");
    3659              : 
    3660              :         if self.cancel.is_cancelled() {
    3661              :             return Err(FlushLayerError::Cancelled);
    3662              :         }
    3663              : 
    3664              :         let disk_consistent_lsn = Lsn(lsn_range.end.0 - 1);
    3665              : 
    3666              :         // The new on-disk layers are now in the layer map. We can remove the
    3667              :         // in-memory layer from the map now. The flushed layer is stored in
    3668              :         // the mapping in `create_delta_layer`.
    3669              :         {
    3670              :             let mut guard = self.layers.write().await;
    3671              : 
    3672              :             guard.open_mut()?.finish_flush_l0_layer(
    3673              :                 delta_layer_to_add.as_ref(),
    3674              :                 &frozen_layer,
    3675              :                 &self.metrics,
    3676              :             );
    3677              : 
    3678              :             if self.set_disk_consistent_lsn(disk_consistent_lsn) {
    3679              :                 // Schedule remote uploads that will reflect our new disk_consistent_lsn
    3680              :                 self.schedule_uploads(disk_consistent_lsn, layers_to_upload)
    3681            0 :                     .map_err(|e| FlushLayerError::from_anyhow(self, e))?;
    3682              :             }
    3683              :             // release lock on 'layers'
    3684              :         };
    3685              : 
    3686              :         // Backpressure mechanism: wait with continuation of the flush loop until we have uploaded all layer files.
    3687              :         // This makes us refuse ingest until the new layers have been persisted to the remote.
    3688              :         self.remote_client
    3689              :             .wait_completion()
    3690              :             .await
    3691            0 :             .map_err(|e| match e {
    3692              :                 WaitCompletionError::UploadQueueShutDownOrStopped
    3693              :                 | WaitCompletionError::NotInitialized(
    3694              :                     NotInitialized::ShuttingDown | NotInitialized::Stopped,
    3695            0 :                 ) => FlushLayerError::Cancelled,
    3696              :                 WaitCompletionError::NotInitialized(NotInitialized::Uninitialized) => {
    3697            0 :                     FlushLayerError::Other(anyhow!(e).into())
    3698              :                 }
    3699            0 :             })?;
    3700              : 
    3701              :         // FIXME: between create_delta_layer and the scheduling of the upload in `update_metadata_file`,
    3702              :         // a compaction can delete the file and then it won't be available for uploads any more.
    3703              :         // We still schedule the upload, resulting in an error, but ideally we'd somehow avoid this
    3704              :         // race situation.
    3705              :         // See https://github.com/neondatabase/neon/issues/4526
    3706              :         pausable_failpoint!("flush-frozen-pausable");
    3707              : 
    3708              :         // This failpoint is used by another test case `test_pageserver_recovery`.
    3709              :         fail_point!("flush-frozen-exit");
    3710              : 
    3711              :         Ok(Lsn(lsn_range.end.0 - 1))
    3712              :     }
    3713              : 
    3714              :     /// Return true if the value changed
    3715              :     ///
    3716              :     /// This function must only be used from the layer flush task.
    3717         3420 :     fn set_disk_consistent_lsn(&self, new_value: Lsn) -> bool {
    3718         3420 :         let old_value = self.disk_consistent_lsn.fetch_max(new_value);
    3719         3420 :         assert!(new_value >= old_value, "disk_consistent_lsn must be growing monotonously at runtime; current {old_value}, offered {new_value}");
    3720         3420 :         new_value != old_value
    3721         3420 :     }
    3722              : 
    3723              :     /// Update metadata file
    3724         3438 :     fn schedule_uploads(
    3725         3438 :         &self,
    3726         3438 :         disk_consistent_lsn: Lsn,
    3727         3438 :         layers_to_upload: impl IntoIterator<Item = ResidentLayer>,
    3728         3438 :     ) -> anyhow::Result<()> {
    3729         3438 :         // We can only save a valid 'prev_record_lsn' value on disk if we
    3730         3438 :         // flushed *all* in-memory changes to disk. We only track
    3731         3438 :         // 'prev_record_lsn' in memory for the latest processed record, so we
    3732         3438 :         // don't remember what the correct value that corresponds to some old
    3733         3438 :         // LSN is. But if we flush everything, then the value corresponding
    3734         3438 :         // current 'last_record_lsn' is correct and we can store it on disk.
    3735         3438 :         let RecordLsn {
    3736         3438 :             last: last_record_lsn,
    3737         3438 :             prev: prev_record_lsn,
    3738         3438 :         } = self.last_record_lsn.load();
    3739         3438 :         let ondisk_prev_record_lsn = if disk_consistent_lsn == last_record_lsn {
    3740         3199 :             Some(prev_record_lsn)
    3741              :         } else {
    3742          239 :             None
    3743              :         };
    3744              : 
    3745         3438 :         let update = crate::tenant::metadata::MetadataUpdate::new(
    3746         3438 :             disk_consistent_lsn,
    3747         3438 :             ondisk_prev_record_lsn,
    3748         3438 :             *self.latest_gc_cutoff_lsn.read(),
    3749         3438 :         );
    3750         3438 : 
    3751         3438 :         fail_point!("checkpoint-before-saving-metadata", |x| bail!(
    3752            0 :             "{}",
    3753            0 :             x.unwrap()
    3754         3438 :         ));
    3755              : 
    3756         6894 :         for layer in layers_to_upload {
    3757         3456 :             self.remote_client.schedule_layer_file_upload(layer)?;
    3758              :         }
    3759         3438 :         self.remote_client
    3760         3438 :             .schedule_index_upload_for_metadata_update(&update)?;
    3761              : 
    3762         3438 :         Ok(())
    3763         3438 :     }
    3764              : 
    3765            0 :     pub(crate) async fn preserve_initdb_archive(&self) -> anyhow::Result<()> {
    3766            0 :         self.remote_client
    3767            0 :             .preserve_initdb_archive(
    3768            0 :                 &self.tenant_shard_id.tenant_id,
    3769            0 :                 &self.timeline_id,
    3770            0 :                 &self.cancel,
    3771            0 :             )
    3772            0 :             .await
    3773            0 :     }
    3774              : 
    3775              :     // Write out the given frozen in-memory layer as a new L0 delta file. This L0 file will not be tracked
    3776              :     // in layer map immediately. The caller is responsible to put it into the layer map.
    3777         2904 :     async fn create_delta_layer(
    3778         2904 :         self: &Arc<Self>,
    3779         2904 :         frozen_layer: &Arc<InMemoryLayer>,
    3780         2904 :         key_range: Option<Range<Key>>,
    3781         2904 :         ctx: &RequestContext,
    3782         2904 :     ) -> anyhow::Result<Option<ResidentLayer>> {
    3783         2904 :         let self_clone = Arc::clone(self);
    3784         2904 :         let frozen_layer = Arc::clone(frozen_layer);
    3785         2904 :         let ctx = ctx.attached_child();
    3786         2904 :         let work = async move {
    3787         2904 :             let Some((desc, path)) = frozen_layer
    3788         2904 :                 .write_to_disk(&ctx, key_range, self_clone.l0_flush_global_state.inner())
    3789        30753 :                 .await?
    3790              :             else {
    3791            0 :                 return Ok(None);
    3792              :             };
    3793         2904 :             let new_delta = Layer::finish_creating(self_clone.conf, &self_clone, desc, &path)?;
    3794              : 
    3795              :             // The write_to_disk() above calls writer.finish() which already did the fsync of the inodes.
    3796              :             // We just need to fsync the directory in which these inodes are linked,
    3797              :             // which we know to be the timeline directory.
    3798              :             //
    3799              :             // We use fatal_err() below because the after write_to_disk returns with success,
    3800              :             // the in-memory state of the filesystem already has the layer file in its final place,
    3801              :             // and subsequent pageserver code could think it's durable while it really isn't.
    3802         2904 :             let timeline_dir = VirtualFile::open(
    3803         2904 :                 &self_clone
    3804         2904 :                     .conf
    3805         2904 :                     .timeline_path(&self_clone.tenant_shard_id, &self_clone.timeline_id),
    3806         2904 :                 &ctx,
    3807         2904 :             )
    3808         1456 :             .await
    3809         2904 :             .fatal_err("VirtualFile::open for timeline dir fsync");
    3810         2904 :             timeline_dir
    3811         2904 :                 .sync_all()
    3812         1452 :                 .await
    3813         2904 :                 .fatal_err("VirtualFile::sync_all timeline dir");
    3814         2904 :             anyhow::Ok(Some(new_delta))
    3815         2904 :         };
    3816              :         // Before tokio-epoll-uring, we ran write_to_disk & the sync_all inside spawn_blocking.
    3817              :         // Preserve that behavior to maintain the same behavior for `virtual_file_io_engine=std-fs`.
    3818              :         use crate::virtual_file::io_engine::IoEngine;
    3819         2904 :         match crate::virtual_file::io_engine::get() {
    3820            0 :             IoEngine::NotSet => panic!("io engine not set"),
    3821              :             IoEngine::StdFs => {
    3822         1452 :                 let span = tracing::info_span!("blocking");
    3823         1452 :                 tokio::task::spawn_blocking({
    3824         1452 :                     move || Handle::current().block_on(work.instrument(span))
    3825         1452 :                 })
    3826         1452 :                 .await
    3827         1452 :                 .context("spawn_blocking")
    3828         1452 :                 .and_then(|x| x)
    3829              :             }
    3830              :             #[cfg(target_os = "linux")]
    3831        33646 :             IoEngine::TokioEpollUring => work.await,
    3832              :         }
    3833         2904 :     }
    3834              : 
    3835         1608 :     async fn repartition(
    3836         1608 :         &self,
    3837         1608 :         lsn: Lsn,
    3838         1608 :         partition_size: u64,
    3839         1608 :         flags: EnumSet<CompactFlags>,
    3840         1608 :         ctx: &RequestContext,
    3841         1608 :     ) -> anyhow::Result<((KeyPartitioning, SparseKeyPartitioning), Lsn)> {
    3842         1608 :         let Ok(mut partitioning_guard) = self.partitioning.try_lock() else {
    3843              :             // NB: there are two callers, one is the compaction task, of which there is only one per struct Tenant and hence Timeline.
    3844              :             // The other is the initdb optimization in flush_frozen_layer, used by `boostrap_timeline`, which runs before `.activate()`
    3845              :             // and hence before the compaction task starts.
    3846            0 :             anyhow::bail!("repartition() called concurrently, this should not happen");
    3847              :         };
    3848         1608 :         let ((dense_partition, sparse_partition), partition_lsn) = &*partitioning_guard;
    3849         1608 :         if lsn < *partition_lsn {
    3850            0 :             anyhow::bail!("repartition() called with LSN going backwards, this should not happen");
    3851         1608 :         }
    3852         1608 : 
    3853         1608 :         let distance = lsn.0 - partition_lsn.0;
    3854         1608 :         if *partition_lsn != Lsn(0)
    3855          786 :             && distance <= self.repartition_threshold
    3856          786 :             && !flags.contains(CompactFlags::ForceRepartition)
    3857              :         {
    3858          744 :             debug!(
    3859              :                 distance,
    3860              :                 threshold = self.repartition_threshold,
    3861            0 :                 "no repartitioning needed"
    3862              :             );
    3863          744 :             return Ok((
    3864          744 :                 (dense_partition.clone(), sparse_partition.clone()),
    3865          744 :                 *partition_lsn,
    3866          744 :             ));
    3867          864 :         }
    3868              : 
    3869        47982 :         let (dense_ks, sparse_ks) = self.collect_keyspace(lsn, ctx).await?;
    3870          864 :         let dense_partitioning = dense_ks.partition(&self.shard_identity, partition_size);
    3871          864 :         let sparse_partitioning = SparseKeyPartitioning {
    3872          864 :             parts: vec![sparse_ks],
    3873          864 :         }; // no partitioning for metadata keys for now
    3874          864 :         *partitioning_guard = ((dense_partitioning, sparse_partitioning), lsn);
    3875          864 : 
    3876          864 :         Ok((partitioning_guard.0.clone(), partitioning_guard.1))
    3877         1608 :     }
    3878              : 
    3879              :     // Is it time to create a new image layer for the given partition?
    3880           42 :     async fn time_for_new_image_layer(&self, partition: &KeySpace, lsn: Lsn) -> bool {
    3881           42 :         let threshold = self.get_image_creation_threshold();
    3882              : 
    3883           42 :         let guard = self.layers.read().await;
    3884           42 :         let Ok(layers) = guard.layer_map() else {
    3885            0 :             return false;
    3886              :         };
    3887              : 
    3888           42 :         let mut max_deltas = 0;
    3889           84 :         for part_range in &partition.ranges {
    3890           42 :             let image_coverage = layers.image_coverage(part_range, lsn);
    3891           84 :             for (img_range, last_img) in image_coverage {
    3892           42 :                 let img_lsn = if let Some(last_img) = last_img {
    3893            0 :                     last_img.get_lsn_range().end
    3894              :                 } else {
    3895           42 :                     Lsn(0)
    3896              :                 };
    3897              :                 // Let's consider an example:
    3898              :                 //
    3899              :                 // delta layer with LSN range 71-81
    3900              :                 // delta layer with LSN range 81-91
    3901              :                 // delta layer with LSN range 91-101
    3902              :                 // image layer at LSN 100
    3903              :                 //
    3904              :                 // If 'lsn' is still 100, i.e. no new WAL has been processed since the last image layer,
    3905              :                 // there's no need to create a new one. We check this case explicitly, to avoid passing
    3906              :                 // a bogus range to count_deltas below, with start > end. It's even possible that there
    3907              :                 // are some delta layers *later* than current 'lsn', if more WAL was processed and flushed
    3908              :                 // after we read last_record_lsn, which is passed here in the 'lsn' argument.
    3909           42 :                 if img_lsn < lsn {
    3910           42 :                     let num_deltas =
    3911           42 :                         layers.count_deltas(&img_range, &(img_lsn..lsn), Some(threshold));
    3912           42 : 
    3913           42 :                     max_deltas = max_deltas.max(num_deltas);
    3914           42 :                     if num_deltas >= threshold {
    3915            0 :                         debug!(
    3916            0 :                             "key range {}-{}, has {} deltas on this timeline in LSN range {}..{}",
    3917              :                             img_range.start, img_range.end, num_deltas, img_lsn, lsn
    3918              :                         );
    3919            0 :                         return true;
    3920           42 :                     }
    3921            0 :                 }
    3922              :             }
    3923              :         }
    3924              : 
    3925           42 :         debug!(
    3926              :             max_deltas,
    3927            0 :             "none of the partitioned ranges had >= {threshold} deltas"
    3928              :         );
    3929           42 :         false
    3930           42 :     }
    3931              : 
    3932              :     /// Create image layers for Postgres data. Assumes the caller passes a partition that is not too large,
    3933              :     /// so that at most one image layer will be produced from this function.
    3934          594 :     async fn create_image_layer_for_rel_blocks(
    3935          594 :         self: &Arc<Self>,
    3936          594 :         partition: &KeySpace,
    3937          594 :         mut image_layer_writer: ImageLayerWriter,
    3938          594 :         lsn: Lsn,
    3939          594 :         ctx: &RequestContext,
    3940          594 :         img_range: Range<Key>,
    3941          594 :         start: Key,
    3942          594 :     ) -> Result<ImageLayerCreationOutcome, CreateImageLayersError> {
    3943          594 :         let mut wrote_keys = false;
    3944          594 : 
    3945          594 :         let mut key_request_accum = KeySpaceAccum::new();
    3946         3948 :         for range in &partition.ranges {
    3947         3354 :             let mut key = range.start;
    3948         7272 :             while key < range.end {
    3949              :                 // Decide whether to retain this key: usually we do, but sharded tenants may
    3950              :                 // need to drop keys that don't belong to them.  If we retain the key, add it
    3951              :                 // to `key_request_accum` for later issuing a vectored get
    3952         3918 :                 if self.shard_identity.is_key_disposable(&key) {
    3953            0 :                     debug!(
    3954            0 :                         "Dropping key {} during compaction (it belongs on shard {:?})",
    3955            0 :                         key,
    3956            0 :                         self.shard_identity.get_shard_number(&key)
    3957              :                     );
    3958         3918 :                 } else {
    3959         3918 :                     key_request_accum.add_key(key);
    3960         3918 :                 }
    3961              : 
    3962         3918 :                 let last_key_in_range = key.next() == range.end;
    3963         3918 :                 key = key.next();
    3964         3918 : 
    3965         3918 :                 // Maybe flush `key_rest_accum`
    3966         3918 :                 if key_request_accum.raw_size() >= Timeline::MAX_GET_VECTORED_KEYS
    3967         3918 :                     || (last_key_in_range && key_request_accum.raw_size() > 0)
    3968              :                 {
    3969         3354 :                     let results = self
    3970         3354 :                         .get_vectored(key_request_accum.consume_keyspace(), lsn, ctx)
    3971          144 :                         .await?;
    3972              : 
    3973         3354 :                     if self.cancel.is_cancelled() {
    3974            0 :                         return Err(CreateImageLayersError::Cancelled);
    3975         3354 :                     }
    3976              : 
    3977         7272 :                     for (img_key, img) in results {
    3978         3918 :                         let img = match img {
    3979         3918 :                             Ok(img) => img,
    3980            0 :                             Err(err) => {
    3981            0 :                                 // If we fail to reconstruct a VM or FSM page, we can zero the
    3982            0 :                                 // page without losing any actual user data. That seems better
    3983            0 :                                 // than failing repeatedly and getting stuck.
    3984            0 :                                 //
    3985            0 :                                 // We had a bug at one point, where we truncated the FSM and VM
    3986            0 :                                 // in the pageserver, but the Postgres didn't know about that
    3987            0 :                                 // and continued to generate incremental WAL records for pages
    3988            0 :                                 // that didn't exist in the pageserver. Trying to replay those
    3989            0 :                                 // WAL records failed to find the previous image of the page.
    3990            0 :                                 // This special case allows us to recover from that situation.
    3991            0 :                                 // See https://github.com/neondatabase/neon/issues/2601.
    3992            0 :                                 //
    3993            0 :                                 // Unfortunately we cannot do this for the main fork, or for
    3994            0 :                                 // any metadata keys, keys, as that would lead to actual data
    3995            0 :                                 // loss.
    3996            0 :                                 if img_key.is_rel_fsm_block_key() || img_key.is_rel_vm_block_key() {
    3997            0 :                                     warn!("could not reconstruct FSM or VM key {img_key}, filling with zeros: {err:?}");
    3998            0 :                                     ZERO_PAGE.clone()
    3999              :                                 } else {
    4000            0 :                                     return Err(CreateImageLayersError::from(err));
    4001              :                                 }
    4002              :                             }
    4003              :                         };
    4004              : 
    4005              :                         // Write all the keys we just read into our new image layer.
    4006         4310 :                         image_layer_writer.put_image(img_key, img, ctx).await?;
    4007         3918 :                         wrote_keys = true;
    4008              :                     }
    4009          564 :                 }
    4010              :             }
    4011              :         }
    4012              : 
    4013          594 :         if wrote_keys {
    4014              :             // Normal path: we have written some data into the new image layer for this
    4015              :             // partition, so flush it to disk.
    4016         1206 :             let image_layer = image_layer_writer.finish(self, ctx).await?;
    4017          594 :             Ok(ImageLayerCreationOutcome {
    4018          594 :                 image: Some(image_layer),
    4019          594 :                 next_start_key: img_range.end,
    4020          594 :             })
    4021              :         } else {
    4022              :             // Special case: the image layer may be empty if this is a sharded tenant and the
    4023              :             // partition does not cover any keys owned by this shard.  In this case, to ensure
    4024              :             // we don't leave gaps between image layers, leave `start` where it is, so that the next
    4025              :             // layer we write will cover the key range that we just scanned.
    4026            0 :             tracing::debug!("no data in range {}-{}", img_range.start, img_range.end);
    4027            0 :             Ok(ImageLayerCreationOutcome {
    4028            0 :                 image: None,
    4029            0 :                 next_start_key: start,
    4030            0 :             })
    4031              :         }
    4032          594 :     }
    4033              : 
    4034              :     /// Create an image layer for metadata keys. This function produces one image layer for all metadata
    4035              :     /// keys for now. Because metadata keys cannot exceed basebackup size limit, the image layer for it
    4036              :     /// would not be too large to fit in a single image layer.
    4037              :     #[allow(clippy::too_many_arguments)]
    4038          564 :     async fn create_image_layer_for_metadata_keys(
    4039          564 :         self: &Arc<Self>,
    4040          564 :         partition: &KeySpace,
    4041          564 :         mut image_layer_writer: ImageLayerWriter,
    4042          564 :         lsn: Lsn,
    4043          564 :         ctx: &RequestContext,
    4044          564 :         img_range: Range<Key>,
    4045          564 :         mode: ImageLayerCreationMode,
    4046          564 :         start: Key,
    4047          564 :     ) -> Result<ImageLayerCreationOutcome, CreateImageLayersError> {
    4048          564 :         // Metadata keys image layer creation.
    4049          564 :         let mut reconstruct_state = ValuesReconstructState::default();
    4050          564 :         let data = self
    4051          564 :             .get_vectored_impl(partition.clone(), lsn, &mut reconstruct_state, ctx)
    4052         9721 :             .await?;
    4053          564 :         let (data, total_kb_retrieved, total_keys_retrieved) = {
    4054          564 :             let mut new_data = BTreeMap::new();
    4055          564 :             let mut total_kb_retrieved = 0;
    4056          564 :             let mut total_keys_retrieved = 0;
    4057        30600 :             for (k, v) in data {
    4058        30036 :                 let v = v?;
    4059        30036 :                 total_kb_retrieved += KEY_SIZE + v.len();
    4060        30036 :                 total_keys_retrieved += 1;
    4061        30036 :                 new_data.insert(k, v);
    4062              :             }
    4063          564 :             (new_data, total_kb_retrieved / 1024, total_keys_retrieved)
    4064          564 :         };
    4065          564 :         let delta_files_accessed = reconstruct_state.get_delta_layers_visited();
    4066          564 : 
    4067          564 :         let trigger_generation = delta_files_accessed as usize >= MAX_AUX_FILE_V2_DELTAS;
    4068          564 :         debug!(
    4069              :             trigger_generation,
    4070              :             delta_files_accessed,
    4071              :             total_kb_retrieved,
    4072              :             total_keys_retrieved,
    4073            0 :             "generate metadata images"
    4074              :         );
    4075              : 
    4076          564 :         if !trigger_generation && mode == ImageLayerCreationMode::Try {
    4077            6 :             return Ok(ImageLayerCreationOutcome {
    4078            6 :                 image: None,
    4079            6 :                 next_start_key: img_range.end,
    4080            6 :             });
    4081          558 :         }
    4082          558 :         if self.cancel.is_cancelled() {
    4083            0 :             return Err(CreateImageLayersError::Cancelled);
    4084          558 :         }
    4085          558 :         let mut wrote_any_image = false;
    4086        30594 :         for (k, v) in data {
    4087        30036 :             if v.is_empty() {
    4088              :                 // the key has been deleted, it does not need an image
    4089              :                 // in metadata keyspace, an empty image == tombstone
    4090           24 :                 continue;
    4091        30012 :             }
    4092        30012 :             wrote_any_image = true;
    4093        30012 : 
    4094        30012 :             // No need to handle sharding b/c metadata keys are always on the 0-th shard.
    4095        30012 : 
    4096        30012 :             // TODO: split image layers to avoid too large layer files. Too large image files are not handled
    4097        30012 :             // on the normal data path either.
    4098        30479 :             image_layer_writer.put_image(k, v, ctx).await?;
    4099              :         }
    4100              : 
    4101          558 :         if wrote_any_image {
    4102              :             // Normal path: we have written some data into the new image layer for this
    4103              :             // partition, so flush it to disk.
    4104           73 :             let image_layer = image_layer_writer.finish(self, ctx).await?;
    4105           36 :             Ok(ImageLayerCreationOutcome {
    4106           36 :                 image: Some(image_layer),
    4107           36 :                 next_start_key: img_range.end,
    4108           36 :             })
    4109              :         } else {
    4110              :             // Special case: the image layer may be empty if this is a sharded tenant and the
    4111              :             // partition does not cover any keys owned by this shard. In this case, to ensure
    4112              :             // we don't leave gaps between image layers, leave `start` where it is, so that the next
    4113              :             // layer we write will cover the key range that we just scanned.
    4114          522 :             tracing::debug!("no data in range {}-{}", img_range.start, img_range.end);
    4115          522 :             Ok(ImageLayerCreationOutcome {
    4116          522 :                 image: None,
    4117          522 :                 next_start_key: start,
    4118          522 :             })
    4119              :         }
    4120          564 :     }
    4121              : 
    4122              :     /// Predicate function which indicates whether we should check if new image layers
    4123              :     /// are required. Since checking if new image layers are required is expensive in
    4124              :     /// terms of CPU, we only do it in the following cases:
    4125              :     /// 1. If the timeline has ingested sufficient WAL to justify the cost
    4126              :     /// 2. If enough time has passed since the last check:
    4127              :     ///     1. For large tenants, we wish to perform the check more often since they
    4128              :     ///        suffer from the lack of image layers
    4129              :     ///     2. For small tenants (that can mostly fit in RAM), we use a much longer interval
    4130         2124 :     fn should_check_if_image_layers_required(self: &Arc<Timeline>, lsn: Lsn) -> bool {
    4131              :         const LARGE_TENANT_THRESHOLD: u64 = 2 * 1024 * 1024 * 1024;
    4132              : 
    4133         2124 :         let last_checks_at = self.last_image_layer_creation_check_at.load();
    4134         2124 :         let distance = lsn
    4135         2124 :             .checked_sub(last_checks_at)
    4136         2124 :             .expect("Attempt to compact with LSN going backwards");
    4137         2124 :         let min_distance =
    4138         2124 :             self.get_image_layer_creation_check_threshold() as u64 * self.get_checkpoint_distance();
    4139         2124 : 
    4140         2124 :         let distance_based_decision = distance.0 >= min_distance;
    4141         2124 : 
    4142         2124 :         let mut time_based_decision = false;
    4143         2124 :         let mut last_check_instant = self.last_image_layer_creation_check_instant.lock().unwrap();
    4144         2124 :         if let CurrentLogicalSize::Exact(logical_size) = self.current_logical_size.current_size() {
    4145         1818 :             let check_required_after = if Into::<u64>::into(&logical_size) >= LARGE_TENANT_THRESHOLD
    4146              :             {
    4147            0 :                 self.get_checkpoint_timeout()
    4148              :             } else {
    4149         1818 :                 Duration::from_secs(3600 * 48)
    4150              :             };
    4151              : 
    4152         1818 :             time_based_decision = match *last_check_instant {
    4153         1302 :                 Some(last_check) => {
    4154         1302 :                     let elapsed = last_check.elapsed();
    4155         1302 :                     elapsed >= check_required_after
    4156              :                 }
    4157          516 :                 None => true,
    4158              :             };
    4159          306 :         }
    4160              : 
    4161              :         // Do the expensive delta layer counting only if this timeline has ingested sufficient
    4162              :         // WAL since the last check or a checkpoint timeout interval has elapsed since the last
    4163              :         // check.
    4164         2124 :         let decision = distance_based_decision || time_based_decision;
    4165              : 
    4166         2124 :         if decision {
    4167          522 :             self.last_image_layer_creation_check_at.store(lsn);
    4168          522 :             *last_check_instant = Some(Instant::now());
    4169         1602 :         }
    4170              : 
    4171         2124 :         decision
    4172         2124 :     }
    4173              : 
    4174         2124 :     #[tracing::instrument(skip_all, fields(%lsn, %mode))]
    4175              :     async fn create_image_layers(
    4176              :         self: &Arc<Timeline>,
    4177              :         partitioning: &KeyPartitioning,
    4178              :         lsn: Lsn,
    4179              :         mode: ImageLayerCreationMode,
    4180              :         ctx: &RequestContext,
    4181              :     ) -> Result<Vec<ResidentLayer>, CreateImageLayersError> {
    4182              :         let timer = self.metrics.create_images_time_histo.start_timer();
    4183              :         let mut image_layers = Vec::new();
    4184              : 
    4185              :         // We need to avoid holes between generated image layers.
    4186              :         // Otherwise LayerMap::image_layer_exists will return false if key range of some layer is covered by more than one
    4187              :         // image layer with hole between them. In this case such layer can not be utilized by GC.
    4188              :         //
    4189              :         // How such hole between partitions can appear?
    4190              :         // if we have relation with relid=1 and size 100 and relation with relid=2 with size 200 then result of
    4191              :         // KeySpace::partition may contain partitions <100000000..100000099> and <200000000..200000199>.
    4192              :         // If there is delta layer <100000000..300000000> then it never be garbage collected because
    4193              :         // image layers  <100000000..100000099> and <200000000..200000199> are not completely covering it.
    4194              :         let mut start = Key::MIN;
    4195              : 
    4196              :         let check_for_image_layers = self.should_check_if_image_layers_required(lsn);
    4197              : 
    4198              :         for partition in partitioning.parts.iter() {
    4199              :             if self.cancel.is_cancelled() {
    4200              :                 return Err(CreateImageLayersError::Cancelled);
    4201              :             }
    4202              : 
    4203              :             let img_range = start..partition.ranges.last().unwrap().end;
    4204              :             let compact_metadata = partition.overlaps(&Key::metadata_key_range());
    4205              :             if compact_metadata {
    4206              :                 for range in &partition.ranges {
    4207              :                     assert!(
    4208              :                         range.start.field1 >= METADATA_KEY_BEGIN_PREFIX
    4209              :                             && range.end.field1 <= METADATA_KEY_END_PREFIX,
    4210              :                         "metadata keys must be partitioned separately"
    4211              :                     );
    4212              :                 }
    4213              :                 if mode == ImageLayerCreationMode::Try && !check_for_image_layers {
    4214              :                     // Skip compaction if there are not enough updates. Metadata compaction will do a scan and
    4215              :                     // might mess up with evictions.
    4216              :                     start = img_range.end;
    4217              :                     continue;
    4218              :                 }
    4219              :                 // For initial and force modes, we always generate image layers for metadata keys.
    4220              :             } else if let ImageLayerCreationMode::Try = mode {
    4221              :                 // check_for_image_layers = false -> skip
    4222              :                 // check_for_image_layers = true -> check time_for_new_image_layer -> skip/generate
    4223              :                 if !check_for_image_layers || !self.time_for_new_image_layer(partition, lsn).await {
    4224              :                     start = img_range.end;
    4225              :                     continue;
    4226              :                 }
    4227              :             }
    4228              :             if let ImageLayerCreationMode::Force = mode {
    4229              :                 // When forced to create image layers, we might try and create them where they already
    4230              :                 // exist.  This mode is only used in tests/debug.
    4231              :                 let layers = self.layers.read().await;
    4232              :                 if layers.contains_key(&PersistentLayerKey {
    4233              :                     key_range: img_range.clone(),
    4234              :                     lsn_range: PersistentLayerDesc::image_layer_lsn_range(lsn),
    4235              :                     is_delta: false,
    4236              :                 }) {
    4237              :                     tracing::info!(
    4238              :                         "Skipping image layer at {lsn} {}..{}, already exists",
    4239              :                         img_range.start,
    4240              :                         img_range.end
    4241              :                     );
    4242              :                     start = img_range.end;
    4243              :                     continue;
    4244              :                 }
    4245              :             }
    4246              : 
    4247              :             let image_layer_writer = ImageLayerWriter::new(
    4248              :                 self.conf,
    4249              :                 self.timeline_id,
    4250              :                 self.tenant_shard_id,
    4251              :                 &img_range,
    4252              :                 lsn,
    4253              :                 ctx,
    4254              :             )
    4255              :             .await?;
    4256              : 
    4257            0 :             fail_point!("image-layer-writer-fail-before-finish", |_| {
    4258            0 :                 Err(CreateImageLayersError::Other(anyhow::anyhow!(
    4259            0 :                     "failpoint image-layer-writer-fail-before-finish"
    4260            0 :                 )))
    4261            0 :             });
    4262              : 
    4263              :             if !compact_metadata {
    4264              :                 let ImageLayerCreationOutcome {
    4265              :                     image,
    4266              :                     next_start_key,
    4267              :                 } = self
    4268              :                     .create_image_layer_for_rel_blocks(
    4269              :                         partition,
    4270              :                         image_layer_writer,
    4271              :                         lsn,
    4272              :                         ctx,
    4273              :                         img_range,
    4274              :                         start,
    4275              :                     )
    4276              :                     .await?;
    4277              : 
    4278              :                 start = next_start_key;
    4279              :                 image_layers.extend(image);
    4280              :             } else {
    4281              :                 let ImageLayerCreationOutcome {
    4282              :                     image,
    4283              :                     next_start_key,
    4284              :                 } = self
    4285              :                     .create_image_layer_for_metadata_keys(
    4286              :                         partition,
    4287              :                         image_layer_writer,
    4288              :                         lsn,
    4289              :                         ctx,
    4290              :                         img_range,
    4291              :                         mode,
    4292              :                         start,
    4293              :                     )
    4294              :                     .await?;
    4295              :                 start = next_start_key;
    4296              :                 image_layers.extend(image);
    4297              :             }
    4298              :         }
    4299              : 
    4300              :         let mut guard = self.layers.write().await;
    4301              : 
    4302              :         // FIXME: we could add the images to be uploaded *before* returning from here, but right
    4303              :         // now they are being scheduled outside of write lock; current way is inconsistent with
    4304              :         // compaction lock order.
    4305              :         guard
    4306              :             .open_mut()?
    4307              :             .track_new_image_layers(&image_layers, &self.metrics);
    4308              :         drop_wlock(guard);
    4309              :         timer.stop_and_record();
    4310              : 
    4311              :         // Creating image layers may have caused some previously visible layers to be covered
    4312              :         self.update_layer_visibility().await?;
    4313              : 
    4314              :         Ok(image_layers)
    4315              :     }
    4316              : 
    4317              :     /// Wait until the background initial logical size calculation is complete, or
    4318              :     /// this Timeline is shut down.  Calling this function will cause the initial
    4319              :     /// logical size calculation to skip waiting for the background jobs barrier.
    4320            0 :     pub(crate) async fn await_initial_logical_size(self: Arc<Self>) {
    4321            0 :         if !self.shard_identity.is_shard_zero() {
    4322              :             // We don't populate logical size on shard >0: skip waiting for it.
    4323            0 :             return;
    4324            0 :         }
    4325            0 : 
    4326            0 :         if self.remote_client.is_deleting() {
    4327              :             // The timeline was created in a deletion-resume state, we don't expect logical size to be populated
    4328            0 :             return;
    4329            0 :         }
    4330            0 : 
    4331            0 :         if self.current_logical_size.current_size().is_exact() {
    4332              :             // root timelines are initialized with exact count, but never start the background
    4333              :             // calculation
    4334            0 :             return;
    4335            0 :         }
    4336              : 
    4337            0 :         if let Some(await_bg_cancel) = self
    4338            0 :             .current_logical_size
    4339            0 :             .cancel_wait_for_background_loop_concurrency_limit_semaphore
    4340            0 :             .get()
    4341            0 :         {
    4342            0 :             await_bg_cancel.cancel();
    4343            0 :         } else {
    4344              :             // We should not wait if we were not able to explicitly instruct
    4345              :             // the logical size cancellation to skip the concurrency limit semaphore.
    4346              :             // TODO: this is an unexpected case.  We should restructure so that it
    4347              :             // can't happen.
    4348            0 :             tracing::warn!(
    4349            0 :                 "await_initial_logical_size: can't get semaphore cancel token, skipping"
    4350              :             );
    4351            0 :             debug_assert!(false);
    4352              :         }
    4353              : 
    4354            0 :         tokio::select!(
    4355            0 :             _ = self.current_logical_size.initialized.acquire() => {},
    4356            0 :             _ = self.cancel.cancelled() => {}
    4357              :         )
    4358            0 :     }
    4359              : 
    4360              :     /// Detach this timeline from its ancestor by copying all of ancestors layers as this
    4361              :     /// Timelines layers up to the ancestor_lsn.
    4362              :     ///
    4363              :     /// Requires a timeline that:
    4364              :     /// - has an ancestor to detach from
    4365              :     /// - the ancestor does not have an ancestor -- follows from the original RFC limitations, not
    4366              :     ///   a technical requirement
    4367              :     ///
    4368              :     /// After the operation has been started, it cannot be canceled. Upon restart it needs to be
    4369              :     /// polled again until completion.
    4370              :     ///
    4371              :     /// During the operation all timelines sharing the data with this timeline will be reparented
    4372              :     /// from our ancestor to be branches of this timeline.
    4373            0 :     pub(crate) async fn prepare_to_detach_from_ancestor(
    4374            0 :         self: &Arc<Timeline>,
    4375            0 :         tenant: &crate::tenant::Tenant,
    4376            0 :         options: detach_ancestor::Options,
    4377            0 :         ctx: &RequestContext,
    4378            0 :     ) -> Result<detach_ancestor::Progress, detach_ancestor::Error> {
    4379            0 :         detach_ancestor::prepare(self, tenant, options, ctx).await
    4380            0 :     }
    4381              : 
    4382              :     /// Second step of detach from ancestor; detaches the `self` from it's current ancestor and
    4383              :     /// reparents any reparentable children of previous ancestor.
    4384              :     ///
    4385              :     /// This method is to be called while holding the TenantManager's tenant slot, so during this
    4386              :     /// method we cannot be deleted nor can any timeline be deleted. After this method returns
    4387              :     /// successfully, tenant must be reloaded.
    4388              :     ///
    4389              :     /// Final step will be to [`Self::complete_detaching_timeline_ancestor`] after optionally
    4390              :     /// resetting the tenant.
    4391            0 :     pub(crate) async fn detach_from_ancestor_and_reparent(
    4392            0 :         self: &Arc<Timeline>,
    4393            0 :         tenant: &crate::tenant::Tenant,
    4394            0 :         prepared: detach_ancestor::PreparedTimelineDetach,
    4395            0 :         ctx: &RequestContext,
    4396            0 :     ) -> Result<detach_ancestor::DetachingAndReparenting, detach_ancestor::Error> {
    4397            0 :         detach_ancestor::detach_and_reparent(self, tenant, prepared, ctx).await
    4398            0 :     }
    4399              : 
    4400              :     /// Final step which unblocks the GC.
    4401              :     ///
    4402              :     /// The tenant must've been reset if ancestry was modified previously (in tenant manager).
    4403            0 :     pub(crate) async fn complete_detaching_timeline_ancestor(
    4404            0 :         self: &Arc<Timeline>,
    4405            0 :         tenant: &crate::tenant::Tenant,
    4406            0 :         attempt: detach_ancestor::Attempt,
    4407            0 :         ctx: &RequestContext,
    4408            0 :     ) -> Result<(), detach_ancestor::Error> {
    4409            0 :         detach_ancestor::complete(self, tenant, attempt, ctx).await
    4410            0 :     }
    4411              : 
    4412              :     /// Switch aux file policy and schedule upload to the index part.
    4413           48 :     pub(crate) fn do_switch_aux_policy(&self, policy: AuxFilePolicy) -> anyhow::Result<()> {
    4414           48 :         self.last_aux_file_policy.store(Some(policy));
    4415           48 :         self.remote_client
    4416           48 :             .schedule_index_upload_for_aux_file_policy_update(Some(policy))?;
    4417           48 :         Ok(())
    4418           48 :     }
    4419              : }
    4420              : 
    4421              : impl Drop for Timeline {
    4422           24 :     fn drop(&mut self) {
    4423           24 :         if let Some(ancestor) = &self.ancestor_timeline {
    4424              :             // This lock should never be poisoned, but in case it is we do a .map() instead of
    4425              :             // an unwrap(), to avoid panicking in a destructor and thereby aborting the process.
    4426            6 :             if let Ok(mut gc_info) = ancestor.gc_info.write() {
    4427            6 :                 gc_info.remove_child(self.timeline_id)
    4428            0 :             }
    4429           18 :         }
    4430           24 :     }
    4431              : }
    4432              : 
    4433              : /// Top-level failure to compact.
    4434            0 : #[derive(Debug, thiserror::Error)]
    4435              : pub(crate) enum CompactionError {
    4436              :     #[error("The timeline or pageserver is shutting down")]
    4437              :     ShuttingDown,
    4438              :     /// Compaction cannot be done right now; page reconstruction and so on.
    4439              :     #[error(transparent)]
    4440              :     Other(anyhow::Error),
    4441              : }
    4442              : 
    4443              : impl From<CollectKeySpaceError> for CompactionError {
    4444            0 :     fn from(err: CollectKeySpaceError) -> Self {
    4445            0 :         match err {
    4446              :             CollectKeySpaceError::Cancelled
    4447              :             | CollectKeySpaceError::PageRead(PageReconstructError::Cancelled) => {
    4448            0 :                 CompactionError::ShuttingDown
    4449              :             }
    4450            0 :             e => CompactionError::Other(e.into()),
    4451              :         }
    4452            0 :     }
    4453              : }
    4454              : 
    4455              : impl From<super::upload_queue::NotInitialized> for CompactionError {
    4456            0 :     fn from(value: super::upload_queue::NotInitialized) -> Self {
    4457            0 :         match value {
    4458              :             super::upload_queue::NotInitialized::Uninitialized => {
    4459            0 :                 CompactionError::Other(anyhow::anyhow!(value))
    4460              :             }
    4461              :             super::upload_queue::NotInitialized::ShuttingDown
    4462            0 :             | super::upload_queue::NotInitialized::Stopped => CompactionError::ShuttingDown,
    4463              :         }
    4464            0 :     }
    4465              : }
    4466              : 
    4467              : impl From<super::storage_layer::layer::DownloadError> for CompactionError {
    4468            0 :     fn from(e: super::storage_layer::layer::DownloadError) -> Self {
    4469            0 :         match e {
    4470              :             super::storage_layer::layer::DownloadError::TimelineShutdown
    4471              :             | super::storage_layer::layer::DownloadError::DownloadCancelled => {
    4472            0 :                 CompactionError::ShuttingDown
    4473              :             }
    4474              :             super::storage_layer::layer::DownloadError::ContextAndConfigReallyDeniesDownloads
    4475              :             | super::storage_layer::layer::DownloadError::DownloadRequired
    4476              :             | super::storage_layer::layer::DownloadError::NotFile(_)
    4477              :             | super::storage_layer::layer::DownloadError::DownloadFailed
    4478              :             | super::storage_layer::layer::DownloadError::PreStatFailed(_) => {
    4479            0 :                 CompactionError::Other(anyhow::anyhow!(e))
    4480              :             }
    4481              :             #[cfg(test)]
    4482              :             super::storage_layer::layer::DownloadError::Failpoint(_) => {
    4483            0 :                 CompactionError::Other(anyhow::anyhow!(e))
    4484              :             }
    4485              :         }
    4486            0 :     }
    4487              : }
    4488              : 
    4489              : impl From<layer_manager::Shutdown> for CompactionError {
    4490            0 :     fn from(_: layer_manager::Shutdown) -> Self {
    4491            0 :         CompactionError::ShuttingDown
    4492            0 :     }
    4493              : }
    4494              : 
    4495              : #[serde_as]
    4496          588 : #[derive(serde::Serialize)]
    4497              : struct RecordedDuration(#[serde_as(as = "serde_with::DurationMicroSeconds")] Duration);
    4498              : 
    4499              : #[derive(Default)]
    4500              : enum DurationRecorder {
    4501              :     #[default]
    4502              :     NotStarted,
    4503              :     Recorded(RecordedDuration, tokio::time::Instant),
    4504              : }
    4505              : 
    4506              : impl DurationRecorder {
    4507         1512 :     fn till_now(&self) -> DurationRecorder {
    4508         1512 :         match self {
    4509              :             DurationRecorder::NotStarted => {
    4510            0 :                 panic!("must only call on recorded measurements")
    4511              :             }
    4512         1512 :             DurationRecorder::Recorded(_, ended) => {
    4513         1512 :                 let now = tokio::time::Instant::now();
    4514         1512 :                 DurationRecorder::Recorded(RecordedDuration(now - *ended), now)
    4515         1512 :             }
    4516         1512 :         }
    4517         1512 :     }
    4518          588 :     fn into_recorded(self) -> Option<RecordedDuration> {
    4519          588 :         match self {
    4520            0 :             DurationRecorder::NotStarted => None,
    4521          588 :             DurationRecorder::Recorded(recorded, _) => Some(recorded),
    4522              :         }
    4523          588 :     }
    4524              : }
    4525              : 
    4526              : /// Descriptor for a delta layer used in testing infra. The start/end key/lsn range of the
    4527              : /// delta layer might be different from the min/max key/lsn in the delta layer. Therefore,
    4528              : /// the layer descriptor requires the user to provide the ranges, which should cover all
    4529              : /// keys specified in the `data` field.
    4530              : #[cfg(test)]
    4531              : #[derive(Clone)]
    4532              : pub struct DeltaLayerTestDesc {
    4533              :     pub lsn_range: Range<Lsn>,
    4534              :     pub key_range: Range<Key>,
    4535              :     pub data: Vec<(Key, Lsn, Value)>,
    4536              : }
    4537              : 
    4538              : #[cfg(test)]
    4539              : impl DeltaLayerTestDesc {
    4540            6 :     pub fn new(lsn_range: Range<Lsn>, key_range: Range<Key>, data: Vec<(Key, Lsn, Value)>) -> Self {
    4541            6 :         Self {
    4542            6 :             lsn_range,
    4543            6 :             key_range,
    4544            6 :             data,
    4545            6 :         }
    4546            6 :     }
    4547              : 
    4548          168 :     pub fn new_with_inferred_key_range(
    4549          168 :         lsn_range: Range<Lsn>,
    4550          168 :         data: Vec<(Key, Lsn, Value)>,
    4551          168 :     ) -> Self {
    4552          372 :         let key_min = data.iter().map(|(key, _, _)| key).min().unwrap();
    4553          372 :         let key_max = data.iter().map(|(key, _, _)| key).max().unwrap();
    4554          168 :         Self {
    4555          168 :             key_range: (*key_min)..(key_max.next()),
    4556          168 :             lsn_range,
    4557          168 :             data,
    4558          168 :         }
    4559          168 :     }
    4560              : 
    4561           30 :     pub(crate) fn layer_name(&self) -> LayerName {
    4562           30 :         LayerName::Delta(super::storage_layer::DeltaLayerName {
    4563           30 :             key_range: self.key_range.clone(),
    4564           30 :             lsn_range: self.lsn_range.clone(),
    4565           30 :         })
    4566           30 :     }
    4567              : }
    4568              : 
    4569              : impl Timeline {
    4570           84 :     async fn finish_compact_batch(
    4571           84 :         self: &Arc<Self>,
    4572           84 :         new_deltas: &[ResidentLayer],
    4573           84 :         new_images: &[ResidentLayer],
    4574           84 :         layers_to_remove: &[Layer],
    4575           84 :     ) -> Result<(), CompactionError> {
    4576           84 :         let mut guard = tokio::select! {
    4577           84 :             guard = self.layers.write() => guard,
    4578           84 :             _ = self.cancel.cancelled() => {
    4579            0 :                 return Err(CompactionError::ShuttingDown);
    4580              :             }
    4581              :         };
    4582              : 
    4583           84 :         let mut duplicated_layers = HashSet::new();
    4584           84 : 
    4585           84 :         let mut insert_layers = Vec::with_capacity(new_deltas.len());
    4586              : 
    4587         1008 :         for l in new_deltas {
    4588          924 :             if guard.contains(l.as_ref()) {
    4589              :                 // expected in tests
    4590            0 :                 tracing::error!(layer=%l, "duplicated L1 layer");
    4591              : 
    4592              :                 // good ways to cause a duplicate: we repeatedly error after taking the writelock
    4593              :                 // `guard`  on self.layers. as of writing this, there are no error returns except
    4594              :                 // for compact_level0_phase1 creating an L0, which does not happen in practice
    4595              :                 // because we have not implemented L0 => L0 compaction.
    4596            0 :                 duplicated_layers.insert(l.layer_desc().key());
    4597          924 :             } else if LayerMap::is_l0(&l.layer_desc().key_range, l.layer_desc().is_delta) {
    4598            0 :                 return Err(CompactionError::Other(anyhow::anyhow!("compaction generates a L0 layer file as output, which will cause infinite compaction.")));
    4599          924 :             } else {
    4600          924 :                 insert_layers.push(l.clone());
    4601          924 :             }
    4602              :         }
    4603              : 
    4604              :         // only remove those inputs which were not outputs
    4605           84 :         let remove_layers: Vec<Layer> = layers_to_remove
    4606           84 :             .iter()
    4607         1206 :             .filter(|l| !duplicated_layers.contains(&l.layer_desc().key()))
    4608           84 :             .cloned()
    4609           84 :             .collect();
    4610           84 : 
    4611           84 :         if !new_images.is_empty() {
    4612            0 :             guard
    4613            0 :                 .open_mut()?
    4614            0 :                 .track_new_image_layers(new_images, &self.metrics);
    4615           84 :         }
    4616              : 
    4617           84 :         guard
    4618           84 :             .open_mut()?
    4619           84 :             .finish_compact_l0(&remove_layers, &insert_layers, &self.metrics);
    4620           84 : 
    4621           84 :         self.remote_client
    4622           84 :             .schedule_compaction_update(&remove_layers, new_deltas)?;
    4623              : 
    4624           84 :         drop_wlock(guard);
    4625           84 : 
    4626           84 :         Ok(())
    4627           84 :     }
    4628              : 
    4629            0 :     async fn rewrite_layers(
    4630            0 :         self: &Arc<Self>,
    4631            0 :         mut replace_layers: Vec<(Layer, ResidentLayer)>,
    4632            0 :         mut drop_layers: Vec<Layer>,
    4633            0 :     ) -> Result<(), CompactionError> {
    4634            0 :         let mut guard = self.layers.write().await;
    4635              : 
    4636              :         // Trim our lists in case our caller (compaction) raced with someone else (GC) removing layers: we want
    4637              :         // to avoid double-removing, and avoid rewriting something that was removed.
    4638            0 :         replace_layers.retain(|(l, _)| guard.contains(l));
    4639            0 :         drop_layers.retain(|l| guard.contains(l));
    4640            0 : 
    4641            0 :         guard
    4642            0 :             .open_mut()?
    4643            0 :             .rewrite_layers(&replace_layers, &drop_layers, &self.metrics);
    4644            0 : 
    4645            0 :         let upload_layers: Vec<_> = replace_layers.into_iter().map(|r| r.1).collect();
    4646            0 : 
    4647            0 :         self.remote_client
    4648            0 :             .schedule_compaction_update(&drop_layers, &upload_layers)?;
    4649              : 
    4650            0 :         Ok(())
    4651            0 :     }
    4652              : 
    4653              :     /// Schedules the uploads of the given image layers
    4654         1092 :     fn upload_new_image_layers(
    4655         1092 :         self: &Arc<Self>,
    4656         1092 :         new_images: impl IntoIterator<Item = ResidentLayer>,
    4657         1092 :     ) -> Result<(), super::upload_queue::NotInitialized> {
    4658         1170 :         for layer in new_images {
    4659           78 :             self.remote_client.schedule_layer_file_upload(layer)?;
    4660              :         }
    4661              :         // should any new image layer been created, not uploading index_part will
    4662              :         // result in a mismatch between remote_physical_size and layermap calculated
    4663              :         // size, which will fail some tests, but should not be an issue otherwise.
    4664         1092 :         self.remote_client
    4665         1092 :             .schedule_index_upload_for_file_changes()?;
    4666         1092 :         Ok(())
    4667         1092 :     }
    4668              : 
    4669              :     /// Find the Lsns above which layer files need to be retained on
    4670              :     /// garbage collection.
    4671              :     ///
    4672              :     /// We calculate two cutoffs, one based on time and one based on WAL size.  `pitr`
    4673              :     /// controls the time cutoff (or ZERO to disable time-based retention), and `space_cutoff` controls
    4674              :     /// the space-based retention.
    4675              :     ///
    4676              :     /// This function doesn't simply to calculate time & space based retention: it treats time-based
    4677              :     /// retention as authoritative if enabled, and falls back to space-based retention if calculating
    4678              :     /// the LSN for a time point isn't possible.  Therefore the GcCutoffs::horizon in the response might
    4679              :     /// be different to the `space_cutoff` input.  Callers should treat the min() of the two cutoffs
    4680              :     /// in the response as the GC cutoff point for the timeline.
    4681         2262 :     #[instrument(skip_all, fields(timeline_id=%self.timeline_id))]
    4682              :     pub(super) async fn find_gc_cutoffs(
    4683              :         &self,
    4684              :         space_cutoff: Lsn,
    4685              :         pitr: Duration,
    4686              :         cancel: &CancellationToken,
    4687              :         ctx: &RequestContext,
    4688              :     ) -> Result<GcCutoffs, PageReconstructError> {
    4689              :         let _timer = self
    4690              :             .metrics
    4691              :             .find_gc_cutoffs_histo
    4692              :             .start_timer()
    4693              :             .record_on_drop();
    4694              : 
    4695              :         pausable_failpoint!("Timeline::find_gc_cutoffs-pausable");
    4696              : 
    4697              :         if cfg!(test) {
    4698              :             // Unit tests which specify zero PITR interval expect to avoid doing any I/O for timestamp lookup
    4699              :             if pitr == Duration::ZERO {
    4700              :                 return Ok(GcCutoffs {
    4701              :                     time: self.get_last_record_lsn(),
    4702              :                     space: space_cutoff,
    4703              :                 });
    4704              :             }
    4705              :         }
    4706              : 
    4707              :         // Calculate a time-based limit on how much to retain:
    4708              :         // - if PITR interval is set, then this is our cutoff.
    4709              :         // - if PITR interval is not set, then we do a lookup
    4710              :         //   based on DEFAULT_PITR_INTERVAL, so that size-based retention does not result in keeping history around permanently on idle databases.
    4711              :         let time_cutoff = {
    4712              :             let now = SystemTime::now();
    4713              :             let time_range = if pitr == Duration::ZERO {
    4714              :                 humantime::parse_duration(DEFAULT_PITR_INTERVAL).expect("constant is invalid")
    4715              :             } else {
    4716              :                 pitr
    4717              :             };
    4718              : 
    4719              :             // If PITR is so large or `now` is so small that this underflows, we will retain no history (highly unexpected case)
    4720              :             let time_cutoff = now.checked_sub(time_range).unwrap_or(now);
    4721              :             let timestamp = to_pg_timestamp(time_cutoff);
    4722              : 
    4723              :             match self.find_lsn_for_timestamp(timestamp, cancel, ctx).await? {
    4724              :                 LsnForTimestamp::Present(lsn) => Some(lsn),
    4725              :                 LsnForTimestamp::Future(lsn) => {
    4726              :                     // The timestamp is in the future. That sounds impossible,
    4727              :                     // but what it really means is that there hasn't been
    4728              :                     // any commits since the cutoff timestamp.
    4729              :                     //
    4730              :                     // In this case we should use the LSN of the most recent commit,
    4731              :                     // which is implicitly the last LSN in the log.
    4732              :                     debug!("future({})", lsn);
    4733              :                     Some(self.get_last_record_lsn())
    4734              :                 }
    4735              :                 LsnForTimestamp::Past(lsn) => {
    4736              :                     debug!("past({})", lsn);
    4737              :                     None
    4738              :                 }
    4739              :                 LsnForTimestamp::NoData(lsn) => {
    4740              :                     debug!("nodata({})", lsn);
    4741              :                     None
    4742              :                 }
    4743              :             }
    4744              :         };
    4745              : 
    4746              :         Ok(match (pitr, time_cutoff) {
    4747              :             (Duration::ZERO, Some(time_cutoff)) => {
    4748              :                 // PITR is not set. Retain the size-based limit, or the default time retention,
    4749              :                 // whichever requires less data.
    4750              :                 GcCutoffs {
    4751              :                     time: self.get_last_record_lsn(),
    4752              :                     space: std::cmp::max(time_cutoff, space_cutoff),
    4753              :                 }
    4754              :             }
    4755              :             (Duration::ZERO, None) => {
    4756              :                 // PITR is not set, and time lookup failed
    4757              :                 GcCutoffs {
    4758              :                     time: self.get_last_record_lsn(),
    4759              :                     space: space_cutoff,
    4760              :                 }
    4761              :             }
    4762              :             (_, None) => {
    4763              :                 // PITR interval is set & we didn't look up a timestamp successfully.  Conservatively assume PITR
    4764              :                 // cannot advance beyond what was already GC'd, and respect space-based retention
    4765              :                 GcCutoffs {
    4766              :                     time: *self.get_latest_gc_cutoff_lsn(),
    4767              :                     space: space_cutoff,
    4768              :                 }
    4769              :             }
    4770              :             (_, Some(time_cutoff)) => {
    4771              :                 // PITR interval is set and we looked up timestamp successfully.  Ignore
    4772              :                 // size based retention and make time cutoff authoritative
    4773              :                 GcCutoffs {
    4774              :                     time: time_cutoff,
    4775              :                     space: time_cutoff,
    4776              :                 }
    4777              :             }
    4778              :         })
    4779              :     }
    4780              : 
    4781              :     /// Garbage collect layer files on a timeline that are no longer needed.
    4782              :     ///
    4783              :     /// Currently, we don't make any attempt at removing unneeded page versions
    4784              :     /// within a layer file. We can only remove the whole file if it's fully
    4785              :     /// obsolete.
    4786         2262 :     pub(super) async fn gc(&self) -> Result<GcResult, GcError> {
    4787              :         // this is most likely the background tasks, but it might be the spawned task from
    4788              :         // immediate_gc
    4789         2262 :         let _g = tokio::select! {
    4790         2262 :             guard = self.gc_lock.lock() => guard,
    4791         2262 :             _ = self.cancel.cancelled() => return Ok(GcResult::default()),
    4792              :         };
    4793         2260 :         let timer = self.metrics.garbage_collect_histo.start_timer();
    4794         2260 : 
    4795         2260 :         fail_point!("before-timeline-gc");
    4796         2260 : 
    4797         2260 :         // Is the timeline being deleted?
    4798         2260 :         if self.is_stopping() {
    4799            0 :             return Err(GcError::TimelineCancelled);
    4800         2260 :         }
    4801         2260 : 
    4802         2260 :         let (space_cutoff, time_cutoff, retain_lsns, max_lsn_with_valid_lease) = {
    4803         2260 :             let gc_info = self.gc_info.read().unwrap();
    4804         2260 : 
    4805         2260 :             let space_cutoff = min(gc_info.cutoffs.space, self.get_disk_consistent_lsn());
    4806         2260 :             let time_cutoff = gc_info.cutoffs.time;
    4807         2260 :             let retain_lsns = gc_info
    4808         2260 :                 .retain_lsns
    4809         2260 :                 .iter()
    4810         2260 :                 .map(|(lsn, _child_id)| *lsn)
    4811         2260 :                 .collect();
    4812         2260 : 
    4813         2260 :             // Gets the maximum LSN that holds the valid lease.
    4814         2260 :             //
    4815         2260 :             // Caveat: `refresh_gc_info` is in charged of updating the lease map.
    4816         2260 :             // Here, we do not check for stale leases again.
    4817         2260 :             let max_lsn_with_valid_lease = gc_info.leases.last_key_value().map(|(lsn, _)| *lsn);
    4818         2260 : 
    4819         2260 :             (
    4820         2260 :                 space_cutoff,
    4821         2260 :                 time_cutoff,
    4822         2260 :                 retain_lsns,
    4823         2260 :                 max_lsn_with_valid_lease,
    4824         2260 :             )
    4825         2260 :         };
    4826         2260 : 
    4827         2260 :         let mut new_gc_cutoff = Lsn::min(space_cutoff, time_cutoff);
    4828         2260 :         let standby_horizon = self.standby_horizon.load();
    4829         2260 :         // Hold GC for the standby, but as a safety guard do it only within some
    4830         2260 :         // reasonable lag.
    4831         2260 :         if standby_horizon != Lsn::INVALID {
    4832            0 :             if let Some(standby_lag) = new_gc_cutoff.checked_sub(standby_horizon) {
    4833              :                 const MAX_ALLOWED_STANDBY_LAG: u64 = 10u64 << 30; // 10 GB
    4834            0 :                 if standby_lag.0 < MAX_ALLOWED_STANDBY_LAG {
    4835            0 :                     new_gc_cutoff = Lsn::min(standby_horizon, new_gc_cutoff);
    4836            0 :                     trace!("holding off GC for standby apply LSN {}", standby_horizon);
    4837              :                 } else {
    4838            0 :                     warn!(
    4839            0 :                         "standby is lagging for more than {}MB, not holding gc for it",
    4840            0 :                         MAX_ALLOWED_STANDBY_LAG / 1024 / 1024
    4841              :                     )
    4842              :                 }
    4843            0 :             }
    4844         2260 :         }
    4845              : 
    4846              :         // Reset standby horizon to ignore it if it is not updated till next GC.
    4847              :         // It is an easy way to unset it when standby disappears without adding
    4848              :         // more conf options.
    4849         2260 :         self.standby_horizon.store(Lsn::INVALID);
    4850         2260 :         self.metrics
    4851         2260 :             .standby_horizon_gauge
    4852         2260 :             .set(Lsn::INVALID.0 as i64);
    4853              : 
    4854         2260 :         let res = self
    4855         2260 :             .gc_timeline(
    4856         2260 :                 space_cutoff,
    4857         2260 :                 time_cutoff,
    4858         2260 :                 retain_lsns,
    4859         2260 :                 max_lsn_with_valid_lease,
    4860         2260 :                 new_gc_cutoff,
    4861         2260 :             )
    4862         2260 :             .instrument(
    4863         2260 :                 info_span!("gc_timeline", timeline_id = %self.timeline_id, cutoff = %new_gc_cutoff),
    4864              :             )
    4865            0 :             .await?;
    4866              : 
    4867              :         // only record successes
    4868         2260 :         timer.stop_and_record();
    4869         2260 : 
    4870         2260 :         Ok(res)
    4871         2262 :     }
    4872              : 
    4873         2260 :     async fn gc_timeline(
    4874         2260 :         &self,
    4875         2260 :         space_cutoff: Lsn,
    4876         2260 :         time_cutoff: Lsn,
    4877         2260 :         retain_lsns: Vec<Lsn>,
    4878         2260 :         max_lsn_with_valid_lease: Option<Lsn>,
    4879         2260 :         new_gc_cutoff: Lsn,
    4880         2260 :     ) -> Result<GcResult, GcError> {
    4881         2260 :         // FIXME: if there is an ongoing detach_from_ancestor, we should just skip gc
    4882         2260 : 
    4883         2260 :         let now = SystemTime::now();
    4884         2260 :         let mut result: GcResult = GcResult::default();
    4885         2260 : 
    4886         2260 :         // Nothing to GC. Return early.
    4887         2260 :         let latest_gc_cutoff = *self.get_latest_gc_cutoff_lsn();
    4888         2260 :         if latest_gc_cutoff >= new_gc_cutoff {
    4889           66 :             info!(
    4890            0 :                 "Nothing to GC: new_gc_cutoff_lsn {new_gc_cutoff}, latest_gc_cutoff_lsn {latest_gc_cutoff}",
    4891              :             );
    4892           66 :             return Ok(result);
    4893         2194 :         }
    4894              : 
    4895              :         // We need to ensure that no one tries to read page versions or create
    4896              :         // branches at a point before latest_gc_cutoff_lsn. See branch_timeline()
    4897              :         // for details. This will block until the old value is no longer in use.
    4898              :         //
    4899              :         // The GC cutoff should only ever move forwards.
    4900         2194 :         let waitlist = {
    4901         2194 :             let write_guard = self.latest_gc_cutoff_lsn.lock_for_write();
    4902         2194 :             if *write_guard > new_gc_cutoff {
    4903            0 :                 return Err(GcError::BadLsn {
    4904            0 :                     why: format!(
    4905            0 :                         "Cannot move GC cutoff LSN backwards (was {}, new {})",
    4906            0 :                         *write_guard, new_gc_cutoff
    4907            0 :                     ),
    4908            0 :                 });
    4909         2194 :             }
    4910         2194 : 
    4911         2194 :             write_guard.store_and_unlock(new_gc_cutoff)
    4912         2194 :         };
    4913         2194 :         waitlist.wait().await;
    4914              : 
    4915         2194 :         info!("GC starting");
    4916              : 
    4917         2194 :         debug!("retain_lsns: {:?}", retain_lsns);
    4918              : 
    4919         2194 :         let mut layers_to_remove = Vec::new();
    4920              : 
    4921              :         // Scan all layers in the timeline (remote or on-disk).
    4922              :         //
    4923              :         // Garbage collect the layer if all conditions are satisfied:
    4924              :         // 1. it is older than cutoff LSN;
    4925              :         // 2. it is older than PITR interval;
    4926              :         // 3. it doesn't need to be retained for 'retain_lsns';
    4927              :         // 4. it does not need to be kept for LSNs holding valid leases.
    4928              :         // 5. newer on-disk image layers cover the layer's whole key range
    4929              :         //
    4930              :         // TODO holding a write lock is too agressive and avoidable
    4931         2194 :         let mut guard = self.layers.write().await;
    4932         2194 :         let layers = guard.layer_map()?;
    4933        37248 :         'outer: for l in layers.iter_historic_layers() {
    4934        37248 :             result.layers_total += 1;
    4935        37248 : 
    4936        37248 :             // 1. Is it newer than GC horizon cutoff point?
    4937        37248 :             if l.get_lsn_range().end > space_cutoff {
    4938         2224 :                 debug!(
    4939            0 :                     "keeping {} because it's newer than space_cutoff {}",
    4940            0 :                     l.layer_name(),
    4941              :                     space_cutoff,
    4942              :                 );
    4943         2224 :                 result.layers_needed_by_cutoff += 1;
    4944         2224 :                 continue 'outer;
    4945        35024 :             }
    4946        35024 : 
    4947        35024 :             // 2. It is newer than PiTR cutoff point?
    4948        35024 :             if l.get_lsn_range().end > time_cutoff {
    4949            0 :                 debug!(
    4950            0 :                     "keeping {} because it's newer than time_cutoff {}",
    4951            0 :                     l.layer_name(),
    4952              :                     time_cutoff,
    4953              :                 );
    4954            0 :                 result.layers_needed_by_pitr += 1;
    4955            0 :                 continue 'outer;
    4956        35024 :             }
    4957              : 
    4958              :             // 3. Is it needed by a child branch?
    4959              :             // NOTE With that we would keep data that
    4960              :             // might be referenced by child branches forever.
    4961              :             // We can track this in child timeline GC and delete parent layers when
    4962              :             // they are no longer needed. This might be complicated with long inheritance chains.
    4963              :             //
    4964              :             // TODO Vec is not a great choice for `retain_lsns`
    4965        35024 :             for retain_lsn in &retain_lsns {
    4966              :                 // start_lsn is inclusive
    4967           32 :                 if &l.get_lsn_range().start <= retain_lsn {
    4968           32 :                     debug!(
    4969            0 :                         "keeping {} because it's still might be referenced by child branch forked at {} is_dropped: xx is_incremental: {}",
    4970            0 :                         l.layer_name(),
    4971            0 :                         retain_lsn,
    4972            0 :                         l.is_incremental(),
    4973              :                     );
    4974           32 :                     result.layers_needed_by_branches += 1;
    4975           32 :                     continue 'outer;
    4976            0 :                 }
    4977              :             }
    4978              : 
    4979              :             // 4. Is there a valid lease that requires us to keep this layer?
    4980        34992 :             if let Some(lsn) = &max_lsn_with_valid_lease {
    4981              :                 // keep if layer start <= any of the lease
    4982           54 :                 if &l.get_lsn_range().start <= lsn {
    4983           42 :                     debug!(
    4984            0 :                         "keeping {} because there is a valid lease preventing GC at {}",
    4985            0 :                         l.layer_name(),
    4986              :                         lsn,
    4987              :                     );
    4988           42 :                     result.layers_needed_by_leases += 1;
    4989           42 :                     continue 'outer;
    4990           12 :                 }
    4991        34938 :             }
    4992              : 
    4993              :             // 5. Is there a later on-disk layer for this relation?
    4994              :             //
    4995              :             // The end-LSN is exclusive, while disk_consistent_lsn is
    4996              :             // inclusive. For example, if disk_consistent_lsn is 100, it is
    4997              :             // OK for a delta layer to have end LSN 101, but if the end LSN
    4998              :             // is 102, then it might not have been fully flushed to disk
    4999              :             // before crash.
    5000              :             //
    5001              :             // For example, imagine that the following layers exist:
    5002              :             //
    5003              :             // 1000      - image (A)
    5004              :             // 1000-2000 - delta (B)
    5005              :             // 2000      - image (C)
    5006              :             // 2000-3000 - delta (D)
    5007              :             // 3000      - image (E)
    5008              :             //
    5009              :             // If GC horizon is at 2500, we can remove layers A and B, but
    5010              :             // we cannot remove C, even though it's older than 2500, because
    5011              :             // the delta layer 2000-3000 depends on it.
    5012        34950 :             if !layers
    5013        34950 :                 .image_layer_exists(&l.get_key_range(), &(l.get_lsn_range().end..new_gc_cutoff))
    5014              :             {
    5015        34926 :                 debug!("keeping {} because it is the latest layer", l.layer_name());
    5016        34926 :                 result.layers_not_updated += 1;
    5017        34926 :                 continue 'outer;
    5018           24 :             }
    5019           24 : 
    5020           24 :             // We didn't find any reason to keep this file, so remove it.
    5021           24 :             debug!(
    5022            0 :                 "garbage collecting {} is_dropped: xx is_incremental: {}",
    5023            0 :                 l.layer_name(),
    5024            0 :                 l.is_incremental(),
    5025              :             );
    5026           24 :             layers_to_remove.push(l);
    5027              :         }
    5028              : 
    5029         2194 :         if !layers_to_remove.is_empty() {
    5030              :             // Persist the new GC cutoff value before we actually remove anything.
    5031              :             // This unconditionally schedules also an index_part.json update, even though, we will
    5032              :             // be doing one a bit later with the unlinked gc'd layers.
    5033           18 :             let disk_consistent_lsn = self.disk_consistent_lsn.load();
    5034           18 :             self.schedule_uploads(disk_consistent_lsn, None)
    5035           18 :                 .map_err(|e| {
    5036            0 :                     if self.cancel.is_cancelled() {
    5037            0 :                         GcError::TimelineCancelled
    5038              :                     } else {
    5039            0 :                         GcError::Remote(e)
    5040              :                     }
    5041           18 :                 })?;
    5042              : 
    5043           18 :             let gc_layers = layers_to_remove
    5044           18 :                 .iter()
    5045           24 :                 .map(|x| guard.get_from_desc(x))
    5046           18 :                 .collect::<Vec<Layer>>();
    5047           18 : 
    5048           18 :             result.layers_removed = gc_layers.len() as u64;
    5049           18 : 
    5050           18 :             self.remote_client.schedule_gc_update(&gc_layers)?;
    5051              : 
    5052           18 :             guard.open_mut()?.finish_gc_timeline(&gc_layers);
    5053           18 : 
    5054           18 :             #[cfg(feature = "testing")]
    5055           18 :             {
    5056           18 :                 result.doomed_layers = gc_layers;
    5057           18 :             }
    5058         2176 :         }
    5059              : 
    5060         2194 :         info!(
    5061            0 :             "GC completed removing {} layers, cutoff {}",
    5062              :             result.layers_removed, new_gc_cutoff
    5063              :         );
    5064              : 
    5065         2194 :         result.elapsed = now.elapsed().unwrap_or(Duration::ZERO);
    5066         2194 :         Ok(result)
    5067         2260 :     }
    5068              : 
    5069              :     /// Reconstruct a value, using the given base image and WAL records in 'data'.
    5070      2001654 :     async fn reconstruct_value(
    5071      2001654 :         &self,
    5072      2001654 :         key: Key,
    5073      2001654 :         request_lsn: Lsn,
    5074      2001654 :         mut data: ValueReconstructState,
    5075      2001654 :     ) -> Result<Bytes, PageReconstructError> {
    5076      2001654 :         // Perform WAL redo if needed
    5077      2001654 :         data.records.reverse();
    5078      2001654 : 
    5079      2001654 :         // If we have a page image, and no WAL, we're all set
    5080      2001654 :         if data.records.is_empty() {
    5081      2000676 :             if let Some((img_lsn, img)) = &data.img {
    5082      2000676 :                 trace!(
    5083            0 :                     "found page image for key {} at {}, no WAL redo required, req LSN {}",
    5084              :                     key,
    5085              :                     img_lsn,
    5086              :                     request_lsn,
    5087              :                 );
    5088      2000676 :                 Ok(img.clone())
    5089              :             } else {
    5090            0 :                 Err(PageReconstructError::from(anyhow!(
    5091            0 :                     "base image for {key} at {request_lsn} not found"
    5092            0 :                 )))
    5093              :             }
    5094              :         } else {
    5095              :             // We need to do WAL redo.
    5096              :             //
    5097              :             // If we don't have a base image, then the oldest WAL record better initialize
    5098              :             // the page
    5099          978 :             if data.img.is_none() && !data.records.first().unwrap().1.will_init() {
    5100            0 :                 Err(PageReconstructError::from(anyhow!(
    5101            0 :                     "Base image for {} at {} not found, but got {} WAL records",
    5102            0 :                     key,
    5103            0 :                     request_lsn,
    5104            0 :                     data.records.len()
    5105            0 :                 )))
    5106              :             } else {
    5107          978 :                 if data.img.is_some() {
    5108          978 :                     trace!(
    5109            0 :                         "found {} WAL records and a base image for {} at {}, performing WAL redo",
    5110            0 :                         data.records.len(),
    5111              :                         key,
    5112              :                         request_lsn
    5113              :                     );
    5114              :                 } else {
    5115            0 :                     trace!("found {} WAL records that will init the page for {} at {}, performing WAL redo", data.records.len(), key, request_lsn);
    5116              :                 };
    5117          978 :                 let res = self
    5118          978 :                     .walredo_mgr
    5119          978 :                     .as_ref()
    5120          978 :                     .context("timeline has no walredo manager")
    5121          978 :                     .map_err(PageReconstructError::WalRedo)?
    5122          978 :                     .request_redo(key, request_lsn, data.img, data.records, self.pg_version)
    5123            0 :                     .await;
    5124          978 :                 let img = match res {
    5125          978 :                     Ok(img) => img,
    5126            0 :                     Err(walredo::Error::Cancelled) => return Err(PageReconstructError::Cancelled),
    5127            0 :                     Err(walredo::Error::Other(e)) => {
    5128            0 :                         return Err(PageReconstructError::WalRedo(
    5129            0 :                             e.context("reconstruct a page image"),
    5130            0 :                         ))
    5131              :                     }
    5132              :                 };
    5133          978 :                 Ok(img)
    5134              :             }
    5135              :         }
    5136      2001654 :     }
    5137              : 
    5138            0 :     pub(crate) async fn spawn_download_all_remote_layers(
    5139            0 :         self: Arc<Self>,
    5140            0 :         request: DownloadRemoteLayersTaskSpawnRequest,
    5141            0 :     ) -> Result<DownloadRemoteLayersTaskInfo, DownloadRemoteLayersTaskInfo> {
    5142              :         use pageserver_api::models::DownloadRemoteLayersTaskState;
    5143              : 
    5144              :         // this is not really needed anymore; it has tests which really check the return value from
    5145              :         // http api. it would be better not to maintain this anymore.
    5146              : 
    5147            0 :         let mut status_guard = self.download_all_remote_layers_task_info.write().unwrap();
    5148            0 :         if let Some(st) = &*status_guard {
    5149            0 :             match &st.state {
    5150              :                 DownloadRemoteLayersTaskState::Running => {
    5151            0 :                     return Err(st.clone());
    5152              :                 }
    5153              :                 DownloadRemoteLayersTaskState::ShutDown
    5154            0 :                 | DownloadRemoteLayersTaskState::Completed => {
    5155            0 :                     *status_guard = None;
    5156            0 :                 }
    5157              :             }
    5158            0 :         }
    5159              : 
    5160            0 :         let self_clone = Arc::clone(&self);
    5161            0 :         let task_id = task_mgr::spawn(
    5162            0 :             task_mgr::BACKGROUND_RUNTIME.handle(),
    5163            0 :             task_mgr::TaskKind::DownloadAllRemoteLayers,
    5164            0 :             self.tenant_shard_id,
    5165            0 :             Some(self.timeline_id),
    5166            0 :             "download all remote layers task",
    5167            0 :             async move {
    5168            0 :                 self_clone.download_all_remote_layers(request).await;
    5169            0 :                 let mut status_guard = self_clone.download_all_remote_layers_task_info.write().unwrap();
    5170            0 :                  match &mut *status_guard {
    5171              :                     None => {
    5172            0 :                         warn!("tasks status is supposed to be Some(), since we are running");
    5173              :                     }
    5174            0 :                     Some(st) => {
    5175            0 :                         let exp_task_id = format!("{}", task_mgr::current_task_id().unwrap());
    5176            0 :                         if st.task_id != exp_task_id {
    5177            0 :                             warn!("task id changed while we were still running, expecting {} but have {}", exp_task_id, st.task_id);
    5178            0 :                         } else {
    5179            0 :                             st.state = DownloadRemoteLayersTaskState::Completed;
    5180            0 :                         }
    5181              :                     }
    5182              :                 };
    5183            0 :                 Ok(())
    5184            0 :             }
    5185            0 :             .instrument(info_span!(parent: None, "download_all_remote_layers", tenant_id = %self.tenant_shard_id.tenant_id, shard_id = %self.tenant_shard_id.shard_slug(), timeline_id = %self.timeline_id))
    5186              :         );
    5187              : 
    5188            0 :         let initial_info = DownloadRemoteLayersTaskInfo {
    5189            0 :             task_id: format!("{task_id}"),
    5190            0 :             state: DownloadRemoteLayersTaskState::Running,
    5191            0 :             total_layer_count: 0,
    5192            0 :             successful_download_count: 0,
    5193            0 :             failed_download_count: 0,
    5194            0 :         };
    5195            0 :         *status_guard = Some(initial_info.clone());
    5196            0 : 
    5197            0 :         Ok(initial_info)
    5198            0 :     }
    5199              : 
    5200            0 :     async fn download_all_remote_layers(
    5201            0 :         self: &Arc<Self>,
    5202            0 :         request: DownloadRemoteLayersTaskSpawnRequest,
    5203            0 :     ) {
    5204              :         use pageserver_api::models::DownloadRemoteLayersTaskState;
    5205              : 
    5206            0 :         let remaining = {
    5207            0 :             let guard = self.layers.read().await;
    5208            0 :             let Ok(lm) = guard.layer_map() else {
    5209              :                 // technically here we could look into iterating accessible layers, but downloading
    5210              :                 // all layers of a shutdown timeline makes no sense regardless.
    5211            0 :                 tracing::info!("attempted to download all layers of shutdown timeline");
    5212            0 :                 return;
    5213              :             };
    5214            0 :             lm.iter_historic_layers()
    5215            0 :                 .map(|desc| guard.get_from_desc(&desc))
    5216            0 :                 .collect::<Vec<_>>()
    5217            0 :         };
    5218            0 :         let total_layer_count = remaining.len();
    5219              : 
    5220              :         macro_rules! lock_status {
    5221              :             ($st:ident) => {
    5222              :                 let mut st = self.download_all_remote_layers_task_info.write().unwrap();
    5223              :                 let st = st
    5224              :                     .as_mut()
    5225              :                     .expect("this function is only called after the task has been spawned");
    5226              :                 assert_eq!(
    5227              :                     st.task_id,
    5228              :                     format!(
    5229              :                         "{}",
    5230              :                         task_mgr::current_task_id().expect("we run inside a task_mgr task")
    5231              :                     )
    5232              :                 );
    5233              :                 let $st = st;
    5234              :             };
    5235              :         }
    5236              : 
    5237              :         {
    5238            0 :             lock_status!(st);
    5239            0 :             st.total_layer_count = total_layer_count as u64;
    5240            0 :         }
    5241            0 : 
    5242            0 :         let mut remaining = remaining.into_iter();
    5243            0 :         let mut have_remaining = true;
    5244            0 :         let mut js = tokio::task::JoinSet::new();
    5245            0 : 
    5246            0 :         let cancel = task_mgr::shutdown_token();
    5247            0 : 
    5248            0 :         let limit = request.max_concurrent_downloads;
    5249              : 
    5250              :         loop {
    5251            0 :             while js.len() < limit.get() && have_remaining && !cancel.is_cancelled() {
    5252            0 :                 let Some(next) = remaining.next() else {
    5253            0 :                     have_remaining = false;
    5254            0 :                     break;
    5255              :                 };
    5256              : 
    5257            0 :                 let span = tracing::info_span!("download", layer = %next);
    5258              : 
    5259            0 :                 js.spawn(
    5260            0 :                     async move {
    5261            0 :                         let res = next.download().await;
    5262            0 :                         (next, res)
    5263            0 :                     }
    5264            0 :                     .instrument(span),
    5265            0 :                 );
    5266            0 :             }
    5267              : 
    5268            0 :             while let Some(res) = js.join_next().await {
    5269            0 :                 match res {
    5270              :                     Ok((_, Ok(_))) => {
    5271            0 :                         lock_status!(st);
    5272            0 :                         st.successful_download_count += 1;
    5273              :                     }
    5274            0 :                     Ok((layer, Err(e))) => {
    5275            0 :                         tracing::error!(%layer, "download failed: {e:#}");
    5276            0 :                         lock_status!(st);
    5277            0 :                         st.failed_download_count += 1;
    5278              :                     }
    5279            0 :                     Err(je) if je.is_cancelled() => unreachable!("not used here"),
    5280            0 :                     Err(je) if je.is_panic() => {
    5281            0 :                         lock_status!(st);
    5282            0 :                         st.failed_download_count += 1;
    5283              :                     }
    5284            0 :                     Err(je) => tracing::warn!("unknown joinerror: {je:?}"),
    5285              :                 }
    5286              :             }
    5287              : 
    5288            0 :             if js.is_empty() && (!have_remaining || cancel.is_cancelled()) {
    5289            0 :                 break;
    5290            0 :             }
    5291              :         }
    5292              : 
    5293              :         {
    5294            0 :             lock_status!(st);
    5295            0 :             st.state = DownloadRemoteLayersTaskState::Completed;
    5296              :         }
    5297            0 :     }
    5298              : 
    5299            0 :     pub(crate) fn get_download_all_remote_layers_task_info(
    5300            0 :         &self,
    5301            0 :     ) -> Option<DownloadRemoteLayersTaskInfo> {
    5302            0 :         self.download_all_remote_layers_task_info
    5303            0 :             .read()
    5304            0 :             .unwrap()
    5305            0 :             .clone()
    5306            0 :     }
    5307              : }
    5308              : 
    5309              : impl Timeline {
    5310              :     /// Returns non-remote layers for eviction.
    5311            0 :     pub(crate) async fn get_local_layers_for_disk_usage_eviction(&self) -> DiskUsageEvictionInfo {
    5312            0 :         let guard = self.layers.read().await;
    5313            0 :         let mut max_layer_size: Option<u64> = None;
    5314            0 : 
    5315            0 :         let resident_layers = guard
    5316            0 :             .likely_resident_layers()
    5317            0 :             .map(|layer| {
    5318            0 :                 let file_size = layer.layer_desc().file_size;
    5319            0 :                 max_layer_size = max_layer_size.map_or(Some(file_size), |m| Some(m.max(file_size)));
    5320            0 : 
    5321            0 :                 let last_activity_ts = layer.latest_activity();
    5322            0 : 
    5323            0 :                 EvictionCandidate {
    5324            0 :                     layer: layer.to_owned().into(),
    5325            0 :                     last_activity_ts,
    5326            0 :                     relative_last_activity: finite_f32::FiniteF32::ZERO,
    5327            0 :                     visibility: layer.visibility(),
    5328            0 :                 }
    5329            0 :             })
    5330            0 :             .collect();
    5331            0 : 
    5332            0 :         DiskUsageEvictionInfo {
    5333            0 :             max_layer_size,
    5334            0 :             resident_layers,
    5335            0 :         }
    5336            0 :     }
    5337              : 
    5338         5100 :     pub(crate) fn get_shard_index(&self) -> ShardIndex {
    5339         5100 :         ShardIndex {
    5340         5100 :             shard_number: self.tenant_shard_id.shard_number,
    5341         5100 :             shard_count: self.tenant_shard_id.shard_count,
    5342         5100 :         }
    5343         5100 :     }
    5344              : 
    5345              :     /// Persistently blocks gc for `Manual` reason.
    5346              :     ///
    5347              :     /// Returns true if no such block existed before, false otherwise.
    5348            0 :     pub(crate) async fn block_gc(&self, tenant: &super::Tenant) -> anyhow::Result<bool> {
    5349              :         use crate::tenant::remote_timeline_client::index::GcBlockingReason;
    5350            0 :         assert_eq!(self.tenant_shard_id, tenant.tenant_shard_id);
    5351            0 :         tenant.gc_block.insert(self, GcBlockingReason::Manual).await
    5352            0 :     }
    5353              : 
    5354              :     /// Persistently unblocks gc for `Manual` reason.
    5355            0 :     pub(crate) async fn unblock_gc(&self, tenant: &super::Tenant) -> anyhow::Result<()> {
    5356              :         use crate::tenant::remote_timeline_client::index::GcBlockingReason;
    5357            0 :         assert_eq!(self.tenant_shard_id, tenant.tenant_shard_id);
    5358            0 :         tenant.gc_block.remove(self, GcBlockingReason::Manual).await
    5359            0 :     }
    5360              : 
    5361              :     #[cfg(test)]
    5362          102 :     pub(super) fn force_advance_lsn(self: &Arc<Timeline>, new_lsn: Lsn) {
    5363          102 :         self.last_record_lsn.advance(new_lsn);
    5364          102 :     }
    5365              : 
    5366              :     #[cfg(test)]
    5367            6 :     pub(super) fn force_set_disk_consistent_lsn(&self, new_value: Lsn) {
    5368            6 :         self.disk_consistent_lsn.store(new_value);
    5369            6 :     }
    5370              : 
    5371              :     /// Force create an image layer and place it into the layer map.
    5372              :     ///
    5373              :     /// DO NOT use this function directly. Use [`Tenant::branch_timeline_test_with_layers`]
    5374              :     /// or [`Tenant::create_test_timeline_with_layers`] to ensure all these layers are placed into the layer map in one run.
    5375              :     #[cfg(test)]
    5376          138 :     pub(super) async fn force_create_image_layer(
    5377          138 :         self: &Arc<Timeline>,
    5378          138 :         lsn: Lsn,
    5379          138 :         mut images: Vec<(Key, Bytes)>,
    5380          138 :         check_start_lsn: Option<Lsn>,
    5381          138 :         ctx: &RequestContext,
    5382          138 :     ) -> anyhow::Result<()> {
    5383          138 :         let last_record_lsn = self.get_last_record_lsn();
    5384          138 :         assert!(
    5385          138 :             lsn <= last_record_lsn,
    5386            0 :             "advance last record lsn before inserting a layer, lsn={lsn}, last_record_lsn={last_record_lsn}"
    5387              :         );
    5388          138 :         if let Some(check_start_lsn) = check_start_lsn {
    5389          138 :             assert!(lsn >= check_start_lsn);
    5390            0 :         }
    5391          276 :         images.sort_unstable_by(|(ka, _), (kb, _)| ka.cmp(kb));
    5392          138 :         let min_key = *images.first().map(|(k, _)| k).unwrap();
    5393          138 :         let end_key = images.last().map(|(k, _)| k).unwrap().next();
    5394          138 :         let mut image_layer_writer = ImageLayerWriter::new(
    5395          138 :             self.conf,
    5396          138 :             self.timeline_id,
    5397          138 :             self.tenant_shard_id,
    5398          138 :             &(min_key..end_key),
    5399          138 :             lsn,
    5400          138 :             ctx,
    5401          138 :         )
    5402           69 :         .await?;
    5403          552 :         for (key, img) in images {
    5404          414 :             image_layer_writer.put_image(key, img, ctx).await?;
    5405              :         }
    5406          276 :         let image_layer = image_layer_writer.finish(self, ctx).await?;
    5407              : 
    5408              :         {
    5409          138 :             let mut guard = self.layers.write().await;
    5410          138 :             guard.open_mut().unwrap().force_insert_layer(image_layer);
    5411          138 :         }
    5412          138 : 
    5413          138 :         Ok(())
    5414          138 :     }
    5415              : 
    5416              :     /// Force create a delta layer and place it into the layer map.
    5417              :     ///
    5418              :     /// DO NOT use this function directly. Use [`Tenant::branch_timeline_test_with_layers`]
    5419              :     /// or [`Tenant::create_test_timeline_with_layers`] to ensure all these layers are placed into the layer map in one run.
    5420              :     #[cfg(test)]
    5421          174 :     pub(super) async fn force_create_delta_layer(
    5422          174 :         self: &Arc<Timeline>,
    5423          174 :         mut deltas: DeltaLayerTestDesc,
    5424          174 :         check_start_lsn: Option<Lsn>,
    5425          174 :         ctx: &RequestContext,
    5426          174 :     ) -> anyhow::Result<()> {
    5427          174 :         let last_record_lsn = self.get_last_record_lsn();
    5428          174 :         deltas
    5429          174 :             .data
    5430          204 :             .sort_unstable_by(|(ka, la, _), (kb, lb, _)| (ka, la).cmp(&(kb, lb)));
    5431          174 :         assert!(deltas.data.first().unwrap().0 >= deltas.key_range.start);
    5432          174 :         assert!(deltas.data.last().unwrap().0 < deltas.key_range.end);
    5433          552 :         for (_, lsn, _) in &deltas.data {
    5434          378 :             assert!(deltas.lsn_range.start <= *lsn && *lsn < deltas.lsn_range.end);
    5435              :         }
    5436          174 :         assert!(
    5437          174 :             deltas.lsn_range.end <= last_record_lsn,
    5438            0 :             "advance last record lsn before inserting a layer, end_lsn={}, last_record_lsn={}",
    5439              :             deltas.lsn_range.end,
    5440              :             last_record_lsn
    5441              :         );
    5442          174 :         if let Some(check_start_lsn) = check_start_lsn {
    5443          174 :             assert!(deltas.lsn_range.start >= check_start_lsn);
    5444            0 :         }
    5445              :         // check if the delta layer does not violate the LSN invariant, the legacy compaction should always produce a batch of
    5446              :         // layers of the same start/end LSN, and so should the force inserted layer
    5447              :         {
    5448              :             /// Checks if a overlaps with b, assume a/b = [start, end).
    5449          114 :             pub fn overlaps_with<T: Ord>(a: &Range<T>, b: &Range<T>) -> bool {
    5450          114 :                 !(a.end <= b.start || b.end <= a.start)
    5451          114 :             }
    5452              : 
    5453          174 :             if deltas.key_range.start.next() != deltas.key_range.end {
    5454           96 :                 let guard = self.layers.read().await;
    5455           96 :                 let mut invalid_layers =
    5456          192 :                     guard.layer_map()?.iter_historic_layers().filter(|layer| {
    5457          192 :                         layer.is_delta()
    5458          114 :                         && overlaps_with(&layer.lsn_range, &deltas.lsn_range)
    5459           36 :                         && layer.lsn_range != deltas.lsn_range
    5460              :                         // skip single-key layer files
    5461           12 :                         && layer.key_range.start.next() != layer.key_range.end
    5462          192 :                     });
    5463           96 :                 if let Some(layer) = invalid_layers.next() {
    5464              :                     // If a delta layer overlaps with another delta layer AND their LSN range is not the same, panic
    5465            0 :                     panic!(
    5466            0 :                         "inserted layer violates delta layer LSN invariant: current_lsn_range={}..{}, conflict_lsn_range={}..{}",
    5467            0 :                         deltas.lsn_range.start, deltas.lsn_range.end, layer.lsn_range.start, layer.lsn_range.end
    5468            0 :                     );
    5469           96 :                 }
    5470           78 :             }
    5471              :         }
    5472          174 :         let mut delta_layer_writer = DeltaLayerWriter::new(
    5473          174 :             self.conf,
    5474          174 :             self.timeline_id,
    5475          174 :             self.tenant_shard_id,
    5476          174 :             deltas.key_range.start,
    5477          174 :             deltas.lsn_range,
    5478          174 :             ctx,
    5479          174 :         )
    5480           87 :         .await?;
    5481          552 :         for (key, lsn, val) in deltas.data {
    5482          378 :             delta_layer_writer.put_value(key, lsn, val, ctx).await?;
    5483              :         }
    5484          435 :         let (desc, path) = delta_layer_writer.finish(deltas.key_range.end, ctx).await?;
    5485          174 :         let delta_layer = Layer::finish_creating(self.conf, self, desc, &path)?;
    5486              : 
    5487              :         {
    5488          174 :             let mut guard = self.layers.write().await;
    5489          174 :             guard.open_mut().unwrap().force_insert_layer(delta_layer);
    5490          174 :         }
    5491          174 : 
    5492          174 :         Ok(())
    5493          174 :     }
    5494              : 
    5495              :     /// Return all keys at the LSN in the image layers
    5496              :     #[cfg(test)]
    5497           18 :     pub(crate) async fn inspect_image_layers(
    5498           18 :         self: &Arc<Timeline>,
    5499           18 :         lsn: Lsn,
    5500           18 :         ctx: &RequestContext,
    5501           18 :     ) -> anyhow::Result<Vec<(Key, Bytes)>> {
    5502           18 :         let mut all_data = Vec::new();
    5503           18 :         let guard = self.layers.read().await;
    5504          102 :         for layer in guard.layer_map()?.iter_historic_layers() {
    5505          102 :             if !layer.is_delta() && layer.image_layer_lsn() == lsn {
    5506           24 :                 let layer = guard.get_from_desc(&layer);
    5507           24 :                 let mut reconstruct_data = ValuesReconstructState::default();
    5508           24 :                 layer
    5509           24 :                     .get_values_reconstruct_data(
    5510           24 :                         KeySpace::single(Key::MIN..Key::MAX),
    5511           24 :                         lsn..Lsn(lsn.0 + 1),
    5512           24 :                         &mut reconstruct_data,
    5513           24 :                         ctx,
    5514           24 :                     )
    5515           39 :                     .await?;
    5516          222 :                 for (k, v) in reconstruct_data.keys {
    5517          198 :                     all_data.push((k, v?.img.unwrap().1));
    5518              :                 }
    5519           78 :             }
    5520              :         }
    5521           18 :         all_data.sort();
    5522           18 :         Ok(all_data)
    5523           18 :     }
    5524              : 
    5525              :     /// Get all historic layer descriptors in the layer map
    5526              :     #[cfg(test)]
    5527            6 :     pub(crate) async fn inspect_historic_layers(
    5528            6 :         self: &Arc<Timeline>,
    5529            6 :     ) -> anyhow::Result<Vec<super::storage_layer::PersistentLayerKey>> {
    5530            6 :         let mut layers = Vec::new();
    5531            6 :         let guard = self.layers.read().await;
    5532           18 :         for layer in guard.layer_map()?.iter_historic_layers() {
    5533           18 :             layers.push(layer.key());
    5534           18 :         }
    5535            6 :         Ok(layers)
    5536            6 :     }
    5537              : 
    5538              :     #[cfg(test)]
    5539           30 :     pub(crate) fn add_extra_test_dense_keyspace(&self, ks: KeySpace) {
    5540           30 :         let mut keyspace = self.extra_test_dense_keyspace.load().as_ref().clone();
    5541           30 :         keyspace.merge(&ks);
    5542           30 :         self.extra_test_dense_keyspace.store(Arc::new(keyspace));
    5543           30 :     }
    5544              : }
    5545              : 
    5546              : /// Tracking writes ingestion does to a particular in-memory layer.
    5547              : ///
    5548              : /// Cleared upon freezing a layer.
    5549              : pub(crate) struct TimelineWriterState {
    5550              :     open_layer: Arc<InMemoryLayer>,
    5551              :     current_size: u64,
    5552              :     // Previous Lsn which passed through
    5553              :     prev_lsn: Option<Lsn>,
    5554              :     // Largest Lsn which passed through the current writer
    5555              :     max_lsn: Option<Lsn>,
    5556              :     // Cached details of the last freeze. Avoids going trough the atomic/lock on every put.
    5557              :     cached_last_freeze_at: Lsn,
    5558              : }
    5559              : 
    5560              : impl TimelineWriterState {
    5561         3810 :     fn new(open_layer: Arc<InMemoryLayer>, current_size: u64, last_freeze_at: Lsn) -> Self {
    5562         3810 :         Self {
    5563         3810 :             open_layer,
    5564         3810 :             current_size,
    5565         3810 :             prev_lsn: None,
    5566         3810 :             max_lsn: None,
    5567         3810 :             cached_last_freeze_at: last_freeze_at,
    5568         3810 :         }
    5569         3810 :     }
    5570              : }
    5571              : 
    5572              : /// Various functions to mutate the timeline.
    5573              : // TODO Currently, Deref is used to allow easy access to read methods from this trait.
    5574              : // This is probably considered a bad practice in Rust and should be fixed eventually,
    5575              : // but will cause large code changes.
    5576              : pub(crate) struct TimelineWriter<'a> {
    5577              :     tl: &'a Timeline,
    5578              :     write_guard: tokio::sync::MutexGuard<'a, Option<TimelineWriterState>>,
    5579              : }
    5580              : 
    5581              : impl Deref for TimelineWriter<'_> {
    5582              :     type Target = Timeline;
    5583              : 
    5584     14421582 :     fn deref(&self) -> &Self::Target {
    5585     14421582 :         self.tl
    5586     14421582 :     }
    5587              : }
    5588              : 
    5589              : #[derive(PartialEq)]
    5590              : enum OpenLayerAction {
    5591              :     Roll,
    5592              :     Open,
    5593              :     None,
    5594              : }
    5595              : 
    5596              : impl<'a> TimelineWriter<'a> {
    5597     14412648 :     async fn handle_open_layer_action(
    5598     14412648 :         &mut self,
    5599     14412648 :         at: Lsn,
    5600     14412648 :         action: OpenLayerAction,
    5601     14412648 :         ctx: &RequestContext,
    5602     14412648 :     ) -> anyhow::Result<&Arc<InMemoryLayer>> {
    5603     14412648 :         match action {
    5604              :             OpenLayerAction::Roll => {
    5605          240 :                 let freeze_at = self.write_guard.as_ref().unwrap().max_lsn.unwrap();
    5606          240 :                 self.roll_layer(freeze_at).await?;
    5607          240 :                 self.open_layer(at, ctx).await?;
    5608              :             }
    5609         3570 :             OpenLayerAction::Open => self.open_layer(at, ctx).await?,
    5610              :             OpenLayerAction::None => {
    5611     14408838 :                 assert!(self.write_guard.is_some());
    5612              :             }
    5613              :         }
    5614              : 
    5615     14412648 :         Ok(&self.write_guard.as_ref().unwrap().open_layer)
    5616     14412648 :     }
    5617              : 
    5618         3810 :     async fn open_layer(&mut self, at: Lsn, ctx: &RequestContext) -> anyhow::Result<()> {
    5619         3810 :         let layer = self
    5620         3810 :             .tl
    5621         3810 :             .get_layer_for_write(at, &self.write_guard, ctx)
    5622         2157 :             .await?;
    5623         3810 :         let initial_size = layer.size().await?;
    5624              : 
    5625         3810 :         let last_freeze_at = self.last_freeze_at.load();
    5626         3810 :         self.write_guard.replace(TimelineWriterState::new(
    5627         3810 :             layer,
    5628         3810 :             initial_size,
    5629         3810 :             last_freeze_at,
    5630         3810 :         ));
    5631         3810 : 
    5632         3810 :         Ok(())
    5633         3810 :     }
    5634              : 
    5635          240 :     async fn roll_layer(&mut self, freeze_at: Lsn) -> Result<(), FlushLayerError> {
    5636          240 :         let current_size = self.write_guard.as_ref().unwrap().current_size;
    5637          240 : 
    5638          240 :         // self.write_guard will be taken by the freezing
    5639          240 :         self.tl
    5640          240 :             .freeze_inmem_layer_at(freeze_at, &mut self.write_guard)
    5641           14 :             .await?;
    5642              : 
    5643          240 :         assert!(self.write_guard.is_none());
    5644              : 
    5645          240 :         if current_size >= self.get_checkpoint_distance() * 2 {
    5646            0 :             warn!("Flushed oversized open layer with size {}", current_size)
    5647          240 :         }
    5648              : 
    5649          240 :         Ok(())
    5650          240 :     }
    5651              : 
    5652     14412648 :     fn get_open_layer_action(&self, lsn: Lsn, new_value_size: u64) -> OpenLayerAction {
    5653     14412648 :         let state = &*self.write_guard;
    5654     14412648 :         let Some(state) = &state else {
    5655         3570 :             return OpenLayerAction::Open;
    5656              :         };
    5657              : 
    5658              :         #[cfg(feature = "testing")]
    5659     14409078 :         if state.cached_last_freeze_at < self.tl.last_freeze_at.load() {
    5660              :             // this check and assertion are not really needed because
    5661              :             // LayerManager::try_freeze_in_memory_layer will always clear out the
    5662              :             // TimelineWriterState if something is frozen. however, we can advance last_freeze_at when there
    5663              :             // is no TimelineWriterState.
    5664            0 :             assert!(
    5665            0 :                 state.open_layer.end_lsn.get().is_some(),
    5666            0 :                 "our open_layer must be outdated"
    5667              :             );
    5668              : 
    5669              :             // this would be a memory leak waiting to happen because the in-memory layer always has
    5670              :             // an index
    5671            0 :             panic!("BUG: TimelineWriterState held on to frozen in-memory layer.");
    5672     14409078 :         }
    5673     14409078 : 
    5674     14409078 :         if state.prev_lsn == Some(lsn) {
    5675              :             // Rolling mid LSN is not supported by [downstream code].
    5676              :             // Hence, only roll at LSN boundaries.
    5677              :             //
    5678              :             // [downstream code]: https://github.com/neondatabase/neon/pull/7993#discussion_r1633345422
    5679           18 :             return OpenLayerAction::None;
    5680     14409060 :         }
    5681     14409060 : 
    5682     14409060 :         if state.current_size == 0 {
    5683              :             // Don't roll empty layers
    5684            0 :             return OpenLayerAction::None;
    5685     14409060 :         }
    5686     14409060 : 
    5687     14409060 :         if self.tl.should_roll(
    5688     14409060 :             state.current_size,
    5689     14409060 :             state.current_size + new_value_size,
    5690     14409060 :             self.get_checkpoint_distance(),
    5691     14409060 :             lsn,
    5692     14409060 :             state.cached_last_freeze_at,
    5693     14409060 :             state.open_layer.get_opened_at(),
    5694     14409060 :         ) {
    5695          240 :             OpenLayerAction::Roll
    5696              :         } else {
    5697     14408820 :             OpenLayerAction::None
    5698              :         }
    5699     14412648 :     }
    5700              : 
    5701              :     /// Put a batch of keys at the specified Lsns.
    5702     14412642 :     pub(crate) async fn put_batch(
    5703     14412642 :         &mut self,
    5704     14412642 :         batch: Vec<(CompactKey, Lsn, usize, Value)>,
    5705     14412642 :         ctx: &RequestContext,
    5706     14412642 :     ) -> anyhow::Result<()> {
    5707     14412642 :         if batch.is_empty() {
    5708            0 :             return Ok(());
    5709     14412642 :         }
    5710              : 
    5711     14412642 :         let serialized_batch = inmemory_layer::SerializedBatch::from_values(batch)?;
    5712     14412642 :         let batch_max_lsn = serialized_batch.max_lsn;
    5713     14412642 :         let buf_size: u64 = serialized_batch.raw.len() as u64;
    5714     14412642 : 
    5715     14412642 :         let action = self.get_open_layer_action(batch_max_lsn, buf_size);
    5716     14412642 :         let layer = self
    5717     14412642 :             .handle_open_layer_action(batch_max_lsn, action, ctx)
    5718         2171 :             .await?;
    5719              : 
    5720     14412642 :         let res = layer.put_batch(serialized_batch, ctx).await;
    5721              : 
    5722     14412642 :         if res.is_ok() {
    5723     14412642 :             // Update the current size only when the entire write was ok.
    5724     14412642 :             // In case of failures, we may have had partial writes which
    5725     14412642 :             // render the size tracking out of sync. That's ok because
    5726     14412642 :             // the checkpoint distance should be significantly smaller
    5727     14412642 :             // than the S3 single shot upload limit of 5GiB.
    5728     14412642 :             let state = self.write_guard.as_mut().unwrap();
    5729     14412642 : 
    5730     14412642 :             state.current_size += buf_size;
    5731     14412642 :             state.prev_lsn = Some(batch_max_lsn);
    5732     14412642 :             state.max_lsn = std::cmp::max(state.max_lsn, Some(batch_max_lsn));
    5733     14412642 :         }
    5734              : 
    5735     14412642 :         res
    5736     14412642 :     }
    5737              : 
    5738              :     #[cfg(test)]
    5739              :     /// Test helper, for tests that would like to poke individual values without composing a batch
    5740     13170462 :     pub(crate) async fn put(
    5741     13170462 :         &mut self,
    5742     13170462 :         key: Key,
    5743     13170462 :         lsn: Lsn,
    5744     13170462 :         value: &Value,
    5745     13170462 :         ctx: &RequestContext,
    5746     13170462 :     ) -> anyhow::Result<()> {
    5747              :         use utils::bin_ser::BeSer;
    5748     13170462 :         if !key.is_valid_key_on_write_path() {
    5749            0 :             bail!(
    5750            0 :                 "the request contains data not supported by pageserver at TimelineWriter::put: {}",
    5751            0 :                 key
    5752            0 :             );
    5753     13170462 :         }
    5754     13170462 :         let val_ser_size = value.serialized_size().unwrap() as usize;
    5755     13170462 :         self.put_batch(
    5756     13170462 :             vec![(key.to_compact(), lsn, val_ser_size, value.clone())],
    5757     13170462 :             ctx,
    5758     13170462 :         )
    5759        12407 :         .await
    5760     13170462 :     }
    5761              : 
    5762            6 :     pub(crate) async fn delete_batch(
    5763            6 :         &mut self,
    5764            6 :         batch: &[(Range<Key>, Lsn)],
    5765            6 :         ctx: &RequestContext,
    5766            6 :     ) -> anyhow::Result<()> {
    5767            6 :         if let Some((_, lsn)) = batch.first() {
    5768            6 :             let action = self.get_open_layer_action(*lsn, 0);
    5769            6 :             let layer = self.handle_open_layer_action(*lsn, action, ctx).await?;
    5770            6 :             layer.put_tombstones(batch).await?;
    5771            0 :         }
    5772              : 
    5773            6 :         Ok(())
    5774            6 :     }
    5775              : 
    5776              :     /// Track the end of the latest digested WAL record.
    5777              :     /// Remember the (end of) last valid WAL record remembered in the timeline.
    5778              :     ///
    5779              :     /// Call this after you have finished writing all the WAL up to 'lsn'.
    5780              :     ///
    5781              :     /// 'lsn' must be aligned. This wakes up any wait_lsn() callers waiting for
    5782              :     /// the 'lsn' or anything older. The previous last record LSN is stored alongside
    5783              :     /// the latest and can be read.
    5784     15837228 :     pub(crate) fn finish_write(&self, new_lsn: Lsn) {
    5785     15837228 :         self.tl.finish_write(new_lsn);
    5786     15837228 :     }
    5787              : 
    5788       811710 :     pub(crate) fn update_current_logical_size(&self, delta: i64) {
    5789       811710 :         self.tl.update_current_logical_size(delta)
    5790       811710 :     }
    5791              : }
    5792              : 
    5793              : // We need TimelineWriter to be send in upcoming conversion of
    5794              : // Timeline::layers to tokio::sync::RwLock.
    5795              : #[test]
    5796            6 : fn is_send() {
    5797            6 :     fn _assert_send<T: Send>() {}
    5798            6 :     _assert_send::<TimelineWriter<'_>>();
    5799            6 : }
    5800              : 
    5801              : #[cfg(test)]
    5802              : mod tests {
    5803              :     use pageserver_api::key::Key;
    5804              :     use utils::{id::TimelineId, lsn::Lsn};
    5805              : 
    5806              :     use crate::{
    5807              :         repository::Value,
    5808              :         tenant::{
    5809              :             harness::{test_img, TenantHarness},
    5810              :             layer_map::LayerMap,
    5811              :             storage_layer::{Layer, LayerName},
    5812              :             timeline::{DeltaLayerTestDesc, EvictionError},
    5813              :             Timeline,
    5814              :         },
    5815              :     };
    5816              : 
    5817              :     #[tokio::test]
    5818            6 :     async fn test_heatmap_generation() {
    5819            6 :         let harness = TenantHarness::create("heatmap_generation").await.unwrap();
    5820            6 : 
    5821            6 :         let covered_delta = DeltaLayerTestDesc::new_with_inferred_key_range(
    5822            6 :             Lsn(0x10)..Lsn(0x20),
    5823            6 :             vec![(
    5824            6 :                 Key::from_hex("620000000033333333444444445500000000").unwrap(),
    5825            6 :                 Lsn(0x11),
    5826            6 :                 Value::Image(test_img("foo")),
    5827            6 :             )],
    5828            6 :         );
    5829            6 :         let visible_delta = DeltaLayerTestDesc::new_with_inferred_key_range(
    5830            6 :             Lsn(0x10)..Lsn(0x20),
    5831            6 :             vec![(
    5832            6 :                 Key::from_hex("720000000033333333444444445500000000").unwrap(),
    5833            6 :                 Lsn(0x11),
    5834            6 :                 Value::Image(test_img("foo")),
    5835            6 :             )],
    5836            6 :         );
    5837            6 :         let l0_delta = DeltaLayerTestDesc::new(
    5838            6 :             Lsn(0x20)..Lsn(0x30),
    5839            6 :             Key::from_hex("000000000000000000000000000000000000").unwrap()
    5840            6 :                 ..Key::from_hex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap(),
    5841            6 :             vec![(
    5842            6 :                 Key::from_hex("720000000033333333444444445500000000").unwrap(),
    5843            6 :                 Lsn(0x25),
    5844            6 :                 Value::Image(test_img("foo")),
    5845            6 :             )],
    5846            6 :         );
    5847            6 :         let delta_layers = vec![
    5848            6 :             covered_delta.clone(),
    5849            6 :             visible_delta.clone(),
    5850            6 :             l0_delta.clone(),
    5851            6 :         ];
    5852            6 : 
    5853            6 :         let image_layer = (
    5854            6 :             Lsn(0x40),
    5855            6 :             vec![(
    5856            6 :                 Key::from_hex("620000000033333333444444445500000000").unwrap(),
    5857            6 :                 test_img("bar"),
    5858            6 :             )],
    5859            6 :         );
    5860            6 :         let image_layers = vec![image_layer];
    5861            6 : 
    5862           24 :         let (tenant, ctx) = harness.load().await;
    5863            6 :         let timeline = tenant
    5864            6 :             .create_test_timeline_with_layers(
    5865            6 :                 TimelineId::generate(),
    5866            6 :                 Lsn(0x10),
    5867            6 :                 14,
    5868            6 :                 &ctx,
    5869            6 :                 delta_layers,
    5870            6 :                 image_layers,
    5871            6 :                 Lsn(0x100),
    5872            6 :             )
    5873           87 :             .await
    5874            6 :             .unwrap();
    5875            6 : 
    5876            6 :         // Layer visibility is an input to heatmap generation, so refresh it first
    5877            6 :         timeline.update_layer_visibility().await.unwrap();
    5878            6 : 
    5879            6 :         let heatmap = timeline
    5880            6 :             .generate_heatmap()
    5881            6 :             .await
    5882            6 :             .expect("Infallible while timeline is not shut down");
    5883            6 : 
    5884            6 :         assert_eq!(heatmap.timeline_id, timeline.timeline_id);
    5885            6 : 
    5886            6 :         // L0 should come last
    5887            6 :         assert_eq!(heatmap.layers.last().unwrap().name, l0_delta.layer_name());
    5888            6 : 
    5889            6 :         let mut last_lsn = Lsn::MAX;
    5890           30 :         for layer in heatmap.layers {
    5891            6 :             // Covered layer should be omitted
    5892           24 :             assert!(layer.name != covered_delta.layer_name());
    5893            6 : 
    5894           24 :             let layer_lsn = match &layer.name {
    5895           12 :                 LayerName::Delta(d) => d.lsn_range.end,
    5896           12 :                 LayerName::Image(i) => i.lsn,
    5897            6 :             };
    5898            6 : 
    5899            6 :             // Apart from L0s, newest Layers should come first
    5900           24 :             if !LayerMap::is_l0(layer.name.key_range(), layer.name.is_delta()) {
    5901           18 :                 assert!(layer_lsn <= last_lsn);
    5902           18 :                 last_lsn = layer_lsn;
    5903            6 :             }
    5904            6 :         }
    5905            6 :     }
    5906              : 
    5907              :     #[tokio::test]
    5908            6 :     async fn two_layer_eviction_attempts_at_the_same_time() {
    5909            6 :         let harness = TenantHarness::create("two_layer_eviction_attempts_at_the_same_time")
    5910            6 :             .await
    5911            6 :             .unwrap();
    5912            6 : 
    5913           24 :         let (tenant, ctx) = harness.load().await;
    5914            6 :         let timeline = tenant
    5915            6 :             .create_test_timeline(TimelineId::generate(), Lsn(0x10), 14, &ctx)
    5916           12 :             .await
    5917            6 :             .unwrap();
    5918            6 : 
    5919            6 :         let layer = find_some_layer(&timeline).await;
    5920            6 :         let layer = layer
    5921            6 :             .keep_resident()
    5922            6 :             .await
    5923            6 :             .expect("no download => no downloading errors")
    5924            6 :             .drop_eviction_guard();
    5925            6 : 
    5926            6 :         let forever = std::time::Duration::from_secs(120);
    5927            6 : 
    5928            6 :         let first = layer.evict_and_wait(forever);
    5929            6 :         let second = layer.evict_and_wait(forever);
    5930            6 : 
    5931            6 :         let (first, second) = tokio::join!(first, second);
    5932            6 : 
    5933            6 :         let res = layer.keep_resident().await;
    5934            6 :         assert!(res.is_none(), "{res:?}");
    5935            6 : 
    5936            6 :         match (first, second) {
    5937            6 :             (Ok(()), Ok(())) => {
    5938            6 :                 // because there are no more timeline locks being taken on eviction path, we can
    5939            6 :                 // witness all three outcomes here.
    5940            6 :             }
    5941            6 :             (Ok(()), Err(EvictionError::NotFound)) | (Err(EvictionError::NotFound), Ok(())) => {
    5942            0 :                 // if one completes before the other, this is fine just as well.
    5943            0 :             }
    5944            6 :             other => unreachable!("unexpected {:?}", other),
    5945            6 :         }
    5946            6 :     }
    5947              : 
    5948            6 :     async fn find_some_layer(timeline: &Timeline) -> Layer {
    5949            6 :         let layers = timeline.layers.read().await;
    5950            6 :         let desc = layers
    5951            6 :             .layer_map()
    5952            6 :             .unwrap()
    5953            6 :             .iter_historic_layers()
    5954            6 :             .next()
    5955            6 :             .expect("must find one layer to evict");
    5956            6 : 
    5957            6 :         layers.get_from_desc(&desc)
    5958            6 :     }
    5959              : }
        

Generated by: LCOV version 2.1-beta