LCOV - code coverage report
Current view: top level - pageserver/src/tenant - remote_timeline_client.rs (source / functions) Coverage Total Hit
Test: 20b6afc7b7f34578dcaab2b3acdaecfe91cd8bf1.info Lines: 65.0 % 1987 1291
Test Date: 2024-11-25 17:48:16 Functions: 51.3 % 191 98

            Line data    Source code
       1              : //! This module manages synchronizing local FS with remote storage.
       2              : //!
       3              : //! # Overview
       4              : //!
       5              : //! * [`RemoteTimelineClient`] provides functions related to upload/download of a particular timeline.
       6              : //!   It contains a queue of pending uploads, and manages the queue, performing uploads in parallel
       7              : //!   when it's safe to do so.
       8              : //!
       9              : //! * Stand-alone function, [`list_remote_timelines`], to get list of timelines of a tenant.
      10              : //!
      11              : //! These functions use the low-level remote storage client, [`remote_storage::RemoteStorage`].
      12              : //!
      13              : //! # APIs & How To Use Them
      14              : //!
      15              : //! There is a [RemoteTimelineClient] for each [Timeline][`crate::tenant::Timeline`] in the system,
      16              : //! unless the pageserver is configured without remote storage.
      17              : //!
      18              : //! We allocate the client instance in [Timeline][`crate::tenant::Timeline`], i.e.,
      19              : //! either in [`crate::tenant::mgr`] during startup or when creating a new
      20              : //! timeline.
      21              : //! However, the client does not become ready for use until we've initialized its upload queue:
      22              : //!
      23              : //! - For timelines that already have some state on the remote storage, we use
      24              : //!   [`RemoteTimelineClient::init_upload_queue`] .
      25              : //! - For newly created timelines, we use
      26              : //!   [`RemoteTimelineClient::init_upload_queue_for_empty_remote`].
      27              : //!
      28              : //! The former takes the remote's [`IndexPart`] as an argument, possibly retrieved
      29              : //! using [`list_remote_timelines`]. We'll elaborate on [`IndexPart`] in the next section.
      30              : //!
      31              : //! Whenever we've created/updated/deleted a file in a timeline directory, we schedule
      32              : //! the corresponding remote operation with the timeline's [`RemoteTimelineClient`]:
      33              : //!
      34              : //! - [`RemoteTimelineClient::schedule_layer_file_upload`]  when we've created a new layer file.
      35              : //! - [`RemoteTimelineClient::schedule_index_upload_for_metadata_update`] when we've updated the timeline metadata file.
      36              : //! - [`RemoteTimelineClient::schedule_index_upload_for_file_changes`] to upload an updated index file, after we've scheduled file uploads
      37              : //! - [`RemoteTimelineClient::schedule_layer_file_deletion`] when we've deleted one or more layer files.
      38              : //!
      39              : //! Internally, these functions create [`UploadOp`]s and put them in a queue.
      40              : //!
      41              : //! There are also APIs for downloading files.
      42              : //! These are not part of the aforementioned queuing and will not be discussed
      43              : //! further here, except in the section covering tenant attach.
      44              : //!
      45              : //! # Remote Storage Structure & [`IndexPart`] Index File
      46              : //!
      47              : //! The "directory structure" in the remote storage mirrors the local directory structure, with paths
      48              : //! like `tenants/<tenant_id>/timelines/<timeline_id>/<layer filename>`.
      49              : //! Yet instead of keeping the `metadata` file remotely, we wrap it with more
      50              : //! data in an "index file" aka [`IndexPart`], containing the list of **all** remote
      51              : //! files for a given timeline.
      52              : //! If a file is not referenced from [`IndexPart`], it's not part of the remote storage state.
      53              : //!
      54              : //! Having the `IndexPart` also avoids expensive and slow `S3 list` commands.
      55              : //!
      56              : //! # Consistency
      57              : //!
      58              : //! To have a consistent remote structure, it's important that uploads and
      59              : //! deletions are performed in the right order. For example, the index file
      60              : //! contains a list of layer files, so it must not be uploaded until all the
      61              : //! layer files that are in its list have been successfully uploaded.
      62              : //!
      63              : //! The contract between client and its user is that the user is responsible of
      64              : //! scheduling operations in an order that keeps the remote consistent as
      65              : //! described above.
      66              : //! From the user's perspective, the operations are executed sequentially.
      67              : //! Internally, the client knows which operations can be performed in parallel,
      68              : //! and which operations act like a "barrier" that require preceding operations
      69              : //! to finish. The calling code just needs to call the schedule-functions in the
      70              : //! correct order, and the client will parallelize the operations in a way that
      71              : //! is safe.
      72              : //!
      73              : //! The caller should be careful with deletion, though. They should not delete
      74              : //! local files that have been scheduled for upload but not yet finished uploading.
      75              : //! Otherwise the upload will fail. To wait for an upload to finish, use
      76              : //! the 'wait_completion' function (more on that later.)
      77              : //!
      78              : //! All of this relies on the following invariants:
      79              : //!
      80              : //! - We rely on read-after write consistency in the remote storage.
      81              : //! - Layer files are immutable
      82              : //!
      83              : //! NB: Pageserver assumes that it has exclusive write access to the tenant in remote
      84              : //! storage. Different tenants can be attached to different pageservers, but if the
      85              : //! same tenant is attached to two pageservers at the same time, they will overwrite
      86              : //! each other's index file updates, and confusion will ensue. There's no interlock or
      87              : //! mechanism to detect that in the pageserver, we rely on the control plane to ensure
      88              : //! that that doesn't happen.
      89              : //!
      90              : //! ## Implementation Note
      91              : //!
      92              : //! The *actual* remote state lags behind the *desired* remote state while
      93              : //! there are in-flight operations.
      94              : //! We keep track of the desired remote state in [`UploadQueueInitialized::dirty`].
      95              : //! It is initialized based on the [`IndexPart`] that was passed during init
      96              : //! and updated with every `schedule_*` function call.
      97              : //! All this is necessary necessary to compute the future [`IndexPart`]s
      98              : //! when scheduling an operation while other operations that also affect the
      99              : //! remote [`IndexPart`] are in flight.
     100              : //!
     101              : //! # Retries & Error Handling
     102              : //!
     103              : //! The client retries operations indefinitely, using exponential back-off.
     104              : //! There is no way to force a retry, i.e., interrupt the back-off.
     105              : //! This could be built easily.
     106              : //!
     107              : //! # Cancellation
     108              : //!
     109              : //! The operations execute as plain [`task_mgr`] tasks, scoped to
     110              : //! the client's tenant and timeline.
     111              : //! Dropping the client will drop queued operations but not executing operations.
     112              : //! These will complete unless the `task_mgr` tasks are cancelled using `task_mgr`
     113              : //! APIs, e.g., during pageserver shutdown, timeline delete, or tenant detach.
     114              : //!
     115              : //! # Completion
     116              : //!
     117              : //! Once an operation has completed, we update [`UploadQueueInitialized::clean`] immediately,
     118              : //! and submit a request through the DeletionQueue to update
     119              : //! [`UploadQueueInitialized::visible_remote_consistent_lsn`] after it has
     120              : //! validated that our generation is not stale.  It is this visible value
     121              : //! that is advertized to safekeepers as a signal that that they can
     122              : //! delete the WAL up to that LSN.
     123              : //!
     124              : //! The [`RemoteTimelineClient::wait_completion`] method can be used to wait
     125              : //! for all pending operations to complete. It does not prevent more
     126              : //! operations from getting scheduled.
     127              : //!
     128              : //! # Crash Consistency
     129              : //!
     130              : //! We do not persist the upload queue state.
     131              : //! If we drop the client, or crash, all unfinished operations are lost.
     132              : //!
     133              : //! To recover, the following steps need to be taken:
     134              : //! - Retrieve the current remote [`IndexPart`]. This gives us a
     135              : //!   consistent remote state, assuming the user scheduled the operations in
     136              : //!   the correct order.
     137              : //! - Initiate upload queue with that [`IndexPart`].
     138              : //! - Reschedule all lost operations by comparing the local filesystem state
     139              : //!   and remote state as per [`IndexPart`]. This is done in
     140              : //!   [`Tenant::timeline_init_and_sync`].
     141              : //!
     142              : //! Note that if we crash during file deletion between the index update
     143              : //! that removes the file from the list of files, and deleting the remote file,
     144              : //! the file is leaked in the remote storage. Similarly, if a new file is created
     145              : //! and uploaded, but the pageserver dies permanently before updating the
     146              : //! remote index file, the new file is leaked in remote storage. We accept and
     147              : //! tolerate that for now.
     148              : //! Note further that we cannot easily fix this by scheduling deletes for every
     149              : //! file that is present only on the remote, because we cannot distinguish the
     150              : //! following two cases:
     151              : //! - (1) We had the file locally, deleted it locally, scheduled a remote delete,
     152              : //!   but crashed before it finished remotely.
     153              : //! - (2) We never had the file locally because we haven't on-demand downloaded
     154              : //!   it yet.
     155              : //!
     156              : //! # Downloads
     157              : //!
     158              : //! In addition to the upload queue, [`RemoteTimelineClient`] has functions for
     159              : //! downloading files from the remote storage. Downloads are performed immediately
     160              : //! against the `RemoteStorage`, independently of the upload queue.
     161              : //!
     162              : //! When we attach a tenant, we perform the following steps:
     163              : //! - create `Tenant` object in `TenantState::Attaching` state
     164              : //! - List timelines that are present in remote storage, and for each:
     165              : //!   - download their remote [`IndexPart`]s
     166              : //!   - create `Timeline` struct and a `RemoteTimelineClient`
     167              : //!   - initialize the client's upload queue with its `IndexPart`
     168              : //!   - schedule uploads for layers that are only present locally.
     169              : //! - After the above is done for each timeline, open the tenant for business by
     170              : //!   transitioning it from `TenantState::Attaching` to `TenantState::Active` state.
     171              : //!   This starts the timelines' WAL-receivers and the tenant's GC & Compaction loops.
     172              : //!
     173              : //! # Operating Without Remote Storage
     174              : //!
     175              : //! If no remote storage configuration is provided, the [`RemoteTimelineClient`] is
     176              : //! not created and the uploads are skipped.
     177              : //!
     178              : //! [`Tenant::timeline_init_and_sync`]: super::Tenant::timeline_init_and_sync
     179              : //! [`Timeline::load_layer_map`]: super::Timeline::load_layer_map
     180              : 
     181              : pub(crate) mod download;
     182              : pub mod index;
     183              : pub mod manifest;
     184              : pub(crate) mod upload;
     185              : 
     186              : use anyhow::Context;
     187              : use camino::Utf8Path;
     188              : use chrono::{NaiveDateTime, Utc};
     189              : 
     190              : pub(crate) use download::download_initdb_tar_zst;
     191              : use pageserver_api::models::TimelineArchivalState;
     192              : use pageserver_api::shard::{ShardIndex, TenantShardId};
     193              : use regex::Regex;
     194              : use scopeguard::ScopeGuard;
     195              : use tokio_util::sync::CancellationToken;
     196              : use utils::backoff::{
     197              :     self, exponential_backoff, DEFAULT_BASE_BACKOFF_SECONDS, DEFAULT_MAX_BACKOFF_SECONDS,
     198              : };
     199              : use utils::pausable_failpoint;
     200              : use utils::shard::ShardNumber;
     201              : 
     202              : use std::collections::{HashMap, HashSet, VecDeque};
     203              : use std::sync::atomic::{AtomicU32, Ordering};
     204              : use std::sync::{Arc, Mutex, OnceLock};
     205              : use std::time::Duration;
     206              : 
     207              : use remote_storage::{
     208              :     DownloadError, GenericRemoteStorage, ListingMode, RemotePath, TimeoutOrCancel,
     209              : };
     210              : use std::ops::DerefMut;
     211              : use tracing::{debug, error, info, instrument, warn};
     212              : use tracing::{info_span, Instrument};
     213              : use utils::lsn::Lsn;
     214              : 
     215              : use crate::context::RequestContext;
     216              : use crate::deletion_queue::{DeletionQueueClient, DeletionQueueError};
     217              : use crate::metrics::{
     218              :     MeasureRemoteOp, RemoteOpFileKind, RemoteOpKind, RemoteTimelineClientMetrics,
     219              :     RemoteTimelineClientMetricsCallTrackSize, REMOTE_ONDEMAND_DOWNLOADED_BYTES,
     220              :     REMOTE_ONDEMAND_DOWNLOADED_LAYERS,
     221              : };
     222              : use crate::task_mgr::shutdown_token;
     223              : use crate::tenant::debug_assert_current_span_has_tenant_and_timeline_id;
     224              : use crate::tenant::remote_timeline_client::download::download_retry;
     225              : use crate::tenant::storage_layer::AsLayerDesc;
     226              : use crate::tenant::upload_queue::{Delete, OpType, UploadQueueStoppedDeletable};
     227              : use crate::tenant::TIMELINES_SEGMENT_NAME;
     228              : use crate::{
     229              :     config::PageServerConf,
     230              :     task_mgr,
     231              :     task_mgr::TaskKind,
     232              :     task_mgr::BACKGROUND_RUNTIME,
     233              :     tenant::metadata::TimelineMetadata,
     234              :     tenant::upload_queue::{
     235              :         UploadOp, UploadQueue, UploadQueueInitialized, UploadQueueStopped, UploadTask,
     236              :     },
     237              :     TENANT_HEATMAP_BASENAME,
     238              : };
     239              : 
     240              : use utils::id::{TenantId, TimelineId};
     241              : 
     242              : use self::index::IndexPart;
     243              : 
     244              : use super::config::AttachedLocationConfig;
     245              : use super::metadata::MetadataUpdate;
     246              : use super::storage_layer::{Layer, LayerName, ResidentLayer};
     247              : use super::timeline::import_pgdata;
     248              : use super::upload_queue::{NotInitialized, SetDeletedFlagProgress};
     249              : use super::{DeleteTimelineError, Generation};
     250              : 
     251              : pub(crate) use download::{
     252              :     download_index_part, download_tenant_manifest, is_temp_download_file,
     253              :     list_remote_tenant_shards, list_remote_timelines,
     254              : };
     255              : pub(crate) use index::LayerFileMetadata;
     256              : pub(crate) use upload::upload_initdb_dir;
     257              : 
     258              : // Occasional network issues and such can cause remote operations to fail, and
     259              : // that's expected. If a download fails, we log it at info-level, and retry.
     260              : // But after FAILED_DOWNLOAD_WARN_THRESHOLD retries, we start to log it at WARN
     261              : // level instead, as repeated failures can mean a more serious problem. If it
     262              : // fails more than FAILED_DOWNLOAD_RETRIES times, we give up
     263              : pub(crate) const FAILED_DOWNLOAD_WARN_THRESHOLD: u32 = 3;
     264              : pub(crate) const FAILED_REMOTE_OP_RETRIES: u32 = 10;
     265              : 
     266              : // Similarly log failed uploads and deletions at WARN level, after this many
     267              : // retries. Uploads and deletions are retried forever, though.
     268              : pub(crate) const FAILED_UPLOAD_WARN_THRESHOLD: u32 = 3;
     269              : 
     270              : pub(crate) const INITDB_PATH: &str = "initdb.tar.zst";
     271              : 
     272              : pub(crate) const INITDB_PRESERVED_PATH: &str = "initdb-preserved.tar.zst";
     273              : 
     274              : /// Default buffer size when interfacing with [`tokio::fs::File`].
     275              : pub(crate) const BUFFER_SIZE: usize = 32 * 1024;
     276              : 
     277              : /// Doing non-essential flushes of deletion queue is subject to this timeout, after
     278              : /// which we warn and skip.
     279              : const DELETION_QUEUE_FLUSH_TIMEOUT: Duration = Duration::from_secs(10);
     280              : 
     281              : pub enum MaybeDeletedIndexPart {
     282              :     IndexPart(IndexPart),
     283              :     Deleted(IndexPart),
     284              : }
     285              : 
     286            0 : #[derive(Debug, thiserror::Error)]
     287              : pub enum PersistIndexPartWithDeletedFlagError {
     288              :     #[error("another task is already setting the deleted_flag, started at {0:?}")]
     289              :     AlreadyInProgress(NaiveDateTime),
     290              :     #[error("the deleted_flag was already set, value is {0:?}")]
     291              :     AlreadyDeleted(NaiveDateTime),
     292              :     #[error(transparent)]
     293              :     Other(#[from] anyhow::Error),
     294              : }
     295              : 
     296            0 : #[derive(Debug, thiserror::Error)]
     297              : pub enum WaitCompletionError {
     298              :     #[error(transparent)]
     299              :     NotInitialized(NotInitialized),
     300              :     #[error("wait_completion aborted because upload queue was stopped")]
     301              :     UploadQueueShutDownOrStopped,
     302              : }
     303              : 
     304            0 : #[derive(Debug, thiserror::Error)]
     305              : #[error("Upload queue either in unexpected state or hasn't downloaded manifest yet")]
     306              : pub struct UploadQueueNotReadyError;
     307              : /// Behavioral modes that enable seamless live migration.
     308              : ///
     309              : /// See docs/rfcs/028-pageserver-migration.md to understand how these fit in.
     310              : struct RemoteTimelineClientConfig {
     311              :     /// If this is false, then update to remote_consistent_lsn are dropped rather
     312              :     /// than being submitted to DeletionQueue for validation.  This behavior is
     313              :     /// used when a tenant attachment is known to have a stale generation number,
     314              :     /// such that validation attempts will always fail.  This is not necessary
     315              :     /// for correctness, but avoids spamming error statistics with failed validations
     316              :     /// when doing migrations of tenants.
     317              :     process_remote_consistent_lsn_updates: bool,
     318              : 
     319              :     /// If this is true, then object deletions are held in a buffer in RemoteTimelineClient
     320              :     /// rather than being submitted to the DeletionQueue.  This behavior is used when a tenant
     321              :     /// is known to be multi-attached, in order to avoid disrupting other attached tenants
     322              :     /// whose generations' metadata refers to the deleted objects.
     323              :     block_deletions: bool,
     324              : }
     325              : 
     326              : /// RemoteTimelineClientConfig's state is entirely driven by LocationConf, but we do
     327              : /// not carry the entire LocationConf structure: it's much more than we need.  The From
     328              : /// impl extracts the subset of the LocationConf that is interesting to RemoteTimelineClient.
     329              : impl From<&AttachedLocationConfig> for RemoteTimelineClientConfig {
     330          428 :     fn from(lc: &AttachedLocationConfig) -> Self {
     331          428 :         Self {
     332          428 :             block_deletions: !lc.may_delete_layers_hint(),
     333          428 :             process_remote_consistent_lsn_updates: lc.may_upload_layers_hint(),
     334          428 :         }
     335          428 :     }
     336              : }
     337              : 
     338              : /// A client for accessing a timeline's data in remote storage.
     339              : ///
     340              : /// This takes care of managing the number of connections, and balancing them
     341              : /// across tenants. This also handles retries of failed uploads.
     342              : ///
     343              : /// Upload and delete requests are ordered so that before a deletion is
     344              : /// performed, we wait for all preceding uploads to finish. This ensures sure
     345              : /// that if you perform a compaction operation that reshuffles data in layer
     346              : /// files, we don't have a transient state where the old files have already been
     347              : /// deleted, but new files have not yet been uploaded.
     348              : ///
     349              : /// Similarly, this enforces an order between index-file uploads, and layer
     350              : /// uploads.  Before an index-file upload is performed, all preceding layer
     351              : /// uploads must be finished.
     352              : ///
     353              : /// This also maintains a list of remote files, and automatically includes that
     354              : /// in the index part file, whenever timeline metadata is uploaded.
     355              : ///
     356              : /// Downloads are not queued, they are performed immediately.
     357              : pub(crate) struct RemoteTimelineClient {
     358              :     conf: &'static PageServerConf,
     359              : 
     360              :     runtime: tokio::runtime::Handle,
     361              : 
     362              :     tenant_shard_id: TenantShardId,
     363              :     timeline_id: TimelineId,
     364              :     generation: Generation,
     365              : 
     366              :     upload_queue: Mutex<UploadQueue>,
     367              : 
     368              :     pub(crate) metrics: Arc<RemoteTimelineClientMetrics>,
     369              : 
     370              :     storage_impl: GenericRemoteStorage,
     371              : 
     372              :     deletion_queue_client: DeletionQueueClient,
     373              : 
     374              :     /// Subset of tenant configuration used to control upload behaviors during migrations
     375              :     config: std::sync::RwLock<RemoteTimelineClientConfig>,
     376              : 
     377              :     cancel: CancellationToken,
     378              : }
     379              : 
     380              : impl RemoteTimelineClient {
     381              :     ///
     382              :     /// Create a remote storage client for given timeline
     383              :     ///
     384              :     /// Note: the caller must initialize the upload queue before any uploads can be scheduled,
     385              :     /// by calling init_upload_queue.
     386              :     ///
     387          418 :     pub(crate) fn new(
     388          418 :         remote_storage: GenericRemoteStorage,
     389          418 :         deletion_queue_client: DeletionQueueClient,
     390          418 :         conf: &'static PageServerConf,
     391          418 :         tenant_shard_id: TenantShardId,
     392          418 :         timeline_id: TimelineId,
     393          418 :         generation: Generation,
     394          418 :         location_conf: &AttachedLocationConfig,
     395          418 :     ) -> RemoteTimelineClient {
     396          418 :         RemoteTimelineClient {
     397          418 :             conf,
     398          418 :             runtime: if cfg!(test) {
     399              :                 // remote_timeline_client.rs tests rely on current-thread runtime
     400          418 :                 tokio::runtime::Handle::current()
     401              :             } else {
     402            0 :                 BACKGROUND_RUNTIME.handle().clone()
     403              :             },
     404          418 :             tenant_shard_id,
     405          418 :             timeline_id,
     406          418 :             generation,
     407          418 :             storage_impl: remote_storage,
     408          418 :             deletion_queue_client,
     409          418 :             upload_queue: Mutex::new(UploadQueue::Uninitialized),
     410          418 :             metrics: Arc::new(RemoteTimelineClientMetrics::new(
     411          418 :                 &tenant_shard_id,
     412          418 :                 &timeline_id,
     413          418 :             )),
     414          418 :             config: std::sync::RwLock::new(RemoteTimelineClientConfig::from(location_conf)),
     415          418 :             cancel: CancellationToken::new(),
     416          418 :         }
     417          418 :     }
     418              : 
     419              :     /// Initialize the upload queue for a remote storage that already received
     420              :     /// an index file upload, i.e., it's not empty.
     421              :     /// The given `index_part` must be the one on the remote.
     422            6 :     pub fn init_upload_queue(&self, index_part: &IndexPart) -> anyhow::Result<()> {
     423            6 :         let mut upload_queue = self.upload_queue.lock().unwrap();
     424            6 :         upload_queue.initialize_with_current_remote_index_part(index_part)?;
     425            6 :         self.update_remote_physical_size_gauge(Some(index_part));
     426            6 :         info!(
     427            0 :             "initialized upload queue from remote index with {} layer files",
     428            0 :             index_part.layer_metadata.len()
     429              :         );
     430            6 :         Ok(())
     431            6 :     }
     432              : 
     433              :     /// Initialize the upload queue for the case where the remote storage is empty,
     434              :     /// i.e., it doesn't have an `IndexPart`.
     435          412 :     pub fn init_upload_queue_for_empty_remote(
     436          412 :         &self,
     437          412 :         local_metadata: &TimelineMetadata,
     438          412 :     ) -> anyhow::Result<()> {
     439          412 :         let mut upload_queue = self.upload_queue.lock().unwrap();
     440          412 :         upload_queue.initialize_empty_remote(local_metadata)?;
     441          412 :         self.update_remote_physical_size_gauge(None);
     442          412 :         info!("initialized upload queue as empty");
     443          412 :         Ok(())
     444          412 :     }
     445              : 
     446              :     /// Initialize the queue in stopped state. Used in startup path
     447              :     /// to continue deletion operation interrupted by pageserver crash or restart.
     448            0 :     pub fn init_upload_queue_stopped_to_continue_deletion(
     449            0 :         &self,
     450            0 :         index_part: &IndexPart,
     451            0 :     ) -> anyhow::Result<()> {
     452              :         // FIXME: consider newtype for DeletedIndexPart.
     453            0 :         let deleted_at = index_part.deleted_at.ok_or(anyhow::anyhow!(
     454            0 :             "bug: it is responsibility of the caller to provide index part from MaybeDeletedIndexPart::Deleted"
     455            0 :         ))?;
     456              : 
     457            0 :         let mut upload_queue = self.upload_queue.lock().unwrap();
     458            0 :         upload_queue.initialize_with_current_remote_index_part(index_part)?;
     459            0 :         self.update_remote_physical_size_gauge(Some(index_part));
     460            0 :         self.stop_impl(&mut upload_queue);
     461            0 : 
     462            0 :         upload_queue
     463            0 :             .stopped_mut()
     464            0 :             .expect("stopped above")
     465            0 :             .deleted_at = SetDeletedFlagProgress::Successful(deleted_at);
     466            0 : 
     467            0 :         Ok(())
     468            0 :     }
     469              : 
     470              :     /// Notify this client of a change to its parent tenant's config, as this may cause us to
     471              :     /// take action (unblocking deletions when transitioning from AttachedMulti to AttachedSingle)
     472            0 :     pub(super) fn update_config(&self, location_conf: &AttachedLocationConfig) {
     473            0 :         let new_conf = RemoteTimelineClientConfig::from(location_conf);
     474            0 :         let unblocked = !new_conf.block_deletions;
     475            0 : 
     476            0 :         // Update config before draining deletions, so that we don't race with more being
     477            0 :         // inserted.  This can result in deletions happening our of order, but that does not
     478            0 :         // violate any invariants: deletions only need to be ordered relative to upload of the index
     479            0 :         // that dereferences the deleted objects, and we are not changing that order.
     480            0 :         *self.config.write().unwrap() = new_conf;
     481            0 : 
     482            0 :         if unblocked {
     483              :             // If we may now delete layers, drain any that were blocked in our old
     484              :             // configuration state
     485            0 :             let mut queue_locked = self.upload_queue.lock().unwrap();
     486              : 
     487            0 :             if let Ok(queue) = queue_locked.initialized_mut() {
     488            0 :                 let blocked_deletions = std::mem::take(&mut queue.blocked_deletions);
     489            0 :                 for d in blocked_deletions {
     490            0 :                     if let Err(e) = self.deletion_queue_client.push_layers_sync(
     491            0 :                         self.tenant_shard_id,
     492            0 :                         self.timeline_id,
     493            0 :                         self.generation,
     494            0 :                         d.layers,
     495            0 :                     ) {
     496              :                         // This could happen if the pageserver is shut down while a tenant
     497              :                         // is transitioning from a deletion-blocked state: we will leak some
     498              :                         // S3 objects in this case.
     499            0 :                         warn!("Failed to drain blocked deletions: {}", e);
     500            0 :                         break;
     501            0 :                     }
     502              :                 }
     503            0 :             }
     504            0 :         }
     505            0 :     }
     506              : 
     507              :     /// Returns `None` if nothing is yet uplodaded, `Some(disk_consistent_lsn)` otherwise.
     508            0 :     pub fn remote_consistent_lsn_projected(&self) -> Option<Lsn> {
     509            0 :         match &mut *self.upload_queue.lock().unwrap() {
     510            0 :             UploadQueue::Uninitialized => None,
     511            0 :             UploadQueue::Initialized(q) => q.get_last_remote_consistent_lsn_projected(),
     512            0 :             UploadQueue::Stopped(UploadQueueStopped::Uninitialized) => None,
     513            0 :             UploadQueue::Stopped(UploadQueueStopped::Deletable(q)) => q
     514            0 :                 .upload_queue_for_deletion
     515            0 :                 .get_last_remote_consistent_lsn_projected(),
     516              :         }
     517            0 :     }
     518              : 
     519            0 :     pub fn remote_consistent_lsn_visible(&self) -> Option<Lsn> {
     520            0 :         match &mut *self.upload_queue.lock().unwrap() {
     521            0 :             UploadQueue::Uninitialized => None,
     522            0 :             UploadQueue::Initialized(q) => Some(q.get_last_remote_consistent_lsn_visible()),
     523            0 :             UploadQueue::Stopped(UploadQueueStopped::Uninitialized) => None,
     524            0 :             UploadQueue::Stopped(UploadQueueStopped::Deletable(q)) => Some(
     525            0 :                 q.upload_queue_for_deletion
     526            0 :                     .get_last_remote_consistent_lsn_visible(),
     527            0 :             ),
     528              :         }
     529            0 :     }
     530              : 
     531              :     /// Returns true if this timeline was previously detached at this Lsn and the remote timeline
     532              :     /// client is currently initialized.
     533            0 :     pub(crate) fn is_previous_ancestor_lsn(&self, lsn: Lsn) -> bool {
     534            0 :         self.upload_queue
     535            0 :             .lock()
     536            0 :             .unwrap()
     537            0 :             .initialized_mut()
     538            0 :             .map(|uq| uq.clean.0.lineage.is_previous_ancestor_lsn(lsn))
     539            0 :             .unwrap_or(false)
     540            0 :     }
     541              : 
     542              :     /// Returns whether the timeline is archived.
     543              :     /// Return None if the remote index_part hasn't been downloaded yet.
     544            2 :     pub(crate) fn is_archived(&self) -> Option<bool> {
     545            2 :         self.upload_queue
     546            2 :             .lock()
     547            2 :             .unwrap()
     548            2 :             .initialized_mut()
     549            2 :             .map(|q| q.clean.0.archived_at.is_some())
     550            2 :             .ok()
     551            2 :     }
     552              : 
     553              :     /// Returns `Ok(Some(timestamp))` if the timeline has been archived, `Ok(None)` if the timeline hasn't been archived.
     554              :     ///
     555              :     /// Return Err(_) if the remote index_part hasn't been downloaded yet, or the timeline hasn't been stopped yet.
     556            2 :     pub(crate) fn archived_at_stopped_queue(
     557            2 :         &self,
     558            2 :     ) -> Result<Option<NaiveDateTime>, UploadQueueNotReadyError> {
     559            2 :         self.upload_queue
     560            2 :             .lock()
     561            2 :             .unwrap()
     562            2 :             .stopped_mut()
     563            2 :             .map(|q| q.upload_queue_for_deletion.clean.0.archived_at)
     564            2 :             .map_err(|_| UploadQueueNotReadyError)
     565            2 :     }
     566              : 
     567         1828 :     fn update_remote_physical_size_gauge(&self, current_remote_index_part: Option<&IndexPart>) {
     568         1828 :         let size: u64 = if let Some(current_remote_index_part) = current_remote_index_part {
     569         1416 :             current_remote_index_part
     570         1416 :                 .layer_metadata
     571         1416 :                 .values()
     572        17527 :                 .map(|ilmd| ilmd.file_size)
     573         1416 :                 .sum()
     574              :         } else {
     575          412 :             0
     576              :         };
     577         1828 :         self.metrics.remote_physical_size_gauge.set(size);
     578         1828 :     }
     579              : 
     580            2 :     pub fn get_remote_physical_size(&self) -> u64 {
     581            2 :         self.metrics.remote_physical_size_gauge.get()
     582            2 :     }
     583              : 
     584              :     //
     585              :     // Download operations.
     586              :     //
     587              :     // These don't use the per-timeline queue. They do use the global semaphore in
     588              :     // S3Bucket, to limit the total number of concurrent operations, though.
     589              :     //
     590              : 
     591              :     /// Download index file
     592           20 :     pub async fn download_index_file(
     593           20 :         &self,
     594           20 :         cancel: &CancellationToken,
     595           20 :     ) -> Result<MaybeDeletedIndexPart, DownloadError> {
     596           20 :         let _unfinished_gauge_guard = self.metrics.call_begin(
     597           20 :             &RemoteOpFileKind::Index,
     598           20 :             &RemoteOpKind::Download,
     599           20 :             crate::metrics::RemoteTimelineClientMetricsCallTrackSize::DontTrackSize {
     600           20 :                 reason: "no need for a downloads gauge",
     601           20 :             },
     602           20 :         );
     603              : 
     604           20 :         let (index_part, index_generation, index_last_modified) = download::download_index_part(
     605           20 :             &self.storage_impl,
     606           20 :             &self.tenant_shard_id,
     607           20 :             &self.timeline_id,
     608           20 :             self.generation,
     609           20 :             cancel,
     610           20 :         )
     611           20 :         .measure_remote_op(
     612           20 :             RemoteOpFileKind::Index,
     613           20 :             RemoteOpKind::Download,
     614           20 :             Arc::clone(&self.metrics),
     615           20 :         )
     616           87 :         .await?;
     617              : 
     618              :         // Defense in depth: monotonicity of generation numbers is an important correctness guarantee, so when we see a very
     619              :         // old index, we do extra checks in case this is the result of backward time-travel of the generation number (e.g.
     620              :         // in case of a bug in the service that issues generation numbers). Indices are allowed to be old, but we expect that
     621              :         // when we load an old index we are loading the _latest_ index: if we are asked to load an old index and there is
     622              :         // also a newer index available, that is surprising.
     623              :         const INDEX_AGE_CHECKS_THRESHOLD: Duration = Duration::from_secs(14 * 24 * 3600);
     624           20 :         let index_age = index_last_modified.elapsed().unwrap_or_else(|e| {
     625            0 :             if e.duration() > Duration::from_secs(5) {
     626              :                 // We only warn if the S3 clock and our local clock are >5s out: because this is a low resolution
     627              :                 // timestamp, it is common to be out by at least 1 second.
     628            0 :                 tracing::warn!("Index has modification time in the future: {e}");
     629            0 :             }
     630            0 :             Duration::ZERO
     631           20 :         });
     632           20 :         if index_age > INDEX_AGE_CHECKS_THRESHOLD {
     633            0 :             tracing::info!(
     634              :                 ?index_generation,
     635            0 :                 age = index_age.as_secs_f64(),
     636            0 :                 "Loaded an old index, checking for other indices..."
     637              :             );
     638              : 
     639              :             // Find the highest-generation index
     640            0 :             let (_latest_index_part, latest_index_generation, latest_index_mtime) =
     641            0 :                 download::download_index_part(
     642            0 :                     &self.storage_impl,
     643            0 :                     &self.tenant_shard_id,
     644            0 :                     &self.timeline_id,
     645            0 :                     Generation::MAX,
     646            0 :                     cancel,
     647            0 :                 )
     648            0 :                 .await?;
     649              : 
     650            0 :             if latest_index_generation > index_generation {
     651              :                 // Unexpected!  Why are we loading such an old index if a more recent one exists?
     652              :                 // We will refuse to proceed, as there is no reasonable scenario where this should happen, but
     653              :                 // there _is_ a clear bug/corruption scenario where it would happen (controller sets the generation
     654              :                 // backwards).
     655            0 :                 tracing::error!(
     656              :                     ?index_generation,
     657              :                     ?latest_index_generation,
     658              :                     ?latest_index_mtime,
     659            0 :                     "Found a newer index while loading an old one"
     660              :                 );
     661            0 :                 return Err(DownloadError::Fatal(
     662            0 :                     "Index age exceeds threshold and a newer index exists".into(),
     663            0 :                 ));
     664            0 :             }
     665           20 :         }
     666              : 
     667           20 :         if index_part.deleted_at.is_some() {
     668            0 :             Ok(MaybeDeletedIndexPart::Deleted(index_part))
     669              :         } else {
     670           20 :             Ok(MaybeDeletedIndexPart::IndexPart(index_part))
     671              :         }
     672           20 :     }
     673              : 
     674              :     /// Download a (layer) file from `path`, into local filesystem.
     675              :     ///
     676              :     /// 'layer_metadata' is the metadata from the remote index file.
     677              :     ///
     678              :     /// On success, returns the size of the downloaded file.
     679            6 :     pub async fn download_layer_file(
     680            6 :         &self,
     681            6 :         layer_file_name: &LayerName,
     682            6 :         layer_metadata: &LayerFileMetadata,
     683            6 :         local_path: &Utf8Path,
     684            6 :         cancel: &CancellationToken,
     685            6 :         ctx: &RequestContext,
     686            6 :     ) -> Result<u64, DownloadError> {
     687            6 :         let downloaded_size = {
     688            6 :             let _unfinished_gauge_guard = self.metrics.call_begin(
     689            6 :                 &RemoteOpFileKind::Layer,
     690            6 :                 &RemoteOpKind::Download,
     691            6 :                 crate::metrics::RemoteTimelineClientMetricsCallTrackSize::DontTrackSize {
     692            6 :                     reason: "no need for a downloads gauge",
     693            6 :                 },
     694            6 :             );
     695            6 :             download::download_layer_file(
     696            6 :                 self.conf,
     697            6 :                 &self.storage_impl,
     698            6 :                 self.tenant_shard_id,
     699            6 :                 self.timeline_id,
     700            6 :                 layer_file_name,
     701            6 :                 layer_metadata,
     702            6 :                 local_path,
     703            6 :                 cancel,
     704            6 :                 ctx,
     705            6 :             )
     706            6 :             .measure_remote_op(
     707            6 :                 RemoteOpFileKind::Layer,
     708            6 :                 RemoteOpKind::Download,
     709            6 :                 Arc::clone(&self.metrics),
     710            6 :             )
     711           64 :             .await?
     712              :         };
     713              : 
     714            6 :         REMOTE_ONDEMAND_DOWNLOADED_LAYERS.inc();
     715            6 :         REMOTE_ONDEMAND_DOWNLOADED_BYTES.inc_by(downloaded_size);
     716            6 : 
     717            6 :         Ok(downloaded_size)
     718            6 :     }
     719              : 
     720              :     //
     721              :     // Upload operations.
     722              :     //
     723              : 
     724              :     /// Launch an index-file upload operation in the background, with
     725              :     /// fully updated metadata.
     726              :     ///
     727              :     /// This should only be used to upload initial metadata to remote storage.
     728              :     ///
     729              :     /// The upload will be added to the queue immediately, but it
     730              :     /// won't be performed until all previously scheduled layer file
     731              :     /// upload operations have completed successfully.  This is to
     732              :     /// ensure that when the index file claims that layers X, Y and Z
     733              :     /// exist in remote storage, they really do. To wait for the upload
     734              :     /// to complete, use `wait_completion`.
     735              :     ///
     736              :     /// If there were any changes to the list of files, i.e. if any
     737              :     /// layer file uploads were scheduled, since the last index file
     738              :     /// upload, those will be included too.
     739          230 :     pub fn schedule_index_upload_for_full_metadata_update(
     740          230 :         self: &Arc<Self>,
     741          230 :         metadata: &TimelineMetadata,
     742          230 :     ) -> anyhow::Result<()> {
     743          230 :         let mut guard = self.upload_queue.lock().unwrap();
     744          230 :         let upload_queue = guard.initialized_mut()?;
     745              : 
     746              :         // As documented in the struct definition, it's ok for latest_metadata to be
     747              :         // ahead of what's _actually_ on the remote during index upload.
     748          230 :         upload_queue.dirty.metadata = metadata.clone();
     749          230 : 
     750          230 :         self.schedule_index_upload(upload_queue)?;
     751              : 
     752          230 :         Ok(())
     753          230 :     }
     754              : 
     755              :     /// Launch an index-file upload operation in the background, with only parts of the metadata
     756              :     /// updated.
     757              :     ///
     758              :     /// This is the regular way of updating metadata on layer flushes or Gc.
     759              :     ///
     760              :     /// Using this lighter update mechanism allows for reparenting and detaching without changes to
     761              :     /// `index_part.json`, while being more clear on what values update regularly.
     762         1146 :     pub(crate) fn schedule_index_upload_for_metadata_update(
     763         1146 :         self: &Arc<Self>,
     764         1146 :         update: &MetadataUpdate,
     765         1146 :     ) -> anyhow::Result<()> {
     766         1146 :         let mut guard = self.upload_queue.lock().unwrap();
     767         1146 :         let upload_queue = guard.initialized_mut()?;
     768              : 
     769         1146 :         upload_queue.dirty.metadata.apply(update);
     770         1146 : 
     771         1146 :         self.schedule_index_upload(upload_queue)?;
     772              : 
     773         1146 :         Ok(())
     774         1146 :     }
     775              : 
     776              :     /// Launch an index-file upload operation in the background, with only the `archived_at` field updated.
     777              :     ///
     778              :     /// Returns whether it is required to wait for the queue to be empty to ensure that the change is uploaded,
     779              :     /// so either if the change is already sitting in the queue, but not commited yet, or the change has not
     780              :     /// been in the queue yet.
     781            2 :     pub(crate) fn schedule_index_upload_for_timeline_archival_state(
     782            2 :         self: &Arc<Self>,
     783            2 :         state: TimelineArchivalState,
     784            2 :     ) -> anyhow::Result<bool> {
     785            2 :         let mut guard = self.upload_queue.lock().unwrap();
     786            2 :         let upload_queue = guard.initialized_mut()?;
     787              : 
     788              :         /// Returns Some(_) if a change is needed, and Some(true) if it's a
     789              :         /// change needed to set archived_at.
     790            4 :         fn need_change(
     791            4 :             archived_at: &Option<NaiveDateTime>,
     792            4 :             state: TimelineArchivalState,
     793            4 :         ) -> Option<bool> {
     794            4 :             match (archived_at, state) {
     795              :                 (Some(_), TimelineArchivalState::Archived)
     796              :                 | (None, TimelineArchivalState::Unarchived) => {
     797              :                     // Nothing to do
     798            0 :                     tracing::info!("intended state matches present state");
     799            0 :                     None
     800              :                 }
     801            4 :                 (None, TimelineArchivalState::Archived) => Some(true),
     802            0 :                 (Some(_), TimelineArchivalState::Unarchived) => Some(false),
     803              :             }
     804            4 :         }
     805            2 :         let need_upload_scheduled = need_change(&upload_queue.dirty.archived_at, state);
     806              : 
     807            2 :         if let Some(archived_at_set) = need_upload_scheduled {
     808            2 :             let intended_archived_at = archived_at_set.then(|| Utc::now().naive_utc());
     809            2 :             upload_queue.dirty.archived_at = intended_archived_at;
     810            2 :             self.schedule_index_upload(upload_queue)?;
     811            0 :         }
     812              : 
     813            2 :         let need_wait = need_change(&upload_queue.clean.0.archived_at, state).is_some();
     814            2 :         Ok(need_wait)
     815            2 :     }
     816              : 
     817              :     /// Launch an index-file upload operation in the background, setting `import_pgdata` field.
     818            0 :     pub(crate) fn schedule_index_upload_for_import_pgdata_state_update(
     819            0 :         self: &Arc<Self>,
     820            0 :         state: Option<import_pgdata::index_part_format::Root>,
     821            0 :     ) -> anyhow::Result<()> {
     822            0 :         let mut guard = self.upload_queue.lock().unwrap();
     823            0 :         let upload_queue = guard.initialized_mut()?;
     824            0 :         upload_queue.dirty.import_pgdata = state;
     825            0 :         self.schedule_index_upload(upload_queue)?;
     826            0 :         Ok(())
     827            0 :     }
     828              : 
     829              :     ///
     830              :     /// Launch an index-file upload operation in the background, if necessary.
     831              :     ///
     832              :     /// Use this function to schedule the update of the index file after
     833              :     /// scheduling file uploads or deletions. If no file uploads or deletions
     834              :     /// have been scheduled since the last index file upload, this does
     835              :     /// nothing.
     836              :     ///
     837              :     /// Like schedule_index_upload_for_metadata_update(), this merely adds
     838              :     /// the upload to the upload queue and returns quickly.
     839          370 :     pub fn schedule_index_upload_for_file_changes(self: &Arc<Self>) -> Result<(), NotInitialized> {
     840          370 :         let mut guard = self.upload_queue.lock().unwrap();
     841          370 :         let upload_queue = guard.initialized_mut()?;
     842              : 
     843          370 :         if upload_queue.latest_files_changes_since_metadata_upload_scheduled > 0 {
     844           14 :             self.schedule_index_upload(upload_queue)?;
     845          356 :         }
     846              : 
     847          370 :         Ok(())
     848          370 :     }
     849              : 
     850              :     /// Launch an index-file upload operation in the background (internal function)
     851         1452 :     fn schedule_index_upload(
     852         1452 :         self: &Arc<Self>,
     853         1452 :         upload_queue: &mut UploadQueueInitialized,
     854         1452 :     ) -> Result<(), NotInitialized> {
     855         1452 :         let disk_consistent_lsn = upload_queue.dirty.metadata.disk_consistent_lsn();
     856         1452 :         // fix up the duplicated field
     857         1452 :         upload_queue.dirty.disk_consistent_lsn = disk_consistent_lsn;
     858         1452 : 
     859         1452 :         // make sure it serializes before doing it in perform_upload_task so that it doesn't
     860         1452 :         // look like a retryable error
     861         1452 :         let void = std::io::sink();
     862         1452 :         serde_json::to_writer(void, &upload_queue.dirty).expect("serialize index_part.json");
     863         1452 : 
     864         1452 :         let index_part = &upload_queue.dirty;
     865         1452 : 
     866         1452 :         info!(
     867            0 :             "scheduling metadata upload up to consistent LSN {disk_consistent_lsn} with {} files ({} changed)",
     868            0 :             index_part.layer_metadata.len(),
     869              :             upload_queue.latest_files_changes_since_metadata_upload_scheduled,
     870              :         );
     871              : 
     872         1452 :         let op = UploadOp::UploadMetadata {
     873         1452 :             uploaded: Box::new(index_part.clone()),
     874         1452 :         };
     875         1452 :         self.metric_begin(&op);
     876         1452 :         upload_queue.queued_operations.push_back(op);
     877         1452 :         upload_queue.latest_files_changes_since_metadata_upload_scheduled = 0;
     878         1452 : 
     879         1452 :         // Launch the task immediately, if possible
     880         1452 :         self.launch_queued_tasks(upload_queue);
     881         1452 :         Ok(())
     882         1452 :     }
     883              : 
     884              :     /// Reparent this timeline to a new parent.
     885              :     ///
     886              :     /// A retryable step of timeline ancestor detach.
     887            0 :     pub(crate) async fn schedule_reparenting_and_wait(
     888            0 :         self: &Arc<Self>,
     889            0 :         new_parent: &TimelineId,
     890            0 :     ) -> anyhow::Result<()> {
     891            0 :         let receiver = {
     892            0 :             let mut guard = self.upload_queue.lock().unwrap();
     893            0 :             let upload_queue = guard.initialized_mut()?;
     894              : 
     895            0 :             let Some(prev) = upload_queue.dirty.metadata.ancestor_timeline() else {
     896            0 :                 return Err(anyhow::anyhow!(
     897            0 :                     "cannot reparent without a current ancestor"
     898            0 :                 ));
     899              :             };
     900              : 
     901            0 :             let uploaded = &upload_queue.clean.0.metadata;
     902            0 : 
     903            0 :             if uploaded.ancestor_timeline().is_none() && !uploaded.ancestor_lsn().is_valid() {
     904              :                 // nothing to do
     905            0 :                 None
     906              :             } else {
     907            0 :                 upload_queue.dirty.metadata.reparent(new_parent);
     908            0 :                 upload_queue.dirty.lineage.record_previous_ancestor(&prev);
     909            0 : 
     910            0 :                 self.schedule_index_upload(upload_queue)?;
     911              : 
     912            0 :                 Some(self.schedule_barrier0(upload_queue))
     913              :             }
     914              :         };
     915              : 
     916            0 :         if let Some(receiver) = receiver {
     917            0 :             Self::wait_completion0(receiver).await?;
     918            0 :         }
     919            0 :         Ok(())
     920            0 :     }
     921              : 
     922              :     /// Schedules uploading a new version of `index_part.json` with the given layers added,
     923              :     /// detaching from ancestor and waits for it to complete.
     924              :     ///
     925              :     /// This is used with `Timeline::detach_ancestor` functionality.
     926            0 :     pub(crate) async fn schedule_adding_existing_layers_to_index_detach_and_wait(
     927            0 :         self: &Arc<Self>,
     928            0 :         layers: &[Layer],
     929            0 :         adopted: (TimelineId, Lsn),
     930            0 :     ) -> anyhow::Result<()> {
     931            0 :         let barrier = {
     932            0 :             let mut guard = self.upload_queue.lock().unwrap();
     933            0 :             let upload_queue = guard.initialized_mut()?;
     934              : 
     935            0 :             if upload_queue.clean.0.lineage.detached_previous_ancestor() == Some(adopted) {
     936            0 :                 None
     937              :             } else {
     938            0 :                 upload_queue.dirty.metadata.detach_from_ancestor(&adopted);
     939            0 :                 upload_queue.dirty.lineage.record_detaching(&adopted);
     940              : 
     941            0 :                 for layer in layers {
     942            0 :                     let prev = upload_queue
     943            0 :                         .dirty
     944            0 :                         .layer_metadata
     945            0 :                         .insert(layer.layer_desc().layer_name(), layer.metadata());
     946            0 :                     assert!(prev.is_none(), "copied layer existed already {layer}");
     947              :                 }
     948              : 
     949            0 :                 self.schedule_index_upload(upload_queue)?;
     950              : 
     951            0 :                 Some(self.schedule_barrier0(upload_queue))
     952              :             }
     953              :         };
     954              : 
     955            0 :         if let Some(barrier) = barrier {
     956            0 :             Self::wait_completion0(barrier).await?;
     957            0 :         }
     958            0 :         Ok(())
     959            0 :     }
     960              : 
     961              :     /// Adds a gc blocking reason for this timeline if one does not exist already.
     962              :     ///
     963              :     /// A retryable step of timeline detach ancestor.
     964              :     ///
     965              :     /// Returns a future which waits until the completion of the upload.
     966            0 :     pub(crate) fn schedule_insert_gc_block_reason(
     967            0 :         self: &Arc<Self>,
     968            0 :         reason: index::GcBlockingReason,
     969            0 :     ) -> Result<impl std::future::Future<Output = Result<(), WaitCompletionError>>, NotInitialized>
     970            0 :     {
     971            0 :         let maybe_barrier = {
     972            0 :             let mut guard = self.upload_queue.lock().unwrap();
     973            0 :             let upload_queue = guard.initialized_mut()?;
     974              : 
     975            0 :             if let index::GcBlockingReason::DetachAncestor = reason {
     976            0 :                 if upload_queue.dirty.metadata.ancestor_timeline().is_none() {
     977            0 :                     drop(guard);
     978            0 :                     panic!("cannot start detach ancestor if there is nothing to detach from");
     979            0 :                 }
     980            0 :             }
     981              : 
     982            0 :             let wanted = |x: Option<&index::GcBlocking>| x.is_some_and(|x| x.blocked_by(reason));
     983              : 
     984            0 :             let current = upload_queue.dirty.gc_blocking.as_ref();
     985            0 :             let uploaded = upload_queue.clean.0.gc_blocking.as_ref();
     986            0 : 
     987            0 :             match (current, uploaded) {
     988            0 :                 (x, y) if wanted(x) && wanted(y) => None,
     989            0 :                 (x, y) if wanted(x) && !wanted(y) => Some(self.schedule_barrier0(upload_queue)),
     990              :                 // Usual case: !wanted(x) && !wanted(y)
     991              :                 //
     992              :                 // Unusual: !wanted(x) && wanted(y) which means we have two processes waiting to
     993              :                 // turn on and off some reason.
     994            0 :                 (x, y) => {
     995            0 :                     if !wanted(x) && wanted(y) {
     996              :                         // this could be avoided by having external in-memory synchronization, like
     997              :                         // timeline detach ancestor
     998            0 :                         warn!(?reason, op="insert", "unexpected: two racing processes to enable and disable a gc blocking reason");
     999            0 :                     }
    1000              : 
    1001              :                     // at this point, the metadata must always show that there is a parent
    1002            0 :                     upload_queue.dirty.gc_blocking = current
    1003            0 :                         .map(|x| x.with_reason(reason))
    1004            0 :                         .or_else(|| Some(index::GcBlocking::started_now_for(reason)));
    1005            0 :                     self.schedule_index_upload(upload_queue)?;
    1006            0 :                     Some(self.schedule_barrier0(upload_queue))
    1007              :                 }
    1008              :             }
    1009              :         };
    1010              : 
    1011            0 :         Ok(async move {
    1012            0 :             if let Some(barrier) = maybe_barrier {
    1013            0 :                 Self::wait_completion0(barrier).await?;
    1014            0 :             }
    1015            0 :             Ok(())
    1016            0 :         })
    1017            0 :     }
    1018              : 
    1019              :     /// Removes a gc blocking reason for this timeline if one exists.
    1020              :     ///
    1021              :     /// A retryable step of timeline detach ancestor.
    1022              :     ///
    1023              :     /// Returns a future which waits until the completion of the upload.
    1024            0 :     pub(crate) fn schedule_remove_gc_block_reason(
    1025            0 :         self: &Arc<Self>,
    1026            0 :         reason: index::GcBlockingReason,
    1027            0 :     ) -> Result<impl std::future::Future<Output = Result<(), WaitCompletionError>>, NotInitialized>
    1028            0 :     {
    1029            0 :         let maybe_barrier = {
    1030            0 :             let mut guard = self.upload_queue.lock().unwrap();
    1031            0 :             let upload_queue = guard.initialized_mut()?;
    1032              : 
    1033            0 :             if let index::GcBlockingReason::DetachAncestor = reason {
    1034            0 :                 if !upload_queue.clean.0.lineage.is_detached_from_ancestor() {
    1035            0 :                     drop(guard);
    1036            0 :                     panic!("cannot complete timeline_ancestor_detach while not detached");
    1037            0 :                 }
    1038            0 :             }
    1039              : 
    1040            0 :             let wanted = |x: Option<&index::GcBlocking>| {
    1041            0 :                 x.is_none() || x.is_some_and(|b| !b.blocked_by(reason))
    1042            0 :             };
    1043              : 
    1044            0 :             let current = upload_queue.dirty.gc_blocking.as_ref();
    1045            0 :             let uploaded = upload_queue.clean.0.gc_blocking.as_ref();
    1046            0 : 
    1047            0 :             match (current, uploaded) {
    1048            0 :                 (x, y) if wanted(x) && wanted(y) => None,
    1049            0 :                 (x, y) if wanted(x) && !wanted(y) => Some(self.schedule_barrier0(upload_queue)),
    1050            0 :                 (x, y) => {
    1051            0 :                     if !wanted(x) && wanted(y) {
    1052            0 :                         warn!(?reason, op="remove", "unexpected: two racing processes to enable and disable a gc blocking reason (remove)");
    1053            0 :                     }
    1054              : 
    1055            0 :                     upload_queue.dirty.gc_blocking =
    1056            0 :                         current.as_ref().and_then(|x| x.without_reason(reason));
    1057            0 :                     assert!(wanted(upload_queue.dirty.gc_blocking.as_ref()));
    1058              :                     // FIXME: bogus ?
    1059            0 :                     self.schedule_index_upload(upload_queue)?;
    1060            0 :                     Some(self.schedule_barrier0(upload_queue))
    1061              :                 }
    1062              :             }
    1063              :         };
    1064              : 
    1065            0 :         Ok(async move {
    1066            0 :             if let Some(barrier) = maybe_barrier {
    1067            0 :                 Self::wait_completion0(barrier).await?;
    1068            0 :             }
    1069            0 :             Ok(())
    1070            0 :         })
    1071            0 :     }
    1072              : 
    1073              :     /// Launch an upload operation in the background; the file is added to be included in next
    1074              :     /// `index_part.json` upload.
    1075         1190 :     pub(crate) fn schedule_layer_file_upload(
    1076         1190 :         self: &Arc<Self>,
    1077         1190 :         layer: ResidentLayer,
    1078         1190 :     ) -> Result<(), NotInitialized> {
    1079         1190 :         let mut guard = self.upload_queue.lock().unwrap();
    1080         1190 :         let upload_queue = guard.initialized_mut()?;
    1081              : 
    1082         1190 :         self.schedule_layer_file_upload0(upload_queue, layer);
    1083         1190 :         self.launch_queued_tasks(upload_queue);
    1084         1190 :         Ok(())
    1085         1190 :     }
    1086              : 
    1087         1538 :     fn schedule_layer_file_upload0(
    1088         1538 :         self: &Arc<Self>,
    1089         1538 :         upload_queue: &mut UploadQueueInitialized,
    1090         1538 :         layer: ResidentLayer,
    1091         1538 :     ) {
    1092         1538 :         let metadata = layer.metadata();
    1093         1538 : 
    1094         1538 :         upload_queue
    1095         1538 :             .dirty
    1096         1538 :             .layer_metadata
    1097         1538 :             .insert(layer.layer_desc().layer_name(), metadata.clone());
    1098         1538 :         upload_queue.latest_files_changes_since_metadata_upload_scheduled += 1;
    1099         1538 : 
    1100         1538 :         info!(
    1101              :             gen=?metadata.generation,
    1102              :             shard=?metadata.shard,
    1103            0 :             "scheduled layer file upload {layer}",
    1104              :         );
    1105              : 
    1106         1538 :         let op = UploadOp::UploadLayer(layer, metadata, None);
    1107         1538 :         self.metric_begin(&op);
    1108         1538 :         upload_queue.queued_operations.push_back(op);
    1109         1538 :     }
    1110              : 
    1111              :     /// Launch a delete operation in the background.
    1112              :     ///
    1113              :     /// The operation does not modify local filesystem state.
    1114              :     ///
    1115              :     /// Note: This schedules an index file upload before the deletions.  The
    1116              :     /// deletion won't actually be performed, until all previously scheduled
    1117              :     /// upload operations, and the index file upload, have completed
    1118              :     /// successfully.
    1119            8 :     pub fn schedule_layer_file_deletion(
    1120            8 :         self: &Arc<Self>,
    1121            8 :         names: &[LayerName],
    1122            8 :     ) -> anyhow::Result<()> {
    1123            8 :         let mut guard = self.upload_queue.lock().unwrap();
    1124            8 :         let upload_queue = guard.initialized_mut()?;
    1125              : 
    1126            8 :         let with_metadata = self
    1127            8 :             .schedule_unlinking_of_layers_from_index_part0(upload_queue, names.iter().cloned())?;
    1128              : 
    1129            8 :         self.schedule_deletion_of_unlinked0(upload_queue, with_metadata);
    1130            8 : 
    1131            8 :         // Launch the tasks immediately, if possible
    1132            8 :         self.launch_queued_tasks(upload_queue);
    1133            8 :         Ok(())
    1134            8 :     }
    1135              : 
    1136              :     /// Unlinks the layer files from `index_part.json` but does not yet schedule deletion for the
    1137              :     /// layer files, leaving them dangling.
    1138              :     ///
    1139              :     /// The files will be leaked in remote storage unless [`Self::schedule_deletion_of_unlinked`]
    1140              :     /// is invoked on them.
    1141            4 :     pub(crate) fn schedule_gc_update(
    1142            4 :         self: &Arc<Self>,
    1143            4 :         gc_layers: &[Layer],
    1144            4 :     ) -> Result<(), NotInitialized> {
    1145            4 :         let mut guard = self.upload_queue.lock().unwrap();
    1146            4 :         let upload_queue = guard.initialized_mut()?;
    1147              : 
    1148              :         // just forget the return value; after uploading the next index_part.json, we can consider
    1149              :         // the layer files as "dangling". this is fine, at worst case we create work for the
    1150              :         // scrubber.
    1151              : 
    1152            4 :         let names = gc_layers.iter().map(|x| x.layer_desc().layer_name());
    1153            4 : 
    1154            4 :         self.schedule_unlinking_of_layers_from_index_part0(upload_queue, names)?;
    1155              : 
    1156            4 :         self.launch_queued_tasks(upload_queue);
    1157            4 : 
    1158            4 :         Ok(())
    1159            4 :     }
    1160              : 
    1161              :     /// Update the remote index file, removing the to-be-deleted files from the index,
    1162              :     /// allowing scheduling of actual deletions later.
    1163           76 :     fn schedule_unlinking_of_layers_from_index_part0<I>(
    1164           76 :         self: &Arc<Self>,
    1165           76 :         upload_queue: &mut UploadQueueInitialized,
    1166           76 :         names: I,
    1167           76 :     ) -> Result<Vec<(LayerName, LayerFileMetadata)>, NotInitialized>
    1168           76 :     where
    1169           76 :         I: IntoIterator<Item = LayerName>,
    1170           76 :     {
    1171           76 :         // Decorate our list of names with each name's metadata, dropping
    1172           76 :         // names that are unexpectedly missing from our metadata.  This metadata
    1173           76 :         // is later used when physically deleting layers, to construct key paths.
    1174           76 :         let with_metadata: Vec<_> = names
    1175           76 :             .into_iter()
    1176          482 :             .filter_map(|name| {
    1177          482 :                 let meta = upload_queue.dirty.layer_metadata.remove(&name);
    1178              : 
    1179          482 :                 if let Some(meta) = meta {
    1180          436 :                     upload_queue.latest_files_changes_since_metadata_upload_scheduled += 1;
    1181          436 :                     Some((name, meta))
    1182              :                 } else {
    1183              :                     // This can only happen if we forgot to to schedule the file upload
    1184              :                     // before scheduling the delete. Log it because it is a rare/strange
    1185              :                     // situation, and in case something is misbehaving, we'd like to know which
    1186              :                     // layers experienced this.
    1187           46 :                     info!("Deleting layer {name} not found in latest_files list, never uploaded?");
    1188           46 :                     None
    1189              :                 }
    1190          482 :             })
    1191           76 :             .collect();
    1192              : 
    1193              :         #[cfg(feature = "testing")]
    1194          512 :         for (name, metadata) in &with_metadata {
    1195          436 :             let gen = metadata.generation;
    1196          436 :             if let Some(unexpected) = upload_queue.dangling_files.insert(name.to_owned(), gen) {
    1197            0 :                 if unexpected == gen {
    1198            0 :                     tracing::error!("{name} was unlinked twice with same generation");
    1199              :                 } else {
    1200            0 :                     tracing::error!("{name} was unlinked twice with different generations {gen:?} and {unexpected:?}");
    1201              :                 }
    1202          436 :             }
    1203              :         }
    1204              : 
    1205              :         // after unlinking files from the upload_queue.latest_files we must always schedule an
    1206              :         // index_part update, because that needs to be uploaded before we can actually delete the
    1207              :         // files.
    1208           76 :         if upload_queue.latest_files_changes_since_metadata_upload_scheduled > 0 {
    1209           60 :             self.schedule_index_upload(upload_queue)?;
    1210           16 :         }
    1211              : 
    1212           76 :         Ok(with_metadata)
    1213           76 :     }
    1214              : 
    1215              :     /// Schedules deletion for layer files which have previously been unlinked from the
    1216              :     /// `index_part.json` with [`Self::schedule_gc_update`] or [`Self::schedule_compaction_update`].
    1217          479 :     pub(crate) fn schedule_deletion_of_unlinked(
    1218          479 :         self: &Arc<Self>,
    1219          479 :         layers: Vec<(LayerName, LayerFileMetadata)>,
    1220          479 :     ) -> anyhow::Result<()> {
    1221          479 :         let mut guard = self.upload_queue.lock().unwrap();
    1222          479 :         let upload_queue = guard.initialized_mut()?;
    1223              : 
    1224          479 :         self.schedule_deletion_of_unlinked0(upload_queue, layers);
    1225          479 :         self.launch_queued_tasks(upload_queue);
    1226          479 :         Ok(())
    1227          479 :     }
    1228              : 
    1229          487 :     fn schedule_deletion_of_unlinked0(
    1230          487 :         self: &Arc<Self>,
    1231          487 :         upload_queue: &mut UploadQueueInitialized,
    1232          487 :         mut with_metadata: Vec<(LayerName, LayerFileMetadata)>,
    1233          487 :     ) {
    1234          487 :         // Filter out any layers which were not created by this tenant shard.  These are
    1235          487 :         // layers that originate from some ancestor shard after a split, and may still
    1236          487 :         // be referenced by other shards. We are free to delete them locally and remove
    1237          487 :         // them from our index (and would have already done so when we reach this point
    1238          487 :         // in the code), but we may not delete them remotely.
    1239          487 :         with_metadata.retain(|(name, meta)| {
    1240          481 :             let retain = meta.shard.shard_number == self.tenant_shard_id.shard_number
    1241          481 :                 && meta.shard.shard_count == self.tenant_shard_id.shard_count;
    1242          481 :             if !retain {
    1243            0 :                 tracing::debug!(
    1244            0 :                     "Skipping deletion of ancestor-shard layer {name}, from shard {}",
    1245              :                     meta.shard
    1246              :                 );
    1247          481 :             }
    1248          481 :             retain
    1249          487 :         });
    1250              : 
    1251          968 :         for (name, meta) in &with_metadata {
    1252          481 :             info!(
    1253            0 :                 "scheduling deletion of layer {}{} (shard {})",
    1254            0 :                 name,
    1255            0 :                 meta.generation.get_suffix(),
    1256              :                 meta.shard
    1257              :             );
    1258              :         }
    1259              : 
    1260              :         #[cfg(feature = "testing")]
    1261          968 :         for (name, meta) in &with_metadata {
    1262          481 :             let gen = meta.generation;
    1263          481 :             match upload_queue.dangling_files.remove(name) {
    1264          429 :                 Some(same) if same == gen => { /* expected */ }
    1265            0 :                 Some(other) => {
    1266            0 :                     tracing::error!("{name} was unlinked with {other:?} but deleted with {gen:?}");
    1267              :                 }
    1268              :                 None => {
    1269           52 :                     tracing::error!("{name} was unlinked but was not dangling");
    1270              :                 }
    1271              :             }
    1272              :         }
    1273              : 
    1274              :         // schedule the actual deletions
    1275          487 :         if with_metadata.is_empty() {
    1276              :             // avoid scheduling the op & bumping the metric
    1277            6 :             return;
    1278          481 :         }
    1279          481 :         let op = UploadOp::Delete(Delete {
    1280          481 :             layers: with_metadata,
    1281          481 :         });
    1282          481 :         self.metric_begin(&op);
    1283          481 :         upload_queue.queued_operations.push_back(op);
    1284          487 :     }
    1285              : 
    1286              :     /// Schedules a compaction update to the remote `index_part.json`.
    1287              :     ///
    1288              :     /// `compacted_from` represent the L0 names which have been `compacted_to` L1 layers.
    1289           64 :     pub(crate) fn schedule_compaction_update(
    1290           64 :         self: &Arc<Self>,
    1291           64 :         compacted_from: &[Layer],
    1292           64 :         compacted_to: &[ResidentLayer],
    1293           64 :     ) -> Result<(), NotInitialized> {
    1294           64 :         let mut guard = self.upload_queue.lock().unwrap();
    1295           64 :         let upload_queue = guard.initialized_mut()?;
    1296              : 
    1297          412 :         for layer in compacted_to {
    1298          348 :             self.schedule_layer_file_upload0(upload_queue, layer.clone());
    1299          348 :         }
    1300              : 
    1301          476 :         let names = compacted_from.iter().map(|x| x.layer_desc().layer_name());
    1302           64 : 
    1303           64 :         self.schedule_unlinking_of_layers_from_index_part0(upload_queue, names)?;
    1304           64 :         self.launch_queued_tasks(upload_queue);
    1305           64 : 
    1306           64 :         Ok(())
    1307           64 :     }
    1308              : 
    1309              :     /// Wait for all previously scheduled uploads/deletions to complete
    1310         1330 :     pub(crate) async fn wait_completion(self: &Arc<Self>) -> Result<(), WaitCompletionError> {
    1311         1330 :         let receiver = {
    1312         1330 :             let mut guard = self.upload_queue.lock().unwrap();
    1313         1330 :             let upload_queue = guard
    1314         1330 :                 .initialized_mut()
    1315         1330 :                 .map_err(WaitCompletionError::NotInitialized)?;
    1316         1330 :             self.schedule_barrier0(upload_queue)
    1317         1330 :         };
    1318         1330 : 
    1319         1330 :         Self::wait_completion0(receiver).await
    1320         1330 :     }
    1321              : 
    1322         1330 :     async fn wait_completion0(
    1323         1330 :         mut receiver: tokio::sync::watch::Receiver<()>,
    1324         1330 :     ) -> Result<(), WaitCompletionError> {
    1325         1330 :         if receiver.changed().await.is_err() {
    1326            0 :             return Err(WaitCompletionError::UploadQueueShutDownOrStopped);
    1327         1330 :         }
    1328         1330 : 
    1329         1330 :         Ok(())
    1330         1330 :     }
    1331              : 
    1332            6 :     pub(crate) fn schedule_barrier(self: &Arc<Self>) -> anyhow::Result<()> {
    1333            6 :         let mut guard = self.upload_queue.lock().unwrap();
    1334            6 :         let upload_queue = guard.initialized_mut()?;
    1335            6 :         self.schedule_barrier0(upload_queue);
    1336            6 :         Ok(())
    1337            6 :     }
    1338              : 
    1339         1336 :     fn schedule_barrier0(
    1340         1336 :         self: &Arc<Self>,
    1341         1336 :         upload_queue: &mut UploadQueueInitialized,
    1342         1336 :     ) -> tokio::sync::watch::Receiver<()> {
    1343         1336 :         let (sender, receiver) = tokio::sync::watch::channel(());
    1344         1336 :         let barrier_op = UploadOp::Barrier(sender);
    1345         1336 : 
    1346         1336 :         upload_queue.queued_operations.push_back(barrier_op);
    1347         1336 :         // Don't count this kind of operation!
    1348         1336 : 
    1349         1336 :         // Launch the task immediately, if possible
    1350         1336 :         self.launch_queued_tasks(upload_queue);
    1351         1336 : 
    1352         1336 :         receiver
    1353         1336 :     }
    1354              : 
    1355              :     /// Wait for all previously scheduled operations to complete, and then stop.
    1356              :     ///
    1357              :     /// Not cancellation safe
    1358            8 :     pub(crate) async fn shutdown(self: &Arc<Self>) {
    1359            8 :         // On cancellation the queue is left in ackward state of refusing new operations but
    1360            8 :         // proper stop is yet to be called. On cancel the original or some later task must call
    1361            8 :         // `stop` or `shutdown`.
    1362            8 :         let sg = scopeguard::guard((), |_| {
    1363            0 :             tracing::error!("RemoteTimelineClient::shutdown was cancelled; this should not happen, do not make this into an allowed_error")
    1364            8 :         });
    1365              : 
    1366            8 :         let fut = {
    1367            8 :             let mut guard = self.upload_queue.lock().unwrap();
    1368            8 :             let upload_queue = match &mut *guard {
    1369              :                 UploadQueue::Stopped(_) => {
    1370            0 :                     scopeguard::ScopeGuard::into_inner(sg);
    1371            0 :                     return;
    1372              :                 }
    1373              :                 UploadQueue::Uninitialized => {
    1374              :                     // transition into Stopped state
    1375            0 :                     self.stop_impl(&mut guard);
    1376            0 :                     scopeguard::ScopeGuard::into_inner(sg);
    1377            0 :                     return;
    1378              :                 }
    1379            8 :                 UploadQueue::Initialized(ref mut init) => init,
    1380            8 :             };
    1381            8 : 
    1382            8 :             // if the queue is already stuck due to a shutdown operation which was cancelled, then
    1383            8 :             // just don't add more of these as they would never complete.
    1384            8 :             //
    1385            8 :             // TODO: if launch_queued_tasks were to be refactored to accept a &mut UploadQueue
    1386            8 :             // in every place we would not have to jump through this hoop, and this method could be
    1387            8 :             // made cancellable.
    1388            8 :             if !upload_queue.shutting_down {
    1389            8 :                 upload_queue.shutting_down = true;
    1390            8 :                 upload_queue.queued_operations.push_back(UploadOp::Shutdown);
    1391            8 :                 // this operation is not counted similar to Barrier
    1392            8 : 
    1393            8 :                 self.launch_queued_tasks(upload_queue);
    1394            8 :             }
    1395              : 
    1396            8 :             upload_queue.shutdown_ready.clone().acquire_owned()
    1397              :         };
    1398              : 
    1399            8 :         let res = fut.await;
    1400              : 
    1401            8 :         scopeguard::ScopeGuard::into_inner(sg);
    1402            8 : 
    1403            8 :         match res {
    1404            0 :             Ok(_permit) => unreachable!("shutdown_ready should not have been added permits"),
    1405            8 :             Err(_closed) => {
    1406            8 :                 // expected
    1407            8 :             }
    1408            8 :         }
    1409            8 : 
    1410            8 :         self.stop();
    1411            8 :     }
    1412              : 
    1413              :     /// Set the deleted_at field in the remote index file.
    1414              :     ///
    1415              :     /// This fails if the upload queue has not been `stop()`ed.
    1416              :     ///
    1417              :     /// The caller is responsible for calling `stop()` AND for waiting
    1418              :     /// for any ongoing upload tasks to finish after `stop()` has succeeded.
    1419              :     /// Check method [`RemoteTimelineClient::stop`] for details.
    1420            0 :     #[instrument(skip_all)]
    1421              :     pub(crate) async fn persist_index_part_with_deleted_flag(
    1422              :         self: &Arc<Self>,
    1423              :     ) -> Result<(), PersistIndexPartWithDeletedFlagError> {
    1424              :         let index_part_with_deleted_at = {
    1425              :             let mut locked = self.upload_queue.lock().unwrap();
    1426              : 
    1427              :             // We must be in stopped state because otherwise
    1428              :             // we can have inprogress index part upload that can overwrite the file
    1429              :             // with missing is_deleted flag that we going to set below
    1430              :             let stopped = locked.stopped_mut()?;
    1431              : 
    1432              :             match stopped.deleted_at {
    1433              :                 SetDeletedFlagProgress::NotRunning => (), // proceed
    1434              :                 SetDeletedFlagProgress::InProgress(at) => {
    1435              :                     return Err(PersistIndexPartWithDeletedFlagError::AlreadyInProgress(at));
    1436              :                 }
    1437              :                 SetDeletedFlagProgress::Successful(at) => {
    1438              :                     return Err(PersistIndexPartWithDeletedFlagError::AlreadyDeleted(at));
    1439              :                 }
    1440              :             };
    1441              :             let deleted_at = Utc::now().naive_utc();
    1442              :             stopped.deleted_at = SetDeletedFlagProgress::InProgress(deleted_at);
    1443              : 
    1444              :             let mut index_part = stopped.upload_queue_for_deletion.dirty.clone();
    1445              :             index_part.deleted_at = Some(deleted_at);
    1446              :             index_part
    1447              :         };
    1448              : 
    1449            0 :         let undo_deleted_at = scopeguard::guard(Arc::clone(self), |self_clone| {
    1450            0 :             let mut locked = self_clone.upload_queue.lock().unwrap();
    1451            0 :             let stopped = locked
    1452            0 :                 .stopped_mut()
    1453            0 :                 .expect("there's no way out of Stopping, and we checked it's Stopping above");
    1454            0 :             stopped.deleted_at = SetDeletedFlagProgress::NotRunning;
    1455            0 :         });
    1456              : 
    1457              :         pausable_failpoint!("persist_deleted_index_part");
    1458              : 
    1459              :         backoff::retry(
    1460            0 :             || {
    1461            0 :                 upload::upload_index_part(
    1462            0 :                     &self.storage_impl,
    1463            0 :                     &self.tenant_shard_id,
    1464            0 :                     &self.timeline_id,
    1465            0 :                     self.generation,
    1466            0 :                     &index_part_with_deleted_at,
    1467            0 :                     &self.cancel,
    1468            0 :                 )
    1469            0 :             },
    1470            0 :             |_e| false,
    1471              :             1,
    1472              :             // have just a couple of attempts
    1473              :             // when executed as part of timeline deletion this happens in context of api call
    1474              :             // when executed as part of tenant deletion this happens in the background
    1475              :             2,
    1476              :             "persist_index_part_with_deleted_flag",
    1477              :             &self.cancel,
    1478              :         )
    1479              :         .await
    1480            0 :         .ok_or_else(|| anyhow::Error::new(TimeoutOrCancel::Cancel))
    1481            0 :         .and_then(|x| x)?;
    1482              : 
    1483              :         // all good, disarm the guard and mark as success
    1484              :         ScopeGuard::into_inner(undo_deleted_at);
    1485              :         {
    1486              :             let mut locked = self.upload_queue.lock().unwrap();
    1487              : 
    1488              :             let stopped = locked
    1489              :                 .stopped_mut()
    1490              :                 .expect("there's no way out of Stopping, and we checked it's Stopping above");
    1491              :             stopped.deleted_at = SetDeletedFlagProgress::Successful(
    1492              :                 index_part_with_deleted_at
    1493              :                     .deleted_at
    1494              :                     .expect("we set it above"),
    1495              :             );
    1496              :         }
    1497              : 
    1498              :         Ok(())
    1499              :     }
    1500              : 
    1501            0 :     pub(crate) fn is_deleting(&self) -> bool {
    1502            0 :         let mut locked = self.upload_queue.lock().unwrap();
    1503            0 :         locked.stopped_mut().is_ok()
    1504            0 :     }
    1505              : 
    1506            0 :     pub(crate) async fn preserve_initdb_archive(
    1507            0 :         self: &Arc<Self>,
    1508            0 :         tenant_id: &TenantId,
    1509            0 :         timeline_id: &TimelineId,
    1510            0 :         cancel: &CancellationToken,
    1511            0 :     ) -> anyhow::Result<()> {
    1512            0 :         backoff::retry(
    1513            0 :             || async {
    1514            0 :                 upload::preserve_initdb_archive(&self.storage_impl, tenant_id, timeline_id, cancel)
    1515            0 :                     .await
    1516            0 :             },
    1517            0 :             TimeoutOrCancel::caused_by_cancel,
    1518            0 :             FAILED_DOWNLOAD_WARN_THRESHOLD,
    1519            0 :             FAILED_REMOTE_OP_RETRIES,
    1520            0 :             "preserve_initdb_tar_zst",
    1521            0 :             &cancel.clone(),
    1522            0 :         )
    1523            0 :         .await
    1524            0 :         .ok_or_else(|| anyhow::Error::new(TimeoutOrCancel::Cancel))
    1525            0 :         .and_then(|x| x)
    1526            0 :         .context("backing up initdb archive")?;
    1527            0 :         Ok(())
    1528            0 :     }
    1529              : 
    1530              :     /// Uploads the given layer **without** adding it to be part of a future `index_part.json` upload.
    1531              :     ///
    1532              :     /// This is not normally needed.
    1533            0 :     pub(crate) async fn upload_layer_file(
    1534            0 :         self: &Arc<Self>,
    1535            0 :         uploaded: &ResidentLayer,
    1536            0 :         cancel: &CancellationToken,
    1537            0 :     ) -> anyhow::Result<()> {
    1538            0 :         let remote_path = remote_layer_path(
    1539            0 :             &self.tenant_shard_id.tenant_id,
    1540            0 :             &self.timeline_id,
    1541            0 :             uploaded.metadata().shard,
    1542            0 :             &uploaded.layer_desc().layer_name(),
    1543            0 :             uploaded.metadata().generation,
    1544            0 :         );
    1545            0 : 
    1546            0 :         backoff::retry(
    1547            0 :             || async {
    1548            0 :                 upload::upload_timeline_layer(
    1549            0 :                     &self.storage_impl,
    1550            0 :                     uploaded.local_path(),
    1551            0 :                     &remote_path,
    1552            0 :                     uploaded.metadata().file_size,
    1553            0 :                     cancel,
    1554            0 :                 )
    1555            0 :                 .await
    1556            0 :             },
    1557            0 :             TimeoutOrCancel::caused_by_cancel,
    1558            0 :             FAILED_UPLOAD_WARN_THRESHOLD,
    1559            0 :             FAILED_REMOTE_OP_RETRIES,
    1560            0 :             "upload a layer without adding it to latest files",
    1561            0 :             cancel,
    1562            0 :         )
    1563            0 :         .await
    1564            0 :         .ok_or_else(|| anyhow::Error::new(TimeoutOrCancel::Cancel))
    1565            0 :         .and_then(|x| x)
    1566            0 :         .context("upload a layer without adding it to latest files")
    1567            0 :     }
    1568              : 
    1569              :     /// Copies the `adopted` remote existing layer to the remote path of `adopted_as`. The layer is
    1570              :     /// not added to be part of a future `index_part.json` upload.
    1571            0 :     pub(crate) async fn copy_timeline_layer(
    1572            0 :         self: &Arc<Self>,
    1573            0 :         adopted: &Layer,
    1574            0 :         adopted_as: &Layer,
    1575            0 :         cancel: &CancellationToken,
    1576            0 :     ) -> anyhow::Result<()> {
    1577            0 :         let source_remote_path = remote_layer_path(
    1578            0 :             &self.tenant_shard_id.tenant_id,
    1579            0 :             &adopted
    1580            0 :                 .get_timeline_id()
    1581            0 :                 .expect("Source timeline should be alive"),
    1582            0 :             adopted.metadata().shard,
    1583            0 :             &adopted.layer_desc().layer_name(),
    1584            0 :             adopted.metadata().generation,
    1585            0 :         );
    1586            0 : 
    1587            0 :         let target_remote_path = remote_layer_path(
    1588            0 :             &self.tenant_shard_id.tenant_id,
    1589            0 :             &self.timeline_id,
    1590            0 :             adopted_as.metadata().shard,
    1591            0 :             &adopted_as.layer_desc().layer_name(),
    1592            0 :             adopted_as.metadata().generation,
    1593            0 :         );
    1594            0 : 
    1595            0 :         backoff::retry(
    1596            0 :             || async {
    1597            0 :                 upload::copy_timeline_layer(
    1598            0 :                     &self.storage_impl,
    1599            0 :                     &source_remote_path,
    1600            0 :                     &target_remote_path,
    1601            0 :                     cancel,
    1602            0 :                 )
    1603            0 :                 .await
    1604            0 :             },
    1605            0 :             TimeoutOrCancel::caused_by_cancel,
    1606            0 :             FAILED_UPLOAD_WARN_THRESHOLD,
    1607            0 :             FAILED_REMOTE_OP_RETRIES,
    1608            0 :             "copy timeline layer",
    1609            0 :             cancel,
    1610            0 :         )
    1611            0 :         .await
    1612            0 :         .ok_or_else(|| anyhow::Error::new(TimeoutOrCancel::Cancel))
    1613            0 :         .and_then(|x| x)
    1614            0 :         .context("remote copy timeline layer")
    1615            0 :     }
    1616              : 
    1617            0 :     async fn flush_deletion_queue(&self) -> Result<(), DeletionQueueError> {
    1618            0 :         match tokio::time::timeout(
    1619            0 :             DELETION_QUEUE_FLUSH_TIMEOUT,
    1620            0 :             self.deletion_queue_client.flush_immediate(),
    1621            0 :         )
    1622            0 :         .await
    1623              :         {
    1624            0 :             Ok(result) => result,
    1625            0 :             Err(_timeout) => {
    1626            0 :                 // Flushing remote deletions is not mandatory: we flush here to make the system easier to test, and
    1627            0 :                 // to ensure that _usually_ objects are really gone after a DELETE is acked.  However, in case of deletion
    1628            0 :                 // queue issues (https://github.com/neondatabase/neon/issues/6440), we don't want to wait indefinitely here.
    1629            0 :                 tracing::warn!(
    1630            0 :                     "Timed out waiting for deletion queue flush, acking deletion anyway"
    1631              :                 );
    1632            0 :                 Ok(())
    1633              :             }
    1634              :         }
    1635            0 :     }
    1636              : 
    1637              :     /// Prerequisites: UploadQueue should be in stopped state and deleted_at should be successfuly set.
    1638              :     /// The function deletes layer files one by one, then lists the prefix to see if we leaked something
    1639              :     /// deletes leaked files if any and proceeds with deletion of index file at the end.
    1640            0 :     pub(crate) async fn delete_all(self: &Arc<Self>) -> Result<(), DeleteTimelineError> {
    1641            0 :         debug_assert_current_span_has_tenant_and_timeline_id();
    1642              : 
    1643            0 :         let layers: Vec<RemotePath> = {
    1644            0 :             let mut locked = self.upload_queue.lock().unwrap();
    1645            0 :             let stopped = locked.stopped_mut().map_err(DeleteTimelineError::Other)?;
    1646              : 
    1647            0 :             if !matches!(stopped.deleted_at, SetDeletedFlagProgress::Successful(_)) {
    1648            0 :                 return Err(DeleteTimelineError::Other(anyhow::anyhow!(
    1649            0 :                     "deleted_at is not set"
    1650            0 :                 )));
    1651            0 :             }
    1652            0 : 
    1653            0 :             debug_assert!(stopped.upload_queue_for_deletion.no_pending_work());
    1654              : 
    1655            0 :             stopped
    1656            0 :                 .upload_queue_for_deletion
    1657            0 :                 .dirty
    1658            0 :                 .layer_metadata
    1659            0 :                 .drain()
    1660            0 :                 .filter(|(_file_name, meta)| {
    1661            0 :                     // Filter out layers that belonged to an ancestor shard.  Since we are deleting the whole timeline from
    1662            0 :                     // all shards anyway, we _could_ delete these, but
    1663            0 :                     // - it creates a potential race if other shards are still
    1664            0 :                     //   using the layers while this shard deletes them.
    1665            0 :                     // - it means that if we rolled back the shard split, the ancestor shards would be in a state where
    1666            0 :                     //   these timelines are present but corrupt (their index exists but some layers don't)
    1667            0 :                     //
    1668            0 :                     // These layers will eventually be cleaned up by the scrubber when it does physical GC.
    1669            0 :                     meta.shard.shard_number == self.tenant_shard_id.shard_number
    1670            0 :                         && meta.shard.shard_count == self.tenant_shard_id.shard_count
    1671            0 :                 })
    1672            0 :                 .map(|(file_name, meta)| {
    1673            0 :                     remote_layer_path(
    1674            0 :                         &self.tenant_shard_id.tenant_id,
    1675            0 :                         &self.timeline_id,
    1676            0 :                         meta.shard,
    1677            0 :                         &file_name,
    1678            0 :                         meta.generation,
    1679            0 :                     )
    1680            0 :                 })
    1681            0 :                 .collect()
    1682            0 :         };
    1683            0 : 
    1684            0 :         let layer_deletion_count = layers.len();
    1685            0 :         self.deletion_queue_client
    1686            0 :             .push_immediate(layers)
    1687            0 :             .await
    1688            0 :             .map_err(|_| DeleteTimelineError::Cancelled)?;
    1689              : 
    1690              :         // Delete the initdb.tar.zst, which is not always present, but deletion attempts of
    1691              :         // inexistant objects are not considered errors.
    1692            0 :         let initdb_path =
    1693            0 :             remote_initdb_archive_path(&self.tenant_shard_id.tenant_id, &self.timeline_id);
    1694            0 :         self.deletion_queue_client
    1695            0 :             .push_immediate(vec![initdb_path])
    1696            0 :             .await
    1697            0 :             .map_err(|_| DeleteTimelineError::Cancelled)?;
    1698              : 
    1699              :         // Do not delete index part yet, it is needed for possible retry. If we remove it first
    1700              :         // and retry will arrive to different pageserver there wont be any traces of it on remote storage
    1701            0 :         let timeline_storage_path = remote_timeline_path(&self.tenant_shard_id, &self.timeline_id);
    1702            0 : 
    1703            0 :         // Execute all pending deletions, so that when we proceed to do a listing below, we aren't
    1704            0 :         // taking the burden of listing all the layers that we already know we should delete.
    1705            0 :         self.flush_deletion_queue()
    1706            0 :             .await
    1707            0 :             .map_err(|_| DeleteTimelineError::Cancelled)?;
    1708              : 
    1709            0 :         let cancel = shutdown_token();
    1710              : 
    1711            0 :         let remaining = download_retry(
    1712            0 :             || async {
    1713            0 :                 self.storage_impl
    1714            0 :                     .list(
    1715            0 :                         Some(&timeline_storage_path),
    1716            0 :                         ListingMode::NoDelimiter,
    1717            0 :                         None,
    1718            0 :                         &cancel,
    1719            0 :                     )
    1720            0 :                     .await
    1721            0 :             },
    1722            0 :             "list remaining files",
    1723            0 :             &cancel,
    1724            0 :         )
    1725            0 :         .await
    1726            0 :         .context("list files remaining files")?
    1727              :         .keys;
    1728              : 
    1729              :         // We will delete the current index_part object last, since it acts as a deletion
    1730              :         // marker via its deleted_at attribute
    1731            0 :         let latest_index = remaining
    1732            0 :             .iter()
    1733            0 :             .filter(|o| {
    1734            0 :                 o.key
    1735            0 :                     .object_name()
    1736            0 :                     .map(|n| n.starts_with(IndexPart::FILE_NAME))
    1737            0 :                     .unwrap_or(false)
    1738            0 :             })
    1739            0 :             .filter_map(|o| parse_remote_index_path(o.key.clone()).map(|gen| (o.key.clone(), gen)))
    1740            0 :             .max_by_key(|i| i.1)
    1741            0 :             .map(|i| i.0.clone())
    1742            0 :             .unwrap_or(
    1743            0 :                 // No generation-suffixed indices, assume we are dealing with
    1744            0 :                 // a legacy index.
    1745            0 :                 remote_index_path(&self.tenant_shard_id, &self.timeline_id, Generation::none()),
    1746            0 :             );
    1747            0 : 
    1748            0 :         let remaining_layers: Vec<RemotePath> = remaining
    1749            0 :             .into_iter()
    1750            0 :             .filter_map(|o| {
    1751            0 :                 if o.key == latest_index || o.key.object_name() == Some(INITDB_PRESERVED_PATH) {
    1752            0 :                     None
    1753              :                 } else {
    1754            0 :                     Some(o.key)
    1755              :                 }
    1756            0 :             })
    1757            0 :             .inspect(|path| {
    1758            0 :                 if let Some(name) = path.object_name() {
    1759            0 :                     info!(%name, "deleting a file not referenced from index_part.json");
    1760              :                 } else {
    1761            0 :                     warn!(%path, "deleting a nameless or non-utf8 object not referenced from index_part.json");
    1762              :                 }
    1763            0 :             })
    1764            0 :             .collect();
    1765            0 : 
    1766            0 :         let not_referenced_count = remaining_layers.len();
    1767            0 :         if !remaining_layers.is_empty() {
    1768            0 :             self.deletion_queue_client
    1769            0 :                 .push_immediate(remaining_layers)
    1770            0 :                 .await
    1771            0 :                 .map_err(|_| DeleteTimelineError::Cancelled)?;
    1772            0 :         }
    1773              : 
    1774            0 :         fail::fail_point!("timeline-delete-before-index-delete", |_| {
    1775            0 :             Err(DeleteTimelineError::Other(anyhow::anyhow!(
    1776            0 :                 "failpoint: timeline-delete-before-index-delete"
    1777            0 :             )))?
    1778            0 :         });
    1779              : 
    1780            0 :         debug!("enqueuing index part deletion");
    1781            0 :         self.deletion_queue_client
    1782            0 :             .push_immediate([latest_index].to_vec())
    1783            0 :             .await
    1784            0 :             .map_err(|_| DeleteTimelineError::Cancelled)?;
    1785              : 
    1786              :         // Timeline deletion is rare and we have probably emitted a reasonably number of objects: wait
    1787              :         // for a flush to a persistent deletion list so that we may be sure deletion will occur.
    1788            0 :         self.flush_deletion_queue()
    1789            0 :             .await
    1790            0 :             .map_err(|_| DeleteTimelineError::Cancelled)?;
    1791              : 
    1792            0 :         fail::fail_point!("timeline-delete-after-index-delete", |_| {
    1793            0 :             Err(DeleteTimelineError::Other(anyhow::anyhow!(
    1794            0 :                 "failpoint: timeline-delete-after-index-delete"
    1795            0 :             )))?
    1796            0 :         });
    1797              : 
    1798            0 :         info!(prefix=%timeline_storage_path, referenced=layer_deletion_count, not_referenced=%not_referenced_count, "done deleting in timeline prefix, including index_part.json");
    1799              : 
    1800            0 :         Ok(())
    1801            0 :     }
    1802              : 
    1803              :     ///
    1804              :     /// Pick next tasks from the queue, and start as many of them as possible without violating
    1805              :     /// the ordering constraints.
    1806              :     ///
    1807              :     /// The caller needs to already hold the `upload_queue` lock.
    1808         7531 :     fn launch_queued_tasks(self: &Arc<Self>, upload_queue: &mut UploadQueueInitialized) {
    1809        12058 :         while let Some(next_op) = upload_queue.queued_operations.front() {
    1810              :             // Can we run this task now?
    1811         8768 :             let can_run_now = match next_op {
    1812              :                 UploadOp::UploadLayer(..) => {
    1813              :                     // Can always be scheduled.
    1814         1529 :                     true
    1815              :                 }
    1816              :                 UploadOp::UploadMetadata { .. } => {
    1817              :                     // These can only be performed after all the preceding operations
    1818              :                     // have finished.
    1819         4457 :                     upload_queue.inprogress_tasks.is_empty()
    1820              :                 }
    1821              :                 UploadOp::Delete(..) => {
    1822              :                     // Wait for preceding uploads to finish. Concurrent deletions are OK, though.
    1823          287 :                     upload_queue.num_inprogress_deletions == upload_queue.inprogress_tasks.len()
    1824              :                 }
    1825              : 
    1826              :                 UploadOp::Barrier(_) | UploadOp::Shutdown => {
    1827         2495 :                     upload_queue.inprogress_tasks.is_empty()
    1828              :                 }
    1829              :             };
    1830              : 
    1831              :             // If we cannot launch this task, don't look any further.
    1832              :             //
    1833              :             // In some cases, we could let some non-frontmost tasks to "jump the queue" and launch
    1834              :             // them now, but we don't try to do that currently.  For example, if the frontmost task
    1835              :             // is an index-file upload that cannot proceed until preceding uploads have finished, we
    1836              :             // could still start layer uploads that were scheduled later.
    1837         8768 :             if !can_run_now {
    1838         4233 :                 break;
    1839         4535 :             }
    1840         4535 : 
    1841         4535 :             if let UploadOp::Shutdown = next_op {
    1842              :                 // leave the op in the queue but do not start more tasks; it will be dropped when
    1843              :                 // the stop is called.
    1844            8 :                 upload_queue.shutdown_ready.close();
    1845            8 :                 break;
    1846         4527 :             }
    1847         4527 : 
    1848         4527 :             // We can launch this task. Remove it from the queue first.
    1849         4527 :             let mut next_op = upload_queue.queued_operations.pop_front().unwrap();
    1850         4527 : 
    1851         4527 :             debug!("starting op: {}", next_op);
    1852              : 
    1853              :             // Update the counters and prepare
    1854         4527 :             match &mut next_op {
    1855         1529 :                 UploadOp::UploadLayer(layer, meta, mode) => {
    1856         1529 :                     if upload_queue
    1857         1529 :                         .recently_deleted
    1858         1529 :                         .remove(&(layer.layer_desc().layer_name().clone(), meta.generation))
    1859            0 :                     {
    1860            0 :                         *mode = Some(OpType::FlushDeletion);
    1861            0 :                     } else {
    1862         1529 :                         *mode = Some(OpType::MayReorder)
    1863              :                     }
    1864         1529 :                     upload_queue.num_inprogress_layer_uploads += 1;
    1865              :                 }
    1866         1424 :                 UploadOp::UploadMetadata { .. } => {
    1867         1424 :                     upload_queue.num_inprogress_metadata_uploads += 1;
    1868         1424 :                 }
    1869          238 :                 UploadOp::Delete(Delete { layers }) => {
    1870          476 :                     for (name, meta) in layers {
    1871          238 :                         upload_queue
    1872          238 :                             .recently_deleted
    1873          238 :                             .insert((name.clone(), meta.generation));
    1874          238 :                     }
    1875          238 :                     upload_queue.num_inprogress_deletions += 1;
    1876              :                 }
    1877         1336 :                 UploadOp::Barrier(sender) => {
    1878         1336 :                     sender.send_replace(());
    1879         1336 :                     continue;
    1880              :                 }
    1881            0 :                 UploadOp::Shutdown => unreachable!("shutdown is intentionally never popped off"),
    1882              :             };
    1883              : 
    1884              :             // Assign unique ID to this task
    1885         3191 :             upload_queue.task_counter += 1;
    1886         3191 :             let upload_task_id = upload_queue.task_counter;
    1887         3191 : 
    1888         3191 :             // Add it to the in-progress map
    1889         3191 :             let task = Arc::new(UploadTask {
    1890         3191 :                 task_id: upload_task_id,
    1891         3191 :                 op: next_op,
    1892         3191 :                 retries: AtomicU32::new(0),
    1893         3191 :             });
    1894         3191 :             upload_queue
    1895         3191 :                 .inprogress_tasks
    1896         3191 :                 .insert(task.task_id, Arc::clone(&task));
    1897         3191 : 
    1898         3191 :             // Spawn task to perform the task
    1899         3191 :             let self_rc = Arc::clone(self);
    1900         3191 :             let tenant_shard_id = self.tenant_shard_id;
    1901         3191 :             let timeline_id = self.timeline_id;
    1902         3191 :             task_mgr::spawn(
    1903         3191 :                 &self.runtime,
    1904         3191 :                 TaskKind::RemoteUploadTask,
    1905         3191 :                 self.tenant_shard_id,
    1906         3191 :                 Some(self.timeline_id),
    1907         3191 :                 "remote upload",
    1908         3088 :                 async move {
    1909        44301 :                     self_rc.perform_upload_task(task).await;
    1910         2990 :                     Ok(())
    1911         2990 :                 }
    1912         3191 :                 .instrument(info_span!(parent: None, "remote_upload", tenant_id=%tenant_shard_id.tenant_id, shard_id=%tenant_shard_id.shard_slug(), %timeline_id, %upload_task_id)),
    1913              :             );
    1914              : 
    1915              :             // Loop back to process next task
    1916              :         }
    1917         7531 :     }
    1918              : 
    1919              :     ///
    1920              :     /// Perform an upload task.
    1921              :     ///
    1922              :     /// The task is in the `inprogress_tasks` list. This function will try to
    1923              :     /// execute it, retrying forever. On successful completion, the task is
    1924              :     /// removed it from the `inprogress_tasks` list, and any next task(s) in the
    1925              :     /// queue that were waiting by the completion are launched.
    1926              :     ///
    1927              :     /// The task can be shut down, however. That leads to stopping the whole
    1928              :     /// queue.
    1929              :     ///
    1930         3088 :     async fn perform_upload_task(self: &Arc<Self>, task: Arc<UploadTask>) {
    1931         3088 :         let cancel = shutdown_token();
    1932              :         // Loop to retry until it completes.
    1933              :         loop {
    1934              :             // If we're requested to shut down, close up shop and exit.
    1935              :             //
    1936              :             // Note: We only check for the shutdown requests between retries, so
    1937              :             // if a shutdown request arrives while we're busy uploading, in the
    1938              :             // upload::upload:*() call below, we will wait not exit until it has
    1939              :             // finished. We probably could cancel the upload by simply dropping
    1940              :             // the Future, but we're not 100% sure if the remote storage library
    1941              :             // is cancellation safe, so we don't dare to do that. Hopefully, the
    1942              :             // upload finishes or times out soon enough.
    1943         3088 :             if cancel.is_cancelled() {
    1944            0 :                 info!("upload task cancelled by shutdown request");
    1945            0 :                 self.stop();
    1946            0 :                 return;
    1947         3088 :             }
    1948              : 
    1949         3088 :             let upload_result: anyhow::Result<()> = match &task.op {
    1950         1436 :                 UploadOp::UploadLayer(ref layer, ref layer_metadata, mode) => {
    1951         1436 :                     if let Some(OpType::FlushDeletion) = mode {
    1952            0 :                         if self.config.read().unwrap().block_deletions {
    1953              :                             // Of course, this is not efficient... but usually the queue should be empty.
    1954            0 :                             let mut queue_locked = self.upload_queue.lock().unwrap();
    1955            0 :                             let mut detected = false;
    1956            0 :                             if let Ok(queue) = queue_locked.initialized_mut() {
    1957            0 :                                 for list in queue.blocked_deletions.iter_mut() {
    1958            0 :                                     list.layers.retain(|(name, meta)| {
    1959            0 :                                         if name == &layer.layer_desc().layer_name()
    1960            0 :                                             && meta.generation == layer_metadata.generation
    1961              :                                         {
    1962            0 :                                             detected = true;
    1963            0 :                                             // remove the layer from deletion queue
    1964            0 :                                             false
    1965              :                                         } else {
    1966              :                                             // keep the layer
    1967            0 :                                             true
    1968              :                                         }
    1969            0 :                                     });
    1970            0 :                                 }
    1971            0 :                             }
    1972            0 :                             if detected {
    1973            0 :                                 info!(
    1974            0 :                                     "cancelled blocked deletion of layer {} at gen {:?}",
    1975            0 :                                     layer.layer_desc().layer_name(),
    1976              :                                     layer_metadata.generation
    1977              :                                 );
    1978            0 :                             }
    1979              :                         } else {
    1980              :                             // TODO: we did not guarantee that upload task starts after deletion task, so there could be possibly race conditions
    1981              :                             // that we still get the layer deleted. But this only happens if someone creates a layer immediately after it's deleted,
    1982              :                             // which is not possible in the current system.
    1983            0 :                             info!(
    1984            0 :                                 "waiting for deletion queue flush to complete before uploading layer {} at gen {:?}",
    1985            0 :                                 layer.layer_desc().layer_name(),
    1986              :                                 layer_metadata.generation
    1987              :                             );
    1988              :                             {
    1989              :                                 // We are going to flush, we can clean up the recently deleted list.
    1990            0 :                                 let mut queue_locked = self.upload_queue.lock().unwrap();
    1991            0 :                                 if let Ok(queue) = queue_locked.initialized_mut() {
    1992            0 :                                     queue.recently_deleted.clear();
    1993            0 :                                 }
    1994              :                             }
    1995            0 :                             if let Err(e) = self.deletion_queue_client.flush_execute().await {
    1996            0 :                                 warn!(
    1997            0 :                                     "failed to flush the deletion queue before uploading layer {} at gen {:?}, still proceeding to upload: {e:#} ",
    1998            0 :                                     layer.layer_desc().layer_name(),
    1999              :                                     layer_metadata.generation
    2000              :                                 );
    2001              :                             } else {
    2002            0 :                                 info!(
    2003            0 :                                     "done flushing deletion queue before uploading layer {} at gen {:?}",
    2004            0 :                                     layer.layer_desc().layer_name(),
    2005              :                                     layer_metadata.generation
    2006              :                                 );
    2007              :                             }
    2008              :                         }
    2009         1436 :                     }
    2010         1436 :                     let local_path = layer.local_path();
    2011         1436 : 
    2012         1436 :                     // We should only be uploading layers created by this `Tenant`'s lifetime, so
    2013         1436 :                     // the metadata in the upload should always match our current generation.
    2014         1436 :                     assert_eq!(layer_metadata.generation, self.generation);
    2015              : 
    2016         1436 :                     let remote_path = remote_layer_path(
    2017         1436 :                         &self.tenant_shard_id.tenant_id,
    2018         1436 :                         &self.timeline_id,
    2019         1436 :                         layer_metadata.shard,
    2020         1436 :                         &layer.layer_desc().layer_name(),
    2021         1436 :                         layer_metadata.generation,
    2022         1436 :                     );
    2023         1436 : 
    2024         1436 :                     upload::upload_timeline_layer(
    2025         1436 :                         &self.storage_impl,
    2026         1436 :                         local_path,
    2027         1436 :                         &remote_path,
    2028         1436 :                         layer_metadata.file_size,
    2029         1436 :                         &self.cancel,
    2030         1436 :                     )
    2031         1436 :                     .measure_remote_op(
    2032         1436 :                         RemoteOpFileKind::Layer,
    2033         1436 :                         RemoteOpKind::Upload,
    2034         1436 :                         Arc::clone(&self.metrics),
    2035         1436 :                     )
    2036        38938 :                     .await
    2037              :                 }
    2038         1414 :                 UploadOp::UploadMetadata { ref uploaded } => {
    2039         1414 :                     let res = upload::upload_index_part(
    2040         1414 :                         &self.storage_impl,
    2041         1414 :                         &self.tenant_shard_id,
    2042         1414 :                         &self.timeline_id,
    2043         1414 :                         self.generation,
    2044         1414 :                         uploaded,
    2045         1414 :                         &self.cancel,
    2046         1414 :                     )
    2047         1414 :                     .measure_remote_op(
    2048         1414 :                         RemoteOpFileKind::Index,
    2049         1414 :                         RemoteOpKind::Upload,
    2050         1414 :                         Arc::clone(&self.metrics),
    2051         1414 :                     )
    2052         5175 :                     .await;
    2053         1410 :                     if res.is_ok() {
    2054         1410 :                         self.update_remote_physical_size_gauge(Some(uploaded));
    2055         1410 :                         let mention_having_future_layers = if cfg!(feature = "testing") {
    2056         1410 :                             uploaded
    2057         1410 :                                 .layer_metadata
    2058         1410 :                                 .keys()
    2059        17481 :                                 .any(|x| x.is_in_future(uploaded.metadata.disk_consistent_lsn()))
    2060              :                         } else {
    2061            0 :                             false
    2062              :                         };
    2063         1410 :                         if mention_having_future_layers {
    2064              :                             // find rationale near crate::tenant::timeline::init::cleanup_future_layer
    2065           18 :                             tracing::info!(
    2066            0 :                                 disk_consistent_lsn = %uploaded.metadata.disk_consistent_lsn(),
    2067            0 :                                 "uploaded an index_part.json with future layers -- this is ok! if shutdown now, expect future layer cleanup"
    2068              :                             );
    2069         1392 :                         }
    2070            0 :                     }
    2071         1410 :                     res
    2072              :                 }
    2073          238 :                 UploadOp::Delete(delete) => {
    2074          238 :                     if self.config.read().unwrap().block_deletions {
    2075            0 :                         let mut queue_locked = self.upload_queue.lock().unwrap();
    2076            0 :                         if let Ok(queue) = queue_locked.initialized_mut() {
    2077            0 :                             queue.blocked_deletions.push(delete.clone());
    2078            0 :                         }
    2079            0 :                         Ok(())
    2080              :                     } else {
    2081          238 :                         pausable_failpoint!("before-delete-layer-pausable");
    2082          238 :                         self.deletion_queue_client
    2083          238 :                             .push_layers(
    2084          238 :                                 self.tenant_shard_id,
    2085          238 :                                 self.timeline_id,
    2086          238 :                                 self.generation,
    2087          238 :                                 delete.layers.clone(),
    2088          238 :                             )
    2089            0 :                             .await
    2090          238 :                             .map_err(|e| anyhow::anyhow!(e))
    2091              :                     }
    2092              :                 }
    2093            0 :                 unexpected @ UploadOp::Barrier(_) | unexpected @ UploadOp::Shutdown => {
    2094              :                     // unreachable. Barrier operations are handled synchronously in
    2095              :                     // launch_queued_tasks
    2096            0 :                     warn!("unexpected {unexpected:?} operation in perform_upload_task");
    2097            0 :                     break;
    2098              :                 }
    2099              :             };
    2100              : 
    2101            0 :             match upload_result {
    2102              :                 Ok(()) => {
    2103         2990 :                     break;
    2104              :                 }
    2105            0 :                 Err(e) if TimeoutOrCancel::caused_by_cancel(&e) => {
    2106            0 :                     // loop around to do the proper stopping
    2107            0 :                     continue;
    2108              :                 }
    2109            0 :                 Err(e) => {
    2110            0 :                     let retries = task.retries.fetch_add(1, Ordering::SeqCst);
    2111            0 : 
    2112            0 :                     // Uploads can fail due to rate limits (IAM, S3), spurious network problems,
    2113            0 :                     // or other external reasons. Such issues are relatively regular, so log them
    2114            0 :                     // at info level at first, and only WARN if the operation fails repeatedly.
    2115            0 :                     //
    2116            0 :                     // (See similar logic for downloads in `download::download_retry`)
    2117            0 :                     if retries < FAILED_UPLOAD_WARN_THRESHOLD {
    2118            0 :                         info!(
    2119            0 :                             "failed to perform remote task {}, will retry (attempt {}): {:#}",
    2120            0 :                             task.op, retries, e
    2121              :                         );
    2122              :                     } else {
    2123            0 :                         warn!(
    2124            0 :                             "failed to perform remote task {}, will retry (attempt {}): {:?}",
    2125            0 :                             task.op, retries, e
    2126              :                         );
    2127              :                     }
    2128              : 
    2129              :                     // sleep until it's time to retry, or we're cancelled
    2130            0 :                     exponential_backoff(
    2131            0 :                         retries,
    2132            0 :                         DEFAULT_BASE_BACKOFF_SECONDS,
    2133            0 :                         DEFAULT_MAX_BACKOFF_SECONDS,
    2134            0 :                         &cancel,
    2135            0 :                     )
    2136            0 :                     .await;
    2137              :                 }
    2138              :             }
    2139              :         }
    2140              : 
    2141         2990 :         let retries = task.retries.load(Ordering::SeqCst);
    2142         2990 :         if retries > 0 {
    2143            0 :             info!(
    2144            0 :                 "remote task {} completed successfully after {} retries",
    2145            0 :                 task.op, retries
    2146              :             );
    2147              :         } else {
    2148         2990 :             debug!("remote task {} completed successfully", task.op);
    2149              :         }
    2150              : 
    2151              :         // The task has completed successfully. Remove it from the in-progress list.
    2152         2990 :         let lsn_update = {
    2153         2990 :             let mut upload_queue_guard = self.upload_queue.lock().unwrap();
    2154         2990 :             let upload_queue = match upload_queue_guard.deref_mut() {
    2155            0 :                 UploadQueue::Uninitialized => panic!("callers are responsible for ensuring this is only called on an initialized queue"),
    2156            0 :                 UploadQueue::Stopped(_stopped) => {
    2157            0 :                     None
    2158              :                 },
    2159         2990 :                 UploadQueue::Initialized(qi) => { Some(qi) }
    2160              :             };
    2161              : 
    2162         2990 :             let upload_queue = match upload_queue {
    2163         2990 :                 Some(upload_queue) => upload_queue,
    2164              :                 None => {
    2165            0 :                     info!("another concurrent task already stopped the queue");
    2166            0 :                     return;
    2167              :                 }
    2168              :             };
    2169              : 
    2170         2990 :             upload_queue.inprogress_tasks.remove(&task.task_id);
    2171              : 
    2172         2990 :             let lsn_update = match task.op {
    2173              :                 UploadOp::UploadLayer(_, _, _) => {
    2174         1342 :                     upload_queue.num_inprogress_layer_uploads -= 1;
    2175         1342 :                     None
    2176              :                 }
    2177         1410 :                 UploadOp::UploadMetadata { ref uploaded } => {
    2178         1410 :                     upload_queue.num_inprogress_metadata_uploads -= 1;
    2179         1410 : 
    2180         1410 :                     // the task id is reused as a monotonicity check for storing the "clean"
    2181         1410 :                     // IndexPart.
    2182         1410 :                     let last_updater = upload_queue.clean.1;
    2183         1410 :                     let is_later = last_updater.is_some_and(|task_id| task_id < task.task_id);
    2184         1410 :                     let monotone = is_later || last_updater.is_none();
    2185              : 
    2186         1410 :                     assert!(monotone, "no two index uploads should be completing at the same time, prev={last_updater:?}, task.task_id={}", task.task_id);
    2187              : 
    2188              :                     // not taking ownership is wasteful
    2189         1410 :                     upload_queue.clean.0.clone_from(uploaded);
    2190         1410 :                     upload_queue.clean.1 = Some(task.task_id);
    2191         1410 : 
    2192         1410 :                     let lsn = upload_queue.clean.0.metadata.disk_consistent_lsn();
    2193         1410 : 
    2194         1410 :                     if self.generation.is_none() {
    2195              :                         // Legacy mode: skip validating generation
    2196            0 :                         upload_queue.visible_remote_consistent_lsn.store(lsn);
    2197            0 :                         None
    2198         1410 :                     } else if self
    2199         1410 :                         .config
    2200         1410 :                         .read()
    2201         1410 :                         .unwrap()
    2202         1410 :                         .process_remote_consistent_lsn_updates
    2203              :                     {
    2204         1410 :                         Some((lsn, upload_queue.visible_remote_consistent_lsn.clone()))
    2205              :                     } else {
    2206              :                         // Our config disables remote_consistent_lsn updates: drop it.
    2207            0 :                         None
    2208              :                     }
    2209              :                 }
    2210              :                 UploadOp::Delete(_) => {
    2211          238 :                     upload_queue.num_inprogress_deletions -= 1;
    2212          238 :                     None
    2213              :                 }
    2214            0 :                 UploadOp::Barrier(..) | UploadOp::Shutdown => unreachable!(),
    2215              :             };
    2216              : 
    2217              :             // Launch any queued tasks that were unblocked by this one.
    2218         2990 :             self.launch_queued_tasks(upload_queue);
    2219         2990 :             lsn_update
    2220              :         };
    2221              : 
    2222         2990 :         if let Some((lsn, slot)) = lsn_update {
    2223              :             // Updates to the remote_consistent_lsn we advertise to pageservers
    2224              :             // are all routed through the DeletionQueue, to enforce important
    2225              :             // data safety guarantees (see docs/rfcs/025-generation-numbers.md)
    2226         1410 :             self.deletion_queue_client
    2227         1410 :                 .update_remote_consistent_lsn(
    2228         1410 :                     self.tenant_shard_id,
    2229         1410 :                     self.timeline_id,
    2230         1410 :                     self.generation,
    2231         1410 :                     lsn,
    2232         1410 :                     slot,
    2233         1410 :                 )
    2234            0 :                 .await;
    2235         1580 :         }
    2236              : 
    2237         2990 :         self.metric_end(&task.op);
    2238         2990 :     }
    2239              : 
    2240         6469 :     fn metric_impl(
    2241         6469 :         &self,
    2242         6469 :         op: &UploadOp,
    2243         6469 :     ) -> Option<(
    2244         6469 :         RemoteOpFileKind,
    2245         6469 :         RemoteOpKind,
    2246         6469 :         RemoteTimelineClientMetricsCallTrackSize,
    2247         6469 :     )> {
    2248              :         use RemoteTimelineClientMetricsCallTrackSize::DontTrackSize;
    2249         6469 :         let res = match op {
    2250         2880 :             UploadOp::UploadLayer(_, m, _) => (
    2251         2880 :                 RemoteOpFileKind::Layer,
    2252         2880 :                 RemoteOpKind::Upload,
    2253         2880 :                 RemoteTimelineClientMetricsCallTrackSize::Bytes(m.file_size),
    2254         2880 :             ),
    2255         2862 :             UploadOp::UploadMetadata { .. } => (
    2256         2862 :                 RemoteOpFileKind::Index,
    2257         2862 :                 RemoteOpKind::Upload,
    2258         2862 :                 DontTrackSize {
    2259         2862 :                     reason: "metadata uploads are tiny",
    2260         2862 :                 },
    2261         2862 :             ),
    2262          719 :             UploadOp::Delete(_delete) => (
    2263          719 :                 RemoteOpFileKind::Layer,
    2264          719 :                 RemoteOpKind::Delete,
    2265          719 :                 DontTrackSize {
    2266          719 :                     reason: "should we track deletes? positive or negative sign?",
    2267          719 :                 },
    2268          719 :             ),
    2269              :             UploadOp::Barrier(..) | UploadOp::Shutdown => {
    2270              :                 // we do not account these
    2271            8 :                 return None;
    2272              :             }
    2273              :         };
    2274         6461 :         Some(res)
    2275         6469 :     }
    2276              : 
    2277         3471 :     fn metric_begin(&self, op: &UploadOp) {
    2278         3471 :         let (file_kind, op_kind, track_bytes) = match self.metric_impl(op) {
    2279         3471 :             Some(x) => x,
    2280            0 :             None => return,
    2281              :         };
    2282         3471 :         let guard = self.metrics.call_begin(&file_kind, &op_kind, track_bytes);
    2283         3471 :         guard.will_decrement_manually(); // in metric_end(), see right below
    2284         3471 :     }
    2285              : 
    2286         2998 :     fn metric_end(&self, op: &UploadOp) {
    2287         2998 :         let (file_kind, op_kind, track_bytes) = match self.metric_impl(op) {
    2288         2990 :             Some(x) => x,
    2289            8 :             None => return,
    2290              :         };
    2291         2990 :         self.metrics.call_end(&file_kind, &op_kind, track_bytes);
    2292         2998 :     }
    2293              : 
    2294              :     /// Close the upload queue for new operations and cancel queued operations.
    2295              :     ///
    2296              :     /// Use [`RemoteTimelineClient::shutdown`] for graceful stop.
    2297              :     ///
    2298              :     /// In-progress operations will still be running after this function returns.
    2299              :     /// Use `task_mgr::shutdown_tasks(Some(TaskKind::RemoteUploadTask), Some(self.tenant_shard_id), Some(timeline_id))`
    2300              :     /// to wait for them to complete, after calling this function.
    2301           18 :     pub(crate) fn stop(&self) {
    2302           18 :         // Whichever *task* for this RemoteTimelineClient grabs the mutex first will transition the queue
    2303           18 :         // into stopped state, thereby dropping all off the queued *ops* which haven't become *tasks* yet.
    2304           18 :         // The other *tasks* will come here and observe an already shut down queue and hence simply wrap up their business.
    2305           18 :         let mut guard = self.upload_queue.lock().unwrap();
    2306           18 :         self.stop_impl(&mut guard);
    2307           18 :     }
    2308              : 
    2309           18 :     fn stop_impl(&self, guard: &mut std::sync::MutexGuard<UploadQueue>) {
    2310           18 :         match &mut **guard {
    2311              :             UploadQueue::Uninitialized => {
    2312            0 :                 info!("UploadQueue is in state Uninitialized, nothing to do");
    2313            0 :                 **guard = UploadQueue::Stopped(UploadQueueStopped::Uninitialized);
    2314              :             }
    2315              :             UploadQueue::Stopped(_) => {
    2316              :                 // nothing to do
    2317            8 :                 info!("another concurrent task already shut down the queue");
    2318              :             }
    2319           10 :             UploadQueue::Initialized(initialized) => {
    2320           10 :                 info!("shutting down upload queue");
    2321              : 
    2322              :                 // Replace the queue with the Stopped state, taking ownership of the old
    2323              :                 // Initialized queue. We will do some checks on it, and then drop it.
    2324           10 :                 let qi = {
    2325              :                     // Here we preserve working version of the upload queue for possible use during deletions.
    2326              :                     // In-place replace of Initialized to Stopped can be done with the help of https://github.com/Sgeo/take_mut
    2327              :                     // but for this use case it doesnt really makes sense to bring unsafe code only for this usage point.
    2328              :                     // Deletion is not really perf sensitive so there shouldnt be any problems with cloning a fraction of it.
    2329           10 :                     let upload_queue_for_deletion = UploadQueueInitialized {
    2330           10 :                         task_counter: 0,
    2331           10 :                         dirty: initialized.dirty.clone(),
    2332           10 :                         clean: initialized.clean.clone(),
    2333           10 :                         latest_files_changes_since_metadata_upload_scheduled: 0,
    2334           10 :                         visible_remote_consistent_lsn: initialized
    2335           10 :                             .visible_remote_consistent_lsn
    2336           10 :                             .clone(),
    2337           10 :                         num_inprogress_layer_uploads: 0,
    2338           10 :                         num_inprogress_metadata_uploads: 0,
    2339           10 :                         num_inprogress_deletions: 0,
    2340           10 :                         inprogress_tasks: HashMap::default(),
    2341           10 :                         queued_operations: VecDeque::default(),
    2342           10 :                         #[cfg(feature = "testing")]
    2343           10 :                         dangling_files: HashMap::default(),
    2344           10 :                         blocked_deletions: Vec::new(),
    2345           10 :                         shutting_down: false,
    2346           10 :                         shutdown_ready: Arc::new(tokio::sync::Semaphore::new(0)),
    2347           10 :                         recently_deleted: HashSet::new(),
    2348           10 :                     };
    2349           10 : 
    2350           10 :                     let upload_queue = std::mem::replace(
    2351           10 :                         &mut **guard,
    2352           10 :                         UploadQueue::Stopped(UploadQueueStopped::Deletable(
    2353           10 :                             UploadQueueStoppedDeletable {
    2354           10 :                                 upload_queue_for_deletion,
    2355           10 :                                 deleted_at: SetDeletedFlagProgress::NotRunning,
    2356           10 :                             },
    2357           10 :                         )),
    2358           10 :                     );
    2359           10 :                     if let UploadQueue::Initialized(qi) = upload_queue {
    2360           10 :                         qi
    2361              :                     } else {
    2362            0 :                         unreachable!("we checked in the match above that it is Initialized");
    2363              :                     }
    2364              :                 };
    2365              : 
    2366              :                 // consistency check
    2367           10 :                 assert_eq!(
    2368           10 :                     qi.num_inprogress_layer_uploads
    2369           10 :                         + qi.num_inprogress_metadata_uploads
    2370           10 :                         + qi.num_inprogress_deletions,
    2371           10 :                     qi.inprogress_tasks.len()
    2372           10 :                 );
    2373              : 
    2374              :                 // We don't need to do anything here for in-progress tasks. They will finish
    2375              :                 // on their own, decrement the unfinished-task counter themselves, and observe
    2376              :                 // that the queue is Stopped.
    2377           10 :                 drop(qi.inprogress_tasks);
    2378              : 
    2379              :                 // Tear down queued ops
    2380           10 :                 for op in qi.queued_operations.into_iter() {
    2381            8 :                     self.metric_end(&op);
    2382            8 :                     // Dropping UploadOp::Barrier() here will make wait_completion() return with an Err()
    2383            8 :                     // which is exactly what we want to happen.
    2384            8 :                     drop(op);
    2385            8 :                 }
    2386              :             }
    2387              :         }
    2388           18 :     }
    2389              : 
    2390              :     /// Returns an accessor which will hold the UploadQueue mutex for accessing the upload queue
    2391              :     /// externally to RemoteTimelineClient.
    2392            0 :     pub(crate) fn initialized_upload_queue(
    2393            0 :         &self,
    2394            0 :     ) -> Result<UploadQueueAccessor<'_>, NotInitialized> {
    2395            0 :         let mut inner = self.upload_queue.lock().unwrap();
    2396            0 :         inner.initialized_mut()?;
    2397            0 :         Ok(UploadQueueAccessor { inner })
    2398            0 :     }
    2399              : 
    2400            8 :     pub(crate) fn no_pending_work(&self) -> bool {
    2401            8 :         let inner = self.upload_queue.lock().unwrap();
    2402            8 :         match &*inner {
    2403              :             UploadQueue::Uninitialized
    2404            0 :             | UploadQueue::Stopped(UploadQueueStopped::Uninitialized) => true,
    2405            8 :             UploadQueue::Stopped(UploadQueueStopped::Deletable(x)) => {
    2406            8 :                 x.upload_queue_for_deletion.no_pending_work()
    2407              :             }
    2408            0 :             UploadQueue::Initialized(x) => x.no_pending_work(),
    2409              :         }
    2410            8 :     }
    2411              : 
    2412              :     /// 'foreign' in the sense that it does not belong to this tenant shard.  This method
    2413              :     /// is used during GC for other shards to get the index of shard zero.
    2414            0 :     pub(crate) async fn download_foreign_index(
    2415            0 :         &self,
    2416            0 :         shard_number: ShardNumber,
    2417            0 :         cancel: &CancellationToken,
    2418            0 :     ) -> Result<(IndexPart, Generation, std::time::SystemTime), DownloadError> {
    2419            0 :         let foreign_shard_id = TenantShardId {
    2420            0 :             shard_number,
    2421            0 :             shard_count: self.tenant_shard_id.shard_count,
    2422            0 :             tenant_id: self.tenant_shard_id.tenant_id,
    2423            0 :         };
    2424            0 :         download_index_part(
    2425            0 :             &self.storage_impl,
    2426            0 :             &foreign_shard_id,
    2427            0 :             &self.timeline_id,
    2428            0 :             Generation::MAX,
    2429            0 :             cancel,
    2430            0 :         )
    2431            0 :         .await
    2432            0 :     }
    2433              : }
    2434              : 
    2435              : pub(crate) struct UploadQueueAccessor<'a> {
    2436              :     inner: std::sync::MutexGuard<'a, UploadQueue>,
    2437              : }
    2438              : 
    2439              : impl UploadQueueAccessor<'_> {
    2440            0 :     pub(crate) fn latest_uploaded_index_part(&self) -> &IndexPart {
    2441            0 :         match &*self.inner {
    2442            0 :             UploadQueue::Initialized(x) => &x.clean.0,
    2443              :             UploadQueue::Uninitialized | UploadQueue::Stopped(_) => {
    2444            0 :                 unreachable!("checked before constructing")
    2445              :             }
    2446              :         }
    2447            0 :     }
    2448              : }
    2449              : 
    2450            0 : pub fn remote_tenant_path(tenant_shard_id: &TenantShardId) -> RemotePath {
    2451            0 :     let path = format!("tenants/{tenant_shard_id}");
    2452            0 :     RemotePath::from_string(&path).expect("Failed to construct path")
    2453            0 : }
    2454              : 
    2455          578 : pub fn remote_tenant_manifest_path(
    2456          578 :     tenant_shard_id: &TenantShardId,
    2457          578 :     generation: Generation,
    2458          578 : ) -> RemotePath {
    2459          578 :     let path = format!(
    2460          578 :         "tenants/{tenant_shard_id}/tenant-manifest{}.json",
    2461          578 :         generation.get_suffix()
    2462          578 :     );
    2463          578 :     RemotePath::from_string(&path).expect("Failed to construct path")
    2464          578 : }
    2465              : 
    2466              : /// Prefix to all generations' manifest objects in a tenant shard
    2467          192 : pub fn remote_tenant_manifest_prefix(tenant_shard_id: &TenantShardId) -> RemotePath {
    2468          192 :     let path = format!("tenants/{tenant_shard_id}/tenant-manifest",);
    2469          192 :     RemotePath::from_string(&path).expect("Failed to construct path")
    2470          192 : }
    2471              : 
    2472          222 : pub fn remote_timelines_path(tenant_shard_id: &TenantShardId) -> RemotePath {
    2473          222 :     let path = format!("tenants/{tenant_shard_id}/{TIMELINES_SEGMENT_NAME}");
    2474          222 :     RemotePath::from_string(&path).expect("Failed to construct path")
    2475          222 : }
    2476              : 
    2477            0 : fn remote_timelines_path_unsharded(tenant_id: &TenantId) -> RemotePath {
    2478            0 :     let path = format!("tenants/{tenant_id}/{TIMELINES_SEGMENT_NAME}");
    2479            0 :     RemotePath::from_string(&path).expect("Failed to construct path")
    2480            0 : }
    2481              : 
    2482           30 : pub fn remote_timeline_path(
    2483           30 :     tenant_shard_id: &TenantShardId,
    2484           30 :     timeline_id: &TimelineId,
    2485           30 : ) -> RemotePath {
    2486           30 :     remote_timelines_path(tenant_shard_id).join(Utf8Path::new(&timeline_id.to_string()))
    2487           30 : }
    2488              : 
    2489              : /// Obtains the path of the given Layer in the remote
    2490              : ///
    2491              : /// Note that the shard component of a remote layer path is _not_ always the same
    2492              : /// as in the TenantShardId of the caller: tenants may reference layers from a different
    2493              : /// ShardIndex.  Use the ShardIndex from the layer's metadata.
    2494         1454 : pub fn remote_layer_path(
    2495         1454 :     tenant_id: &TenantId,
    2496         1454 :     timeline_id: &TimelineId,
    2497         1454 :     shard: ShardIndex,
    2498         1454 :     layer_file_name: &LayerName,
    2499         1454 :     generation: Generation,
    2500         1454 : ) -> RemotePath {
    2501         1454 :     // Generation-aware key format
    2502         1454 :     let path = format!(
    2503         1454 :         "tenants/{tenant_id}{0}/{TIMELINES_SEGMENT_NAME}/{timeline_id}/{1}{2}",
    2504         1454 :         shard.get_suffix(),
    2505         1454 :         layer_file_name,
    2506         1454 :         generation.get_suffix()
    2507         1454 :     );
    2508         1454 : 
    2509         1454 :     RemotePath::from_string(&path).expect("Failed to construct path")
    2510         1454 : }
    2511              : 
    2512            4 : pub fn remote_initdb_archive_path(tenant_id: &TenantId, timeline_id: &TimelineId) -> RemotePath {
    2513            4 :     RemotePath::from_string(&format!(
    2514            4 :         "tenants/{tenant_id}/{TIMELINES_SEGMENT_NAME}/{timeline_id}/{INITDB_PATH}"
    2515            4 :     ))
    2516            4 :     .expect("Failed to construct path")
    2517            4 : }
    2518              : 
    2519            2 : pub fn remote_initdb_preserved_archive_path(
    2520            2 :     tenant_id: &TenantId,
    2521            2 :     timeline_id: &TimelineId,
    2522            2 : ) -> RemotePath {
    2523            2 :     RemotePath::from_string(&format!(
    2524            2 :         "tenants/{tenant_id}/{TIMELINES_SEGMENT_NAME}/{timeline_id}/{INITDB_PRESERVED_PATH}"
    2525            2 :     ))
    2526            2 :     .expect("Failed to construct path")
    2527            2 : }
    2528              : 
    2529         1480 : pub fn remote_index_path(
    2530         1480 :     tenant_shard_id: &TenantShardId,
    2531         1480 :     timeline_id: &TimelineId,
    2532         1480 :     generation: Generation,
    2533         1480 : ) -> RemotePath {
    2534         1480 :     RemotePath::from_string(&format!(
    2535         1480 :         "tenants/{tenant_shard_id}/{TIMELINES_SEGMENT_NAME}/{timeline_id}/{0}{1}",
    2536         1480 :         IndexPart::FILE_NAME,
    2537         1480 :         generation.get_suffix()
    2538         1480 :     ))
    2539         1480 :     .expect("Failed to construct path")
    2540         1480 : }
    2541              : 
    2542            0 : pub(crate) fn remote_heatmap_path(tenant_shard_id: &TenantShardId) -> RemotePath {
    2543            0 :     RemotePath::from_string(&format!(
    2544            0 :         "tenants/{tenant_shard_id}/{TENANT_HEATMAP_BASENAME}"
    2545            0 :     ))
    2546            0 :     .expect("Failed to construct path")
    2547            0 : }
    2548              : 
    2549              : /// Given the key of an index, parse out the generation part of the name
    2550           18 : pub fn parse_remote_index_path(path: RemotePath) -> Option<Generation> {
    2551           18 :     let file_name = match path.get_path().file_name() {
    2552           18 :         Some(f) => f,
    2553              :         None => {
    2554              :             // Unexpected: we should be seeing index_part.json paths only
    2555            0 :             tracing::warn!("Malformed index key {}", path);
    2556            0 :             return None;
    2557              :         }
    2558              :     };
    2559              : 
    2560           18 :     match file_name.split_once('-') {
    2561           12 :         Some((_, gen_suffix)) => Generation::parse_suffix(gen_suffix),
    2562            6 :         None => None,
    2563              :     }
    2564           18 : }
    2565              : 
    2566              : /// Given the key of a tenant manifest, parse out the generation number
    2567            0 : pub(crate) fn parse_remote_tenant_manifest_path(path: RemotePath) -> Option<Generation> {
    2568              :     static RE: OnceLock<Regex> = OnceLock::new();
    2569            0 :     let re = RE.get_or_init(|| Regex::new(r".+tenant-manifest-([0-9a-f]{8}).json").unwrap());
    2570            0 :     re.captures(path.get_path().as_str())
    2571            0 :         .and_then(|c| c.get(1))
    2572            0 :         .and_then(|m| Generation::parse_suffix(m.as_str()))
    2573            0 : }
    2574              : 
    2575              : #[cfg(test)]
    2576              : mod tests {
    2577              :     use super::*;
    2578              :     use crate::{
    2579              :         context::RequestContext,
    2580              :         tenant::{
    2581              :             config::AttachmentMode,
    2582              :             harness::{TenantHarness, TIMELINE_ID},
    2583              :             storage_layer::layer::local_layer_path,
    2584              :             Tenant, Timeline,
    2585              :         },
    2586              :         DEFAULT_PG_VERSION,
    2587              :     };
    2588              : 
    2589              :     use std::collections::HashSet;
    2590              : 
    2591            8 :     pub(super) fn dummy_contents(name: &str) -> Vec<u8> {
    2592            8 :         format!("contents for {name}").into()
    2593            8 :     }
    2594              : 
    2595            2 :     pub(super) fn dummy_metadata(disk_consistent_lsn: Lsn) -> TimelineMetadata {
    2596            2 :         let metadata = TimelineMetadata::new(
    2597            2 :             disk_consistent_lsn,
    2598            2 :             None,
    2599            2 :             None,
    2600            2 :             Lsn(0),
    2601            2 :             Lsn(0),
    2602            2 :             Lsn(0),
    2603            2 :             // Any version will do
    2604            2 :             // but it should be consistent with the one in the tests
    2605            2 :             crate::DEFAULT_PG_VERSION,
    2606            2 :         );
    2607            2 : 
    2608            2 :         // go through serialize + deserialize to fix the header, including checksum
    2609            2 :         TimelineMetadata::from_bytes(&metadata.to_bytes().unwrap()).unwrap()
    2610            2 :     }
    2611              : 
    2612            2 :     fn assert_file_list(a: &HashSet<LayerName>, b: &[&str]) {
    2613            6 :         let mut avec: Vec<String> = a.iter().map(|x| x.to_string()).collect();
    2614            2 :         avec.sort();
    2615            2 : 
    2616            2 :         let mut bvec = b.to_vec();
    2617            2 :         bvec.sort_unstable();
    2618            2 : 
    2619            2 :         assert_eq!(avec, bvec);
    2620            2 :     }
    2621              : 
    2622            4 :     fn assert_remote_files(expected: &[&str], remote_path: &Utf8Path, generation: Generation) {
    2623            4 :         let mut expected: Vec<String> = expected
    2624            4 :             .iter()
    2625           16 :             .map(|x| format!("{}{}", x, generation.get_suffix()))
    2626            4 :             .collect();
    2627            4 :         expected.sort();
    2628            4 : 
    2629            4 :         let mut found: Vec<String> = Vec::new();
    2630           16 :         for entry in std::fs::read_dir(remote_path).unwrap().flatten() {
    2631           16 :             let entry_name = entry.file_name();
    2632           16 :             let fname = entry_name.to_str().unwrap();
    2633           16 :             found.push(String::from(fname));
    2634           16 :         }
    2635            4 :         found.sort();
    2636            4 : 
    2637            4 :         assert_eq!(found, expected);
    2638            4 :     }
    2639              : 
    2640              :     struct TestSetup {
    2641              :         harness: TenantHarness,
    2642              :         tenant: Arc<Tenant>,
    2643              :         timeline: Arc<Timeline>,
    2644              :         tenant_ctx: RequestContext,
    2645              :     }
    2646              : 
    2647              :     impl TestSetup {
    2648            8 :         async fn new(test_name: &str) -> anyhow::Result<Self> {
    2649            8 :             let test_name = Box::leak(Box::new(format!("remote_timeline_client__{test_name}")));
    2650            8 :             let harness = TenantHarness::create(test_name).await?;
    2651           80 :             let (tenant, ctx) = harness.load().await;
    2652              : 
    2653            8 :             let timeline = tenant
    2654            8 :                 .create_test_timeline(TIMELINE_ID, Lsn(8), DEFAULT_PG_VERSION, &ctx)
    2655           24 :                 .await?;
    2656              : 
    2657            8 :             Ok(Self {
    2658            8 :                 harness,
    2659            8 :                 tenant,
    2660            8 :                 timeline,
    2661            8 :                 tenant_ctx: ctx,
    2662            8 :             })
    2663            8 :         }
    2664              : 
    2665              :         /// Construct a RemoteTimelineClient in an arbitrary generation
    2666           10 :         fn build_client(&self, generation: Generation) -> Arc<RemoteTimelineClient> {
    2667           10 :             let location_conf = AttachedLocationConfig {
    2668           10 :                 generation,
    2669           10 :                 attach_mode: AttachmentMode::Single,
    2670           10 :             };
    2671           10 :             Arc::new(RemoteTimelineClient {
    2672           10 :                 conf: self.harness.conf,
    2673           10 :                 runtime: tokio::runtime::Handle::current(),
    2674           10 :                 tenant_shard_id: self.harness.tenant_shard_id,
    2675           10 :                 timeline_id: TIMELINE_ID,
    2676           10 :                 generation,
    2677           10 :                 storage_impl: self.harness.remote_storage.clone(),
    2678           10 :                 deletion_queue_client: self.harness.deletion_queue.new_client(),
    2679           10 :                 upload_queue: Mutex::new(UploadQueue::Uninitialized),
    2680           10 :                 metrics: Arc::new(RemoteTimelineClientMetrics::new(
    2681           10 :                     &self.harness.tenant_shard_id,
    2682           10 :                     &TIMELINE_ID,
    2683           10 :                 )),
    2684           10 :                 config: std::sync::RwLock::new(RemoteTimelineClientConfig::from(&location_conf)),
    2685           10 :                 cancel: CancellationToken::new(),
    2686           10 :             })
    2687           10 :         }
    2688              : 
    2689              :         /// A tracing::Span that satisfies remote_timeline_client methods that assert tenant_id
    2690              :         /// and timeline_id are present.
    2691            6 :         fn span(&self) -> tracing::Span {
    2692            6 :             tracing::info_span!(
    2693              :                 "test",
    2694              :                 tenant_id = %self.harness.tenant_shard_id.tenant_id,
    2695            0 :                 shard_id = %self.harness.tenant_shard_id.shard_slug(),
    2696              :                 timeline_id = %TIMELINE_ID
    2697              :             )
    2698            6 :         }
    2699              :     }
    2700              : 
    2701              :     // Test scheduling
    2702              :     #[tokio::test]
    2703            2 :     async fn upload_scheduling() {
    2704            2 :         // Test outline:
    2705            2 :         //
    2706            2 :         // Schedule upload of a bunch of layers. Check that they are started immediately, not queued
    2707            2 :         // Schedule upload of index. Check that it is queued
    2708            2 :         // let the layer file uploads finish. Check that the index-upload is now started
    2709            2 :         // let the index-upload finish.
    2710            2 :         //
    2711            2 :         // Download back the index.json. Check that the list of files is correct
    2712            2 :         //
    2713            2 :         // Schedule upload. Schedule deletion. Check that the deletion is queued
    2714            2 :         // let upload finish. Check that deletion is now started
    2715            2 :         // Schedule another deletion. Check that it's launched immediately.
    2716            2 :         // Schedule index upload. Check that it's queued
    2717            2 : 
    2718           26 :         let test_setup = TestSetup::new("upload_scheduling").await.unwrap();
    2719            2 :         let span = test_setup.span();
    2720            2 :         let _guard = span.enter();
    2721            2 : 
    2722            2 :         let TestSetup {
    2723            2 :             harness,
    2724            2 :             tenant: _tenant,
    2725            2 :             timeline,
    2726            2 :             tenant_ctx: _tenant_ctx,
    2727            2 :         } = test_setup;
    2728            2 : 
    2729            2 :         let client = &timeline.remote_client;
    2730            2 : 
    2731            2 :         // Download back the index.json, and check that the list of files is correct
    2732            2 :         let initial_index_part = match client
    2733            2 :             .download_index_file(&CancellationToken::new())
    2734            6 :             .await
    2735            2 :             .unwrap()
    2736            2 :         {
    2737            2 :             MaybeDeletedIndexPart::IndexPart(index_part) => index_part,
    2738            2 :             MaybeDeletedIndexPart::Deleted(_) => panic!("unexpectedly got deleted index part"),
    2739            2 :         };
    2740            2 :         let initial_layers = initial_index_part
    2741            2 :             .layer_metadata
    2742            2 :             .keys()
    2743            2 :             .map(|f| f.to_owned())
    2744            2 :             .collect::<HashSet<LayerName>>();
    2745            2 :         let initial_layer = {
    2746            2 :             assert!(initial_layers.len() == 1);
    2747            2 :             initial_layers.into_iter().next().unwrap()
    2748            2 :         };
    2749            2 : 
    2750            2 :         let timeline_path = harness.timeline_path(&TIMELINE_ID);
    2751            2 : 
    2752            2 :         println!("workdir: {}", harness.conf.workdir);
    2753            2 : 
    2754            2 :         let remote_timeline_dir = harness
    2755            2 :             .remote_fs_dir
    2756            2 :             .join(timeline_path.strip_prefix(&harness.conf.workdir).unwrap());
    2757            2 :         println!("remote_timeline_dir: {remote_timeline_dir}");
    2758            2 : 
    2759            2 :         let generation = harness.generation;
    2760            2 :         let shard = harness.shard;
    2761            2 : 
    2762            2 :         // Create a couple of dummy files,  schedule upload for them
    2763            2 : 
    2764            2 :         let layers = [
    2765            2 :             ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000016B59D8-00000000016B5A51".parse().unwrap(), dummy_contents("foo")),
    2766            2 :             ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000016B59D9-00000000016B5A52".parse().unwrap(), dummy_contents("bar")),
    2767            2 :             ("000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000016B59DA-00000000016B5A53".parse().unwrap(), dummy_contents("baz"))
    2768            2 :         ]
    2769            2 :         .into_iter()
    2770            6 :         .map(|(name, contents): (LayerName, Vec<u8>)| {
    2771            6 : 
    2772            6 :             let local_path = local_layer_path(
    2773            6 :                 harness.conf,
    2774            6 :                 &timeline.tenant_shard_id,
    2775            6 :                 &timeline.timeline_id,
    2776            6 :                 &name,
    2777            6 :                 &generation,
    2778            6 :             );
    2779            6 :             std::fs::write(&local_path, &contents).unwrap();
    2780            6 : 
    2781            6 :             Layer::for_resident(
    2782            6 :                 harness.conf,
    2783            6 :                 &timeline,
    2784            6 :                 local_path,
    2785            6 :                 name,
    2786            6 :                 LayerFileMetadata::new(contents.len() as u64, generation, shard),
    2787            6 :             )
    2788            6 :         }).collect::<Vec<_>>();
    2789            2 : 
    2790            2 :         client
    2791            2 :             .schedule_layer_file_upload(layers[0].clone())
    2792            2 :             .unwrap();
    2793            2 :         client
    2794            2 :             .schedule_layer_file_upload(layers[1].clone())
    2795            2 :             .unwrap();
    2796            2 : 
    2797            2 :         // Check that they are started immediately, not queued
    2798            2 :         //
    2799            2 :         // this works because we running within block_on, so any futures are now queued up until
    2800            2 :         // our next await point.
    2801            2 :         {
    2802            2 :             let mut guard = client.upload_queue.lock().unwrap();
    2803            2 :             let upload_queue = guard.initialized_mut().unwrap();
    2804            2 :             assert!(upload_queue.queued_operations.is_empty());
    2805            2 :             assert!(upload_queue.inprogress_tasks.len() == 2);
    2806            2 :             assert!(upload_queue.num_inprogress_layer_uploads == 2);
    2807            2 : 
    2808            2 :             // also check that `latest_file_changes` was updated
    2809            2 :             assert!(upload_queue.latest_files_changes_since_metadata_upload_scheduled == 2);
    2810            2 :         }
    2811            2 : 
    2812            2 :         // Schedule upload of index. Check that it is queued
    2813            2 :         let metadata = dummy_metadata(Lsn(0x20));
    2814            2 :         client
    2815            2 :             .schedule_index_upload_for_full_metadata_update(&metadata)
    2816            2 :             .unwrap();
    2817            2 :         {
    2818            2 :             let mut guard = client.upload_queue.lock().unwrap();
    2819            2 :             let upload_queue = guard.initialized_mut().unwrap();
    2820            2 :             assert!(upload_queue.queued_operations.len() == 1);
    2821            2 :             assert!(upload_queue.latest_files_changes_since_metadata_upload_scheduled == 0);
    2822            2 :         }
    2823            2 : 
    2824            2 :         // Wait for the uploads to finish
    2825            2 :         client.wait_completion().await.unwrap();
    2826            2 :         {
    2827            2 :             let mut guard = client.upload_queue.lock().unwrap();
    2828            2 :             let upload_queue = guard.initialized_mut().unwrap();
    2829            2 : 
    2830            2 :             assert!(upload_queue.queued_operations.is_empty());
    2831            2 :             assert!(upload_queue.inprogress_tasks.is_empty());
    2832            2 :         }
    2833            2 : 
    2834            2 :         // Download back the index.json, and check that the list of files is correct
    2835            2 :         let index_part = match client
    2836            2 :             .download_index_file(&CancellationToken::new())
    2837            6 :             .await
    2838            2 :             .unwrap()
    2839            2 :         {
    2840            2 :             MaybeDeletedIndexPart::IndexPart(index_part) => index_part,
    2841            2 :             MaybeDeletedIndexPart::Deleted(_) => panic!("unexpectedly got deleted index part"),
    2842            2 :         };
    2843            2 : 
    2844            2 :         assert_file_list(
    2845            2 :             &index_part
    2846            2 :                 .layer_metadata
    2847            2 :                 .keys()
    2848            6 :                 .map(|f| f.to_owned())
    2849            2 :                 .collect(),
    2850            2 :             &[
    2851            2 :                 &initial_layer.to_string(),
    2852            2 :                 &layers[0].layer_desc().layer_name().to_string(),
    2853            2 :                 &layers[1].layer_desc().layer_name().to_string(),
    2854            2 :             ],
    2855            2 :         );
    2856            2 :         assert_eq!(index_part.metadata, metadata);
    2857            2 : 
    2858            2 :         // Schedule upload and then a deletion. Check that the deletion is queued
    2859            2 :         client
    2860            2 :             .schedule_layer_file_upload(layers[2].clone())
    2861            2 :             .unwrap();
    2862            2 : 
    2863            2 :         // this is no longer consistent with how deletion works with Layer::drop, but in this test
    2864            2 :         // keep using schedule_layer_file_deletion because we don't have a way to wait for the
    2865            2 :         // spawn_blocking started by the drop.
    2866            2 :         client
    2867            2 :             .schedule_layer_file_deletion(&[layers[0].layer_desc().layer_name()])
    2868            2 :             .unwrap();
    2869            2 :         {
    2870            2 :             let mut guard = client.upload_queue.lock().unwrap();
    2871            2 :             let upload_queue = guard.initialized_mut().unwrap();
    2872            2 : 
    2873            2 :             // Deletion schedules upload of the index file, and the file deletion itself
    2874            2 :             assert_eq!(upload_queue.queued_operations.len(), 2);
    2875            2 :             assert_eq!(upload_queue.inprogress_tasks.len(), 1);
    2876            2 :             assert_eq!(upload_queue.num_inprogress_layer_uploads, 1);
    2877            2 :             assert_eq!(upload_queue.num_inprogress_deletions, 0);
    2878            2 :             assert_eq!(
    2879            2 :                 upload_queue.latest_files_changes_since_metadata_upload_scheduled,
    2880            2 :                 0
    2881            2 :             );
    2882            2 :         }
    2883            2 :         assert_remote_files(
    2884            2 :             &[
    2885            2 :                 &initial_layer.to_string(),
    2886            2 :                 &layers[0].layer_desc().layer_name().to_string(),
    2887            2 :                 &layers[1].layer_desc().layer_name().to_string(),
    2888            2 :                 "index_part.json",
    2889            2 :             ],
    2890            2 :             &remote_timeline_dir,
    2891            2 :             generation,
    2892            2 :         );
    2893            2 : 
    2894            2 :         // Finish them
    2895            2 :         client.wait_completion().await.unwrap();
    2896            2 :         harness.deletion_queue.pump().await;
    2897            2 : 
    2898            2 :         assert_remote_files(
    2899            2 :             &[
    2900            2 :                 &initial_layer.to_string(),
    2901            2 :                 &layers[1].layer_desc().layer_name().to_string(),
    2902            2 :                 &layers[2].layer_desc().layer_name().to_string(),
    2903            2 :                 "index_part.json",
    2904            2 :             ],
    2905            2 :             &remote_timeline_dir,
    2906            2 :             generation,
    2907            2 :         );
    2908            2 :     }
    2909              : 
    2910              :     #[tokio::test]
    2911            2 :     async fn bytes_unfinished_gauge_for_layer_file_uploads() {
    2912            2 :         // Setup
    2913            2 : 
    2914            2 :         let TestSetup {
    2915            2 :             harness,
    2916            2 :             tenant: _tenant,
    2917            2 :             timeline,
    2918            2 :             ..
    2919           26 :         } = TestSetup::new("metrics").await.unwrap();
    2920            2 :         let client = &timeline.remote_client;
    2921            2 : 
    2922            2 :         let layer_file_name_1: LayerName = "000000000000000000000000000000000000-FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF__00000000016B59D8-00000000016B5A51".parse().unwrap();
    2923            2 :         let local_path = local_layer_path(
    2924            2 :             harness.conf,
    2925            2 :             &timeline.tenant_shard_id,
    2926            2 :             &timeline.timeline_id,
    2927            2 :             &layer_file_name_1,
    2928            2 :             &harness.generation,
    2929            2 :         );
    2930            2 :         let content_1 = dummy_contents("foo");
    2931            2 :         std::fs::write(&local_path, &content_1).unwrap();
    2932            2 : 
    2933            2 :         let layer_file_1 = Layer::for_resident(
    2934            2 :             harness.conf,
    2935            2 :             &timeline,
    2936            2 :             local_path,
    2937            2 :             layer_file_name_1.clone(),
    2938            2 :             LayerFileMetadata::new(content_1.len() as u64, harness.generation, harness.shard),
    2939            2 :         );
    2940            2 : 
    2941            2 :         #[derive(Debug, PartialEq, Clone, Copy)]
    2942            2 :         struct BytesStartedFinished {
    2943            2 :             started: Option<usize>,
    2944            2 :             finished: Option<usize>,
    2945            2 :         }
    2946            2 :         impl std::ops::Add for BytesStartedFinished {
    2947            2 :             type Output = Self;
    2948            4 :             fn add(self, rhs: Self) -> Self::Output {
    2949            4 :                 Self {
    2950            4 :                     started: self.started.map(|v| v + rhs.started.unwrap_or(0)),
    2951            4 :                     finished: self.finished.map(|v| v + rhs.finished.unwrap_or(0)),
    2952            4 :                 }
    2953            4 :             }
    2954            2 :         }
    2955            6 :         let get_bytes_started_stopped = || {
    2956            6 :             let started = client
    2957            6 :                 .metrics
    2958            6 :                 .get_bytes_started_counter_value(&RemoteOpFileKind::Layer, &RemoteOpKind::Upload)
    2959            6 :                 .map(|v| v.try_into().unwrap());
    2960            6 :             let stopped = client
    2961            6 :                 .metrics
    2962            6 :                 .get_bytes_finished_counter_value(&RemoteOpFileKind::Layer, &RemoteOpKind::Upload)
    2963            6 :                 .map(|v| v.try_into().unwrap());
    2964            6 :             BytesStartedFinished {
    2965            6 :                 started,
    2966            6 :                 finished: stopped,
    2967            6 :             }
    2968            6 :         };
    2969            2 : 
    2970            2 :         // Test
    2971            2 :         tracing::info!("now doing actual test");
    2972            2 : 
    2973            2 :         let actual_a = get_bytes_started_stopped();
    2974            2 : 
    2975            2 :         client
    2976            2 :             .schedule_layer_file_upload(layer_file_1.clone())
    2977            2 :             .unwrap();
    2978            2 : 
    2979            2 :         let actual_b = get_bytes_started_stopped();
    2980            2 : 
    2981            2 :         client.wait_completion().await.unwrap();
    2982            2 : 
    2983            2 :         let actual_c = get_bytes_started_stopped();
    2984            2 : 
    2985            2 :         // Validate
    2986            2 : 
    2987            2 :         let expected_b = actual_a
    2988            2 :             + BytesStartedFinished {
    2989            2 :                 started: Some(content_1.len()),
    2990            2 :                 // assert that the _finished metric is created eagerly so that subtractions work on first sample
    2991            2 :                 finished: Some(0),
    2992            2 :             };
    2993            2 :         assert_eq!(actual_b, expected_b);
    2994            2 : 
    2995            2 :         let expected_c = actual_a
    2996            2 :             + BytesStartedFinished {
    2997            2 :                 started: Some(content_1.len()),
    2998            2 :                 finished: Some(content_1.len()),
    2999            2 :             };
    3000            2 :         assert_eq!(actual_c, expected_c);
    3001            2 :     }
    3002              : 
    3003           12 :     async fn inject_index_part(test_state: &TestSetup, generation: Generation) -> IndexPart {
    3004           12 :         // An empty IndexPart, just sufficient to ensure deserialization will succeed
    3005           12 :         let example_index_part = IndexPart::example();
    3006           12 : 
    3007           12 :         let index_part_bytes = serde_json::to_vec(&example_index_part).unwrap();
    3008           12 : 
    3009           12 :         let index_path = test_state.harness.remote_fs_dir.join(
    3010           12 :             remote_index_path(
    3011           12 :                 &test_state.harness.tenant_shard_id,
    3012           12 :                 &TIMELINE_ID,
    3013           12 :                 generation,
    3014           12 :             )
    3015           12 :             .get_path(),
    3016           12 :         );
    3017           12 : 
    3018           12 :         std::fs::create_dir_all(index_path.parent().unwrap())
    3019           12 :             .expect("creating test dir should work");
    3020           12 : 
    3021           12 :         eprintln!("Writing {index_path}");
    3022           12 :         std::fs::write(&index_path, index_part_bytes).unwrap();
    3023           12 :         example_index_part
    3024           12 :     }
    3025              : 
    3026              :     /// Assert that when a RemoteTimelineclient in generation `get_generation` fetches its
    3027              :     /// index, the IndexPart returned is equal to `expected`
    3028           10 :     async fn assert_got_index_part(
    3029           10 :         test_state: &TestSetup,
    3030           10 :         get_generation: Generation,
    3031           10 :         expected: &IndexPart,
    3032           10 :     ) {
    3033           10 :         let client = test_state.build_client(get_generation);
    3034              : 
    3035           10 :         let download_r = client
    3036           10 :             .download_index_file(&CancellationToken::new())
    3037           65 :             .await
    3038           10 :             .expect("download should always succeed");
    3039           10 :         assert!(matches!(download_r, MaybeDeletedIndexPart::IndexPart(_)));
    3040           10 :         match download_r {
    3041           10 :             MaybeDeletedIndexPart::IndexPart(index_part) => {
    3042           10 :                 assert_eq!(&index_part, expected);
    3043              :             }
    3044            0 :             MaybeDeletedIndexPart::Deleted(_index_part) => panic!("Test doesn't set deleted_at"),
    3045              :         }
    3046           10 :     }
    3047              : 
    3048              :     #[tokio::test]
    3049            2 :     async fn index_part_download_simple() -> anyhow::Result<()> {
    3050           26 :         let test_state = TestSetup::new("index_part_download_simple").await.unwrap();
    3051            2 :         let span = test_state.span();
    3052            2 :         let _guard = span.enter();
    3053            2 : 
    3054            2 :         // Simple case: we are in generation N, load the index from generation N - 1
    3055            2 :         let generation_n = 5;
    3056            2 :         let injected = inject_index_part(&test_state, Generation::new(generation_n - 1)).await;
    3057            2 : 
    3058            8 :         assert_got_index_part(&test_state, Generation::new(generation_n), &injected).await;
    3059            2 : 
    3060            2 :         Ok(())
    3061            2 :     }
    3062              : 
    3063              :     #[tokio::test]
    3064            2 :     async fn index_part_download_ordering() -> anyhow::Result<()> {
    3065            2 :         let test_state = TestSetup::new("index_part_download_ordering")
    3066           26 :             .await
    3067            2 :             .unwrap();
    3068            2 : 
    3069            2 :         let span = test_state.span();
    3070            2 :         let _guard = span.enter();
    3071            2 : 
    3072            2 :         // A generation-less IndexPart exists in the bucket, we should find it
    3073            2 :         let generation_n = 5;
    3074            2 :         let injected_none = inject_index_part(&test_state, Generation::none()).await;
    3075           16 :         assert_got_index_part(&test_state, Generation::new(generation_n), &injected_none).await;
    3076            2 : 
    3077            2 :         // If a more recent-than-none generation exists, we should prefer to load that
    3078            2 :         let injected_1 = inject_index_part(&test_state, Generation::new(1)).await;
    3079           18 :         assert_got_index_part(&test_state, Generation::new(generation_n), &injected_1).await;
    3080            2 : 
    3081            2 :         // If a more-recent-than-me generation exists, we should ignore it.
    3082            2 :         let _injected_10 = inject_index_part(&test_state, Generation::new(10)).await;
    3083           20 :         assert_got_index_part(&test_state, Generation::new(generation_n), &injected_1).await;
    3084            2 : 
    3085            2 :         // If a directly previous generation exists, _and_ an index exists in my own
    3086            2 :         // generation, I should prefer my own generation.
    3087            2 :         let _injected_prev =
    3088            2 :             inject_index_part(&test_state, Generation::new(generation_n - 1)).await;
    3089            2 :         let injected_current = inject_index_part(&test_state, Generation::new(generation_n)).await;
    3090            2 :         assert_got_index_part(
    3091            2 :             &test_state,
    3092            2 :             Generation::new(generation_n),
    3093            2 :             &injected_current,
    3094            2 :         )
    3095            3 :         .await;
    3096            2 : 
    3097            2 :         Ok(())
    3098            2 :     }
    3099              : }
        

Generated by: LCOV version 2.1-beta