-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathclient.rs
More file actions
700 lines (662 loc) · 24.4 KB
/
client.rs
File metadata and controls
700 lines (662 loc) · 24.4 KB
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
//! This client binary creates or opens a [commonware_storage::qmdb] database and
//! synchronizes it to a remote server's state. It uses the [Resolver] to fetch operations and
//! sync target updates from the server, and continuously syncs to demonstrate that sync works
//! with both empty and already-initialized databases.
use clap::{Arg, Command};
use commonware_codec::{EncodeShared, Read};
use commonware_runtime::{
tokio as tokio_runtime, BufferPooler, Clock, Metrics, Network, Runner, Spawner, Storage,
Supervisor as _,
};
use commonware_storage::{
mmr,
qmdb::{
any::sync::Target,
current as current_qmdb,
sync::{self, compact},
},
};
use commonware_sync::{
any, crate_version, current,
databases::{DatabaseType, SyncMode},
immutable, immutable_compact, keyless, keyless_compact,
net::{ErrorCode, Resolver},
Error, Key,
};
use commonware_utils::{
channel::mpsc::{self, error::TrySendError},
DurationExt,
};
use rand::Rng;
use std::{
future::Future,
net::{Ipv4Addr, SocketAddr},
num::NonZeroU64,
time::Duration,
};
use tracing::{debug, error, info, warn};
/// Default server address.
const DEFAULT_SERVER: &str = "127.0.0.1:8080";
/// Default client storage directory prefix.
const DEFAULT_CLIENT_DIR_PREFIX: &str = "/tmp/commonware-sync/client";
/// Size of the channel for target updates.
const UPDATE_CHANNEL_SIZE: usize = 16;
/// Client configuration.
#[derive(Debug, Clone)]
struct Config {
/// Sync mode to use.
sync_mode: SyncMode,
/// Database family to use.
family: DatabaseType,
/// Server address to connect to.
server: SocketAddr,
/// Batch size for fetching operations.
batch_size: NonZeroU64,
/// Storage directory.
storage_dir: String,
/// Port on which metrics are exposed.
metrics_port: u16,
/// Interval for requesting target updates.
target_update_interval: Duration,
/// Interval between sync operations.
sync_interval: Duration,
/// Maximum number of outstanding requests.
max_outstanding_requests: usize,
}
/// Every `interval_duration`, request an updated full-sync target from `resolver` and send any
/// changes on `update_tx`.
async fn target_update_task<E, Op, D>(
context: E,
resolver: Resolver<Op, D>,
update_tx: mpsc::Sender<Target<mmr::Family, D>>,
interval_duration: Duration,
initial_target: Target<mmr::Family, D>,
) -> Result<(), Error>
where
E: Clock,
Op: Read + EncodeShared,
Op::Cfg: commonware_codec::IsUnit,
D: commonware_cryptography::Digest,
{
let mut current_target = initial_target;
loop {
context.sleep(interval_duration).await;
match resolver.get_sync_target().await {
Ok(new_target) => {
// Check if target has changed
if current_target != new_target {
// Send new target to the sync client
match update_tx.clone().try_send(new_target.clone()) {
Ok(()) => {
info!(old_target = ?current_target, new_target = ?new_target, "target updated");
current_target = new_target;
}
Err(TrySendError::Closed(_)) => {
debug!("sync client disconnected, terminating target update task");
return Ok(());
}
Err(err) => {
warn!(?err, "failed to send target update to sync client");
return Err(Error::TargetUpdateChannel {
reason: err.to_string(),
});
}
}
} else {
debug!(current_target = ?current_target, "target unchanged");
}
}
Err(err) => {
warn!(?err, "failed to get sync target from server");
}
}
}
}
/// Repeatedly sync a full database to the server's state.
async fn run_full_sync<DB, Op, E, SyncOnce, SyncFut>(
context: E,
config: Config,
sync_once: SyncOnce,
label: &'static str,
) -> Result<(), Box<dyn std::error::Error>>
where
E: BufferPooler + Storage + Clock + Metrics + Network + Spawner,
Op: Clone + Read + EncodeShared + 'static,
Op::Cfg: commonware_codec::IsUnit,
SyncOnce: Fn(
E,
Config,
Resolver<Op, Key>,
Target<mmr::Family, Key>,
mpsc::Receiver<Target<mmr::Family, Key>>,
u32,
) -> SyncFut,
SyncFut: Future<Output = Result<DB, Box<dyn std::error::Error>>>,
{
info!("starting {label} sync process");
let mut iteration = 0u32;
loop {
let resolver =
Resolver::<Op, Key>::connect(context.child("resolver"), config.server).await?;
let initial_target = resolver.get_sync_target().await?;
let (update_sender, update_receiver) = mpsc::channel(UPDATE_CHANNEL_SIZE);
let target_update_handle = {
let resolver = resolver.clone();
let initial_target_clone = initial_target.clone();
let target_update_interval = config.target_update_interval;
context.child("target_update").spawn(move |context| {
target_update_task(
context,
resolver,
update_sender,
target_update_interval,
initial_target_clone,
)
})
};
sync_once(
context.child("sync"),
config.clone(),
resolver,
initial_target,
update_receiver,
iteration,
)
.await?;
target_update_handle.abort();
context.sleep(config.sync_interval).await;
iteration += 1;
}
}
/// Repeatedly sync an Any database to the server's state.
async fn run_any<E>(context: E, config: Config) -> Result<(), Box<dyn std::error::Error>>
where
E: BufferPooler + Storage + Clock + Metrics + Network + Spawner,
{
run_full_sync::<any::Database<_>, any::Operation, _, _, _>(
context,
config,
|context, config, resolver, initial_target, update_receiver, iteration| async move {
let db_config = any::create_config(&context);
let sync_config =
sync::engine::Config::<any::Database<_>, Resolver<any::Operation, Key>, _> {
context,
db_config,
fetch_batch_size: config.batch_size,
target: initial_target,
resolver,
apply_batch_size: 1024,
max_outstanding_requests: config.max_outstanding_requests,
update_rx: Some(update_receiver),
finish_rx: None,
reached_target_tx: None,
max_retained_roots: 8,
};
let database: any::Database<_> = sync::sync(sync_config).await?;
info!(
sync_iteration = iteration,
root = %database.root(),
sync_interval = ?config.sync_interval,
"Any sync completed successfully"
);
Ok(database)
},
"Any database",
)
.await
}
/// Repeatedly sync a Current database to the server's state.
///
/// Uses the `current::sync::sync` wrapper. The wrapper verifies each target's `OpsRootWitness`
/// before forwarding its ops root to the shared sync engine, then checks the database root for the
/// target the engine finishes on.
async fn run_current<E>(context: E, config: Config) -> Result<(), Box<dyn std::error::Error>>
where
E: BufferPooler + Storage + Clock + Metrics + Network + Spawner,
{
info!("starting Current database sync process");
let mut iteration = 0u32;
loop {
let resolver =
Resolver::<current::Operation, Key>::connect(context.child("resolver"), config.server)
.await?;
let initial_target = resolver.get_current_sync_target().await?;
info!(
root = %initial_target.root,
ops_root = %initial_target.ops_root,
range = ?initial_target.range,
"received current sync target"
);
let (update_sender, update_receiver) = mpsc::channel(UPDATE_CHANNEL_SIZE);
let target_update_handle = {
let resolver = resolver.clone();
let mut current_target_root = initial_target.root;
let target_update_interval = config.target_update_interval;
context
.child("target_update")
.spawn(move |context| async move {
loop {
context.sleep(target_update_interval).await;
match resolver.get_current_sync_target().await {
Ok(new_target) => {
if current_target_root != new_target.root {
let new_root = new_target.root;
match update_sender.clone().try_send(new_target) {
Ok(()) => {
info!("target updated");
current_target_root = new_root;
}
Err(mpsc::error::TrySendError::Closed(_)) => return Ok(()),
Err(err) => {
warn!(?err, "failed to send target update");
return Err(Error::TargetUpdateChannel {
reason: err.to_string(),
});
}
}
}
}
Err(err) => {
warn!(?err, "failed to get sync target from server");
}
}
}
})
};
let db_config = current::create_config(&context);
let database: current::Database<_> = current_qmdb::sync::sync(current_qmdb::sync::Config {
context: context.child("sync"),
resolver,
target: initial_target,
max_outstanding_requests: config.max_outstanding_requests,
fetch_batch_size: config.batch_size,
apply_batch_size: 1024,
db_config,
update_rx: Some(update_receiver),
finish_rx: None,
reached_target_tx: None,
max_retained_roots: 8,
})
.await?;
target_update_handle.abort();
info!(
sync_iteration = iteration,
root = %database.root(),
ops_root = %database.ops_root(),
sync_interval = ?config.sync_interval,
"Current sync completed successfully"
);
database.destroy().await?;
context.sleep(config.sync_interval).await;
iteration += 1;
}
}
/// Repeatedly sync an Immutable database to the server's state.
async fn run_immutable<E>(context: E, config: Config) -> Result<(), Box<dyn std::error::Error>>
where
E: BufferPooler + Storage + Clock + Metrics + Network + Spawner,
{
run_full_sync::<immutable::Database<_>, immutable::Operation, _, _, _>(
context,
config,
|context, config, resolver, initial_target, update_receiver, iteration| async move {
let db_config = immutable::create_config(&context);
let sync_config = sync::engine::Config::<
immutable::Database<_>,
Resolver<immutable::Operation, Key>,
_,
> {
context,
db_config,
fetch_batch_size: config.batch_size,
target: initial_target,
resolver,
apply_batch_size: 1024,
max_outstanding_requests: config.max_outstanding_requests,
update_rx: Some(update_receiver),
finish_rx: None,
reached_target_tx: None,
max_retained_roots: 8,
};
let database: immutable::Database<_> = sync::sync(sync_config).await?;
info!(
sync_iteration = iteration,
root = %database.root(),
sync_interval = ?config.sync_interval,
"Immutable sync completed successfully"
);
Ok(database)
},
"Immutable database",
)
.await
}
/// Repeatedly sync a Keyless database to the server's state.
async fn run_keyless<E>(context: E, config: Config) -> Result<(), Box<dyn std::error::Error>>
where
E: BufferPooler + Storage + Clock + Metrics + Network + Spawner,
{
run_full_sync::<keyless::Database<_>, keyless::Operation, _, _, _>(
context,
config,
|context, config, resolver, initial_target, update_receiver, iteration| async move {
let db_config = keyless::create_config(&context);
let sync_config = sync::engine::Config::<
keyless::Database<_>,
Resolver<keyless::Operation, Key>,
_,
> {
context,
db_config,
fetch_batch_size: config.batch_size,
target: initial_target,
resolver,
apply_batch_size: 1024,
max_outstanding_requests: config.max_outstanding_requests,
update_rx: Some(update_receiver),
finish_rx: None,
reached_target_tx: None,
max_retained_roots: 8,
};
let database: keyless::Database<_> = sync::sync(sync_config).await?;
info!(
sync_iteration = iteration,
root = %database.root(),
sync_interval = ?config.sync_interval,
"Keyless sync completed successfully"
);
Ok(database)
},
"Keyless database",
)
.await
}
/// Repeatedly sync a compact-storage database via compact state transfer.
async fn run_compact_sync<DB, Op, E, MakeConfig>(
context: E,
config: Config,
make_db_config: MakeConfig,
label: &'static str,
) -> Result<(), Box<dyn std::error::Error>>
where
E: BufferPooler + Storage + Clock + Metrics + Network + Spawner,
DB: compact::Database<Family = mmr::Family, Context = E, Digest = Key, Op = Op>,
Op: Clone + Read + EncodeShared + 'static,
Op::Cfg: commonware_codec::IsUnit,
MakeConfig: Fn(&E) -> DB::Config,
{
info!("starting {label} compact sync process");
let mut iteration = 0u32;
loop {
let resolver =
Resolver::<Op, Key>::connect(context.child("resolver"), config.server).await?;
let target = resolver.get_compact_target().await?;
let sync_config = compact::Config::<DB, Resolver<Op, Key>> {
context: context.child("sync"),
resolver,
target,
db_config: make_db_config(&context),
};
let database: DB = match compact::sync(sync_config).await {
Ok(database) => database,
Err(sync::Error::Resolver(Error::Server {
code: ErrorCode::StaleTarget,
message,
})) => {
warn!(
sync_iteration = iteration,
"{label} target went stale before state fetch: {message}; retrying"
);
continue;
}
Err(err) => return Err(err.into()),
};
info!(
sync_iteration = iteration,
root = %database.root(),
sync_interval = ?config.sync_interval,
"{label} sync completed successfully"
);
context.sleep(config.sync_interval).await;
iteration += 1;
}
}
async fn run_immutable_compact<E>(
context: E,
config: Config,
) -> Result<(), Box<dyn std::error::Error>>
where
E: BufferPooler + Storage + Clock + Metrics + Network + Spawner,
{
run_compact_sync::<immutable_compact::Database<_>, immutable_compact::Operation, _, _>(
context,
config,
|ctx| immutable_compact::create_config(ctx),
"Immutable compact",
)
.await
}
async fn run_keyless_compact<E>(
context: E,
config: Config,
) -> Result<(), Box<dyn std::error::Error>>
where
E: BufferPooler + Storage + Clock + Metrics + Network + Spawner,
{
run_compact_sync::<keyless_compact::Database<_>, keyless_compact::Operation, _, _>(
context,
config,
|ctx| keyless_compact::create_config(ctx),
"Keyless compact",
)
.await
}
fn parse_config() -> Result<Config, Box<dyn std::error::Error>> {
// Parse command line arguments
let matches = Command::new("Sync Client")
.version(crate_version())
.about("Continuously syncs a database to a server's database state")
.arg(
Arg::new("mode")
.long("mode")
.value_name("full|compact")
.help("Sync mode to demonstrate. Use `full` for operation replay or `compact` for compact state transfer.")
.default_value("full"),
)
.arg(
Arg::new("family")
.long("family")
.value_name("any|current|immutable|keyless")
.help("Database family to use for the selected mode.")
.default_value("any"),
)
.arg(
Arg::new("server")
.short('s')
.long("server")
.value_name("ADDRESS")
.help("Server address to connect to")
.default_value(DEFAULT_SERVER),
)
.arg(
Arg::new("batch-size")
.short('b')
.long("batch-size")
.value_name("SIZE")
.help("Batch size for fetching operations in full mode")
.default_value("50"),
)
.arg(
Arg::new("storage-dir")
.short('d')
.long("storage-dir")
.value_name("PATH")
.help("Storage directory for local database")
.default_value(DEFAULT_CLIENT_DIR_PREFIX),
)
.arg(
Arg::new("metrics-port")
.short('m')
.long("metrics-port")
.value_name("PORT")
.help("Port on which metrics are exposed")
.default_value("9091"),
)
.arg(
Arg::new("target-update-interval")
.short('t')
.long("target-update-interval")
.value_name("DURATION")
.help("Interval for requesting target updates in full mode ('ms', 's', 'm', 'h')")
.default_value("1s"),
)
.arg(
Arg::new("sync-interval")
.short('i')
.long("sync-interval")
.value_name("DURATION")
.help("Interval between sync operations ('ms', 's', 'm', 'h')")
.default_value("10s"),
)
.arg(
Arg::new("max-outstanding-requests")
.short('r')
.long("max-outstanding-requests")
.value_name("COUNT")
.help("Maximum number of outstanding sync requests in full mode")
.default_value("1"),
)
.get_matches();
let sync_mode = matches
.get_one::<String>("mode")
.unwrap()
.parse::<SyncMode>()
.map_err(|e| format!("Invalid sync mode: {e}"))?;
let family = matches
.get_one::<String>("family")
.unwrap()
.parse::<DatabaseType>()
.map_err(|e| format!("Invalid database family: {e}"))?;
if !family.supports_client_mode(sync_mode) {
return Err(format!(
"Database family '{}' is not supported in '{}' mode",
family.as_str(),
sync_mode.as_str()
)
.into());
}
let server = matches
.get_one::<String>("server")
.unwrap()
.parse()
.map_err(|e| format!("Invalid server address: {e}"))?;
let batch_size = matches
.get_one::<String>("batch-size")
.unwrap()
.parse()
.map_err(|e| format!("Invalid batch size: {e}"))?;
let storage_dir = {
let storage_dir = matches
.get_one::<String>("storage-dir")
.unwrap()
.to_string();
// Only add suffix if using the default value
if storage_dir == DEFAULT_CLIENT_DIR_PREFIX {
let suffix: u64 = rand::thread_rng().gen();
format!("{storage_dir}-{suffix}")
} else {
storage_dir
}
};
let metrics_port = matches
.get_one::<String>("metrics-port")
.unwrap()
.parse()
.map_err(|e| format!("Invalid metrics port: {e}"))?;
let target_update_interval =
Duration::parse(matches.get_one::<String>("target-update-interval").unwrap())
.map_err(|e| format!("Invalid target update interval: {e}"))?;
let sync_interval = Duration::parse(matches.get_one::<String>("sync-interval").unwrap())
.map_err(|e| format!("Invalid sync interval: {e}"))?;
let max_outstanding_requests = matches
.get_one::<String>("max-outstanding-requests")
.unwrap()
.parse()
.map_err(|e| format!("Invalid max outstanding requests: {e}"))?;
Ok(Config {
sync_mode,
family,
server,
batch_size,
storage_dir,
metrics_port,
target_update_interval,
sync_interval,
max_outstanding_requests,
})
}
fn main() {
let config = parse_config().unwrap_or_else(|e| {
eprintln!("Configuration error: {e}");
std::process::exit(1);
});
let materialized_storage = match config.sync_mode {
SyncMode::Full => "full",
SyncMode::Compact => "compact",
};
let executor_config =
tokio_runtime::Config::default().with_storage_directory(config.storage_dir.clone());
let executor = tokio_runtime::Runner::new(executor_config);
executor.start(|context| async move {
tokio_runtime::telemetry::init(
context.child("telemetry"),
tokio_runtime::telemetry::Logging {
level: tracing::Level::INFO,
json: false,
},
Some(SocketAddr::from((Ipv4Addr::LOCALHOST, config.metrics_port))),
None,
);
info!(
sync_mode = %config.sync_mode.as_str(),
family = %config.family.as_str(),
materialized_storage,
server = %config.server,
batch_size = config.batch_size,
storage_dir = %config.storage_dir,
metrics_port = config.metrics_port,
target_update_interval = ?config.target_update_interval,
sync_interval = ?config.sync_interval,
max_outstanding_requests = config.max_outstanding_requests,
"client starting with configuration"
);
// Dispatch based on sync mode and database family.
let result = match (config.sync_mode, config.family) {
(SyncMode::Full, DatabaseType::Any) => run_any(context.child("sync"), config).await,
(SyncMode::Full, DatabaseType::Current) => {
run_current(context.child("sync"), config).await
}
(SyncMode::Full, DatabaseType::Immutable) => {
run_immutable(context.child("sync"), config).await
}
(SyncMode::Full, DatabaseType::Keyless) => {
run_keyless(context.child("sync"), config).await
}
(SyncMode::Compact, DatabaseType::Immutable) => {
run_immutable_compact(context.child("sync"), config).await
}
(SyncMode::Compact, DatabaseType::Keyless) => {
run_keyless_compact(context.child("sync"), config).await
}
_ => Err(Box::<dyn std::error::Error>::from(format!(
"unsupported combination: mode={} family={}",
config.sync_mode.as_str(),
config.family.as_str()
))),
};
if let Err(err) = result {
error!(?err, "continuous sync failed");
std::process::exit(1);
}
});
}