forked from apache/horaedb
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcluster_impl.rs
407 lines (353 loc) · 12.4 KB
/
cluster_impl.rs
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Copyright 2022-2023 CeresDB Project Authors. Licensed under Apache-2.0.
use std::{
sync::{Arc, Mutex, RwLock},
time::Duration,
};
use async_trait::async_trait;
use common_types::table::ShardId;
use etcd_client::ConnectOptions;
use log::{error, info, warn};
use meta_client::{
types::{
GetNodesRequest, GetTablesOfShardsRequest, RouteTablesRequest, RouteTablesResponse,
ShardInfo,
},
MetaClientRef,
};
use runtime::{JoinHandle, Runtime};
use snafu::{ensure, OptionExt, ResultExt};
use tokio::{
sync::mpsc::{self, Sender},
time,
};
use crate::{
config::ClusterConfig,
shard_lock_manager::{ShardLockManager, ShardLockManagerRef},
shard_set::{Shard, ShardRef, ShardSet},
topology::ClusterTopology,
Cluster, ClusterNodesNotFound, ClusterNodesResp, EtcdClientFailureWithCause, InvalidArguments,
MetaClientFailure, OpenShard, Result, ShardNotFound,
};
/// ClusterImpl is an implementation of [`Cluster`] based [`MetaClient`].
///
/// Its functions are to:
/// - Handle the some action from the CeresMeta;
/// - Handle the heartbeat between ceresdb-server and CeresMeta;
/// - Provide the cluster topology.
pub struct ClusterImpl {
inner: Arc<Inner>,
runtime: Arc<Runtime>,
config: ClusterConfig,
heartbeat_handle: Mutex<Option<JoinHandle<()>>>,
stop_heartbeat_tx: Mutex<Option<Sender<()>>>,
shard_lock_manager: ShardLockManagerRef,
}
impl ClusterImpl {
pub async fn try_new(
node_name: String,
shard_set: ShardSet,
meta_client: MetaClientRef,
config: ClusterConfig,
runtime: Arc<Runtime>,
) -> Result<Self> {
if let Err(e) = config.etcd_client.validate() {
return InvalidArguments { msg: e }.fail();
}
let inner = Arc::new(Inner::new(shard_set, meta_client)?);
let connect_options = ConnectOptions::from(&config.etcd_client);
let etcd_client =
etcd_client::Client::connect(&config.etcd_client.server_addrs, Some(connect_options))
.await
.context(EtcdClientFailureWithCause {
msg: "failed to connect to etcd",
})?;
let shard_lock_key_prefix = Self::shard_lock_key_prefix(
&config.etcd_client.root_path,
&config.meta_client.cluster_name,
)?;
let shard_lock_manager = ShardLockManager::new(
shard_lock_key_prefix,
node_name,
etcd_client,
config.etcd_client.shard_lock_lease_ttl_sec,
config.etcd_client.shard_lock_lease_check_interval.0,
config.etcd_client.rpc_timeout(),
runtime.clone(),
);
Ok(Self {
inner,
runtime,
config,
heartbeat_handle: Mutex::new(None),
stop_heartbeat_tx: Mutex::new(None),
shard_lock_manager: Arc::new(shard_lock_manager),
})
}
fn start_heartbeat_loop(&self) {
let interval = self.heartbeat_interval();
let error_wait_lease = self.error_wait_lease();
let inner = self.inner.clone();
let (tx, mut rx) = mpsc::channel(1);
let handle = self.runtime.spawn(async move {
loop {
let shard_infos = inner
.shard_set
.all_shards()
.iter()
.map(|shard| shard.shard_info())
.collect();
info!("Node heartbeat to meta, shard infos:{:?}", shard_infos);
let resp = inner.meta_client.send_heartbeat(shard_infos).await;
let wait = match resp {
Ok(()) => interval,
Err(e) => {
error!("Send heartbeat to meta failed, err:{}", e);
error_wait_lease
}
};
if time::timeout(wait, rx.recv()).await.is_ok() {
warn!("Receive exit command and exit heartbeat loop");
break;
}
}
});
*self.stop_heartbeat_tx.lock().unwrap() = Some(tx);
*self.heartbeat_handle.lock().unwrap() = Some(handle);
}
// Register node every 2/3 lease
fn heartbeat_interval(&self) -> Duration {
Duration::from_millis(self.config.meta_client.lease.as_millis() * 2 / 3)
}
fn error_wait_lease(&self) -> Duration {
self.config.meta_client.lease.0 / 2
}
fn shard_lock_key_prefix(root_path: &str, cluster_name: &str) -> Result<String> {
ensure!(
root_path.starts_with('/'),
InvalidArguments {
msg: "root_path is required to start with /",
}
);
ensure!(
!cluster_name.is_empty(),
InvalidArguments {
msg: "cluster_name is required non-empty",
}
);
const SHARD_LOCK_KEY: &str = "shards";
Ok(format!("{root_path}/{cluster_name}/{SHARD_LOCK_KEY}"))
}
}
struct Inner {
shard_set: ShardSet,
meta_client: MetaClientRef,
topology: RwLock<ClusterTopology>,
}
impl Inner {
fn new(shard_set: ShardSet, meta_client: MetaClientRef) -> Result<Self> {
Ok(Self {
shard_set,
meta_client,
topology: Default::default(),
})
}
async fn route_tables(&self, req: &RouteTablesRequest) -> Result<RouteTablesResponse> {
// TODO: we should use self.topology to cache the route result to reduce the
// pressure on the CeresMeta.
let route_resp = self
.meta_client
.route_tables(req.clone())
.await
.context(MetaClientFailure)?;
Ok(route_resp)
}
async fn fetch_nodes(&self) -> Result<ClusterNodesResp> {
{
let topology = self.topology.read().unwrap();
let cached_node_topology = topology.nodes();
if let Some(cached_node_topology) = cached_node_topology {
return Ok(ClusterNodesResp {
cluster_topology_version: cached_node_topology.version,
cluster_nodes: cached_node_topology.nodes,
});
}
}
let req = GetNodesRequest::default();
let resp = self
.meta_client
.get_nodes(req)
.await
.context(MetaClientFailure)?;
let version = resp.cluster_topology_version;
let nodes = Arc::new(resp.node_shards);
let updated = self
.topology
.write()
.unwrap()
.maybe_update_nodes(nodes.clone(), version);
let resp = if updated {
ClusterNodesResp {
cluster_topology_version: version,
cluster_nodes: nodes,
}
} else {
let topology = self.topology.read().unwrap();
// The fetched topology is outdated, and we will use the cache.
let cached_node_topology =
topology.nodes().context(ClusterNodesNotFound { version })?;
ClusterNodesResp {
cluster_topology_version: cached_node_topology.version,
cluster_nodes: cached_node_topology.nodes,
}
};
Ok(resp)
}
async fn open_shard(&self, shard_info: &ShardInfo) -> Result<ShardRef> {
if let Some(shard) = self.shard_set.get(shard_info.id) {
let cur_shard_info = shard.shard_info();
if cur_shard_info.version == shard_info.version {
info!(
"No need to open the exactly same shard again, shard_info:{:?}",
shard_info
);
return Ok(shard);
}
ensure!(
cur_shard_info.version < shard_info.version,
OpenShard {
shard_id: shard_info.id,
msg: format!("open a shard with a smaller version, curr_shard_info:{cur_shard_info:?}, new_shard_info:{shard_info:?}"),
}
);
}
let resp = future_ext::retry_async(
|| {
let req = GetTablesOfShardsRequest {
shard_ids: vec![shard_info.id],
};
async {
self.meta_client
.get_tables_of_shards(req)
.await
.map_err(|e| {
error!("Get tables of shard failed, err:{e}");
e
})
}
},
// TODO: configure retry
&future_ext::RetryConfig {
max_retries: 10,
interval: Duration::from_secs(5),
},
)
.await;
let mut resp = match resp {
Ok(v) => v,
Err(_e) => panic!("Release shard lock failed and we have to panic otherwise this shard wont be assigned again."),
};
ensure!(
resp.tables_by_shard.len() == 1,
OpenShard {
shard_id: shard_info.id,
msg: "expect only one shard tables"
}
);
let tables_of_shard = resp
.tables_by_shard
.remove(&shard_info.id)
.context(OpenShard {
shard_id: shard_info.id,
msg: "shard tables are missing from the response",
})?;
let shard_id = tables_of_shard.shard_info.id;
let shard = Arc::new(Shard::new(tables_of_shard));
self.shard_set.insert(shard_id, shard.clone());
Ok(shard)
}
fn shard(&self, shard_id: ShardId) -> Option<ShardRef> {
self.shard_set.get(shard_id)
}
fn close_shard(&self, shard_id: ShardId) -> Result<ShardRef> {
self.shard_set
.remove(shard_id)
.with_context(|| ShardNotFound {
msg: format!("close non-existent shard, shard_id:{shard_id}"),
})
}
fn list_shards(&self) -> Vec<ShardInfo> {
let shards = self.shard_set.all_shards();
shards.iter().map(|shard| shard.shard_info()).collect()
}
}
#[async_trait]
impl Cluster for ClusterImpl {
async fn start(&self) -> Result<()> {
info!("Cluster is starting with config:{:?}", self.config);
// start the background loop for sending heartbeat.
self.start_heartbeat_loop();
info!("Cluster has started");
Ok(())
}
async fn stop(&self) -> Result<()> {
info!("Cluster is stopping");
{
let tx = self.stop_heartbeat_tx.lock().unwrap().take();
if let Some(tx) = tx {
let _ = tx.send(()).await;
}
}
{
let handle = self.heartbeat_handle.lock().unwrap().take();
if let Some(handle) = handle {
let _ = handle.await;
}
}
info!("Cluster has stopped");
Ok(())
}
async fn open_shard(&self, shard_info: &ShardInfo) -> Result<ShardRef> {
self.inner.open_shard(shard_info).await
}
fn shard(&self, shard_id: ShardId) -> Option<ShardRef> {
self.inner.shard(shard_id)
}
async fn close_shard(&self, shard_id: ShardId) -> Result<ShardRef> {
self.inner.close_shard(shard_id)
}
fn list_shards(&self) -> Vec<ShardInfo> {
self.inner.list_shards()
}
async fn route_tables(&self, req: &RouteTablesRequest) -> Result<RouteTablesResponse> {
self.inner.route_tables(req).await
}
async fn fetch_nodes(&self) -> Result<ClusterNodesResp> {
self.inner.fetch_nodes().await
}
fn shard_lock_manager(&self) -> ShardLockManagerRef {
self.shard_lock_manager.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_format_shard_lock_key_prefix() {
let cases = vec![
(
("/ceresdb", "defaultCluster"),
Some("/ceresdb/defaultCluster/shards"),
),
(("", "defaultCluster"), None),
(("vvv", "defaultCluster"), None),
(("/x", ""), None),
];
for ((root_path, cluster_name), expected) in cases {
let actual = ClusterImpl::shard_lock_key_prefix(root_path, cluster_name);
match expected {
Some(expected) => assert_eq!(actual.unwrap(), expected),
None => assert!(actual.is_err()),
}
}
}
}