@@ -55,38 +55,38 @@ use url::Url;
55
55
56
56
use crate :: reading;
57
57
use crate :: upgrade;
58
- use crate :: Config ;
58
+ use crate :: ImportArgs ;
59
59
60
- pub async fn import_data ( config : & Config ) -> anyhow:: Result < ( ) > {
61
- let raft_dir = config . raft_dir . clone ( ) . unwrap_or_default ( ) ;
60
+ pub async fn import_data ( args : & ImportArgs ) -> anyhow:: Result < ( ) > {
61
+ let raft_dir = args . raft_dir . clone ( ) . unwrap_or_default ( ) ;
62
62
63
63
eprintln ! ( ) ;
64
64
eprintln ! ( "Import:" ) ;
65
65
eprintln ! ( " Into Meta Dir: '{}'" , raft_dir) ;
66
- eprintln ! ( " Initialize Cluster with Id: {}, cluster: {{" , config . id) ;
67
- for peer in config . initial_cluster . clone ( ) {
66
+ eprintln ! ( " Initialize Cluster with Id: {}, cluster: {{" , args . id) ;
67
+ for peer in args . initial_cluster . clone ( ) {
68
68
eprintln ! ( " Peer: {}" , peer) ;
69
69
}
70
70
eprintln ! ( " }}" ) ;
71
71
72
- let nodes = build_nodes ( config . initial_cluster . clone ( ) , config . id ) ?;
72
+ let nodes = build_nodes ( args . initial_cluster . clone ( ) , args . id ) ?;
73
73
74
74
init_sled_db ( raft_dir. clone ( ) , 64 * 1024 * 1024 * 1024 ) ;
75
75
76
- clear ( config ) ?;
77
- let max_log_id = import_from_stdin_or_file ( config ) . await ?;
76
+ clear ( args ) ?;
77
+ let max_log_id = import_from_stdin_or_file ( args ) . await ?;
78
78
79
- if config . initial_cluster . is_empty ( ) {
79
+ if args . initial_cluster . is_empty ( ) {
80
80
return Ok ( ( ) ) ;
81
81
}
82
82
83
- init_new_cluster ( config , nodes, max_log_id, config . id ) . await ?;
83
+ init_new_cluster ( args , nodes, max_log_id) . await ?;
84
84
Ok ( ( ) )
85
85
}
86
86
87
87
/// Import from lines of exported data and Return the max log id that is found.
88
88
async fn import_lines < B : BufRead + ' static > (
89
- config : & Config ,
89
+ raft_config : RaftConfig ,
90
90
lines : Lines < B > ,
91
91
) -> anyhow:: Result < Option < LogId > > {
92
92
#[ allow( clippy:: useless_conversion) ]
@@ -106,8 +106,8 @@ async fn import_lines<B: BufRead + 'static>(
106
106
please use an older version databend-metactl to import from V001"
107
107
) ) ;
108
108
}
109
- DataVersion :: V002 => import_v002 ( config , it) . await ?,
110
- DataVersion :: V003 => import_v003 ( config , it) . await ?,
109
+ DataVersion :: V002 => import_v002 ( raft_config , it) . await ?,
110
+ DataVersion :: V003 => import_v003 ( raft_config , it) . await ?,
111
111
} ;
112
112
113
113
Ok ( max_log_id)
@@ -119,11 +119,11 @@ async fn import_lines<B: BufRead + 'static>(
119
119
///
120
120
/// It write logs and related entries to sled trees, and state_machine entries to a snapshot.
121
121
async fn import_v002 (
122
- config : & Config ,
122
+ raft_config : RaftConfig ,
123
123
lines : impl IntoIterator < Item = Result < String , io:: Error > > ,
124
124
) -> anyhow:: Result < Option < LogId > > {
125
125
// v002 and v003 share the same exported data format.
126
- import_v003 ( config , lines) . await
126
+ import_v003 ( raft_config , lines) . await
127
127
}
128
128
129
129
/// Import serialized lines for `DataVersion::V003`
@@ -132,11 +132,9 @@ async fn import_v002(
132
132
///
133
133
/// It write logs and related entries to sled trees, and state_machine entries to a snapshot.
134
134
async fn import_v003 (
135
- config : & Config ,
135
+ raft_config : RaftConfig ,
136
136
lines : impl IntoIterator < Item = Result < String , io:: Error > > ,
137
137
) -> anyhow:: Result < Option < LogId > > {
138
- let raft_config: RaftConfig = config. clone ( ) . into ( ) ;
139
-
140
138
let db = get_sled_db ( ) ;
141
139
142
140
let mut n = 0 ;
@@ -221,24 +219,26 @@ async fn import_v003(
221
219
/// Insert them into sled db and flush.
222
220
///
223
221
/// Finally upgrade the data in raft_dir to the latest version.
224
- async fn import_from_stdin_or_file ( config : & Config ) -> anyhow:: Result < Option < LogId > > {
225
- let restore = config . db . clone ( ) ;
222
+ async fn import_from_stdin_or_file ( args : & ImportArgs ) -> anyhow:: Result < Option < LogId > > {
223
+ let restore = args . db . clone ( ) ;
226
224
225
+ let raft_config: RaftConfig = args. clone ( ) . into ( ) ;
227
226
let max_log_id = if restore. is_empty ( ) {
228
227
eprintln ! ( " From: <stdin>" ) ;
229
228
let lines = io:: stdin ( ) . lines ( ) ;
230
229
231
- import_lines ( config , lines) . await ?
230
+ import_lines ( raft_config , lines) . await ?
232
231
} else {
233
- eprintln ! ( " From: {}" , config . db) ;
232
+ eprintln ! ( " From: {}" , args . db) ;
234
233
let file = File :: open ( restore) ?;
235
234
let reader = BufReader :: new ( file) ;
236
235
let lines = reader. lines ( ) ;
237
236
238
- import_lines ( config , lines) . await ?
237
+ import_lines ( raft_config , lines) . await ?
239
238
} ;
240
239
241
- upgrade:: upgrade ( config) . await ?;
240
+ let raft_config: RaftConfig = args. clone ( ) . into ( ) ;
241
+ upgrade:: upgrade ( & raft_config) . await ?;
242
242
243
243
Ok ( max_log_id)
244
244
}
@@ -298,16 +298,15 @@ fn build_nodes(initial_cluster: Vec<String>, id: u64) -> anyhow::Result<BTreeMap
298
298
299
299
// initial_cluster format: node_id=endpoint,grpc_api_addr;
300
300
async fn init_new_cluster (
301
- config : & Config ,
301
+ args : & ImportArgs ,
302
302
nodes : BTreeMap < NodeId , Node > ,
303
303
max_log_id : Option < LogId > ,
304
- id : u64 ,
305
304
) -> anyhow:: Result < ( ) > {
306
305
eprintln ! ( ) ;
307
306
eprintln ! ( "Initialize Cluster with: {:?}" , nodes) ;
308
307
309
308
let db = get_sled_db ( ) ;
310
- let raft_config: RaftConfig = config . clone ( ) . into ( ) ;
309
+ let raft_config: RaftConfig = args . clone ( ) . into ( ) ;
311
310
312
311
let mut sto = RaftStore :: open_create ( & raft_config, Some ( ( ) ) , None ) . await ?;
313
312
@@ -375,13 +374,13 @@ async fn init_new_cluster(
375
374
376
375
// Reset node id
377
376
let raft_state = RaftState :: open_create ( & db, & raft_config, Some ( ( ) ) , None ) . await ?;
378
- raft_state. set_node_id ( id) . await ?;
377
+ raft_state. set_node_id ( args . id ) . await ?;
379
378
380
379
Ok ( ( ) )
381
380
}
382
381
383
382
/// Clear all sled data and on-disk snapshot.
384
- fn clear ( config : & Config ) -> anyhow:: Result < ( ) > {
383
+ fn clear ( args : & ImportArgs ) -> anyhow:: Result < ( ) > {
385
384
eprintln ! ( ) ;
386
385
eprintln ! ( "Clear All Sled Trees Before Import:" ) ;
387
386
let db = get_sled_db ( ) ;
@@ -394,7 +393,7 @@ fn clear(config: &Config) -> anyhow::Result<()> {
394
393
eprintln ! ( " Cleared sled tree: {}" , name) ;
395
394
}
396
395
397
- let df_meta_path = format ! ( "{}/df_meta" , config . raft_dir. clone( ) . unwrap_or_default( ) ) ;
396
+ let df_meta_path = format ! ( "{}/df_meta" , args . raft_dir. clone( ) . unwrap_or_default( ) ) ;
398
397
if Path :: new ( & df_meta_path) . exists ( ) {
399
398
remove_dir_all ( & df_meta_path) ?;
400
399
}
0 commit comments