Skip to content

Commit

Permalink
tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
karencfv committed Aug 27, 2024
1 parent d52ea89 commit 1de9376
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
25 changes: 13 additions & 12 deletions clickhouse-admin/types/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn path_schema(gen: &mut SchemaGenerator) -> Schema {
schema.into()
}

/// Config for an individual Clickhouse Replica
/// Configuration for a ClickHouse replica server
#[derive(Debug, Clone, PartialEq, Eq, JsonSchema, Serialize, Deserialize)]
pub struct ReplicaConfig {
pub logger: LogConfig,
Expand All @@ -45,6 +45,7 @@ pub struct ReplicaConfig {
}

impl ReplicaConfig {
/// A new ClickHouse replica server configuration with default ports and directories
pub fn new(
logger: LogConfig,
macros: Macros,
Expand Down Expand Up @@ -166,6 +167,7 @@ pub struct Macros {
}

impl Macros {
/// A new macros configuration block with default cluster
pub fn new(replica: ServerId) -> Self {
Self { shard: 1, replica, cluster: OXIMETER_CLUSTER.to_string() }
}
Expand All @@ -191,6 +193,7 @@ pub struct RemoteServers {
}

impl RemoteServers {
/// A new remote_servers configuration block with default cluster
pub fn new(replicas: Vec<ServerNodeConfig>) -> Self {
Self {
cluster: OXIMETER_CLUSTER.to_string(),
Expand Down Expand Up @@ -270,9 +273,10 @@ pub struct KeeperNodeConfig {
}

impl KeeperNodeConfig {
/// A new ClickHouse keeper node configuration with default port
pub fn new(host: String) -> Self {
let port = CLICKHOUSE_KEEPER_TCP_PORT;
KeeperNodeConfig { host, port }
Self { host, port }
}
}

Expand All @@ -283,9 +287,10 @@ pub struct ServerNodeConfig {
}

impl ServerNodeConfig {
/// A new ClickHouse replica node configuration with default port
pub fn new(host: String) -> Self {
let port = CLICKHOUSE_TCP_PORT;
ServerNodeConfig { host, port }
Self { host, port }
}
}

Expand All @@ -306,6 +311,7 @@ pub struct LogConfig {
}

impl LogConfig {
/// A new logger configuration with default directories
pub fn new(path: Utf8PathBuf, node_type: NodeType) -> Self {
let prefix = match node_type {
NodeType::Server => "clickhouse",
Expand All @@ -316,13 +322,7 @@ impl LogConfig {
let log = logs.join(format!("{prefix}.log"));
let errorlog = logs.join(format!("{prefix}.err.log"));

LogConfig {
level: LogLevel::default(),
log,
errorlog,
size: 100,
count: 1,
}
Self { level: LogLevel::default(), log, errorlog, size: 100, count: 1 }
}

pub fn to_xml(&self) -> String {
Expand Down Expand Up @@ -395,11 +395,11 @@ pub struct RaftServerConfig {

impl RaftServerConfig {
pub fn new(id: KeeperId, hostname: String) -> Self {
RaftServerConfig { id, hostname, port: CLICKHOUSE_KEEPER_RAFT_PORT }
Self { id, hostname, port: CLICKHOUSE_KEEPER_RAFT_PORT }
}
}

/// Config for an individual Clickhouse Keeper
/// Configuration for a ClickHouse keeper
#[derive(Debug, Clone, PartialEq, Eq, JsonSchema, Serialize, Deserialize)]
pub struct KeeperConfig {
pub logger: LogConfig,
Expand All @@ -415,6 +415,7 @@ pub struct KeeperConfig {
}

impl KeeperConfig {
/// A new ClickHouse keeper node configuration with default ports and directories
pub fn new(
logger: LogConfig,
listen_host: Ipv6Addr,
Expand Down
34 changes: 22 additions & 12 deletions clickhouse-admin/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use config::*;

pub const OXIMETER_CLUSTER: &str = "oximeter_cluster";

/// A unique ID for a clickhouse keeper
/// A unique ID for a ClickHouse Keeper
#[derive(
Debug,
Clone,
Expand All @@ -35,7 +35,7 @@ pub const OXIMETER_CLUSTER: &str = "oximeter_cluster";
)]
pub struct KeeperId(pub u64);

/// A unique ID for a clickhouse server
/// A unique ID for a Clickhouse Server
#[derive(
Debug,
Clone,
Expand Down Expand Up @@ -65,7 +65,6 @@ pub struct ClickhouseServerConfig {
}

impl ClickhouseServerConfig {
#[allow(clippy::too_many_arguments)]
pub fn new(
config_dir: Utf8PathBuf,
id: ServerId,
Expand All @@ -77,6 +76,7 @@ impl ClickhouseServerConfig {
Self { config_dir, id, datastore_path, listen_addr, keepers, servers }
}

/// Generate a configuration file for a replica server node
pub fn generate_xml_file(&self) -> Result<()> {
let logger =
LogConfig::new(self.datastore_path.clone(), NodeType::Server);
Expand All @@ -101,30 +101,39 @@ impl ClickhouseServerConfig {
#[derive(Debug, Clone)]
pub struct ClickhouseKeeperConfig {
pub config_dir: Utf8PathBuf,
pub id: KeeperId,
pub raft_servers: Vec<RaftServerConfig>,
pub path: Utf8PathBuf,
pub datastore_path: Utf8PathBuf,
pub listen_addr: Ipv6Addr,
}

impl ClickhouseKeeperConfig {
pub fn new(
config_dir: Utf8PathBuf,
id: KeeperId,
raft_servers: Vec<RaftServerConfig>,
path: Utf8PathBuf,
datastore_path: Utf8PathBuf,
listen_addr: Ipv6Addr,
) -> Self {
ClickhouseKeeperConfig { config_dir, raft_servers, path, listen_addr }
ClickhouseKeeperConfig {
config_dir,
id,
raft_servers,
datastore_path,
listen_addr,
}
}

/// Generate a configuration file for `this_keeper` consisting of the keepers in `raft_servers`
pub fn generate_xml_file(&self, this_keeper: KeeperId) -> Result<()> {
let logger = LogConfig::new(self.path.clone(), NodeType::Keeper);
/// Generate a configuration file for a keeper node
pub fn generate_xml_file(&self) -> Result<()> {
let logger =
LogConfig::new(self.datastore_path.clone(), NodeType::Keeper);
let raft_config = RaftServers::new(self.raft_servers.clone());
let config = KeeperConfig::new(
logger,
self.listen_addr,
this_keeper,
self.path.clone(),
self.id,
self.datastore_path.clone(),
raft_config,
);

Expand Down Expand Up @@ -164,12 +173,13 @@ mod tests {

let config = ClickhouseKeeperConfig::new(
Utf8PathBuf::from(config_dir.path()),
KeeperId(1),
keepers,
Utf8PathBuf::from_str("./").unwrap(),
Ipv6Addr::from_str("ff::08").unwrap(),
);

config.generate_xml_file(KeeperId(1)).unwrap();
config.generate_xml_file().unwrap();

let expected_file = Utf8PathBuf::from_str("./testutils")
.unwrap()
Expand Down

0 comments on commit 1de9376

Please sign in to comment.