Line data Source code
1 : use std::collections::{HashMap, HashSet};
2 :
3 : use crate::checks::{
4 : branch_cleanup_and_check_errors, list_timeline_blobs, BlobDataParseResult, S3TimelineBlobData,
5 : TenantObjectListing, TimelineAnalysis,
6 : };
7 : use crate::metadata_stream::{stream_tenant_timelines, stream_tenants};
8 : use crate::{init_remote, BucketConfig, NodeKind, RootTarget, TenantShardTimelineId};
9 : use aws_sdk_s3::Client;
10 : use futures_util::{StreamExt, TryStreamExt};
11 : use pageserver::tenant::remote_timeline_client::remote_layer_path;
12 : use pageserver_api::controller_api::MetadataHealthUpdateRequest;
13 : use pageserver_api::shard::TenantShardId;
14 : use serde::Serialize;
15 : use utils::id::TenantId;
16 : use utils::shard::ShardCount;
17 :
18 : #[derive(Serialize, Default)]
19 : pub struct MetadataSummary {
20 : tenant_count: usize,
21 : timeline_count: usize,
22 : timeline_shard_count: usize,
23 : with_errors: HashSet<TenantShardTimelineId>,
24 : with_warnings: HashSet<TenantShardTimelineId>,
25 : with_orphans: HashSet<TenantShardTimelineId>,
26 : indices_by_version: HashMap<usize, usize>,
27 :
28 : #[serde(skip)]
29 : pub(crate) healthy_tenant_shards: HashSet<TenantShardId>,
30 : #[serde(skip)]
31 : pub(crate) unhealthy_tenant_shards: HashSet<TenantShardId>,
32 : }
33 :
34 : impl MetadataSummary {
35 0 : fn new() -> Self {
36 0 : Self::default()
37 0 : }
38 :
39 0 : fn update_data(&mut self, data: &S3TimelineBlobData) {
40 0 : self.timeline_shard_count += 1;
41 : if let BlobDataParseResult::Parsed {
42 0 : index_part,
43 : index_part_generation: _,
44 : s3_layers: _,
45 0 : } = &data.blob_data
46 0 : {
47 0 : *self
48 0 : .indices_by_version
49 0 : .entry(index_part.version())
50 0 : .or_insert(0) += 1;
51 0 : }
52 0 : }
53 :
54 0 : fn update_analysis(&mut self, id: &TenantShardTimelineId, analysis: &TimelineAnalysis) {
55 0 : if analysis.is_healthy() {
56 0 : self.healthy_tenant_shards.insert(id.tenant_shard_id);
57 0 : } else {
58 0 : self.healthy_tenant_shards.remove(&id.tenant_shard_id);
59 0 : self.unhealthy_tenant_shards.insert(id.tenant_shard_id);
60 0 : }
61 :
62 0 : if !analysis.errors.is_empty() {
63 0 : self.with_errors.insert(*id);
64 0 : }
65 :
66 0 : if !analysis.warnings.is_empty() {
67 0 : self.with_warnings.insert(*id);
68 0 : }
69 0 : }
70 :
71 0 : fn notify_timeline_orphan(&mut self, ttid: &TenantShardTimelineId) {
72 0 : self.with_orphans.insert(*ttid);
73 0 : }
74 :
75 : /// Long-form output for printing at end of a scan
76 0 : pub fn summary_string(&self) -> String {
77 0 : let version_summary: String = itertools::join(
78 0 : self.indices_by_version
79 0 : .iter()
80 0 : .map(|(k, v)| format!("{k}: {v}")),
81 0 : ", ",
82 0 : );
83 0 :
84 0 : format!(
85 0 : "Tenants: {}
86 0 : Timelines: {}
87 0 : Timeline-shards: {}
88 0 : With errors: {}
89 0 : With warnings: {}
90 0 : With orphan layers: {}
91 0 : Index versions: {version_summary}
92 0 : ",
93 0 : self.tenant_count,
94 0 : self.timeline_count,
95 0 : self.timeline_shard_count,
96 0 : self.with_errors.len(),
97 0 : self.with_warnings.len(),
98 0 : self.with_orphans.len(),
99 0 : )
100 0 : }
101 :
102 0 : pub fn is_fatal(&self) -> bool {
103 0 : !self.with_errors.is_empty()
104 0 : }
105 :
106 0 : pub fn is_empty(&self) -> bool {
107 0 : self.timeline_shard_count == 0
108 0 : }
109 :
110 0 : pub fn build_health_update_request(&self) -> MetadataHealthUpdateRequest {
111 0 : MetadataHealthUpdateRequest {
112 0 : healthy_tenant_shards: self.healthy_tenant_shards.clone(),
113 0 : unhealthy_tenant_shards: self.unhealthy_tenant_shards.clone(),
114 0 : }
115 0 : }
116 : }
117 :
118 : /// Scan the pageserver metadata in an S3 bucket, reporting errors and statistics.
119 0 : pub async fn scan_metadata(
120 0 : bucket_config: BucketConfig,
121 0 : tenant_ids: Vec<TenantShardId>,
122 0 : ) -> anyhow::Result<MetadataSummary> {
123 0 : let (s3_client, target) = init_remote(bucket_config, NodeKind::Pageserver).await?;
124 :
125 0 : let tenants = if tenant_ids.is_empty() {
126 0 : futures::future::Either::Left(stream_tenants(&s3_client, &target))
127 : } else {
128 0 : futures::future::Either::Right(futures::stream::iter(tenant_ids.into_iter().map(Ok)))
129 : };
130 :
131 : // How many tenants to process in parallel. We need to be mindful of pageservers
132 : // accessing the same per tenant prefixes, so use a lower setting than pageservers.
133 : const CONCURRENCY: usize = 32;
134 :
135 : // Generate a stream of TenantTimelineId
136 0 : let timelines = tenants.map_ok(|t| stream_tenant_timelines(&s3_client, &target, t));
137 0 : let timelines = timelines.try_buffered(CONCURRENCY);
138 0 : let timelines = timelines.try_flatten();
139 0 :
140 0 : // Generate a stream of S3TimelineBlobData
141 0 : async fn report_on_timeline(
142 0 : s3_client: &Client,
143 0 : target: &RootTarget,
144 0 : ttid: TenantShardTimelineId,
145 0 : ) -> anyhow::Result<(TenantShardTimelineId, S3TimelineBlobData)> {
146 0 : let data = list_timeline_blobs(s3_client, ttid, target).await?;
147 0 : Ok((ttid, data))
148 0 : }
149 0 : let timelines = timelines.map_ok(|ttid| report_on_timeline(&s3_client, &target, ttid));
150 0 : let mut timelines = std::pin::pin!(timelines.try_buffered(CONCURRENCY));
151 0 :
152 0 : // We must gather all the TenantShardTimelineId->S3TimelineBlobData for each tenant, because different
153 0 : // shards in the same tenant might refer to one anothers' keys if a shard split has happened.
154 0 :
155 0 : let mut tenant_id = None;
156 0 : let mut tenant_objects = TenantObjectListing::default();
157 0 : let mut tenant_timeline_results = Vec::new();
158 0 :
159 0 : async fn analyze_tenant(
160 0 : s3_client: &Client,
161 0 : target: &RootTarget,
162 0 : tenant_id: TenantId,
163 0 : summary: &mut MetadataSummary,
164 0 : mut tenant_objects: TenantObjectListing,
165 0 : timelines: Vec<(TenantShardTimelineId, S3TimelineBlobData)>,
166 0 : highest_shard_count: ShardCount,
167 0 : ) {
168 0 : summary.tenant_count += 1;
169 0 :
170 0 : let mut timeline_ids = HashSet::new();
171 0 : let mut timeline_generations = HashMap::new();
172 0 : for (ttid, data) in timelines {
173 0 : if ttid.tenant_shard_id.shard_count == highest_shard_count {
174 0 : // Only analyze `TenantShardId`s with highest shard count.
175 0 :
176 0 : // Stash the generation of each timeline, for later use identifying orphan layers
177 0 : if let BlobDataParseResult::Parsed {
178 0 : index_part,
179 0 : index_part_generation,
180 0 : s3_layers: _s3_layers,
181 0 : } = &data.blob_data
182 0 : {
183 0 : if index_part.deleted_at.is_some() {
184 0 : // skip deleted timeline.
185 0 : tracing::info!("Skip analysis of {} b/c timeline is already deleted", ttid);
186 0 : continue;
187 0 : }
188 0 : timeline_generations.insert(ttid, *index_part_generation);
189 0 : }
190 0 :
191 0 : // Apply checks to this timeline shard's metadata, and in the process update `tenant_objects`
192 0 : // reference counts for layers across the tenant.
193 0 : let analysis = branch_cleanup_and_check_errors(
194 0 : s3_client,
195 0 : target,
196 0 : &ttid,
197 0 : &mut tenant_objects,
198 0 : None,
199 0 : None,
200 0 : Some(data),
201 0 : )
202 0 : .await;
203 0 : summary.update_analysis(&ttid, &analysis);
204 0 :
205 0 : timeline_ids.insert(ttid.timeline_id);
206 0 : } else {
207 0 : tracing::info!(
208 0 : "Skip analysis of {} b/c a lower shard count than {}",
209 0 : ttid,
210 0 : highest_shard_count.0,
211 0 : );
212 0 : }
213 0 : }
214 0 :
215 0 : summary.timeline_count += timeline_ids.len();
216 0 :
217 0 : // Identifying orphan layers must be done on a tenant-wide basis, because individual
218 0 : // shards' layers may be referenced by other shards.
219 0 : //
220 0 : // Orphan layers are not a corruption, and not an indication of a problem. They are just
221 0 : // consuming some space in remote storage, and may be cleaned up at leisure.
222 0 : for (shard_index, timeline_id, layer_file, generation) in tenant_objects.get_orphans() {
223 0 : let ttid = TenantShardTimelineId {
224 0 : tenant_shard_id: TenantShardId {
225 0 : tenant_id,
226 0 : shard_count: shard_index.shard_count,
227 0 : shard_number: shard_index.shard_number,
228 0 : },
229 0 : timeline_id,
230 0 : };
231 0 :
232 0 : if let Some(timeline_generation) = timeline_generations.get(&ttid) {
233 0 : if &generation >= timeline_generation {
234 0 : // Candidate orphan layer is in the current or future generation relative
235 0 : // to the index we read for this timeline shard, so its absence from the index
236 0 : // doesn't make it an orphan: more likely, it is a case where the layer was
237 0 : // uploaded, but the index referencing the layer wasn't written yet.
238 0 : continue;
239 0 : }
240 0 : }
241 0 :
242 0 : let orphan_path = remote_layer_path(
243 0 : &tenant_id,
244 0 : &timeline_id,
245 0 : shard_index,
246 0 : &layer_file,
247 0 : generation,
248 0 : );
249 0 :
250 0 : tracing::info!("Orphan layer detected: {orphan_path}");
251 0 :
252 0 : summary.notify_timeline_orphan(&ttid);
253 0 : }
254 0 : }
255 0 :
256 0 : // Iterate through all the timeline results. These are in key-order, so
257 0 : // all results for the same tenant will be adjacent. We accumulate these,
258 0 : // and then call `analyze_tenant` to flush, when we see the next tenant ID.
259 0 : let mut summary = MetadataSummary::new();
260 0 : let mut highest_shard_count = ShardCount::MIN;
261 0 : while let Some(i) = timelines.next().await {
262 0 : let (ttid, data) = i?;
263 0 : summary.update_data(&data);
264 0 :
265 0 : match tenant_id {
266 0 : None => {
267 0 : tenant_id = Some(ttid.tenant_shard_id.tenant_id);
268 0 : highest_shard_count = highest_shard_count.max(ttid.tenant_shard_id.shard_count);
269 0 : }
270 0 : Some(prev_tenant_id) => {
271 0 : if prev_tenant_id != ttid.tenant_shard_id.tenant_id {
272 : // New tenant: analyze this tenant's timelines, clear accumulated tenant_timeline_results
273 0 : let tenant_objects = std::mem::take(&mut tenant_objects);
274 0 : let timelines = std::mem::take(&mut tenant_timeline_results);
275 0 : analyze_tenant(
276 0 : &s3_client,
277 0 : &target,
278 0 : prev_tenant_id,
279 0 : &mut summary,
280 0 : tenant_objects,
281 0 : timelines,
282 0 : highest_shard_count,
283 0 : )
284 0 : .await;
285 0 : tenant_id = Some(ttid.tenant_shard_id.tenant_id);
286 0 : highest_shard_count = ttid.tenant_shard_id.shard_count;
287 0 : } else {
288 0 : highest_shard_count = highest_shard_count.max(ttid.tenant_shard_id.shard_count);
289 0 : }
290 : }
291 : }
292 :
293 : if let BlobDataParseResult::Parsed {
294 0 : index_part: _index_part,
295 0 : index_part_generation: _index_part_generation,
296 0 : s3_layers,
297 0 : } = &data.blob_data
298 0 : {
299 0 : tenant_objects.push(ttid, s3_layers.clone());
300 0 : }
301 0 : tenant_timeline_results.push((ttid, data));
302 : }
303 :
304 0 : if !tenant_timeline_results.is_empty() {
305 0 : analyze_tenant(
306 0 : &s3_client,
307 0 : &target,
308 0 : tenant_id.expect("Must be set if results are present"),
309 0 : &mut summary,
310 0 : tenant_objects,
311 0 : tenant_timeline_results,
312 0 : highest_shard_count,
313 0 : )
314 0 : .await;
315 0 : }
316 :
317 0 : Ok(summary)
318 0 : }
|