/
handlers.rs
704 lines (623 loc) · 25.4 KB
/
handlers.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
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
701
702
703
704
//! Various handler functions
//!
//! This module is _big_ and maybe should be split up further.
use std::{
cmp,
collections::VecDeque,
net::SocketAddr,
time::{Duration, Instant},
};
use crate::{
agent::{bi, bootstrap, uni, util, SyncClientError, ANNOUNCE_INTERVAL},
api::peer::parallel_sync,
transport::Transport,
};
use corro_types::{
actor::{Actor, ActorId},
agent::{Agent, Bookie, SplitPool},
base::CrsqlSeq,
broadcast::{BroadcastInput, BroadcastV1, ChangeSource, ChangeV1, FocaInput},
channel::CorroReceiver,
sync::generate_sync,
};
use bytes::Bytes;
use foca::Notification;
use indexmap::IndexMap;
use metrics::{counter, gauge, histogram};
use rand::{prelude::IteratorRandom, rngs::StdRng, SeedableRng};
use rangemap::RangeInclusiveSet;
use spawn::spawn_counted;
use tokio::{
sync::mpsc::Receiver as TokioReceiver,
task::{block_in_place, JoinSet},
};
use tokio_stream::{wrappers::ReceiverStream, StreamExt};
use tracing::{debug, debug_span, error, info, trace, warn, Instrument};
use tripwire::{Outcome, PreemptibleFutureExt, TimeoutFutureExt, Tripwire};
/// Spawn a tree of tasks that handles incoming gossip server
/// connections, streams, and their respective payloads.
pub fn spawn_gossipserver_handler(
agent: &Agent,
bookie: &Bookie,
tripwire: &Tripwire,
gossip_server_endpoint: quinn::Endpoint,
) {
spawn_counted({
let agent = agent.clone();
let bookie = bookie.clone();
let mut tripwire = tripwire.clone();
async move {
loop {
let connecting = match gossip_server_endpoint
.accept()
.preemptible(&mut tripwire)
.await
{
Outcome::Completed(Some(connecting)) => connecting,
Outcome::Completed(None) => return,
Outcome::Preempted(_) => break,
};
// Spawn incoming connection handlers
spawn_incoming_connection_handlers(&agent, &bookie, &tripwire, connecting);
}
// graceful shutdown
gossip_server_endpoint.reject_new_connections();
_ = gossip_server_endpoint
.wait_idle()
.with_timeout(Duration::from_secs(5))
.await;
gossip_server_endpoint.close(0u32.into(), b"shutting down");
}
});
}
/// Spawn a task which handles all state and interactions for a given
/// incoming connection. This function spawns many futures!
pub fn spawn_incoming_connection_handlers(
agent: &Agent,
bookie: &Bookie,
tripwire: &Tripwire,
connecting: quinn::Connecting,
) {
let agent = agent.clone();
let bookie = bookie.clone();
let tripwire = tripwire.clone();
tokio::spawn(async move {
let remote_addr = connecting.remote_address();
// let local_ip = connecting.local_ip().unwrap();
debug!("got a connection from {remote_addr}");
let conn = match connecting.await {
Ok(conn) => conn,
Err(e) => {
error!("could not handshake connection from {remote_addr}: {e}");
return;
}
};
counter!("corro.peer.connection.accept.total").increment(1);
debug!("accepted a QUIC conn from {remote_addr}");
// Spawn handler tasks for this connection
spawn_foca_handler(&agent, &tripwire, &conn);
uni::spawn_unipayload_handler(&tripwire, &conn, agent.clone());
bi::spawn_bipayload_handler(&agent, &bookie, &tripwire, &conn);
});
}
/// Spawn a single task that accepts chunks from a receiver and
/// updates cluster member round-trip-times in the agent state.
pub fn spawn_rtt_handler(agent: &Agent, rtt_rx: TokioReceiver<(SocketAddr, Duration)>) {
tokio::spawn({
let agent = agent.clone();
async move {
let stream = ReceiverStream::new(rtt_rx);
// we can handle a lot of them I think...
let chunker = stream.chunks_timeout(1024, Duration::from_secs(1));
tokio::pin!(chunker);
while let Some(chunks) = StreamExt::next(&mut chunker).await {
let mut members = agent.members().write();
for (addr, rtt) in chunks {
members.add_rtt(addr, rtt);
}
}
}
});
}
/// Spawn a single task to listen for `Datagram`s from the transport
/// and apply FOCA messages to the local SWIM statemachine.
pub fn spawn_foca_handler(agent: &Agent, tripwire: &Tripwire, conn: &quinn::Connection) {
tokio::spawn({
let conn = conn.clone();
let mut tripwire = tripwire.clone();
let foca_tx = agent.tx_foca().clone();
async move {
loop {
let b = tokio::select! {
b_res = conn.read_datagram() => match b_res {
Ok(b) => {
counter!("corro.peer.datagram.recv.total").increment(1);
counter!("corro.peer.datagram.bytes.recv.total").increment(b.len() as u64);
b
},
Err(e) => {
debug!("could not read datagram from connection: {e}");
return;
}
},
_ = &mut tripwire => {
debug!("connection cancelled");
return;
}
};
if let Err(e) = foca_tx.send(FocaInput::Data(b)).await {
error!("could not send data foca input: {e}");
}
}
}
});
}
/// Announce this node to other random nodes (according to SWIM)
///
/// We use an exponential backoff to announce aggressively in the
/// beginning, get a full picture of the cluster, then stop spamming
/// everyone.
///
///
pub fn spawn_swim_announcer(agent: &Agent, gossip_addr: SocketAddr) {
tokio::spawn({
let agent = agent.clone();
async move {
let mut boff = backoff::Backoff::new(10)
.timeout_range(Duration::from_secs(5), Duration::from_secs(120))
.iter();
let timer = tokio::time::sleep(Duration::new(0, 0));
tokio::pin!(timer);
loop {
timer.as_mut().await;
match bootstrap::generate_bootstrap(
agent.config().gossip.bootstrap.as_slice(),
gossip_addr,
agent.pool(),
)
.await
{
Ok(addrs) => {
for addr in addrs.iter() {
debug!("Bootstrapping w/ {addr}");
if let Err(e) = agent
.tx_foca()
.send(FocaInput::Announce((*addr).into()))
.await
{
error!("could not send foca Announce message: {e}");
} else {
debug!("successfully sent announce message");
}
}
}
Err(e) => {
error!("could not find nodes to announce ourselves to: {e}");
}
}
let dur = boff.next().unwrap_or(ANNOUNCE_INTERVAL);
timer.as_mut().reset(tokio::time::Instant::now() + dur);
}
}
});
}
/// A central dispatcher for SWIM cluster management messages
// TODO: we may be able to inline this code where it is needed
pub async fn handle_gossip_to_send(
transport: Transport,
mut swim_to_send_rx: CorroReceiver<(Actor, Bytes)>,
) {
// TODO: use tripwire and drain messages to send when that happens...
while let Some((actor, data)) = swim_to_send_rx.recv().await {
trace!("got gossip to send to {actor:?}");
let addr = actor.addr();
let actor_id = actor.id();
let transport = transport.clone();
let len = data.len();
spawn_counted(
async move {
if let Err(e) = transport.send_datagram(addr, data).await {
error!("could not write datagram {addr}: {e}");
return;
}
counter!("corro.peer.datagram.sent.total", "actor_id" => actor_id.to_string())
.increment(1);
counter!("corro.peer.datagram.bytes.sent.total").increment(len as u64);
}
.instrument(debug_span!("send_swim_payload", %addr, %actor_id, buf_size = len)),
);
}
}
/// Poll for updates from the cluster membership system (`foca`/ SWIM)
/// and apply any incoming changes to the local actor/ agent state.
pub async fn handle_notifications(
agent: Agent,
mut notification_rx: CorroReceiver<Notification<Actor>>,
) {
while let Some(notification) = notification_rx.recv().await {
trace!("handle notification");
match notification {
Notification::MemberUp(actor) => {
let (added, same) = { agent.members().write().add_member(&actor) };
trace!("Member Up {actor:?} (added: {added})");
if added {
debug!("Member Up {actor:?}");
counter!("corro.gossip.member.added", "id" => actor.id().0.to_string(), "addr" => actor.addr().to_string()).increment(1);
// actually added a member
// notify of new cluster size
let members_len = { agent.members().read().states.len() as u32 };
if let Ok(size) = members_len.try_into() {
if let Err(e) = agent.tx_foca().send(FocaInput::ClusterSize(size)).await {
error!("could not send new foca cluster size: {e}");
}
}
} else if !same {
// had a older timestamp!
if let Err(e) = agent
.tx_foca()
.send(FocaInput::ApplyMany(vec![foca::Member::new(
actor.clone(),
foca::Incarnation::default(),
foca::State::Down,
)]))
.await
{
warn!(?actor, "could not manually declare actor as down! {e}");
}
}
counter!("corro.swim.notification", "type" => "memberup").increment(1);
}
Notification::MemberDown(actor) => {
let removed = { agent.members().write().remove_member(&actor) };
trace!("Member Down {actor:?} (removed: {removed})");
if removed {
debug!("Member Down {actor:?}");
counter!("corro.gossip.member.removed", "id" => actor.id().0.to_string(), "addr" => actor.addr().to_string()).increment(1);
// actually removed a member
// notify of new cluster size
let member_len = { agent.members().read().states.len() as u32 };
if let Ok(size) = member_len.try_into() {
if let Err(e) = agent.tx_foca().send(FocaInput::ClusterSize(size)).await {
error!("could not send new foca cluster size: {e}");
}
}
}
counter!("corro.swim.notification", "type" => "memberdown").increment(1);
}
Notification::Active => {
info!("Current node is considered ACTIVE");
counter!("corro.swim.notification", "type" => "active").increment(1);
}
Notification::Idle => {
warn!("Current node is considered IDLE");
counter!("corro.swim.notification", "type" => "idle").increment(1);
}
// this happens when we leave the cluster
Notification::Defunct => {
debug!("Current node is considered DEFUNCT");
counter!("corro.swim.notification", "type" => "defunct").increment(1);
}
Notification::Rejoin(id) => {
info!("Rejoined the cluster with id: {id:?}");
counter!("corro.swim.notification", "type" => "rejoin").increment(1);
}
}
}
}
/// We keep a write-ahead-log, which under write-pressure can grow to
/// multiple gigabytes and needs periodic truncation. We don't want
/// to schedule this task too often since it locks the whole DB.
// TODO: can we get around the lock somehow?
fn db_cleanup(conn: &rusqlite::Connection) -> eyre::Result<()> {
debug!("handling db_cleanup (WAL truncation)");
let start = Instant::now();
let orig: u64 = conn.pragma_query_value(None, "busy_timeout", |row| row.get(0))?;
conn.pragma_update(None, "busy_timeout", 60000)?;
let busy: bool = conn.query_row("PRAGMA wal_checkpoint(TRUNCATE);", [], |row| row.get(0))?;
if busy {
warn!("could not truncate sqlite WAL, database busy");
counter!("corro.db.wal.truncate.busy").increment(1);
} else {
debug!("successfully truncated sqlite WAL!");
histogram!("corro.db.wal.truncate.seconds").record(start.elapsed().as_secs_f64());
}
_ = conn.pragma_update(None, "busy_timeout", orig);
Ok::<_, eyre::Report>(())
}
/// See `db_cleanup`
pub fn spawn_handle_db_cleanup(pool: SplitPool) {
tokio::spawn(async move {
let mut db_cleanup_interval = tokio::time::interval(Duration::from_secs(60 * 15));
loop {
db_cleanup_interval.tick().await;
match pool.write_low().await {
Ok(conn) => {
if let Err(e) = block_in_place(|| db_cleanup(&conn)) {
error!("could not truncate db: {e}");
}
}
Err(e) => {
error!("could not acquire low priority conn to truncate wal: {e}")
}
}
}
});
}
/// Bundle incoming changes to optimise transaction sizes with SQLite
///
/// *Performance tradeoff*: introduce latency (with a max timeout) to
/// apply changes more efficiently.
///
/// This function used by broadcast receivers and sync receivers
pub async fn handle_changes(
agent: Agent,
bookie: Bookie,
mut rx_changes: CorroReceiver<(ChangeV1, ChangeSource)>,
mut tripwire: Tripwire,
) {
const MAX_CHANGES_CHUNK: usize = 800;
let mut queue: VecDeque<(ChangeV1, ChangeSource, Instant)> = VecDeque::new();
let mut buf = vec![];
let mut count = 0;
const MAX_CONCURRENT: usize = 5;
let mut join_set = JoinSet::new();
let mut max_wait = tokio::time::interval(Duration::from_millis(500));
const MAX_SEEN_CACHE_LEN: usize = 10000;
const KEEP_SEEN_CACHE_SIZE: usize = 1000;
let mut seen: IndexMap<_, RangeInclusiveSet<CrsqlSeq>> = IndexMap::new();
// complicated loop to process changes efficiently w/ a max concurrency
// and a minimum chunk size for bigger and faster SQLite transactions
loop {
while count >= MAX_CHANGES_CHUNK && join_set.len() < MAX_CONCURRENT {
// we're already bigger than the minimum size of changes batch
// so we want to accumulate at least that much and process them
// concurrently bvased on MAX_CONCURRENCY
let mut tmp_count = 0;
while let Some((change, src, queued_at)) = queue.pop_front() {
tmp_count += change.len();
buf.push((change, src, queued_at));
if tmp_count >= MAX_CHANGES_CHUNK {
break;
}
}
if buf.is_empty() {
break;
}
debug!(count = %tmp_count, "spawning processing multiple changes from beginning of loop");
join_set.spawn(util::process_multiple_changes(
agent.clone(),
bookie.clone(),
std::mem::take(&mut buf),
));
count -= tmp_count;
}
tokio::select! {
biased;
// process these first, we don't care about the result,
// but we need to drain it to free up concurrency
res = join_set.join_next(), if !join_set.is_empty() => {
debug!("processed multiple changes concurrently");
if let Some(Ok(Err(e))) = res {
error!("could not process multiple changes: {e}");
}
continue;
},
Some((change, src)) = rx_changes.recv() => {
let change_len = change.len();
counter!("corro.agent.changes.recv").increment(std::cmp::max(change_len, 1) as u64); // count empties...
if change.actor_id == agent.actor_id() {
continue;
}
if let Some(mut seqs) = change.seqs().cloned() {
let v = *change.versions().start();
if let Some(seen_seqs) = seen.get(&(change.actor_id, v)) {
if seqs.all(|seq| seen_seqs.contains(&seq)) {
continue;
}
}
} else {
// empty versions
if change.versions().all(|v| seen.contains_key(&(change.actor_id, v))) {
continue;
}
}
let recv_lag = change
.ts()
.map(|ts| (agent.clock().new_timestamp().get_time() - ts.0).to_duration());
if matches!(src, ChangeSource::Broadcast) {
counter!("corro.broadcast.recv.count", "kind" => "change").increment(1);
}
let booked = {
bookie
.read(format!(
"handle_change(get):{}",
change.actor_id.as_simple()
))
.await
.get(&change.actor_id)
.cloned()
};
if let Some(booked) = booked {
if booked
.read(format!(
"handle_change(contains?):{}",
change.actor_id.as_simple()
))
.await
.contains_all(change.versions(), change.seqs())
{
trace!("already seen, stop disseminating");
continue;
}
}
if let Some(recv_lag) = recv_lag {
let src_str: &'static str = src.into();
histogram!("corro.agent.changes.recv.lag.seconds", "source" => src_str).record(recv_lag.as_secs_f64());
}
// this will only run once for a non-empty changeset
for v in change.versions() {
let entry = seen.entry((change.actor_id, v)).or_default();
if let Some(seqs) = change.seqs().cloned() {
entry.extend([seqs]);
}
}
if matches!(src, ChangeSource::Broadcast) && !change.is_empty() {
if let Err(_e) =
agent
.tx_bcast()
.try_send(BroadcastInput::Rebroadcast(BroadcastV1::Change(change.clone())))
{
debug!("broadcasts are full or done!");
}
}
queue.push_back((change, src, Instant::now()));
count += change_len; // track number of individual changes, not changesets
},
_ = max_wait.tick() => {
// got a wait interval tick...
gauge!("corro.agent.changes.in_queue").set(count as f64);
gauge!("corro.agent.changesets.in_queue").set(queue.len() as f64);
gauge!("corro.agent.changes.processing.jobs").set(join_set.len() as f64);
if count < MAX_CHANGES_CHUNK && !queue.is_empty() && join_set.len() < MAX_CONCURRENT {
// we can process this right away
debug!(%count, "spawning processing multiple changes from max wait interval");
join_set.spawn(util::process_multiple_changes(
agent.clone(),
bookie.clone(),
queue.drain(..).collect(),
));
count = 0;
}
if seen.len() > MAX_SEEN_CACHE_LEN {
// we don't want to keep too many entries in here.
seen = seen.split_off(seen.len() - KEEP_SEEN_CACHE_SIZE);
}
},
_ = &mut tripwire => {
break;
}
else => {
break;
}
}
}
info!("Draining changes receiver...");
// drain!
while let Ok((change, src)) = rx_changes.try_recv() {
let changes_count = std::cmp::max(change.len(), 1);
counter!("corro.agent.changes.recv").increment(changes_count as u64);
count += changes_count;
queue.push_back((change, src, Instant::now()));
if count >= MAX_CHANGES_CHUNK {
// drain and process current changes!
if let Err(e) = util::process_multiple_changes(
agent.clone(),
bookie.clone(),
queue.drain(..).collect(),
)
.await
{
error!("could not process last multiple changes: {e}");
}
// reset count
count = 0;
}
}
// process the last changes we got!
if let Err(e) = util::process_multiple_changes(agent, bookie, queue.into_iter().collect()).await
{
error!("could not process multiple changes: {e}");
}
}
/// Start a new sync with multiple other nodes
///
/// Choose members to sync with based on the current RTT and how many
/// (known) versions we need from that peer. Add randomness to taste.
#[tracing::instrument(skip_all, err, level = "debug")]
pub async fn handle_sync(
agent: &Agent,
bookie: &Bookie,
transport: &Transport,
) -> Result<(), SyncClientError> {
let sync_state = generate_sync(bookie, agent.actor_id()).await;
for (actor_id, needed) in sync_state.need.iter() {
gauge!("corro.sync.client.needed", "actor_id" => actor_id.to_string())
.set(needed.len() as f64);
}
for (actor_id, version) in sync_state.heads.iter() {
gauge!("corro.sync.client.head", "actor_id" => actor_id.to_string()).set(version.0 as f64);
}
let chosen: Vec<(ActorId, SocketAddr)> = {
let candidates = {
let members = agent.members().read();
members
.states
.iter()
// Filter out self
.filter(|(id, state)| {
**id != agent.actor_id() && state.cluster_id == agent.cluster_id()
})
// Grab a ring-buffer index to the member RTT range
.map(|(id, state)| (*id, state.ring.unwrap_or(255), state.addr))
.collect::<Vec<(ActorId, u8, SocketAddr)>>()
};
if candidates.is_empty() {
return Ok(());
}
debug!("found {} candidates to synchronize with", candidates.len());
let desired_count = cmp::max(cmp::min(candidates.len() / 100, 10), 3);
let mut rng = StdRng::from_entropy();
let mut choices = candidates
.into_iter()
.choose_multiple(&mut rng, desired_count * 2);
choices.sort_by(|a, b| {
// most missing actors first
sync_state
.need_len_for_actor(&b.0)
.cmp(&sync_state.need_len_for_actor(&a.0))
// if equal, look at proximity (via `ring`)
.then_with(|| a.1.cmp(&b.1))
});
choices.truncate(desired_count);
choices
.into_iter()
.map(|(actor_id, _, addr)| (actor_id, addr))
.collect()
};
if chosen.is_empty() {
return Ok(());
}
let start = Instant::now();
let n = parallel_sync(agent, transport, chosen.clone(), sync_state).await?;
let elapsed = start.elapsed();
if n > 0 {
info!(
"synced {n} changes w/ {} in {}s @ {} changes/s",
chosen
.into_iter()
.map(|(actor_id, _)| actor_id.to_string())
.collect::<Vec<_>>()
.join(", "),
elapsed.as_secs_f64(),
n as f64 / elapsed.as_secs_f64()
);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ensure_truncate_works() -> eyre::Result<()> {
let tmpdir = tempfile::tempdir()?;
let conn = rusqlite::Connection::open(tmpdir.path().join("db.sqlite"))?;
let pragma_value = 12345u64;
conn.pragma_update(None, "busy_timeout", pragma_value)?;
db_cleanup(&conn)?;
assert_eq!(
conn.pragma_query_value(None, "busy_timeout", |row| row.get::<_, u64>(0))?,
pragma_value
);
Ok(())
}
}