fix: GET: /config return all configurations when running standalone (#2630)

* fix: config api return all configurations when running standalone

* chore: follow same style

* fix: avoid panic
This commit is contained in:
Niwaka
2023-11-08 12:22:19 +09:00
committed by GitHub
parent b382900c5c
commit 06d273b75a
13 changed files with 163 additions and 34 deletions

View File

@@ -272,6 +272,12 @@ 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>;
@@ -279,7 +285,8 @@ 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 { .. }

View File

@@ -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,
@@ -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::*;

View File

@@ -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;
@@ -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);

View File

@@ -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;
@@ -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>();
@@ -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));
}