-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[reconfigurator] Retrieve keeper lgif information (#6549)
## Overview This commit introduces a new `clickhouse-admin` API endpoint: `/keeper/lgif`. This endpoint uses the ClickHouse CLI internally to retrieve and parse the logically grouped information file from the ClickHouse keepers. ## Purpose Reconfigurator will need this information to reliably manage and operate a ClickHouse replicated cluster. Additional endpoints to retrieve other information from ClickHouse servers or keepers will be added in follow up PRs. ## Testing In addition to the unit tests, I have manually tested with the following results: ```console $ cargo run --bin=clickhouse-admin -- run -c ./smf/clickhouse-admin/config.toml -a [::1]:8888 -l [::1]:20001 -b /Users/karcar/src/omicron/out/clickhouse/clickhouse Compiling omicron-clickhouse-admin v0.1.0 (/Users/karcar/src/omicron/clickhouse-admin) Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.46s Running `target/debug/clickhouse-admin run -c ./smf/clickhouse-admin/config.toml -a '[::1]:8888' -l '[::1]:20001' -b /Users/karcar/src/omicron/out/clickhouse/clickhouse` note: configured to log to "/dev/stdout" {"msg":"listening","v":0,"name":"clickhouse-admin","level":30,"time":"2024-09-12T02:37:19.383597Z","hostname":"ixchel","pid":3115,"local_addr":"[::1]:8888","component":"dropshot","file":"/Users/karcar/.cargo/git/checkouts/dropshot-a4a923d29dccc492/06c8dab/dropshot/src/server.rs:205"} {"msg":"accepted connection","v":0,"name":"clickhouse-admin","level":30,"time":"2024-09-12T02:37:23.843325Z","hostname":"ixchel","pid":3115,"local_addr":"[::1]:8888","component":"dropshot","file":"/Users/karcar/.cargo/git/checkouts/dropshot-a4a923d29dccc492/06c8dab/dropshot/src/server.rs:775","remote_addr":"[::1]:54455"} {"msg":"request completed","v":0,"name":"clickhouse-admin","level":30,"time":"2024-09-12T02:37:24.302588Z","hostname":"ixchel","pid":3115,"uri":"/keeper/lgif","method":"GET","req_id":"64b232d0-d6ac-4cae-8f0a-f14cf6d1dfba","remote_addr":"[::1]:54455","local_addr":"[::1]:8888","component":"dropshot","file":"/Users/karcar/.cargo/git/checkouts/dropshot-a4a923d29dccc492/06c8dab/dropshot/src/server.rs:914","latency_us":458301,"response_code":"200"} ``` ```console $ curl http://[::1]:8888/keeper/lgif {"first_log_idx":1,"first_log_term":1,"last_log_idx":11717,"last_log_term":20,"last_committed_log_idx":11717,"leader_committed_log_idx":11717,"target_committed_log_idx":11717,"last_snapshot_idx":9465} ``` Related: #5999
- Loading branch information
Showing
17 changed files
with
725 additions
and
95 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
use anyhow::Result; | ||
use camino::Utf8PathBuf; | ||
use clickhouse_admin_types::Lgif; | ||
use dropshot::HttpError; | ||
use illumos_utils::{output_to_exec_error, ExecutionError}; | ||
use slog::Logger; | ||
use slog_error_chain::{InlineErrorChain, SlogInlineError}; | ||
use std::ffi::OsStr; | ||
use std::io; | ||
use std::net::SocketAddrV6; | ||
use tokio::process::Command; | ||
|
||
#[derive(Debug, thiserror::Error, SlogInlineError)] | ||
pub enum ClickhouseCliError { | ||
#[error("failed to run `clickhouse {subcommand}`")] | ||
Run { | ||
description: &'static str, | ||
subcommand: String, | ||
#[source] | ||
err: io::Error, | ||
}, | ||
#[error(transparent)] | ||
ExecutionError(#[from] ExecutionError), | ||
#[error("failed to parse command output")] | ||
Parse { | ||
description: &'static str, | ||
stdout: String, | ||
stderr: String, | ||
#[source] | ||
err: anyhow::Error, | ||
}, | ||
} | ||
|
||
impl From<ClickhouseCliError> for HttpError { | ||
fn from(err: ClickhouseCliError) -> Self { | ||
match err { | ||
ClickhouseCliError::Run { .. } | ||
| ClickhouseCliError::Parse { .. } | ||
| ClickhouseCliError::ExecutionError(_) => { | ||
let message = InlineErrorChain::new(&err).to_string(); | ||
HttpError { | ||
status_code: http::StatusCode::INTERNAL_SERVER_ERROR, | ||
error_code: Some(String::from("Internal")), | ||
external_message: message.clone(), | ||
internal_message: message, | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct ClickhouseCli { | ||
/// Path to where the clickhouse binary is located | ||
pub binary_path: Utf8PathBuf, | ||
/// Address on where the clickhouse keeper is listening on | ||
pub listen_address: SocketAddrV6, | ||
pub log: Option<Logger>, | ||
} | ||
|
||
impl ClickhouseCli { | ||
pub fn new(binary_path: Utf8PathBuf, listen_address: SocketAddrV6) -> Self { | ||
Self { binary_path, listen_address, log: None } | ||
} | ||
|
||
pub fn with_log(mut self, log: Logger) -> Self { | ||
self.log = Some(log); | ||
self | ||
} | ||
|
||
pub async fn lgif(&self) -> Result<Lgif, ClickhouseCliError> { | ||
self.keeper_client_non_interactive( | ||
"lgif", | ||
"Retrieve logically grouped information file", | ||
Lgif::parse, | ||
self.log.clone().unwrap(), | ||
) | ||
.await | ||
} | ||
|
||
async fn keeper_client_non_interactive<F, T>( | ||
&self, | ||
query: &str, | ||
subcommand_description: &'static str, | ||
parse: F, | ||
log: Logger, | ||
) -> Result<T, ClickhouseCliError> | ||
where | ||
F: FnOnce(&Logger, &[u8]) -> Result<T>, | ||
{ | ||
let mut command = Command::new(&self.binary_path); | ||
command | ||
.arg("keeper-client") | ||
.arg("--host") | ||
.arg(&format!("[{}]", self.listen_address.ip())) | ||
.arg("--port") | ||
.arg(&format!("{}", self.listen_address.port())) | ||
.arg("--query") | ||
.arg(query); | ||
|
||
let output = command.output().await.map_err(|err| { | ||
let err_args: Vec<&OsStr> = command.as_std().get_args().collect(); | ||
let err_args_parsed: Vec<String> = err_args | ||
.iter() | ||
.map(|&os_str| os_str.to_string_lossy().into_owned()) | ||
.collect(); | ||
let err_args_str = err_args_parsed.join(" "); | ||
ClickhouseCliError::Run { | ||
description: subcommand_description, | ||
subcommand: err_args_str, | ||
err, | ||
} | ||
})?; | ||
|
||
if !output.status.success() { | ||
return Err(output_to_exec_error(command.as_std(), &output).into()); | ||
} | ||
|
||
parse(&log, &output.stdout).map_err(|err| ClickhouseCliError::Parse { | ||
description: subcommand_description, | ||
stdout: String::from_utf8_lossy(&output.stdout).to_string(), | ||
stderr: String::from_utf8_lossy(&output.stdout).to_string(), | ||
err, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.