LCOV - code coverage report
Current view: top level - pageserver/ctl/src - layers.rs (source / functions) Coverage Total Hit
Test: 2e3a7638747e564a4f6d1af1cc0c3b3438fbb740.info Lines: 0.0 % 153 0
Test Date: 2024-11-20 01:36:58 Functions: 0.0 % 33 0

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

Generated by: LCOV version 2.1-beta