use crate::cnf::Config;
#[derive(Clone, Debug)]
pub struct TikvConfig {
pub api_version: u8,
pub keyspace: Option<String>,
pub request_timeout_secs: u64,
pub async_commit: bool,
pub one_phase_commit: bool,
pub grpc_max_decoding_message_size: usize,
pub grpc_max_encoding_message_size: usize,
pub delr_max_keys: u32,
pub tls_ca_path: Option<String>,
pub tls_cert_path: Option<String>,
pub tls_key_path: Option<String>,
pub gc_enabled: bool,
pub gc_lifetime_secs: u64,
pub health_probe: bool,
pub shutdown_grace_secs: u64,
pub shutdown_gc_timeout_secs: u64,
}
pub(super) const TIKV_DELR_BATCH_SIZE: u32 = 1_024;
impl Default for TikvConfig {
fn default() -> Self {
Self {
api_version: 1,
keyspace: None,
request_timeout_secs: 10,
async_commit: true,
one_phase_commit: true,
grpc_max_decoding_message_size: 4 * 1024 * 1024,
grpc_max_encoding_message_size: 4 * 1024 * 1024,
delr_max_keys: 1_000_000,
tls_ca_path: None,
tls_cert_path: None,
tls_key_path: None,
gc_enabled: true,
gc_lifetime_secs: 600,
health_probe: true,
shutdown_grace_secs: 30,
shutdown_gc_timeout_secs: 10,
}
}
}
impl Config for TikvConfig {
fn parse(&mut self, map: &crate::cnf::ConfigMap) {
map.parse_key("tikv_api_version", &mut self.api_version)
.parse_key_option("tikv_keyspace", &mut self.keyspace)
.parse_key("tikv_request_timeout", &mut self.request_timeout_secs)
.parse_key_bool("tikv_async_commit", &mut self.async_commit)
.parse_key_bool("tikv_one_phase_commit", &mut self.one_phase_commit)
.parse_key(
"tikv_grpc_max_decoding_message_size",
&mut self.grpc_max_decoding_message_size,
)
.parse_key(
"tikv_grpc_max_encoding_message_size",
&mut self.grpc_max_encoding_message_size,
)
.parse_key("tikv_delr_max_keys", &mut self.delr_max_keys)
.parse_key_option("tikv_tls_ca_path", &mut self.tls_ca_path)
.parse_key_option("tikv_tls_cert_path", &mut self.tls_cert_path)
.parse_key_option("tikv_tls_key_path", &mut self.tls_key_path)
.parse_key_bool("tikv_gc_enabled", &mut self.gc_enabled)
.parse_key("tikv_gc_lifetime", &mut self.gc_lifetime_secs)
.parse_key_bool("tikv_health_probe", &mut self.health_probe)
.parse_key("tikv_shutdown_grace", &mut self.shutdown_grace_secs)
.parse_key("tikv_shutdown_gc_timeout", &mut self.shutdown_gc_timeout_secs);
}
}
#[cfg(test)]
mod test {
use super::TikvConfig;
use crate::cnf::ConfigMap;
#[test]
fn defaults_when_map_empty() {
let config = ConfigMap::empty().load::<TikvConfig>();
assert_eq!(config.api_version, 1);
assert!(config.keyspace.is_none());
assert_eq!(config.request_timeout_secs, 10);
assert!(config.async_commit);
assert!(config.one_phase_commit);
assert_eq!(config.grpc_max_decoding_message_size, 4 * 1024 * 1024);
assert_eq!(config.grpc_max_encoding_message_size, 4 * 1024 * 1024);
assert_eq!(config.delr_max_keys, 1_000_000);
assert!(config.tls_ca_path.is_none());
assert!(config.gc_enabled);
assert_eq!(config.gc_lifetime_secs, 600);
assert!(config.health_probe);
assert_eq!(config.shutdown_grace_secs, 30);
assert_eq!(config.shutdown_gc_timeout_secs, 10);
}
#[test]
fn parses_tikv_keys_via_configmap() {
let map = ConfigMap::empty()
.with_key_value("tikv_api_version", "2")
.with_key_value("tikv_keyspace", "my-keyspace")
.with_key_value("tikv_request_timeout", "20")
.with_key_value("tikv_async_commit", "false")
.with_key_value("tikv_one_phase_commit", "false")
.with_key_value("tikv_grpc_max_decoding_message_size", "8388608")
.with_key_value("tikv_grpc_max_encoding_message_size", "16777216")
.with_key_value("tikv_delr_max_keys", "50000")
.with_key_value("tikv_tls_ca_path", "/etc/tikv/ca.pem")
.with_key_value("tikv_tls_cert_path", "/etc/tikv/cert.pem")
.with_key_value("tikv_tls_key_path", "/etc/tikv/key.pem")
.with_key_value("tikv_gc_enabled", "false")
.with_key_value("tikv_gc_lifetime", "1200")
.with_key_value("tikv_health_probe", "false")
.with_key_value("tikv_shutdown_grace", "60")
.with_key_value("tikv_shutdown_gc_timeout", "5");
let config = map.load::<TikvConfig>();
assert_eq!(config.api_version, 2);
assert_eq!(config.keyspace.as_deref(), Some("my-keyspace"));
assert_eq!(config.request_timeout_secs, 20);
assert!(!config.async_commit);
assert!(!config.one_phase_commit);
assert_eq!(config.grpc_max_decoding_message_size, 8 * 1024 * 1024);
assert_eq!(config.grpc_max_encoding_message_size, 16 * 1024 * 1024);
assert_eq!(config.delr_max_keys, 50_000);
assert_eq!(config.tls_ca_path.as_deref(), Some("/etc/tikv/ca.pem"));
assert_eq!(config.tls_cert_path.as_deref(), Some("/etc/tikv/cert.pem"));
assert_eq!(config.tls_key_path.as_deref(), Some("/etc/tikv/key.pem"));
assert!(!config.gc_enabled);
assert_eq!(config.gc_lifetime_secs, 1200);
assert!(!config.health_probe);
assert_eq!(config.shutdown_grace_secs, 60);
assert_eq!(config.shutdown_gc_timeout_secs, 5);
}
}