/
TimeSeriesScheduler.scala
1197 lines (1063 loc) · 46.1 KB
/
TimeSeriesScheduler.scala
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
package com.criteo.cuttle.timeseries
import java.time.ZoneOffset.UTC
import java.time._
import java.time.temporal.ChronoUnit._
import java.time.temporal.{ChronoUnit, TemporalAdjusters}
import java.util.{Comparator, UUID}
import scala.collection.mutable
import scala.concurrent._
import scala.concurrent.duration.{Duration => ScalaDuration}
import scala.concurrent.stm.Txn.ExternalDecider
import scala.concurrent.stm._
import scala.math.Ordering.Implicits._
import cats._
import cats.effect.IO
import cats.implicits._
import de.sciss.fingertree.RangedSeq
import doobie._
import doobie.implicits._
import io.circe._
import io.circe.generic.semiauto._
import io.circe.syntax._
import codes.reactive.scalatime._
import com.criteo.cuttle.ThreadPools.Implicits.sideEffectThreadPool
import com.criteo.cuttle.ThreadPools._
import com.criteo.cuttle.Metrics._
import com.criteo.cuttle._
import com.criteo.cuttle.timeseries.Internal._
import com.criteo.cuttle.timeseries.TimeSeriesCalendar.{Daily, Monthly, NHourly, Weekly}
import com.criteo.cuttle.timeseries.intervals.Bound.{Bottom, Finite, Top}
import com.criteo.cuttle.timeseries.intervals.{Interval, IntervalMap}
/** Represents calendar partitions for which a job will be run by the [[TimeSeriesScheduler]].
* See the companion object for the available calendars. */
sealed trait TimeSeriesCalendar {
private[timeseries] def next(t: Instant): Instant
private[timeseries] def truncate(t: Instant): Instant
private[timeseries] def ceil(t: Instant): Instant = {
val truncated = truncate(t)
if (truncated == t) t
else next(t)
}
private[timeseries] def inInterval(interval: Interval[Instant], batching: TimeSeriesBatching) = {
def go(lo: Instant, hi: Instant, acc: List[(Instant, Instant)]): List[(Instant, Instant)] = {
val nextLo = next(lo)
if (nextLo.isAfter(hi)) acc
else go(nextLo, hi, (lo, nextLo) +: acc)
}
interval match {
case Interval(Finite(lo), Finite(hi)) =>
go(ceil(lo), hi, List.empty).reverse.grouped(batching.size).map(xs => (xs.head._1, xs.last._2))
case _ =>
sys.error("panic")
}
}
private[timeseries] def split(interval: Interval[Instant]) = {
def go(lo: Instant, hi: Instant, acc: List[(Instant, Instant)]): List[(Instant, Instant)] = {
val nextLo = next(lo)
if (nextLo.isBefore(hi)) go(nextLo, hi, (lo, nextLo) +: acc)
else (lo, hi) +: acc
}
interval match {
case Interval(Finite(lo), Finite(hi)) =>
go(lo, hi, List.empty).reverse
case _ =>
sys.error("panic")
}
}
}
private[timeseries] sealed trait TimeSeriesCalendarView {
def calendar: TimeSeriesCalendar
def upper(): TimeSeriesCalendarView
val aggregationFactor: Int
}
/** Define the available calendars. */
object TimeSeriesCalendar {
/** A calendar for periods of n hours. Hours are defined as complete calendar hours starting
* at 00 minutes, 00 seconds. */
case class NHourly(n: Int) extends TimeSeriesCalendar {
def truncate(t: Instant): Instant = {
val hour = (t.atOffset(java.time.ZoneOffset.UTC).getHour / n) * n
t.truncatedTo(HOURS).atOffset(java.time.ZoneOffset.UTC).withHour(hour).toInstant
}
def next(t: Instant): Instant =
truncate(t).plus(n, HOURS)
}
/** An daily calendar. Days are defined as complete calendar days starting a midnight and
* during 24 hours. If the specified timezone defines lightsaving it is possible that some
* days are 23 or 25 horus thus.
*
* @param tz The time zone for which these _days_ are defined.
*/
case class Daily(tz: ZoneId) extends TimeSeriesCalendar {
def truncate(t: Instant) = t.atZone(tz).truncatedTo(DAYS).toInstant
def next(t: Instant) = t.atZone(tz).truncatedTo(DAYS).plus(1, DAYS).toInstant
}
/** A weekly calendar. Weeks are defined as complete calendar weeks starting on a specified day of week
* at midnight and lasting 7 days. The specified time zone is used to define the exact week start instant.
*
* @param tz The time zone for which these _weeks_ are defined.
* @param firstDay The first day of the week for these weeks.
*/
case class Weekly(tz: ZoneId, firstDay: DayOfWeek) extends TimeSeriesCalendar {
private def truncateToWeek(t: ZonedDateTime) =
t.`with`(TemporalAdjusters.previousOrSame(firstDay)).truncatedTo(DAYS)
def truncate(t: Instant) = truncateToWeek(t.atZone(tz)).toInstant
def next(t: Instant) = truncateToWeek(t.atZone(tz)).plus(1, WEEKS).toInstant
}
/** An monthly calendar. Months are defined as complete calendar months starting on the 1st day and
* during 28,29,30 or 31 days. The specified time zone is used to define the exact month start instant.
*
* @param tz The time zone for which these _months_ are defined.
*/
case class Monthly(tz: ZoneId) extends TimeSeriesCalendar {
private val truncateToMonth = (t: ZonedDateTime) =>
t.`with`(TemporalAdjusters.firstDayOfMonth()).truncatedTo(ChronoUnit.DAYS)
def truncate(t: Instant) = truncateToMonth(t.atZone(tz)).toInstant
def next(t: Instant) = truncateToMonth(t.atZone(tz)).plus(1, MONTHS).toInstant
}
private[timeseries] implicit val calendarEncoder = new Encoder[TimeSeriesCalendar] {
override def apply(calendar: TimeSeriesCalendar) = calendar match {
case NHourly(h) => Json.obj("period" -> "hourly".asJson, "n" -> h.asJson)
case Daily(tz: ZoneId) =>
Json.obj(
"period" -> "daily".asJson,
"zoneId" -> tz.getId().asJson
)
case Weekly(tz: ZoneId, firstDay: DayOfWeek) =>
Json.obj(
"period" -> "weekly".asJson,
"zoneId" -> tz.getId.asJson,
"firstDay" -> firstDay.toString.asJson
)
case Monthly(tz: ZoneId) =>
Json.obj(
"period" -> "monthly".asJson,
"zoneId" -> tz.getId().asJson
)
}
}
}
private[timeseries] object TimeSeriesCalendarView {
def apply(calendar: TimeSeriesCalendar) = calendar match {
case TimeSeriesCalendar.NHourly(h) => new NHourlyView(1, h)
case TimeSeriesCalendar.Daily(tz) => new DailyView(tz, 1)
case TimeSeriesCalendar.Weekly(tz, firstDay) => new WeeklyView(tz, firstDay, 1)
case TimeSeriesCalendar.Monthly(tz) => new MonthlyView(tz, 1)
}
sealed trait GenericView extends TimeSeriesCalendarView {
def over: (Int, TimeSeriesCalendar)
def calendar = over._2
def truncate(t: Instant) = calendar.truncate(t)
def next(t: Instant) = (1 to over._1).foldLeft(calendar.truncate(t))((acc, _) => calendar.next(acc))
def upper(): TimeSeriesCalendarView
}
case class NHourlyView(aggregationFactor: Int, nHours: Int) extends GenericView {
override def over: (Int, TimeSeriesCalendar) = (1, NHourly(nHours))
override def upper(): TimeSeriesCalendarView = new DailyView(UTC, aggregationFactor * 24 / nHours)
}
case class DailyView(tz: ZoneId, aggregationFactor: Int) extends GenericView {
def over = (1, Daily(tz))
override def upper: TimeSeriesCalendarView = new WeeklyView(tz, DayOfWeek.MONDAY, aggregationFactor * 7)
}
case class WeeklyView(tz: ZoneId, firstDay: DayOfWeek, aggregationFactor: Int) extends GenericView {
def over = (1, Weekly(tz, firstDay))
override def upper: TimeSeriesCalendarView = new MonthlyView(tz, aggregationFactor * 4)
}
case class MonthlyView(tz: ZoneId, aggregationFactor: Int) extends GenericView {
def over = (1, Monthly(tz))
override def upper: TimeSeriesCalendarView = new MonthlyView(tz, 1)
}
}
/** A [[Backfill]] allows to recompute already computed time partitions in the past.
*
* @param id Unique id for the backfill.
* @param start Start instant for the partitions to backfill.
* @param end End instant for the partitions to backfill.
* @param jobs Indicates the part of the graph to backfill.
* @param priority The backfill priority. If minus than 0 it is less priority than the day
* to day workload. If more than 0 it becomes more prioritary and can delay
* the day to day workload.
* @param description Description (for audit logs).
* @param status Status of the backfill.
* @param createdBy User who created the backfill (for audit logs).
*/
case class Backfill(id: String,
start: Instant,
end: Instant,
jobs: Set[Job[TimeSeries]],
priority: Int,
name: String,
description: String,
status: String,
createdBy: String)
private[timeseries] object Backfill {
implicit val eqInstance: Eq[Backfill] = Eq.fromUniversalEquals[Backfill]
implicit val encoder: Encoder[Backfill] = deriveEncoder
implicit def decoder(implicit jobs: Set[Job[TimeSeries]]) =
new Decoder[Backfill] {
def apply(c: HCursor): Decoder.Result[Backfill] =
for {
id <- c.downField("id").as[String]
start <- c.downField("start").as[Instant]
end <- c.downField("end").as[Instant]
jobs <- c.downField("jobs").as[Seq[String]].right.map { jobIds =>
jobIds.flatMap { jobId =>
jobs.find(_.id == jobId).toSeq
}.toSet
}
priority <- c.downField("priority").as[Int]
name <- c.downField("name").as[String]
description <- c.downField("description").as[String]
status <- c.downField("status").as[String]
createdBy <- c.downField("createdBy").as[String]
} yield Backfill(id, start, end, jobs, priority, name, description, status, createdBy)
}
}
/** A [[TimeSeriesContext]] is passed to [[com.criteo.cuttle.Execution Executions]] initiated by
* the [[TimeSeriesScheduler]].
*
* @param start Start instant of the partition to compute.
* @param end End instant of the partition to compute.
* @param backfill If this execution is for a backfill, the [[Backfill]] informations are provided.
*/
case class TimeSeriesContext(start: Instant,
end: Instant,
backfill: Option[Backfill] = None,
projectVersion: String = "")
extends SchedulingContext {
override def asJson: Json = TimeSeriesContext.encoder(this)
def toId: String = {
val priority = backfill.fold(0)(_.priority)
val bytesPriority = BigInt(priority).toByteArray
val paddedPriority: Array[Byte] = bytesPriority.reverse.padTo(10, '\u0000'.toByte).reverse
s"${start}${paddedPriority.mkString}${UUID.randomUUID().toString}"
}
override def logIntoDatabase: ConnectionIO[String] = Database.serializeContext(this)
def toInterval: Interval[Instant] = Interval(start, end)
def compareTo(other: SchedulingContext) = other match {
case TimeSeriesContext(otherStart, _, otherBackfill, _) =>
val priority: (Option[Backfill] => Int) = _.map(_.priority).getOrElse(0)
val thisBackfillPriority = priority(backfill)
val otherBackfillPriority = priority(otherBackfill)
if (thisBackfillPriority == otherBackfillPriority) {
start.compareTo(otherStart)
} else {
thisBackfillPriority.compareTo(otherBackfillPriority)
}
}
override def longRunningId(): String =
(start, end, backfill).toString
}
object TimeSeriesContext {
private[timeseries] implicit val encoder: Encoder[TimeSeriesContext] = deriveEncoder
private[timeseries] implicit def decoder(implicit jobs: Set[Job[TimeSeries]]): Decoder[TimeSeriesContext] =
deriveDecoder
/** Provide an implicit `Ordering` for [[TimeSeriesContext]] based on the `compareTo` function. */
implicit val ordering: Ordering[TimeSeriesContext] =
Ordering.comparatorToOrdering(new Comparator[TimeSeriesContext] {
def compare(o1: TimeSeriesContext, o2: TimeSeriesContext) = o1.compareTo(o2)
})
}
/** A [[TimeSeriesDependency]] qualify the dependency between 2 [[com.criteo.cuttle.Job Jobs]] in a
* [[TimeSeries]] [[com.criteo.cuttle.Workflow Workflow]]. It can be configured to `offset` the dependency.
*
* Supposing job1 depends on job2 with dependency descriptor (offsetLow, offsetHigh).
* Then to execute period (low, high) of job1, we need period
* (low+offsetLow, high+offsetHigh) of job2.
*
* @param offsetLow the offset for the low end of the duration
* @param offsetHigh the offset for the high end of the duration
*
*/
case class TimeSeriesDependency(offsetLow: Duration, offsetHigh: Duration)
/**
* The maximum number of partitions the job can handle at once and a delay the scheduler will wait for partition to arrive. If the size is defined
* to a value more than `1`, the scheduler will wait for delay trigger [[com.criteo.cuttle.Execution Execution]]
* with a scheduling context extended to by the @size.
*
* @param size the maximum number of joint intervals which are going to be run within single execution.
* @param delay the delay for which the scheduler will wait for the new executions to arrive for current batch.
*
*/
case class TimeSeriesBatching(size: Int, delay: Duration) {
require(size >= 1)
def asJson: Json =
Json.obj(
"size" -> size.asJson,
"delay" -> delay.toMillis.asJson
)
}
object TimeSeriesBatching {
val default = TimeSeriesBatching(1, 0.seconds)
}
/** Configure a [[com.criteo.cuttle.Job Job]] as a [[TimeSeries]] job,
*
* @param calendar The calendar partitions configuration for this job (for example hourly or daily).
* @param start The start instant at which this job must start being executed.
* @param batching The batching parameters [[com.criteo.cuttle.timeseries.TimeSeriesBatching]].
*
*/
case class TimeSeries(calendar: TimeSeriesCalendar,
start: Instant,
end: Option[Instant] = None,
batching: TimeSeriesBatching = TimeSeriesBatching.default)
extends Scheduling {
type Context = TimeSeriesContext
override def asJson: Json =
Json.obj(
"kind" -> "timeseries".asJson,
"start" -> start.asJson,
"end" -> end.asJson,
"batching" -> batching.asJson,
"calendar" -> calendar.asJson
)
}
/** [[TimeSeries]] utilities. */
object TimeSeries
private[timeseries] sealed trait JobState
private[timeseries] object JobState {
case class Done(projectVersion: String) extends JobState
case class Todo(maybeBackfill: Option[Backfill]) extends JobState
case class Running(executionId: String) extends JobState
import TimeSeriesUtils._
implicit val doneEncoder: Encoder[Done] = new Encoder[Done] {
def apply(done: Done) =
Json.obj(
"v" -> done.projectVersion.asJson
)
}
implicit val doneDecoder: Decoder[Done] = new Decoder[Done] {
def apply(c: HCursor): Decoder.Result[Done] =
for {
version <- c
.downField("v")
.as[String]
.orElse(c.downField("projectVersion").as[String])
.orElse(Right("no-version"))
} yield Done(version)
}
implicit val todoEncoder: Encoder[Todo] = new Encoder[Todo] {
def apply(todo: Todo) =
todo.maybeBackfill
.map { backfill =>
Json.obj("backfill" -> todo.maybeBackfill.map(_.id).asJson)
}
.getOrElse(Json.obj())
}
implicit def todoDecoder(implicit jobs: Set[TimeSeriesJob], backfills: List[Backfill]): Decoder[Todo] =
new Decoder[Todo] {
def apply(c: HCursor): Decoder.Result[Todo] =
for {
maybeBackfill <- c
.downField("backfill")
.as[String]
.right
.map { id =>
backfills.find(_.id == id)
}
// Legacy format
.orElse(c.downField("maybeBackfill").as[Option[Backfill]])
.orElse(Right(None))
} yield Todo(maybeBackfill)
}
implicit val encoder: Encoder[JobState] = deriveEncoder
implicit def decoder(implicit jobs: Set[TimeSeriesJob], backfills: List[Backfill]): Decoder[JobState] = deriveDecoder
implicit val eqInstance: Eq[JobState] = Eq.fromUniversalEquals[JobState]
}
/** A [[TimeSeriesScheduler]] executes the [[com.criteo.cuttle.Workflow Workflow]] for the
* time partitions defined in a calendar. Each [[com.criteo.cuttle.Job Job]] defines how it mnaps
* to the calendar (for example Hourly or Daily UTC), and the [[com.criteo.cuttle.Scheduler Scheduler]]
* ensure that at least one [[com.criteo.cuttle.Execution Execution]] is created and successfully run
* for each defined Job/Period.
*
* The scheduler also allow to [[Backfill]] already computed partitions. The [[Backfill]] can be recursive
* or not and an audit log of backfills is kept.
*/
case class TimeSeriesScheduler(logger: Logger,
stateRetention: Option[ScalaDuration] = None,
maxVersionsHistory: Option[Int] = None)
extends Scheduler[TimeSeries] {
import JobState.{Done, Running, Todo}
import TimeSeriesUtils._
override val name = "timeseries"
override val allContexts = Database.sqlGetContextsBetween(None, None)
private val _state = Ref(Map.empty[TimeSeriesJob, IntervalMap[Instant, JobState]])
private val _backfills = Ref(Set.empty[Backfill])
private val _pausedJobs = Ref(Set.empty[PausedJob])
private val debouncedJobs = collection.mutable.Map.empty[TimeSeriesJob, Instant]
def pausedJobs(): Set[PausedJob] = atomic { implicit txn =>
_pausedJobs()
}
private val queries = Queries(logger)
private[timeseries] def state: (State, Set[Backfill]) = atomic { implicit txn =>
(_state(), _backfills())
}
private[timeseries] case class BackfillError(job: TimeSeriesJob, msg: String) {
override def toString: String =
s"""
|Error during backfill creation.
|Job id: ${job.id}
|Job name: ${job.name}
|Error: $msg
""".stripMargin
}
private def assert(condition: Boolean, msg: String)(implicit job: TimeSeriesJob) =
(if (condition) Right(true) else Left(BackfillError(job, msg))).right
/**
* @return the list of errors for the input backfill configuration, if any
*/
private[timeseries] def validateBackfill(
backfill: Backfill,
states: Map[TimeSeriesUtils.TimeSeriesJob, IntervalMap[Instant, JobState]]
): Set[BackfillError] = {
val start = backfill.start
val end = backfill.end
val validationByJob = backfill.jobs.map { implicit job =>
for {
_ <- assert(start.isBefore(end), s"The start date[$start] should be superior that end date[$end].")
validIn <- {
val interval = Interval(start, end)
// collect all periods that are intersecting with [start, end]
val interval2State = states.apply(job).intersect(interval).toList
// in a Done state, and correct periodicity
Right(
interval2State
.collect {
case (Interval(Finite(lo), Finite(hi)), Done(_)) => (lo, hi)
}
.sortBy(_._1)
).right
}
_ <- assert(validIn.nonEmpty, s"There isn't any successful execution between start[$start] and end[$end].")
_ <- assert(
validIn.head._1 == start,
s"The start date[${validIn.head._1}] of first successful execution doesn't equal to backfill start date" +
s"[$start]."
)
_ <- assert(
validIn.last._2 == end,
s"The end date[${validIn.last._2}] of last successful execution doesn't equal to backfill end date" +
s"[$end]."
)
valid <- assert(validIn.zip(validIn.tail).forall { case (prev, next) => prev._2 == next._1 },
"There are some unsuccessful intervals.")
} yield valid
}
validationByJob.flatMap(_ match {
case Left(error) => Some(error)
case Right(_) => None
})
}
private def updateBackfillState(backfill: Backfill): Map[TimeSeriesJob, IntervalMap[Instant, JobState]] = atomic {
implicit txn =>
_backfills() = _backfills() + backfill
_state() = _state() ++ backfill.jobs.map(job => {
val newStart = job.scheduling.calendar.truncate(backfill.start)
val newEnd = job.scheduling.calendar.ceil(backfill.end)
job -> _state().apply(job).update(Interval(newStart, newEnd), Todo(Some(backfill)))
})
_state()
}
/**
* Launch backfills on a set of jobs on each subperiod that can be backfilled.
* A job has to succeed at least once on a period to be backfillable on that same period.
* The current executions of jobs with periods overlapping the backfills are cancelled.
*
* @returns a side-effect performing the requested backfill on success, an error otherwise
*
* @example Assume a backfill is requested over the period t1, t5 for the jobs = {job1, job2}
* For illustrative purposes, let the past execution state be as follows:
* <table>
* <tr>
* <th>Time</th> <th>t1</th> <th>t2</th> <th>t3</th> <th>t4</th> <th>t5</th>
* </tr>
* <tr>
* <td>job1</td> <td>OK</td> <td>OK</td> <td>KO</td> <td>OK</td> <td>OK</td>
* </tr>
* <tr>
* <td>job2</td> <td>OK</td> <td>OK</td> <td>KO</td> <td>KO</td> <td>OK</td>
* </tr>
* </table>
* Then the backfill will be lauched on {t1, t2} for jobs {job1, job2}, on {t4} for {job1} and on {t5} for {job1, job2}
*/
private[timeseries] def backfillJob(name: String,
description: String,
jobs: Set[TimeSeriesJob],
start: Instant,
end: Instant,
priority: Int,
runningExecutions: TMap[Execution[TimeSeries], Future[Completed]],
xa: XA)(implicit user: Auth.User): IO[Either[String, Unit]] = {
logger.debug(s"Requesting a backfill of ${jobs.map(_.id)} between $start and $end")
val result = atomic { implicit txn =>
val currentJobStates = _state()
val backfillErrors = mutable.ArrayBuffer.empty[String]
val backfills = createBackfills(name, description, jobs, currentJobStates, start, end, priority)
val validBackfills: List[Backfill] = backfills.flatMap { newBackfill =>
validateBackfill(newBackfill, currentJobStates).toList match {
case Nil => Some(newBackfill)
case (validationErrors) =>
backfillErrors += validationErrors.mkString("\n")
None
}
}
if (backfillErrors.isEmpty) Right(validBackfills)
else Left(backfillErrors.toList)
}
result match {
case Left(errors) => IO.pure(Left(errors.mkString("\n")))
case Right(backfills) =>
val dbUpdate: IO[Either[String, Unit]] = backfills
.map { newBackfill =>
val updatedState = updateBackfillState(newBackfill)
runOrLogAndDie(
(Database.createBackfill(newBackfill) >>
Database.serializeState(updatedState, stateRetention)).transact(xa),
"TimeseriesScheduler, cannot serialize state, shutting down"
)
}
.sequence
.map(_ => Right(Unit))
dbUpdate
}
}
private[timeseries] def createBackfills(name: String,
description: String,
jobs: Set[TimeSeriesJob],
states: Map[TimeSeriesUtils.TimeSeriesJob, IntervalMap[Instant, JobState]],
start: Instant,
end: Instant,
priority: Int)(implicit user: Auth.User): List[Backfill] = {
type TimeInterval = (Instant, Instant)
val queryInterval = Interval(start, end)
// Identify the jobs to backfill for each elementary period spanning the query interval
// Find jobs which can be backfilled on the requested period. Those are the jobs whose state is 'Done'.
val candidateBackfillsByPeriod: List[(TimeInterval, TimeSeriesJob)] = jobs.flatMap { job =>
// collect all periods that are intersecting with [start, end]
// in a Done state, and correct periodicity
states(job)
.intersect(queryInterval)
.mapFilter {
case Done(_) =>
Some(job)
case _ =>
None
}
.toList
.map {
case (Interval(Finite(lo), Finite(hi)), job) =>
((lo, hi), job)
}
}.toList
val candidateBackfillsGroupedByPeriod: Map[TimeInterval, Set[TimeSeriesJob]] = candidateBackfillsByPeriod
.groupBy { case (interval, job) => interval }
.map { case (interval, groups) => interval -> groups.map { case (interval, job) => job }.toSet }
val orderingByTimestamp = Ordering.by { e: Instant =>
e.toEpochMilli
}
// Build an interval tree with the jobs to backfill for faster intersection queries
var jobsByElementaryPeriod = RangedSeq.empty[(TimeInterval, Set[TimeSeriesJob]), Instant](_._1, orderingByTimestamp)
candidateBackfillsGroupedByPeriod.foreach {
case (interval, jobs) =>
jobsByElementaryPeriod = jobsByElementaryPeriod + (interval -> jobs)
}
val elementaryPeriods: List[(Instant, Instant)] = candidateBackfillsByPeriod
.flatMap { case (interval, _) => List(interval._1, interval._2) }
.distinct
.sorted
.iterator
.sliding(2)
.withPartial(false)
.toList
.map { case List(start, end) => (start, end) }
val jobsToBackFillByPeriod: List[(TimeInterval, Set[TimeSeriesJob])] = elementaryPeriods
.map {
case (start, end) =>
val jobsOnPeriod = jobsByElementaryPeriod
.filterOverlaps(start -> end)
.map(_._2)
.foldLeft(Set.empty[TimeSeriesJob])(_ ++ _)
(start, end) -> jobsOnPeriod
}
.filter { case (interval, jobs) => jobs.nonEmpty }
val backfills: List[Backfill] = jobsToBackFillByPeriod.map {
case ((backfillStart, backfillEnd), jobsToBackfill) =>
Backfill(
UUID.randomUUID().toString,
backfillStart,
backfillEnd,
jobsToBackfill,
priority,
name,
description,
"RUNNING",
user.userId
)
}
backfills
}
private[timeseries] def pauseJobs(jobs: Set[Job[TimeSeries]], executor: Executor[TimeSeries], xa: XA)(
implicit user: Auth.User
): Unit = {
val executionsToCancel = atomic { implicit tx =>
val pauseDate = Instant.now()
val pausedJobIds = _pausedJobs().map(_.id)
val jobsToPause: Set[PausedJob] = jobs
.filter(job => !pausedJobIds.contains(job.id))
.map(job => PausedJob(job.id, user, pauseDate))
if (jobsToPause.isEmpty) return
_pausedJobs() = _pausedJobs() ++ jobsToPause
val pauseQuery = jobsToPause.map(queries.pauseJob).reduceLeft(_ *> _)
Txn.setExternalDecider(new ExternalDecider {
def shouldCommit(implicit txn: InTxnEnd): Boolean = {
pauseQuery.transact(xa).unsafeRunSync
true
}
})
jobsToPause.flatMap { pausedJob =>
executor.runningState.filterKeys(_.job.id == pausedJob.id).keys ++ executor.throttledState
.filterKeys(_.job.id == pausedJob.id)
.keys
}
}
logger.debug(s"we will cancel ${executionsToCancel.size} executions")
executionsToCancel.toList.sortBy(_.context).reverse.foreach { execution =>
execution.streams.debug(s"Job has been paused by user ${user.userId}")
execution.cancel()
}
}
private[timeseries] def resumeJobs(jobs: Set[Job[TimeSeries]], xa: XA)(implicit user: Auth.User): Unit = {
val jobIdsToResume = jobs.map(_.id)
val resumeQuery = jobIdsToResume.map(queries.resumeJob).reduceLeft(_ *> _)
atomic { implicit tx =>
Txn.setExternalDecider(new ExternalDecider {
def shouldCommit(implicit tx: InTxnEnd): Boolean = {
resumeQuery.transact(xa).unsafeRunSync
true
}
})
_pausedJobs() = _pausedJobs() -- _pausedJobs().filter(pausedJob => jobIdsToResume.contains(pausedJob.id))
}
}
private[timeseries] def compressState(state: Map[TimeSeriesJob, IntervalMap[Instant, JobState]], maxVersions: Int) =
state.mapValues {
case imap =>
// Search for all known versions of this jobs
val allKnownVersions =
imap.toList.collect {
case (_, JobState.Done(version)) =>
version
}
val latestVersions = allKnownVersions.takeRight(maxVersions).toSet
imap.map {
case JobState.Done(version) if !latestVersions.contains(version) =>
JobState.Done("old")
case same =>
same
}
}
/**
* Given a list of current executions, update their state and submit new executions depending on the current time and
* changes in execution states.
* @param running set of still running executions
* @return new set of running executions
**/
private[timeseries] def updateCurrentExecutionsAndSubmitNewExecutions(running: Set[Run],
workflow: Workflow,
executor: Executor[TimeSeries],
xa: XA): Set[Run] = {
val (completed, stillRunning) = running.partition {
case (_, _, effect) => effect.isCompleted
}
val (stateSnapshot, completedBackfills, toRun, debounced, commitInstant) = atomic { implicit txn =>
val now = Instant.now
val (stateSnapshot, newBackfills, completedBackfills) =
collectCompletedJobs(_state(), _backfills(), completed)
val (toRun, debounced) = jobsToRun(workflow, stateSnapshot, now, executor.projectVersion)
_state() = stateSnapshot
_backfills() = newBackfills
(stateSnapshot, completedBackfills, toRun, debounced, now)
}
toRun.groupBy(_._1).foreach {
case (job, _) =>
if (job.scheduling.batching.size > 1) {
logger.debug(s"scheduling batches for ${job.name}")
debouncedJobs -= job
}
}
debounced.groupBy(_._1).foreach {
case (job, _) =>
debouncedJobs.get(job) match {
case Some(interval) =>
logger.debug(s"job ${job.name} is waiting for intervals to batch until $interval")
case None =>
val debouncePeriodEnd = commitInstant.plus(job.scheduling.batching.delay)
debouncedJobs(job) = debouncePeriodEnd
logger.debug(s"job ${job.name} will wait for intervals to batch until $debouncePeriodEnd")
}
}
val newExecutions = executor.runAll(toRun)
atomic { implicit txn =>
_state() = newExecutions.foldLeft(_state()) {
case (st, (execution, _)) =>
st + (execution.job ->
st(execution.job).update(execution.context.toInterval, Running(execution.id)))
}
}
if (completed.nonEmpty || toRun.nonEmpty) {
maxVersionsHistory.foreach { maxVersions =>
atomic { implicit txn =>
_state() = compressState(_state(), maxVersions)
}
}
runOrLogAndDie(Database.serializeState(stateSnapshot, stateRetention).transact(xa),
"TimeseriesScheduler, cannot serialize state, shutting down").unsafeRunSync()
}
if (completedBackfills.nonEmpty)
runOrLogAndDie(
Database
.setBackfillStatus(completedBackfills.map(_.id), "COMPLETE")
.transact(xa),
"TimeseriesScheduler, cannot serialize state, shutting down"
).unsafeRunSync()
val newRunning = stillRunning ++ newExecutions.map {
case (execution, result) =>
(execution.job, execution.context, result)
}
newRunning
}
private[timeseries] def initialize(workflow0: Workload[TimeSeries], xa: XA, logger: Logger) = {
val workflow = workflow0.asInstanceOf[Workflow]
logger.info("Validate workflow before start")
TimeSeriesUtils.validate(workflow) match {
case Left(errors) =>
val consolidatedError = errors.mkString("\n")
logger.error(consolidatedError)
throw new IllegalArgumentException(consolidatedError)
case Right(_) => ()
}
logger.info("Workflow is valid")
logger.info("Applying migrations to database")
Database.doSchemaUpdates.transact(xa).unsafeRunSync
logger.info("Database up-to-date")
val incompleteBackfills = Database
.queryBackfills(Some(sql"""status = 'RUNNING'"""))
.to[List]
.map(_.map {
case (id, name, description, jobsIdsString, priority, start, end, _, status, createdBy) =>
val jobsIds = jobsIdsString.split(",")
val jobs = workflow.vertices.filter { job =>
jobsIds.contains(job.id)
}
Backfill(id, start, end, jobs, priority, name, description, status, createdBy)
})
.transact(xa)
.unsafeRunSync
Database
.deserializeState(workflow.vertices, incompleteBackfills)
.transact(xa)
.unsafeRunSync
.foreach { state =>
atomic { implicit txn =>
_state() = cleanTimeseriesState(state)
}
}
atomic { implicit txn =>
_backfills() = _backfills() ++ incompleteBackfills
_pausedJobs() = _pausedJobs() ++ queries.getPausedJobs.transact(xa).unsafeRunSync()
workflow.vertices.foreach { job =>
val calendar = job.scheduling.calendar
val definedInterval = Interval(Finite(calendar.ceil(job.scheduling.start)),
job.scheduling.end.map(calendar.truncate _).map(Finite.apply _).getOrElse(Top))
val oldJobState = _state().getOrElse(job, IntervalMap.empty[Instant, JobState])
val missingIntervals = IntervalMap(definedInterval -> (()))
.whenIsUndef(oldJobState.intersect(definedInterval))
.toList
.map(_._1)
val jobState = missingIntervals
.foldLeft(oldJobState) { (st, interval) =>
st.update(interval, Todo(None))
}
.intersect(definedInterval)
_state() = _state() + (job -> jobState)
}
}
workflow
}
def start(workflow0: Workload[TimeSeries], executor: Executor[TimeSeries], xa: XA, logger: Logger): Unit = {
val workflow = initialize(workflow0, xa, logger)
def mainLoop(running: Set[Run]): Unit = {
val newRunning = updateCurrentExecutionsAndSubmitNewExecutions(running, workflow, executor, xa)
utils.Timeout(ScalaDuration.create(1, "s")).andThen { case _ => mainLoop(newRunning) }
}
mainLoop(Set.empty)
}
private def runOrLogAndDie[K](thunk: IO[K], message: => String): IO[K] = {
import java.io._
IO.suspend(thunk)
.onError({
case e =>
IO {
logger.error(message)
val sw = new StringWriter
e.printStackTrace(new PrintWriter(sw))
logger.error(sw.toString)
System.exit(-1)
}
})
}
private[timeseries] def collectCompletedJobs(state: State,
backfills: Set[Backfill],
completed: Set[Run]): (State, Set[Backfill], Set[Backfill]) = {
def isDone(state: State, job: TimeSeriesJob, context: TimeSeriesContext): Boolean =
state.apply(job).intersect(context.toInterval).toList.forall {
case (_, Done(_)) => true
case _ => false
}
// update state with job statuses
val newState = completed.foldLeft(state) {
case (acc, (job, context, future)) =>
val jobState =
if (future.value.get.isSuccess || isDone(state, job, context)) Done(context.projectVersion)
else Todo(context.backfill)
acc + (job -> acc(job).update(context.toInterval, jobState))
}
def jobHasExecutionsRunningOnPeriod(job: Job[TimeSeries], period: Interval[Instant]): Boolean = {
val jobStateOnPeriod = newState(job).intersect(period).toList
jobStateOnPeriod.exists {
case (interval, jobState) =>
jobState match {
case Done(_) => false
case _ => true
}
}
}
val notCompletedBackfills = backfills.filter { bf =>
val itvl = Interval(bf.start, bf.end)
bf.jobs.exists(job => jobHasExecutionsRunningOnPeriod(job, itvl))
}
(newState, notCompletedBackfills, backfills -- notCompletedBackfills)
}
private[timeseries] def jobsToRun(workflow: Workflow,
state0: State,
now: Instant,
projectVersion: String): (List[Executable], List[Executable]) = {
val timerInterval = Interval(Bottom, Finite(now))
val state = state0.mapValues(_.intersect(timerInterval))
val parentsMap = workflow.edges.groupBy { case (child, _, _) => child }
val childrenMap = workflow.edges.groupBy { case (_, parent, _) => parent }
val pausedJobIds = pausedJobs().map(_.id)
def reverseDescr(dep: TimeSeriesDependency) =
TimeSeriesDependency(dep.offsetLow.negated, dep.offsetHigh.negated)
def applyDep(dep: TimeSeriesDependency): PartialFunction[Interval[Instant], Interval[Instant]] =
Function.unlift { (i: Interval[Instant]) =>
val low = i.lo.map(_.plus(dep.offsetLow))
val high = i.hi.map(_.plus(dep.offsetHigh))
if (low >= high) None else Some(Interval(low, high))
}
def joinIntervals(intervals: List[Interval[Instant]]): List[Interval[Instant]] =
intervals
.foldLeft(IntervalMap.empty[Instant, Unit]) { case (intervalMap, interval) => intervalMap.update(interval, ()) }
.toList
.map { case (interval, _) => interval }
val job2Contexts = workflow.vertices.filter(job => !pausedJobIds.contains(job.id)).toList.flatMap { job =>
val full = IntervalMap[Instant, Unit](Interval[Instant](Bottom, Top) -> (()))
val dependenciesSatisfied = parentsMap
.getOrElse(job, Set.empty)
.map {
case (_, parent, lbl) =>
val donePeriods: IntervalMap[Instant, Unit] = state(parent).collect { case Done(_) => () }
val intervals: List[Interval[Instant]] =
joinIntervals(donePeriods.toList.map { case (interval, _) => interval })
val newIntervals = intervals.collect(applyDep(reverseDescr(lbl)))
val intervalMapOfSatisfiedDeps = newIntervals.foldLeft(IntervalMap.empty[Instant, Unit])(_.update(_, ()))
intervalMapOfSatisfiedDeps
}
.fold(full)(_ whenIsDef _)
val noChildrenRunning = childrenMap
.getOrElse(job, Set.empty)
.map {
case (child, _, lbl) =>
val runningPeriods: IntervalMap[Instant, Unit] = state(child).collect { case Running(_) => () }
val intervals = joinIntervals(runningPeriods.toList.map { case (interval, _) => interval })
val newIntervals = intervals.collect(applyDep(lbl))
val intervalMapWithCompletedChildren =
newIntervals.foldLeft(IntervalMap.empty[Instant, Unit])(_.update(_, ()))
intervalMapWithCompletedChildren
}