Skip to content

Commit

Permalink
feat: support parallel with execution order groups (#2403)
Browse files Browse the repository at this point in the history
  • Loading branch information
tufitko committed Sep 8, 2022
1 parent 9f31e49 commit 2d7ee9d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
2 changes: 2 additions & 0 deletions runatlantis.io/docs/repo-level-atlantis-yaml.md
Expand Up @@ -214,6 +214,8 @@ projects:
```
With this config above, Atlantis runs planning/applying for project2 first, then for project1.
Several projects can have same `execution_order_group`. Any order in one group isn't guaranteed.
`parallel_plan` and `parallel_apply` respect these order groups, so parallel planning/applying works
in each group one by one.

### Custom Backend Config
See [Custom Workflow Use Cases: Custom Backend Config](custom-workflows.html#custom-backend-config)
Expand Down
2 changes: 1 addition & 1 deletion server/events/apply_command_runner.go
Expand Up @@ -144,7 +144,7 @@ func (a *ApplyCommandRunner) Run(ctx *command.Context, cmd *CommentCommand) {
var result command.Result
if a.isParallelEnabled(projectCmds) {
ctx.Log.Info("Running applies in parallel")
result = runProjectCmdsParallel(projectCmds, a.prjCmdRunner.Apply, a.parallelPoolSize)
result = runProjectCmdsParallelGroups(projectCmds, a.prjCmdRunner.Apply, a.parallelPoolSize)
} else {
result = runProjectCmds(projectCmds, a.prjCmdRunner.Apply)
}
Expand Down
4 changes: 2 additions & 2 deletions server/events/plan_command_runner.go
Expand Up @@ -110,7 +110,7 @@ func (p *PlanCommandRunner) runAutoplan(ctx *command.Context) {
var result command.Result
if p.isParallelEnabled(projectCmds) {
ctx.Log.Info("Running plans in parallel")
result = runProjectCmdsParallel(projectCmds, p.prjCmdRunner.Plan, p.parallelPoolSize)
result = runProjectCmdsParallelGroups(projectCmds, p.prjCmdRunner.Plan, p.parallelPoolSize)
} else {
result = runProjectCmds(projectCmds, p.prjCmdRunner.Plan)
}
Expand Down Expand Up @@ -184,7 +184,7 @@ func (p *PlanCommandRunner) run(ctx *command.Context, cmd *CommentCommand) {
var result command.Result
if p.isParallelEnabled(projectCmds) {
ctx.Log.Info("Running applies in parallel")
result = runProjectCmdsParallel(projectCmds, p.prjCmdRunner.Plan, p.parallelPoolSize)
result = runProjectCmdsParallelGroups(projectCmds, p.prjCmdRunner.Plan, p.parallelPoolSize)
} else {
result = runProjectCmds(projectCmds, p.prjCmdRunner.Plan)
}
Expand Down
35 changes: 35 additions & 0 deletions server/events/project_command_pool_executor.go
@@ -1,6 +1,7 @@
package events

import (
"sort"
"sync"

"github.com/remeh/sizedwaitgroup"
Expand Down Expand Up @@ -50,3 +51,37 @@ func runProjectCmds(
}
return command.Result{ProjectResults: results}
}

func splitByExecutionOrderGroup(cmds []command.ProjectContext) [][]command.ProjectContext {
groups := make(map[int][]command.ProjectContext)
for _, cmd := range cmds {
groups[cmd.ExecutionOrderGroup] = append(groups[cmd.ExecutionOrderGroup], cmd)
}

var groupKeys []int
for k := range groups {
groupKeys = append(groupKeys, k)
}
sort.Ints(groupKeys)

var res [][]command.ProjectContext
for _, group := range groupKeys {
res = append(res, groups[group])
}
return res
}

func runProjectCmdsParallelGroups(
cmds []command.ProjectContext,
runnerFunc prjCmdRunnerFunc,
poolSize int,
) command.Result {
var results []command.ProjectResult
groups := splitByExecutionOrderGroup(cmds)
for _, group := range groups {
res := runProjectCmdsParallel(group, runnerFunc, poolSize)
results = append(results, res.ProjectResults...)
}

return command.Result{ProjectResults: results}
}

0 comments on commit 2d7ee9d

Please sign in to comment.