1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
// Copyright (c) 2017-present, PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
//! # Raft Engine Control
use std::path::Path;
use std::sync::Arc;
use clap::{crate_authors, crate_version, Parser};
use raft_engine::env::{DefaultFileSystem, FileSystem};
use raft_engine::internals::LogQueue;
use raft_engine::{Engine, Error, Result as EngineResult};
#[derive(Debug, clap::Parser)]
#[clap(
name = "ctl",
author = crate_authors!(),
version = crate_version!(),
dont_collapse_args_in_usage = true,
)]
pub struct ControlOpt {
// sub command type
#[clap(subcommand)]
cmd: Option<Cmd>,
}
#[derive(Debug, Parser)]
enum Cmd {
/// Dump log entries in data file(s).
Dump {
/// Path of Raft Engine directory or specific log file.
#[clap(short, long)]
path: String,
#[clap(short, long, use_value_delimiter = true)]
raft_groups: Vec<u64>,
},
/// Check data files for logical errors.
Check {
/// Path of Raft Engine directory.
#[clap(short, long)]
path: String,
},
/// Run Rhai script to repair data files.
Repair {
/// Path of Raft Engine directory.
#[clap(short, long)]
path: String,
#[clap(
short,
long,
possible_values = &["append", "rewrite", "all"]
)]
queue: String,
/// Path of Rhai script file.
#[clap(short, long)]
script: String,
},
/// Try running `purge_expired_files` on existing data directory.
TryPurge {
/// Path of Raft Engine directory.
#[clap(short, long)]
path: String,
},
}
fn convert_queue(queue: &str) -> Option<LogQueue> {
match queue {
"append" => Some(LogQueue::Append),
"rewrite" => Some(LogQueue::Rewrite),
"all" => None,
_ => unreachable!(),
}
}
impl ControlOpt {
pub fn validate_and_execute(self) -> EngineResult<()> {
self.validate_and_execute_with_file_system(Arc::new(DefaultFileSystem))
}
pub fn validate_and_execute_with_file_system<F: FileSystem>(
mut self,
fs: Arc<F>,
) -> EngineResult<()> {
if self.cmd.is_none() {
return Err(Error::InvalidArgument("subcommand is needed".to_owned()));
}
match self.cmd.take().unwrap() {
Cmd::Dump { path, raft_groups } => {
let it = Engine::dump_with_file_system(Path::new(&path), fs)?;
for item in it {
if let Ok(v) = item {
if raft_groups.is_empty() || raft_groups.contains(&v.raft_group_id) {
println!("{v:?}")
}
} else {
// output error message
println!("{item:?}")
}
}
}
Cmd::Repair {
path,
queue,
script,
} => {
let script = std::fs::read_to_string(script)?;
Engine::unsafe_repair_with_file_system(
Path::new(&path),
convert_queue(&queue),
script,
fs,
)?;
}
Cmd::Check { path } => {
let r = Engine::consistency_check_with_file_system(Path::new(&path), fs)?;
if r.is_empty() {
println!("All data is Ok")
} else {
println!("Corrupted info are as follows:\nraft_group_id, last_intact_index\n");
r.iter().for_each(|(x, y)| println!("{x:?}, {y:?}"))
}
}
Cmd::TryPurge { path } => {
let e = Engine::open_with_file_system(
raft_engine::Config {
dir: path,
..Default::default()
},
fs,
)?;
println!(
"purge_expired_files() returns {:?}",
e.purge_expired_files()?
);
}
}
Ok(())
}
}
pub fn run_command<F: FileSystem>(mut args: Vec<String>, fs: Arc<F>) {
args.insert(0, "ctl".to_owned());
let opts = ControlOpt::parse_from(args);
if let Err(e) = opts.validate_and_execute_with_file_system(fs) {
println!("{e:?}");
}
}