Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: block tasks within the same pipeline (#16128)

p0ny9f689895f9dc
diff --git a/backend/runner/taskrun/schedulerv2.go b/backend/runner/taskrun/schedulerv2.go
index be41c37a23..3976f526fe 100644
--- a/backend/runner/taskrun/schedulerv2.go
+++ b/backend/runner/taskrun/schedulerv2.go
@@ -304,16 +304,16 @@ func (s *SchedulerV2) schedulePendingTaskRun(ctx context.Context, taskRun *store
return true, nil
}
- taskIDs, err := s.store.FindBlockingTasksByVersion(ctx, task.InstanceID, *task.DatabaseName, schemaVersion)
+ maybeTaskID, err := s.store.FindBlockingTaskByVersion(ctx, task.PipelineID, task.InstanceID, *task.DatabaseName, schemaVersion)
if err != nil {
return false, errors.Wrapf(err, "failed to find blocking versioned tasks")
}
- if len(taskIDs) > 0 {
+ if maybeTaskID != nil {
s.stateCfg.TaskRunSchedulerInfo.Store(taskRun.ID, &storepb.SchedulerInfo{
ReportTime: timestamppb.Now(),
WaitingCause: &storepb.SchedulerInfo_WaitingCause{
Cause: &storepb.SchedulerInfo_WaitingCause_TaskUid{
- TaskUid: int32(taskIDs[0]),
+ TaskUid: int32(*maybeTaskID),
},
},
})
@@ -357,6 +357,7 @@ func (s *SchedulerV2) scheduleRunningTaskRuns(ctx context.Context) error {
// We only run the first (i.e. which has the minimum task ID) task for each database.
// 1. For ddl tasks, we run them one by one to get a sane schema dump and thus diff.
// 2. For versioned tasks, this is our last resort to determine the order for tasks with the same version. We don't want to run them in parallel.
+ // 2.1. Rollout 1 tasks will be run before rollout 2 tasks. Where, rollout 1 tasks are created before rollout 2 tasks.
minTaskIDForDatabase := map[string]int{}
for _, taskRun := range taskRuns {
task, err := s.store.GetTaskV2ByID(ctx, taskRun.TaskUID)
diff --git a/backend/store/task.go b/backend/store/task.go
index e3eb46b4fa..eeca0cee87 100644
--- a/backend/store/task.go
+++ b/backend/store/task.go
@@ -103,7 +103,9 @@ func (s *Store) GetTaskV2ByID(ctx context.Context, id int) (*TaskMessage, error)
return tasks[0], nil
}
-func (s *Store) FindBlockingTasksByVersion(ctx context.Context, instanceID, databaseName string, version string) ([]int, error) {
+// Get a blocking task in the pipeline.
+// A task is blocked by a task with a smaller schema version within the same pipeline.
+func (s *Store) FindBlockingTaskByVersion(ctx context.Context, pipelineUID int, instanceID, databaseName string, version string) (*int, error) {
query := `
SELECT
task.id
@@ -121,35 +123,24 @@ func (s *Store) FindBlockingTasksByVersion(ctx context.Context, instanceID, data
), 'NOT_STARTED'
) AS status
) AS latest_task_run ON TRUE
- WHERE task.instance = $1 AND task.db_name = $2
+ WHERE task.pipeline_id = $1 AND task.instance = $2 AND task.db_name = $3
AND task.payload->>'schemaVersion' IS NOT NULL
- AND task.payload->>'schemaVersion' < $3
+ AND task.payload->>'schemaVersion' < $4
AND (task.payload->>'skipped')::BOOLEAN IS NOT TRUE
AND latest_task_run.status != 'DONE'
AND COALESCE(issue.status, 'OPEN') = 'OPEN'
ORDER BY task.id ASC
+ LIMIT 1
`
- rows, err := s.db.QueryContext(ctx, query, instanceID, databaseName, version)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to query rows")
- }
- defer rows.Close()
-
- var ids []int
- for rows.Next() {
- var id int
- if err := rows.Scan(&id); err != nil {
- return nil, errors.Wrapf(err, "failed to scan")
+ var id int
+ if err := s.db.QueryRowContext(ctx, query, pipelineUID, instanceID, databaseName, version).Scan(&id); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, nil
}
- ids = append(ids, id)
+ return nil, errors.Wrapf(err, "failed to find blocking task by version %s", version)
}
-
- if err := rows.Err(); err != nil {
- return nil, errors.Wrapf(err, "rows err")
- }
-
- return ids, nil
+ return &id, nil
}
func (*Store) createTasks(ctx context.Context, txn *sql.Tx, creates ...*TaskMessage) ([]*TaskMessage, error) {