-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathlib.rs
871 lines (802 loc) · 35.2 KB
/
lib.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
/*
* Copyright (c) Dell Inc., or its subsidiaries. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
#![deny(
clippy::all,
clippy::cargo,
clippy::else_if_without_else,
clippy::empty_line_after_outer_attr,
clippy::multiple_inherent_impl,
clippy::mut_mut,
clippy::path_buf_push_overwrite
)]
#![warn(
clippy::cargo_common_metadata,
clippy::mutex_integer,
clippy::needless_borrow,
clippy::similar_names
)]
#![allow(clippy::multiple_crate_versions)]
#![allow(dead_code)]
use std::result::Result as StdResult;
use std::time::{Duration, Instant};
use snafu::Snafu;
use tonic::transport::channel::Channel;
use tonic::{Code, Status};
use async_trait::async_trait;
use controller::{
controller_service_client::ControllerServiceClient, create_scope_status, create_stream_status,
delete_scope_status, delete_stream_status, ping_txn_status, scale_request, scale_response,
scale_status_response, txn_state, txn_status, update_stream_status, CreateScopeStatus,
CreateStreamStatus, CreateTxnRequest, CreateTxnResponse, DeleteScopeStatus, DeleteStreamStatus, NodeUri,
PingTxnRequest, PingTxnStatus, ScaleRequest, ScaleResponse, ScaleStatusRequest, ScaleStatusResponse,
ScopeInfo, SegmentId, SegmentRanges, StreamConfig, StreamInfo, SuccessorResponse, TxnId, TxnRequest,
TxnState, TxnStatus, UpdateStreamStatus,
};
use log::debug;
use pravega_rust_client_retry::retry_async::retry_async;
use pravega_rust_client_retry::retry_policy::RetryWithBackoff;
use pravega_rust_client_retry::retry_result::RetryResult;
use pravega_rust_client_shared::*;
use pravega_wire_protocol::client_config::ClientConfig;
use std::convert::{From, Into};
use std::str::FromStr;
use std::sync::RwLock;
use tonic::codegen::http::uri::InvalidUri;
use tonic::transport::Uri;
#[allow(non_camel_case_types)]
pub mod controller {
tonic::include_proto!("io.pravega.controller.stream.api.grpc.v1");
// this is the rs file name generated after compiling the proto file, located inside the target folder.
}
pub mod mock_controller;
mod model_helper;
#[cfg(test)]
mod test;
#[derive(Debug, Snafu)]
pub enum ControllerError {
#[snafu(display(
"Controller client failed to perform operation {} due to {}",
operation,
error_msg,
))]
OperationError {
can_retry: bool,
operation: String,
error_msg: String,
},
#[snafu(display("Could not connect to controller due to {}", error_msg))]
ConnectionError { can_retry: bool, error_msg: String },
#[snafu(display("Invalid configuration passed to the Controller client. Error {}", error_msg))]
InvalidConfiguration { can_retry: bool, error_msg: String },
}
pub type Result<T> = StdResult<T, ControllerError>;
/// Controller APIs for administrative action for streams
#[async_trait]
pub trait ControllerClient: Send + Sync {
/**
* API to create a scope. The future completes with true in the case the scope did not exist
* when the controller executed the operation. In the case of a re-attempt to create the
* same scope, the future completes with false to indicate that the scope existed when the
* controller executed the operation.
*/
async fn create_scope(&self, scope: &Scope) -> Result<bool>;
async fn list_streams(&self, scope: &Scope) -> Result<Vec<String>>;
/**
* API to delete a scope. Note that a scope can only be deleted in the case is it empty. If
* the scope contains at least one stream, then the delete request will fail.
*/
async fn delete_scope(&self, scope: &Scope) -> Result<bool>;
/**
* API to create a stream. The future completes with true in the case the stream did not
* exist when the controller executed the operation. In the case of a re-attempt to create
* the same stream, the future completes with false to indicate that the stream existed when
* the controller executed the operation.
*/
async fn create_stream(&self, stream_config: &StreamConfiguration) -> Result<bool>;
/**
* API to update the configuration of a Stream.
*/
async fn update_stream(&self, stream_config: &StreamConfiguration) -> Result<bool>;
/**
* API to Truncate stream. This api takes a stream cut point which corresponds to a cut in
* the stream segments which is consistent and covers the entire key range space.
*/
async fn truncate_stream(&self, stream_cut: &StreamCut) -> Result<bool>;
/**
* API to seal a Stream.
*/
async fn seal_stream(&self, stream: &ScopedStream) -> Result<bool>;
/**
* API to delete a stream. Only a sealed stream can be deleted.
*/
async fn delete_stream(&self, stream: &ScopedStream) -> Result<bool>;
// Controller APIs called by Pravega producers for getting stream specific information
/**
* API to get list of current segments for the stream to write to.
*/
async fn get_current_segments(&self, stream: &ScopedStream) -> Result<StreamSegments>;
/**
* API to create a new transaction. The transaction timeout is relative to the creation time.
*/
async fn create_transaction(&self, stream: &ScopedStream, lease: Duration) -> Result<TxnSegments>;
/**
* API to send transaction heartbeat and increase the transaction timeout by lease amount of milliseconds.
*/
async fn ping_transaction(
&self,
stream: &ScopedStream,
tx_id: TxId,
lease: Duration,
) -> Result<PingStatus>;
/**
* Commits a transaction, atomically committing all events to the stream, subject to the
* ordering guarantees specified in {@link EventStreamWriter}. Will fail if the transaction has
* already been committed or aborted.
*/
async fn commit_transaction(
&self,
stream: &ScopedStream,
tx_id: TxId,
writer_id: WriterId,
time: Timestamp,
) -> Result<()>;
/**
* Aborts a transaction. No events written to it may be read, and no further events may be
* written. Will fail with if the transaction has already been committed or aborted.
*/
async fn abort_transaction(&self, stream: &ScopedStream, tx_id: TxId) -> Result<()>;
/**
* Returns the status of the specified transaction.
*/
async fn check_transaction_status(&self, stream: &ScopedStream, tx_id: TxId)
-> Result<TransactionStatus>;
// Controller APIs that are called by readers
/**
* Given a segment return the endpoint that currently is the owner of that segment.
*
* This is called when a reader or a writer needs to determine which host/server it needs to contact to
* read and write, respectively. The result of this function can be cached until the endpoint is
* unreachable or indicates it is no longer the owner.
*/
async fn get_endpoint_for_segment(&self, segment: &ScopedSegment) -> Result<PravegaNodeUri>;
/**
* Refreshes an expired/non-existent delegation token.
* @param scope Scope of the stream.
* @param streamName Name of the stream.
* @return The delegation token for the given stream.
*/
async fn get_or_refresh_delegation_token_for(&self, stream: ScopedStream) -> Result<DelegationToken>;
///
/// Fetch the successors for a given Segment.
///
async fn get_successors(&self, segment: &ScopedSegment) -> Result<StreamSegmentsWithPredecessors>;
///
/// Scale a Stream to the new key ranges. This API returns a result once the scale operation has completed.
/// This internally uses the check_scale API to verify the Stream Scaling status.
///
async fn scale_stream(
&self,
stream: &ScopedStream,
sealed_segments: &[Segment],
new_key_ranges: &[(f64, f64)],
) -> StdResult<(), ControllerError>;
///
///Check the status of a Stream Scale operation for a given scale epoch. It returns a
///`true` if the stream scaling operation has completed and `false` if the stream scaling is
///in progress.
///
async fn check_scale(&self, stream: &ScopedStream, scale_epoch: i32) -> Result<bool>;
}
pub struct ControllerClientImpl {
config: ClientConfig,
channel: RwLock<ControllerServiceClient<Channel>>,
}
fn get_channel(config: &ClientConfig) -> Channel {
const HTTP_PREFIX: &str = "http://";
// Placeholder to add authentication headers.
let s = format!("{}{}", HTTP_PREFIX, &config.controller_uri.to_string());
let uri_result = Uri::from_str(s.as_str())
.map_err(|e1: InvalidUri| ControllerError::InvalidConfiguration {
can_retry: false,
error_msg: e1.to_string(),
})
.unwrap();
let iterable_endpoints =
(0..config.max_controller_connections).map(|_a| Channel::builder(uri_result.clone()));
Channel::balance_list(iterable_endpoints)
}
#[allow(unused_variables)]
#[async_trait]
impl ControllerClient for ControllerClientImpl {
async fn create_scope(&self, scope: &Scope) -> Result<bool> {
use create_scope_status::Status;
let request: ScopeInfo = ScopeInfo::from(scope);
let op_status: StdResult<tonic::Response<CreateScopeStatus>, tonic::Status> = self
.get_controller_client()
.create_scope(tonic::Request::new(request))
.await;
let operation_name = "CreateScope";
match op_status {
Ok(code) => match code.into_inner().status() {
Status::Success => Ok(true),
Status::ScopeExists => Ok(false),
Status::InvalidScopeName => Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Invalid scope".into(),
}),
_ => Err(ControllerError::OperationError {
can_retry: true,
operation: operation_name.into(),
error_msg: "Operation failed".into(),
}),
},
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
}
async fn list_streams(&self, scope: &Scope) -> Result<Vec<String>> {
unimplemented!()
}
async fn delete_scope(&self, scope: &Scope) -> Result<bool> {
use delete_scope_status::Status;
let op_status: StdResult<tonic::Response<DeleteScopeStatus>, tonic::Status> = self
.get_controller_client()
.delete_scope(tonic::Request::new(ScopeInfo::from(scope)))
.await;
let operation_name = "DeleteScope";
match op_status {
Ok(code) => match code.into_inner().status() {
Status::Success => Ok(true),
Status::ScopeNotFound => Ok(false),
Status::ScopeNotEmpty => Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Scope not empty".into(),
}),
_ => Err(ControllerError::OperationError {
can_retry: true,
operation: operation_name.into(),
error_msg: "Operation failed".into(),
}),
},
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
}
async fn create_stream(&self, stream_config: &StreamConfiguration) -> Result<bool> {
use create_stream_status::Status;
let request: StreamConfig = StreamConfig::from(stream_config);
let op_status: StdResult<tonic::Response<CreateStreamStatus>, tonic::Status> = self
.get_controller_client()
.create_stream(tonic::Request::new(request))
.await;
let operation_name = "CreateStream";
match op_status {
Ok(code) => match code.into_inner().status() {
Status::Success => Ok(true),
Status::StreamExists => Ok(false),
Status::InvalidStreamName | Status::ScopeNotFound => {
Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Invalid Stream/Scope Not Found".into(),
})
}
_ => Err(ControllerError::OperationError {
can_retry: true, // retry for all other errors
operation: operation_name.into(),
error_msg: "Operation failed".into(),
}),
},
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
}
async fn update_stream(&self, stream_config: &StreamConfiguration) -> Result<bool> {
use update_stream_status::Status;
let request: StreamConfig = StreamConfig::from(stream_config);
let op_status: StdResult<tonic::Response<UpdateStreamStatus>, tonic::Status> = self
.get_controller_client()
.update_stream(tonic::Request::new(request))
.await;
let operation_name = "updateStream";
match op_status {
Ok(code) => match code.into_inner().status() {
Status::Success => Ok(true),
Status::ScopeNotFound | Status::StreamNotFound => {
Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Stream/Scope Not Found".into(),
})
}
_ => Err(ControllerError::OperationError {
can_retry: true, // retry for all other errors
operation: operation_name.into(),
error_msg: "Operation failed".into(),
}),
},
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
}
async fn truncate_stream(&self, stream_cut: &StreamCut) -> Result<bool> {
use update_stream_status::Status;
let request: controller::StreamCut = controller::StreamCut::from(stream_cut);
let op_status: StdResult<tonic::Response<UpdateStreamStatus>, tonic::Status> = self
.get_controller_client()
.truncate_stream(tonic::Request::new(request))
.await;
let operation_name = "truncateStream";
match op_status {
Ok(code) => match code.into_inner().status() {
Status::Success => Ok(true),
Status::ScopeNotFound | Status::StreamNotFound => {
Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Stream/Scope Not Found".into(),
})
}
_ => Err(ControllerError::OperationError {
can_retry: true, // retry for all other errors
operation: operation_name.into(),
error_msg: "Operation failed".into(),
}),
},
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
}
async fn seal_stream(&self, stream: &ScopedStream) -> Result<bool> {
use update_stream_status::Status;
let request: StreamInfo = StreamInfo::from(stream);
let op_status: StdResult<tonic::Response<UpdateStreamStatus>, tonic::Status> = self
.get_controller_client()
.seal_stream(tonic::Request::new(request))
.await;
let operation_name = "SealStream";
match op_status {
Ok(code) => match code.into_inner().status() {
Status::Success => Ok(true),
Status::StreamNotFound | Status::ScopeNotFound => {
Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Stream/Scope Not Found".into(),
})
}
_ => Err(ControllerError::OperationError {
can_retry: true, // retry for all other errors
operation: operation_name.into(),
error_msg: "Operation failed".into(),
}),
},
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
}
async fn delete_stream(&self, stream: &ScopedStream) -> Result<bool> {
use delete_stream_status::Status;
let request: StreamInfo = StreamInfo::from(stream);
let op_status: StdResult<tonic::Response<DeleteStreamStatus>, tonic::Status> = self
.get_controller_client()
.delete_stream(tonic::Request::new(request))
.await;
let operation_name = "DeleteStream";
match op_status {
Ok(code) => match code.into_inner().status() {
Status::Success => Ok(true),
Status::StreamNotFound => Ok(false),
Status::StreamNotSealed => {
Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Stream Not Sealed".into(),
})
}
_ => Err(ControllerError::OperationError {
can_retry: true, // retry for all other errors
operation: operation_name.into(),
error_msg: "Operation failed".into(),
}),
},
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
}
async fn get_current_segments(&self, stream: &ScopedStream) -> Result<StreamSegments> {
let request: StreamInfo = StreamInfo::from(stream);
let op_status: StdResult<tonic::Response<SegmentRanges>, tonic::Status> = self
.get_controller_client()
.get_current_segments(tonic::Request::new(request))
.await;
let operation_name = "getCurrentSegments";
match op_status {
Ok(segment_ranges) => Ok(StreamSegments::from(segment_ranges.into_inner())),
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
}
async fn create_transaction(&self, stream: &ScopedStream, lease: Duration) -> Result<TxnSegments> {
let request = CreateTxnRequest {
stream_info: Some(StreamInfo::from(stream)),
lease: lease.as_millis() as i64,
scale_grace_period: 0,
};
let op_status: StdResult<tonic::Response<CreateTxnResponse>, tonic::Status> = self
.get_controller_client()
.create_transaction(tonic::Request::new(request))
.await;
let operation_name = "createTransaction";
match op_status {
Ok(create_txn_response) => Ok(TxnSegments::from(create_txn_response.into_inner())),
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
}
async fn ping_transaction(
&self,
stream: &ScopedStream,
tx_id: TxId,
lease: Duration,
) -> Result<PingStatus> {
use ping_txn_status::Status;
let request = PingTxnRequest {
stream_info: Some(StreamInfo::from(stream)),
txn_id: Some(TxnId::from(tx_id)),
lease: lease.as_millis() as i64,
};
let op_status: StdResult<tonic::Response<PingTxnStatus>, tonic::Status> = self
.get_controller_client()
.ping_transaction(tonic::Request::new(request))
.await;
let operation_name = "pingTransaction";
match op_status {
Ok(code) => match code.into_inner().status() {
Status::Ok => Ok(PingStatus::Ok),
Status::Committed => Ok(PingStatus::Committed),
Status::Aborted => Ok(PingStatus::Aborted),
Status::LeaseTooLarge => Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Ping transaction failed, Reason:LeaseTooLarge".into(),
}),
Status::MaxExecutionTimeExceeded => Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Ping transaction failed, Reason:MaxExecutionTimeExceeded".into(),
}),
Status::ScaleGraceTimeExceeded => Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Ping transaction failed, Reason:ScaleGraceTimeExceeded".into(),
}),
Status::Disconnected => Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Ping transaction failed, Reason:ScaleGraceTimeExceeded".into(),
}),
_ => Err(ControllerError::OperationError {
can_retry: true, // retry for all other errors
operation: operation_name.into(),
error_msg: "Operation failed".into(),
}),
},
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
}
async fn commit_transaction(
&self,
stream: &ScopedStream,
tx_id: TxId,
writer_id: WriterId,
time: Timestamp,
) -> Result<()> {
use txn_status::Status;
let request = TxnRequest {
stream_info: Some(StreamInfo::from(stream)),
txn_id: Some(TxnId::from(tx_id)),
writer_id: writer_id.0.to_string(),
timestamp: time.0 as i64,
};
let op_status: StdResult<tonic::Response<TxnStatus>, tonic::Status> = self
.get_controller_client()
.commit_transaction(tonic::Request::new(request))
.await;
let operation_name = "commitTransaction";
match op_status {
Ok(code) => match code.into_inner().status() {
Status::Success => Ok(()),
Status::TransactionNotFound => Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Commit transaction failed, Reason:TransactionNotFound".into(),
}),
Status::StreamNotFound => Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Commit transaction failed, Reason:StreamNotFound".into(),
}),
_ => Err(ControllerError::OperationError {
can_retry: true, // retry for all other errors
operation: operation_name.into(),
error_msg: "Operation failed".into(),
}),
},
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
}
async fn abort_transaction(&self, stream: &ScopedStream, tx_id: TxId) -> Result<()> {
use txn_status::Status;
let request = TxnRequest {
stream_info: Some(StreamInfo::from(stream)),
txn_id: Some(TxnId::from(tx_id)),
writer_id: "".to_string(),
timestamp: 0,
};
let op_status: StdResult<tonic::Response<TxnStatus>, tonic::Status> = self
.get_controller_client()
.commit_transaction(tonic::Request::new(request))
.await;
let operation_name = "abortTransaction";
match op_status {
Ok(code) => match code.into_inner().status() {
Status::Success => Ok(()),
Status::TransactionNotFound => Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Abort transaction failed, Reason:TransactionNotFound".into(),
}),
Status::StreamNotFound => Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Abort transaction failed, Reason:StreamNotFound".into(),
}),
_ => Err(ControllerError::OperationError {
can_retry: true, // retry for all other errors
operation: operation_name.into(),
error_msg: "Operation failed".into(),
}),
},
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
}
async fn check_transaction_status(
&self,
stream: &ScopedStream,
tx_id: TxId,
) -> Result<TransactionStatus> {
use txn_state::State;
let request = TxnRequest {
stream_info: Some(StreamInfo::from(stream)),
txn_id: Some(TxnId::from(tx_id)),
writer_id: "".to_string(),
timestamp: 0,
};
let op_status: StdResult<tonic::Response<TxnState>, tonic::Status> = self
.get_controller_client()
.check_transaction_state(tonic::Request::new(request))
.await;
let operation_name = "checkTransactionStatus";
match op_status {
Ok(code) => match code.into_inner().state() {
State::Open => Ok(TransactionStatus::Open),
State::Committing => Ok(TransactionStatus::Committing),
State::Committed => Ok(TransactionStatus::Committed),
State::Aborting => Ok(TransactionStatus::Aborting),
State::Aborted => Ok(TransactionStatus::Aborted),
_ => Err(ControllerError::OperationError {
can_retry: true, // retry for all other errors
operation: operation_name.into(),
error_msg: "Operation failed".into(),
}),
},
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
}
async fn get_endpoint_for_segment(&self, segment: &ScopedSegment) -> Result<PravegaNodeUri> {
let op_status: StdResult<tonic::Response<NodeUri>, tonic::Status> = self
.get_controller_client()
.get_uri(tonic::Request::new(segment.into()))
.await;
let operation_name = "get_endpoint";
match op_status {
Ok(response) => Ok(response.into_inner()),
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
.map(PravegaNodeUri::from)
}
async fn get_or_refresh_delegation_token_for(&self, stream: ScopedStream) -> Result<DelegationToken> {
unimplemented!()
}
async fn get_successors(&self, segment: &ScopedSegment) -> Result<StreamSegmentsWithPredecessors> {
let scoped_stream = ScopedStream {
scope: segment.scope.clone(),
stream: segment.stream.clone(),
};
let segment_id_request = SegmentId {
stream_info: Some(StreamInfo::from(&scoped_stream)),
segment_id: segment.segment.number,
};
debug!("sending get successors request for {:?}", segment);
let op_status: StdResult<tonic::Response<SuccessorResponse>, tonic::Status> = self
.get_controller_client()
.get_segments_immediately_following(tonic::Request::new(segment_id_request))
.await;
let operation_name = "get_successors_segment";
match op_status {
Ok(response) => Ok(response.into_inner()),
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
.map(StreamSegmentsWithPredecessors::from)
}
async fn scale_stream(
&self,
stream: &ScopedStream,
sealed_segment_ids: &[Segment],
new_ranges: &[(f64, f64)],
) -> StdResult<(), ControllerError> {
use scale_response::ScaleStreamStatus;
let scale_request = ScaleRequest {
stream_info: Some(StreamInfo::from(stream)),
sealed_segments: sealed_segment_ids.iter().map(|s| s.number).collect(),
new_key_ranges: new_ranges
.iter()
.map(|(l, r)| scale_request::KeyRangeEntry { start: *l, end: *r })
.collect(),
scale_timestamp: Instant::now().elapsed().as_millis() as i64,
};
// start the scale Stream operation.
let op_status: StdResult<tonic::Response<ScaleResponse>, tonic::Status> = self
.get_controller_client()
.scale(tonic::Request::new(scale_request))
.await;
let operation_name = "scale_stream";
match op_status {
Ok(response) => {
let scale_response = response.into_inner();
match scale_response.status() {
ScaleStreamStatus::Started => {
// scale Stream has started. check for its completion.
self.check_scale_status(stream, scale_response.epoch, RetryWithBackoff::default())
.await
}
ScaleStreamStatus::PreconditionFailed => Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "PreconditionFailed".into(),
}),
_ => Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Operation failed".into(),
}),
}
}
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
}
async fn check_scale(&self, stream: &ScopedStream, scale_epoch: i32) -> Result<bool> {
use scale_status_response::ScaleStatus;
let request = ScaleStatusRequest {
stream_info: Some(StreamInfo::from(stream)),
epoch: scale_epoch,
};
let op_status: StdResult<tonic::Response<ScaleStatusResponse>, tonic::Status> = self
.get_controller_client()
.check_scale(tonic::Request::new(request))
.await;
let operation_name = "check_scale";
debug!("Check Stream scale status {:?}", op_status);
match op_status {
Ok(response) => match response.into_inner().status() {
ScaleStatus::Success => Ok(true),
ScaleStatus::InProgress => Ok(false),
ScaleStatus::InvalidInput => Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Invalid Input".into(),
}),
_ => Err(ControllerError::OperationError {
can_retry: false, // do not retry.
operation: operation_name.into(),
error_msg: "Operation failed".into(),
}),
},
Err(status) => Err(self.map_grpc_error(operation_name, status)),
}
}
}
impl ControllerClientImpl {
///
/// Create a pooled connection to the controller. The pool size is decided by the ClientConfig.
/// The requests will be load balanced across multiple connections and every connection supports
/// multiplexing of requests.
///
pub fn new(config: ClientConfig) -> Self {
// actual connection is established lazily.
let ch = get_channel(&config);
ControllerClientImpl {
config,
channel: RwLock::new(ControllerServiceClient::new(ch)),
}
}
///
/// reset method needs to be invoked in the case of ConnectionError.
/// This logic can be removed once https://github.com/tower-rs/tower/issues/383 is fixed.
///
pub fn reset(&self) {
let ch = get_channel(&self.config);
let mut x = self.channel.write().unwrap();
*x = ControllerServiceClient::new(ch);
}
///
/// Tonic library suggests we clone the channel to enable multiplexing of requests.
/// This is because at the very top level the channel is backed by a `tower_buffer::Buffer`
/// which runs the connection in a background task and provides a `mpsc` channel interface.
/// Due to this cloning the `Channel` type is cheap and encouraged.
///
fn get_controller_client(&self) -> ControllerServiceClient<Channel> {
self.channel.read().unwrap().clone()
}
// Method used to translate grpc errors to ControllerError.
fn map_grpc_error(&self, operation_name: &str, status: Status) -> ControllerError {
match status.code() {
Code::InvalidArgument
| Code::NotFound
| Code::AlreadyExists
| Code::PermissionDenied
| Code::OutOfRange
| Code::Unimplemented
| Code::Unauthenticated => ControllerError::OperationError {
can_retry: false,
operation: operation_name.into(),
error_msg: status.to_string(),
},
Code::Unknown => {
self.reset();
ControllerError::ConnectionError {
can_retry: true,
error_msg: status.to_string(),
}
}
_ => ControllerError::OperationError {
can_retry: true, // retry is enabled for all other errors
operation: operation_name.into(),
error_msg: format!("{:?}", status.code()),
},
}
}
// Helper method which checks for the completion of the Stream scale for the given epoch with the specified retry policy.
async fn check_scale_status(
&self,
stream: &ScopedStream,
epoch: i32,
retry_policy: RetryWithBackoff,
) -> Result<()> {
let operation_name = "check_scale post scale_stream";
retry_async(retry_policy, || async {
let r = self.check_scale(stream, epoch).await;
match r {
Ok(status) => {
if status {
RetryResult::Success(())
} else {
RetryResult::Retry(ControllerError::OperationError {
can_retry: true,
operation: operation_name.into(),
error_msg: "Stream Scaling in progress".to_string(),
})
}
}
Err(error) => RetryResult::Fail(error),
}
})
.await
.map_err(|retry_error| ControllerError::OperationError {
can_retry: false,
operation: operation_name.to_string(),
error_msg: format!("{:?}", retry_error),
})
}
}