LCOV - code coverage report
Current view: top level - pageserver/ctl/src - layers.rs (source / functions) Coverage Total Hit
Test: 49aa928ec5b4b510172d8b5c6d154da28e70a46c.info Lines: 0.0 % 150 0
Test Date: 2024-11-13 18:23:39 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 :     );
      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 :     );
      69            0 :     page_cache::init(100);
      70            0 :     let path = Utf8Path::from_path(path.as_ref()).expect("non-Unicode path");
      71            0 :     let file = File::open(path)?;
      72            0 :     let image_layer = ImageLayer::new_for_path(path, file)?;
      73            0 :     image_layer.dump(true, ctx).await?;
      74            0 :     Ok(())
      75            0 : }
      76              : 
      77            0 : pub(crate) async fn main(cmd: &LayerCmd) -> Result<()> {
      78            0 :     let ctx = RequestContext::new(TaskKind::DebugTool, DownloadBehavior::Error);
      79            0 :     match cmd {
      80            0 :         LayerCmd::List { path } => {
      81            0 :             for tenant in fs::read_dir(path.join(TENANTS_SEGMENT_NAME))? {
      82            0 :                 let tenant = tenant?;
      83            0 :                 if !tenant.file_type()?.is_dir() {
      84            0 :                     continue;
      85            0 :                 }
      86            0 :                 println!("tenant {}", tenant.file_name().to_string_lossy());
      87            0 :                 for timeline in fs::read_dir(tenant.path().join(TIMELINES_SEGMENT_NAME))? {
      88            0 :                     let timeline = timeline?;
      89            0 :                     if !timeline.file_type()?.is_dir() {
      90            0 :                         continue;
      91            0 :                     }
      92            0 :                     println!("- timeline {}", timeline.file_name().to_string_lossy());
      93              :                 }
      94              :             }
      95            0 :             Ok(())
      96              :         }
      97              :         LayerCmd::ListLayer {
      98            0 :             path,
      99            0 :             tenant,
     100            0 :             timeline,
     101            0 :         } => {
     102            0 :             let timeline_path = path
     103            0 :                 .join(TENANTS_SEGMENT_NAME)
     104            0 :                 .join(tenant)
     105            0 :                 .join(TIMELINES_SEGMENT_NAME)
     106            0 :                 .join(timeline);
     107            0 :             let mut idx = 0;
     108            0 :             for layer in fs::read_dir(timeline_path)? {
     109            0 :                 let layer = layer?;
     110            0 :                 if let Ok(layer_file) = parse_filename(&layer.file_name().into_string().unwrap()) {
     111            0 :                     println!(
     112            0 :                         "[{:3}]  key:{}-{}\n       lsn:{}-{}\n       delta:{}",
     113            0 :                         idx,
     114            0 :                         layer_file.key_range.start,
     115            0 :                         layer_file.key_range.end,
     116            0 :                         layer_file.lsn_range.start,
     117            0 :                         layer_file.lsn_range.end,
     118            0 :                         layer_file.is_delta,
     119            0 :                     );
     120            0 :                     idx += 1;
     121            0 :                 }
     122              :             }
     123            0 :             Ok(())
     124              :         }
     125              :         LayerCmd::DumpLayer {
     126            0 :             path,
     127            0 :             tenant,
     128            0 :             timeline,
     129            0 :             id,
     130            0 :         } => {
     131            0 :             let timeline_path = path
     132            0 :                 .join("tenants")
     133            0 :                 .join(tenant)
     134            0 :                 .join("timelines")
     135            0 :                 .join(timeline);
     136            0 :             let mut idx = 0;
     137            0 :             for layer in fs::read_dir(timeline_path)? {
     138            0 :                 let layer = layer?;
     139            0 :                 if let Ok(layer_file) = parse_filename(&layer.file_name().into_string().unwrap()) {
     140            0 :                     if *id == idx {
     141              :                         // TODO(chi): dedup code
     142            0 :                         println!(
     143            0 :                             "[{:3}]  key:{}-{}\n       lsn:{}-{}\n       delta:{}",
     144            0 :                             idx,
     145            0 :                             layer_file.key_range.start,
     146            0 :                             layer_file.key_range.end,
     147            0 :                             layer_file.lsn_range.start,
     148            0 :                             layer_file.lsn_range.end,
     149            0 :                             layer_file.is_delta,
     150            0 :                         );
     151            0 : 
     152            0 :                         if layer_file.is_delta {
     153            0 :                             read_delta_file(layer.path(), &ctx).await?;
     154              :                         } else {
     155            0 :                             read_image_file(layer.path(), &ctx).await?;
     156              :                         }
     157              : 
     158            0 :                         break;
     159            0 :                     }
     160            0 :                     idx += 1;
     161            0 :                 }
     162              :             }
     163            0 :             Ok(())
     164              :         }
     165              :         LayerCmd::RewriteSummary {
     166            0 :             layer_file_path,
     167            0 :             new_tenant_id,
     168            0 :             new_timeline_id,
     169            0 :         } => {
     170            0 :             pageserver::virtual_file::init(
     171            0 :                 10,
     172            0 :                 virtual_file::api::IoEngineKind::StdFs,
     173            0 :                 IoMode::preferred(),
     174            0 :             );
     175            0 :             pageserver::page_cache::init(100);
     176            0 : 
     177            0 :             let ctx = RequestContext::new(TaskKind::DebugTool, DownloadBehavior::Error);
     178              : 
     179              :             macro_rules! rewrite_closure {
     180              :                 ($($summary_ty:tt)*) => {{
     181              :                     |summary| $($summary_ty)* {
     182              :                         tenant_id: new_tenant_id.unwrap_or(summary.tenant_id),
     183              :                         timeline_id: new_timeline_id.unwrap_or(summary.timeline_id),
     184              :                         ..summary
     185            0 :                     }
     186              :                 }};
     187              :             }
     188              : 
     189            0 :             let res = ImageLayer::rewrite_summary(
     190            0 :                 layer_file_path,
     191            0 :                 rewrite_closure!(image_layer::Summary),
     192            0 :                 &ctx,
     193            0 :             )
     194            0 :             .await;
     195            0 :             match res {
     196              :                 Ok(()) => {
     197            0 :                     println!("Successfully rewrote summary of image layer {layer_file_path}");
     198            0 :                     return Ok(());
     199              :                 }
     200            0 :                 Err(image_layer::RewriteSummaryError::MagicMismatch) => (), // fallthrough
     201            0 :                 Err(image_layer::RewriteSummaryError::Other(e)) => {
     202            0 :                     return Err(e);
     203              :                 }
     204              :             }
     205              : 
     206            0 :             let res = DeltaLayer::rewrite_summary(
     207            0 :                 layer_file_path,
     208            0 :                 rewrite_closure!(delta_layer::Summary),
     209            0 :                 &ctx,
     210            0 :             )
     211            0 :             .await;
     212            0 :             match res {
     213              :                 Ok(()) => {
     214            0 :                     println!("Successfully rewrote summary of delta layer {layer_file_path}");
     215            0 :                     return Ok(());
     216              :                 }
     217            0 :                 Err(delta_layer::RewriteSummaryError::MagicMismatch) => (), // fallthrough
     218            0 :                 Err(delta_layer::RewriteSummaryError::Other(e)) => {
     219            0 :                     return Err(e);
     220              :                 }
     221              :             }
     222              : 
     223            0 :             anyhow::bail!("not an image or delta layer: {layer_file_path}");
     224              :         }
     225              :     }
     226            0 : }
        

Generated by: LCOV version 2.1-beta