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

refactor(service/tikv): Add TikvConfig to implement ConfigDeserializer #3512

Merged
merged 18 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
ee90429
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 7, 2023
c581237
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
c589df6
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
abcd7f9
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
a3d63d9
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
8ab1eeb
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
9532459
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
7ce1172
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
848cc66
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
6ffa785
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
56df5bc
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
38ebbbe
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
c90c397
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
ca3cb72
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
6015964
Merge branch 'main' into tikv
caicancai Nov 8, 2023
d51567a
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
35c86aa
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
1836318
refactor(service/tikv): add tikvConfig to implement ConfigDeserializer
caicancai Nov 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ pub use self::redb::Redb;
mod tikv;
#[cfg(feature = "services-tikv")]
pub use self::tikv::Tikv;
#[cfg(feature = "services-tikv")]
pub use self::tikv::TikvConfig;

#[cfg(feature = "services-foundationdb")]
mod foundationdb;
Expand Down
94 changes: 60 additions & 34 deletions core/src/services/tikv/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,69 +20,101 @@ use std::fmt::Debug;
use std::fmt::Formatter;

use async_trait::async_trait;
use serde::Deserialize;
use tikv_client::Config;
use tikv_client::RawClient;
use tokio::sync::OnceCell;

use crate::raw::adapters::kv;
use crate::raw::*;
use crate::Builder;
use crate::Capability;
use crate::Error;
use crate::ErrorKind;
use crate::Scheme;
use crate::*;

/// TiKV backend builder
#[doc = include_str!("docs.md")]
#[derive(Clone, Default)]
pub struct TikvBuilder {
/// Config for Tikv services support.
#[derive(Default, Deserialize, Clone)]
#[serde(default)]
#[non_exhaustive]
pub struct TikvConfig {
caicancai marked this conversation as resolved.
Show resolved Hide resolved
/// network address of the TiKV service.
endpoints: Option<Vec<String>>,
pub endpoints: Option<Vec<String>>,
/// whether using insecure connection to TiKV
insecure: bool,
pub insecure: bool,
/// certificate authority file path
ca_path: Option<String>,
pub ca_path: Option<String>,
/// cert path
cert_path: Option<String>,
pub cert_path: Option<String>,
/// key path
key_path: Option<String>,
pub key_path: Option<String>,
}

impl Debug for TikvConfig {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut d = f.debug_struct("TikvConfig");

d.field("endpoints", &self.endpoints)
.field("insecure", &self.insecure)
.field("ca_path", &self.ca_path)
.field("cert_path", &self.cert_path)
.field("key_path", &self.key_path)
.finish()
}
}

/// TiKV backend builder
#[doc = include_str!("docs.md")]
#[derive(Clone, Default)]
pub struct TikvBuilder {
config: TikvConfig,
}

impl Debug for TikvBuilder {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut d = f.debug_struct("TikvBuilder");

d.field("config", &self.config);
d.finish_non_exhaustive()
}
}

impl TikvBuilder {
/// Set the network address of the TiKV service.
pub fn endpoints(&mut self, endpoints: Vec<String>) -> &mut Self {
if !endpoints.is_empty() {
self.endpoints = Some(endpoints)
self.config.endpoints = Some(endpoints)
}
self
}

/// Set the insecure connection to TiKV.
pub fn insecure(&mut self) -> &mut Self {
self.insecure = true;
self.config.insecure = true;
self
}

/// Set the certificate authority file path.
pub fn ca_path(&mut self, ca_path: &str) -> &mut Self {
if !ca_path.is_empty() {
self.ca_path = Some(ca_path.to_string())
self.config.ca_path = Some(ca_path.to_string())
}
self
}

/// Set the certificate file path.
pub fn cert_path(&mut self, cert_path: &str) -> &mut Self {
if !cert_path.is_empty() {
self.cert_path = Some(cert_path.to_string())
self.config.cert_path = Some(cert_path.to_string())
}
self
}

/// Set the key file path.
pub fn key_path(&mut self, key_path: &str) -> &mut Self {
if !key_path.is_empty() {
self.key_path = Some(key_path.to_string())
self.config.key_path = Some(key_path.to_string())
}
self
}
Expand All @@ -93,32 +125,25 @@ impl Builder for TikvBuilder {
type Accessor = Backend;

fn from_map(map: HashMap<String, String>) -> Self {
let mut builder = TikvBuilder::default();

map.get("endpoints")
.map(|v| v.split(',').map(|s| s.to_owned()).collect::<Vec<String>>())
.map(|v| builder.endpoints(v));
map.get("insecure")
.filter(|v| *v == "on" || *v == "true")
.map(|_| builder.insecure());
map.get("ca_path").map(|v| builder.ca_path(v));
map.get("cert_path").map(|v| builder.cert_path(v));
map.get("key_path").map(|v| builder.key_path(v));

builder
let config = TikvConfig::deserialize(ConfigDeserializer::new(map))
.expect("config deserialize must succeed");

TikvBuilder { config }
}

fn build(&mut self) -> Result<Self::Accessor> {
let endpoints = self.endpoints.take().ok_or_else(|| {
let endpoints = self.config.endpoints.take().ok_or_else(|| {
Error::new(
ErrorKind::ConfigInvalid,
"endpoints is required but not set",
)
.with_context("service", Scheme::Tikv)
})?;

if self.insecure
&& (self.ca_path.is_some() || self.key_path.is_some() || self.cert_path.is_some())
if self.config.insecure
&& (self.config.ca_path.is_some()
|| self.config.key_path.is_some()
|| self.config.cert_path.is_some())
{
return Err(
Error::new(ErrorKind::ConfigInvalid, "invalid tls configuration")
Expand All @@ -130,10 +155,10 @@ impl Builder for TikvBuilder {
Ok(Backend::new(Adapter {
client: OnceCell::new(),
endpoints,
insecure: self.insecure,
ca_path: self.ca_path.clone(),
cert_path: self.cert_path.clone(),
key_path: self.key_path.clone(),
insecure: self.config.insecure,
ca_path: self.config.ca_path.clone(),
cert_path: self.config.cert_path.clone(),
key_path: self.config.key_path.clone(),
}))
}
}
Expand All @@ -154,6 +179,7 @@ pub struct Adapter {
impl Debug for Adapter {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut ds = f.debug_struct("Adapter");

ds.field("endpoints", &self.endpoints);
ds.finish()
}
Expand Down
1 change: 1 addition & 0 deletions core/src/services/tikv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
mod backend;

pub use backend::TikvBuilder as Tikv;
pub use backend::TikvConfig;