Line data Source code
1 : use std::{
2 : ops::{Deref, DerefMut},
3 : sync::Arc,
4 : };
5 :
6 : use anyhow::Context;
7 : use pageserver_api::{models::TimelineState, shard::TenantShardId};
8 : use tokio::sync::OwnedMutexGuard;
9 : use tracing::{error, info, instrument, Instrument};
10 : use utils::{crashsafe, fs_ext, id::TimelineId};
11 :
12 : use crate::{
13 : config::PageServerConf,
14 : deletion_queue::DeletionQueueClient,
15 : task_mgr::{self, TaskKind},
16 : tenant::{
17 : metadata::TimelineMetadata,
18 : remote_timeline_client::{PersistIndexPartWithDeletedFlagError, RemoteTimelineClient},
19 : CreateTimelineCause, DeleteTimelineError, Tenant,
20 : },
21 : };
22 :
23 : use super::{Timeline, TimelineResources};
24 :
25 : /// Mark timeline as deleted in S3 so we won't pick it up next time
26 : /// during attach or pageserver restart.
27 : /// See comment in persist_index_part_with_deleted_flag.
28 0 : async fn set_deleted_in_remote_index(timeline: &Timeline) -> Result<(), DeleteTimelineError> {
29 0 : if let Some(remote_client) = timeline.remote_client.as_ref() {
30 0 : match remote_client.persist_index_part_with_deleted_flag().await {
31 : // If we (now, or already) marked it successfully as deleted, we can proceed
32 0 : Ok(()) | Err(PersistIndexPartWithDeletedFlagError::AlreadyDeleted(_)) => (),
33 : // Bail out otherwise
34 : //
35 : // AlreadyInProgress shouldn't happen, because the 'delete_lock' prevents
36 : // two tasks from performing the deletion at the same time. The first task
37 : // that starts deletion should run it to completion.
38 0 : Err(e @ PersistIndexPartWithDeletedFlagError::AlreadyInProgress(_))
39 0 : | Err(e @ PersistIndexPartWithDeletedFlagError::Other(_)) => {
40 0 : return Err(DeleteTimelineError::Other(anyhow::anyhow!(e)));
41 : }
42 : }
43 0 : }
44 0 : Ok(())
45 0 : }
46 :
47 : /// Grab the compaction and gc locks, and actually perform the deletion.
48 : ///
49 : /// The locks prevent GC or compaction from running at the same time. The background tasks do not
50 : /// register themselves with the timeline it's operating on, so it might still be running even
51 : /// though we called `shutdown_tasks`.
52 : ///
53 : /// Note that there are still other race conditions between
54 : /// GC, compaction and timeline deletion. See
55 : /// <https://github.com/neondatabase/neon/issues/2671>
56 : ///
57 : /// No timeout here, GC & Compaction should be responsive to the
58 : /// `TimelineState::Stopping` change.
59 : // pub(super): documentation link
60 0 : pub(super) async fn delete_local_timeline_directory(
61 0 : conf: &PageServerConf,
62 0 : tenant_shard_id: TenantShardId,
63 0 : timeline: &Timeline,
64 0 : ) -> anyhow::Result<()> {
65 0 : let guards = async { tokio::join!(timeline.gc_lock.lock(), timeline.compaction_lock.lock()) };
66 0 : let guards = crate::timed(
67 0 : guards,
68 0 : "acquire gc and compaction locks",
69 0 : std::time::Duration::from_secs(5),
70 0 : )
71 0 : .await;
72 :
73 : // NB: storage_sync upload tasks that reference these layers have been cancelled
74 : // by the caller.
75 :
76 0 : let local_timeline_directory = conf.timeline_path(&tenant_shard_id, &timeline.timeline_id);
77 0 :
78 0 : fail::fail_point!("timeline-delete-before-rm", |_| {
79 0 : Err(anyhow::anyhow!("failpoint: timeline-delete-before-rm"))?
80 0 : });
81 :
82 : // NB: This need not be atomic because the deleted flag in the IndexPart
83 : // will be observed during tenant/timeline load. The deletion will be resumed there.
84 : //
85 : // Note that here we do not bail out on std::io::ErrorKind::NotFound.
86 : // This can happen if we're called a second time, e.g.,
87 : // because of a previous failure/cancellation at/after
88 : // failpoint timeline-delete-after-rm.
89 : //
90 : // ErrorKind::NotFound can also happen if we race with tenant detach, because,
91 : // no locks are shared.
92 0 : tokio::fs::remove_dir_all(local_timeline_directory)
93 0 : .await
94 0 : .or_else(fs_ext::ignore_not_found)
95 0 : .context("remove local timeline directory")?;
96 :
97 : // Make sure previous deletions are ordered before mark removal.
98 : // Otherwise there is no guarantee that they reach the disk before mark deletion.
99 : // So its possible for mark to reach disk first and for other deletions
100 : // to be reordered later and thus missed if a crash occurs.
101 : // Note that we dont need to sync after mark file is removed
102 : // because we can tolerate the case when mark file reappears on startup.
103 0 : let timeline_path = conf.timelines_path(&tenant_shard_id);
104 0 : crashsafe::fsync_async(timeline_path)
105 0 : .await
106 0 : .context("fsync_pre_mark_remove")?;
107 :
108 0 : info!("finished deleting layer files, releasing locks");
109 0 : drop(guards);
110 0 :
111 0 : fail::fail_point!("timeline-delete-after-rm", |_| {
112 0 : Err(anyhow::anyhow!("failpoint: timeline-delete-after-rm"))?
113 0 : });
114 :
115 0 : Ok(())
116 0 : }
117 :
118 : /// Removes remote layers and an index file after them.
119 0 : async fn delete_remote_layers_and_index(timeline: &Timeline) -> anyhow::Result<()> {
120 0 : if let Some(remote_client) = &timeline.remote_client {
121 0 : remote_client.delete_all().await.context("delete_all")?
122 0 : };
123 :
124 0 : Ok(())
125 0 : }
126 :
127 : // This function removs remaining traces of a timeline on disk.
128 : // Namely: metadata file, timeline directory, delete mark.
129 : // Note: io::ErrorKind::NotFound are ignored for metadata and timeline dir.
130 : // delete mark should be present because it is the last step during deletion.
131 : // (nothing can fail after its deletion)
132 0 : async fn cleanup_remaining_timeline_fs_traces(
133 0 : conf: &PageServerConf,
134 0 : tenant_shard_id: TenantShardId,
135 0 : timeline_id: TimelineId,
136 0 : ) -> anyhow::Result<()> {
137 0 : // Remove delete mark
138 0 : // TODO: once we are confident that no more exist in the field, remove this
139 0 : // line. It cleans up a legacy marker file that might in rare cases be present.
140 0 : tokio::fs::remove_file(conf.timeline_delete_mark_file_path(tenant_shard_id, timeline_id))
141 0 : .await
142 0 : .or_else(fs_ext::ignore_not_found)
143 0 : .context("remove delete mark")
144 0 : }
145 :
146 : /// It is important that this gets called when DeletionGuard is being held.
147 : /// For more context see comments in [`DeleteTimelineFlow::prepare`]
148 0 : async fn remove_timeline_from_tenant(
149 0 : tenant: &Tenant,
150 0 : timeline_id: TimelineId,
151 0 : _: &DeletionGuard, // using it as a witness
152 0 : ) -> anyhow::Result<()> {
153 0 : // Remove the timeline from the map.
154 0 : let mut timelines = tenant.timelines.lock().unwrap();
155 0 : let children_exist = timelines
156 0 : .iter()
157 0 : .any(|(_, entry)| entry.get_ancestor_timeline_id() == Some(timeline_id));
158 0 : // XXX this can happen because `branch_timeline` doesn't check `TimelineState::Stopping`.
159 0 : // We already deleted the layer files, so it's probably best to panic.
160 0 : // (Ideally, above remove_dir_all is atomic so we don't see this timeline after a restart)
161 0 : if children_exist {
162 0 : panic!("Timeline grew children while we removed layer files");
163 0 : }
164 0 :
165 0 : timelines
166 0 : .remove(&timeline_id)
167 0 : .expect("timeline that we were deleting was concurrently removed from 'timelines' map");
168 0 :
169 0 : drop(timelines);
170 0 :
171 0 : Ok(())
172 0 : }
173 :
174 : /// Orchestrates timeline shut down of all timeline tasks, removes its in-memory structures,
175 : /// and deletes its data from both disk and s3.
176 : /// The sequence of steps:
177 : /// 1. Set deleted_at in remote index part.
178 : /// 2. Create local mark file.
179 : /// 3. Delete local files except metadata (it is simpler this way, to be able to reuse timeline initialization code that expects metadata)
180 : /// 4. Delete remote layers
181 : /// 5. Delete index part
182 : /// 6. Delete meta, timeline directory
183 : /// 7. Delete mark file
184 : /// It is resumable from any step in case a crash/restart occurs.
185 : /// There are three entrypoints to the process:
186 : /// 1. [`DeleteTimelineFlow::run`] this is the main one called by a management api handler.
187 : /// 2. [`DeleteTimelineFlow::resume_deletion`] is called during restarts when local metadata is still present
188 : /// and we possibly neeed to continue deletion of remote files.
189 : /// 3. [`DeleteTimelineFlow::cleanup_remaining_timeline_fs_traces`] is used when we deleted remote
190 : /// index but still have local metadata, timeline directory and delete mark.
191 : /// Note the only other place that messes around timeline delete mark is the logic that scans directory with timelines during tenant load.
192 : #[derive(Default)]
193 : pub enum DeleteTimelineFlow {
194 : #[default]
195 : NotStarted,
196 : InProgress,
197 : Finished,
198 : }
199 :
200 : impl DeleteTimelineFlow {
201 : // These steps are run in the context of management api request handler.
202 : // Long running steps are continued to run in the background.
203 : // NB: If this fails half-way through, and is retried, the retry will go through
204 : // all the same steps again. Make sure the code here is idempotent, and don't
205 : // error out if some of the shutdown tasks have already been completed!
206 0 : #[instrument(skip_all, fields(%inplace))]
207 : pub async fn run(
208 : tenant: &Arc<Tenant>,
209 : timeline_id: TimelineId,
210 : inplace: bool,
211 : ) -> Result<(), DeleteTimelineError> {
212 : super::debug_assert_current_span_has_tenant_and_timeline_id();
213 :
214 : let (timeline, mut guard) = Self::prepare(tenant, timeline_id)?;
215 :
216 : guard.mark_in_progress()?;
217 :
218 : // Now that the Timeline is in Stopping state, request all the related tasks to shut down.
219 : timeline.shutdown(super::ShutdownMode::Hard).await;
220 :
221 0 : fail::fail_point!("timeline-delete-before-index-deleted-at", |_| {
222 0 : Err(anyhow::anyhow!(
223 0 : "failpoint: timeline-delete-before-index-deleted-at"
224 0 : ))?
225 0 : });
226 :
227 : set_deleted_in_remote_index(&timeline).await?;
228 :
229 0 : fail::fail_point!("timeline-delete-before-schedule", |_| {
230 0 : Err(anyhow::anyhow!(
231 0 : "failpoint: timeline-delete-before-schedule"
232 0 : ))?
233 0 : });
234 :
235 : if inplace {
236 : Self::background(guard, tenant.conf, tenant, &timeline).await?
237 : } else {
238 : Self::schedule_background(guard, tenant.conf, Arc::clone(tenant), timeline);
239 : }
240 :
241 : Ok(())
242 : }
243 :
244 0 : fn mark_in_progress(&mut self) -> anyhow::Result<()> {
245 0 : match self {
246 0 : Self::Finished => anyhow::bail!("Bug. Is in finished state"),
247 0 : Self::InProgress { .. } => { /* We're in a retry */ }
248 0 : Self::NotStarted => { /* Fresh start */ }
249 : }
250 :
251 0 : *self = Self::InProgress;
252 0 :
253 0 : Ok(())
254 0 : }
255 :
256 : /// Shortcut to create Timeline in stopping state and spawn deletion task.
257 : /// See corresponding parts of [`crate::tenant::delete::DeleteTenantFlow`]
258 0 : #[instrument(skip_all, fields(%timeline_id))]
259 : pub async fn resume_deletion(
260 : tenant: Arc<Tenant>,
261 : timeline_id: TimelineId,
262 : local_metadata: &TimelineMetadata,
263 : remote_client: Option<RemoteTimelineClient>,
264 : deletion_queue_client: DeletionQueueClient,
265 : ) -> anyhow::Result<()> {
266 : // Note: here we even skip populating layer map. Timeline is essentially uninitialized.
267 : // RemoteTimelineClient is the only functioning part.
268 : let timeline = tenant
269 : .create_timeline_struct(
270 : timeline_id,
271 : local_metadata,
272 : None, // Ancestor is not needed for deletion.
273 : TimelineResources {
274 : remote_client,
275 : deletion_queue_client,
276 : timeline_get_throttle: tenant.timeline_get_throttle.clone(),
277 : },
278 : // Important. We dont pass ancestor above because it can be missing.
279 : // Thus we need to skip the validation here.
280 : CreateTimelineCause::Delete,
281 : )
282 : .context("create_timeline_struct")?;
283 :
284 : let mut guard = DeletionGuard(
285 : Arc::clone(&timeline.delete_progress)
286 : .try_lock_owned()
287 : .expect("cannot happen because we're the only owner"),
288 : );
289 :
290 : // We meed to do this because when console retries delete request we shouldnt answer with 404
291 : // because 404 means successful deletion.
292 : {
293 : let mut locked = tenant.timelines.lock().unwrap();
294 : locked.insert(timeline_id, Arc::clone(&timeline));
295 : }
296 :
297 : guard.mark_in_progress()?;
298 :
299 : Self::schedule_background(guard, tenant.conf, tenant, timeline);
300 :
301 : Ok(())
302 : }
303 :
304 0 : #[instrument(skip_all, fields(%timeline_id))]
305 : pub async fn cleanup_remaining_timeline_fs_traces(
306 : tenant: &Tenant,
307 : timeline_id: TimelineId,
308 : ) -> anyhow::Result<()> {
309 : let r =
310 : cleanup_remaining_timeline_fs_traces(tenant.conf, tenant.tenant_shard_id, timeline_id)
311 : .await;
312 : info!("Done");
313 : r
314 : }
315 :
316 0 : fn prepare(
317 0 : tenant: &Tenant,
318 0 : timeline_id: TimelineId,
319 0 : ) -> Result<(Arc<Timeline>, DeletionGuard), DeleteTimelineError> {
320 0 : // Note the interaction between this guard and deletion guard.
321 0 : // Here we attempt to lock deletion guard when we're holding a lock on timelines.
322 0 : // This is important because when you take into account `remove_timeline_from_tenant`
323 0 : // we remove timeline from memory when we still hold the deletion guard.
324 0 : // So here when timeline deletion is finished timeline wont be present in timelines map at all
325 0 : // which makes the following sequence impossible:
326 0 : // T1: get preempted right before the try_lock on `Timeline::delete_progress`
327 0 : // T2: do a full deletion, acquire and drop `Timeline::delete_progress`
328 0 : // T1: acquire deletion lock, do another `DeleteTimelineFlow::run`
329 0 : // For more context see this discussion: `https://github.com/neondatabase/neon/pull/4552#discussion_r1253437346`
330 0 : let timelines = tenant.timelines.lock().unwrap();
331 :
332 0 : let timeline = match timelines.get(&timeline_id) {
333 0 : Some(t) => t,
334 0 : None => return Err(DeleteTimelineError::NotFound),
335 : };
336 :
337 : // Ensure that there are no child timelines **attached to that pageserver**,
338 : // because detach removes files, which will break child branches
339 0 : let children: Vec<TimelineId> = timelines
340 0 : .iter()
341 0 : .filter_map(|(id, entry)| {
342 0 : if entry.get_ancestor_timeline_id() == Some(timeline_id) {
343 0 : Some(*id)
344 : } else {
345 0 : None
346 : }
347 0 : })
348 0 : .collect();
349 0 :
350 0 : if !children.is_empty() {
351 0 : return Err(DeleteTimelineError::HasChildren(children));
352 0 : }
353 0 :
354 0 : // Note that using try_lock here is important to avoid a deadlock.
355 0 : // Here we take lock on timelines and then the deletion guard.
356 0 : // At the end of the operation we're holding the guard and need to lock timelines map
357 0 : // to remove the timeline from it.
358 0 : // Always if you have two locks that are taken in different order this can result in a deadlock.
359 0 :
360 0 : let delete_progress = Arc::clone(&timeline.delete_progress);
361 0 : let delete_lock_guard = match delete_progress.try_lock_owned() {
362 0 : Ok(guard) => DeletionGuard(guard),
363 : Err(_) => {
364 : // Unfortunately if lock fails arc is consumed.
365 0 : return Err(DeleteTimelineError::AlreadyInProgress(Arc::clone(
366 0 : &timeline.delete_progress,
367 0 : )));
368 : }
369 : };
370 :
371 0 : timeline.set_state(TimelineState::Stopping);
372 0 :
373 0 : Ok((Arc::clone(timeline), delete_lock_guard))
374 0 : }
375 :
376 0 : fn schedule_background(
377 0 : guard: DeletionGuard,
378 0 : conf: &'static PageServerConf,
379 0 : tenant: Arc<Tenant>,
380 0 : timeline: Arc<Timeline>,
381 0 : ) {
382 0 : let tenant_shard_id = timeline.tenant_shard_id;
383 0 : let timeline_id = timeline.timeline_id;
384 0 :
385 0 : task_mgr::spawn(
386 0 : task_mgr::BACKGROUND_RUNTIME.handle(),
387 0 : TaskKind::TimelineDeletionWorker,
388 0 : Some(tenant_shard_id),
389 0 : Some(timeline_id),
390 0 : "timeline_delete",
391 : false,
392 0 : async move {
393 0 : if let Err(err) = Self::background(guard, conf, &tenant, &timeline).await {
394 0 : error!("Error: {err:#}");
395 0 : timeline.set_broken(format!("{err:#}"))
396 0 : };
397 0 : Ok(())
398 0 : }
399 0 : .instrument(tracing::info_span!(parent: None, "delete_timeline", tenant_id=%tenant_shard_id.tenant_id, shard_id=%tenant_shard_id.shard_slug(),timeline_id=%timeline_id)),
400 : );
401 0 : }
402 :
403 0 : async fn background(
404 0 : mut guard: DeletionGuard,
405 0 : conf: &PageServerConf,
406 0 : tenant: &Tenant,
407 0 : timeline: &Timeline,
408 0 : ) -> Result<(), DeleteTimelineError> {
409 0 : delete_local_timeline_directory(conf, tenant.tenant_shard_id, timeline).await?;
410 :
411 0 : delete_remote_layers_and_index(timeline).await?;
412 :
413 : pausable_failpoint!("in_progress_delete");
414 :
415 0 : remove_timeline_from_tenant(tenant, timeline.timeline_id, &guard).await?;
416 :
417 0 : *guard = Self::Finished;
418 0 :
419 0 : Ok(())
420 0 : }
421 :
422 0 : pub(crate) fn is_finished(&self) -> bool {
423 0 : matches!(self, Self::Finished)
424 0 : }
425 :
426 0 : pub(crate) fn is_not_started(&self) -> bool {
427 0 : matches!(self, Self::NotStarted)
428 0 : }
429 : }
430 :
431 : struct DeletionGuard(OwnedMutexGuard<DeleteTimelineFlow>);
432 :
433 : impl Deref for DeletionGuard {
434 : type Target = DeleteTimelineFlow;
435 :
436 0 : fn deref(&self) -> &Self::Target {
437 0 : &self.0
438 0 : }
439 : }
440 :
441 : impl DerefMut for DeletionGuard {
442 0 : fn deref_mut(&mut self) -> &mut Self::Target {
443 0 : &mut self.0
444 0 : }
445 : }
|