@@ -16,6 +16,7 @@ use tonic::Request;
16
16
use super :: timestamp:: TimestampOracle ;
17
17
use crate :: internal_err;
18
18
use crate :: proto:: pdpb;
19
+ use crate :: Error ;
19
20
use crate :: Result ;
20
21
use crate :: SecurityManager ;
21
22
use crate :: Timestamp ;
@@ -24,6 +25,7 @@ use crate::Timestamp;
24
25
pub struct Cluster {
25
26
id : u64 ,
26
27
client : pdpb:: pd_client:: PdClient < Channel > ,
28
+ endpoint : String ,
27
29
members : pdpb:: GetMembersResponse ,
28
30
tso : TimestampOracle ,
29
31
}
@@ -91,6 +93,18 @@ impl Cluster {
91
93
req. safe_point = safepoint;
92
94
req. send ( & mut self . client , timeout) . await
93
95
}
96
+
97
+ pub async fn get_keyspace_id ( & self , keyspace : & str ) -> Result < u32 > {
98
+ let resp =
99
+ reqwest:: get ( format ! ( "{}/pd/api/v2/keyspaces/{keyspace}" , self . endpoint) ) . await ?;
100
+ let body = resp. json :: < serde_json:: Value > ( ) . await ?;
101
+ let keyspace_id = body
102
+ . get ( "id" )
103
+ . ok_or_else ( || Error :: UnknownHttpRespond ( body. to_string ( ) ) ) ?
104
+ . as_u64 ( )
105
+ . ok_or_else ( || Error :: UnknownHttpRespond ( body. to_string ( ) ) ) ?;
106
+ Ok ( keyspace_id as u32 )
107
+ }
94
108
}
95
109
96
110
/// An object for connecting and reconnecting to a PD cluster.
@@ -109,12 +123,13 @@ impl Connection {
109
123
timeout : Duration ,
110
124
) -> Result < Cluster > {
111
125
let members = self . validate_endpoints ( endpoints, timeout) . await ?;
112
- let ( client, members) = self . try_connect_leader ( & members, timeout) . await ?;
126
+ let ( client, endpoint , members) = self . try_connect_leader ( & members, timeout) . await ?;
113
127
let id = members. header . as_ref ( ) . unwrap ( ) . cluster_id ;
114
128
let tso = TimestampOracle :: new ( id, & client) ?;
115
129
let cluster = Cluster {
116
130
id,
117
131
client,
132
+ endpoint,
118
133
members,
119
134
tso,
120
135
} ;
@@ -125,11 +140,13 @@ impl Connection {
125
140
pub async fn reconnect ( & self , cluster : & mut Cluster , timeout : Duration ) -> Result < ( ) > {
126
141
warn ! ( "updating pd client" ) ;
127
142
let start = Instant :: now ( ) ;
128
- let ( client, members) = self . try_connect_leader ( & cluster. members , timeout) . await ?;
143
+ let ( client, endpoint, members) =
144
+ self . try_connect_leader ( & cluster. members , timeout) . await ?;
129
145
let tso = TimestampOracle :: new ( cluster. id , & client) ?;
130
146
* cluster = Cluster {
131
147
id : cluster. id ,
132
148
client,
149
+ endpoint,
133
150
members,
134
151
tso,
135
152
} ;
@@ -239,7 +256,11 @@ impl Connection {
239
256
& self ,
240
257
previous : & pdpb:: GetMembersResponse ,
241
258
timeout : Duration ,
242
- ) -> Result < ( pdpb:: pd_client:: PdClient < Channel > , pdpb:: GetMembersResponse ) > {
259
+ ) -> Result < (
260
+ pdpb:: pd_client:: PdClient < Channel > ,
261
+ String ,
262
+ pdpb:: GetMembersResponse ,
263
+ ) > {
243
264
let previous_leader = previous. leader . as_ref ( ) . unwrap ( ) ;
244
265
let members = & previous. members ;
245
266
let cluster_id = previous. header . as_ref ( ) . unwrap ( ) . cluster_id ;
@@ -269,9 +290,10 @@ impl Connection {
269
290
if let Some ( resp) = resp {
270
291
let leader = resp. leader . as_ref ( ) . unwrap ( ) ;
271
292
for ep in & leader. client_urls {
272
- let r = self . try_connect ( ep. as_str ( ) , cluster_id, timeout) . await ;
273
- if r. is_ok ( ) {
274
- return r;
293
+ if let Ok ( ( client, members) ) =
294
+ self . try_connect ( ep. as_str ( ) , cluster_id, timeout) . await
295
+ {
296
+ return Ok ( ( client, ep. to_string ( ) , members) ) ;
275
297
}
276
298
}
277
299
}
0 commit comments