Skip to content

Commit

Permalink
fix: GET: /config return all configurations when running standalone (
Browse files Browse the repository at this point in the history
…#2630)

* fix: config api return all configurations when running standalone

* chore: follow same style

* fix: avoid panic
  • Loading branch information
NiwakaDev authored Nov 8, 2023
1 parent b382900 commit 06d273b
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 34 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/cmd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ snafu.workspace = true
substrait = { workspace = true }
table = { workspace = true }
tokio.workspace = true
toml.workspace = true

[target.'cfg(not(windows))'.dependencies]
tikv-jemallocator = "0.5"
Expand All @@ -69,7 +70,6 @@ tikv-jemallocator = "0.5"
common-test-util = { workspace = true }
serde.workspace = true
temp-env = "0.3"
toml.workspace = true

[target.'cfg(not(windows))'.dev-dependencies]
rexpect = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl StartCommand {
.context(StartFrontendSnafu)?;

instance
.build_servers(&opts)
.build_servers(opts)
.await
.context(StartFrontendSnafu)?;

Expand Down
16 changes: 15 additions & 1 deletion src/cmd/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use common_config::KvBackendConfig;
use common_telemetry::logging::LoggingOptions;
use config::{Config, Environment, File, FileFormat};
use datanode::config::{DatanodeOptions, ProcedureConfig};
use frontend::frontend::FrontendOptions;
use frontend::error::{Result as FeResult, TomlFormatSnafu};
use frontend::frontend::{FrontendOptions, TomlSerializable};
use meta_srv::metasrv::MetaSrvOptions;
use serde::{Deserialize, Serialize};
use snafu::ResultExt;
Expand All @@ -27,6 +28,7 @@ pub const ENV_VAR_SEP: &str = "__";
pub const ENV_LIST_SEP: &str = ",";

/// Options mixed up from datanode, frontend and metasrv.
#[derive(Serialize)]
pub struct MixOptions {
pub data_home: String,
pub procedure: ProcedureConfig,
Expand All @@ -36,6 +38,18 @@ pub struct MixOptions {
pub logging: LoggingOptions,
}

impl From<MixOptions> for FrontendOptions {
fn from(value: MixOptions) -> Self {
value.frontend
}
}

impl TomlSerializable for MixOptions {
fn to_toml(&self) -> FeResult<String> {
toml::to_string(self).context(TomlFormatSnafu)
}
}

pub enum Options {
Datanode(Box<DatanodeOptions>),
Frontend(Box<FrontendOptions>),
Expand Down
12 changes: 6 additions & 6 deletions src/cmd/src/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,13 @@ impl StartCommand {
#[allow(unused_variables)]
#[allow(clippy::diverging_sub_expression)]
async fn build(self, opts: MixOptions) -> Result<Instance> {
let mut fe_opts = opts.frontend;
#[allow(clippy::unnecessary_mut_passed)]
let fe_plugins = plugins::setup_frontend_plugins(&mut fe_opts)
let fe_opts = opts.frontend.clone();
let fe_plugins = plugins::setup_frontend_plugins(&fe_opts)
.await
.context(StartFrontendSnafu)?;

let dn_opts = opts.datanode;
let dn_opts = opts.datanode.clone();

info!("Standalone start command: {:#?}", self);
info!(
Expand All @@ -338,8 +338,8 @@ impl StartCommand {
let metadata_dir = metadata_store_dir(&opts.data_home);
let (kv_backend, procedure_manager) = FeInstance::try_build_standalone_components(
metadata_dir,
opts.metadata_store,
opts.procedure,
opts.metadata_store.clone(),
opts.procedure.clone(),
)
.await
.context(StartFrontendSnafu)?;
Expand Down Expand Up @@ -377,7 +377,7 @@ impl StartCommand {
.await?;

frontend
.build_servers(&fe_opts)
.build_servers(opts)
.await
.context(StartFrontendSnafu)?;

Expand Down
9 changes: 8 additions & 1 deletion src/frontend/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,21 @@ pub enum Error {

#[snafu(display("Invalid auth config"))]
IllegalAuthConfig { source: auth::error::Error },

#[snafu(display("Failed to serialize options to TOML"))]
TomlFormat {
#[snafu(source)]
error: toml::ser::Error,
},
}

pub type Result<T> = std::result::Result<T, Error>;

impl ErrorExt for Error {
fn status_code(&self) -> StatusCode {
match self {
Error::ParseAddr { .. }
Error::TomlFormat { .. }
| Error::ParseAddr { .. }
| Error::InvalidSql { .. }
| Error::InvalidInsertRequest { .. }
| Error::InvalidDeleteRequest { .. }
Expand Down
12 changes: 12 additions & 0 deletions src/frontend/src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use serde::{Deserialize, Serialize};
use servers::heartbeat_options::HeartbeatOptions;
use servers::http::HttpOptions;
use servers::Mode;
use snafu::prelude::*;

use crate::error::{Result, TomlFormatSnafu};
use crate::service_config::{
DatanodeOptions, GrpcOptions, InfluxdbOptions, MysqlOptions, OpentsdbOptions, OtlpOptions,
PostgresOptions, PromStoreOptions,
Expand Down Expand Up @@ -76,6 +78,16 @@ impl FrontendOptions {
}
}

pub trait TomlSerializable {
fn to_toml(&self) -> Result<String>;
}

impl TomlSerializable for FrontendOptions {
fn to_toml(&self) -> Result<String> {
toml::to_string(&self).context(TomlFormatSnafu)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
7 changes: 5 additions & 2 deletions src/frontend/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ use crate::error::{
ParseSqlSnafu, PermissionSnafu, PlanStatementSnafu, Result, SqlExecInterceptedSnafu,
TableOperationSnafu,
};
use crate::frontend::FrontendOptions;
use crate::frontend::{FrontendOptions, TomlSerializable};
use crate::heartbeat::handler::invalidate_table_cache::InvalidateTableCacheHandler;
use crate::heartbeat::HeartbeatTask;
use crate::metrics;
Expand Down Expand Up @@ -358,7 +358,10 @@ impl Instance {
})
}

pub async fn build_servers(&mut self, opts: &FrontendOptions) -> Result<()> {
pub async fn build_servers(
&mut self,
opts: impl Into<FrontendOptions> + TomlSerializable,
) -> Result<()> {
let servers = Services::build(opts, Arc::new(self.clone()), self.plugins.clone()).await?;
self.servers = Arc::new(servers);

Expand Down
15 changes: 9 additions & 6 deletions src/frontend/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use servers::server::Server;
use snafu::ResultExt;

use crate::error::{self, Result, StartServerSnafu};
use crate::frontend::FrontendOptions;
use crate::frontend::{FrontendOptions, TomlSerializable};
use crate::instance::FrontendInstance;

pub(crate) struct Services;
Expand All @@ -43,14 +43,17 @@ pub type ServerHandlers = HashMap<String, ServerHandler>;
pub type ServerHandler = (Box<dyn Server>, SocketAddr);

impl Services {
pub(crate) async fn build<T>(
opts: &FrontendOptions,
instance: Arc<T>,
pub(crate) async fn build<T, U>(
opts: T,
instance: Arc<U>,
plugins: Plugins,
) -> Result<ServerHandlers>
where
T: FrontendInstance,
T: Into<FrontendOptions> + TomlSerializable,
U: FrontendInstance,
{
let toml = opts.to_toml()?;
let opts: FrontendOptions = opts.into();
let mut result = Vec::<ServerHandler>::with_capacity(plugins.len());
let user_provider = plugins.get::<UserProviderRef>();

Expand Down Expand Up @@ -120,7 +123,7 @@ impl Services {
.with_metrics_handler(MetricsHandler)
.with_script_handler(instance.clone())
.with_plugins(plugins)
.with_greptime_config_options(opts.to_toml_string())
.with_greptime_config_options(toml)
.build();
result.push((Box::new(http_server), http_addr));
}
Expand Down
1 change: 1 addition & 0 deletions tests-integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ axum-test-helper = { git = "https://github.com/sunng87/axum-test-helper.git", br
catalog = { workspace = true }
chrono.workspace = true
client = { workspace = true, features = ["testing"] }
cmd.workspace = true
common-base = { workspace = true }
common-catalog = { workspace = true }
common-config = { workspace = true }
Expand Down
20 changes: 17 additions & 3 deletions tests-integration/src/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@
use std::sync::Arc;

use catalog::kvbackend::KvBackendCatalogManager;
use cmd::options::MixOptions;
use common_base::Plugins;
use common_config::KvBackendConfig;
use common_meta::cache_invalidator::DummyKvCacheInvalidator;
use common_procedure::options::ProcedureConfig;
use common_telemetry::logging::LoggingOptions;
use datanode::config::DatanodeOptions;
use datanode::datanode::DatanodeBuilder;
use frontend::frontend::FrontendOptions;
use frontend::instance::{FrontendInstance, Instance, StandaloneDatanodeManager};

use crate::test_util::{self, create_tmp_dir_and_datanode_opts, StorageType, TestGuard};

pub struct GreptimeDbStandalone {
pub instance: Arc<Instance>,
pub datanode_opts: DatanodeOptions,
pub mix_options: MixOptions,
pub guard: TestGuard,
}

Expand Down Expand Up @@ -66,10 +70,12 @@ impl GreptimeDbStandaloneBuilder {

let (opts, guard) = create_tmp_dir_and_datanode_opts(store_type, &self.instance_name);

let procedure_config = ProcedureConfig::default();
let kv_backend_config = KvBackendConfig::default();
let (kv_backend, procedure_manager) = Instance::try_build_standalone_components(
format!("{}/kv", &opts.storage.data_home),
KvBackendConfig::default(),
ProcedureConfig::default(),
kv_backend_config.clone(),
procedure_config.clone(),
)
.await
.unwrap();
Expand Down Expand Up @@ -110,7 +116,15 @@ impl GreptimeDbStandaloneBuilder {

GreptimeDbStandalone {
instance: Arc::new(instance),
datanode_opts: opts,
datanode_opts: opts.clone(),
mix_options: MixOptions {
data_home: opts.storage.data_home.to_string(),
procedure: procedure_config,
metadata_store: kv_backend_config,
frontend: FrontendOptions::default(),
datanode: opts,
logging: LoggingOptions::default(),
},
guard,
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests-integration/src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use datanode::config::{
AzblobConfig, DatanodeOptions, FileConfig, GcsConfig, ObjectStoreConfig, OssConfig, S3Config,
StorageConfig,
};
use frontend::frontend::TomlSerializable;
use frontend::instance::Instance;
use frontend::service_config::{MysqlOptions, PostgresOptions};
use object_store::services::{Azblob, Gcs, Oss, S3};
Expand Down Expand Up @@ -376,7 +377,7 @@ pub async fn setup_test_http_app_with_frontend_and_user_provider(
instance.instance.clone(),
))
.with_script_handler(instance.instance.clone())
.with_greptime_config_options(instance.datanode_opts.to_toml_string());
.with_greptime_config_options(instance.mix_options.to_toml().unwrap());

if let Some(user_provider) = user_provider {
http_server.with_user_provider(user_provider);
Expand Down
Loading

0 comments on commit 06d273b

Please sign in to comment.