-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathhttp_objects.rs
965 lines (873 loc) · 35 KB
/
http_objects.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
use std::{collections::HashMap, fmt, sync::Arc};
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use data_model::{ComputeGraphCode, GraphInvocationCtx, GraphInvocationOutcome};
use indexify_utils::get_epoch_time_in_ms;
use serde::{Deserialize, Serialize};
use state_store::IndexifyState;
use tracing::error;
use utoipa::{IntoParams, ToSchema};
use crate::executor_api::blob_store_path_to_url;
#[derive(Debug, ToSchema, Serialize, Deserialize)]
pub struct IndexifyAPIError {
#[serde(skip)]
status_code: StatusCode,
message: String,
}
impl IndexifyAPIError {
pub fn new(status_code: StatusCode, message: &str) -> Self {
Self {
status_code,
message: message.to_string(),
}
}
pub fn _bad_request(e: &str) -> Self {
Self::new(StatusCode::BAD_REQUEST, e)
}
pub fn internal_error(e: anyhow::Error) -> Self {
Self::new(StatusCode::INTERNAL_SERVER_ERROR, e.to_string().as_str())
}
pub fn internal_error_str(e: &str) -> Self {
Self::new(StatusCode::INTERNAL_SERVER_ERROR, e)
}
pub fn not_found(message: &str) -> Self {
Self::new(StatusCode::NOT_FOUND, message)
}
pub fn bad_request(message: &str) -> Self {
Self::new(StatusCode::BAD_REQUEST, message)
}
}
impl IntoResponse for IndexifyAPIError {
fn into_response(self) -> Response {
error!("API Error: {} - {}", self.status_code, self.message);
(self.status_code, self.message).into_response()
}
}
impl From<serde_json::Error> for IndexifyAPIError {
fn from(e: serde_json::Error) -> Self {
Self::bad_request(&e.to_string())
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub enum CursorDirection {
#[serde(rename = "forward")]
Forward,
#[serde(rename = "backward")]
Backward,
}
#[derive(Debug, Serialize, Deserialize, ToSchema, IntoParams)]
pub struct ListParams {
pub limit: Option<usize>,
pub cursor: Option<String>,
pub direction: Option<CursorDirection>,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct Namespace {
name: String,
created_at: u64,
}
impl From<data_model::Namespace> for Namespace {
fn from(namespace: data_model::Namespace) -> Self {
Self {
name: namespace.name,
created_at: namespace.created_at,
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct NamespaceList {
pub namespaces: Vec<Namespace>,
}
#[derive(Clone, Serialize, Deserialize, ToSchema)]
pub struct ImageInformation {
pub image_name: String,
#[serde(default)]
pub image_hash: String,
pub tag: String, // Deprecated
pub base_image: String, // Deprecated
pub run_strs: Vec<String>, // Deprecated
pub image_uri: Option<String>,
pub sdk_version: Option<String>,
}
impl fmt::Debug for ImageInformation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ImageInformation")
.field("image_name", &self.image_name)
.field("tag", &self.tag)
.field("base_image", &self.base_image)
.field("run_strs", &self.run_strs)
.finish()
}
}
impl From<ImageInformation> for data_model::ImageInformation {
fn from(value: ImageInformation) -> Self {
data_model::ImageInformation::new(
value.image_name,
value.image_hash,
value.image_uri,
value.tag,
value.base_image,
value.run_strs,
value.sdk_version,
)
}
}
impl From<data_model::ImageInformation> for ImageInformation {
fn from(value: data_model::ImageInformation) -> ImageInformation {
ImageInformation {
image_name: value.image_name,
image_hash: value.image_hash,
tag: value.tag,
base_image: value.base_image,
run_strs: value.run_strs,
image_uri: value.image_uri,
sdk_version: value.sdk_version,
}
}
}
fn default_encoder() -> String {
"cloudpickle".to_string()
}
#[derive(Debug, Serialize, Deserialize, ToSchema, Clone)]
pub struct ComputeFn {
pub name: String,
pub fn_name: String,
pub description: String,
pub reducer: bool,
#[serde(default = "default_encoder")]
pub input_encoder: String,
#[serde(default = "default_encoder")]
pub output_encoder: String,
pub image_information: ImageInformation,
#[serde(default)]
pub secret_names: Vec<String>,
}
impl From<ComputeFn> for data_model::ComputeFn {
fn from(val: ComputeFn) -> Self {
data_model::ComputeFn {
name: val.name.clone(),
fn_name: val.fn_name.clone(),
description: val.description.clone(),
placement_constraints: Default::default(),
reducer: val.reducer,
input_encoder: val.input_encoder.clone(),
output_encoder: val.output_encoder.clone(),
image_information: val.image_information.into(),
secret_names: Some(val.secret_names),
}
}
}
impl From<data_model::ComputeFn> for ComputeFn {
fn from(c: data_model::ComputeFn) -> Self {
Self {
name: c.name,
fn_name: c.fn_name,
description: c.description,
reducer: c.reducer,
input_encoder: c.input_encoder,
output_encoder: c.output_encoder,
image_information: c.image_information.into(),
secret_names: c.secret_names.unwrap_or(vec![]),
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema, Clone)]
pub struct DynamicRouter {
pub name: String,
pub source_fn: String,
pub description: String,
pub target_fns: Vec<String>,
#[serde(default = "default_encoder")]
pub input_encoder: String,
#[serde(default = "default_encoder")]
pub output_encoder: String,
pub image_information: ImageInformation,
#[serde(default)]
pub secret_names: Vec<String>,
}
impl From<DynamicRouter> for data_model::DynamicEdgeRouter {
fn from(val: DynamicRouter) -> Self {
data_model::DynamicEdgeRouter {
name: val.name.clone(),
source_fn: val.source_fn.clone(),
description: val.description.clone(),
target_functions: val.target_fns.clone(),
input_encoder: val.input_encoder.clone(),
output_encoder: val.output_encoder.clone(),
image_information: val.image_information.clone().into(),
secret_names: Some(val.secret_names),
}
}
}
impl From<data_model::DynamicEdgeRouter> for DynamicRouter {
fn from(d: data_model::DynamicEdgeRouter) -> Self {
Self {
name: d.name,
source_fn: d.source_fn,
description: d.description,
target_fns: d.target_functions,
input_encoder: d.input_encoder,
output_encoder: d.output_encoder,
image_information: d.image_information.into(),
secret_names: d.secret_names.unwrap_or(vec![]),
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema, Clone)]
pub enum Node {
#[serde(rename = "dynamic_router")]
DynamicRouter(DynamicRouter),
#[serde(rename = "compute_fn")]
ComputeFn(ComputeFn),
}
impl Node {
pub fn name(&self) -> String {
match self {
Node::DynamicRouter(d) => d.name.clone(),
Node::ComputeFn(c) => c.name.clone(),
}
}
}
impl From<Node> for data_model::Node {
fn from(val: Node) -> Self {
match val {
Node::DynamicRouter(d) => data_model::Node::Router(d.into()),
Node::ComputeFn(c) => data_model::Node::Compute(c.into()),
}
}
}
impl From<data_model::Node> for Node {
fn from(node: data_model::Node) -> Self {
match node {
data_model::Node::Router(d) => Node::DynamicRouter(d.into()),
data_model::Node::Compute(c) => Node::ComputeFn(c.into()),
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct RuntimeInformation {
pub major_version: u8,
pub minor_version: u8,
#[serde(default)]
pub sdk_version: String,
}
impl From<RuntimeInformation> for data_model::RuntimeInformation {
fn from(value: RuntimeInformation) -> Self {
data_model::RuntimeInformation {
major_version: value.major_version,
minor_version: value.minor_version,
sdk_version: value.sdk_version,
}
}
}
impl From<data_model::RuntimeInformation> for RuntimeInformation {
fn from(value: data_model::RuntimeInformation) -> Self {
Self {
major_version: value.major_version,
minor_version: value.minor_version,
sdk_version: value.sdk_version,
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct ComputeGraph {
pub name: String,
pub namespace: String,
pub description: String,
#[serde(default)]
pub tombstoned: bool,
pub start_node: Node,
pub version: GraphVersion,
#[serde(default)]
pub tags: Option<HashMap<String, String>>,
pub nodes: HashMap<String, Node>,
pub edges: HashMap<String, Vec<String>>,
#[serde(default = "get_epoch_time_in_ms")]
pub created_at: u64,
pub runtime_information: RuntimeInformation,
#[serde(skip_deserializing)]
pub replaying: bool,
}
impl ComputeGraph {
pub fn into_data_model(
self,
code_path: &str,
sha256_hash: &str,
size: u64,
) -> Result<data_model::ComputeGraph, IndexifyAPIError> {
let mut nodes = HashMap::new();
for (name, node) in self.nodes {
nodes.insert(name, node.into());
}
let start_fn: data_model::Node = self.start_node.into();
let compute_graph = data_model::ComputeGraph {
name: self.name,
namespace: self.namespace,
description: self.description,
start_fn,
tags: self.tags.unwrap_or_default(),
version: self.version.into(),
code: ComputeGraphCode {
sha256_hash: sha256_hash.to_string(),
size,
path: code_path.to_string(),
},
nodes,
edges: self.edges.clone(),
created_at: 0,
runtime_information: self.runtime_information.into(),
replaying: false,
tombstoned: self.tombstoned,
};
Ok(compute_graph)
}
}
impl From<data_model::ComputeGraph> for ComputeGraph {
fn from(compute_graph: data_model::ComputeGraph) -> Self {
let start_fn = match compute_graph.start_fn {
data_model::Node::Router(d) => Node::DynamicRouter(d.into()),
data_model::Node::Compute(c) => Node::ComputeFn(c.into()),
};
let mut nodes = HashMap::new();
for (k, v) in compute_graph.nodes.into_iter() {
nodes.insert(k, v.into());
}
Self {
name: compute_graph.name,
namespace: compute_graph.namespace,
description: compute_graph.description,
start_node: start_fn,
tags: Some(compute_graph.tags),
version: compute_graph.version.into(),
nodes,
edges: compute_graph.edges,
created_at: compute_graph.created_at,
runtime_information: compute_graph.runtime_information.into(),
replaying: compute_graph.replaying,
tombstoned: compute_graph.tombstoned,
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct CreateNamespace {
pub name: String,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct ComputeGraphsList {
pub compute_graphs: Vec<ComputeGraph>,
pub cursor: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct QueryParams {
pub input_id: Option<String>,
pub on_graph_end: bool,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GraphOutputNotification {
pub output_id: String,
pub compute_graph: String,
pub fn_name: String,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct CreateNamespaceResponse {
pub name: Namespace,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct GraphInvocations {
pub invocations: Vec<Invocation>,
pub prev_cursor: Option<String>,
pub next_cursor: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct GraphInputJson {
pub payload: serde_json::Value,
}
#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
pub struct GraphInputFile {
// file:///s3://bucket/key
// file:///data/path/to/file
pub url: String,
pub metadata: serde_json::Value,
pub sha_256: String,
pub size: u64,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub enum TaskOutcome {
Undefined,
Success,
Failure,
}
impl From<data_model::TaskOutcome> for TaskOutcome {
fn from(outcome: data_model::TaskOutcome) -> Self {
match outcome {
data_model::TaskOutcome::Unknown => TaskOutcome::Undefined,
data_model::TaskOutcome::Success => TaskOutcome::Success,
data_model::TaskOutcome::Failure => TaskOutcome::Failure,
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema, Clone)]
pub struct GraphVersion(pub String);
impl From<data_model::GraphVersion> for GraphVersion {
fn from(version: data_model::GraphVersion) -> Self {
Self(version.0)
}
}
impl From<GraphVersion> for data_model::GraphVersion {
fn from(version: GraphVersion) -> Self {
Self(version.0)
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema, Clone)]
pub enum TaskStatus {
Pending,
Running,
Completed,
}
impl From<data_model::TaskStatus> for TaskStatus {
fn from(status: data_model::TaskStatus) -> Self {
match status {
data_model::TaskStatus::Pending => TaskStatus::Pending,
data_model::TaskStatus::Running => TaskStatus::Running,
data_model::TaskStatus::Completed => TaskStatus::Completed,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ToSchema)]
pub struct DataPayload {
pub path: String,
pub size: u64,
pub sha256_hash: String,
pub content_type: String,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct Task {
pub id: String,
pub namespace: String,
pub compute_fn: String,
pub compute_graph: String,
pub invocation_id: String,
pub input_key: String,
pub status: TaskStatus,
pub outcome: TaskOutcome,
pub reducer_output_id: Option<String>,
pub graph_version: GraphVersion,
pub image_uri: Option<String>,
pub secret_names: Vec<String>,
pub graph_payload: Option<DataPayload>,
pub input_payload: Option<DataPayload>,
pub reducer_input_payload: Option<DataPayload>,
pub output_payload_uri_prefix: Option<String>,
}
impl From<data_model::Task> for Task {
fn from(task: data_model::Task) -> Self {
Self {
id: task.id.to_string(),
namespace: task.namespace,
compute_fn: task.compute_fn_name,
compute_graph: task.compute_graph_name,
invocation_id: task.invocation_id,
input_key: task.input_node_output_key,
outcome: task.outcome.into(),
status: task.status.into(),
reducer_output_id: task.reducer_output_id,
graph_version: task.graph_version.into(),
image_uri: task.image_uri,
secret_names: task.secret_names.unwrap_or_default(),
graph_payload: None,
input_payload: None,
reducer_input_payload: None,
output_payload_uri_prefix: None,
}
}
}
pub fn into_task_with_data_payloads(
task: data_model::Task,
indexify_state: Arc<IndexifyState>,
blob_store_url_scheme: String,
blob_store_url: String,
) -> Task {
let mut api_task = Task::from(task.clone());
let compute_graph_version = indexify_state.reader().get_compute_graph_version(
&task.namespace,
&task.compute_graph_name,
&task.graph_version,
);
match compute_graph_version {
Ok(Some(compute_graph_version)) => {
api_task.graph_payload = Some(DataPayload {
path: blob_store_path_to_url(
&compute_graph_version.code.path,
&blob_store_url_scheme,
&blob_store_url,
),
size: compute_graph_version.code.size,
sha256_hash: compute_graph_version.code.sha256_hash,
content_type: "application/octet-stream".to_string(),
});
}
Ok(None) => {
error!("Compute graph version not found task_id: {}, namespace: {}, graph_name: {}, graph_version: {}", task.id, task.namespace, task.compute_graph_name, task.graph_version);
}
Err(e) => {
error!("Failed to get compute graph version task_id: {}, namespace: {}, graph_name: {}, graph_version: {} : {}", task.id, task.namespace, task.compute_graph_name, task.graph_version, e);
}
}
let first_function_in_graph =
api_task.invocation_id == api_task.input_key.split("|").last().unwrap_or("");
if first_function_in_graph {
let invocation_payload = indexify_state.reader().invocation_payload(
&task.namespace,
&task.compute_graph_name,
&task.invocation_id,
);
match invocation_payload {
Ok(invocation_payload) => {
api_task.input_payload = Some(DataPayload {
path: blob_store_path_to_url(
&invocation_payload.payload.path,
&blob_store_url_scheme,
&blob_store_url,
),
size: invocation_payload.payload.size,
sha256_hash: invocation_payload.payload.sha256_hash,
content_type: invocation_payload.encoding,
});
}
Err(e) => {
error!("Failed to get invocation payload task_id: {}, namespace: {}, graph_name: {}, invocation_id: {} : {}", task.id, task.namespace, task.compute_graph_name, task.invocation_id, e);
}
}
} else {
let node_output = indexify_state
.reader()
.fn_output_payload_by_key(&api_task.input_key);
match node_output {
Ok(node_output) => {
match node_output.payload {
data_model::OutputPayload::Fn(payload) => {
api_task.input_payload = Some(DataPayload {
path: blob_store_path_to_url(
&payload.path,
&blob_store_url_scheme,
&blob_store_url,
),
size: payload.size,
sha256_hash: payload.sha256_hash,
content_type: node_output.encoding,
});
}
_ => {
error!("Unexpected node output payload task_id: {}, namespace: {}, graph_name: {}, invocation_id: {} input_key: {} : {:?}", task.id, task.namespace, task.compute_graph_name, task.invocation_id, api_task.input_key, node_output.payload);
}
};
}
Err(e) => {
error!("Failed to get node output payload task_id: {}, namespace: {}, graph_name: {}, invocation_id: {} input_key: {} : {}", task.id, task.namespace, task.compute_graph_name, task.invocation_id, api_task.input_key, e);
}
}
}
match api_task.reducer_output_id.clone() {
Some(reducer_output_id) => {
let reducer_output = indexify_state.reader().fn_output_payload(
&task.namespace,
&task.compute_graph_name,
&task.invocation_id,
&task.compute_fn_name,
&reducer_output_id,
);
match reducer_output {
Ok(Some(reducer_output)) => {
match reducer_output.payload {
data_model::OutputPayload::Fn(payload) => {
api_task.reducer_input_payload = Some(DataPayload {
path: blob_store_path_to_url(
&payload.path,
&blob_store_url_scheme,
&blob_store_url,
),
size: payload.size,
sha256_hash: payload.sha256_hash,
content_type: reducer_output.encoding,
});
}
_ => {
error!("Unexpected reducer output payload task_id: {}, namespace: {}, graph_name: {}, invocation_id: {} reducer_output_id: {} : {:?}", task.id, task.namespace, task.compute_graph_name, task.invocation_id, reducer_output_id, reducer_output.payload);
}
};
}
Ok(None) => {
error!("Failed to get reducer output payload task_id: {}, namespace: {}, graph_name: {}, invocation_id: {} reducer_output_id: {} : not found", task.id, task.namespace, task.compute_graph_name, task.invocation_id, reducer_output_id);
}
Err(e) => {
error!("Failed to get reducer output payload task_id: {}, namespace: {}, graph_name: {}, invocation_id: {} reducer_output_id: {} : {}", task.id, task.namespace, task.compute_graph_name, task.invocation_id, reducer_output_id, e);
}
}
}
_ => {} // The task is not a reducer.
}
// Executor adds task id into the payload path if the output is for non-reducer
// function.
api_task.output_payload_uri_prefix = Some(format!(
"{}/{}.{}.{}.{}",
blob_store_url,
task.namespace,
task.compute_graph_name,
task.compute_fn_name,
task.invocation_id,
));
api_task
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct Tasks {
pub tasks: Vec<Task>,
pub cursor: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct FnOutput {
pub compute_fn: String,
pub id: String,
pub created_at: u64,
}
impl From<data_model::NodeOutput> for FnOutput {
fn from(output: data_model::NodeOutput) -> Self {
Self {
compute_fn: output.compute_fn_name,
id: output.id.to_string(),
created_at: output.created_at,
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub enum InvocationStatus {
Pending,
Running,
Finalized,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub enum InvocationOutcome {
Undefined,
Success,
Failure,
}
impl From<GraphInvocationOutcome> for InvocationOutcome {
fn from(outcome: GraphInvocationOutcome) -> Self {
match outcome {
GraphInvocationOutcome::Undefined => InvocationOutcome::Undefined,
GraphInvocationOutcome::Success => InvocationOutcome::Success,
GraphInvocationOutcome::Failure => InvocationOutcome::Failure,
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct FnOutputs {
pub status: InvocationStatus,
pub outcome: InvocationOutcome,
pub outputs: Vec<FnOutput>,
pub cursor: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct InvocationId {
pub id: String,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct Invocation {
pub id: String,
pub completed: bool,
pub status: InvocationStatus,
pub outcome: InvocationOutcome,
pub outstanding_tasks: u64,
pub task_analytics: HashMap<String, TaskAnalytics>,
pub graph_version: String,
pub created_at: u64,
}
impl From<GraphInvocationCtx> for Invocation {
fn from(value: GraphInvocationCtx) -> Self {
let mut task_analytics = HashMap::new();
for (k, v) in value.fn_task_analytics {
task_analytics.insert(
k,
TaskAnalytics {
pending_tasks: v.pending_tasks,
successful_tasks: v.successful_tasks,
failed_tasks: v.failed_tasks,
},
);
}
let status = if value.completed {
InvocationStatus::Finalized
} else if value.outstanding_tasks > 0 {
InvocationStatus::Running
} else {
InvocationStatus::Pending
};
Self {
id: value.invocation_id.to_string(),
completed: value.completed,
outcome: value.outcome.into(),
status,
outstanding_tasks: value.outstanding_tasks,
task_analytics,
graph_version: value.graph_version.0,
created_at: value.created_at,
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct TaskAnalytics {
pub pending_tasks: u64,
pub successful_tasks: u64,
pub failed_tasks: u64,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct FunctionURI {
pub namespace: String,
pub compute_graph: String,
pub compute_fn: String,
// Temporary fix to enable internal migration
// to new executor version, we will bring this back
// when the scheduler can turn off containers of older
// versions after all the invocations into them have been
// completed, and turn on new versions of the executor.
pub version: Option<GraphVersion>,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct ExecutorMetadata {
pub id: String,
pub executor_version: String,
pub addr: String,
pub function_allowlist: Option<Vec<FunctionURI>>,
pub labels: HashMap<String, serde_json::Value>,
}
impl From<data_model::ExecutorMetadata> for ExecutorMetadata {
fn from(executor: data_model::ExecutorMetadata) -> Self {
let function_allowlist = executor.function_allowlist.map(|allowlist| {
allowlist
.iter()
.map(|fn_uri| FunctionURI {
namespace: fn_uri.namespace.clone(),
compute_graph: fn_uri.compute_graph_name.clone(),
compute_fn: fn_uri.compute_fn_name.clone(),
version: fn_uri.version.clone().map(|v| v.into()),
})
.collect()
});
Self {
id: executor.id.to_string(),
executor_version: executor.executor_version,
addr: executor.addr,
function_allowlist,
labels: executor.labels,
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct Allocation {
pub namespace: String,
pub compute_graph: String,
pub compute_fn: String,
pub executor_id: String,
pub task_id: String,
pub invocation_id: String,
pub created_at: u128,
}
impl From<data_model::Allocation> for Allocation {
fn from(allocation: data_model::Allocation) -> Self {
Self {
namespace: allocation.namespace,
compute_graph: allocation.compute_graph,
compute_fn: allocation.compute_fn,
executor_id: allocation.executor_id.to_string(),
task_id: allocation.task_id.to_string(),
invocation_id: allocation.invocation_id.to_string(),
created_at: allocation.created_at,
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct StateChange {
pub id: String,
pub object_id: String,
pub change_type: String,
pub created_at: u64,
pub namespace: Option<String>,
pub compute_graph: Option<String>,
pub invocation: Option<String>,
}
impl From<data_model::StateChange> for StateChange {
fn from(item: data_model::StateChange) -> Self {
StateChange {
id: item.id.to_string(),
object_id: item.object_id.to_string(),
change_type: item.change_type.to_string(),
created_at: item.created_at,
namespace: item.namespace,
compute_graph: item.compute_graph,
invocation: item.invocation,
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct StateChangesResponse {
pub count: usize,
pub state_changes: Vec<StateChange>,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UnallocatedTasks {
pub count: usize,
pub tasks: Vec<Task>,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct FnExecutor {
pub count: usize,
pub fn_name: String,
pub allocations: Vec<Allocation>,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct ExecutorAllocations {
pub total: usize,
pub function_executors: Vec<FnExecutor>,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct ExecutorsAllocationsResponse {
pub allocations: HashMap<String, ExecutorAllocations>,
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct InvocationQueryParams {
pub block_until_finish: Option<bool>,
}
#[cfg(test)]
mod tests {
use crate::http_objects::{ComputeFn, DynamicRouter};
#[test]
fn test_compute_graph_deserialization() {
// Don't delete this. It makes it easier
// to test the deserialization of the ComputeGraph struct
// from the python side
let json = r#"{"name":"test","description":"test","start_node":{"compute_fn":{"name":"extractor_a","fn_name":"extractor_a","description":"Random description of extractor_a", "reducer": false, "image_information": {"image_name": "name1", "tag": "tag1", "base_image": "base1", "run_strs": ["tuff", "life", "running", "docker"], "sdk_version":"1.2.3"}, "input_encoder":"cloudpickle", "output_encoder":"cloudpickle", "image_name": "default_image"}},"nodes":{"extractor_a":{"compute_fn":{"name":"extractor_a","fn_name":"extractor_a","description":"Random description of extractor_a", "reducer": false, "image_information": {"image_name": "name1", "tag": "tag1", "base_image": "base1", "run_strs": ["tuff", "life", "running", "docker"], "sdk_version":"1.2.3"}, "input_encoder":"cloudpickle", "output_encoder":"cloudpickle","image_name": "default_image"}},"extractor_b":{"compute_fn":{"name":"extractor_b","fn_name":"extractor_b","description":"", "reducer": false, "image_information": {"image_name": "name1", "tag": "tag1", "base_image": "base1", "run_strs": ["tuff", "life", "running", "docker"], "sdk_version":"1.2.3"}, "input_encoder":"cloudpickle", "output_encoder":"cloudpickle", "image_name": "default_image"}},"extractor_c":{"compute_fn":{"name":"extractor_c","fn_name":"extractor_c","description":"", "reducer": false, "image_information": {"image_name": "name1", "tag": "tag1", "base_image": "base1", "run_strs": ["tuff", "life", "running", "docker"], "sdk_version":"1.2.3"}, "input_encoder":"cloudpickle", "output_encoder":"cloudpickle", "image_name": "default_image"}}},"edges":{"extractor_a":["extractor_b"],"extractor_b":["extractor_c"]},"runtime_information": {"major_version": 3, "minor_version": 10, "sdk_version": "1.2.3"}, "version": "1.2.3"}"#;
let mut json_value: serde_json::Value = serde_json::from_str(json).unwrap();
json_value["namespace"] = serde_json::Value::String("test".to_string());
let _: super::ComputeGraph = serde_json::from_value(json_value).unwrap();
}
#[test]
fn test_compute_graph_with_router_deserialization() {
let json = r#"{"name":"graph_a_router","description":"description of graph_a","start_node":{"compute_fn":{"name":"extractor_a","fn_name":"extractor_a","description":"Random description of extractor_a", "reducer": false, "image_information": {"image_name": "name1", "tag": "tag1", "base_image": "base1", "run_strs": ["tuff", "life", "running", "docker"], "sdk_version":"1.2.3"}, "input_encoder":"cloudpickle", "output_encoder":"cloudpickle", "image_name": "default_image"}},"nodes":{"extractor_a":{"compute_fn":{"name":"extractor_a","fn_name":"extractor_a","description":"Random description of extractor_a", "reducer": false, "image_information": {"image_name": "name1", "tag": "tag1", "base_image": "base1", "run_strs": ["tuff", "life", "running", "docker"], "sdk_version":"1.2.3"}, "input_encoder":"cloudpickle", "output_encoder":"cloudpickle", "image_name": "default_image"}},"router_x":{"dynamic_router":{"name":"router_x","description":"","source_fn":"router_x","target_fns":["extractor_y","extractor_z"], "reducer": false, "image_information": {"image_name": "name1", "tag": "tag1", "base_image": "base1", "run_strs": ["tuff", "life", "running", "docker"], "sdk_version":"1.2.3"}, "input_encoder":"cloudpickle", "output_encoder":"cloudpickle", "image_name": "default_image"}},"extractor_y":{"compute_fn":{"name":"extractor_y","fn_name":"extractor_y","description":"", "reducer": false, "image_information": {"image_name": "name1", "tag": "tag1", "base_image": "base1", "run_strs": ["tuff", "life", "running", "docker"], "sdk_version":"1.2.3"}, "input_encoder":"cloudpickle", "output_encoder":"cloudpickle", "image_name": "default_image"}},"extractor_z":{"compute_fn":{"name":"extractor_z","fn_name":"extractor_z","description":"", "reducer": false, "image_information": {"image_name": "name1", "tag": "tag1", "base_image": "base1", "run_strs": ["tuff", "life", "running", "docker"], "sdk_version":"1.2.3"}, "input_encoder":"cloudpickle", "output_encoder":"cloudpickle", "image_name": "default_image"}},"extractor_c":{"compute_fn":{"name":"extractor_c","fn_name":"extractor_c","description":"", "reducer": false, "image_information": {"image_name": "name1", "tag": "tag1", "base_image": "base1", "run_strs": ["tuff", "life", "running", "docker"], "sdk_version":"1.2.3"}, "input_encoder":"cloudpickle", "output_encoder":"cloudpickle", "image_name": "default_image"}}},"edges":{"extractor_a":["router_x"],"extractor_y":["extractor_c"],"extractor_z":["extractor_c"]},"runtime_information": {"major_version": 3, "minor_version": 10, "sdk_version": "1.2.3"}, "version": "1.2.3"}"#;
let mut json_value: serde_json::Value = serde_json::from_str(json).unwrap();
json_value["namespace"] = serde_json::Value::String("test".to_string());
let _: super::ComputeGraph = serde_json::from_value(json_value).unwrap();
}
#[test]
fn test_compute_fn_deserialization() {
let json = r#"{"name": "one", "fn_name": "two", "description": "desc", "reducer": true, "image_name": "im1", "image_information": {"image_name": "name1", "tag": "tag1", "base_image": "base1", "run_strs": ["tuff", "life", "running", "docker"], "sdk_version":"1.2.3"}, "input_encoder": "cloudpickle", "output_encoder":"cloudpickle"}"#;
let compute_fn: ComputeFn = serde_json::from_str(json).unwrap();
println!("{:?}", compute_fn);
}
#[test]
fn test_router_deserialization() {
let json = r#"{"name": "one", "source_fn": "two", "description": "desc", "target_fns": ["one", "two", "three"], "image_name": "im1", "image_information": {"image_name": "name1", "tag": "tag1", "base_image": "base1", "run_strs": ["tuff", "life", "running", "docker"], "sdk_version":"1.2.3"}, "encoder": "clouds"}"#;
let dynamic_router: DynamicRouter = serde_json::from_str(json).unwrap();
println!("{:?}", dynamic_router);
}
}