1
1
mod ctl;
2
2
3
3
use futures_timer:: Delay ;
4
- use log:: info;
4
+ use log:: { info, warn } ;
5
5
use std:: { env, time:: Duration } ;
6
6
use tikv_client:: { ColumnFamily , Key , RawClient , Result , TransactionClient } ;
7
7
8
8
const ENV_PD_ADDRS : & str = "PD_ADDRS" ;
9
9
const ENV_ENABLE_MULIT_REGION : & str = "MULTI_REGION" ;
10
+ const REGION_SPLIT_TIME_LIMIT : Duration = Duration :: from_secs ( 15 ) ;
10
11
11
12
// Delete all entries in TiKV to leave a clean space for following tests.
12
13
pub async fn clear_tikv ( ) {
@@ -24,6 +25,11 @@ pub async fn clear_tikv() {
24
25
// To test with multiple regions, prewrite some data. Tests that hope to test
25
26
// with multiple regions should use keys in the corresponding ranges.
26
27
pub async fn init ( ) -> Result < ( ) > {
28
+ // ignore SetLoggerError
29
+ let _ = simple_logger:: SimpleLogger :: new ( )
30
+ . with_level ( log:: LevelFilter :: Warn )
31
+ . init ( ) ;
32
+
27
33
if enable_multi_region ( ) {
28
34
// 1000 keys: 0..1000
29
35
let keys_1 = std:: iter:: successors ( Some ( 0u32 ) , |x| Some ( x + 1 ) )
@@ -36,14 +42,14 @@ pub async fn init() -> Result<()> {
36
42
. take ( count as usize - 1 )
37
43
. map ( |x| x. to_be_bytes ( ) . to_vec ( ) ) ;
38
44
39
- ensure_region_splitted ( keys_1. chain ( keys_2) , 100 ) . await ?;
45
+ ensure_region_split ( keys_1. chain ( keys_2) , 100 ) . await ?;
40
46
}
41
47
42
48
clear_tikv ( ) . await ;
43
49
Ok ( ( ) )
44
50
}
45
51
46
- async fn ensure_region_splitted (
52
+ async fn ensure_region_split (
47
53
keys : impl IntoIterator < Item = impl Into < Key > > ,
48
54
region_count : u32 ,
49
55
) -> Result < ( ) > {
@@ -52,7 +58,7 @@ async fn ensure_region_splitted(
52
58
}
53
59
54
60
// 1. write plenty transactional keys
55
- // 2. wait until regions splitted
61
+ // 2. wait until regions split
56
62
57
63
let client = TransactionClient :: new ( pd_addrs ( ) ) . await ?;
58
64
let mut txn = client. begin_optimistic ( ) . await ?;
@@ -65,11 +71,16 @@ async fn ensure_region_splitted(
65
71
txn. commit ( ) . await ?;
66
72
67
73
info ! ( "splitting regions..." ) ;
74
+ let start_time = std:: time:: Instant :: now ( ) ;
68
75
loop {
69
76
if ctl:: get_region_count ( ) . await ? as u32 >= region_count {
70
77
break ;
71
78
}
72
- Delay :: new ( Duration :: from_secs ( 1 ) ) . await ;
79
+ if start_time. elapsed ( ) > REGION_SPLIT_TIME_LIMIT {
80
+ warn ! ( "Stop splitting regions: time limit exceeded" ) ;
81
+ break ;
82
+ }
83
+ Delay :: new ( Duration :: from_millis ( 200 ) ) . await ;
73
84
}
74
85
75
86
Ok ( ( ) )
0 commit comments