Skip to content

Commit

Permalink
Merge pull request #9074 from Xuanwo/fix-hive-config
Browse files Browse the repository at this point in the history
fix: Add more tests for hive config loading
  • Loading branch information
BohuTANG committed Dec 2, 2022
2 parents 57af75f + 007cb80 commit 4aaa873
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 36 deletions.
10 changes: 9 additions & 1 deletion src/meta/service/src/configs/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,19 @@ impl Config {
///
/// In the future, we could have `ConfigV1` and `ConfigV2`.
pub fn load() -> Result<Self, MetaStartupError> {
let cfg = OuterV0Config::load()?.into();
let cfg = OuterV0Config::load(true)?.into();

Ok(cfg)
}

/// # NOTE
///
/// This function is served for tests only.
pub fn load_for_test() -> Result<Self, MetaStartupError> {
let cfg: Self = OuterV0Config::load(false)?.into();
Ok(cfg)
}

/// Transform config into the outer style.
///
/// This function should only be used for end-users.
Expand Down
18 changes: 15 additions & 3 deletions src/meta/service/src/configs/outer_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,18 @@ impl Config {
/// - Load from file as default.
/// - Load from env, will override config from file.
/// - Load from args as finally override
pub fn load() -> Result<Self, MetaStartupError> {
let arg_conf = Self::parse();
///
/// # Notes
///
/// with_args is to control whether we need to load from args or not.
/// We should set this to false during tests because we don't want
/// our test binary to parse cargo's args.
pub fn load(with_args: bool) -> Result<Self, MetaStartupError> {
let mut arg_conf = Self::default();

if with_args {
arg_conf = Self::parse();
}

let mut builder: serfig::Builder<Self> = serfig::Builder::default();

Expand All @@ -213,7 +223,9 @@ impl Config {
builder = builder.collect(from_self(cfg_via_env.into()));

// Finally, load from args.
builder = builder.collect(from_self(arg_conf));
if with_args {
builder = builder.collect(from_self(arg_conf));
}

builder
.build()
Expand Down
6 changes: 3 additions & 3 deletions src/meta/service/tests/it/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ cluster_name = "foo_cluster"
)?;

temp_env::with_var("METASRV_CONFIG_FILE", Some(file_path.clone()), || {
let cfg = Config::load().expect("load must success");
let cfg = Config::load_for_test().expect("load must success");
assert_eq!(cfg.log.file.level, "ERROR");
assert_eq!(cfg.log.file.dir, "foo/logs");
assert_eq!(cfg.admin_api_address, "127.0.0.1:9000");
Expand Down Expand Up @@ -101,7 +101,7 @@ cluster_name = "foo_cluster"
("METASRV_LOG_LEVEL", Some("DEBUG")),
],
|| {
let cfg = Config::load().expect("load must success");
let cfg = Config::load_for_test().expect("load must success");
assert_eq!(cfg.log.file.level, "DEBUG");
},
);
Expand All @@ -116,7 +116,7 @@ cluster_name = "foo_cluster"
("KVSRV_API_PORT", Some("123")),
],
|| {
let cfg = Config::load().expect("load must success");
let cfg = Config::load_for_test().expect("load must success");
assert_eq!(cfg.raft_config.raft_api_port, 123);
},
);
Expand Down
10 changes: 9 additions & 1 deletion src/query/config/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Config {
///
/// In the future, we could have `ConfigV1` and `ConfigV2`.
pub fn load() -> Result<Self> {
let cfg: Self = OuterV0Config::load()?.try_into()?;
let cfg: Self = OuterV0Config::load(true)?.try_into()?;

// Only check meta config when cmd is empty.
if cfg.cmd.is_empty() {
Expand All @@ -71,6 +71,14 @@ impl Config {
Ok(cfg)
}

/// # NOTE
///
/// This function is served for tests only.
pub fn load_for_test() -> Result<Self> {
let cfg: Self = OuterV0Config::load(false)?.try_into()?;
Ok(cfg)
}

pub fn tls_query_cli_enabled(&self) -> bool {
!self.query.rpc_tls_query_server_root_ca_cert.is_empty()
&& !self.query.rpc_tls_query_service_domain_name.is_empty()
Expand Down
2 changes: 2 additions & 0 deletions src/query/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ mod outer_v0;
mod version;

pub use inner::CatalogConfig;
pub use inner::CatalogHiveConfig;
pub use inner::Config;
pub use inner::QueryConfig;
pub use inner::ThriftProtocol;
pub use version::DATABEND_COMMIT_VERSION;
pub use version::QUERY_SEMVER;
43 changes: 20 additions & 23 deletions src/query/config/src/outer_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,18 @@ impl Config {
/// - Load from file as default.
/// - Load from env, will override config from file.
/// - Load from args as finally override
pub fn load() -> Result<Self> {
let arg_conf = Self::parse();
///
/// # Notes
///
/// with_args is to control whether we need to load from args or not.
/// We should set this to false during tests because we don't want
/// our test binary to parse cargo's args.
pub fn load(with_args: bool) -> Result<Self> {
let mut arg_conf = Self::default();

if with_args {
arg_conf = Self::parse();
}

let mut builder: serfig::Builder<Self> = serfig::Builder::default();

Expand All @@ -154,7 +164,9 @@ impl Config {
builder = builder.collect(from_env());

// Finally, load from args.
builder = builder.collect(from_self(arg_conf));
if with_args {
builder = builder.collect(from_self(arg_conf));
}

Ok(builder.build()?)
}
Expand All @@ -169,7 +181,7 @@ impl From<InnerConfig> for Config {
log: inner.log.into(),
meta: inner.meta.into(),
storage: inner.storage.into(),
catalog: HiveCatalogConfig::empty(),
catalog: HiveCatalogConfig::default(),

catalogs: inner
.catalogs
Expand Down Expand Up @@ -366,9 +378,9 @@ impl TryInto<InnerStorageConfig> for StorageConfig {
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CatalogConfig {
#[serde(rename = "type")]
ty: String,
pub ty: String,
#[serde(flatten)]
hive: CatalogsHiveConfig,
pub hive: CatalogsHiveConfig,
}

impl Default for CatalogConfig {
Expand All @@ -389,8 +401,8 @@ pub struct CatalogsHiveConfig {
}

/// this is the legacy version of external catalog configuration
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Args)]
#[serde(default = "HiveCatalogConfig::empty")]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Args)]
#[serde(default)]
pub struct HiveCatalogConfig {
#[clap(long = "hive-meta-store-address", default_value_t)]
#[serde(rename = "address", alias = "meta_store_address")]
Expand Down Expand Up @@ -468,21 +480,6 @@ impl From<InnerCatalogHiveConfig> for HiveCatalogConfig {
}
}

impl Default for HiveCatalogConfig {
fn default() -> Self {
InnerCatalogHiveConfig::default().into()
}
}

impl HiveCatalogConfig {
pub fn empty() -> Self {
HiveCatalogConfig {
meta_store_address: "".to_string(),
protocol: "".to_string(),
}
}
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct CacheConfig {
Expand Down
93 changes: 88 additions & 5 deletions src/query/service/tests/it/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use std::fs;
use std::io::Write;

use common_config::CatalogConfig;
use common_config::CatalogHiveConfig;
use common_config::Config;
use common_config::ThriftProtocol;
use common_exception::Result;
use pretty_assertions::assert_eq;

Expand Down Expand Up @@ -252,7 +254,7 @@ fn test_env_config_s3() -> Result<()> {
("CONFIG_FILE", None),
],
|| {
let configured = Config::load().expect("must success").into_outer();
let configured = Config::load_for_test().expect("must success").into_outer();

assert_eq!("DEBUG", configured.log.level);

Expand Down Expand Up @@ -370,7 +372,7 @@ fn test_env_config_fs() -> Result<()> {
("CONFIG_FILE", None),
],
|| {
let configured = Config::load().expect("must success").into_outer();
let configured = Config::load_for_test().expect("must success").into_outer();

assert_eq!("DEBUG", configured.log.level);

Expand Down Expand Up @@ -489,7 +491,7 @@ fn test_env_config_gcs() -> Result<()> {
("CONFIG_FILE", None),
],
|| {
let configured = Config::load().expect("must success").into_outer();
let configured = Config::load_for_test().expect("must success").into_outer();

assert_eq!("DEBUG", configured.log.level);

Expand Down Expand Up @@ -615,7 +617,7 @@ fn test_env_config_oss() -> Result<()> {
("CONFIG_FILE", None),
],
|| {
let configured = Config::load().expect("must success").into_outer();
let configured = Config::load_for_test().expect("must success").into_outer();

assert_eq!("DEBUG", configured.log.level);

Expand Down Expand Up @@ -824,7 +826,9 @@ protocol = "binary"
("STORAGE_TYPE", None),
],
|| {
let cfg = Config::load().expect("config load success").into_outer();
let cfg = Config::load_for_test()
.expect("config load success")
.into_outer();

assert_eq!("tenant_id_from_env", cfg.query.tenant_id);
assert_eq!("access_key_id_from_env", cfg.storage.s3.access_key_id);
Expand Down Expand Up @@ -860,3 +864,82 @@ protocol = "binary"

Ok(())
}

/// Test old hive catalog
#[test]
fn test_override_config_old_hive_catalog() -> Result<()> {
let file_path = temp_dir().join("databend_test_override_config_old_hive_catalog.toml");

let mut f = fs::File::create(&file_path)?;
f.write_all(
r#"
[catalog]
meta_store_address = "1.1.1.1:10000"
protocol = "binary"
"#
.as_bytes(),
)?;

// Make sure all data flushed.
f.flush()?;

temp_env::with_vars(
vec![("CONFIG_FILE", Some(file_path.to_string_lossy().as_ref()))],
|| {
let cfg = Config::load_for_test().expect("config load success");

assert_eq!(
cfg.catalogs["hive"],
CatalogConfig::Hive(CatalogHiveConfig {
address: "1.1.1.1:10000".to_string(),
protocol: ThriftProtocol::Binary,
})
);
},
);

// remove temp file
fs::remove_file(file_path)?;

Ok(())
}

/// Test new hive catalog
#[test]
fn test_override_config_new_hive_catalog() -> Result<()> {
let file_path = temp_dir().join("databend_test_override_config_new_hive_catalog.toml");

let mut f = fs::File::create(&file_path)?;
f.write_all(
r#"
[catalogs.my_hive]
type = "hive"
address = "1.1.1.1:12000"
protocol = "binary"
"#
.as_bytes(),
)?;

// Make sure all data flushed.
f.flush()?;

temp_env::with_vars(
vec![("CONFIG_FILE", Some(file_path.to_string_lossy().as_ref()))],
|| {
let cfg = Config::load_for_test().expect("config load success");

assert_eq!(
cfg.catalogs["my_hive"],
CatalogConfig::Hive(CatalogHiveConfig {
address: "1.1.1.1:12000".to_string(),
protocol: ThriftProtocol::Binary,
})
);
},
);

// remove temp file
fs::remove_file(file_path)?;

Ok(())
}

1 comment on commit 4aaa873

@vercel
Copy link

@vercel vercel bot commented on 4aaa873 Dec 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

databend – ./

databend-databend.vercel.app
databend.vercel.app
databend-git-main-databend.vercel.app
databend.rs

Please sign in to comment.