12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- use std:: collections:: BTreeMap ;
16
15
use std:: collections:: BTreeSet ;
17
16
use std:: net:: Ipv4Addr ;
18
17
use std:: sync:: atomic:: AtomicI32 ;
@@ -32,7 +31,6 @@ use databend_common_grpc::DNSResolver;
32
31
use databend_common_meta_client:: reply_to_api_result;
33
32
use databend_common_meta_client:: RequestFor ;
34
33
use databend_common_meta_raft_store:: config:: RaftConfig ;
35
- use databend_common_meta_raft_store:: ondisk:: DataVersion ;
36
34
use databend_common_meta_raft_store:: ondisk:: DATA_VERSION ;
37
35
use databend_common_meta_sled_store:: openraft;
38
36
use databend_common_meta_sled_store:: openraft:: ChangeMembers ;
@@ -83,6 +81,7 @@ use crate::message::LeaveRequest;
83
81
use crate :: meta_service:: errors:: grpc_error_to_network_err;
84
82
use crate :: meta_service:: forwarder:: MetaForwarder ;
85
83
use crate :: meta_service:: meta_leader:: MetaLeader ;
84
+ use crate :: meta_service:: meta_node_status:: MetaNodeStatus ;
86
85
use crate :: meta_service:: RaftServiceImpl ;
87
86
use crate :: metrics:: server_metrics;
88
87
use crate :: network:: NetworkFactory ;
@@ -97,66 +96,6 @@ use crate::watcher::Watcher;
97
96
use crate :: watcher:: WatcherSender ;
98
97
use crate :: Opened ;
99
98
100
- #[ derive( serde:: Serialize ) ]
101
- pub struct MetaNodeStatus {
102
- pub id : NodeId ,
103
-
104
- /// The build version of meta-service binary.
105
- pub binary_version : String ,
106
-
107
- /// The version of the data this meta-service is serving.
108
- pub data_version : DataVersion ,
109
-
110
- /// The raft service endpoint for internal communication
111
- pub endpoint : String ,
112
-
113
- /// The size in bytes of the on disk data.
114
- pub db_size : u64 ,
115
-
116
- /// key number of current snapshot
117
- pub key_num : u64 ,
118
-
119
- /// Server state, one of "Follower", "Learner", "Candidate", "Leader".
120
- pub state : String ,
121
-
122
- /// Is this node a leader.
123
- pub is_leader : bool ,
124
-
125
- /// Current term.
126
- pub current_term : u64 ,
127
-
128
- /// Last received log index
129
- pub last_log_index : u64 ,
130
-
131
- /// Last log id that has been committed and applied to state machine.
132
- pub last_applied : LogId ,
133
-
134
- /// The last log id contained in the last built snapshot.
135
- pub snapshot_last_log_id : Option < LogId > ,
136
-
137
- /// The last log id that has been purged, inclusive.
138
- pub purged : Option < LogId > ,
139
-
140
- /// The last known leader node.
141
- pub leader : Option < Node > ,
142
-
143
- /// The replication state of all nodes.
144
- ///
145
- /// Only leader node has non-None data for this field, i.e., `is_leader` is true.
146
- pub replication : Option < BTreeMap < NodeId , Option < LogId > > > ,
147
-
148
- /// Nodes that can vote in election can grant replication.
149
- pub voters : Vec < Node > ,
150
-
151
- /// Also known as `learner`s.
152
- pub non_voters : Vec < Node > ,
153
-
154
- /// The last `seq` used by GenericKV sub tree.
155
- ///
156
- /// `seq` is a monotonically incremental integer for every value that is inserted or updated.
157
- pub last_seq : u64 ,
158
- }
159
-
160
99
pub type LogStore = RaftStore ;
161
100
pub type SMStore = RaftStore ;
162
101
@@ -184,8 +123,7 @@ pub struct MetaNodeBuilder {
184
123
node_id : Option < NodeId > ,
185
124
raft_config : Option < Config > ,
186
125
sto : Option < RaftStore > ,
187
- monitor_metrics : bool ,
188
- endpoint : Option < Endpoint > ,
126
+ raft_service_endpoint : Option < Endpoint > ,
189
127
}
190
128
191
129
impl MetaNodeBuilder {
@@ -212,7 +150,6 @@ impl MetaNodeBuilder {
212
150
let raft = MetaRaft :: new ( node_id, Arc :: new ( config) , net, log_store, sm_store)
213
151
. await
214
152
. map_err ( |e| MetaStartupError :: MetaServiceError ( e. to_string ( ) ) ) ?;
215
- let metrics_rx = raft. metrics ( ) ;
216
153
217
154
let ( tx, rx) = watch:: channel :: < ( ) > ( ( ) ) ;
218
155
@@ -222,22 +159,19 @@ impl MetaNodeBuilder {
222
159
. await
223
160
. set_subscriber ( Box :: new ( DispatcherSender ( dispatcher_tx. clone ( ) ) ) ) ;
224
161
225
- let mn = Arc :: new ( MetaNode {
162
+ let meta_node = Arc :: new ( MetaNode {
226
163
sto : sto. clone ( ) ,
227
164
dispatcher_handle : EventDispatcherHandle :: new ( dispatcher_tx) ,
228
- raft,
165
+ raft : raft . clone ( ) ,
229
166
running_tx : tx,
230
167
running_rx : rx,
231
168
join_handles : Mutex :: new ( Vec :: new ( ) ) ,
232
169
joined_tasks : AtomicI32 :: new ( 1 ) ,
233
170
} ) ;
234
171
235
- if self . monitor_metrics {
236
- info ! ( "about to subscribe raft metrics" ) ;
237
- MetaNode :: subscribe_metrics ( mn. clone ( ) , metrics_rx) . await ;
238
- }
172
+ MetaNode :: subscribe_metrics ( meta_node. clone ( ) , raft. metrics ( ) ) . await ;
239
173
240
- let endpoint = if let Some ( a) = self . endpoint . take ( ) {
174
+ let endpoint = if let Some ( a) = self . raft_service_endpoint . take ( ) {
241
175
a
242
176
} else {
243
177
sto. get_node_raft_endpoint ( & node_id) . await . map_err ( |e| {
@@ -248,11 +182,9 @@ impl MetaNodeBuilder {
248
182
} ) ?
249
183
} ;
250
184
251
- info ! ( "about to start raft grpc on endpoint {}" , endpoint) ;
185
+ MetaNode :: start_raft_service ( meta_node . clone ( ) , & endpoint) . await ? ;
252
186
253
- MetaNode :: start_grpc ( mn. clone ( ) , endpoint. addr ( ) , endpoint. port ( ) ) . await ?;
254
-
255
- Ok ( mn)
187
+ Ok ( meta_node)
256
188
}
257
189
258
190
#[ must_use]
@@ -268,14 +200,8 @@ impl MetaNodeBuilder {
268
200
}
269
201
270
202
#[ must_use]
271
- pub fn endpoint ( mut self , a : Endpoint ) -> Self {
272
- self . endpoint = Some ( a) ;
273
- self
274
- }
275
-
276
- #[ must_use]
277
- pub fn monitor_metrics ( mut self , b : bool ) -> Self {
278
- self . monitor_metrics = b;
203
+ pub fn raft_service_endpoint ( mut self , endpoint : Endpoint ) -> Self {
204
+ self . raft_service_endpoint = Some ( endpoint) ;
279
205
self
280
206
}
281
207
}
@@ -288,8 +214,7 @@ impl MetaNode {
288
214
node_id : None ,
289
215
raft_config : Some ( raft_config) ,
290
216
sto : None ,
291
- monitor_metrics : true ,
292
- endpoint : None ,
217
+ raft_service_endpoint : None ,
293
218
}
294
219
}
295
220
@@ -315,20 +240,24 @@ impl MetaNode {
315
240
316
241
/// Start the grpc service for raft communication and meta operation API.
317
242
#[ fastrace:: trace]
318
- pub async fn start_grpc (
319
- mn : Arc < MetaNode > ,
320
- host : & str ,
321
- port : u16 ,
243
+ pub async fn start_raft_service (
244
+ meta_node : Arc < MetaNode > ,
245
+ endpoint : & Endpoint ,
322
246
) -> Result < ( ) , MetaNetworkError > {
323
- let mut rx = mn. running_rx . clone ( ) ;
247
+ info ! ( "Start raft service listening on: {}" , endpoint) ;
248
+
249
+ let host = endpoint. addr ( ) ;
250
+ let port = endpoint. port ( ) ;
251
+
252
+ let mut running_rx = meta_node. running_rx . clone ( ) ;
324
253
325
- let meta_srv_impl = RaftServiceImpl :: create ( mn . clone ( ) ) ;
326
- let meta_srv = RaftServiceServer :: new ( meta_srv_impl )
254
+ let raft_service_impl = RaftServiceImpl :: create ( meta_node . clone ( ) ) ;
255
+ let raft_server = RaftServiceServer :: new ( raft_service_impl )
327
256
. max_decoding_message_size ( GrpcConfig :: MAX_DECODING_SIZE )
328
257
. max_encoding_message_size ( GrpcConfig :: MAX_ENCODING_SIZE ) ;
329
258
330
259
let ipv4_addr = host. parse :: < Ipv4Addr > ( ) ;
331
- let addr = match ipv4_addr {
260
+ let ip_port = match ipv4_addr {
332
261
Ok ( addr) => format ! ( "{}:{}" , addr, port) ,
333
262
Err ( _) => {
334
263
let resolver = DNSResolver :: instance ( ) . map_err ( |e| {
@@ -347,37 +276,30 @@ impl MetaNode {
347
276
}
348
277
} ;
349
278
350
- info ! ( "about to start raft grpc on resolved addr {}" , addr ) ;
279
+ info ! ( "about to start raft grpc on: {}" , ip_port ) ;
351
280
352
- let addr_str = addr. to_string ( ) ;
353
- let ret = addr. parse :: < std:: net:: SocketAddr > ( ) ;
354
- let addr = match ret {
355
- Ok ( addr) => addr,
356
- Err ( e) => {
357
- return Err ( e. into ( ) ) ;
358
- }
359
- } ;
360
- let node_id = mn. sto . id ;
281
+ let socket_addr = ip_port. parse :: < std:: net:: SocketAddr > ( ) ?;
282
+ let node_id = meta_node. sto . id ;
361
283
362
- let srv = tonic:: transport:: Server :: builder ( ) . add_service ( meta_srv ) ;
284
+ let srv = tonic:: transport:: Server :: builder ( ) . add_service ( raft_server ) ;
363
285
364
286
let h = databend_common_base:: runtime:: spawn ( async move {
365
- srv. serve_with_shutdown ( addr , async move {
366
- let _ = rx . changed ( ) . await ;
287
+ srv. serve_with_shutdown ( socket_addr , async move {
288
+ let _ = running_rx . changed ( ) . await ;
367
289
info ! (
368
290
"signal received, shutting down: id={} {} " ,
369
- node_id, addr_str
291
+ node_id, ip_port
370
292
) ;
371
293
} )
372
294
. await
373
295
. map_err ( |e| {
374
- AnyError :: new ( & e) . add_context ( || "when serving meta-service grpc service" )
296
+ AnyError :: new ( & e) . add_context ( || "when serving meta-service raft service" )
375
297
} ) ?;
376
298
377
299
Ok :: < ( ) , AnyError > ( ( ) )
378
300
} ) ;
379
301
380
- let mut jh = mn . join_handles . lock ( ) . await ;
302
+ let mut jh = meta_node . join_handles . lock ( ) . await ;
381
303
jh. push ( h) ;
382
304
Ok ( ( ) )
383
305
}
@@ -415,7 +337,7 @@ impl MetaNode {
415
337
let builder = MetaNode :: builder ( & config)
416
338
. sto ( sto. clone ( ) )
417
339
. node_id ( self_node_id)
418
- . endpoint ( config. raft_api_listen_host_endpoint ( ) ) ;
340
+ . raft_service_endpoint ( config. raft_api_listen_host_endpoint ( ) ) ;
419
341
let mn = builder. build ( ) . await ?;
420
342
421
343
info ! ( "MetaNode started: {:?}" , config) ;
@@ -489,6 +411,7 @@ impl MetaNode {
489
411
490
412
/// Spawn a monitor to watch raft state changes and report metrics changes.
491
413
pub async fn subscribe_metrics ( mn : Arc < Self > , mut metrics_rx : watch:: Receiver < RaftMetrics > ) {
414
+ info ! ( "Start a task subscribing raft metrics and forward to metrics API" ) ;
492
415
let meta_node = mn. clone ( ) ;
493
416
494
417
let fut = async move {
@@ -971,7 +894,7 @@ impl MetaNode {
971
894
972
895
#[ fastrace:: trace]
973
896
pub async fn get_grpc_advertise_addrs ( & self ) -> Vec < String > {
974
- // inconsistent get: from local state machine
897
+ // Maybe stale get: from local state machine
975
898
976
899
let nodes = {
977
900
let sm = self . sto . state_machine . read ( ) . await ;
0 commit comments