Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clickhouse policy to the db #6862

Merged
merged 17 commits into from
Oct 17, 2024
303 changes: 156 additions & 147 deletions dev-tools/reconfigurator-cli/tests/output/cmd-example-stdout

Large diffs are not rendered by default.

111 changes: 111 additions & 0 deletions nexus/db-model/src/clickhouse_policy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 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/.

//! Database representation of a clickhouse deployment policy

use super::impl_enum_type;
use crate::SqlU32;
use crate::{schema::clickhouse_policy, SqlU8};
use chrono::{DateTime, Utc};
use nexus_types::deployment;
use serde::{Deserialize, Serialize};

impl_enum_type!(
#[derive(Clone, SqlType, Debug, QueryId)]
#[diesel(postgres_type(name = "clickhouse_mode", schema = "public"))]
pub struct ClickhouseModeEnum;

#[derive(Clone, Copy, Debug, AsExpression, FromSqlRow, Serialize, Deserialize, PartialEq)]
#[diesel(sql_type = ClickhouseModeEnum)]
pub enum DbClickhouseMode;

// Enum values
SingleNodeOnly => b"single_node_only"
ClusterOnly => b"cluster_only"
Both => b"both"
);

#[derive(Queryable, Clone, Debug, Selectable, Insertable)]
#[diesel(table_name = clickhouse_policy)]
pub struct ClickhousePolicy {
pub version: SqlU32,
pub clickhouse_mode: DbClickhouseMode,
pub clickhouse_cluster_target_servers: SqlU8,
pub clickhouse_cluster_target_keepers: SqlU8,
pub time_created: DateTime<Utc>,
}

impl From<&deployment::ClickhouseMode> for DbClickhouseMode {
fn from(value: &deployment::ClickhouseMode) -> Self {
match value {
deployment::ClickhouseMode::SingleNodeOnly => {
DbClickhouseMode::SingleNodeOnly
}
deployment::ClickhouseMode::ClusterOnly { .. } => {
DbClickhouseMode::ClusterOnly
}
deployment::ClickhouseMode::Both { .. } => DbClickhouseMode::Both,
}
}
}

impl From<ClickhousePolicy> for deployment::ClickhousePolicy {
fn from(value: ClickhousePolicy) -> Self {
let mode = match value.clickhouse_mode {
DbClickhouseMode::SingleNodeOnly => {
deployment::ClickhouseMode::SingleNodeOnly
}
DbClickhouseMode::ClusterOnly => {
deployment::ClickhouseMode::ClusterOnly {
target_servers: value.clickhouse_cluster_target_servers.0,
target_keepers: value.clickhouse_cluster_target_keepers.0,
}
}
DbClickhouseMode::Both => deployment::ClickhouseMode::Both {
target_servers: value.clickhouse_cluster_target_servers.0,
target_keepers: value.clickhouse_cluster_target_keepers.0,
},
};

deployment::ClickhousePolicy {
version: value.version.0,
mode,
time_created: value.time_created,
}
}
}

impl From<deployment::ClickhousePolicy> for ClickhousePolicy {
fn from(value: deployment::ClickhousePolicy) -> Self {
match value.mode {
deployment::ClickhouseMode::SingleNodeOnly => ClickhousePolicy {
version: value.version.into(),
clickhouse_mode: DbClickhouseMode::SingleNodeOnly,
clickhouse_cluster_target_servers: 0.into(),
clickhouse_cluster_target_keepers: 0.into(),
time_created: value.time_created,
},
deployment::ClickhouseMode::ClusterOnly {
target_servers,
target_keepers,
} => ClickhousePolicy {
version: value.version.into(),
clickhouse_mode: DbClickhouseMode::ClusterOnly,
clickhouse_cluster_target_servers: target_servers.into(),
clickhouse_cluster_target_keepers: target_keepers.into(),
time_created: value.time_created,
},
deployment::ClickhouseMode::Both {
target_servers,
target_keepers,
} => ClickhousePolicy {
version: value.version.into(),
clickhouse_mode: DbClickhouseMode::Both,
clickhouse_cluster_target_servers: target_servers.into(),
clickhouse_cluster_target_keepers: target_keepers.into(),
time_created: value.time_created,
},
}
}
}
2 changes: 2 additions & 0 deletions nexus/db-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod block_size;
mod bootstore;
mod bytecount;
mod certificate;
mod clickhouse_policy;
mod cockroachdb_node_id;
mod collection;
mod console_session;
Expand Down Expand Up @@ -133,6 +134,7 @@ pub use block_size::*;
pub use bootstore::*;
pub use bytecount::*;
pub use certificate::*;
pub use clickhouse_policy::*;
pub use cockroachdb_node_id::*;
pub use collection::*;
pub use console_session::*;
Expand Down
10 changes: 10 additions & 0 deletions nexus/db-model/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,16 @@ table! {
}
}

table! {
clickhouse_policy (version) {
version -> Int8,
clickhouse_mode -> crate::clickhouse_policy::ClickhouseModeEnum,
clickhouse_cluster_target_servers -> Int2,
clickhouse_cluster_target_keepers -> Int2,
time_created -> Timestamptz,
}
}

table! {
rack (id) {
id -> Uuid,
Expand Down
3 changes: 2 additions & 1 deletion nexus/db-model/src/schema_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::collections::BTreeMap;
///
/// This must be updated when you change the database schema. Refer to
/// schema/crdb/README.adoc in the root of this repository for details.
pub const SCHEMA_VERSION: SemverVersion = SemverVersion::new(109, 0, 0);
pub const SCHEMA_VERSION: SemverVersion = SemverVersion::new(110, 0, 0);

/// List of all past database schema versions, in *reverse* order
///
Expand All @@ -29,6 +29,7 @@ static KNOWN_VERSIONS: Lazy<Vec<KnownVersion>> = Lazy::new(|| {
// | leaving the first copy as an example for the next person.
// v
// KnownVersion::new(next_int, "unique-dirname-with-the-sql-files"),
KnownVersion::new(110, "clickhouse-policy"),
KnownVersion::new(109, "inv-clickhouse-keeper-membership"),
KnownVersion::new(108, "internet-gateway"),
KnownVersion::new(107, "add-instance-boot-disk"),
Expand Down
Loading
Loading