@@ -128,13 +128,18 @@ func FlattenByNumberAndComponent(tasks []*Task) [][]*TaskNode {
128
128
return [][]* TaskNode {groups }
129
129
}
130
130
131
+ // TaskNode represents a node in a TaskGraph. The node assumes the graph is indexable and nodes are retrievable by index,
132
+ // and In/Out are indices of other nodes connected to this one by incoming/outgoing edges, respectively.
131
133
type TaskNode struct {
132
- In []int
134
+ // In is a list of node indices from which there is an edge to this node (=prerequisites)
135
+ In []int
136
+ // Tasks to be executed when this node is visited
133
137
Tasks []* Task
134
- Out []int
138
+ // Out is a list of node indices to which there is an edge from this node (=dependents).
139
+ Out []int
135
140
}
136
141
137
- func (n TaskNode ) String () string {
142
+ func (n * TaskNode ) String () string {
138
143
var arr []string
139
144
for _ , t := range n .Tasks {
140
145
if len (t .Manifest .OriginalFilename ) > 0 {
@@ -262,7 +267,7 @@ func (g *TaskGraph) Split(onFn func(task *Task) bool) {
262
267
}
263
268
264
269
// BreakFunc returns the input tasks in order of dependencies with
265
- // explicit parallelizm allowed per task in an array of task nodes.
270
+ // explicit parallelism allowed per task in an array of task nodes.
266
271
type BreakFunc func ([]* Task ) [][]* TaskNode
267
272
268
273
// ShiftOrder rotates each TaskNode by step*len/stride when stride > len,
@@ -424,8 +429,8 @@ type taskStatus struct {
424
429
}
425
430
426
431
// RunGraph executes the provided graph in order and in parallel up to maxParallelism. It will not start
427
- // a new TaskNode until all of the prerequisites have completed. If fn returns an error, no dependencies
428
- // of that node will be executed, but other indepedent edges will continue executing.
432
+ // a new TaskNode until all its prerequisites have completed. If fn returns an error, no dependencies
433
+ // of that node will be executed, but other independent edges will continue executing.
429
434
func RunGraph (ctx context.Context , graph * TaskGraph , maxParallelism int , fn func (ctx context.Context , tasks []* Task ) error ) []error {
430
435
submitted := make ([]bool , len (graph .Nodes ))
431
436
results := make ([]* taskStatus , len (graph .Nodes ))
@@ -452,7 +457,7 @@ func RunGraph(ctx context.Context, graph *TaskGraph, maxParallelism int, fn func
452
457
return - 1
453
458
}
454
459
455
- // Tasks go out to the workers via workCh, and results come brack
460
+ // Tasks go out to the workers via workCh, and results come back
456
461
// from the workers via resultCh.
457
462
workCh := make (chan runTasks , maxParallelism )
458
463
defer close (workCh )
@@ -469,16 +474,16 @@ func RunGraph(ctx context.Context, graph *TaskGraph, maxParallelism int, fn func
469
474
}
470
475
for i := 0 ; i < maxParallelism ; i ++ {
471
476
wg .Add (1 )
472
- go func (ctx context.Context , job int ) {
477
+ go func (ctx context.Context , worker int ) {
473
478
defer utilruntime .HandleCrash ()
474
479
defer wg .Done ()
475
480
for {
476
481
select {
477
482
case <- ctx .Done ():
478
- klog .V (2 ).Infof ("Canceled worker %d while waiting for work" , job )
483
+ klog .V (2 ).Infof ("Worker %d: Received cancel signal while waiting for work" , worker )
479
484
return
480
485
case runTask := <- workCh :
481
- klog .V (2 ).Infof ("Running %d on worker %d" , runTask .index , job )
486
+ klog .V (2 ).Infof ("Worker %d: Running job %d (with %d tasks) " , worker , runTask .index , len ( runTask . tasks ) )
482
487
err := fn (ctx , runTask .tasks )
483
488
resultCh <- taskStatus {index : runTask .index , error : err }
484
489
}
@@ -487,9 +492,10 @@ func RunGraph(ctx context.Context, graph *TaskGraph, maxParallelism int, fn func
487
492
}
488
493
489
494
var inflight int
490
- nextNode := getNextNode ()
491
- done := false
495
+ var done bool
496
+
492
497
for ! done {
498
+ nextNode := getNextNode ()
493
499
switch {
494
500
case ctx .Err () == nil && nextNode >= 0 : // push a task or collect a result
495
501
select {
@@ -517,18 +523,16 @@ func RunGraph(ctx context.Context, graph *TaskGraph, maxParallelism int, fn func
517
523
default : // no work to push and nothing in flight. We're done
518
524
done = true
519
525
}
520
- if ! done {
521
- nextNode = getNextNode ()
522
- }
523
526
}
524
527
525
528
cancelFn ()
526
529
wg .Wait ()
527
- klog .V (2 ).Infof ("Workers finished" )
530
+ klog .V (2 ).Info ("Workers finished" )
528
531
529
532
var errs []error
530
533
var firstIncompleteNode * TaskNode
531
- incompleteCount := 0
534
+ var incompleteCount int
535
+
532
536
for i , result := range results {
533
537
if result == nil {
534
538
if firstIncompleteNode == nil && len (graph .Nodes [i ].Tasks ) > 0 {
@@ -547,9 +551,6 @@ func RunGraph(ctx context.Context, graph *TaskGraph, maxParallelism int, fn func
547
551
}
548
552
}
549
553
550
- klog .V (2 ).Infof ("Result of work: %v" , errs )
551
- if len (errs ) > 0 {
552
- return errs
553
- }
554
- return nil
554
+ klog .V (2 ).Infof ("Result of work: errs=%v" , errs )
555
+ return errs
555
556
}
0 commit comments