Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested steps workflow parallelism #1046

Merged
merged 6 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion workflow/controller/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func (woc *wfOperationCtx) executeDAGTask(dagCtx *dagContext, taskName string) {
}
}
// Finally execute the template
_, _ = woc.executeTemplate(t.Template, t.Arguments, taskNodeName, dagCtx.boundaryID)
_, _ = woc.executeTemplate(t.Template, t.Arguments, taskNodeName, dagCtx.boundaryID, dagCtx.boundaryName)
}

// If we expanded the task, we still need to create the task entry for the non-expanded node,
Expand Down
54 changes: 48 additions & 6 deletions workflow/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (woc *wfOperationCtx) operate() {
}
var workflowStatus wfv1.NodePhase
var workflowMessage string
node, _ := woc.executeTemplate(woc.wf.Spec.Entrypoint, woc.wf.Spec.Arguments, woc.wf.ObjectMeta.Name, "")
node, _ := woc.executeTemplate(woc.wf.Spec.Entrypoint, woc.wf.Spec.Arguments, woc.wf.ObjectMeta.Name, "", "")
if node == nil || !node.Completed() {
// node can be nil if a workflow created immediately in a parallelism == 0 state
return
Expand All @@ -175,7 +175,7 @@ func (woc *wfOperationCtx) operate() {
}
woc.log.Infof("Running OnExit handler: %s", woc.wf.Spec.OnExit)
onExitNodeName := woc.wf.ObjectMeta.Name + ".onExit"
onExitNode, _ = woc.executeTemplate(woc.wf.Spec.OnExit, woc.wf.Spec.Arguments, onExitNodeName, "")
onExitNode, _ = woc.executeTemplate(woc.wf.Spec.OnExit, woc.wf.Spec.Arguments, onExitNodeName, "", "")
if onExitNode == nil || !onExitNode.Completed() {
return
}
Expand Down Expand Up @@ -495,6 +495,27 @@ func (woc *wfOperationCtx) countActivePods(boundaryIDs ...string) int64 {
return activePods
}

// countActiveChildren counts the number of active (Pending/Running) children nodes of parent parentName
func (woc *wfOperationCtx) countActiveChildren(parentName string) int64 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think countActiveChildren() should replace the use of countActivePods() and sum the number of all NodeTypePod, NodeTypeSteps, NodeTypeDAG nodes within a boundaryID as an aggregate. It can have similar logic to countActivePods() but modified slightly. Something like:

	activeChildren := 0
	for _, node := range woc.wf.Status.Nodes {
		if boundaryID != "" && node.BoundaryID != boundaryID {
			continue
		}
		switch node.Type {
		case wfv1.NodeTypePod, wfv1.NodeTypeSteps, wfv1.NodeTypeDAG:
		default:
			continue
		}
		switch node.Phase {
		case wfv1.NodePending, wfv1.NodeRunning:
			activeChildren++
		}
	}
	return activeChildren

Then, the existing calls to countActivePods() in checkParallelism() would be replaced with the call to countActiveChildren().

We would then only use countActivePods() when checking against the global parallelism limit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Finding children nodes with the same boundaryID should be able to solve my problem in #1046 (comment). (I was stuck at using the Children field to find children 😅). I'll let you know if it works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's working! 😀

parent := woc.getNodeByName(parentName)
if parent == nil {
return 0
}
var activeChildren int64
// if we care about parallelism, count the active children nodes at the template level
for _, c := range parent.Children {
node, ok := woc.wf.Status.Nodes[c]
if !ok {
continue
}
switch node.Phase {
case wfv1.NodePending, wfv1.NodeRunning:
activeChildren++
}
}
return activeChildren
}

// getAllWorkflowPods returns all pods related to the current workflow
func (woc *wfOperationCtx) getAllWorkflowPods() (*apiv1.PodList, error) {
options := metav1.ListOptions{
Expand Down Expand Up @@ -866,8 +887,9 @@ func (woc *wfOperationCtx) getLastChildNode(node *wfv1.NodeStatus) (*wfv1.NodeSt
// for the created node (if created). Nodes may not be created if parallelism or deadline exceeded.
// nodeName is the name to be used as the name of the node, and boundaryID indicates which template
// boundary this node belongs to.
func (woc *wfOperationCtx) executeTemplate(templateName string, args wfv1.Arguments, nodeName string, boundaryID string) (*wfv1.NodeStatus, error) {
woc.log.Debugf("Evaluating node %s: template: %s", nodeName, templateName)
func (woc *wfOperationCtx) executeTemplate(templateName string, args wfv1.Arguments, nodeName string, boundaryID string, parentName string) (*wfv1.NodeStatus, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be necessary to change the method signature to include parentName. parentName is the same as woc.wf.Status.Nodes[boundaryID].Name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reply. I tried to use boundaryID to find the parent node, but I found it not necessarily the case. An example is in the example workflow I used in the issue #1035.

The seq-step (template B) 's boundaryID is the top node (template A), while its parent node is a StepGroup node between it and the top node. I could only find the children nodes with the parentNode, but not with the top node.

I'm afraid I can't express it precisely, so let me show the figure.
image

Some related logs are here if it helps (see the lines starting with "Evaluating node "):


time="2018-10-27T03:53:03Z" level=info msg="Processing workflow" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=info msg="Updated phase  -> Running" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=debug msg="Evaluating node seq-test-ms4fj: template: A, boundaryID: , parentName: " namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=info msg="tmpl type: Steps, parallelism:0xc4207ea8c0, node:<nil>" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=info msg="Steps node seq-test-ms4fj (seq-test-ms4fj) initialized Running" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=info msg="StepGroup node seq-test-ms4fj[0] (seq-test-ms4fj-3412503640) initialized Running" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=error msg="shouldExecute , proceed: true, error: <nil>" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=debug msg="Evaluating node seq-test-ms4fj[0].seq-step(0:a): template: B, boundaryID: seq-test-ms4fj, parentName: seq-test-ms4fj[0]" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=info msg="tmpl type: Steps, parallelism:<nil>, node:<nil>" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=debug msg="counted 0/1 active children in boundary seq-test-ms4fj of parent seq-test-ms4fj[0]" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=info msg="Steps node seq-test-ms4fj[0].seq-step(0:a) (seq-test-ms4fj-643033778) initialized Running" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=info msg="StepGroup node seq-test-ms4fj[0].seq-step(0:a)[0] (seq-test-ms4fj-3735306292) initialized Running" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=error msg="shouldExecute , proceed: true, error: <nil>" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=debug msg="Evaluating node seq-test-ms4fj[0].seq-step(0:a)[0].jobs(0:1): template: one-job, boundaryID: seq-test-ms4fj-643033778, parentName: seq-test-ms4fj[0].seq-step(0:a)[0]" namespace=default workflow
=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=info msg="tmpl type: Container, parallelism:<nil>, node:<nil>" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=debug msg="Executing node seq-test-ms4fj[0].seq-step(0:a)[0].jobs(0:1) with container template: &{one-job {[{seq-id <nil> 0xc42088bdc0 <nil> }] []} {[] [] <nil>} map[] nil {map[] map[]} <nil> [] &Containe
r{Name:,Image:alpine,Command:[/bin/sh -c],Args:[echo a; sleep 10],WorkingDir:,Ports:[],Env:[],Resources:ResourceRequirements{Limits:ResourceList{},Requests:ResourceList{},},VolumeMounts:[],LivenessProbe:nil,ReadinessProbe:nil,Lifecycle:ni
l,TerminationMessagePath:,ImagePullPolicy:,SecurityContext:nil,Stdin:false,StdinOnce:false,TTY:false,EnvFrom:[],TerminationMessagePolicy:,VolumeDevices:[],} <nil> <nil> <nil> <nil> [] <nil> <nil> <nil> <nil> []}\n" namespace=default workf
low=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=debug msg="Creating Pod: seq-test-ms4fj[0].seq-step(0:a)[0].jobs(0:1) (seq-test-ms4fj-2529398666)" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=info msg="Created pod: seq-test-ms4fj[0].seq-step(0:a)[0].jobs(0:1) (seq-test-ms4fj-2529398666)" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=info msg="Pod node seq-test-ms4fj[0].seq-step(0:a)[0].jobs(0:1) (seq-test-ms4fj-2529398666) initialized Pending" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=error msg="shouldExecute , proceed: true, error: <nil>" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=debug msg="Evaluating node seq-test-ms4fj[0].seq-step(0:a)[0].jobs(1:2): template: one-job, boundaryID: seq-test-ms4fj-643033778, parentName: seq-test-ms4fj[0].seq-step(0:a)[0]" namespace=default workflow
=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=info msg="tmpl type: Container, parallelism:<nil>, node:<nil>" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=debug msg="Executing node seq-test-ms4fj[0].seq-step(0:a)[0].jobs(1:2) with container template: &{one-job {[{seq-id <nil> 0xc420b09c30 <nil> }] []} {[] [] <nil>} map[] nil {map[] map[]} <nil> [] &Containe
r{Name:,Image:alpine,Command:[/bin/sh -c],Args:[echo a; sleep 10],WorkingDir:,Ports:[],Env:[],Resources:ResourceRequirements{Limits:ResourceList{},Requests:ResourceList{},},VolumeMounts:[],LivenessProbe:nil,ReadinessProbe:nil,Lifecycle:ni
l,TerminationMessagePath:,ImagePullPolicy:,SecurityContext:nil,Stdin:false,StdinOnce:false,TTY:false,EnvFrom:[],TerminationMessagePolicy:,VolumeDevices:[],} <nil> <nil> <nil> <nil> [] <nil> <nil> <nil> <nil> []}\n" namespace=default workf
low=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=debug msg="Creating Pod: seq-test-ms4fj[0].seq-step(0:a)[0].jobs(1:2) (seq-test-ms4fj-765137104)" namespace=default workflow=seq-test-ms4fj
time="2018-10-27T03:53:03Z" level=info msg="Created pod: seq-test-ms4fj[0].seq-step(0:a)[0].jobs(1:2) (seq-test-ms4fj-765137104)" namespace=default workflow=seq-test-ms4fj

woc.log.Debugf("Evaluating node %s: template: %s, boundaryID: %s, parentName: %s", nodeName, templateName, boundaryID, parentName)

node := woc.getNodeByName(nodeName)
if node != nil && node.Completed() {
woc.log.Debugf("Node %s already completed", nodeName)
Expand All @@ -887,7 +909,7 @@ func (woc *wfOperationCtx) executeTemplate(templateName string, args wfv1.Argume
err := errors.Errorf(errors.CodeBadRequest, "Node %v error: template '%s' undefined", node, templateName)
return woc.initializeNode(nodeName, wfv1.NodeTypeSkipped, "", boundaryID, wfv1.NodeError, err.Error()), err
}
if err := woc.checkParallelism(tmpl, node, boundaryID); err != nil {
if err := woc.checkParallelism(tmpl, node, boundaryID, parentName); err != nil {
return node, err
}

Expand Down Expand Up @@ -1096,11 +1118,13 @@ func (woc *wfOperationCtx) markNodeError(nodeName string, err error) *wfv1.NodeS
}

// checkParallelism checks if the given template is able to be executed, considering the current active pods and workflow/template parallelism
func (woc *wfOperationCtx) checkParallelism(tmpl *wfv1.Template, node *wfv1.NodeStatus, boundaryID string) error {
func (woc *wfOperationCtx) checkParallelism(tmpl *wfv1.Template, node *wfv1.NodeStatus, boundaryID string, parentName string) error {
woc.log.Infof("tmpl type: %v, parallelism:%v, node:%v", tmpl.GetType(), tmpl.Parallelism, node)
if woc.wf.Spec.Parallelism != nil && woc.activePods >= *woc.wf.Spec.Parallelism {
woc.log.Infof("workflow active pod spec parallelism reached %d/%d", woc.activePods, *woc.wf.Spec.Parallelism)
return ErrParallelismReached
}

// TODO: repeated calls to countActivePods is not optimal
switch tmpl.GetType() {
case wfv1.TemplateTypeDAG, wfv1.TemplateTypeSteps:
Expand All @@ -1112,6 +1136,24 @@ func (woc *wfOperationCtx) checkParallelism(tmpl *wfv1.Template, node *wfv1.Node
return ErrParallelismReached
}
}

// if we are about to start executing a StepGroup, make our parent hasn't reached it's limit
// only when it is not started yet, i.e. let it keep running if it has started
if boundaryID != "" && (node == nil || (node.Phase != wfv1.NodePending && node.Phase != wfv1.NodeRunning)) {
boundaryNode := woc.wf.Status.Nodes[boundaryID]
boundaryTemplate := woc.wf.GetTemplate(boundaryNode.TemplateName)
if boundaryTemplate.Parallelism != nil {
// for stepgroups, parent is different from boundary
activeSiblings := woc.countActiveChildren(parentName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like it's possible to miscalculate the parallelism since we are checking active pods independently of child steps/dag template invocations, and not summing up the counts of each to compare against the parallelism limit. I think the calculation needs to be the summation of both pods, as well as dag/step templates for the parallelism calculation to be accurate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I put the two countings together.


woc.log.Debugf("counted %d/%d active children in boundary %s of parent %s", activeSiblings, *boundaryTemplate.Parallelism, boundaryID, parentName)
if activeSiblings >= *boundaryTemplate.Parallelism {
woc.log.Infof("template (node %s) active pod parallelism reached %d/%d", boundaryID, activeSiblings, *boundaryTemplate.Parallelism)
return ErrParallelismReached
}
}
}

default:
// if we are about to execute a pod, make our parent hasn't reached it's limit
if boundaryID != "" {
Expand Down
2 changes: 1 addition & 1 deletion workflow/controller/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (woc *wfOperationCtx) executeStepGroup(stepGroup []wfv1.WorkflowStep, sgNod
}
continue
}
childNode, err := woc.executeTemplate(step.Template, step.Arguments, childNodeName, stepsCtx.boundaryID)
childNode, err := woc.executeTemplate(step.Template, step.Arguments, childNodeName, stepsCtx.boundaryID, sgNodeName)
if err != nil {
switch err {
case ErrDeadlineExceeded:
Expand Down