-
Notifications
You must be signed in to change notification settings - Fork 51
/
change.go
896 lines (797 loc) · 24.4 KB
/
change.go
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
// Copyright (c) 2024 Canonical Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package state
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strings"
"time"
"github.com/canonical/pebble/internals/logger"
)
// Status is used for status values for changes and tasks.
type Status int
// Admitted status values for changes and tasks.
const (
// DefaultStatus is the standard computed status for a change or task.
// For tasks it's always mapped to DoStatus, and for change its mapped
// to an aggregation of its tasks' statuses. See Change.Status for details.
DefaultStatus Status = 0
// HoldStatus means the task should not run for the moment, perhaps as a
// consequence of an error on another task.
HoldStatus Status = 1
// DoStatus means the change or task is ready to start.
DoStatus Status = 2
// DoingStatus means the change or task is running or an attempt was made to run it.
DoingStatus Status = 3
// DoneStatus means the change or task was accomplished successfully.
DoneStatus Status = 4
// AbortStatus means the task should stop doing its activities and then undo.
AbortStatus Status = 5
// UndoStatus means the change or task should be undone, probably due to an error elsewhere.
UndoStatus Status = 6
// UndoingStatus means the change or task is being undone or an attempt was made to undo it.
UndoingStatus Status = 7
// UndoneStatus means a task was first done and then undone after an error elsewhere.
// Changes go directly into the error status instead of being marked as undone.
UndoneStatus Status = 8
// ErrorStatus means the change or task has errored out while running or being undone.
ErrorStatus Status = 9
// WaitStatus means the task was accomplished successfully but some
// external event needs to happen before work can progress further.
WaitStatus Status = 10
nStatuses = iota
)
// Ready returns whether a task or change with this status needs further
// work or has completed its attempt to perform the current goal.
func (s Status) Ready() bool {
switch s {
case DoneStatus, UndoneStatus, HoldStatus, ErrorStatus:
return true
}
return false
}
func (s Status) String() string {
switch s {
case DefaultStatus:
return "Default"
case DoStatus:
return "Do"
case DoingStatus:
return "Doing"
case DoneStatus:
return "Done"
case WaitStatus:
return "Wait"
case AbortStatus:
return "Abort"
case UndoStatus:
return "Undo"
case UndoingStatus:
return "Undoing"
case UndoneStatus:
return "Undone"
case HoldStatus:
return "Hold"
case ErrorStatus:
return "Error"
}
panic(fmt.Sprintf("internal error: unknown task status code: %d", s))
}
// taskWaitComputeStatus is used while computing the wait status of a
// change. It keeps track of whether a task is waiting or not waiting, or the
// computation for it is still in-progress to detect cyclic dependencies.
type taskWaitComputeStatus int
const (
taskWaitStatusNotComputed taskWaitComputeStatus = iota
taskWaitStatusComputing
taskWaitStatusNotWaiting
taskWaitStatusWaiting
)
// Change represents a tracked modification to the system state.
//
// The Change provides both the justification for individual tasks
// to be performed and the grouping of them.
//
// As an example, if an administrator requests an interface connection,
// multiple hooks might be individually run to accomplish the task. The
// Change summary would reflect the request for an interface connection,
// while the individual Task values would track the running of
// the hooks themselves.
type Change struct {
state *State
id string
kind string
summary string
status Status
clean bool
data customData
taskIDs []string
ready chan struct{}
lastObservedStatus Status
lastRecordedNoticeStatus Status
spawnTime time.Time
readyTime time.Time
}
type byReadyTime []*Change
func (a byReadyTime) Len() int { return len(a) }
func (a byReadyTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byReadyTime) Less(i, j int) bool { return a[i].readyTime.Before(a[j].readyTime) }
func newChange(state *State, id, kind, summary string) *Change {
return &Change{
state: state,
id: id,
kind: kind,
summary: summary,
data: make(customData),
ready: make(chan struct{}),
spawnTime: timeNow(),
}
}
type marshalledChange struct {
ID string `json:"id"`
Kind string `json:"kind"`
Summary string `json:"summary"`
Status Status `json:"status"`
Clean bool `json:"clean,omitempty"`
Data map[string]*json.RawMessage `json:"data,omitempty"`
TaskIDs []string `json:"task-ids,omitempty"`
SpawnTime time.Time `json:"spawn-time"`
ReadyTime *time.Time `json:"ready-time,omitempty"`
LastRecordedNoticeStatus Status `json:"last-recorded-notice-status,omitempty"`
}
// MarshalJSON makes Change a json.Marshaller
func (c *Change) MarshalJSON() ([]byte, error) {
c.state.reading()
var readyTime *time.Time
if !c.readyTime.IsZero() {
readyTime = &c.readyTime
}
return json.Marshal(marshalledChange{
ID: c.id,
Kind: c.kind,
Summary: c.summary,
Status: c.status,
Clean: c.clean,
Data: c.data,
TaskIDs: c.taskIDs,
SpawnTime: c.spawnTime,
ReadyTime: readyTime,
LastRecordedNoticeStatus: c.lastRecordedNoticeStatus,
})
}
// UnmarshalJSON makes Change a json.Unmarshaller
func (c *Change) UnmarshalJSON(data []byte) error {
if c.state != nil {
c.state.writing()
}
var unmarshalled marshalledChange
err := json.Unmarshal(data, &unmarshalled)
if err != nil {
return err
}
c.id = unmarshalled.ID
c.kind = unmarshalled.Kind
c.summary = unmarshalled.Summary
c.status = unmarshalled.Status
c.clean = unmarshalled.Clean
custData := unmarshalled.Data
if custData == nil {
custData = make(customData)
}
c.data = custData
c.taskIDs = unmarshalled.TaskIDs
c.ready = make(chan struct{})
c.spawnTime = unmarshalled.SpawnTime
if unmarshalled.ReadyTime != nil {
c.readyTime = *unmarshalled.ReadyTime
}
c.lastRecordedNoticeStatus = unmarshalled.LastRecordedNoticeStatus
return nil
}
// finishUnmarshal is called after the state and tasks are accessible.
func (c *Change) finishUnmarshal() {
if c.Status().Ready() {
close(c.ready)
}
}
// ID returns the individual random key for the change.
func (c *Change) ID() string {
return c.id
}
// Kind returns the nature of the change for managers to know how to handle it.
func (c *Change) Kind() string {
return c.kind
}
// Summary returns a summary describing what the change is about.
func (c *Change) Summary() string {
return c.summary
}
// Set associates value with key for future consulting by managers.
// The provided value must properly marshal and unmarshal with encoding/json.
func (c *Change) Set(key string, value interface{}) {
c.state.writing()
c.data.set(key, value)
}
// Get unmarshals the stored value associated with the provided key
// into the value parameter.
func (c *Change) Get(key string, value interface{}) error {
c.state.reading()
return c.data.get(key, value)
}
// Has returns whether the provided key has an associated value.
func (c *Change) Has(key string) bool {
c.state.reading()
return c.data.has(key)
}
var statusOrder = []Status{
AbortStatus,
UndoingStatus,
UndoStatus,
DoingStatus,
DoStatus,
WaitStatus,
ErrorStatus,
UndoneStatus,
DoneStatus,
HoldStatus,
}
func init() {
if len(statusOrder) != nStatuses-1 {
panic("statusOrder has wrong number of elements")
}
}
func (c *Change) isTaskWaiting(visited map[string]taskWaitComputeStatus, t *Task, deps []*Task) bool {
taskID := t.ID()
// Retrieve the compute status of the wait for the task, if not
// computed this defaults to 0 (taskWaitStatusNotComputed).
computeStatus := visited[taskID]
switch computeStatus {
case taskWaitStatusComputing:
// Cyclic dependency detected, return false to short-circuit.
logger.Noticef("detected cyclic dependencies for task %q in change %q", t.Kind(), t.Change().Kind())
// Make sure errors show up in "pebble change <id>" too
t.Logf("detected cyclic dependencies for task %q in change %q", t.Kind(), t.Change().Kind())
return false
case taskWaitStatusWaiting, taskWaitStatusNotWaiting:
return computeStatus == taskWaitStatusWaiting
}
visited[taskID] = taskWaitStatusComputing
var isWaiting bool
depscheck:
for _, wt := range deps {
switch wt.Status() {
case WaitStatus:
isWaiting = true
// States that can be valid when waiting
// - Done, Undone, ErrorStatus, HoldStatus
case DoneStatus, UndoneStatus, ErrorStatus, HoldStatus:
continue
// For 'Do' and 'Undo' we have to check whether the task is waiting
// for any dependencies. The logic is the same, but the set of tasks
// varies.
case DoStatus:
isWaiting = c.isTaskWaiting(visited, wt, wt.WaitTasks())
if !isWaiting {
// Cancel early if we detect something is runnable.
break depscheck
}
case UndoStatus:
isWaiting = c.isTaskWaiting(visited, wt, wt.HaltTasks())
if !isWaiting {
// Cancel early if we detect something is runnable.
break depscheck
}
default:
// When we determine the change can not be in a wait-state then
// break early.
isWaiting = false
break depscheck
}
}
if isWaiting {
visited[taskID] = taskWaitStatusWaiting
} else {
visited[taskID] = taskWaitStatusNotWaiting
}
return isWaiting
}
// isChangeWaiting should only ever return true iff it determines all tasks in Do/Undo
// are blocked by tasks in either of three states: 'DoneStatus', 'UndoneStatus' or 'WaitStatus',
// if this fails, we default to the normal status ordering logic.
func (c *Change) isChangeWaiting() bool {
// Since we might visit tasks more than once, we store results to avoid recomputing them.
visited := make(map[string]taskWaitComputeStatus)
for _, t := range c.Tasks() {
switch t.Status() {
case WaitStatus, DoneStatus, UndoneStatus, ErrorStatus, HoldStatus:
continue
case DoStatus:
if !c.isTaskWaiting(visited, t, t.WaitTasks()) {
return false
}
case UndoStatus:
if !c.isTaskWaiting(visited, t, t.HaltTasks()) {
return false
}
default:
return false
}
}
// If we end up here, then return true as we know we
// have at least one waiter in this change.
return true
}
// Status returns the current status of the change.
// If the status was not explicitly set the result is derived from the status
// of the individual tasks related to the change, according to the following
// decision sequence:
//
// - With all pending tasks blocked by other tasks in WaitStatus, return WaitStatus
// - With at least one task in DoStatus, return DoStatus
// - With at least one task in ErrorStatus, return ErrorStatus
// - Otherwise, return DoneStatus
func (c *Change) Status() Status {
c.state.reading()
if c.status != DefaultStatus {
return c.status
}
if len(c.taskIDs) == 0 {
return HoldStatus
}
statusStats := make([]int, nStatuses)
for _, tid := range c.taskIDs {
statusStats[c.state.tasks[tid].Status()]++
}
// If the change has any waiters, check for any runnable tasks
// or whether it's completely blocked by waiters.
if statusStats[WaitStatus] > 0 {
// Only if the change has all tasks blocked we return WaitStatus.
if c.isChangeWaiting() {
return WaitStatus
}
}
// Otherwise we return the current status with the highest priority.
for _, s := range statusOrder {
if statusStats[s] > 0 {
return s
}
}
panic(fmt.Sprintf("internal error: cannot process change status: %v", statusStats))
}
// addNotice records an occurrence of a change-update notice for this change.
// The notice key is set to the change ID.
func (c *Change) addNotice() error {
opts := &AddNoticeOptions{
Data: map[string]string{"kind": c.Kind()},
}
_, err := c.state.AddNotice(nil, ChangeUpdateNotice, c.id, opts)
return err
}
func shouldSkipChangeUpdateNotice(old, new Status) bool {
// Skip alternating Doing->Do->Doing and Undoing->Undo->Undoing notices
return (old == new) || (old == DoingStatus && new == DoStatus) || (old == UndoingStatus && new == UndoStatus)
}
func (c *Change) notifyStatusChange(new Status) {
if c.lastObservedStatus != new {
c.state.notifyChangeStatusChangedHandlers(c, c.lastObservedStatus, new)
c.lastObservedStatus = new
}
if !shouldSkipChangeUpdateNotice(c.lastRecordedNoticeStatus, new) {
// NOTE: Implies State.writing()
if err := c.addNotice(); err != nil {
logger.Panicf(`internal error: failed to add "change-update" notice on status change: %v`, err)
}
c.lastRecordedNoticeStatus = new
}
}
// SetStatus sets the change status, overriding the default behavior (see Status method).
func (c *Change) SetStatus(s Status) {
c.state.writing()
c.status = s
if s.Ready() {
c.markReady()
}
c.notifyStatusChange(c.Status())
}
func (c *Change) markReady() {
select {
case <-c.ready:
default:
close(c.ready)
}
if c.readyTime.IsZero() {
c.readyTime = timeNow()
}
}
// Ready returns a channel that is closed the first time the change becomes ready.
func (c *Change) Ready() <-chan struct{} {
return c.ready
}
func (c *Change) detectChangeReady(excludeTask *Task) {
for _, tid := range c.taskIDs {
task := c.state.tasks[tid]
if task != excludeTask && !task.status.Ready() {
return
}
}
// Here is the exact moment when a change goes from unready to ready,
// and from ready to unready. For now handle only the first of those.
// For the latter the channel might be replaced in the future.
if c.IsReady() && !c.Status().Ready() {
panic(fmt.Errorf("change %s unexpectedly became unready (%s)", c.ID(), c.Status()))
}
c.markReady()
}
// taskStatusChanged is called by tasks when their status is changed,
// to give the opportunity for the change to close its ready channel, and
// notify observers of Change changes.
func (c *Change) taskStatusChanged(t *Task, old, new Status) {
cs := c.Status()
// If the task changes from ready => unready or unready => ready,
// update the ready status for the change.
if old.Ready() == new.Ready() {
c.notifyStatusChange(cs)
return
}
c.detectChangeReady(t)
c.notifyStatusChange(cs)
}
// IsClean returns whether all tasks in the change have been cleaned. See SetClean.
func (c *Change) IsClean() bool {
c.state.reading()
return c.clean
}
// IsReady returns whether the change is considered ready.
//
// The result is similar to calling Ready on the status returned by the Status
// method, but this function is more efficient as it doesn't need to recompute
// the aggregated state of tasks on every call.
//
// As an exception, IsReady returns false for a Change without any tasks that
// never had its status explicitly set and was never unmarshalled out of the
// persistent state, despite its initial status being Hold. This is how the
// system represents changes right after they are created.
func (c *Change) IsReady() bool {
select {
case <-c.ready:
return true
default:
}
return false
}
func (c *Change) taskCleanChanged() {
if !c.IsReady() {
panic("internal error: attempted to set a task clean while change not ready")
}
for _, tid := range c.taskIDs {
task := c.state.tasks[tid]
if !task.clean {
return
}
}
c.clean = true
}
// SpawnTime returns the time when the change was created.
func (c *Change) SpawnTime() time.Time {
c.state.reading()
return c.spawnTime
}
// ReadyTime returns the time when the change became ready.
func (c *Change) ReadyTime() time.Time {
c.state.reading()
return c.readyTime
}
// changeError holds a set of task errors.
type changeError struct {
errors []taskError
}
type taskError struct {
task string
error string
}
func (e *changeError) Error() string {
var buf bytes.Buffer
buf.WriteString("cannot perform the following tasks:\n")
for _, te := range e.errors {
fmt.Fprintf(&buf, "- %s (%s)\n", te.task, te.error)
}
return strings.TrimSuffix(buf.String(), "\n")
}
func stripErrorMsg(msg string) (string, bool) {
i := strings.Index(msg, " ")
if i >= 0 && strings.HasPrefix(msg[i:], " ERROR ") {
return msg[i+len(" ERROR "):], true
}
return "", false
}
// Err returns an error value based on errors that were logged for tasks registered
// in this change, or nil if the change is not in ErrorStatus.
func (c *Change) Err() error {
c.state.reading()
if c.Status() != ErrorStatus {
return nil
}
var errors []taskError
for _, tid := range c.taskIDs {
task := c.state.tasks[tid]
if task.Status() != ErrorStatus {
continue
}
for _, msg := range task.Log() {
if s, ok := stripErrorMsg(msg); ok {
errors = append(errors, taskError{task.Summary(), s})
}
}
}
if len(errors) == 0 {
return fmt.Errorf("internal inconsistency: change %q in ErrorStatus with no task errors logged", c.Kind())
}
return &changeError{errors}
}
// State returns the system State
func (c *Change) State() *State {
return c.state
}
// AddTask registers a task as required for the state change to
// be accomplished.
func (c *Change) AddTask(t *Task) {
c.state.writing()
if t.change != "" {
panic(fmt.Sprintf("internal error: cannot add one %q task to multiple changes", t.Kind()))
}
t.change = c.id
c.taskIDs = addOnce(c.taskIDs, t.ID())
}
// AddAll registers all tasks in the set as required for the state
// change to be accomplished.
func (c *Change) AddAll(ts *TaskSet) {
c.state.writing()
for _, t := range ts.tasks {
c.AddTask(t)
}
}
// Tasks returns all the tasks this state change depends on.
func (c *Change) Tasks() []*Task {
c.state.reading()
return c.state.tasksIn(c.taskIDs)
}
// LaneTasks returns all tasks from given lanes the state change depends on.
func (c *Change) LaneTasks(lanes ...int) []*Task {
laneLookup := make(map[int]bool)
for _, l := range lanes {
laneLookup[l] = true
}
c.state.reading()
var tasks []*Task
for _, tid := range c.taskIDs {
t := c.state.tasks[tid]
if len(t.lanes) == 0 && laneLookup[0] {
tasks = append(tasks, t)
}
for _, l := range t.lanes {
if laneLookup[l] {
tasks = append(tasks, t)
break
}
}
}
return tasks
}
// Abort flags the change for cancellation, whether in progress or not.
// Cancellation will proceed at the next ensure pass.
func (c *Change) Abort() {
c.state.writing()
tasks := make([]*Task, len(c.taskIDs))
for i, tid := range c.taskIDs {
tasks[i] = c.state.tasks[tid]
}
c.abortTasks(tasks, make(map[int]bool), make(map[string]bool))
}
// AbortLanes aborts all tasks in the provided lanes and any tasks waiting on them,
// except for tasks that are also in a healthy lane (not aborted, and not waiting
// on aborted).
func (c *Change) AbortLanes(lanes []int) {
c.state.writing()
c.abortLanes(lanes, make(map[int]bool), make(map[string]bool))
}
// AbortUnreadyLanes aborts the tasks from lanes that aren't fully ready, where
// a ready lane is one in which all tasks are ready.
func (c *Change) AbortUnreadyLanes() {
c.state.writing()
c.abortUnreadyLanes()
}
func (c *Change) abortUnreadyLanes() {
lanesWithLiveTasks := map[int]bool{}
for _, tid := range c.taskIDs {
t := c.state.tasks[tid]
if !t.Status().Ready() {
for _, tlane := range t.Lanes() {
lanesWithLiveTasks[tlane] = true
}
}
}
abortLanes := []int{}
for lane := range lanesWithLiveTasks {
abortLanes = append(abortLanes, lane)
}
c.abortLanes(abortLanes, make(map[int]bool), make(map[string]bool))
}
// taskEffectiveStatus returns the 'effective' status. This means it accounts
// for tasks being in WaitStatus, and instead of returning the WaitStatus we
// return the actual status. (The status after the wait).
func taskEffectiveStatus(t *Task) Status {
status := t.Status()
if status == WaitStatus {
// If the task is waiting, then use the effective status instead.
status = t.WaitedStatus()
}
return status
}
func (c *Change) abortLanes(lanes []int, abortedLanes map[int]bool, seenTasks map[string]bool) {
var hasLive = make(map[int]bool)
var hasDead = make(map[int]bool)
var laneTasks []*Task
NextChangeTask:
for _, tid := range c.taskIDs {
t := c.state.tasks[tid]
var live bool
switch taskEffectiveStatus(t) {
case DoStatus, DoingStatus, DoneStatus:
live = true
}
for _, tlane := range t.Lanes() {
for _, lane := range lanes {
if tlane == lane {
laneTasks = append(laneTasks, t)
continue NextChangeTask
}
}
// Track opinion about lanes not in the kill list.
// If the lane ends up being entirely live, we'll
// preserve this task alive too.
if live {
hasLive[tlane] = true
} else {
hasDead[tlane] = true
}
}
}
abortTasks := make([]*Task, 0, len(laneTasks))
NextLaneTask:
for _, t := range laneTasks {
for _, tlane := range t.Lanes() {
if hasLive[tlane] && !hasDead[tlane] {
continue NextLaneTask
}
}
abortTasks = append(abortTasks, t)
}
for _, lane := range lanes {
abortedLanes[lane] = true
}
if len(abortTasks) > 0 {
c.abortTasks(abortTasks, abortedLanes, seenTasks)
}
}
func (c *Change) abortTasks(tasks []*Task, abortedLanes map[int]bool, seenTasks map[string]bool) {
var lanes []int
for i := 0; i < len(tasks); i++ {
t := tasks[i]
if seenTasks[t.id] {
continue
}
seenTasks[t.id] = true
switch taskEffectiveStatus(t) {
case DoStatus:
// Still pending so don't even start.
t.SetStatus(HoldStatus)
case DoingStatus:
// In progress so stop and undo it.
t.SetStatus(AbortStatus)
case DoneStatus:
// Already done so undo it.
t.SetStatus(UndoStatus)
}
for _, lane := range t.Lanes() {
if !abortedLanes[lane] {
lanes = append(lanes, t.Lanes()...)
}
}
for _, halted := range t.HaltTasks() {
if !seenTasks[halted.id] {
tasks = append(tasks, halted)
}
}
}
if len(lanes) > 0 {
c.abortLanes(lanes, abortedLanes, seenTasks)
}
}
type TaskDependencyCycleError struct {
IDs []string
msg string
}
func (e *TaskDependencyCycleError) Error() string { return e.msg }
func (e *TaskDependencyCycleError) Is(err error) bool {
_, ok := err.(*TaskDependencyCycleError)
return ok
}
// CheckTaskDependencies checks the tasks in the change for cyclic dependencies
// and returns an error in such case.
func (c *Change) CheckTaskDependencies() error {
tasks := c.Tasks()
// count how many tasks any given non-independent task waits for
predecessors := make(map[string]int, len(tasks))
taskByID := map[string]*Task{}
for _, t := range tasks {
taskByID[t.id] = t
if l := len(t.waitTasks); l > 0 {
// only add an entry if the task is not independent
predecessors[t.id] = l
}
}
// Kahn topological sort: make our way starting with tasks that are
// independent (their predecessors count is 0), then visit their direct
// successors (halt tasks), and for each reduce their predecessors
// count; once the count drops to 0, all direct dependencies of a given
// task have been accounted for and the task becomes independent.
// queue of tasks to check
queue := make([]string, 0, len(tasks))
// identify all independent tasks
for _, t := range tasks {
if predecessors[t.id] == 0 {
queue = append(queue, t.id)
}
}
for len(queue) > 0 {
// take the first independent task
id := queue[0]
queue = queue[1:]
// reduce the incoming edge of its successors
for _, successor := range taskByID[id].haltTasks {
predecessors[successor]--
if predecessors[successor] == 0 {
// a task that was a successor has become
// independent
delete(predecessors, successor)
queue = append(queue, successor)
}
}
}
if len(predecessors) != 0 {
// tasks that are left cannot have their dependencies satisfied
var unsatisfiedTasks []string
for id := range predecessors {
unsatisfiedTasks = append(unsatisfiedTasks, id)
}
sort.Strings(unsatisfiedTasks)
msg := strings.Builder{}
msg.WriteString("dependency cycle involving tasks [")
for i, id := range unsatisfiedTasks {
t := taskByID[id]
msg.WriteString(fmt.Sprintf("%v:%v", t.id, t.kind))
if i < len(unsatisfiedTasks)-1 {
msg.WriteRune(' ')
}
}
msg.WriteRune(']')
return &TaskDependencyCycleError{
IDs: unsatisfiedTasks,
msg: msg.String(),
}
}
return nil
}