persistcli/
main.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10#![warn(missing_debug_implementations)]
11#![warn(
12    clippy::cast_possible_truncation,
13    clippy::cast_precision_loss,
14    clippy::cast_sign_loss
15)]
16
17//! Persist command-line utilities
18
19use mz_orchestrator_tracing::{StaticTracingConfig, TracingCliArgs};
20use mz_ore::cli::{self, CliConfig};
21use mz_ore::error::ErrorExt;
22use mz_ore::metrics::MetricsRegistry;
23
24pub mod maelstrom;
25pub mod open_loop;
26pub mod service;
27
28#[derive(Debug, clap::Parser)]
29#[clap(about = "Persist command-line utilities", long_about = None)]
30struct Args {
31    #[clap(subcommand)]
32    command: Command,
33
34    #[clap(flatten)]
35    tracing: TracingCliArgs,
36}
37
38#[derive(Debug, clap::Subcommand)]
39enum Command {
40    Maelstrom(crate::maelstrom::Args),
41    MaelstromTxn(crate::maelstrom::Args),
42    OpenLoop(crate::open_loop::Args),
43    Inspect(mz_persist_client::cli::inspect::InspectArgs),
44    Admin(mz_persist_client::cli::admin::AdminArgs),
45    Bench(mz_persist_client::cli::bench::BenchArgs),
46    Service(crate::service::Args),
47}
48
49fn main() {
50    let args: Args = cli::parse_args(CliConfig::default());
51
52    // Mirror the tokio Runtime configuration in our production binaries.
53    let ncpus_useful = usize::max(1, std::cmp::min(num_cpus::get(), num_cpus::get_physical()));
54    let runtime = tokio::runtime::Builder::new_multi_thread()
55        .worker_threads(ncpus_useful)
56        .enable_all()
57        .build()
58        .expect("Failed building the Runtime");
59
60    let (_, _tracing_guard) = runtime
61        .block_on(args.tracing.configure_tracing(
62            StaticTracingConfig {
63                service_name: "persistcli",
64                build_info: mz_persist_client::BUILD_INFO,
65            },
66            MetricsRegistry::new(),
67        ))
68        .expect("failed to init tracing");
69
70    let res = match args.command {
71        Command::Maelstrom(args) => runtime.block_on(crate::maelstrom::run::<
72            crate::maelstrom::txn_list_append_single::TransactorService,
73        >(args)),
74        Command::MaelstromTxn(args) => runtime.block_on(crate::maelstrom::run::<
75            crate::maelstrom::txn_list_append_multi::TransactorService,
76        >(args)),
77        Command::OpenLoop(args) => runtime.block_on(crate::open_loop::run(args)),
78        Command::Inspect(command) => {
79            runtime.block_on(mz_persist_client::cli::inspect::run(command))
80        }
81        Command::Admin(command) => runtime.block_on(mz_persist_client::cli::admin::run(command)),
82        Command::Bench(command) => runtime.block_on(mz_persist_client::cli::bench::run(command)),
83        Command::Service(args) => runtime.block_on(crate::service::run(args)),
84    };
85
86    if let Err(err) = res {
87        eprintln!("persistcli: fatal: {}", err.display_with_causes());
88        std::process::exit(1);
89    }
90    drop(_tracing_guard);
91}