/
controller.rs
967 lines (867 loc) · 34.4 KB
/
controller.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
use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::future::Future;
use std::net::SocketAddr;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::{Duration, Instant};
use futures_util::future;
use hyper::client::HttpConnector;
use nom_sql::{NonReplicatedRelation, Relation};
use parking_lot::RwLock;
use petgraph::graph::NodeIndex;
use readyset_errors::{
internal, internal_err, rpc_err, rpc_err_no_downcast, ReadySetError, ReadySetResult,
};
use replication_offset::ReplicationOffsets;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use tokio::sync::Mutex;
use tower::ServiceExt;
use tower_service::Service;
use tracing::{debug, trace};
use url::Url;
use crate::consensus::{Authority, AuthorityControl};
use crate::debug::info::{GraphInfo, MaterializationInfo, NodeSize};
use crate::debug::stats;
use crate::internal::{DomainIndex, ReplicaAddress};
use crate::metrics::MetricsDump;
use crate::recipe::changelist::ChangeList;
use crate::recipe::{CacheExpr, ExtendRecipeResult, ExtendRecipeSpec, MigrationStatus};
use crate::status::ReadySetControllerStatus;
use crate::table::{PersistencePoint, Table, TableBuilder, TableRpc};
use crate::view::{View, ViewBuilder, ViewRpc};
use crate::{
ReplicationOffset, SingleKeyEviction, TableStatus, ViewCreateRequest, ViewFilter, ViewRequest,
};
mod rpc;
const EXTEND_RECIPE_POLL_INTERVAL: Duration = Duration::from_secs(1);
const WAIT_FOR_ALL_TABLES_TO_COMPACT_POLL_INTERVAL: Duration = Duration::from_secs(5);
/// Describes a running controller instance.
///
/// A serialized version of this struct is stored in the Authority so that clients can reach the
/// currently active controller.
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct ControllerDescriptor {
pub controller_uri: Url,
pub nonce: u64,
}
fn make_http_client(timeout: Option<Duration>) -> hyper::Client<hyper::client::HttpConnector> {
let mut http_connector = HttpConnector::new();
http_connector.set_connect_timeout(timeout);
hyper::Client::builder()
.http2_only(true)
// Sets to the keep alive default if request_timeout is not specified.
.http2_keep_alive_timeout(timeout.unwrap_or(Duration::from_secs(20)))
.build(http_connector)
}
/// Errors that can occur when making a request to a controller
struct ControllerRequestError {
/// The error itself
error: ReadySetError,
/// Does this error mean that the controller URL used to make the request is invalid, and a new
/// URL should be retrieved from the authority
invalidate_url: bool,
/// Is this error permanent, meaning the request should not be retried?
permanent: bool,
}
impl<E> From<E> for ControllerRequestError
where
ReadySetError: From<E>,
{
fn from(error: E) -> Self {
Self {
error: error.into(),
invalidate_url: false,
permanent: true,
}
}
}
async fn controller_request(
url: &Url,
client: &hyper::Client<hyper::client::HttpConnector>,
req: ControllerRequest,
timeout: Duration,
) -> Result<hyper::body::Bytes, ControllerRequestError> {
// FIXME(eta): error[E0277]: the trait bound `Uri: From<&Url>` is not satisfied
// (if you try and use the `url` directly instead of stringifying)
#[allow(clippy::unwrap_used)]
let string_url = url.join(req.path)?.to_string();
let r = hyper::Request::post(string_url)
.body(hyper::Body::from(req.request.clone()))
.map_err(|e| internal_err!("http request failed: {}", e))?;
let res = match tokio::time::timeout(timeout, client.request(r)).await {
Ok(Ok(v)) => v,
Ok(Err(e)) => {
return Err(ControllerRequestError {
error: e.into(),
invalidate_url: true,
permanent: false,
})
}
Err(_) => {
internal!("request timeout reached!");
}
};
let status = res.status();
let body = hyper::body::to_bytes(res.into_body())
.await
.map_err(|he| internal_err!("hyper response failed: {}", he))?;
match status {
hyper::StatusCode::OK => Ok(body),
hyper::StatusCode::INTERNAL_SERVER_ERROR => {
let err: ReadySetError = bincode::deserialize(&body)?;
Err(err.into())
}
s => Err(ControllerRequestError {
error: internal_err!("HTTP status {s}"),
invalidate_url: s == hyper::StatusCode::SERVICE_UNAVAILABLE,
permanent: false,
}),
}
}
/// A direct handle to a controller instance running at a known URL, without access to an authority
#[derive(Clone)]
struct RawController {
url: Url,
client: hyper::Client<hyper::client::HttpConnector>,
request_timeout: Option<Duration>,
}
impl RawController {
/// Create a new handle to a controller instance with a known URL, optionally configured to use
/// the given timeout for requests
fn new(url: Url, request_timeout: Option<Duration>) -> Self {
Self {
url,
client: make_http_client(request_timeout),
request_timeout,
}
}
}
impl Service<ControllerRequest> for RawController {
type Response = hyper::body::Bytes;
type Error = ReadySetError;
type Future = impl Future<Output = Result<Self::Response, Self::Error>> + Send;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: ControllerRequest) -> Self::Future {
trace!(?req, "Issuing controller RPC");
let url = self.url.clone();
let client = self.client.clone();
let request_timeout = self.request_timeout.unwrap_or(Duration::MAX);
async move {
controller_request(&url, &client, req, request_timeout)
.await
.map_err(|e| e.error)
}
}
}
#[derive(Clone)]
struct Controller {
authority: Arc<Authority>,
client: hyper::Client<hyper::client::HttpConnector>,
/// The last valid leader URL seen by this service. Used to circumvent requests to Consul in
/// the happy-path.
leader_url: Arc<RwLock<Option<Url>>>,
}
#[derive(Debug, Clone)]
struct ControllerRequest {
path: &'static str,
request: Vec<u8>,
timeout: Option<Duration>,
}
impl ControllerRequest {
fn new<Q: Serialize>(
path: &'static str,
r: Q,
timeout: Option<Duration>,
) -> ReadySetResult<Self> {
Ok(ControllerRequest {
path,
request: bincode::serialize(&r)?,
timeout,
})
}
}
impl Service<ControllerRequest> for Controller {
type Response = hyper::body::Bytes;
type Error = ReadySetError;
type Future = impl Future<Output = Result<Self::Response, Self::Error>> + Send;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: ControllerRequest) -> Self::Future {
trace!(?req, "Issuing controller RPC");
let client = self.client.clone();
let auth = self.authority.clone();
let leader_url = self.leader_url.clone();
let request_timeout = req.timeout.unwrap_or(Duration::MAX);
let start = Instant::now();
let mut last_error_desc: Option<String> = None;
async move {
let original_url = leader_url.read().clone();
let mut url = original_url.clone();
loop {
let elapsed = Instant::now().duration_since(start);
if elapsed >= request_timeout {
internal!(
"request timeout reached; last error: {}",
last_error_desc.unwrap_or_else(|| "(none)".into())
);
}
let url_ = match &url {
Some(u) => u,
None => {
let descriptor = match tokio::time::timeout(
request_timeout - elapsed,
auth.get_leader(),
)
.await
{
Ok(Ok(url)) => url,
Ok(Err(_)) | Err(_) => {
return Err(ReadySetError::ControllerUnavailable);
}
};
url.insert(descriptor.controller_uri)
}
};
match controller_request(url_, &client, req.clone(), request_timeout - elapsed)
.await
{
Ok(res) => {
if url != original_url {
*leader_url.write() = url;
}
return Ok(res);
}
Err(ControllerRequestError {
error,
invalidate_url,
permanent,
}) => {
if permanent {
return Err(error);
}
last_error_desc = Some(error.to_string());
if invalidate_url {
url = None
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
}
}
}
}
/// Options for generating graphviz [dot][] visualizations of the ReadySet dataflow graph.
///
/// Used as the argument to [`ReadySetHandle::graphviz`].
///
/// [dot]: https://graphviz.org/doc/info/lang.html
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphvizOptions {
/// Limit to only visualizing the graph for a single query by name
pub for_query: Option<Relation>,
/// Generate a detailed representation of the graph, larger and with more information
pub detailed: bool,
}
impl Default for GraphvizOptions {
fn default() -> Self {
Self {
for_query: None,
detailed: true,
}
}
}
/// A handle to a ReadySet controller.
///
/// This handle is the primary mechanism for interacting with a running ReadySet instance, and lets
/// you add and remove queries, retrieve handles for inserting or querying the underlying data, and
/// to perform meta-operations such as fetching the dataflow's GraphViz visualization.
///
/// To establish a new connection to ReadySet, use `ReadySetHandle::new`, and pass in the
/// appropriate `Authority`.
///
/// Note that whatever Tokio Runtime you use to execute the `Future` that resolves into the
/// `ReadySetHandle` will also be the one that executes all your reads and writes through `View`
/// and `Table`. Make sure that that `Runtime` stays alive, and continues to be driven, otherwise
/// none of your operations will ever complete! Furthermore, you *must* use the `Runtime` to
/// execute any futures returned from `ReadySetHandle` (that is, you cannot just call `.wait()`
/// on them).
#[derive(Clone)]
pub struct ReadySetHandle {
handle: tower::util::Either<Controller, RawController>,
domains: Arc<Mutex<HashMap<(SocketAddr, usize), TableRpc>>>,
views: Arc<Mutex<HashMap<(SocketAddr, usize), ViewRpc>>>,
request_timeout: Option<Duration>,
migration_timeout: Option<Duration>,
}
/// Define a simple RPC request wrapper for the controller, which queries the same RPC endpoint as
/// the name of the function and takes no arguments.
///
/// This is a common enough pattern that it's worth defining a macro for, but anything more complex
/// than this is worth defining as a regular function
macro_rules! simple_request {
($(#[$($attrss:tt)*])* $name: ident()) => { simple_request!($name() -> ()); };
($(#[$($attrss:tt)*])* $name: ident() -> $ret: ty) => {
$(#[$($attrss)*])*
pub fn $name(&mut self) -> impl Future<Output = ReadySetResult<$ret>> + '_ {
self.rpc(stringify!($name), (), self.request_timeout)
}
};
($(#[$($attrss:tt)*])* $name: ident($arg: ident : $ty: ty) -> $ret: ty) => {
$(#[$($attrss)*])*
pub fn $name(&mut self, $arg : $ty) -> impl Future<Output = ReadySetResult<$ret>> + '_ {
self.rpc(stringify!($name), $arg, self.request_timeout)
}
};
($(#[$($attrss:tt)*])* $name: ident($($arg: ident : $ty: ty,)+) -> $ret: ty) => {
$(#[$($attrss)*])*
pub fn $name(&mut self, $($arg : $ty,)+) -> impl Future<Output = ReadySetResult<$ret>> + '_ {
self.rpc(stringify!($name), ($($arg,)+), self.request_timeout)
}
};
}
impl ReadySetHandle {
pub fn make(
authority: Arc<Authority>,
request_timeout: Option<Duration>,
migration_timeout: Option<Duration>,
) -> Self {
ReadySetHandle {
views: Default::default(),
domains: Default::default(),
handle: tower::util::Either::A(Controller {
authority,
client: make_http_client(request_timeout),
leader_url: Arc::new(RwLock::new(None)),
}),
request_timeout,
migration_timeout,
}
}
/// Make a new controller handle for connecting directly to a controller running at the given
/// URL, without access to an authority
pub fn make_raw(
url: Url,
request_timeout: Option<Duration>,
migration_timeout: Option<Duration>,
) -> Self {
ReadySetHandle {
views: Default::default(),
domains: Default::default(),
handle: tower::util::Either::B(RawController::new(url, request_timeout)),
request_timeout,
migration_timeout,
}
}
/// Check that the `ReadySetHandle` can accept another request.
///
/// Note that this method _must_ return `Poll::Ready` before any other methods that return
/// a `Future` on `ReadySetHandle` can be called.
pub fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<ReadySetResult<()>> {
self.handle
.poll_ready(cx)
.map_err(rpc_err!("ReadySetHandle::poll_ready"))
}
/// A future that resolves when the controller can accept more messages.
///
/// When this future resolves, you it is safe to call any methods that require `poll_ready` to
/// have returned `Poll::Ready`.
pub async fn ready(&mut self) -> ReadySetResult<()> {
future::poll_fn(move |cx| self.poll_ready(cx)).await
}
/// Create a [`ReadySetHandle`] that bootstraps a connection to ReadySet via the configuration
/// stored in the given `authority`. Assigns the authority no timeouts for requests and
/// migrations.
pub async fn new<I: Into<Arc<Authority>>>(authority: I) -> Self {
Self::make(authority.into(), None, None)
}
/// Create a [`ReadySetHandle`] that bootstraps a connection to ReadySet via the configuration
/// stored in the given `authority`. Assigns a timeout to requests to ReadySet, `timeout`.
pub async fn with_timeouts<I: Into<Arc<Authority>>>(
authority: I,
request_timeout: Option<Duration>,
migration_timeout: Option<Duration>,
) -> Self {
Self::make(authority.into(), request_timeout, migration_timeout)
}
/// Issues a POST request to the given path with no body.
async fn simple_post_request<R>(&mut self, path: &'static str) -> ReadySetResult<R>
where
R: DeserializeOwned,
{
let body: hyper::body::Bytes = self
.handle
.ready()
.await
.map_err(rpc_err!(format_args!("ReadySetHandle::{}", path)))?
.call(ControllerRequest::new(path, (), self.request_timeout)?)
.await
.map_err(rpc_err!(format_args!("ReadySetHandle::{}", path)))?;
bincode::deserialize(&body)
.map_err(ReadySetError::from)
.map_err(Box::new)
.map_err(rpc_err!(format_args!("ReadySetHandle::{}", path)))
}
/// Enumerate all known base tables.
///
/// These have all been created in response to a `CREATE TABLE` statement in a recipe.
pub async fn tables(&mut self) -> ReadySetResult<BTreeMap<Relation, NodeIndex>> {
self.simple_post_request("tables").await
}
/// Query the status of all known tables, including those not replicated by ReadySet
pub async fn table_statuses(&mut self) -> ReadySetResult<BTreeMap<Relation, TableStatus>> {
self.simple_post_request("table_statuses").await?
}
/// Return a list of all relations (tables or views) which are known to exist in the upstream
/// database that we are replicating from, but are not being replicated to ReadySet (which are
/// recorded via [`Change::AddNonReplicatedRelation`]).
///
/// [`Change::AddNonReplicatedRelation`]: readyset_client::recipe::changelist::Change::AddNonReplicatedRelation
pub async fn non_replicated_relations(
&mut self,
) -> ReadySetResult<HashSet<NonReplicatedRelation>> {
self.simple_post_request("non_replicated_relations").await
}
/// Enumerate all known external views.
///
/// These have all been created in response to a `CREATE CACHE` or `CREATE VIEW` statement in a
/// recipe.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
pub async fn views(&mut self) -> ReadySetResult<BTreeMap<Relation, NodeIndex>> {
self.simple_post_request("views").await
}
/// Enumerate all known external views. Includes the SqlQuery that created
/// the view and the fallback behavior.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call
/// this method.
pub async fn verbose_views(&mut self) -> ReadySetResult<Vec<CacheExpr>> {
self.simple_post_request("verbose_views").await
}
simple_request!(
/// For each of the given list of queries, determine whether that query (or a semantically
/// equivalent query) has been created as a `View`.
///
/// To save on data, this returns a list of `Option<Relation>` corresponding to the provided
/// list of queries, where each option is `Some(<query name>)` if the query at the same
/// position in the argument list has been installed as a view.
view_names(
queries: Vec<ViewCreateRequest>,
dialect: dataflow_expression::Dialect,
) -> Vec<Option<Relation>>
);
/// Obtain a `View` that allows you to query the given external view.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
pub fn view<I: Into<Relation>>(
&mut self,
name: I,
) -> impl Future<Output = ReadySetResult<View>> + '_ {
let request = ViewRequest {
name: name.into(),
filter: None,
};
self.request_view(request)
}
/// Obtain a `View` from the given pool of workers, that allows you to query the given external
/// view.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
pub fn view_from_workers<I: Into<Relation>>(
&mut self,
name: I,
workers: Vec<Url>,
) -> impl Future<Output = ReadySetResult<View>> + '_ {
let request = ViewRequest {
name: name.into(),
filter: Some(ViewFilter::Workers(workers)),
};
self.request_view(request)
}
/// Obtain the replica of a `View` with the given replica index
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
pub fn view_with_replica<I: Into<Relation>>(
&mut self,
name: I,
replica: usize,
) -> impl Future<Output = ReadySetResult<View>> + '_ {
self.request_view(ViewRequest {
name: name.into(),
filter: Some(ViewFilter::Replica(replica)),
})
}
/// Obtain a 'ViewBuilder' for a specific view that allows you to build a view.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
/// This is made public for inspection in integration tests and is not meant to be
/// used to construct views, instead use `view`, which calls this method.
pub async fn view_builder(&mut self, view_request: ViewRequest) -> ReadySetResult<ViewBuilder> {
let body: hyper::body::Bytes = self
.handle
.ready()
.await
.map_err(rpc_err!("ReadySetHandle::view_builder"))?
.call(ControllerRequest::new(
"view_builder",
&view_request,
self.request_timeout,
)?)
.await
.map_err(rpc_err!("ReadySetHandle::view_builder"))?;
match bincode::deserialize::<ReadySetResult<Option<ViewBuilder>>>(&body)?
.map_err(|e| rpc_err_no_downcast("ReadySetHandle::view_builder", e))?
{
Some(vb) => Ok(vb),
None => match view_request.filter {
Some(ViewFilter::Workers(w)) => Err(ReadySetError::ViewNotFoundInWorkers {
name: view_request.name.display_unquoted().to_string(),
workers: w,
}),
_ => Err(ReadySetError::ViewNotFound(
view_request.name.display_unquoted().to_string(),
)),
},
}
}
fn request_view(
&mut self,
view_request: ViewRequest,
) -> impl Future<Output = ReadySetResult<View>> + '_ {
let views = self.views.clone();
async move {
let replica = if let Some(ViewFilter::Replica(replica)) = &view_request.filter {
Some(*replica)
} else {
None
};
let view_builder = self.view_builder(view_request).await?;
view_builder.build(replica, views).await
}
}
/// Obtain a `Table` that allows you to perform writes, deletes, and other operations on the
/// given base table, looking up the table by its name
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
pub fn table<N>(&mut self, name: N) -> impl Future<Output = ReadySetResult<Table>> + '_
where
N: Into<Relation>,
{
let name = name.into();
async move {
self.request_table(ControllerRequest::new(
"table_builder",
&name,
self.request_timeout,
)?)
.await?
.ok_or_else(|| ReadySetError::TableNotFound {
name: name.name.clone().into(),
schema: name.schema.clone().map(Into::into),
})
}
}
/// Obtain a `Table` that allows you to perform writes, deletes, and other operations on the
/// given base table, looking up the table by its node index
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
pub async fn table_by_index(&mut self, ni: NodeIndex) -> ReadySetResult<Table> {
self.request_table(ControllerRequest::new(
"table_builder_by_index",
ni,
self.request_timeout,
)?)
.await?
.ok_or_else(|| ReadySetError::NoSuchNode(ni.index()))
}
fn request_table(
&mut self,
req: ControllerRequest,
) -> impl Future<Output = ReadySetResult<Option<Table>>> + '_ {
let domains = self.domains.clone();
async move {
let body: hyper::body::Bytes = self
.handle
.ready()
.await
.map_err(rpc_err!("ReadySetHandle::table"))?
.call(req)
.await
.map_err(rpc_err!("ReadySetHandle::table"))?;
match bincode::deserialize::<ReadySetResult<Option<TableBuilder>>>(&body)?
.map_err(|e| rpc_err_no_downcast("ReadySetHandle::table", e))?
{
Some(tb) => Ok(Some(tb.build(domains).await)),
None => Ok(None),
}
}
}
simple_request!(
/// Get statistics about the time spent processing different parts of the graph.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
statistics() -> stats::GraphStats
);
simple_request!(
/// Flush all partial state, evicting all rows present.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
flush_partial()
);
/// Performs a dry-run migration with the given set of queries.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
pub fn dry_run(
&mut self,
changes: ChangeList,
) -> impl Future<Output = ReadySetResult<ExtendRecipeResult>> + '_ {
let request = ExtendRecipeSpec::from(changes);
self.rpc("dry_run", request, self.migration_timeout)
}
/// Extend the existing recipe with the given set of queries.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
pub fn extend_recipe(
&mut self,
changes: ChangeList,
) -> impl Future<Output = ReadySetResult<()>> + '_ {
let request = ExtendRecipeSpec::from(changes);
async move {
match self
.rpc("extend_recipe", request, self.migration_timeout)
.await?
{
ExtendRecipeResult::Done => Ok(()),
ExtendRecipeResult::Pending(migration_id) => {
while self.migration_status(migration_id).await?.is_pending() {
tokio::time::sleep(EXTEND_RECIPE_POLL_INTERVAL).await;
}
Ok(())
}
}
}
}
/// Query the status of a pending migration identified by the given `migration_id`.
pub fn migration_status(
&mut self,
migration_id: u64,
) -> impl Future<Output = ReadySetResult<MigrationStatus>> + '_ {
self.rpc::<_, MigrationStatus>("migration_status", migration_id, self.request_timeout)
}
/// Asynchronous version of extend_recipe(). The Controller should immediately return an ID that
/// can be used to query the migration status.
pub fn extend_recipe_async(
&mut self,
changes: ChangeList,
) -> impl Future<Output = ReadySetResult<u64>> + '_ {
let request = ExtendRecipeSpec::from(changes).concurrently();
async move {
match self
.rpc("extend_recipe", request, self.migration_timeout)
.await?
{
ExtendRecipeResult::Pending(migration_id) => Ok(migration_id),
ExtendRecipeResult::Done => {
internal!("CREATE CACHE CONCURRENTLY did not return migration id")
}
}
}
}
/// Extend the existing recipe with the given set of queries and don't require leader ready.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
pub fn extend_recipe_no_leader_ready(
&mut self,
changes: ChangeList,
) -> impl Future<Output = ReadySetResult<()>> + '_ {
let request = ExtendRecipeSpec {
require_leader_ready: false,
..changes.into()
};
self.rpc("extend_recipe", request, self.migration_timeout)
}
/// Extend the existing recipe with the given set of queries and an optional replication offset
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
pub fn extend_recipe_with_offset(
&mut self,
changes: ChangeList,
replication_offset: &ReplicationOffset,
require_leader_ready: bool,
) -> impl Future<Output = ReadySetResult<()>> + '_ {
let request = ExtendRecipeSpec {
changes,
replication_offset: Some(Cow::Borrowed(replication_offset)),
require_leader_ready,
concurrently: false,
};
self.rpc("extend_recipe", request, self.migration_timeout)
}
simple_request!(
/// Remove all nodes related to the query with the given name
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
remove_query(name: &Relation) -> u64
);
simple_request!(
/// Remove all non-base nodes from the graph
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
remove_all_queries()
);
simple_request!(
/// Set the replication offset for the schema, which is stored with the recipe.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
set_schema_replication_offset(
replication_offset: Option<&ReplicationOffset>,
) -> ()
);
simple_request!(
/// Fetch a graphviz description of the dataflow graph.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
graphviz(options: GraphvizOptions) -> String
);
simple_request!(
/// Fetch a simplified graphviz description of the dataflow graph.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
simple_graphviz() -> String
);
simple_request!(
/// Replicate the readers associated with the list of queries to the given worker.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
get_instances() -> Vec<(Url, bool)>
);
simple_request!(
/// Query the controller for information about the graph.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
get_info() -> GraphInfo
);
simple_request!(
/// Remove the given external view from the graph.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
remove_node(
// TODO: this should likely take a view name, and we should verify that it's a Reader.
view: NodeIndex,
) -> ()
);
simple_request!(
/// Fetch a dump of metrics values from the running noria instance
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
metrics_dump() -> MetricsDump
);
simple_request!(
/// Get a list of all registered worker URIs.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
workers() -> Vec<Url>
);
simple_request!(
/// Get a list of all registered worker URIs that are currently healthy.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
healthy_workers() -> Vec<Url>
);
simple_request!(
/// Get a map from domain index to a shard->replica mapping of the workers that are running the
/// shard replicas of that domain .
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
domains() -> HashMap<DomainIndex, Vec<Vec<Option<Url>>>>
);
simple_request!(
/// Get information about all materializations (stateful nodes) within the graph
materialization_info() -> Vec<MaterializationInfo>
);
simple_request!(
/// Get the url of the current noria controller.
///
/// `Self::poll_ready` must have returned `Async::Ready` before you call this method.
controller_uri() -> Url
);
simple_request!(
/// Get a set of all replication offsets for the entire system.
///
/// See [the documentation for
/// PersistentState](::readyset_dataflow::state::persistent_state) for more information
/// about replication offsets.
replication_offsets() -> ReplicationOffsets
);
simple_request!(
/// Each base table has an offset up to which data has been persisted to disk, and this
/// method returns the minimum of those offsets. If no base tables have unpersisted data,
/// this method returns `PersistencePoint::Persisted`.
///
/// See [the documentation for PersistentState](::readyset_dataflow::state::persistent_state)
/// for more information about replication offsets.
min_persisted_replication_offset() -> PersistencePoint
);
/// Poll in a loop to wait for all tables to finish compacting
pub async fn wait_for_all_tables_to_compact(&mut self) -> ReadySetResult<()> {
while !self
.rpc("all_tables_compacted", (), self.request_timeout)
.await?
{
debug!("Waiting for all tables to compact");
tokio::time::sleep(WAIT_FOR_ALL_TABLES_TO_COMPACT_POLL_INTERVAL).await;
}
Ok(())
}
simple_request!(
/// Return a map of node indices to key counts.
node_sizes() -> HashMap<NodeIndex, NodeSize>
);
simple_request!(
/// Return whether the leader is ready or not.
leader_ready() -> bool
);
simple_request!(
/// Returns the ReadySetControllerStatus struct returned by the leader.
status() -> ReadySetControllerStatus
);
simple_request!(
/// Returns true if topk and pagination support are enabled on the server
supports_pagination() -> bool
);
simple_request!(
/// Returns the server's release version
version() -> String
);
simple_request!(
/// Returns the amount of actually allocated memory
allocated_bytes() -> Option<usize>
);
simple_request!(
/// Set memory limit parameters
set_memory_limit(
period: Option<Duration>,
limit: Option<usize>,
) -> ()
);
simple_request!(
/// Evict a single key from a partial index. If no `eviction_request` is provided, a random
/// key and partial index will be selected. Returns a [`SingleKeyEviction`] if an eviction
/// occurred.
evict_single(eviction_request: Option<SingleKeyEviction>) -> Option<SingleKeyEviction>
);
#[cfg(feature = "failure_injection")]
simple_request!(
/// Set a failpoint with provided name and action
failpoint(name: String, action: String,) -> ()
);
simple_request!(
/// Notify the controller that a running domain replica has died
domain_died(replica_address: ReplicaAddress) -> ()
);
}