Line data Source code
1 : use std::fs::{self, File};
2 : use std::path::{Path, PathBuf};
3 :
4 : use anyhow::Result;
5 : use camino::{Utf8Path, Utf8PathBuf};
6 : use clap::Subcommand;
7 : use pageserver::context::{DownloadBehavior, RequestContext};
8 : use pageserver::task_mgr::TaskKind;
9 : use pageserver::tenant::storage_layer::{DeltaLayer, ImageLayer, delta_layer, image_layer};
10 : use pageserver::tenant::{TENANTS_SEGMENT_NAME, TIMELINES_SEGMENT_NAME};
11 : use pageserver::virtual_file::api::IoMode;
12 : use pageserver::{page_cache, virtual_file};
13 : use utils::id::{TenantId, TimelineId};
14 :
15 : use crate::layer_map_analyzer::parse_filename;
16 :
17 : #[derive(Subcommand)]
18 : pub(crate) enum LayerCmd {
19 : /// List all tenants and timelines under the pageserver path
20 : ///
21 : /// Example: `cargo run --bin pagectl layer list .neon/`
22 0 : List { path: PathBuf },
23 : /// List all layers of a given tenant and timeline
24 : ///
25 : /// Example: `cargo run --bin pagectl layer list .neon/`
26 : ListLayer {
27 0 : path: PathBuf,
28 0 : tenant: String,
29 0 : timeline: String,
30 : },
31 : /// Dump all information of a layer file
32 : DumpLayer {
33 0 : path: PathBuf,
34 0 : tenant: String,
35 0 : timeline: String,
36 : /// The id from list-layer command
37 0 : id: usize,
38 : },
39 : RewriteSummary {
40 0 : layer_file_path: Utf8PathBuf,
41 : #[clap(long)]
42 : new_tenant_id: Option<TenantId>,
43 : #[clap(long)]
44 : new_timeline_id: Option<TimelineId>,
45 : },
46 : }
47 :
48 0 : async fn read_delta_file(path: impl AsRef<Path>, ctx: &RequestContext) -> Result<()> {
49 0 : virtual_file::init(
50 0 : 10,
51 0 : virtual_file::api::IoEngineKind::StdFs,
52 0 : IoMode::preferred(),
53 0 : virtual_file::SyncMode::Sync,
54 0 : );
55 0 : page_cache::init(100);
56 0 : let path = Utf8Path::from_path(path.as_ref()).expect("non-Unicode path");
57 0 : let file = File::open(path)?;
58 0 : let delta_layer = DeltaLayer::new_for_path(path, file)?;
59 0 : delta_layer.dump(true, ctx).await?;
60 0 : Ok(())
61 0 : }
62 :
63 0 : async fn read_image_file(path: impl AsRef<Path>, ctx: &RequestContext) -> Result<()> {
64 0 : virtual_file::init(
65 0 : 10,
66 0 : virtual_file::api::IoEngineKind::StdFs,
67 0 : IoMode::preferred(),
68 0 : virtual_file::SyncMode::Sync,
69 0 : );
70 0 : page_cache::init(100);
71 0 : let path = Utf8Path::from_path(path.as_ref()).expect("non-Unicode path");
72 0 : let file = File::open(path)?;
73 0 : let image_layer = ImageLayer::new_for_path(path, file)?;
74 0 : image_layer.dump(true, ctx).await?;
75 0 : Ok(())
76 0 : }
77 :
78 0 : pub(crate) async fn main(cmd: &LayerCmd) -> Result<()> {
79 0 : let ctx = RequestContext::new(TaskKind::DebugTool, DownloadBehavior::Error);
80 0 : match cmd {
81 0 : LayerCmd::List { path } => {
82 0 : for tenant in fs::read_dir(path.join(TENANTS_SEGMENT_NAME))? {
83 0 : let tenant = tenant?;
84 0 : if !tenant.file_type()?.is_dir() {
85 0 : continue;
86 0 : }
87 0 : println!("tenant {}", tenant.file_name().to_string_lossy());
88 0 : for timeline in fs::read_dir(tenant.path().join(TIMELINES_SEGMENT_NAME))? {
89 0 : let timeline = timeline?;
90 0 : if !timeline.file_type()?.is_dir() {
91 0 : continue;
92 0 : }
93 0 : println!("- timeline {}", timeline.file_name().to_string_lossy());
94 : }
95 : }
96 0 : Ok(())
97 : }
98 : LayerCmd::ListLayer {
99 0 : path,
100 0 : tenant,
101 0 : timeline,
102 0 : } => {
103 0 : let timeline_path = path
104 0 : .join(TENANTS_SEGMENT_NAME)
105 0 : .join(tenant)
106 0 : .join(TIMELINES_SEGMENT_NAME)
107 0 : .join(timeline);
108 0 : let mut idx = 0;
109 0 : for layer in fs::read_dir(timeline_path)? {
110 0 : let layer = layer?;
111 0 : if let Ok(layer_file) = parse_filename(&layer.file_name().into_string().unwrap()) {
112 0 : println!(
113 0 : "[{:3}] key:{}-{}\n lsn:{}-{}\n delta:{}",
114 0 : idx,
115 0 : layer_file.key_range.start,
116 0 : layer_file.key_range.end,
117 0 : layer_file.lsn_range.start,
118 0 : layer_file.lsn_range.end,
119 0 : layer_file.is_delta,
120 0 : );
121 0 : idx += 1;
122 0 : }
123 : }
124 0 : Ok(())
125 : }
126 : LayerCmd::DumpLayer {
127 0 : path,
128 0 : tenant,
129 0 : timeline,
130 0 : id,
131 0 : } => {
132 0 : let timeline_path = path
133 0 : .join("tenants")
134 0 : .join(tenant)
135 0 : .join("timelines")
136 0 : .join(timeline);
137 0 : let mut idx = 0;
138 0 : for layer in fs::read_dir(timeline_path)? {
139 0 : let layer = layer?;
140 0 : if let Ok(layer_file) = parse_filename(&layer.file_name().into_string().unwrap()) {
141 0 : if *id == idx {
142 : // TODO(chi): dedup code
143 0 : println!(
144 0 : "[{:3}] key:{}-{}\n lsn:{}-{}\n delta:{}",
145 0 : idx,
146 0 : layer_file.key_range.start,
147 0 : layer_file.key_range.end,
148 0 : layer_file.lsn_range.start,
149 0 : layer_file.lsn_range.end,
150 0 : layer_file.is_delta,
151 0 : );
152 0 :
153 0 : if layer_file.is_delta {
154 0 : read_delta_file(layer.path(), &ctx).await?;
155 : } else {
156 0 : read_image_file(layer.path(), &ctx).await?;
157 : }
158 :
159 0 : break;
160 0 : }
161 0 : idx += 1;
162 0 : }
163 : }
164 0 : Ok(())
165 : }
166 : LayerCmd::RewriteSummary {
167 0 : layer_file_path,
168 0 : new_tenant_id,
169 0 : new_timeline_id,
170 0 : } => {
171 0 : pageserver::virtual_file::init(
172 0 : 10,
173 0 : virtual_file::api::IoEngineKind::StdFs,
174 0 : IoMode::preferred(),
175 0 : virtual_file::SyncMode::Sync,
176 0 : );
177 0 : pageserver::page_cache::init(100);
178 0 :
179 0 : let ctx = RequestContext::new(TaskKind::DebugTool, DownloadBehavior::Error);
180 :
181 : macro_rules! rewrite_closure {
182 : ($($summary_ty:tt)*) => {{
183 : |summary| $($summary_ty)* {
184 : tenant_id: new_tenant_id.unwrap_or(summary.tenant_id),
185 : timeline_id: new_timeline_id.unwrap_or(summary.timeline_id),
186 : ..summary
187 0 : }
188 : }};
189 : }
190 :
191 0 : let res = ImageLayer::rewrite_summary(
192 0 : layer_file_path,
193 0 : rewrite_closure!(image_layer::Summary),
194 0 : &ctx,
195 0 : )
196 0 : .await;
197 0 : match res {
198 : Ok(()) => {
199 0 : println!("Successfully rewrote summary of image layer {layer_file_path}");
200 0 : return Ok(());
201 : }
202 0 : Err(image_layer::RewriteSummaryError::MagicMismatch) => (), // fallthrough
203 0 : Err(image_layer::RewriteSummaryError::Other(e)) => {
204 0 : return Err(e);
205 : }
206 : }
207 :
208 0 : let res = DeltaLayer::rewrite_summary(
209 0 : layer_file_path,
210 0 : rewrite_closure!(delta_layer::Summary),
211 0 : &ctx,
212 0 : )
213 0 : .await;
214 0 : match res {
215 : Ok(()) => {
216 0 : println!("Successfully rewrote summary of delta layer {layer_file_path}");
217 0 : return Ok(());
218 : }
219 0 : Err(delta_layer::RewriteSummaryError::MagicMismatch) => (), // fallthrough
220 0 : Err(delta_layer::RewriteSummaryError::Other(e)) => {
221 0 : return Err(e);
222 : }
223 : }
224 :
225 0 : anyhow::bail!("not an image or delta layer: {layer_file_path}");
226 : }
227 : }
228 0 : }
|