-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathretry.rs
More file actions
364 lines (314 loc) · 11.5 KB
/
retry.rs
File metadata and controls
364 lines (314 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
//! A utility module for managing and retrying PD requests.
use std::fmt;
use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;
use async_trait::async_trait;
use tokio::sync::RwLock;
use tokio::time::sleep;
use crate::pd::Cluster;
use crate::pd::Connection;
use crate::proto::metapb;
use crate::proto::pdpb::Timestamp;
use crate::proto::pdpb::{self};
use crate::region::RegionId;
use crate::region::RegionWithLeader;
use crate::region::StoreId;
use crate::stats::pd_stats;
use crate::Config;
use crate::Error;
use crate::Result;
use crate::SecurityManager;
// FIXME: these numbers and how they are used are all just cargo-culted in, there
// may be more optimal values.
const RECONNECT_INTERVAL_SEC: u64 = 1;
const MAX_REQUEST_COUNT: usize = 5;
const LEADER_CHANGE_RETRY: usize = 10;
#[async_trait]
pub trait RetryClientTrait {
// These get_* functions will try multiple times to make a request, reconnecting as necessary.
// It does not know about encoding. Caller should take care of it.
async fn get_region(self: Arc<Self>, key: Vec<u8>) -> Result<RegionWithLeader>;
async fn get_region_by_id(self: Arc<Self>, region_id: RegionId) -> Result<RegionWithLeader>;
async fn get_store(self: Arc<Self>, id: StoreId) -> Result<metapb::Store>;
async fn get_all_stores(self: Arc<Self>) -> Result<Vec<metapb::Store>>;
async fn get_timestamp(self: Arc<Self>) -> Result<Timestamp>;
async fn update_safepoint(self: Arc<Self>, safepoint: u64) -> Result<bool>;
}
/// Client for communication with a PD cluster. Has the facility to reconnect to the cluster.
pub struct RetryClient<Cl = Cluster> {
// Tuple is the cluster and the time of the cluster's last reconnect.
cluster: RwLock<(Cl, Instant)>,
connection: Connection,
config: Config,
}
#[cfg(test)]
impl<Cl> RetryClient<Cl> {
pub fn new_with_cluster(
security_mgr: Arc<SecurityManager>,
config: Config,
cluster: Cl,
) -> RetryClient<Cl> {
let connection = Connection::new(security_mgr);
RetryClient {
cluster: RwLock::new((cluster, Instant::now())),
connection,
config,
}
}
}
macro_rules! retry {
($self: ident, $tag: literal, |$cluster: ident| $call: expr) => {{
let stats = pd_stats($tag);
let mut last_err = Ok(());
for _ in 0..LEADER_CHANGE_RETRY {
// use the block here to drop the guard of the read lock,
// otherwise `reconnect` will try to acquire the write lock and results in a deadlock
let res = {
let $cluster = &mut $self.cluster.write().await.0;
let res = $call.await;
res
};
match stats.done(res) {
Ok(r) => return Ok(r),
Err(e) => last_err = Err(e),
}
let mut reconnect_count = MAX_REQUEST_COUNT;
while let Err(e) = $self.reconnect(RECONNECT_INTERVAL_SEC).await {
reconnect_count -= 1;
if reconnect_count == 0 {
return Err(e);
}
sleep(Duration::from_secs(RECONNECT_INTERVAL_SEC)).await;
}
}
last_err?;
unreachable!();
}};
}
impl RetryClient<Cluster> {
pub async fn connect(
endpoints: &[String],
security_mgr: Arc<SecurityManager>,
config: &Config,
) -> Result<RetryClient> {
let connection = Connection::new(security_mgr);
let cluster = RwLock::new((
connection.connect_cluster(endpoints, config).await?,
Instant::now(),
));
Ok(RetryClient {
cluster,
connection,
config: config.clone(),
})
}
}
#[async_trait]
impl RetryClientTrait for RetryClient<Cluster> {
// These get_* functions will try multiple times to make a request, reconnecting as necessary.
// It does not know about encoding. Caller should take care of it.
async fn get_region(self: Arc<Self>, key: Vec<u8>) -> Result<RegionWithLeader> {
retry!(self, "get_region", |cluster| {
let key = key.clone();
async {
cluster
.get_region(key.clone(), self.config.timeout)
.await
.and_then(|resp| {
region_from_response(resp, || Error::RegionForKeyNotFound { key })
})
}
})
}
async fn get_region_by_id(self: Arc<Self>, region_id: RegionId) -> Result<RegionWithLeader> {
retry!(self, "get_region_by_id", |cluster| async {
cluster
.get_region_by_id(region_id, self.config.timeout)
.await
.and_then(|resp| {
region_from_response(resp, || Error::RegionNotFoundInResponse { region_id })
})
})
}
async fn get_store(self: Arc<Self>, id: StoreId) -> Result<metapb::Store> {
retry!(self, "get_store", |cluster| async {
cluster
.get_store(id, self.config.timeout)
.await
.map(|resp| resp.store.unwrap())
})
}
#[allow(dead_code)]
async fn get_all_stores(self: Arc<Self>) -> Result<Vec<metapb::Store>> {
retry!(self, "get_all_stores", |cluster| async {
cluster
.get_all_stores(self.config.timeout)
.await
.map(|resp| resp.stores.into_iter().map(Into::into).collect())
})
}
async fn get_timestamp(self: Arc<Self>) -> Result<Timestamp> {
retry!(self, "get_timestamp", |cluster| cluster.get_timestamp())
}
async fn update_safepoint(self: Arc<Self>, safepoint: u64) -> Result<bool> {
retry!(self, "update_gc_safepoint", |cluster| async {
cluster
.update_safepoint(safepoint, self.config.timeout)
.await
.map(|resp| resp.new_safe_point == safepoint)
})
}
}
impl fmt::Debug for RetryClient {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("pd::RetryClient")
.field("timeout", &self.config.timeout)
.finish()
}
}
fn region_from_response(
mut resp: pdpb::GetRegionResponse,
err: impl FnOnce() -> Error,
) -> Result<RegionWithLeader> {
let region = resp.region.take().ok_or_else(err)?;
Ok(RegionWithLeader::new(region, resp.leader.take()))
}
// A node-like thing that can be connected to.
#[async_trait]
trait Reconnect {
type Cl;
async fn reconnect(&self, interval_sec: u64) -> Result<()>;
}
#[async_trait]
impl Reconnect for RetryClient<Cluster> {
type Cl = Cluster;
async fn reconnect(&self, interval_sec: u64) -> Result<()> {
let reconnect_begin = Instant::now();
let mut lock = self.cluster.write().await;
let (cluster, last_connected) = &mut *lock;
// If `last_connected + interval_sec` is larger or equal than reconnect_begin,
// a concurrent reconnect is just succeed when this thread trying to get write lock
let should_connect = reconnect_begin > *last_connected + Duration::from_secs(interval_sec);
if should_connect {
self.connection.reconnect(cluster, &self.config).await?;
*last_connected = Instant::now();
}
Ok(())
}
}
#[cfg(test)]
mod test {
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Mutex;
use futures::executor;
use futures::future::ready;
use super::*;
use crate::internal_err;
#[tokio::test(flavor = "multi_thread")]
async fn test_reconnect() {
struct MockClient {
reconnect_count: AtomicUsize,
cluster: RwLock<((), Instant)>,
}
#[async_trait]
impl Reconnect for MockClient {
type Cl = ();
async fn reconnect(&self, _: u64) -> Result<()> {
self.reconnect_count
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
// Not actually unimplemented, we just don't care about the error.
Err(Error::Unimplemented)
}
}
async fn retry_err(client: Arc<MockClient>) -> Result<()> {
retry!(client, "test", |_c| ready(Err(internal_err!("whoops"))))
}
async fn retry_ok(client: Arc<MockClient>) -> Result<()> {
retry!(client, "test", |_c| ready(Ok::<_, Error>(())))
}
executor::block_on(async {
let client = Arc::new(MockClient {
reconnect_count: AtomicUsize::new(0),
cluster: RwLock::new(((), Instant::now())),
});
assert!(retry_err(client.clone()).await.is_err());
assert_eq!(
client
.reconnect_count
.load(std::sync::atomic::Ordering::SeqCst),
MAX_REQUEST_COUNT
);
client
.reconnect_count
.store(0, std::sync::atomic::Ordering::SeqCst);
assert!(retry_ok(client.clone()).await.is_ok());
assert_eq!(
client
.reconnect_count
.load(std::sync::atomic::Ordering::SeqCst),
0
);
})
}
#[test]
fn test_retry() {
struct MockClient {
cluster: RwLock<(AtomicUsize, Instant)>,
}
#[async_trait]
impl Reconnect for MockClient {
type Cl = Mutex<usize>;
async fn reconnect(&self, _: u64) -> Result<()> {
Ok(())
}
}
async fn retry_max_err(
client: Arc<MockClient>,
max_retries: Arc<AtomicUsize>,
) -> Result<()> {
retry!(client, "test", |c| {
c.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
let max_retries = max_retries.fetch_sub(1, Ordering::SeqCst) - 1;
if max_retries == 0 {
ready(Ok(()))
} else {
ready(Err(internal_err!("whoops")))
}
})
}
async fn retry_max_ok(
client: Arc<MockClient>,
max_retries: Arc<AtomicUsize>,
) -> Result<()> {
retry!(client, "test", |c| {
c.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
let max_retries = max_retries.fetch_sub(1, Ordering::SeqCst) - 1;
if max_retries == 0 {
ready(Ok(()))
} else {
ready(Err(internal_err!("whoops")))
}
})
}
executor::block_on(async {
let client = Arc::new(MockClient {
cluster: RwLock::new((AtomicUsize::new(0), Instant::now())),
});
let max_retries = Arc::new(AtomicUsize::new(1000));
assert!(retry_max_err(client.clone(), max_retries).await.is_err());
assert_eq!(
client.cluster.read().await.0.load(Ordering::SeqCst),
LEADER_CHANGE_RETRY
);
let client = Arc::new(MockClient {
cluster: RwLock::new((AtomicUsize::new(0), Instant::now())),
});
let max_retries = Arc::new(AtomicUsize::new(2));
assert!(retry_max_ok(client.clone(), max_retries).await.is_ok());
assert_eq!(client.cluster.read().await.0.load(Ordering::SeqCst), 2);
})
}
}