Skip to content

Commit 5c8e062

Browse files
committed
add config to solve by custom dns server
1 parent abf22ba commit 5c8e062

File tree

12 files changed

+133
-46
lines changed

12 files changed

+133
-46
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ serde_derive = "1.0"
4343
thiserror = "1"
4444
tokio = { version = "1", features = ["sync", "rt-multi-thread", "macros"] }
4545
tonic = { version = "0.9", features = ["tls"] }
46+
trust-dns-resolver = "0.19.4"
47+
url = "2.4"
4648

4749
[dev-dependencies]
4850
clap = "2"

src/common/security.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::fs::File;
44
use std::io::Read;
55
use std::path::Path;
66
use std::path::PathBuf;
7-
use std::time::Duration;
87

98
use log::info;
109
use regex::Regex;
@@ -15,6 +14,7 @@ use tonic::transport::Identity;
1514

1615
use crate::internal_err;
1716
use crate::Result;
17+
use crate::{util, Config};
1818

1919
lazy_static::lazy_static! {
2020
static ref SCHEME_REG: Regex = Regex::new(r"^\s*(https?://)").unwrap();
@@ -73,17 +73,30 @@ impl SecurityManager {
7373
// env: Arc<Environment>,
7474
addr: &str,
7575
factory: Factory,
76+
config: &Config,
7677
) -> Result<Client>
7778
where
7879
Factory: FnOnce(Channel) -> Client,
7980
{
8081
let addr = "http://".to_string() + &SCHEME_REG.replace(addr, "");
8182

83+
let addr = match config.dns_server_addr {
84+
Some(ref dns_server_addr) => {
85+
util::dns::custom_dns(
86+
addr,
87+
dns_server_addr.clone(),
88+
config.dns_search_domain.clone(),
89+
)
90+
.await?
91+
}
92+
None => addr,
93+
};
94+
8295
info!("connect to rpc server at endpoint: {:?}", addr);
8396

8497
let mut builder = Channel::from_shared(addr)?
85-
.tcp_keepalive(Some(Duration::from_secs(10)))
86-
.keep_alive_timeout(Duration::from_secs(3));
98+
.tcp_keepalive(config.tcp_keepalive)
99+
.keep_alive_timeout(config.keep_alive_timeout);
87100

88101
if !self.ca.is_empty() {
89102
let tls = ClientTlsConfig::new()

src/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ pub struct Config {
1919
pub cert_path: Option<PathBuf>,
2020
pub key_path: Option<PathBuf>,
2121
pub timeout: Duration,
22+
pub tcp_keepalive: Option<Duration>,
23+
pub keep_alive_timeout: Duration,
24+
pub dns_server_addr: Option<String>,
25+
pub dns_search_domain: Vec<String>,
2226
}
2327

2428
const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_secs(2);
29+
const DEFAULT_TCP_KEEPALIVE: Duration = Duration::from_secs(10);
30+
const DEFAULT_KEEP_ALIVE_TIMEOUT: Duration = Duration::from_secs(3);
2531

2632
impl Default for Config {
2733
fn default() -> Self {
@@ -30,6 +36,10 @@ impl Default for Config {
3036
cert_path: None,
3137
key_path: None,
3238
timeout: DEFAULT_REQUEST_TIMEOUT,
39+
tcp_keepalive: Some(DEFAULT_TCP_KEEPALIVE),
40+
keep_alive_timeout: DEFAULT_KEEP_ALIVE_TIMEOUT,
41+
dns_server_addr: None,
42+
dns_search_domain: vec![],
3343
}
3444
}
3545
}

src/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ use crate::Timestamp;
3333
pub async fn pd_rpc_client() -> PdRpcClient<MockKvConnect, MockCluster> {
3434
let config = Config::default();
3535
PdRpcClient::new(
36-
config.clone(),
36+
&config,
3737
|_| MockKvConnect,
3838
|sm| {
3939
futures::future::ok(RetryClient::new_with_cluster(
4040
sm,
41-
config.timeout,
41+
config.clone(),
4242
MockCluster,
4343
))
4444
},

src/pd/client.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,13 @@ impl<KvC: KvConnect + Send + Sync + 'static> PdClient for PdRpcClient<KvC> {
260260
impl PdRpcClient<TikvConnect, Cluster> {
261261
pub async fn connect(
262262
pd_endpoints: &[String],
263-
config: Config,
263+
config: &Config,
264264
enable_codec: bool,
265265
) -> Result<PdRpcClient> {
266266
PdRpcClient::new(
267-
config.clone(),
268-
|security_mgr| TikvConnect::new(security_mgr, config.timeout),
269-
|security_mgr| RetryClient::connect(pd_endpoints, security_mgr, config.timeout),
267+
config,
268+
|security_mgr| TikvConnect::new(security_mgr, config.clone()),
269+
|security_mgr| RetryClient::connect(pd_endpoints, security_mgr, config),
270270
enable_codec,
271271
)
272272
.await
@@ -275,7 +275,7 @@ impl PdRpcClient<TikvConnect, Cluster> {
275275

276276
impl<KvC: KvConnect + Send + Sync + 'static, Cl> PdRpcClient<KvC, Cl> {
277277
pub async fn new<PdFut, MakeKvC, MakePd>(
278-
config: Config,
278+
config: &Config,
279279
kv_connect: MakeKvC,
280280
pd: MakePd,
281281
enable_codec: bool,

src/pd/cluster.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use tonic::Request;
1616
use super::timestamp::TimestampOracle;
1717
use crate::internal_err;
1818
use crate::proto::pdpb;
19+
use crate::Config;
1920
use crate::Result;
2021
use crate::SecurityManager;
2122
use crate::Timestamp;
@@ -103,13 +104,9 @@ impl Connection {
103104
Connection { security_mgr }
104105
}
105106

106-
pub async fn connect_cluster(
107-
&self,
108-
endpoints: &[String],
109-
timeout: Duration,
110-
) -> Result<Cluster> {
111-
let members = self.validate_endpoints(endpoints, timeout).await?;
112-
let (client, members) = self.try_connect_leader(&members, timeout).await?;
107+
pub async fn connect_cluster(&self, endpoints: &[String], config: &Config) -> Result<Cluster> {
108+
let members = self.validate_endpoints(endpoints, config).await?;
109+
let (client, members) = self.try_connect_leader(&members, config).await?;
113110
let id = members.header.as_ref().unwrap().cluster_id;
114111
let tso = TimestampOracle::new(id, &client)?;
115112
let cluster = Cluster {
@@ -122,10 +119,10 @@ impl Connection {
122119
}
123120

124121
// Re-establish connection with PD leader in asynchronous fashion.
125-
pub async fn reconnect(&self, cluster: &mut Cluster, timeout: Duration) -> Result<()> {
122+
pub async fn reconnect(&self, cluster: &mut Cluster, config: &Config) -> Result<()> {
126123
warn!("updating pd client");
127124
let start = Instant::now();
128-
let (client, members) = self.try_connect_leader(&cluster.members, timeout).await?;
125+
let (client, members) = self.try_connect_leader(&cluster.members, config).await?;
129126
let tso = TimestampOracle::new(cluster.id, &client)?;
130127
*cluster = Cluster {
131128
id: cluster.id,
@@ -141,7 +138,7 @@ impl Connection {
141138
async fn validate_endpoints(
142139
&self,
143140
endpoints: &[String],
144-
timeout: Duration,
141+
config: &Config,
145142
) -> Result<pdpb::GetMembersResponse> {
146143
let mut endpoints_set = HashSet::with_capacity(endpoints.len());
147144

@@ -152,7 +149,7 @@ impl Connection {
152149
return Err(internal_err!("duplicated PD endpoint {}", ep));
153150
}
154151

155-
let (_, resp) = match self.connect(ep, timeout).await {
152+
let (_, resp) = match self.connect(ep, config).await {
156153
Ok(resp) => resp,
157154
// Ignore failed PD node.
158155
Err(e) => {
@@ -193,11 +190,11 @@ impl Connection {
193190
async fn connect(
194191
&self,
195192
addr: &str,
196-
_timeout: Duration,
193+
config: &Config,
197194
) -> Result<(pdpb::pd_client::PdClient<Channel>, pdpb::GetMembersResponse)> {
198195
let mut client = self
199196
.security_mgr
200-
.connect(addr, pdpb::pd_client::PdClient::<Channel>::new)
197+
.connect(addr, pdpb::pd_client::PdClient::<Channel>::new, config)
201198
.await?;
202199
let resp: pdpb::GetMembersResponse = client
203200
.get_members(pdpb::GetMembersRequest::default())
@@ -210,9 +207,9 @@ impl Connection {
210207
&self,
211208
addr: &str,
212209
cluster_id: u64,
213-
timeout: Duration,
210+
config: &Config,
214211
) -> Result<(pdpb::pd_client::PdClient<Channel>, pdpb::GetMembersResponse)> {
215-
let (client, r) = self.connect(addr, timeout).await?;
212+
let (client, r) = self.connect(addr, config).await?;
216213
Connection::validate_cluster_id(addr, &r, cluster_id)?;
217214
Ok((client, r))
218215
}
@@ -238,7 +235,7 @@ impl Connection {
238235
async fn try_connect_leader(
239236
&self,
240237
previous: &pdpb::GetMembersResponse,
241-
timeout: Duration,
238+
config: &Config,
242239
) -> Result<(pdpb::pd_client::PdClient<Channel>, pdpb::GetMembersResponse)> {
243240
let previous_leader = previous.leader.as_ref().unwrap();
244241
let members = &previous.members;
@@ -252,7 +249,7 @@ impl Connection {
252249
.chain(Some(previous_leader))
253250
{
254251
for ep in &m.client_urls {
255-
match self.try_connect(ep.as_str(), cluster_id, timeout).await {
252+
match self.try_connect(ep.as_str(), cluster_id, config).await {
256253
Ok((_, r)) => {
257254
resp = Some(r);
258255
break 'outer;
@@ -269,7 +266,7 @@ impl Connection {
269266
if let Some(resp) = resp {
270267
let leader = resp.leader.as_ref().unwrap();
271268
for ep in &leader.client_urls {
272-
let r = self.try_connect(ep.as_str(), cluster_id, timeout).await;
269+
let r = self.try_connect(ep.as_str(), cluster_id, config).await;
273270
if r.is_ok() {
274271
return r;
275272
}

src/pd/retry.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::region::RegionId;
2020
use crate::region::RegionWithLeader;
2121
use crate::region::StoreId;
2222
use crate::stats::pd_stats;
23+
use crate::Config;
2324
use crate::Error;
2425
use crate::Result;
2526
use crate::SecurityManager;
@@ -51,21 +52,21 @@ pub struct RetryClient<Cl = Cluster> {
5152
// Tuple is the cluster and the time of the cluster's last reconnect.
5253
cluster: RwLock<(Cl, Instant)>,
5354
connection: Connection,
54-
timeout: Duration,
55+
config: Config,
5556
}
5657

5758
#[cfg(test)]
5859
impl<Cl> RetryClient<Cl> {
5960
pub fn new_with_cluster(
6061
security_mgr: Arc<SecurityManager>,
61-
timeout: Duration,
62+
config: Config,
6263
cluster: Cl,
6364
) -> RetryClient<Cl> {
6465
let connection = Connection::new(security_mgr);
6566
RetryClient {
6667
cluster: RwLock::new((cluster, Instant::now())),
6768
connection,
68-
timeout,
69+
config,
6970
}
7071
}
7172
}
@@ -107,17 +108,17 @@ impl RetryClient<Cluster> {
107108
pub async fn connect(
108109
endpoints: &[String],
109110
security_mgr: Arc<SecurityManager>,
110-
timeout: Duration,
111+
config: &Config,
111112
) -> Result<RetryClient> {
112113
let connection = Connection::new(security_mgr);
113114
let cluster = RwLock::new((
114-
connection.connect_cluster(endpoints, timeout).await?,
115+
connection.connect_cluster(endpoints, config).await?,
115116
Instant::now(),
116117
));
117118
Ok(RetryClient {
118119
cluster,
119120
connection,
120-
timeout,
121+
config: config.clone(),
121122
})
122123
}
123124
}
@@ -131,7 +132,7 @@ impl RetryClientTrait for RetryClient<Cluster> {
131132
let key = key.clone();
132133
async {
133134
cluster
134-
.get_region(key.clone(), self.timeout)
135+
.get_region(key.clone(), self.config.timeout)
135136
.await
136137
.and_then(|resp| {
137138
region_from_response(resp, || Error::RegionForKeyNotFound { key })
@@ -143,7 +144,7 @@ impl RetryClientTrait for RetryClient<Cluster> {
143144
async fn get_region_by_id(self: Arc<Self>, region_id: RegionId) -> Result<RegionWithLeader> {
144145
retry!(self, "get_region_by_id", |cluster| async {
145146
cluster
146-
.get_region_by_id(region_id, self.timeout)
147+
.get_region_by_id(region_id, self.config.timeout)
147148
.await
148149
.and_then(|resp| {
149150
region_from_response(resp, || Error::RegionNotFoundInResponse { region_id })
@@ -154,7 +155,7 @@ impl RetryClientTrait for RetryClient<Cluster> {
154155
async fn get_store(self: Arc<Self>, id: StoreId) -> Result<metapb::Store> {
155156
retry!(self, "get_store", |cluster| async {
156157
cluster
157-
.get_store(id, self.timeout)
158+
.get_store(id, self.config.timeout)
158159
.await
159160
.map(|resp| resp.store.unwrap())
160161
})
@@ -164,7 +165,7 @@ impl RetryClientTrait for RetryClient<Cluster> {
164165
async fn get_all_stores(self: Arc<Self>) -> Result<Vec<metapb::Store>> {
165166
retry!(self, "get_all_stores", |cluster| async {
166167
cluster
167-
.get_all_stores(self.timeout)
168+
.get_all_stores(self.config.timeout)
168169
.await
169170
.map(|resp| resp.stores.into_iter().map(Into::into).collect())
170171
})
@@ -177,7 +178,7 @@ impl RetryClientTrait for RetryClient<Cluster> {
177178
async fn update_safepoint(self: Arc<Self>, safepoint: u64) -> Result<bool> {
178179
retry!(self, "update_gc_safepoint", |cluster| async {
179180
cluster
180-
.update_safepoint(safepoint, self.timeout)
181+
.update_safepoint(safepoint, self.config.timeout)
181182
.await
182183
.map(|resp| resp.new_safe_point == safepoint)
183184
})
@@ -187,7 +188,7 @@ impl RetryClientTrait for RetryClient<Cluster> {
187188
impl fmt::Debug for RetryClient {
188189
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
189190
fmt.debug_struct("pd::RetryClient")
190-
.field("timeout", &self.timeout)
191+
.field("timeout", &self.config.timeout)
191192
.finish()
192193
}
193194
}
@@ -219,7 +220,7 @@ impl Reconnect for RetryClient<Cluster> {
219220
// a concurrent reconnect is just succeed when this thread trying to get write lock
220221
let should_connect = reconnect_begin > *last_connected + Duration::from_secs(interval_sec);
221222
if should_connect {
222-
self.connection.reconnect(cluster, self.timeout).await?;
223+
self.connection.reconnect(cluster, &self.config).await?;
223224
*last_connected = Instant::now();
224225
}
225226
Ok(())

src/raw/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl Client<PdRpcClient> {
100100
config: Config,
101101
) -> Result<Self> {
102102
let pd_endpoints: Vec<String> = pd_endpoints.into_iter().map(Into::into).collect();
103-
let rpc = Arc::new(PdRpcClient::connect(&pd_endpoints, config, false).await?);
103+
let rpc = Arc::new(PdRpcClient::connect(&pd_endpoints, &config, false).await?);
104104
Ok(Client {
105105
rpc,
106106
cf: None,

src/store/client.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use tonic::transport::Channel;
1010

1111
use super::Request;
1212
use crate::proto::tikvpb::tikv_client::TikvClient;
13+
use crate::Config;
1314
use crate::Result;
1415
use crate::SecurityManager;
1516

@@ -24,7 +25,7 @@ pub trait KvConnect: Sized + Send + Sync + 'static {
2425
#[derive(new, Clone)]
2526
pub struct TikvConnect {
2627
security_mgr: Arc<SecurityManager>,
27-
timeout: Duration,
28+
config: Config,
2829
}
2930

3031
#[async_trait]
@@ -33,9 +34,9 @@ impl KvConnect for TikvConnect {
3334

3435
async fn connect(&self, address: &str) -> Result<KvRpcClient> {
3536
self.security_mgr
36-
.connect(address, TikvClient::new)
37+
.connect(address, TikvClient::new, &self.config)
3738
.await
38-
.map(|c| KvRpcClient::new(c, self.timeout))
39+
.map(|c| KvRpcClient::new(c, self.config.timeout))
3940
}
4041
}
4142

src/transaction/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl Client {
102102
) -> Result<Client> {
103103
debug!("creating new transactional client");
104104
let pd_endpoints: Vec<String> = pd_endpoints.into_iter().map(Into::into).collect();
105-
let pd = Arc::new(PdRpcClient::connect(&pd_endpoints, config, true).await?);
105+
let pd = Arc::new(PdRpcClient::connect(&pd_endpoints, &config, true).await?);
106106
Ok(Client { pd })
107107
}
108108

0 commit comments

Comments
 (0)