-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathjobs.rs
More file actions
1007 lines (897 loc) · 34.7 KB
/
Copy pathjobs.rs
File metadata and controls
1007 lines (897 loc) · 34.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::queries::api_queries::{DbCheckpoint, DbLogMessage, DbPipelineJob};
use anyhow::Context;
use arroyo_rpc::api_types::checkpoints::{
Checkpoint, CheckpointType, JobCheckpointSpan, OperatorCheckpointGroup, SubtaskCheckpointGroup,
};
use arroyo_rpc::api_types::pipelines::{JobLogLevel, JobLogMessage, OutputData, StopType};
use arroyo_rpc::api_types::{
CheckpointCollection, JobCollection, JobLogMessageCollection,
OperatorCheckpointGroupCollection, PaginationQueryParams,
};
use arroyo_rpc::grpc::api::OperatorCheckpointDetail;
use arroyo_rpc::public_ids::{IdTypes, generate_id};
use arroyo_rpc::{LeaderContext, StateContext, get_event_spans, grpc, job_status_client};
use axum::Json;
use axum::extract::{Path, Query, State};
use axum::response::sse::{Event, Sse};
use futures_util::stream::Stream;
use std::convert::Infallible;
use std::str::FromStr;
use std::{collections::HashMap, time::Duration};
use tokio_stream::StreamExt as _;
use tokio_stream::wrappers::ReceiverStream;
use tonic::{Code, Request};
use tracing::info;
const PREVIEW_TTL: Duration = Duration::from_secs(60);
use crate::pipelines::{query_job_by_pub_id, query_pipeline_by_pub_id};
use crate::rest::AppState;
use crate::rest_utils::{
BearerAuth, ErrorResp, PipelineJobCheckpointPath, PipelineJobPath, authenticate, bad_request,
conflict, internal_server_error, log_and_map, not_found, paginate_results,
validate_pagination_params,
};
use crate::types::public::LogLevel;
use crate::{AuthData, queries::api_queries, to_micros, types::public};
use arroyo_rpc::config::config;
use arroyo_rpc::controller_client;
use arroyo_rpc::errors::ErrorDomain;
use arroyo_rpc::grpc::rpc::job_status_grpc_client::JobStatusGrpcClient;
use arroyo_rpc::grpc::rpc::{GetCheckpointDetailsReq, GetJobCheckpointsReq};
use arroyo_rpc::identity::InjectWorkerId;
use cornucopia_async::DatabaseSource;
use http::StatusCode;
use tonic::codegen::InterceptedService;
use tonic::transport::Channel;
type LeaderCheckpointClient = JobStatusGrpcClient<InterceptedService<Channel, InjectWorkerId>>;
async fn fetch_from_leader<
R,
F: Future<Output = Result<R, tonic::Status>>,
T: FnOnce(LeaderCheckpointClient, u64) -> F,
>(
leader_context: LeaderContext,
req: T,
) -> Result<R, ErrorResp> {
let client = job_status_client(
"api",
&config().api.tls,
leader_context.worker_id,
leader_context.rpc_address,
None,
)
.await
.map_err(|_| ErrorResp {
status_code: StatusCode::BAD_GATEWAY,
message: "the leader is not currently available; try again later".to_string(),
})?;
req(client, leader_context.generation)
.await
.map_err(|e| match e.code() {
Code::FailedPrecondition | Code::NotFound => bad_request(e.message().to_string()),
Code::Unavailable => ErrorResp {
status_code: StatusCode::BAD_GATEWAY,
message: "the leader is not currently available; try again later".to_string(),
},
_ => log_and_map(e),
})
}
fn operator_checkpoint_groups(
operator_details: HashMap<String, OperatorCheckpointDetail>,
) -> Vec<OperatorCheckpointGroup> {
let mut operators = vec![];
operator_details
.iter()
.for_each(|(operator_id, operator_details)| {
let mut operator_bytes = 0;
let mut subtasks = vec![];
operator_details
.tasks
.iter()
.for_each(|(subtask_index, subtask_details)| {
operator_bytes += subtask_details.bytes.unwrap_or(0);
subtasks.push(SubtaskCheckpointGroup {
index: *subtask_index,
bytes: subtask_details.bytes.unwrap_or(0),
event_spans: get_event_spans(subtask_details).into(),
});
});
operators.push(OperatorCheckpointGroup {
operator_id: operator_id.to_string(),
bytes: operator_bytes,
started_metadata_write: operator_details.started_metadata_write,
finish_time: operator_details.finish_time,
subtasks,
});
});
operators
}
#[allow(clippy::too_many_arguments)]
pub(crate) async fn create_job(
pipeline_name: &str,
pipeline_id: i64,
checkpoint_interval: Duration,
preview: bool,
auth: &AuthData,
db: &DatabaseSource,
env_vars: HashMap<String, String>,
scheduler_config: serde_json::Value,
) -> Result<String, ErrorResp> {
let checkpoint_interval = if preview {
Duration::from_secs(24 * 60 * 60)
} else {
checkpoint_interval
};
if checkpoint_interval < Duration::from_secs(1)
|| checkpoint_interval > Duration::from_secs(24 * 60 * 60)
{
return Err(bad_request(
"Checkpoint_interval_micros must be between 1 second and 1 day.".to_string(),
));
}
let running_jobs = api_queries::fetch_get_jobs(&db.client().await?, &auth.organization_id)
.await?
.iter()
.filter(|j| {
j.stop == public::StopMode::none
&& !j
.state
.as_ref()
.map(|s| s == "Failed" || s == "Finished")
.unwrap_or(false)
})
.count();
if running_jobs > auth.org_metadata.max_running_jobs as usize {
let message = format!("You have exceeded the maximum number
of running jobs in your plan ({}). Stop an existing job or contact support@arroyo.systems for
an increase", auth.org_metadata.max_running_jobs);
return Err(bad_request(message));
}
let job_id = generate_id(IdTypes::JobConfig);
let env_vars_json =
serde_json::to_value(&env_vars).expect("HashMap<String, String> is always serializable");
// TODO: handle chance of collision in ids
api_queries::execute_create_job(
&db.client().await?,
&job_id,
&auth.organization_id,
&pipeline_name,
&auth.user_id,
&pipeline_id,
&(checkpoint_interval.as_micros() as i64),
&(if preview {
Some(PREVIEW_TTL.as_micros() as i64)
} else {
None
}),
&env_vars_json,
&scheduler_config,
)
.await?;
api_queries::execute_create_job_status(
&db.client().await?,
&generate_id(IdTypes::JobStatus),
&job_id,
&auth.organization_id,
)
.await?;
Ok(job_id)
}
fn replaceable_job(jobs: Vec<(String, String)>) -> Result<String, ErrorResp> {
let count = jobs.len();
let Some((job_id, state)) = jobs.into_iter().next() else {
return Err(not_found("Job for pipeline"));
};
if count != 1 {
return Err(internal_server_error(format!(
"expected one job for pipeline, found {count}"
)));
}
if state != "Stopped" && state != "Failed" {
return Err(conflict(format!(
"cannot restart job {job_id} without state while it is in state {state}; stop the job first"
)));
}
Ok(job_id)
}
/// Replaces a pipeline's single terminal job with a fresh job.
///
/// This is used for restart-without-state in leader mode. Keeping the replacement in a single
/// transaction preserves the current one-job-per-pipeline assumption for all other API queries.
pub(crate) async fn replace_job_without_state(
db: &DatabaseSource,
pipeline_pub_id: &str,
auth: &AuthData,
) -> Result<String, ErrorResp> {
let new_job_id = generate_id(IdTypes::JobConfig);
let new_status_id = generate_id(IdTypes::JobStatus);
match db {
DatabaseSource::Postgres(pool) => {
let mut client = pool.get().await.map_err(log_and_map)?;
let transaction = client.transaction().await.map_err(log_and_map)?;
// Lock the pipeline first. This serializes concurrent replacement requests before
// either request resolves the pipeline's current singleton job.
let pipeline = transaction
.query_opt(
"SELECT id FROM pipelines \
WHERE pub_id = $1 AND organization_id = $2 \
FOR UPDATE",
&[&pipeline_pub_id, &auth.organization_id],
)
.await
.map_err(log_and_map)?
.ok_or_else(|| not_found("Pipeline"))?;
let pipeline_id: i64 = pipeline.get(0);
let jobs = transaction
.query(
"SELECT c.id, COALESCE(s.state, 'Created') \
FROM job_configs c \
INNER JOIN job_statuses s ON c.id = s.id \
WHERE c.pipeline_id = $1 AND c.organization_id = $2 \
FOR UPDATE OF c, s",
&[&pipeline_id, &auth.organization_id],
)
.await
.map_err(log_and_map)?
.into_iter()
.map(|row| (row.get(0), row.get(1)))
.collect();
let old_job_id = replaceable_job(jobs)?;
let inserted = transaction
.execute(
"INSERT INTO job_configs \
(id, organization_id, pipeline_name, created_by, updated_by, updated_at, \
ttl_micros, stop, pipeline_id, parallelism_overrides, \
checkpoint_interval_micros, env_vars, scheduler_config) \
SELECT $1, organization_id, pipeline_name, created_by, $2, CURRENT_TIMESTAMP, \
ttl_micros, 'none', pipeline_id, parallelism_overrides, \
checkpoint_interval_micros, env_vars, scheduler_config \
FROM job_configs WHERE id = $3",
&[&new_job_id, &auth.user_id, &old_job_id],
)
.await
.map_err(log_and_map)?;
if inserted != 1 {
return Err(internal_server_error("failed to clone job configuration"));
}
transaction
.execute(
"INSERT INTO job_statuses (pub_id, id, organization_id) VALUES ($1, $2, $3)",
&[&new_status_id, &new_job_id, &auth.organization_id],
)
.await
.map_err(log_and_map)?;
// Delete children explicitly because the SQLite checkpoints table does not have the
// same foreign-key cascade as PostgreSQL.
transaction
.execute("DELETE FROM checkpoints WHERE job_id = $1", &[&old_job_id])
.await
.map_err(log_and_map)?;
transaction
.execute(
"DELETE FROM job_log_messages WHERE job_id = $1",
&[&old_job_id],
)
.await
.map_err(log_and_map)?;
transaction
.execute("DELETE FROM job_statuses WHERE id = $1", &[&old_job_id])
.await
.map_err(log_and_map)?;
transaction
.execute("DELETE FROM job_configs WHERE id = $1", &[&old_job_id])
.await
.map_err(log_and_map)?;
transaction.commit().await.map_err(log_and_map)?;
}
DatabaseSource::Sqlite(connection) => {
let mut connection = connection.lock().map_err(log_and_map)?;
let transaction = connection.transaction().map_err(log_and_map)?;
// Beginning a SQLite transaction acquires the connection mutex for this entire block,
// so resolving and replacing the singleton job cannot interleave with another request.
let pipeline_id = transaction
.query_row(
"SELECT id FROM pipelines WHERE pub_id = ?1 AND organization_id = ?2",
rusqlite::params![pipeline_pub_id, auth.organization_id],
|row| row.get::<_, i64>(0),
)
.map_err(|error| match error {
rusqlite::Error::QueryReturnedNoRows => not_found("Pipeline"),
error => log_and_map(error),
})?;
let jobs = {
let mut statement = transaction
.prepare(
"SELECT c.id, COALESCE(s.state, 'Created') \
FROM job_configs c \
INNER JOIN job_statuses s ON c.id = s.id \
WHERE c.pipeline_id = ?1 AND c.organization_id = ?2",
)
.map_err(log_and_map)?;
statement
.query_map(
rusqlite::params![pipeline_id, auth.organization_id],
|row| Ok((row.get(0)?, row.get(1)?)),
)
.map_err(log_and_map)?
.collect::<Result<Vec<_>, _>>()
.map_err(log_and_map)?
};
let old_job_id = replaceable_job(jobs)?;
let inserted = transaction
.execute(
"INSERT INTO job_configs \
(id, organization_id, pipeline_name, created_by, updated_by, updated_at, \
ttl_micros, stop, pipeline_id, parallelism_overrides, \
checkpoint_interval_micros, env_vars, scheduler_config) \
SELECT ?1, organization_id, pipeline_name, created_by, ?2, CURRENT_TIMESTAMP, \
ttl_micros, 'none', pipeline_id, parallelism_overrides, \
checkpoint_interval_micros, env_vars, scheduler_config \
FROM job_configs WHERE id = ?3",
rusqlite::params![new_job_id, auth.user_id, old_job_id],
)
.map_err(log_and_map)?;
if inserted != 1 {
return Err(internal_server_error("failed to clone job configuration"));
}
transaction
.execute(
"INSERT INTO job_statuses (pub_id, id, organization_id) VALUES (?1, ?2, ?3)",
rusqlite::params![new_status_id, new_job_id, auth.organization_id],
)
.map_err(log_and_map)?;
transaction
.execute(
"DELETE FROM checkpoints WHERE job_id = ?1",
rusqlite::params![old_job_id],
)
.map_err(log_and_map)?;
transaction
.execute(
"DELETE FROM job_log_messages WHERE job_id = ?1",
rusqlite::params![old_job_id],
)
.map_err(log_and_map)?;
transaction
.execute(
"DELETE FROM job_statuses WHERE id = ?1",
rusqlite::params![old_job_id],
)
.map_err(log_and_map)?;
transaction
.execute(
"DELETE FROM job_configs WHERE id = ?1",
rusqlite::params![old_job_id],
)
.map_err(log_and_map)?;
transaction.commit().map_err(log_and_map)?;
}
}
Ok(new_job_id)
}
pub(crate) fn get_action(state: &str, running_desired: &bool) -> (String, Option<StopType>, bool) {
enum Progress {
InProgress,
Stable,
}
use Progress::*;
use StopType::*;
let (a, s, p) = match (state, running_desired) {
("Created", true) => ("Stop", Some(Checkpoint), InProgress),
("Created", false) => ("Start", Some(None), Stable),
("Compiling", true) => ("Stop", Some(Checkpoint), InProgress),
("Compiling", false) => ("Stopping", Option::None, InProgress),
("Scheduling", true) => ("Stop", Some(Checkpoint), InProgress),
("Scheduling", false) => ("Stopping", Option::None, InProgress),
("Running", true) => ("Stop", Some(Checkpoint), Stable),
("Running", false) => ("Stopping", Option::None, InProgress),
("Rescaling", true) => ("Stop", Some(Checkpoint), InProgress),
("Rescaling", false) => ("Stopping", Option::None, InProgress),
("CheckpointStopping", true) => ("Force Stop", Some(Immediate), InProgress),
("CheckpointStopping", false) => ("Force Stop", Some(Immediate), InProgress),
("Recovering", true) => ("Stop", Some(Checkpoint), InProgress),
("Recovering", false) => ("Stopping", Option::None, InProgress),
("Restarting", true) => ("Stop", Some(Checkpoint), InProgress),
("Restarting", false) => ("Stopping", Option::None, InProgress),
("Stopping", true) => ("Stopping", Some(Checkpoint), InProgress),
("Stopping", false) => ("Stopping", Option::None, InProgress),
("Stopped", true) => ("Starting", Option::None, InProgress),
("Stopped", false) => ("Start", Some(None), Stable),
("Finishing", true) => ("Finishing", Option::None, InProgress),
("Finishing", false) => ("Finishing", Option::None, InProgress),
("Finished", true) => ("Finished", Option::None, Stable),
("Finished", false) => ("Finished", Option::None, Stable),
("Failed", true) => ("Failed", Option::None, Stable),
("Failed", false) => ("Start", Some(None), Stable),
// Failing is a transient state during graceful shutdown before Failed
("Failing", true) => ("Failing", Option::None, InProgress),
("Failing", false) => ("Failing", Option::None, InProgress),
_ => panic!("unhandled state {state}"),
};
let in_progress = match p {
InProgress => true,
Stable => false,
};
(a.to_string(), s, in_progress)
}
/// List a job's error messages
#[utoipa::path(
get,
path = "/v1/pipelines/{pipeline_id}/jobs/{job_id}/errors",
tag = "jobs",
params(
("pipeline_id" = String, Path, description = "Pipeline id"),
("job_id" = String, Path, description = "Job id"),
("starting_after" = Option<String>, Query, description = "Starting after"),
("limit" = Option<u32>, Query, description = "Limit"),
),
responses(
(status = 200, description = "Got job's error messages", body = JobLogMessageCollection),
),
)]
pub async fn get_job_errors(
State(state): State<AppState>,
bearer_auth: BearerAuth,
Path(PipelineJobPath {
id: pipeline_pub_id,
job_id: job_pub_id,
}): Path<PipelineJobPath>,
query_params: Query<PaginationQueryParams>,
) -> Result<Json<JobLogMessageCollection>, ErrorResp> {
let auth_data = authenticate(&state.database, bearer_auth).await?;
let db = state.database.client().await?;
let (starting_after, limit) =
validate_pagination_params(query_params.starting_after.clone(), query_params.limit)?;
query_job_by_pub_id(&pipeline_pub_id, &job_pub_id, &db, &auth_data).await?;
let errors = api_queries::fetch_get_operator_errors(
&db,
&auth_data.organization_id,
&job_pub_id,
&starting_after.unwrap_or_default(),
&(limit as i32),
)
.await
.map_err(log_and_map)?
.into_iter()
.map(|m| m.into())
.collect();
let (errors, has_more) = paginate_results(errors, limit);
Ok(Json(JobLogMessageCollection {
data: errors,
has_more,
}))
}
impl From<DbLogMessage> for JobLogMessage {
fn from(val: DbLogMessage) -> Self {
let level: JobLogLevel = match val.log_level {
LogLevel::info => JobLogLevel::Info,
LogLevel::warn => JobLogLevel::Warn,
LogLevel::error => JobLogLevel::Error,
};
JobLogMessage {
id: val.pub_id,
created_at: to_micros(val.created_at),
operator_id: val.operator_id,
task_index: val.task_index.map(|i| i as u64),
level,
message: val.message,
details: val.details,
error_domain: ErrorDomain::from_str(&val.error_domain).ok(),
}
}
}
/// List a job's checkpoints
#[utoipa::path(
get,
path = "/v1/pipelines/{pipeline_id}/jobs/{job_id}/checkpoints",
tag = "jobs",
params(
("pipeline_id" = String, Path, description = "Pipeline id"),
("job_id" = String, Path, description = "Job id")
),
responses(
(status = 200, description = "Got job's checkpoints", body = CheckpointCollection),
),
)]
pub async fn get_job_checkpoints(
State(state): State<AppState>,
bearer_auth: BearerAuth,
Path(PipelineJobPath {
id: pipeline_pub_id,
job_id: job_pub_id,
}): Path<PipelineJobPath>,
) -> Result<Json<CheckpointCollection>, ErrorResp> {
let db = state.database.client().await?;
let auth_data = authenticate(&state.database, bearer_auth).await?;
let job = api_queries::fetch_get_pipeline_job(
&db,
&auth_data.organization_id,
&pipeline_pub_id,
&job_pub_id,
)
.await?
.into_iter()
.next()
.ok_or_else(|| not_found("Job"))?;
let state_context: Option<StateContext> = job
.state_context
.map(serde_json::from_value)
.transpose()
.with_context(|| format!("converting state context for job {}", job_pub_id))
.map_err(log_and_map)?;
let checkpoints = if let Some(state_context) = state_context
&& let Some(leader) = state_context.leader
{
fetch_from_leader(leader, move |mut client, generation| async move {
client
.get_job_checkpoints(GetJobCheckpointsReq {
job_id: job.id,
generation,
})
.await
})
.await?
.into_inner()
.checkpoints
.into_iter()
.map(Checkpoint::from)
.collect()
} else {
api_queries::fetch_get_job_checkpoints(&db, &job_pub_id, &auth_data.organization_id)
.await
.map_err(log_and_map)?
.into_iter()
.filter_map(|m| m.try_into().ok())
.collect()
};
Ok(Json(CheckpointCollection { data: checkpoints }))
}
/// Get a checkpoint's details
#[utoipa::path(
get,
path = "/v1/pipelines/{pipeline_id}/jobs/{job_id}/checkpoints/{epoch}/operator_checkpoint_groups",
tag = "jobs",
params(
("pipeline_id" = String, Path, description = "Pipeline id"),
("job_id" = String, Path, description = "Job id"),
("epoch" = u32, Path, description = "Epoch")
),
responses(
(status = 200, description = "Got checkpoint's details", body = OperatorCheckpointGroupCollection),
),
)]
pub async fn get_checkpoint_details(
State(state): State<AppState>,
bearer_auth: BearerAuth,
Path(PipelineJobCheckpointPath {
id: pipeline_pub_id,
job_id: job_pub_id,
epoch,
}): Path<PipelineJobCheckpointPath>,
) -> Result<Json<OperatorCheckpointGroupCollection>, ErrorResp> {
let db = state.database.client().await?;
let auth_data = authenticate(&state.database, bearer_auth).await?;
let job = api_queries::fetch_get_pipeline_job(
&db,
&auth_data.organization_id,
&pipeline_pub_id,
&job_pub_id,
)
.await?
.into_iter()
.next()
.ok_or_else(|| not_found("Job"))?;
let state_context: Option<StateContext> = job
.state_context
.map(serde_json::from_value)
.transpose()
.with_context(|| format!("converting state context for job {}", job_pub_id))
.map_err(log_and_map)?;
let operators = if let Some(state_context) = state_context
&& let Some(leader) = state_context.leader
{
fetch_from_leader(leader, move |mut client, generation| async move {
client
.get_checkpoint_details(GetCheckpointDetailsReq {
job_id: job.id,
generation,
epoch: epoch as u64,
})
.await
})
.await?
.into_inner()
.operators
} else {
let checkpoint_details = api_queries::fetch_get_checkpoint_details(
&db,
&job_pub_id,
&auth_data.organization_id,
&(epoch as i32),
)
.await
.map_err(log_and_map)?
.into_iter()
.next()
.ok_or_else(|| {
not_found(&format!(
"Checkpoint with epoch {epoch} for job '{job_pub_id}'"
))
})?;
checkpoint_details
.operators
.map(|o| serde_json::from_value(o).unwrap())
.unwrap_or_else(HashMap::<String, OperatorCheckpointDetail>::new)
};
let operators = operator_checkpoint_groups(operators);
Ok(Json(OperatorCheckpointGroupCollection { data: operators }))
}
/// Subscribe to a job's output
#[utoipa::path(
get,
path = "/v1/pipelines/{pipeline_id}/jobs/{job_id}/output",
tag = "jobs",
params(
("pipeline_id" = String, Path, description = "Pipeline id"),
("job_id" = String, Path, description = "Job id")
),
responses(
(status = 200, description = "Job output as 'text/event-stream'"),
),
)]
pub async fn get_job_output(
State(state): State<AppState>,
bearer_auth: BearerAuth,
Path(PipelineJobPath {
id: pipeline_pub_id,
job_id: job_pub_id,
}): Path<PipelineJobPath>,
) -> Result<Sse<impl Stream<Item = Result<Event, Infallible>>>, ErrorResp> {
let db = state.database.client().await?;
let auth_data = authenticate(&state.database, bearer_auth).await?;
// validate that the job exists, the user has access, and the graph has a GrpcSink
query_job_by_pub_id(&pipeline_pub_id, &job_pub_id, &db, &auth_data).await?;
let pipeline = query_pipeline_by_pub_id(&pipeline_pub_id, &db, &auth_data).await?;
if !pipeline
.graph
.nodes
.iter()
.any(|n| n.operator.contains("preview"))
{
// TODO: make this check more robust
return Err(bad_request("Job does not have a preview sink".to_string()));
}
let (tx, rx) = tokio::sync::mpsc::channel(32);
let mut controller = controller_client("api", &config().api.tls)
.await
.map_err(log_and_map)?;
let mut stream = controller
.subscribe_to_output(Request::new(grpc::rpc::GrpcOutputSubscription {
job_id: job_pub_id.clone(),
}))
.await
.map_err(|e| match e.code() {
Code::FailedPrecondition | Code::NotFound => bad_request(e.message().to_string()),
_ => log_and_map(e),
})?
.into_inner();
info!("Subscribed to output");
tokio::spawn(async move {
let _controller = controller;
let mut message_count = 0;
while let Some(d) = stream.next().await {
if d.as_ref().map(|t| t.done).unwrap_or(false) {
info!("Stream done for {}", job_pub_id);
break;
}
let v = match d {
Ok(d) => d,
Err(_) => break,
};
let output_data: OutputData = v.into();
let e = Ok(Event::default()
.json_data(output_data)
.unwrap()
.id(message_count.to_string()));
if tx.send(e).await.is_err() {
break;
}
message_count += 1;
}
info!("Closing watch stream for {}", job_pub_id);
});
Ok(Sse::new(ReceiverStream::new(rx)))
}
/// Get all jobs
#[utoipa::path(
get,
path = "/v1/jobs",
tag = "jobs",
responses(
(status = 200, description = "Get all jobs", body = JobCollection),
),
)]
pub async fn get_jobs(
State(state): State<AppState>,
bearer_auth: BearerAuth,
) -> Result<Json<JobCollection>, ErrorResp> {
let auth_data = authenticate(&state.database, bearer_auth).await?;
let jobs: Vec<DbPipelineJob> = api_queries::fetch_get_all_jobs(
&state.database.client().await?,
&auth_data.organization_id,
)
.await?;
Ok(Json(JobCollection {
data: jobs.into_iter().map(|p| p.into()).collect(),
}))
}
impl TryFrom<DbCheckpoint> for Checkpoint {
type Error = anyhow::Error;
fn try_from(val: DbCheckpoint) -> anyhow::Result<Self> {
let events: Vec<JobCheckpointSpan> = serde_json::from_value(val.event_spans)?;
Ok(Checkpoint {
epoch: val.epoch as u64,
backend: val.state_backend,
checkpoint_type: CheckpointType::from_is_stopping(val.is_stopping),
start_time: to_micros(val.start_time),
finish_time: val.finish_time.map(to_micros),
events: events.into_iter().map(|e| e.into()).collect(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::OrgMetadata;
use std::sync::{Arc, Mutex};
fn auth() -> AuthData {
AuthData {
user_id: "user_2".to_string(),
organization_id: "org_1".to_string(),
role: "user".to_string(),
org_metadata: OrgMetadata::default(),
}
}
fn sqlite_job(state: &str) -> DatabaseSource {
let connection = rusqlite::Connection::open_in_memory().unwrap();
connection
.execute_batch(
"CREATE TABLE pipelines (
id INTEGER PRIMARY KEY,
pub_id TEXT NOT NULL,
organization_id TEXT NOT NULL
);
CREATE TABLE job_configs (
id TEXT PRIMARY KEY,
organization_id TEXT,
pipeline_name TEXT NOT NULL,
created_by TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_by TEXT,
updated_at TIMESTAMP,
ttl_micros INTEGER,
stop TEXT DEFAULT 'none' NOT NULL,
parallelism_overrides TEXT DEFAULT '{}' NOT NULL,
checkpoint_interval_micros INTEGER DEFAULT 10000000 NOT NULL,
pipeline_id INTEGER NOT NULL,
restart_nonce INTEGER DEFAULT 0 NOT NULL,
restart_mode TEXT DEFAULT 'safe' NOT NULL,
ignore_state_before_epoch INTEGER,
env_vars TEXT DEFAULT '{}' NOT NULL,
scheduler_config TEXT DEFAULT '{}' NOT NULL
);
CREATE TABLE job_statuses (
pub_id TEXT NOT NULL UNIQUE,
id TEXT PRIMARY KEY,
organization_id TEXT,
run_id INTEGER DEFAULT 0 NOT NULL,
state TEXT DEFAULT 'Created' NOT NULL
);
CREATE TABLE checkpoints (job_id TEXT NOT NULL);
CREATE TABLE job_log_messages (job_id TEXT NOT NULL);
INSERT INTO pipelines (id, pub_id, organization_id)
VALUES (1, 'pl_1', 'org_1');
INSERT INTO job_configs
(id, organization_id, pipeline_name, created_by, updated_by, ttl_micros, stop,
parallelism_overrides, checkpoint_interval_micros, pipeline_id, restart_nonce,
restart_mode, ignore_state_before_epoch, env_vars, scheduler_config)
VALUES
('job_old', 'org_1', 'pipeline', 'user_1', 'user_1', 123, 'immediate',
'{\"1\": 4}', 5000000, 1, 3, 'force', 42,
'{\"ENV\": \"value\"}', '{\"scheduler\": true}');
INSERT INTO checkpoints (job_id) VALUES ('job_old');
INSERT INTO job_log_messages (job_id) VALUES ('job_old');",
)
.unwrap();
connection
.execute(
"INSERT INTO job_statuses (pub_id, id, organization_id, run_id, state)
VALUES ('js_old', 'job_old', 'org_1', 7, ?1)",
[state],
)
.unwrap();
DatabaseSource::Sqlite(Arc::new(Mutex::new(connection)))
}
#[tokio::test]
async fn replace_job_without_state_replaces_terminal_job_and_deletes_history() {
let database = sqlite_job("Stopped");
let new_job_id = replace_job_without_state(&database, "pl_1", &auth())
.await
.unwrap();
assert_ne!(new_job_id, "job_old");
let DatabaseSource::Sqlite(connection) = &database else {
unreachable!()
};
let connection = connection.lock().unwrap();
let job: (
String,
String,
i64,
String,
String,
Option<i64>,
String,
String,
) = connection
.query_row(
"SELECT id, stop, restart_nonce, restart_mode, updated_by,
ignore_state_before_epoch, env_vars, scheduler_config
FROM job_configs",
[],
|row| {
Ok((
row.get(0)?,
row.get(1)?,
row.get(2)?,
row.get(3)?,
row.get(4)?,
row.get(5)?,
row.get(6)?,
row.get(7)?,
))
},
)
.unwrap();
assert_eq!(
job,
(
new_job_id.clone(),
"none".to_string(),
0,
"safe".to_string(),
"user_2".to_string(),
None,
"{\"ENV\": \"value\"}".to_string(),
"{\"scheduler\": true}".to_string(),
)
);
let status: (String, i64, String) = connection
.query_row("SELECT id, run_id, state FROM job_statuses", [], |row| {
Ok((row.get(0)?, row.get(1)?, row.get(2)?))
})
.unwrap();
assert_eq!(status, (new_job_id, 0, "Created".to_string()));
for table in ["checkpoints", "job_log_messages"] {
let count: i64 = connection
.query_row(&format!("SELECT COUNT(*) FROM {table}"), [], |row| {
row.get(0)
})
.unwrap();
assert_eq!(count, 0, "{table} should be cleared");
}
}
#[tokio::test]
async fn replace_job_without_state_rejects_non_terminal_job() {
let database = sqlite_job("Running");
let error = replace_job_without_state(&database, "pl_1", &auth())
.await
.unwrap_err();
assert_eq!(error.status_code, StatusCode::CONFLICT);
let DatabaseSource::Sqlite(connection) = &database else {
unreachable!()
};