Line data Source code
1 : use std::path::{Path, PathBuf};
2 :
3 : use anyhow::Result;
4 : use clap::Subcommand;
5 : use pageserver::tenant::block_io::BlockCursor;
6 : use pageserver::tenant::disk_btree::DiskBtreeReader;
7 : use pageserver::tenant::storage_layer::delta_layer::{BlobRef, Summary};
8 : use pageserver::tenant::{TENANTS_SEGMENT_NAME, TIMELINES_SEGMENT_NAME};
9 : use pageserver::{page_cache, virtual_file};
10 : use pageserver::{
11 : repository::{Key, KEY_SIZE},
12 : tenant::{
13 : block_io::FileBlockReader, disk_btree::VisitDirection,
14 : storage_layer::delta_layer::DELTA_KEY_SIZE,
15 : },
16 : virtual_file::VirtualFile,
17 : };
18 : use std::fs;
19 : use utils::bin_ser::BeSer;
20 :
21 : use crate::layer_map_analyzer::parse_filename;
22 :
23 0 : #[derive(Subcommand)]
24 : pub(crate) enum LayerCmd {
25 : /// List all tenants and timelines under the pageserver path
26 : ///
27 : /// Example: `cargo run --bin pagectl layer list .neon/`
28 0 : List { path: PathBuf },
29 : /// List all layers of a given tenant and timeline
30 : ///
31 : /// Example: `cargo run --bin pagectl layer list .neon/`
32 : ListLayer {
33 0 : path: PathBuf,
34 0 : tenant: String,
35 0 : timeline: String,
36 : },
37 : /// Dump all information of a layer file
38 : DumpLayer {
39 0 : path: PathBuf,
40 0 : tenant: String,
41 0 : timeline: String,
42 : /// The id from list-layer command
43 0 : id: usize,
44 0 : },
45 : }
46 :
47 0 : async fn read_delta_file(path: impl AsRef<Path>) -> Result<()> {
48 0 : let path = path.as_ref();
49 0 : virtual_file::init(10);
50 0 : page_cache::init(100);
51 0 : let file = FileBlockReader::new(VirtualFile::open(path)?);
52 0 : let summary_blk = file.read_blk(0).await?;
53 0 : let actual_summary = Summary::des_prefix(summary_blk.as_ref())?;
54 0 : let tree_reader = DiskBtreeReader::<_, DELTA_KEY_SIZE>::new(
55 0 : actual_summary.index_start_blk,
56 0 : actual_summary.index_root_blk,
57 0 : &file,
58 0 : );
59 0 : // TODO(chi): dedup w/ `delta_layer.rs` by exposing the API.
60 0 : let mut all = vec![];
61 0 : tree_reader
62 0 : .visit(
63 0 : &[0u8; DELTA_KEY_SIZE],
64 0 : VisitDirection::Forwards,
65 0 : |key, value_offset| {
66 0 : let curr = Key::from_slice(&key[..KEY_SIZE]);
67 0 : all.push((curr, BlobRef(value_offset)));
68 0 : true
69 0 : },
70 0 : )
71 0 : .await?;
72 0 : let cursor = BlockCursor::new_fileblockreader(&file);
73 0 : for (k, v) in all {
74 0 : let value = cursor.read_blob(v.pos()).await?;
75 0 : println!("key:{} value_len:{}", k, value.len());
76 : }
77 : // TODO(chi): special handling for last key?
78 0 : Ok(())
79 0 : }
80 :
81 0 : pub(crate) async fn main(cmd: &LayerCmd) -> Result<()> {
82 0 : match cmd {
83 0 : LayerCmd::List { path } => {
84 0 : for tenant in fs::read_dir(path.join(TENANTS_SEGMENT_NAME))? {
85 0 : let tenant = tenant?;
86 0 : if !tenant.file_type()?.is_dir() {
87 0 : continue;
88 0 : }
89 0 : println!("tenant {}", tenant.file_name().to_string_lossy());
90 0 : for timeline in fs::read_dir(tenant.path().join(TIMELINES_SEGMENT_NAME))? {
91 0 : let timeline = timeline?;
92 0 : if !timeline.file_type()?.is_dir() {
93 0 : continue;
94 0 : }
95 0 : println!("- timeline {}", timeline.file_name().to_string_lossy());
96 : }
97 : }
98 : }
99 : LayerCmd::ListLayer {
100 0 : path,
101 0 : tenant,
102 0 : timeline,
103 0 : } => {
104 0 : let timeline_path = path
105 0 : .join(TENANTS_SEGMENT_NAME)
106 0 : .join(tenant)
107 0 : .join(TIMELINES_SEGMENT_NAME)
108 0 : .join(timeline);
109 0 : let mut idx = 0;
110 0 : for layer in fs::read_dir(timeline_path)? {
111 0 : let layer = layer?;
112 0 : if let Some(layer_file) = parse_filename(&layer.file_name().into_string().unwrap())
113 0 : {
114 0 : println!(
115 0 : "[{:3}] key:{}-{}\n lsn:{}-{}\n delta:{}",
116 0 : idx,
117 0 : layer_file.key_range.start,
118 0 : layer_file.key_range.end,
119 0 : layer_file.lsn_range.start,
120 0 : layer_file.lsn_range.end,
121 0 : layer_file.is_delta,
122 0 : );
123 0 : idx += 1;
124 0 : }
125 : }
126 : }
127 : LayerCmd::DumpLayer {
128 0 : path,
129 0 : tenant,
130 0 : timeline,
131 0 : id,
132 0 : } => {
133 0 : let timeline_path = path
134 0 : .join("tenants")
135 0 : .join(tenant)
136 0 : .join("timelines")
137 0 : .join(timeline);
138 0 : let mut idx = 0;
139 0 : for layer in fs::read_dir(timeline_path)? {
140 0 : let layer = layer?;
141 0 : if let Some(layer_file) = parse_filename(&layer.file_name().into_string().unwrap())
142 : {
143 0 : if *id == idx {
144 : // TODO(chi): dedup code
145 0 : println!(
146 0 : "[{:3}] key:{}-{}\n lsn:{}-{}\n delta:{}",
147 0 : idx,
148 0 : layer_file.key_range.start,
149 0 : layer_file.key_range.end,
150 0 : layer_file.lsn_range.start,
151 0 : layer_file.lsn_range.end,
152 0 : layer_file.is_delta,
153 0 : );
154 0 :
155 0 : if layer_file.is_delta {
156 0 : read_delta_file(layer.path()).await?;
157 : } else {
158 0 : anyhow::bail!("not supported yet :(");
159 : }
160 :
161 0 : break;
162 0 : }
163 0 : idx += 1;
164 0 : }
165 : }
166 : }
167 : }
168 0 : Ok(())
169 0 : }
|