/
mod.rs
1621 lines (1474 loc) · 59.2 KB
/
mod.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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::future;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use anyhow::{format_err, Context};
use dataflow::node::{self, Column};
use dataflow::prelude::ChannelCoordinator;
use failpoint_macros::set_failpoint;
use futures::future::Either;
use hyper::http::{Method, StatusCode};
use metrics::{counter, gauge, histogram};
use nom_sql::Relation;
use readyset_client::consensus::{
Authority, AuthorityControl, AuthorityWorkerHeartbeatResponse, GetLeaderResult,
WorkerDescriptor, WorkerId, WorkerSchedulingConfig,
};
#[cfg(feature = "failure_injection")]
use readyset_client::failpoints;
use readyset_client::metrics::recorded;
use readyset_client::ControllerDescriptor;
use readyset_data::Dialect;
use readyset_errors::{internal, internal_err, ReadySetError, ReadySetResult};
use readyset_telemetry_reporter::TelemetrySender;
use readyset_util::select;
use readyset_util::shutdown::ShutdownReceiver;
use replicators::ReplicatorMessage;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc::{self, Receiver, Sender, UnboundedReceiver, UnboundedSender};
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use tracing::{error, info, info_span, warn};
use tracing_futures::Instrument;
use url::Url;
use crate::controller::inner::Leader;
use crate::controller::migrate::Migration;
use crate::controller::sql::Recipe;
use crate::controller::state::DfState;
use crate::materialization::Materializations;
use crate::worker::{WorkerRequest, WorkerRequestKind, WorkerRequestType};
use crate::{Config, VolumeId};
mod domain_handle;
mod inner;
mod keys;
pub(crate) mod migrate; // crate viz for tests
mod mir_to_flow;
pub(crate) mod replication;
pub(crate) mod schema;
pub(crate) mod sql;
mod state;
/// Time between leader state change checks without thread parking.
const LEADER_STATE_CHECK_INTERVAL: Duration = Duration::from_secs(1);
/// Amount of time to wait for watches on the authority.
const WATCH_DURATION: Duration = Duration::from_secs(5);
/// A set of placement restrictions applied to a domain
/// that a dataflow node is in. Each base table node can have
/// a set of DomainPlacementRestrictions. A domain's
/// DomainPlacementRestriction is the merged set of restrictions
/// of all contained dataflow nodes.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct DomainPlacementRestriction {
worker_volume: Option<VolumeId>,
}
/// The key for a DomainPlacementRestriction for a dataflow node.
/// Each dataflow node, shard pair may have a DomainPlacementRestriction.
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq, Hash, Debug)]
pub struct NodeRestrictionKey {
node_name: Relation,
shard: usize,
}
/// The full (metadata) state of a running ReadySet cluster.
///
/// This struct is the root data structure that is serialized atomically and written to the
/// [`Authority`] upon changes to the state of the cluster. It includes:
/// - All configuration for the cluster
/// - The full schema of the database (both `CREATE TABLE` statements taken from the upstream
/// database and ReadySet-specific configuration including `CREATE CACHE` statements)
/// - The full state of the graph, including both [`MIR`][] and the dataflow graph itself
///
/// [`MIR`]: readyset_mir
#[derive(Clone, Serialize, Deserialize)]
pub(crate) struct ControllerState {
/// The user-provided configuration for the cluster
pub(crate) config: Config,
/// The full state of the dataflow engine
pub(crate) dataflow_state: DfState,
}
// We implement [`Debug`] manually so that we can skip the [`DfState`] field.
// In the future, we might want to implement [`Debug`] for [`DfState`] as well and just derive
// it from [`Debug`] for [`ControllerState`].
impl Debug for ControllerState {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ControllerState")
.field("config", &self.config)
.field(
"schema_replication_offset",
&self.dataflow_state.schema_replication_offset(),
)
.field("node_restrictions", &self.dataflow_state.node_restrictions)
.finish()
}
}
impl ControllerState {
/// Initialize a new, empty [`ControllerState`] with the given configuration, and with the given
/// value for the `permissive_writes` setting
fn new(config: Config, permissive_writes: bool) -> Self {
let mut g = petgraph::Graph::new();
// Create the root node in the graph.
let source = g.add_node(node::Node::new::<_, _, Vec<Column>, _>(
"source",
Vec::new(),
node::special::Source,
));
let mut materializations = Materializations::new();
materializations.set_config(config.materialization_config.clone());
let cc = Arc::new(ChannelCoordinator::new());
assert_ne!(config.min_workers, 0);
let recipe = Recipe::with_config(
crate::sql::Config {
reuse_type: config.reuse,
..Default::default()
},
config.mir_config.clone(),
permissive_writes,
);
let dataflow_state = DfState::new(
g,
source,
0,
config.sharding,
config.domain_config.clone(),
config.persistence.clone(),
materializations,
recipe,
None,
HashMap::new(),
cc,
config.replication_strategy,
);
Self {
config,
dataflow_state,
}
}
}
#[derive(Clone)]
pub struct Worker {
healthy: bool,
uri: Url,
http: reqwest::Client,
/// Configuration for how domains should be scheduled onto this worker
domain_scheduling_config: WorkerSchedulingConfig,
request_timeout: Duration,
}
impl Worker {
pub fn new(
instance_uri: Url,
domain_scheduling_config: WorkerSchedulingConfig,
request_timeout: Duration,
) -> Self {
Worker {
healthy: true,
uri: instance_uri,
http: reqwest::Client::new(),
domain_scheduling_config,
request_timeout,
}
}
pub async fn rpc<T: DeserializeOwned>(&self, req: WorkerRequestKind) -> ReadySetResult<T> {
let body = hyper::Body::from(bincode::serialize(&req)?);
let http_req = self.http.post(self.uri.join("worker_request")?).body(body);
let resp = http_req
.timeout(self.request_timeout)
.send()
.await
.map_err(|e| ReadySetError::HttpRequestFailed {
request: format!("{:?}", WorkerRequestType::from(&req)),
message: e.to_string(),
})?;
let status = resp.status();
let body = resp
.bytes()
.await
.map_err(|e| ReadySetError::HttpRequestFailed {
request: format!("{:?}", WorkerRequestType::from(&req)),
message: e.to_string(),
})?;
if !status.is_success() {
if status == reqwest::StatusCode::SERVICE_UNAVAILABLE {
return Err(ReadySetError::ServiceUnavailable);
} else if status == reqwest::StatusCode::BAD_REQUEST {
return Err(ReadySetError::SerializationFailed(
"remote server returned 400".into(),
));
} else {
let err: ReadySetError = bincode::deserialize(&body)?;
return Err(err);
}
}
Ok(bincode::deserialize::<T>(&body)?)
}
}
/// Type alias for "a worker's URI" (as reported in a `RegisterPayload`).
type WorkerIdentifier = Url;
/// Channel used to notify the controller of replicator events. This channel conveys information on
/// replicator status and allows us to gracefully kill the controller loop in the event of an
/// unrecoverable replicator error.
pub struct ReplicatorChannel {
sender: UnboundedSender<ReplicatorMessage>,
receiver: UnboundedReceiver<ReplicatorMessage>,
}
impl ReplicatorChannel {
fn new() -> Self {
let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
Self { sender, receiver }
}
fn sender(&self) -> UnboundedSender<ReplicatorMessage> {
self.sender.clone()
}
}
/// An update on the leader election and failure detection.
#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub(crate) enum AuthorityUpdate {
/// The current leader has changed.
///
/// The King is dead; long live the King!
LeaderChange(ControllerDescriptor),
/// We are now the new leader.
WonLeaderElection(ControllerState),
/// New worker detected
NewWorkers(Vec<WorkerDescriptor>),
/// Worker failed.
FailedWorkers(Vec<WorkerDescriptor>),
/// An error occurred in the authority thread, which won't be restarted.
AuthorityError(anyhow::Error),
}
/// An HTTP request made to a controller.
pub struct ControllerRequest {
/// The HTTP method used.
pub method: Method,
/// The path of the request.
pub path: String,
/// The request's query string.
pub query: Option<String>,
/// The request body.
pub body: hyper::body::Bytes,
/// Sender to send the response down.
pub reply_tx: tokio::sync::oneshot::Sender<Result<Result<Vec<u8>, Vec<u8>>, StatusCode>>,
}
/// A request made from a `Handle` to a controller.
pub enum HandleRequest {
/// Inquires whether the controller is ready to accept requests (but see below for caveats).
///
/// This is primarily used in tests; in this context, "ready" means "controller is the leader,
/// with at least 1 worker attached". The result gets sent down the provided sender.
#[allow(dead_code)]
QueryReadiness(tokio::sync::oneshot::Sender<bool>),
/// Performs a manual migration.
PerformMigration {
/// The migration function to perform.
func: Box<
dyn FnOnce(&mut crate::controller::migrate::Migration<'_>) -> ReadySetResult<()>
+ Send
+ 'static,
>,
/// The SQL dialect to use for all migrated queries and types
dialect: Dialect,
/// The result of the migration gets sent down here.
done_tx: tokio::sync::oneshot::Sender<ReadySetResult<()>>,
},
/// Set a failpoint
#[cfg(feature = "failure_injection")]
Failpoint {
name: String,
action: String,
done_tx: tokio::sync::oneshot::Sender<()>,
},
}
/// A structure to hold and manage access to the [`Leader`].
/// The main purpose of this structure is to implement the interior mutability pattern,
/// allowing for multiple threads to have an [`Arc`] reference to it, and to read/write/replace
/// the inner [`Leader`] without having to worry about synchronization.
struct LeaderHandle {
leader: RwLock<Option<Leader>>,
}
impl LeaderHandle {
/// Creates a new, empty [`LeaderHandle`].
fn new() -> Self {
LeaderHandle {
leader: RwLock::new(None),
}
}
/// Replaces the current [`Leader`] with a new one.
/// This method will block if there's a thread currently waiting to acquire or holding
/// the [`Leader`] write lock.
async fn replace(&self, leader: Leader) {
let mut guard = self.leader.write().await;
*guard = Some(leader);
}
/// Acquires a read lock on the [`Leader`].
/// This method will block if there's a thread currently waiting to acquire or holding
/// the [`Leader`] write lock.
async fn read(&self) -> RwLockReadGuard<'_, Option<Leader>> {
self.leader.read().await
}
/// Acquires a write lock on the [`Leader`].
/// This method will block if there's a thread currently waiting to acquire or holding
/// the [`Leader`] write lock.
async fn write(&self) -> RwLockWriteGuard<'_, Option<Leader>> {
self.leader.write().await
}
}
/// A wrapper for the control plane of the server instance that handles: leader election,
/// control rpcs, and requests originated from this server's instance's `Handle`.
pub struct Controller {
/// If we are the leader, the leader object to use for performing leader operations.
inner: Arc<LeaderHandle>,
/// The `Authority` structure used for leadership elections & such state.
authority: Arc<Authority>,
/// Channel to the `Worker` running inside this server instance.
///
/// This is used to convey changes in leadership state.
worker_tx: Sender<WorkerRequest>,
/// Receives external HTTP requests.
http_rx: Receiver<ControllerRequest>,
/// Receives requests from the controller's `Handle`.
handle_rx: Receiver<HandleRequest>,
/// Receives notifications that background tasks have failed
background_task_failed_rx: Receiver<ReadySetError>,
/// Clone to send notifications that background tasks have failed
background_task_failed_tx: Sender<ReadySetError>,
/// A `ControllerDescriptor` that describes this server instance.
our_descriptor: ControllerDescriptor,
/// The descriptor of the worker this controller's server is running.
worker_descriptor: WorkerDescriptor,
/// The config associated with this controller's server.
config: Config,
/// Whether we are the leader and ready to handle requests.
leader_ready: Arc<AtomicBool>,
/// Channel used to notify the controller of replicator events.
replicator_channel: ReplicatorChannel,
/// Provides the ability to report metrics to Segment
telemetry_sender: TelemetrySender,
/// Whether or not to consider failed writes to base tables as no-ops
permissive_writes: bool,
/// Handle used to receive a shutdown signal
shutdown_rx: ShutdownReceiver,
}
impl Controller {
pub(crate) fn new(
authority: Arc<Authority>,
worker_tx: Sender<WorkerRequest>,
controller_rx: Receiver<ControllerRequest>,
handle_rx: Receiver<HandleRequest>,
our_descriptor: ControllerDescriptor,
worker_descriptor: WorkerDescriptor,
telemetry_sender: TelemetrySender,
config: Config,
shutdown_rx: ShutdownReceiver,
) -> Self {
// If we don't have an upstream, we allow permissive writes to base tables.
let permissive_writes = config.replicator_config.upstream_db_url.is_none();
let (background_task_failed_tx, background_task_failed_rx) = mpsc::channel(1);
Self {
inner: Arc::new(LeaderHandle::new()),
authority,
worker_tx,
http_rx: controller_rx,
handle_rx,
background_task_failed_tx,
background_task_failed_rx,
our_descriptor,
worker_descriptor,
config,
leader_ready: Arc::new(AtomicBool::new(false)),
replicator_channel: ReplicatorChannel::new(),
telemetry_sender,
permissive_writes,
shutdown_rx,
}
}
/// Send the provided `WorkerRequestKind` to the worker running in the same server instance as
/// this controller wrapper, but don't bother waiting for the response.
///
/// Not waiting for the response avoids deadlocking when the controller and worker are in the
/// same readyset-server instance.
async fn send_worker_request(&self, kind: WorkerRequestKind) -> ReadySetResult<()> {
let (tx, _rx) = tokio::sync::oneshot::channel();
self.worker_tx
.send(WorkerRequest { kind, done_tx: tx })
.await
.map_err(|e| internal_err!("failed to send to instance worker: {}", e))?;
Ok(())
}
async fn handle_handle_request(&self, req: HandleRequest) -> ReadySetResult<()> {
match req {
HandleRequest::QueryReadiness(tx) => {
let guard = self.inner.read().await;
let leader_ready = self.leader_ready.load(Ordering::Acquire);
let done = leader_ready
&& match guard.as_ref() {
Some(leader) => {
leader.running_recovery.is_none() && {
let ds = leader.dataflow_state_handle.read().await;
!ds.workers.is_empty()
}
}
None => false,
};
if tx.send(done).is_err() {
warn!("readiness query sender hung up!");
}
}
HandleRequest::PerformMigration {
func,
dialect,
done_tx,
} => {
let mut guard = self.inner.write().await;
if let Some(ref mut inner) = *guard {
let mut writer = inner.dataflow_state_handle.write().await;
let ds = writer.as_mut();
let res = ds.migrate(false, dialect, move |m| func(m)).await;
if res.is_ok() {
inner
.dataflow_state_handle
.commit(writer, &self.authority)
.await?;
}
if done_tx.send(res).is_err() {
warn!("handle-based migration sender hung up!");
}
} else {
return Err(ReadySetError::NotLeader);
}
}
#[cfg(feature = "failure_injection")]
HandleRequest::Failpoint {
name,
action,
done_tx,
} => {
info!(%name, %action, "handling failpoint request");
fail::cfg(name, action.as_str()).expect("failed to set failpoint");
if done_tx.send(()).is_err() {
warn!("handle-based failpoint sender hung up!");
}
}
}
Ok(())
}
async fn handle_authority_update(&self, msg: AuthorityUpdate) -> ReadySetResult<()> {
match msg {
AuthorityUpdate::LeaderChange(descr) => {
gauge!(recorded::CONTROLLER_IS_LEADER, 0f64);
self.send_worker_request(WorkerRequestKind::NewController {
controller_uri: descr.controller_uri,
})
.await?;
}
AuthorityUpdate::WonLeaderElection(state) => {
info!("won leader election, creating Leader");
gauge!(recorded::CONTROLLER_IS_LEADER, 1f64);
let background_task_failed_tx = self.background_task_failed_tx.clone();
let mut leader = Leader::new(
state,
self.our_descriptor.controller_uri.clone(),
self.authority.clone(),
background_task_failed_tx,
self.config.replicator_statement_logging,
self.config.replicator_config.clone(),
self.config.worker_request_timeout,
self.config.background_recovery_interval,
);
self.leader_ready.store(false, Ordering::Release);
leader
.start(
self.replicator_channel.sender(),
self.telemetry_sender.clone(),
self.shutdown_rx.clone(),
)
.await;
self.inner.replace(leader).await;
self.send_worker_request(WorkerRequestKind::NewController {
controller_uri: self.our_descriptor.controller_uri.clone(),
})
.await?;
}
AuthorityUpdate::NewWorkers(w) => {
let mut guard = self.inner.write().await;
if let Some(ref mut inner) = *guard {
inner.handle_register_from_authority(w).await?;
} else {
return Err(ReadySetError::NotLeader);
}
}
AuthorityUpdate::FailedWorkers(w) => {
let mut guard = self.inner.write().await;
if let Some(ref mut inner) = *guard {
inner
.handle_failed_workers(w.into_iter().map(|desc| desc.worker_uri).collect())
.await?;
} else {
return Err(ReadySetError::NotLeader);
}
}
AuthorityUpdate::AuthorityError(e) => {
// the authority won't be restarted, so the controller should hard-exit
internal!("controller's authority thread failed: {:#}", e);
}
}
Ok(())
}
/// Run the controller wrapper continuously, processing leadership updates and external
/// requests (if it gets elected).
/// This function returns if the wrapper fails, or the controller request sender is dropped.
pub async fn run(mut self) -> ReadySetResult<()> {
// Start the authority thread responsible for leader election and liveness updates.
let (authority_tx, mut authority_rx) = tokio::sync::mpsc::channel(16);
tokio::spawn(
crate::controller::authority_runner(
authority_tx,
self.authority.clone(),
self.our_descriptor.clone(),
self.worker_descriptor.clone(),
self.config.clone(),
self.permissive_writes,
self.shutdown_rx.clone(),
)
.instrument(info_span!("authority")),
);
let leader_ready = self.leader_ready.clone();
loop {
// There is either...
let running_recovery = self
.inner
.read()
.await
.as_ref()
.and_then(|leader| {
leader.running_recovery.clone().map(|mut rx| {
// a. A currently running recovery, which when it's done will place its
// result in a `tokio::sync::watch`. ...
let inner = Arc::clone(&self.inner);
Either::Left(async move {
// ... We wait until that recovery is done...
let _ = rx.changed().await;
// ... then yield the result of the recovery.
inner
.read()
.await
.as_ref()
.and_then(|leader| leader.running_recovery.as_ref())
.map(|rx| (*rx.borrow()).clone())
.unwrap_or(Ok(()))
})
})
})
// b. No currently running recovery, in which case we don't need to wait for
// anything (but we make a `pending()` future so that we can have a single value
// to await on in the `select`)
.unwrap_or_else(|| Either::Right(future::pending::<ReadySetResult<()>>()));
select! {
req = self.handle_rx.recv() => {
if let Some(req) = req {
self.handle_handle_request(req).await?;
}
else {
info!("Controller shutting down after request handle dropped");
break;
}
}
req = self.http_rx.recv() => {
if let Some(req) = req {
let leader_ready = leader_ready.load(Ordering::Acquire);
tokio::spawn(handle_controller_request(
req,
self.authority.clone(),
self.inner.clone(),
leader_ready
));
}
else {
info!("Controller shutting down after HTTP handle dropped");
break;
}
}
req = authority_rx.recv() => {
set_failpoint!(failpoints::AUTHORITY);
match req {
Some(req) => match self.handle_authority_update(req).await {
Ok(()) => {},
Err(_) if self.shutdown_rx.signal_received() => {
// If we've encountered an error but the shutdown signal has been received, the
// error probably occurred because the server is shutting down
info!("Controller shutting down after shutdown signal received");
break;
}
Err(e) => return Err(e),
}
None => {
if self.shutdown_rx.signal_received() {
// If we've encountered an error but the shutdown signal has been received, the
// error probably occurred because the server is shutting down
info!("Controller shutting down after shutdown signal received");
break;
} else {
// this shouldn't ever happen: if the leadership campaign thread fails,
// it should send a `CampaignError` in `handle_authority_update`.
internal!("leadership thread has unexpectedly failed.")
}
}
}
}
req = self.replicator_channel.receiver.recv() => {
fn now() -> u64 {
#[allow(clippy::unwrap_used)] // won't error comparing to UNIX EPOCH
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64
}
match req {
Some(msg) => match msg {
ReplicatorMessage::UnrecoverableError(e) => return Err(e),
ReplicatorMessage::SnapshotDone => {
self.leader_ready.store(true, Ordering::Release);
let now = now();
if let Err(error) = self.authority.update_persistent_stats(|stats| {
let mut stats = stats.unwrap_or_default();
stats.last_completed_snapshot = Some(now);
Ok(stats)
}).await {
error!(%error, "Failed to persist stats in the Authority");
}
},
ReplicatorMessage::ReplicationStarted => {
let now = now();
if let Err(error) = self.authority.update_persistent_stats(|stats| {
let mut stats = stats.unwrap_or_default();
stats.last_started_replication = Some(now);
// Clear the last replicator error if we have started the main loop.
stats.last_replicator_error = None;
Ok(stats)
}).await {
error!(%error, "Failed to persist stats in the Authority");
}
},
ReplicatorMessage::RecoverableError(e) => {
if let Err(error) = self.authority.update_persistent_stats(|stats| {
let mut stats = stats.unwrap_or_default();
let error = if let ReadySetError::ReplicationFailed(msg) = &e {
// If we have `ReplicationFailed` we can just show the error message
msg.to_string()
} else {
e.to_string()
};
stats.last_replicator_error = Some(error);
Ok(stats)
}).await {
error!(%error, "Failed to persist stats in the Authority");
}
},
},
_ => {
if self.shutdown_rx.signal_received() {
// If we've encountered an error but the shutdown signal has been received, the
// error probably occurred because the server is shutting down
info!("Controller shutting down after shutdown signal received");
break;
} else {
internal!("leader status invalid or channel dropped, leader failed")
}
}
}
}
res = running_recovery => {
res?; // If recovery fails, fail the whole controller (there's not much else we
// can do!)
if let Some(leader) = self.inner.write().await.as_mut() {
// Recovery is done, so we can write back None (and avoid the whole
// rigamarole with .await'ing it)
leader.running_recovery = None;
}
}
err = self.background_task_failed_rx.recv() => {
if let Some(err) = err {
return Err(err)
}
}
_ = self.shutdown_rx.recv() => {
info!("Controller shutting down after shutdown signal received");
break;
}
}
}
if let Err(error) = self.authority.surrender_leadership().await {
error!(%error, "failed to surrender leadership");
internal!("failed to surrender leadership: {}", error)
}
Ok(())
}
}
/// Manages this authority's leader election state and sends update
/// along `event_tx` when the state changes.
struct AuthorityLeaderElectionState {
event_tx: Sender<AuthorityUpdate>,
authority: Arc<Authority>,
descriptor: ControllerDescriptor,
config: Config,
/// True if we are eligible to become the leader.
leader_eligible: bool,
/// True if we are the current leader.
is_leader: bool,
/// Whether or not to treat failed writes to base nodes as no-ops
permissive_writes: bool,
}
impl AuthorityLeaderElectionState {
fn new(
event_tx: Sender<AuthorityUpdate>,
authority: Arc<Authority>,
descriptor: ControllerDescriptor,
permissive_writes: bool,
config: Config,
leader_eligible: bool,
) -> Self {
Self {
event_tx,
authority,
descriptor,
config,
leader_eligible,
is_leader: false,
permissive_writes,
}
}
fn is_leader(&self) -> bool {
self.is_leader
}
async fn watch_leader(&self) -> ReadySetResult<()> {
self.authority.watch_leader().await
}
async fn update_leader_state(&mut self) -> ReadySetResult<()> {
let mut should_attempt_leader_election = false;
match self.authority.try_get_leader().await? {
// The leader has changed, inform the worker.
GetLeaderResult::NewLeader(payload) => {
self.is_leader = false;
let authority_update = AuthorityUpdate::LeaderChange(payload);
self.event_tx
.send(authority_update)
.await
.map_err(|_| internal_err!("send failed"))?;
}
GetLeaderResult::NoLeader if self.leader_eligible => {
should_attempt_leader_election = true;
}
_ => {}
}
if should_attempt_leader_election {
// If we fail to become the leader restart, go back to checking for a new leader.
if self
.authority
.become_leader(self.descriptor.clone())
.await?
.is_none()
{
return Ok(());
}
// We are the new leader, attempt to update the leader state with our state.
let update_res = self
.authority
.update_controller_state(
|state: Option<ControllerState>| -> Result<ControllerState, ()> {
match state {
None => {
Ok(ControllerState::new(self.config.clone(), self.permissive_writes))
},
Some(mut state) => {
// check that running config is compatible with the new
// configuration.
if state.config != self.config {
warn!(
authority_config = ?state.config,
our_config = ?self.config,
"Config in authority different than our config, changing to our config"
);
}
state.dataflow_state.domain_config = self.config.domain_config.clone();
state.dataflow_state.replication_strategy = self.config.replication_strategy;
state.config = self.config.clone();
Ok(state)
}
}
},
|state: &mut ControllerState| {
state.dataflow_state.touch_up();
}
)
.await;
let state = match update_res {
Ok(Ok(state)) => state,
Ok(Err(_)) => return Ok(()),
Err(error) if error.caused_by_serialization_failed() => {
warn!(
%error,
"Error deserializing controller state, wiping state and starting fresh \
(NOTE: this will drop all caches!)"
);
let state = ControllerState::new(self.config.clone(), self.permissive_writes);
let new_state = state.clone(); // needs to be in a `let` binding for Send reasons...
self.authority.overwrite_controller_state(new_state).await?;
state
}
Err(e) => return Err(e),
};
// Notify our worker that we have won the leader election.
self.event_tx
.send(AuthorityUpdate::WonLeaderElection(state))
.await
.map_err(|_| internal_err!("failed to announce who won leader election"))?;
self.is_leader = true;
}
Ok(())
}
}
/// Manages this authority's leader worker state and sends update
/// along `event_tx` when the state changes.
struct AuthorityWorkerState {
event_tx: Sender<AuthorityUpdate>,
authority: Arc<Authority>,
descriptor: WorkerDescriptor,
worker_id: Option<WorkerId>,
active_workers: HashMap<WorkerId, WorkerDescriptor>,
}
impl AuthorityWorkerState {
fn new(
event_tx: Sender<AuthorityUpdate>,
authority: Arc<Authority>,
descriptor: WorkerDescriptor,
) -> Self {
Self {
event_tx,
authority,
descriptor,
worker_id: None,
active_workers: HashMap::new(),
}
}
async fn register(&mut self) -> ReadySetResult<()> {
self.worker_id = self
.authority
.register_worker(self.descriptor.clone())
.await?;
Ok(())
}
async fn heartbeat(&self) -> ReadySetResult<AuthorityWorkerHeartbeatResponse> {
if let Some(id) = &self.worker_id {
return self.authority.worker_heartbeat(id.clone()).await;
}
Ok(AuthorityWorkerHeartbeatResponse::Failed)
}
fn clear_active_workers(&mut self) {
self.active_workers.clear();
}
async fn watch_workers(&self) -> ReadySetResult<()> {
self.authority.watch_workers().await
}
async fn update_worker_state(&mut self) -> anyhow::Result<()> {
// Retrieve the worker ids of current workers.
let workers = self.authority.get_workers().await?;
let failed_workers: Vec<_> = self
.active_workers
.iter()
.filter_map(|(w, _)| {
if !workers.contains(w) {
Some(w.clone())
} else {
None
}
})
.collect();
// Get the descriptors of the failed workers, removing them
// from the active worker set.
let failed_descriptors: Vec<_> = failed_workers
.iter()
.map(|w| {
// The key was just pulled from the map above.
#[allow(clippy::unwrap_used)]
self.active_workers.remove(w).unwrap()
})
.collect();
if !failed_descriptors.is_empty() {
self.event_tx
.send(AuthorityUpdate::FailedWorkers(failed_descriptors))
.await
.map_err(|_| format_err!("failed to announce failed workers"))?;
}
let new_workers = workers
.into_iter()
.filter(|w| !self.active_workers.contains_key(w))
.collect();
// Get the descriptors of the new workers, adding them to the
// active workers set.
let new_descriptor_map = self.authority.worker_data(new_workers).await?;
let new_descriptors: Vec<WorkerDescriptor> = new_descriptor_map.values().cloned().collect();
self.active_workers.extend(new_descriptor_map);
if !new_descriptors.is_empty() {
self.event_tx
.send(AuthorityUpdate::NewWorkers(new_descriptors))
.await
.map_err(|_| format_err!("failed to announce new workers"))?;
}
Ok(())
}
}
async fn authority_inner(
event_tx: Sender<AuthorityUpdate>,
authority: Arc<Authority>,
descriptor: ControllerDescriptor,
worker_descriptor: WorkerDescriptor,
config: Config,
permissive_writes: bool,
) -> anyhow::Result<()> {
authority.init().await?;