Skip to content

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

feat: impl UpdatePlan task dags (#11205)

p0ny1f0ce1ee5662
diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.go
index eff26a1b55..d5e7eb3f56 100644
--- a/backend/api/v1/rollout_service.go
+++ b/backend/api/v1/rollout_service.go
@@ -876,6 +876,13 @@ func (s *RolloutService) UpdatePlan(ctx context.Context, request *v1pb.UpdatePla
return nil, status.Errorf(codes.InvalidArgument, "no specs updated")
}
+ oldSpecsByID := make(map[string]*v1pb.Plan_Spec)
+ for _, step := range oldSteps {
+ for _, spec := range step.Specs {
+ oldSpecsByID[spec.Id] = spec
+ }
+ }
+
updatedByID := make(map[string]*v1pb.Plan_Spec)
for _, spec := range updated {
updatedByID[spec.Id] = spec
@@ -885,12 +892,26 @@ func (s *RolloutService) UpdatePlan(ctx context.Context, request *v1pb.UpdatePla
var taskPatchList []*api.TaskPatch
var statementUpdates []api.ActivityPipelineTaskStatementUpdatePayload
var earliestUpdates []api.ActivityPipelineTaskEarliestAllowedTimeUpdatePayload
+ var taskDAGRebuildList []struct {
+ fromTaskIDs []int
+ toTaskID int
+ }
if oldPlan.PipelineUID != nil {
tasks, err := s.store.ListTasks(ctx, &api.TaskFind{PipelineID: oldPlan.PipelineUID})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list tasks: %v", err)
}
+ tasksBySpecID := make(map[string][]*store.TaskMessage)
+ for _, task := range tasks {
+ var taskSpecID struct {
+ SpecID string `json:"specId"`
+ }
+ if err := json.Unmarshal([]byte(task.Payload), &taskSpecID); err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to unmarshal task payload: %v", err)
+ }
+ tasksBySpecID[taskSpecID.SpecID] = append(tasksBySpecID[taskSpecID.SpecID], task)
+ }
for _, task := range tasks {
doUpdate := false
taskPatch := &api.TaskPatch{
@@ -1039,6 +1060,42 @@ func (s *RolloutService) UpdatePlan(ctx context.Context, request *v1pb.UpdatePla
return nil, err
}
+ // task dag
+ if err := func() error {
+ oldSpec, ok := oldSpecsByID[taskSpecID.SpecID]
+ if !ok {
+ return nil
+ }
+
+ sort.Slice(oldSpec.DependsOnSpecs, func(i, j int) bool {
+ return oldSpec.DependsOnSpecs[i] < oldSpec.DependsOnSpecs[j]
+ })
+ sort.Slice(spec.DependsOnSpecs, func(i, j int) bool {
+ return spec.DependsOnSpecs[i] < spec.DependsOnSpecs[j]
+ })
+ if slices.Equal(oldSpec.GetDependsOnSpecs(), spec.GetDependsOnSpecs()) {
+ return nil
+ }
+
+ // rebuild the task dag
+ var fromTaskIDs []int
+ for _, dependsOnSpec := range spec.GetDependsOnSpecs() {
+ for _, dependsOnTask := range tasksBySpecID[dependsOnSpec] {
+ fromTaskIDs = append(fromTaskIDs, dependsOnTask.ID)
+ }
+ }
+ taskDAGRebuildList = append(taskDAGRebuildList, struct {
+ fromTaskIDs []int
+ toTaskID int
+ }{
+ fromTaskIDs: fromTaskIDs,
+ toTaskID: task.ID,
+ })
+ return nil
+ }(); err != nil {
+ return nil, err
+ }
+
if !doUpdate {
continue
}
@@ -1092,6 +1149,12 @@ func (s *RolloutService) UpdatePlan(ctx context.Context, request *v1pb.UpdatePla
}
}
+ for _, taskDAGRebuild := range taskDAGRebuildList {
+ if err := s.store.RebuildTaskDAG(ctx, taskDAGRebuild.fromTaskIDs, taskDAGRebuild.toTaskID); err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to rebuild task dag: %v", err)
+ }
+ }
+
if err := s.store.UpdatePlan(ctx, &store.UpdatePlanMessage{
UID: oldPlan.UID,
UpdaterID: user.ID,
diff --git a/backend/store/task_dag.go b/backend/store/task_dag.go
index da5eebc54a..0992b351da 100644
--- a/backend/store/task_dag.go
+++ b/backend/store/task_dag.go
@@ -5,6 +5,8 @@ import (
"database/sql"
"fmt"
"strings"
+
+ "github.com/pkg/errors"
)
// TaskDAGMessage is the message for task dags.
@@ -19,6 +21,35 @@ type TaskDAGFind struct {
PipelineID *int
}
+func (s *Store) RebuildTaskDAG(ctx context.Context, fromTaskIDs []int, toTaskID int) error {
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return err
+ }
+ defer tx.Rollback()
+
+ if _, err := tx.ExecContext(ctx, `
+ DELETE FROM task_dag WHERE to_task_id = $1
+ `, toTaskID); err != nil {
+ return errors.Wrapf(err, "failed to delete old task dags")
+ }
+
+ query := `
+ INSERT INTO task_dag (
+ from_task_id,
+ to_task_id
+ ) SELECT unnest(CAST($1 AS INTEGER[])), $2
+ `
+ if _, err := tx.ExecContext(ctx, query,
+ fromTaskIDs,
+ toTaskID,
+ ); err != nil {
+ return err
+ }
+
+ return tx.Commit()
+}
+
// CreateTaskDAGV2 creates a task DAG.
func (s *Store) CreateTaskDAGV2(ctx context.Context, create *TaskDAGMessage) error {
tx, err := s.db.BeginTx(ctx, nil)