Line data Source code
1 : //! Helper functions to download files from remote storage with a RemoteStorage
2 : //!
3 : //! The functions in this module retry failed operations automatically, according
4 : //! to the FAILED_DOWNLOAD_RETRIES constant.
5 :
6 : use std::collections::HashSet;
7 : use std::future::Future;
8 : use std::str::FromStr;
9 : use std::time::SystemTime;
10 :
11 : use anyhow::{Context, anyhow};
12 : use camino::{Utf8Path, Utf8PathBuf};
13 : use pageserver_api::shard::TenantShardId;
14 : use remote_storage::{
15 : DownloadError, DownloadKind, DownloadOpts, GenericRemoteStorage, ListingMode, RemotePath,
16 : };
17 : use tokio::fs::{self, File, OpenOptions};
18 : use tokio::io::{AsyncSeekExt, AsyncWriteExt};
19 : use tokio_util::io::StreamReader;
20 : use tokio_util::sync::CancellationToken;
21 : use tracing::warn;
22 : use utils::crashsafe::path_with_suffix_extension;
23 : use utils::id::{TenantId, TimelineId};
24 : use utils::{backoff, pausable_failpoint};
25 :
26 : use super::index::{IndexPart, LayerFileMetadata};
27 : use super::manifest::TenantManifest;
28 : use super::{
29 : FAILED_DOWNLOAD_WARN_THRESHOLD, FAILED_REMOTE_OP_RETRIES, INITDB_PATH, parse_remote_index_path,
30 : parse_remote_tenant_manifest_path, remote_index_path, remote_initdb_archive_path,
31 : remote_initdb_preserved_archive_path, remote_tenant_manifest_path,
32 : remote_tenant_manifest_prefix, remote_tenant_path,
33 : };
34 : use crate::TEMP_FILE_SUFFIX;
35 : use crate::config::PageServerConf;
36 : use crate::context::RequestContext;
37 : use crate::span::{
38 : debug_assert_current_span_has_tenant_and_timeline_id, debug_assert_current_span_has_tenant_id,
39 : };
40 : use crate::tenant::Generation;
41 : use crate::tenant::remote_timeline_client::{remote_layer_path, remote_timelines_path};
42 : use crate::tenant::storage_layer::LayerName;
43 : use crate::virtual_file::{MaybeFatalIo, VirtualFile, on_fatal_io_error};
44 :
45 : ///
46 : /// If 'metadata' is given, we will validate that the downloaded file's size matches that
47 : /// in the metadata. (In the future, we might do more cross-checks, like CRC validation)
48 : ///
49 : /// Returns the size of the downloaded file.
50 : #[allow(clippy::too_many_arguments)]
51 28 : pub async fn download_layer_file<'a>(
52 28 : conf: &'static PageServerConf,
53 28 : storage: &'a GenericRemoteStorage,
54 28 : tenant_shard_id: TenantShardId,
55 28 : timeline_id: TimelineId,
56 28 : layer_file_name: &'a LayerName,
57 28 : layer_metadata: &'a LayerFileMetadata,
58 28 : local_path: &Utf8Path,
59 28 : gate: &utils::sync::gate::Gate,
60 28 : cancel: &CancellationToken,
61 28 : ctx: &RequestContext,
62 28 : ) -> Result<u64, DownloadError> {
63 28 : debug_assert_current_span_has_tenant_and_timeline_id();
64 28 :
65 28 : let timeline_path = conf.timeline_path(&tenant_shard_id, &timeline_id);
66 28 :
67 28 : let remote_path = remote_layer_path(
68 28 : &tenant_shard_id.tenant_id,
69 28 : &timeline_id,
70 28 : layer_metadata.shard,
71 28 : layer_file_name,
72 28 : layer_metadata.generation,
73 28 : );
74 28 :
75 28 : // Perform a rename inspired by durable_rename from file_utils.c.
76 28 : // The sequence:
77 28 : // write(tmp)
78 28 : // fsync(tmp)
79 28 : // rename(tmp, new)
80 28 : // fsync(new)
81 28 : // fsync(parent)
82 28 : // For more context about durable_rename check this email from postgres mailing list:
83 28 : // https://www.postgresql.org/message-id/56583BDD.9060302@2ndquadrant.com
84 28 : // If pageserver crashes the temp file will be deleted on startup and re-downloaded.
85 28 : let temp_file_path = path_with_suffix_extension(local_path, TEMP_DOWNLOAD_EXTENSION);
86 :
87 28 : let bytes_amount = download_retry(
88 28 : || async {
89 28 : download_object(storage, &remote_path, &temp_file_path, gate, cancel, ctx).await
90 56 : },
91 28 : &format!("download {remote_path:?}"),
92 28 : cancel,
93 28 : )
94 28 : .await?;
95 :
96 28 : let expected = layer_metadata.file_size;
97 28 : if expected != bytes_amount {
98 0 : return Err(DownloadError::Other(anyhow!(
99 0 : "According to layer file metadata should have downloaded {expected} bytes but downloaded {bytes_amount} bytes into file {temp_file_path:?}",
100 0 : )));
101 28 : }
102 28 :
103 28 : fail::fail_point!("remote-storage-download-pre-rename", |_| {
104 0 : Err(DownloadError::Other(anyhow!(
105 0 : "remote-storage-download-pre-rename failpoint triggered"
106 0 : )))
107 28 : });
108 :
109 28 : fs::rename(&temp_file_path, &local_path)
110 28 : .await
111 28 : .with_context(|| format!("rename download layer file to {local_path}"))
112 28 : .map_err(DownloadError::Other)?;
113 :
114 : // We use fatal_err() below because the after the rename above,
115 : // the in-memory state of the filesystem already has the layer file in its final place,
116 : // and subsequent pageserver code could think it's durable while it really isn't.
117 28 : let work = {
118 28 : let ctx = ctx.detached_child(ctx.task_kind(), ctx.download_behavior());
119 28 : async move {
120 28 : let timeline_dir = VirtualFile::open(&timeline_path, &ctx)
121 28 : .await
122 28 : .fatal_err("VirtualFile::open for timeline dir fsync");
123 28 : timeline_dir
124 28 : .sync_all()
125 28 : .await
126 28 : .fatal_err("VirtualFile::sync_all timeline dir");
127 28 : }
128 : };
129 28 : crate::virtual_file::io_engine::get()
130 28 : .spawn_blocking_and_block_on_if_std(work)
131 28 : .await;
132 :
133 28 : tracing::debug!("download complete: {local_path}");
134 :
135 28 : Ok(bytes_amount)
136 28 : }
137 :
138 : /// Download the object `src_path` in the remote `storage` to local path `dst_path`.
139 : ///
140 : /// If Ok() is returned, the download succeeded and the inode & data have been made durable.
141 : /// (Note that the directory entry for the inode is not made durable.)
142 : /// The file size in bytes is returned.
143 : ///
144 : /// If Err() is returned, there was some error. The file at `dst_path` has been unlinked.
145 : /// The unlinking has _not_ been made durable.
146 28 : async fn download_object(
147 28 : storage: &GenericRemoteStorage,
148 28 : src_path: &RemotePath,
149 28 : dst_path: &Utf8PathBuf,
150 28 : #[cfg_attr(target_os = "macos", allow(unused_variables))] gate: &utils::sync::gate::Gate,
151 28 : cancel: &CancellationToken,
152 28 : #[cfg_attr(target_os = "macos", allow(unused_variables))] ctx: &RequestContext,
153 28 : ) -> Result<u64, DownloadError> {
154 28 : let res = match crate::virtual_file::io_engine::get() {
155 0 : crate::virtual_file::io_engine::IoEngine::NotSet => panic!("unset"),
156 : crate::virtual_file::io_engine::IoEngine::StdFs => {
157 14 : async {
158 14 : let destination_file = tokio::fs::File::create(dst_path)
159 14 : .await
160 14 : .with_context(|| format!("create a destination file for layer '{dst_path}'"))
161 14 : .map_err(DownloadError::Other)?;
162 :
163 14 : let download = storage
164 14 : .download(src_path, &DownloadOpts::default(), cancel)
165 14 : .await?;
166 :
167 14 : pausable_failpoint!("before-downloading-layer-stream-pausable");
168 :
169 14 : let mut buf_writer =
170 14 : tokio::io::BufWriter::with_capacity(super::BUFFER_SIZE, destination_file);
171 14 :
172 14 : let mut reader = tokio_util::io::StreamReader::new(download.download_stream);
173 :
174 14 : let bytes_amount = tokio::io::copy_buf(&mut reader, &mut buf_writer).await?;
175 14 : buf_writer.flush().await?;
176 :
177 14 : let mut destination_file = buf_writer.into_inner();
178 14 :
179 14 : // Tokio doc here: https://docs.rs/tokio/1.17.0/tokio/fs/struct.File.html states that:
180 14 : // A file will not be closed immediately when it goes out of scope if there are any IO operations
181 14 : // that have not yet completed. To ensure that a file is closed immediately when it is dropped,
182 14 : // you should call flush before dropping it.
183 14 : //
184 14 : // From the tokio code I see that it waits for pending operations to complete. There shouldt be any because
185 14 : // we assume that `destination_file` file is fully written. I e there is no pending .write(...).await operations.
186 14 : // But for additional safety lets check/wait for any pending operations.
187 14 : destination_file
188 14 : .flush()
189 14 : .await
190 14 : .maybe_fatal_err("download_object sync_all")
191 14 : .with_context(|| format!("flush source file at {dst_path}"))
192 14 : .map_err(DownloadError::Other)?;
193 :
194 : // not using sync_data because it can lose file size update
195 14 : destination_file
196 14 : .sync_all()
197 14 : .await
198 14 : .maybe_fatal_err("download_object sync_all")
199 14 : .with_context(|| format!("failed to fsync source file at {dst_path}"))
200 14 : .map_err(DownloadError::Other)?;
201 :
202 14 : Ok(bytes_amount)
203 14 : }
204 14 : .await
205 : }
206 : #[cfg(target_os = "linux")]
207 : crate::virtual_file::io_engine::IoEngine::TokioEpollUring => {
208 : use std::sync::Arc;
209 :
210 : use crate::virtual_file::{IoBufferMut, owned_buffers_io};
211 14 : async {
212 14 : let destination_file = Arc::new(
213 14 : VirtualFile::create(dst_path, ctx)
214 14 : .await
215 14 : .with_context(|| {
216 0 : format!("create a destination file for layer '{dst_path}'")
217 14 : })
218 14 : .map_err(DownloadError::Other)?,
219 : );
220 :
221 14 : let mut download = storage
222 14 : .download(src_path, &DownloadOpts::default(), cancel)
223 14 : .await?;
224 :
225 14 : pausable_failpoint!("before-downloading-layer-stream-pausable");
226 :
227 14 : let mut buffered = owned_buffers_io::write::BufferedWriter::<IoBufferMut, _>::new(
228 14 : destination_file,
229 28 : || IoBufferMut::with_capacity(super::BUFFER_SIZE),
230 14 : gate.enter().map_err(|_| DownloadError::Cancelled)?,
231 14 : ctx,
232 : );
233 :
234 : // TODO: use vectored write (writev) once supported by tokio-epoll-uring.
235 : // There's chunks_vectored() on the stream.
236 14 : let (bytes_amount, destination_file) = async {
237 84 : while let Some(res) =
238 98 : futures::StreamExt::next(&mut download.download_stream).await
239 : {
240 84 : let chunk = match res {
241 84 : Ok(chunk) => chunk,
242 0 : Err(e) => return Err(e),
243 : };
244 84 : buffered.write_buffered_borrowed(&chunk, ctx).await?;
245 : }
246 14 : let inner = buffered.flush_and_into_inner(ctx).await?;
247 14 : Ok(inner)
248 14 : }
249 14 : .await?;
250 :
251 : // not using sync_data because it can lose file size update
252 14 : destination_file
253 14 : .sync_all()
254 14 : .await
255 14 : .maybe_fatal_err("download_object sync_all")
256 14 : .with_context(|| format!("failed to fsync source file at {dst_path}"))
257 14 : .map_err(DownloadError::Other)?;
258 :
259 14 : Ok(bytes_amount)
260 14 : }
261 14 : .await
262 : }
263 : };
264 :
265 : // in case the download failed, clean up
266 28 : match res {
267 28 : Ok(bytes_amount) => Ok(bytes_amount),
268 0 : Err(e) => {
269 0 : if let Err(e) = tokio::fs::remove_file(dst_path).await {
270 0 : if e.kind() != std::io::ErrorKind::NotFound {
271 0 : on_fatal_io_error(&e, &format!("Removing temporary file {dst_path}"));
272 0 : }
273 0 : }
274 0 : Err(e)
275 : }
276 : }
277 28 : }
278 :
279 : const TEMP_DOWNLOAD_EXTENSION: &str = "temp_download";
280 :
281 0 : pub(crate) fn is_temp_download_file(path: &Utf8Path) -> bool {
282 0 : let extension = path.extension();
283 0 : match extension {
284 0 : Some(TEMP_DOWNLOAD_EXTENSION) => true,
285 0 : Some(_) => false,
286 0 : None => false,
287 : }
288 0 : }
289 :
290 452 : async fn list_identifiers<T>(
291 452 : storage: &GenericRemoteStorage,
292 452 : prefix: RemotePath,
293 452 : cancel: CancellationToken,
294 452 : ) -> anyhow::Result<(HashSet<T>, HashSet<String>)>
295 452 : where
296 452 : T: FromStr + Eq + std::hash::Hash,
297 452 : {
298 452 : let listing = download_retry_forever(
299 452 : || storage.list(Some(&prefix), ListingMode::WithDelimiter, None, &cancel),
300 452 : &format!("list identifiers in prefix {prefix}"),
301 452 : &cancel,
302 452 : )
303 452 : .await?;
304 :
305 452 : let mut parsed_ids = HashSet::new();
306 452 : let mut other_prefixes = HashSet::new();
307 :
308 464 : for id_remote_storage_key in listing.prefixes {
309 12 : let object_name = id_remote_storage_key.object_name().ok_or_else(|| {
310 0 : anyhow::anyhow!("failed to get object name for key {id_remote_storage_key}")
311 12 : })?;
312 :
313 12 : match object_name.parse::<T>() {
314 12 : Ok(t) => parsed_ids.insert(t),
315 0 : Err(_) => other_prefixes.insert(object_name.to_string()),
316 : };
317 : }
318 :
319 452 : for object in listing.keys {
320 0 : let object_name = object
321 0 : .key
322 0 : .object_name()
323 0 : .ok_or_else(|| anyhow::anyhow!("object name for key {}", object.key))?;
324 0 : other_prefixes.insert(object_name.to_string());
325 : }
326 :
327 452 : Ok((parsed_ids, other_prefixes))
328 452 : }
329 :
330 : /// List shards of given tenant in remote storage
331 0 : pub(crate) async fn list_remote_tenant_shards(
332 0 : storage: &GenericRemoteStorage,
333 0 : tenant_id: TenantId,
334 0 : cancel: CancellationToken,
335 0 : ) -> anyhow::Result<(HashSet<TenantShardId>, HashSet<String>)> {
336 0 : let remote_path = remote_tenant_path(&TenantShardId::unsharded(tenant_id));
337 0 : list_identifiers::<TenantShardId>(storage, remote_path, cancel).await
338 0 : }
339 :
340 : /// List timelines of given tenant shard in remote storage
341 452 : pub async fn list_remote_timelines(
342 452 : storage: &GenericRemoteStorage,
343 452 : tenant_shard_id: TenantShardId,
344 452 : cancel: CancellationToken,
345 452 : ) -> anyhow::Result<(HashSet<TimelineId>, HashSet<String>)> {
346 452 : fail::fail_point!("storage-sync-list-remote-timelines", |_| {
347 0 : anyhow::bail!("storage-sync-list-remote-timelines");
348 452 : });
349 :
350 452 : let remote_path = remote_timelines_path(&tenant_shard_id).add_trailing_slash();
351 452 : list_identifiers::<TimelineId>(storage, remote_path, cancel).await
352 452 : }
353 :
354 1424 : async fn do_download_remote_path_retry_forever(
355 1424 : storage: &GenericRemoteStorage,
356 1424 : remote_path: &RemotePath,
357 1424 : download_opts: DownloadOpts,
358 1424 : cancel: &CancellationToken,
359 1424 : ) -> Result<(Vec<u8>, SystemTime), DownloadError> {
360 1424 : download_retry_forever(
361 1424 : || async {
362 1424 : let download = storage
363 1424 : .download(remote_path, &download_opts, cancel)
364 1424 : .await?;
365 :
366 40 : let mut bytes = Vec::new();
367 40 :
368 40 : let stream = download.download_stream;
369 40 : let mut stream = StreamReader::new(stream);
370 40 :
371 40 : tokio::io::copy_buf(&mut stream, &mut bytes).await?;
372 :
373 40 : Ok((bytes, download.last_modified))
374 2848 : },
375 1424 : &format!("download {remote_path:?}"),
376 1424 : cancel,
377 1424 : )
378 1424 : .await
379 1424 : }
380 :
381 1356 : async fn do_download_tenant_manifest(
382 1356 : storage: &GenericRemoteStorage,
383 1356 : tenant_shard_id: &TenantShardId,
384 1356 : _timeline_id: Option<&TimelineId>,
385 1356 : generation: Generation,
386 1356 : cancel: &CancellationToken,
387 1356 : ) -> Result<(TenantManifest, Generation, SystemTime), DownloadError> {
388 1356 : let remote_path = remote_tenant_manifest_path(tenant_shard_id, generation);
389 1356 :
390 1356 : let download_opts = DownloadOpts {
391 1356 : kind: DownloadKind::Small,
392 1356 : ..Default::default()
393 1356 : };
394 :
395 0 : let (manifest_bytes, manifest_bytes_mtime) =
396 1356 : do_download_remote_path_retry_forever(storage, &remote_path, download_opts, cancel).await?;
397 :
398 0 : let tenant_manifest = TenantManifest::from_json_bytes(&manifest_bytes)
399 0 : .with_context(|| format!("deserialize tenant manifest file at {remote_path:?}"))
400 0 : .map_err(DownloadError::Other)?;
401 :
402 0 : Ok((tenant_manifest, generation, manifest_bytes_mtime))
403 1356 : }
404 :
405 68 : async fn do_download_index_part(
406 68 : storage: &GenericRemoteStorage,
407 68 : tenant_shard_id: &TenantShardId,
408 68 : timeline_id: Option<&TimelineId>,
409 68 : index_generation: Generation,
410 68 : cancel: &CancellationToken,
411 68 : ) -> Result<(IndexPart, Generation, SystemTime), DownloadError> {
412 68 : let timeline_id =
413 68 : timeline_id.expect("A timeline ID is always provided when downloading an index");
414 68 : let remote_path = remote_index_path(tenant_shard_id, timeline_id, index_generation);
415 68 :
416 68 : let download_opts = DownloadOpts {
417 68 : kind: DownloadKind::Small,
418 68 : ..Default::default()
419 68 : };
420 :
421 40 : let (index_part_bytes, index_part_mtime) =
422 68 : do_download_remote_path_retry_forever(storage, &remote_path, download_opts, cancel).await?;
423 :
424 40 : let index_part: IndexPart = serde_json::from_slice(&index_part_bytes)
425 40 : .with_context(|| format!("deserialize index part file at {remote_path:?}"))
426 40 : .map_err(DownloadError::Other)?;
427 :
428 40 : Ok((index_part, index_generation, index_part_mtime))
429 68 : }
430 :
431 : /// Metadata objects are "generationed", meaning that they include a generation suffix. This
432 : /// function downloads the object with the highest generation <= `my_generation`.
433 : ///
434 : /// Data objects (layer files) also include a generation in their path, but there is no equivalent
435 : /// search process, because their reference from an index includes the generation.
436 : ///
437 : /// An expensive object listing operation is only done if necessary: the typical fast path is to issue two
438 : /// GET operations, one to our own generation (stale attachment case), and one to the immediately preceding
439 : /// generation (normal case when migrating/restarting). Only if both of these return 404 do we fall back
440 : /// to listing objects.
441 : ///
442 : /// * `my_generation`: the value of `[crate::tenant::Tenant::generation]`
443 : /// * `what`: for logging, what object are we downloading
444 : /// * `prefix`: when listing objects, use this prefix (i.e. the part of the object path before the generation)
445 : /// * `do_download`: a GET of the object in a particular generation, which should **retry indefinitely** unless
446 : /// `cancel`` has fired. This function does not do its own retries of GET operations, and relies
447 : /// on the function passed in to do so.
448 : /// * `parse_path`: parse a fully qualified remote storage path to get the generation of the object.
449 : #[allow(clippy::too_many_arguments)]
450 : #[tracing::instrument(skip_all, fields(generation=?my_generation))]
451 : pub(crate) async fn download_generation_object<'a, T, DF, DFF, PF>(
452 : storage: &'a GenericRemoteStorage,
453 : tenant_shard_id: &'a TenantShardId,
454 : timeline_id: Option<&'a TimelineId>,
455 : my_generation: Generation,
456 : what: &str,
457 : prefix: RemotePath,
458 : do_download: DF,
459 : parse_path: PF,
460 : cancel: &'a CancellationToken,
461 : ) -> Result<(T, Generation, SystemTime), DownloadError>
462 : where
463 : DF: Fn(
464 : &'a GenericRemoteStorage,
465 : &'a TenantShardId,
466 : Option<&'a TimelineId>,
467 : Generation,
468 : &'a CancellationToken,
469 : ) -> DFF,
470 : DFF: Future<Output = Result<(T, Generation, SystemTime), DownloadError>>,
471 : PF: Fn(RemotePath) -> Option<Generation>,
472 : T: 'static,
473 : {
474 : debug_assert_current_span_has_tenant_id();
475 :
476 : if my_generation.is_none() {
477 : // Operating without generations: just fetch the generation-less path
478 : return do_download(storage, tenant_shard_id, timeline_id, my_generation, cancel).await;
479 : }
480 :
481 : // Stale case: If we were intentionally attached in a stale generation, the remote object may already
482 : // exist in our generation.
483 : //
484 : // This is an optimization to avoid doing the listing for the general case below.
485 : let res = do_download(storage, tenant_shard_id, timeline_id, my_generation, cancel).await;
486 : match res {
487 : Ok(decoded) => {
488 : tracing::debug!("Found {what} from current generation (this is a stale attachment)");
489 : return Ok(decoded);
490 : }
491 : Err(DownloadError::NotFound) => {}
492 : Err(e) => return Err(e),
493 : };
494 :
495 : // Typical case: the previous generation of this tenant was running healthily, and had uploaded the object
496 : // we are seeking in that generation. We may safely start from this index without doing a listing, because:
497 : // - We checked for current generation case above
498 : // - generations > my_generation are to be ignored
499 : // - any other objects that exist would have an older generation than `previous_gen`, and
500 : // we want to find the most recent object from a previous generation.
501 : //
502 : // This is an optimization to avoid doing the listing for the general case below.
503 : let res = do_download(
504 : storage,
505 : tenant_shard_id,
506 : timeline_id,
507 : my_generation.previous(),
508 : cancel,
509 : )
510 : .await;
511 : match res {
512 : Ok(decoded) => {
513 : tracing::debug!("Found {what} from previous generation");
514 : return Ok(decoded);
515 : }
516 : Err(DownloadError::NotFound) => {
517 : tracing::debug!("No {what} found from previous generation, falling back to listing");
518 : }
519 : Err(e) => {
520 : return Err(e);
521 : }
522 : }
523 :
524 : // General case/fallback: if there is no index at my_generation or prev_generation, then list all index_part.json
525 : // objects, and select the highest one with a generation <= my_generation. Constructing the prefix is equivalent
526 : // to constructing a full index path with no generation, because the generation is a suffix.
527 : let paths = download_retry(
528 464 : || async {
529 464 : storage
530 464 : .list(Some(&prefix), ListingMode::NoDelimiter, None, cancel)
531 464 : .await
532 928 : },
533 : "list index_part files",
534 : cancel,
535 : )
536 : .await?
537 : .keys;
538 :
539 : // General case logic for which index to use: the latest index whose generation
540 : // is <= our own. See "Finding the remote indices for timelines" in docs/rfcs/025-generation-numbers.md
541 : let max_previous_generation = paths
542 : .into_iter()
543 36 : .filter_map(|o| parse_path(o.key))
544 24 : .filter(|g| g <= &my_generation)
545 : .max();
546 :
547 : match max_previous_generation {
548 : Some(g) => {
549 : tracing::debug!("Found {what} in generation {g:?}");
550 : do_download(storage, tenant_shard_id, timeline_id, g, cancel).await
551 : }
552 : None => {
553 : // Migration from legacy pre-generation state: we have a generation but no prior
554 : // attached pageservers did. Try to load from a no-generation path.
555 : tracing::debug!("No {what}* found");
556 : do_download(
557 : storage,
558 : tenant_shard_id,
559 : timeline_id,
560 : Generation::none(),
561 : cancel,
562 : )
563 : .await
564 : }
565 : }
566 : }
567 :
568 : /// index_part.json objects are suffixed with a generation number, so we cannot
569 : /// directly GET the latest index part without doing some probing.
570 : ///
571 : /// In this function we probe for the most recent index in a generation <= our current generation.
572 : /// See "Finding the remote indices for timelines" in docs/rfcs/025-generation-numbers.md
573 40 : pub(crate) async fn download_index_part(
574 40 : storage: &GenericRemoteStorage,
575 40 : tenant_shard_id: &TenantShardId,
576 40 : timeline_id: &TimelineId,
577 40 : my_generation: Generation,
578 40 : cancel: &CancellationToken,
579 40 : ) -> Result<(IndexPart, Generation, SystemTime), DownloadError> {
580 40 : debug_assert_current_span_has_tenant_and_timeline_id();
581 40 :
582 40 : let index_prefix = remote_index_path(tenant_shard_id, timeline_id, Generation::none());
583 40 : download_generation_object(
584 40 : storage,
585 40 : tenant_shard_id,
586 40 : Some(timeline_id),
587 40 : my_generation,
588 40 : "index_part",
589 40 : index_prefix,
590 40 : do_download_index_part,
591 40 : parse_remote_index_path,
592 40 : cancel,
593 40 : )
594 40 : .await
595 40 : }
596 :
597 452 : pub(crate) async fn download_tenant_manifest(
598 452 : storage: &GenericRemoteStorage,
599 452 : tenant_shard_id: &TenantShardId,
600 452 : my_generation: Generation,
601 452 : cancel: &CancellationToken,
602 452 : ) -> Result<(TenantManifest, Generation, SystemTime), DownloadError> {
603 452 : let manifest_prefix = remote_tenant_manifest_prefix(tenant_shard_id);
604 452 :
605 452 : download_generation_object(
606 452 : storage,
607 452 : tenant_shard_id,
608 452 : None,
609 452 : my_generation,
610 452 : "tenant-manifest",
611 452 : manifest_prefix,
612 452 : do_download_tenant_manifest,
613 452 : parse_remote_tenant_manifest_path,
614 452 : cancel,
615 452 : )
616 452 : .await
617 452 : }
618 :
619 4 : pub(crate) async fn download_initdb_tar_zst(
620 4 : conf: &'static PageServerConf,
621 4 : storage: &GenericRemoteStorage,
622 4 : tenant_shard_id: &TenantShardId,
623 4 : timeline_id: &TimelineId,
624 4 : cancel: &CancellationToken,
625 4 : ) -> Result<(Utf8PathBuf, File), DownloadError> {
626 4 : debug_assert_current_span_has_tenant_and_timeline_id();
627 4 :
628 4 : let remote_path = remote_initdb_archive_path(&tenant_shard_id.tenant_id, timeline_id);
629 4 :
630 4 : let remote_preserved_path =
631 4 : remote_initdb_preserved_archive_path(&tenant_shard_id.tenant_id, timeline_id);
632 4 :
633 4 : let timeline_path = conf.timelines_path(tenant_shard_id);
634 4 :
635 4 : if !timeline_path.exists() {
636 0 : tokio::fs::create_dir_all(&timeline_path)
637 0 : .await
638 0 : .with_context(|| format!("timeline dir creation {timeline_path}"))
639 0 : .map_err(DownloadError::Other)?;
640 4 : }
641 4 : let temp_path = timeline_path.join(format!(
642 4 : "{INITDB_PATH}.download-{timeline_id}.{TEMP_FILE_SUFFIX}"
643 4 : ));
644 :
645 4 : let file = download_retry(
646 4 : || async {
647 4 : let file = OpenOptions::new()
648 4 : .create(true)
649 4 : .truncate(true)
650 4 : .read(true)
651 4 : .write(true)
652 4 : .open(&temp_path)
653 4 : .await
654 4 : .with_context(|| format!("tempfile creation {temp_path}"))
655 4 : .map_err(DownloadError::Other)?;
656 :
657 4 : let download = match storage
658 4 : .download(&remote_path, &DownloadOpts::default(), cancel)
659 4 : .await
660 : {
661 4 : Ok(dl) => dl,
662 : Err(DownloadError::NotFound) => {
663 0 : storage
664 0 : .download(&remote_preserved_path, &DownloadOpts::default(), cancel)
665 0 : .await?
666 : }
667 0 : Err(other) => Err(other)?,
668 : };
669 4 : let mut download = tokio_util::io::StreamReader::new(download.download_stream);
670 4 : let mut writer = tokio::io::BufWriter::with_capacity(super::BUFFER_SIZE, file);
671 4 :
672 4 : tokio::io::copy_buf(&mut download, &mut writer).await?;
673 :
674 4 : let mut file = writer.into_inner();
675 4 :
676 4 : file.seek(std::io::SeekFrom::Start(0))
677 4 : .await
678 4 : .with_context(|| format!("rewinding initdb.tar.zst at: {remote_path:?}"))
679 4 : .map_err(DownloadError::Other)?;
680 :
681 4 : Ok(file)
682 8 : },
683 4 : &format!("download {remote_path}"),
684 4 : cancel,
685 4 : )
686 4 : .await
687 4 : .inspect_err(|_e| {
688 : // Do a best-effort attempt at deleting the temporary file upon encountering an error.
689 : // We don't have async here nor do we want to pile on any extra errors.
690 0 : if let Err(e) = std::fs::remove_file(&temp_path) {
691 0 : if e.kind() != std::io::ErrorKind::NotFound {
692 0 : warn!("error deleting temporary file {temp_path}: {e}");
693 0 : }
694 0 : }
695 4 : })?;
696 :
697 4 : Ok((temp_path, file))
698 4 : }
699 :
700 : /// Helper function to handle retries for a download operation.
701 : ///
702 : /// Remote operations can fail due to rate limits (S3), spurious network
703 : /// problems, or other external reasons. Retry FAILED_DOWNLOAD_RETRIES times,
704 : /// with backoff.
705 : ///
706 : /// (See similar logic for uploads in `perform_upload_task`)
707 496 : pub(super) async fn download_retry<T, O, F>(
708 496 : op: O,
709 496 : description: &str,
710 496 : cancel: &CancellationToken,
711 496 : ) -> Result<T, DownloadError>
712 496 : where
713 496 : O: FnMut() -> F,
714 496 : F: Future<Output = Result<T, DownloadError>>,
715 496 : {
716 496 : backoff::retry(
717 496 : op,
718 496 : DownloadError::is_permanent,
719 496 : FAILED_DOWNLOAD_WARN_THRESHOLD,
720 496 : FAILED_REMOTE_OP_RETRIES,
721 496 : description,
722 496 : cancel,
723 496 : )
724 496 : .await
725 496 : .ok_or_else(|| DownloadError::Cancelled)
726 496 : .and_then(|x| x)
727 496 : }
728 :
729 1876 : pub(crate) async fn download_retry_forever<T, O, F>(
730 1876 : op: O,
731 1876 : description: &str,
732 1876 : cancel: &CancellationToken,
733 1876 : ) -> Result<T, DownloadError>
734 1876 : where
735 1876 : O: FnMut() -> F,
736 1876 : F: Future<Output = Result<T, DownloadError>>,
737 1876 : {
738 1876 : backoff::retry(
739 1876 : op,
740 1876 : DownloadError::is_permanent,
741 1876 : FAILED_DOWNLOAD_WARN_THRESHOLD,
742 1876 : u32::MAX,
743 1876 : description,
744 1876 : cancel,
745 1876 : )
746 1876 : .await
747 1876 : .ok_or_else(|| DownloadError::Cancelled)
748 1876 : .and_then(|x| x)
749 1876 : }
|