Line data Source code
1 : use std::collections::hash_map::Entry;
2 : use std::fs;
3 : use std::future::Future;
4 : use std::sync::Arc;
5 :
6 : use anyhow::Context;
7 : use camino::Utf8PathBuf;
8 : use tracing::{error, info, info_span};
9 : use utils::fs_ext;
10 : use utils::id::TimelineId;
11 : use utils::lsn::Lsn;
12 : use utils::sync::gate::GateGuard;
13 :
14 : use super::Timeline;
15 : use crate::context::RequestContext;
16 : use crate::import_datadir;
17 : use crate::span::debug_assert_current_span_has_tenant_and_timeline_id;
18 : use crate::tenant::{
19 : CreateTimelineError, CreateTimelineIdempotency, TenantShard, TimelineOrOffloaded,
20 : };
21 :
22 : /// A timeline with some of its files on disk, being initialized.
23 : /// This struct ensures the atomicity of the timeline init: it's either properly created and inserted into pageserver's memory, or
24 : /// its local files are removed. If we crash while this class exists, then the timeline's local
25 : /// state is cleaned up during [`TenantShard::clean_up_timelines`], because the timeline's content isn't in remote storage.
26 : ///
27 : /// The caller is responsible for proper timeline data filling before the final init.
28 : #[must_use]
29 : pub struct UninitializedTimeline<'t> {
30 : pub(crate) owning_tenant: &'t TenantShard,
31 : timeline_id: TimelineId,
32 : raw_timeline: Option<(Arc<Timeline>, TimelineCreateGuard)>,
33 : /// Whether we spawned the inner Timeline's tasks such that we must later shut it down
34 : /// if aborting the timeline creation
35 : needs_shutdown: bool,
36 : }
37 :
38 : impl<'t> UninitializedTimeline<'t> {
39 232 : pub(crate) fn new(
40 232 : owning_tenant: &'t TenantShard,
41 232 : timeline_id: TimelineId,
42 232 : raw_timeline: Option<(Arc<Timeline>, TimelineCreateGuard)>,
43 232 : ) -> Self {
44 232 : Self {
45 232 : owning_tenant,
46 232 : timeline_id,
47 232 : raw_timeline,
48 232 : needs_shutdown: false,
49 232 : }
50 232 : }
51 :
52 : /// When writing data to this timeline during creation, use this wrapper: it will take care of
53 : /// setup of Timeline tasks required for I/O (flush loop) and making sure they are torn down
54 : /// later.
55 1 : pub(crate) async fn write<F, Fut>(&mut self, f: F) -> anyhow::Result<()>
56 1 : where
57 1 : F: FnOnce(Arc<Timeline>) -> Fut,
58 1 : Fut: Future<Output = Result<(), CreateTimelineError>>,
59 1 : {
60 1 : debug_assert_current_span_has_tenant_and_timeline_id();
61 :
62 : // Remember that we did I/O (spawned the flush loop), so that we can check we shut it down on drop
63 1 : self.needs_shutdown = true;
64 :
65 1 : let timeline = self.raw_timeline()?;
66 :
67 : // Spawn flush loop so that the Timeline is ready to accept writes
68 1 : timeline.maybe_spawn_flush_loop();
69 :
70 : // Invoke the provided function, which will write some data into the new timeline
71 1 : if let Err(e) = f(timeline.clone()).await {
72 0 : self.abort().await;
73 0 : return Err(e.into());
74 1 : }
75 :
76 : // Flush the underlying timeline's ephemeral layers to disk
77 1 : if let Err(e) = timeline
78 1 : .freeze_and_flush()
79 1 : .await
80 1 : .context("Failed to flush after timeline creation writes")
81 : {
82 0 : self.abort().await;
83 0 : return Err(e);
84 1 : }
85 :
86 1 : Ok(())
87 1 : }
88 :
89 0 : pub(crate) async fn abort(&self) {
90 0 : if let Some((raw_timeline, _)) = self.raw_timeline.as_ref() {
91 0 : raw_timeline.shutdown(super::ShutdownMode::Hard).await;
92 0 : }
93 0 : }
94 :
95 : /// Finish timeline creation: insert it into the Tenant's timelines map
96 : ///
97 : /// This function launches the flush loop if not already done.
98 : ///
99 : /// The caller is responsible for activating the timeline (function `.activate()`).
100 228 : pub(crate) async fn finish_creation(mut self) -> anyhow::Result<Arc<Timeline>> {
101 228 : let timeline_id = self.timeline_id;
102 228 : let tenant_shard_id = self.owning_tenant.tenant_shard_id;
103 :
104 228 : if self.raw_timeline.is_none() {
105 0 : self.abort().await;
106 :
107 0 : return Err(anyhow::anyhow!(
108 0 : "No timeline for initialization found for {tenant_shard_id}/{timeline_id}"
109 0 : ));
110 228 : }
111 :
112 : // Check that the caller initialized disk_consistent_lsn
113 228 : let new_disk_consistent_lsn = self
114 228 : .raw_timeline
115 228 : .as_ref()
116 228 : .expect("checked above")
117 228 : .0
118 228 : .get_disk_consistent_lsn();
119 :
120 228 : if !new_disk_consistent_lsn.is_valid() {
121 0 : self.abort().await;
122 :
123 0 : return Err(anyhow::anyhow!(
124 0 : "new timeline {tenant_shard_id}/{timeline_id} has invalid disk_consistent_lsn"
125 0 : ));
126 228 : }
127 :
128 228 : let mut timelines = self.owning_tenant.timelines.lock().unwrap();
129 228 : match timelines.entry(timeline_id) {
130 : Entry::Occupied(_) => {
131 : // Unexpected, bug in the caller. Tenant is responsible for preventing concurrent creation of the same timeline.
132 : //
133 : // We do not call Self::abort here. Because we don't cleanly shut down our Timeline, [`Self::drop`] should
134 : // skip trying to delete the timeline directory too.
135 0 : anyhow::bail!(
136 0 : "Found freshly initialized timeline {tenant_shard_id}/{timeline_id} in the tenant map"
137 : )
138 : }
139 228 : Entry::Vacant(v) => {
140 : // after taking here should be no fallible operations, because the drop guard will not
141 : // cleanup after and would block for example the tenant deletion
142 228 : let (new_timeline, _create_guard) =
143 228 : self.raw_timeline.take().expect("already checked");
144 :
145 228 : v.insert(Arc::clone(&new_timeline));
146 :
147 228 : new_timeline.maybe_spawn_flush_loop();
148 :
149 228 : Ok(new_timeline)
150 : }
151 : }
152 228 : }
153 :
154 0 : pub(crate) fn finish_creation_myself(&mut self) -> (Arc<Timeline>, TimelineCreateGuard) {
155 0 : self.raw_timeline.take().expect("already checked")
156 0 : }
157 :
158 : /// Prepares timeline data by loading it from the basebackup archive.
159 0 : pub(crate) async fn import_basebackup_from_tar(
160 0 : mut self,
161 0 : tenant: Arc<TenantShard>,
162 0 : copyin_read: &mut (impl tokio::io::AsyncRead + Send + Sync + Unpin),
163 0 : base_lsn: Lsn,
164 0 : broker_client: storage_broker::BrokerClientChannel,
165 0 : ctx: &RequestContext,
166 0 : ) -> anyhow::Result<Arc<Timeline>> {
167 0 : self.write(|raw_timeline| async move {
168 0 : import_datadir::import_basebackup_from_tar(&raw_timeline, copyin_read, base_lsn, ctx)
169 0 : .await
170 0 : .context("Failed to import basebackup")
171 0 : .map_err(CreateTimelineError::Other)?;
172 :
173 0 : fail::fail_point!("before-checkpoint-new-timeline", |_| {
174 0 : Err(CreateTimelineError::Other(anyhow::anyhow!(
175 0 : "failpoint before-checkpoint-new-timeline"
176 0 : )))
177 0 : });
178 :
179 0 : Ok(())
180 0 : })
181 0 : .await?;
182 :
183 : // All the data has been imported. Insert the Timeline into the tenant's timelines map
184 0 : let tl = self.finish_creation().await?;
185 0 : tl.activate(tenant, broker_client, None, ctx);
186 0 : Ok(tl)
187 0 : }
188 :
189 115 : pub(crate) fn raw_timeline(&self) -> anyhow::Result<&Arc<Timeline>> {
190 115 : Ok(&self
191 115 : .raw_timeline
192 115 : .as_ref()
193 115 : .with_context(|| {
194 0 : format!(
195 0 : "No raw timeline {}/{} found",
196 : self.owning_tenant.tenant_shard_id, self.timeline_id
197 : )
198 0 : })?
199 : .0)
200 115 : }
201 : }
202 :
203 : impl Drop for UninitializedTimeline<'_> {
204 231 : fn drop(&mut self) {
205 231 : if let Some((timeline, create_guard)) = self.raw_timeline.take() {
206 3 : let _entered = info_span!("drop_uninitialized_timeline", tenant_id = %self.owning_tenant.tenant_shard_id.tenant_id, shard_id = %self.owning_tenant.tenant_shard_id.shard_slug(), timeline_id = %self.timeline_id).entered();
207 3 : if self.needs_shutdown && !timeline.gate.close_complete() {
208 : // This should not happen: caller should call [`Self::abort`] on failures
209 0 : tracing::warn!(
210 0 : "Timeline not shut down after initialization failure, cannot clean up files"
211 : );
212 : } else {
213 : // This is unusual, but can happen harmlessly if the pageserver is stopped while
214 : // creating a timeline.
215 3 : info!("Timeline got dropped without initializing, cleaning its files");
216 3 : cleanup_timeline_directory(create_guard);
217 : }
218 228 : }
219 231 : }
220 : }
221 :
222 3 : pub(crate) fn cleanup_timeline_directory(create_guard: TimelineCreateGuard) {
223 3 : let timeline_path = &create_guard.timeline_path;
224 3 : match fs_ext::ignore_absent_files(|| fs::remove_dir_all(timeline_path)) {
225 : Ok(()) => {
226 3 : info!("Timeline dir {timeline_path:?} removed successfully")
227 : }
228 0 : Err(e) => {
229 0 : error!("Failed to clean up uninitialized timeline directory {timeline_path:?}: {e:?}")
230 : }
231 : }
232 : // Having cleaned up, we can release this TimelineId in `[TenantShard::timelines_creating]` to allow other
233 : // timeline creation attempts under this TimelineId to proceed
234 3 : drop(create_guard);
235 3 : }
236 :
237 : /// A guard for timeline creations in process: as long as this object exists, the timeline ID
238 : /// is kept in `[TenantShard::timelines_creating]` to exclude concurrent attempts to create the same timeline.
239 : #[must_use]
240 : pub(crate) struct TimelineCreateGuard {
241 : pub(crate) _tenant_gate_guard: GateGuard,
242 : pub(crate) owning_tenant: Arc<TenantShard>,
243 : pub(crate) timeline_id: TimelineId,
244 : pub(crate) timeline_path: Utf8PathBuf,
245 : pub(crate) idempotency: CreateTimelineIdempotency,
246 : }
247 :
248 : /// Errors when acquiring exclusive access to a timeline ID for creation
249 : #[derive(thiserror::Error, Debug)]
250 : pub(crate) enum TimelineExclusionError {
251 : #[error("Already exists")]
252 : AlreadyExists {
253 : existing: TimelineOrOffloaded,
254 : arg: CreateTimelineIdempotency,
255 : },
256 : #[error("Already creating")]
257 : AlreadyCreating,
258 : #[error("Shutting down")]
259 : ShuttingDown,
260 :
261 : // e.g. I/O errors, or some failure deep in postgres initdb
262 : #[error(transparent)]
263 : Other(#[from] anyhow::Error),
264 : }
265 :
266 : impl TimelineCreateGuard {
267 235 : pub(crate) fn new(
268 235 : owning_tenant: &Arc<TenantShard>,
269 235 : timeline_id: TimelineId,
270 235 : timeline_path: Utf8PathBuf,
271 235 : idempotency: CreateTimelineIdempotency,
272 235 : allow_offloaded: bool,
273 235 : ) -> Result<Self, TimelineExclusionError> {
274 235 : let _tenant_gate_guard = owning_tenant
275 235 : .gate
276 235 : .enter()
277 235 : .map_err(|_| TimelineExclusionError::ShuttingDown)?;
278 :
279 : // Lock order: this is the only place we take both locks. During drop() we only
280 : // lock creating_timelines
281 235 : let timelines = owning_tenant.timelines.lock().unwrap();
282 235 : let timelines_offloaded = owning_tenant.timelines_offloaded.lock().unwrap();
283 235 : let mut creating_timelines: std::sync::MutexGuard<
284 235 : '_,
285 235 : std::collections::HashSet<TimelineId>,
286 235 : > = owning_tenant.timelines_creating.lock().unwrap();
287 :
288 235 : if let Some(existing) = timelines.get(&timeline_id) {
289 1 : return Err(TimelineExclusionError::AlreadyExists {
290 1 : existing: TimelineOrOffloaded::Timeline(existing.clone()),
291 1 : arg: idempotency,
292 1 : });
293 234 : }
294 234 : if !allow_offloaded {
295 234 : if let Some(existing) = timelines_offloaded.get(&timeline_id) {
296 0 : return Err(TimelineExclusionError::AlreadyExists {
297 0 : existing: TimelineOrOffloaded::Offloaded(existing.clone()),
298 0 : arg: idempotency,
299 0 : });
300 234 : }
301 0 : }
302 234 : if creating_timelines.contains(&timeline_id) {
303 0 : return Err(TimelineExclusionError::AlreadyCreating);
304 234 : }
305 234 : creating_timelines.insert(timeline_id);
306 234 : drop(creating_timelines);
307 234 : drop(timelines_offloaded);
308 234 : drop(timelines);
309 234 : Ok(Self {
310 234 : _tenant_gate_guard,
311 234 : owning_tenant: Arc::clone(owning_tenant),
312 234 : timeline_id,
313 234 : timeline_path,
314 234 : idempotency,
315 234 : })
316 235 : }
317 : }
318 :
319 : impl Drop for TimelineCreateGuard {
320 233 : fn drop(&mut self) {
321 233 : self.owning_tenant
322 233 : .timelines_creating
323 233 : .lock()
324 233 : .unwrap()
325 233 : .remove(&self.timeline_id);
326 233 : }
327 : }
|