Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: run tasks concurrently (#2366)
p0ny9b9c012698c5
diff --git a/server/issue.go b/server/issue.goindex 855c196534..ab54e6dba8 100644--- a/server/issue.go+++ b/server/issue.go@@ -316,7 +316,7 @@ func (s *Server) createIssue(ctx context.Context, issueCreate *api.IssueCreate,}}- if _, err := s.ScheduleNextTaskIfNeeded(ctx, issue.Pipeline); err != nil {+ if err := s.ScheduleActiveStage(ctx, issue.Pipeline); err != nil {return nil, errors.Wrapf(err, "failed to schedule task after creating the issue: %v", issue.Name)}diff --git a/server/pipeline.go b/server/pipeline.goindex 84398707a4..bfcef822c8 100644--- a/server/pipeline.go+++ b/server/pipeline.go@@ -7,57 +7,48 @@ import ("github.com/pkg/errors")-// ScheduleNextTaskIfNeeded tries to schedule the next task if needed.-// Returns nil if no task applicable can be scheduled.-func (s *Server) ScheduleNextTaskIfNeeded(ctx context.Context, pipeline *api.Pipeline) (*api.Task, error) {- for _, stage := range pipeline.StageList {- for _, task := range stage.TaskList {- // Should short circuit upon reaching RUNNING or FAILED task.- if task.Status == api.TaskRunning || task.Status == api.TaskFailed {- return nil, nil+// ScheduleActiveStage tries to schedule the tasks in the active stage.+func (s *Server) ScheduleActiveStage(ctx context.Context, pipeline *api.Pipeline) error {+ stage := getActiveStage(pipeline.StageList)+ if stage == nil {+ return nil+ }+ for _, task := range stage.TaskList {+ switch task.Status {+ case api.TaskPendingApproval:+ task, err := s.TaskCheckScheduler.ScheduleCheckIfNeeded(ctx, task, api.SystemBotID, true /* skipIfAlreadyTerminated */)+ if err != nil {+ return err}-- skipIfAlreadyTerminated := true- if task.Status == api.TaskPendingApproval {- task, err := s.TaskCheckScheduler.ScheduleCheckIfNeeded(ctx, task, api.SystemBotID, skipIfAlreadyTerminated)- if err != nil {- return nil, err- }-- policy, err := s.store.GetPipelineApprovalPolicy(ctx, task.Instance.EnvironmentID)+ policy, err := s.store.GetPipelineApprovalPolicy(ctx, task.Instance.EnvironmentID)+ if err != nil {+ return errors.Wrapf(err, "failed to get approval policy for environment ID %d", task.Instance.EnvironmentID)+ }+ if policy.Value == api.PipelineApprovalValueManualNever {+ // transit into Pending for ManualNever (auto-approval) tasks if all required task checks passed.+ ok, err := s.TaskScheduler.canAutoApprove(ctx, task)if err != nil {- return nil, errors.Wrapf(err, "failed to get approval policy for environment ID %d", task.Instance.EnvironmentID)+ return err}- if policy.Value == api.PipelineApprovalValueManualNever {- // transit into Pending for ManualNever (auto-approval) tasks if all required task checks passed.- ok, err := s.TaskScheduler.canAutoApprove(ctx, task)- if err != nil {- return nil, err- }- if ok {- if _, err := s.patchTaskStatus(ctx, task, &api.TaskStatusPatch{- ID: task.ID,- UpdaterID: api.SystemBotID,- Status: api.TaskPending,- }); err != nil {- return nil, err- }+ if ok {+ if _, err := s.patchTaskStatus(ctx, task, &api.TaskStatusPatch{+ ID: task.ID,+ UpdaterID: api.SystemBotID,+ Status: api.TaskPending,+ }); err != nil {+ return err}}- return task, nil}-- if task.Status == api.TaskPending {- if _, err := s.TaskCheckScheduler.ScheduleCheckIfNeeded(ctx, task, api.SystemBotID, skipIfAlreadyTerminated); err != nil {- return nil, err- }- updatedTask, err := s.TaskScheduler.ScheduleIfNeeded(ctx, task)- if err != nil {- return nil, err- }- return updatedTask, nil+ case api.TaskPending:+ if _, err := s.TaskCheckScheduler.ScheduleCheckIfNeeded(ctx, task, api.SystemBotID, true /* skipIfAlreadyTerminated */); err != nil {+ return err+ }+ _, err := s.TaskScheduler.ScheduleIfNeeded(ctx, task)+ if err != nil {+ return err}}}- return nil, nil+ return nil}diff --git a/server/task.go b/server/task.goindex 6703564991..6fd6ab1f1a 100644--- a/server/task.go+++ b/server/task.go@@ -703,7 +703,7 @@ func (s *Server) patchTaskStatus(ctx context.Context, task *api.Task, taskStatus}}- // If this is the last task in the pipeline and just completed, and the assignee is system bot:+ // If every task in the pipeline completes, and the assignee is system bot:// Case 1: If the task is associated with an issue, then we mark the issue (including the pipeline) as DONE.// Case 2: If the task is NOT associated with an issue, then we mark the pipeline as DONE.if taskPatched.Status == "DONE" && (issue == nil || issue.AssigneeID == api.SystemBotID) {@@ -714,9 +714,9 @@ func (s *Server) patchTaskStatus(ctx context.Context, task *api.Task, taskStatusif pipeline == nil {return nil, errors.Errorf("pipeline not found for ID %v", taskPatched.PipelineID)}- lastStage := pipeline.StageList[len(pipeline.StageList)-1]- if lastStage.TaskList[len(lastStage.TaskList)-1].ID == taskPatched.ID {+ if areAllTasksDone(pipeline) {if issue == nil {+ // System-generated tasks such as backup tasks don't have corresponding issues.status := api.PipelineDonepipelinePatch := &api.PipelinePatch{ID: pipeline.ID,@@ -815,3 +815,14 @@ func (s *Server) getDefaultAssigneeID(ctx context.Context, environmentID int, pr// never reachedreturn api.UnknownID, errors.New("invalid assigneeGroupValue")}++func areAllTasksDone(pipeline *api.Pipeline) bool {+ for _, stage := range pipeline.StageList {+ for _, task := range stage.TaskList {+ if task.Status != api.TaskDone {+ return false+ }+ }+ }+ return true+}diff --git a/server/task_executor.go b/server/task_executor.goindex bcadf08dc5..3d85484e24 100644--- a/server/task_executor.go+++ b/server/task_executor.go@@ -169,6 +169,7 @@ func postMigration(ctx context.Context, server *Server, task *api.Task, vcsPushEdatabaseName := task.Database.Nameissue, err := findIssueByTask(ctx, server, task)if err != nil {+ // If somehow we cannot find the issue, emit the error since it's not fatal.log.Error("failed to find containing issue", zap.Error(err))}var repo *api.Repository@@ -180,13 +181,13 @@ func postMigration(ctx context.Context, server *Server, task *api.Task, vcsPushE}// If VCS based and schema path template is specified, then we will write back the latest schema file after migration.writeBack := (vcsPushEvent != nil) && (repo.SchemaPathTemplate != "")- // For tenant mode project, we will only write back latest schema file on the last task.project, err := server.store.GetProjectByID(ctx, task.Database.ProjectID)if err != nil {return true, nil, err}if writeBack && issue != nil {if project.TenantMode == api.TenantModeTenant {+ // For tenant mode project, we will only write back once and we happen to write back on lastTask done.var lastTask *api.Taskfor i := len(issue.Pipeline.StageList) - 1; i >= 0; i-- {stage := issue.Pipeline.StageList[i]@@ -312,7 +313,6 @@ func runMigration(ctx context.Context, server *Server, task *api.Task, migrationfunc findIssueByTask(ctx context.Context, server *Server, task *api.Task) (*api.Issue, error) {issue, err := server.store.GetIssueByPipelineID(ctx, task.PipelineID)if err != nil {- // If somehow we cannot find the issue, emit the error since it's not fatal.return nil, errors.Wrapf(err, "failed to fetch containing issue for composing the migration info, task_id: %v", task.ID)}if issue == nil {diff --git a/server/task_scheduler.go b/server/task_scheduler.goindex 090586b075..86e3f6a2ca 100644--- a/server/task_scheduler.go+++ b/server/task_scheduler.go@@ -89,8 +89,8 @@ func (s *TaskScheduler) Run(ctx context.Context, wg *sync.WaitGroup) {continue}- if _, err := s.server.ScheduleNextTaskIfNeeded(ctx, pipeline); err != nil {- log.Error("Failed to schedule next running task",+ if err := s.server.ScheduleActiveStage(ctx, pipeline); err != nil {+ log.Error("Failed to schedule tasks in the active stage",zap.Int("pipeline_id", pipeline.ID),zap.Error(err),)diff --git a/server/task_test.go b/server/task_test.gonew file mode 100644index 0000000000..faac806717--- /dev/null+++ b/server/task_test.go@@ -0,0 +1,86 @@+package server++import (+ "testing"++ "github.com/bytebase/bytebase/api"+ "github.com/stretchr/testify/assert"+)++func TestAreAllTasksDone(t *testing.T) {+ tests := []struct {+ pipeline *api.Pipeline+ want bool+ }{+ {+ pipeline: &api.Pipeline{+ StageList: []*api.Stage{+ {+ TaskList: []*api.Task{+ {+ Status: api.TaskDone,+ },+ },+ },+ },+ },+ want: true,+ },+ {+ pipeline: &api.Pipeline{+ StageList: []*api.Stage{+ {+ TaskList: []*api.Task{+ {+ Status: api.TaskDone,+ },+ },+ },+ {+ TaskList: []*api.Task{+ {+ Status: api.TaskDone,+ },+ },+ },+ },+ },+ want: true,+ },+ {+ pipeline: &api.Pipeline{+ StageList: []*api.Stage{+ {+ TaskList: []*api.Task{+ {+ Status: api.TaskDone,+ },+ },+ },+ {+ TaskList: []*api.Task{+ {+ Status: api.TaskDone,+ },+ {+ Status: api.TaskFailed,+ },+ {+ Status: api.TaskPending,+ },+ {+ Status: api.TaskRunning,+ },+ },+ },+ },+ },+ want: false,+ },+ }++ for _, test := range tests {+ res := areAllTasksDone(test.pipeline)+ assert.Equal(t, test.want, res)+ }+}