Skip to content

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

chore: impl max running taskruns per rollout Former-commit-id: b94c46f3bfd91ee53ea8772ed0c001bc0626fc46 Former-commit-id: d820ba05b2a337dd6586e07d701ce7e9a1f5b621

p0ny2b62530c2c60
diff --git a/backend/component/state/state.go b/backend/component/state/state.go
index 483649e12a..d5dc4544c0 100644
--- a/backend/component/state/state.go
+++ b/backend/component/state/state.go
@@ -9,9 +9,6 @@ import (
"github.com/pkg/errors"
)
-// defaultInstanceMaximumConnections is the maximum number of connections outstanding per instance by default.
-const defaultInstanceMaximumConnections = 10
-
// State is the state for all in-memory states within the server.
type State struct {
// ApprovalFinding is the set of issues for finding the approval template.
@@ -78,22 +75,25 @@ type resourceLimiter struct {
connections map[string]int
}
-func (c *resourceLimiter) Increment(instanceID string, maxConnections int) bool {
+// limit <= 0 means no limit.
+func (c *resourceLimiter) Increment(key string, limit int) bool {
c.Lock()
defer c.Unlock()
- if maxConnections == 0 {
- maxConnections = defaultInstanceMaximumConnections
+ if limit <= 0 {
+ // No limit.
+ // Increment anyway to balance the decrement.
+ c.connections[key]++
+ return false
}
-
- if c.connections[instanceID] >= maxConnections {
+ if c.connections[key] >= limit {
return true
}
- c.connections[instanceID]++
+ c.connections[key]++
return false
}
-func (c *resourceLimiter) Decrement(instanceID string) {
+func (c *resourceLimiter) Decrement(key string) {
c.Lock()
defer c.Unlock()
- c.connections[instanceID]--
+ c.connections[key]--
}
diff --git a/backend/runner/taskrun/schedulerv2.go b/backend/runner/taskrun/schedulerv2.go
index 9b95cc300b..6f0ead834e 100644
--- a/backend/runner/taskrun/schedulerv2.go
+++ b/backend/runner/taskrun/schedulerv2.go
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log/slog"
+ "strconv"
"sync"
"time"
@@ -28,6 +29,13 @@ const (
taskSchedulerInterval = 5 * time.Second
)
+// defaultInstanceMaximumConnections is the maximum number of connections outstanding per instance by default.
+const defaultInstanceMaximumConnections = 10
+
+// defaultRolloutMaxRunningTaskRuns is the maximum number of running tasks per rollout.
+// No limit by default.
+const defaultRolloutMaxRunningTaskRuns = 0
+
// SchedulerV2 is the V2 scheduler for task run.
type SchedulerV2 struct {
store *store.Store
@@ -425,7 +433,12 @@ func (s *SchedulerV2) scheduleRunningTaskRuns(ctx context.Context) error {
)
continue
}
+
+ // Check max connections per instance.
maximumConnections := int(instance.Metadata.GetMaximumConnections())
+ if maximumConnections <= 0 {
+ maximumConnections = defaultInstanceMaximumConnections
+ }
if s.stateCfg.InstanceOutstandingConnections.Increment(task.InstanceID, maximumConnections) {
s.stateCfg.TaskRunSchedulerInfo.Store(taskRun.ID, &storepb.SchedulerInfo{
ReportTime: timestamppb.Now(),
@@ -438,6 +451,40 @@ func (s *SchedulerV2) scheduleRunningTaskRuns(ctx context.Context) error {
continue
}
+ // Check max running task runs per rollout.
+ pipeline, err := s.store.GetPipelineV2ByID(ctx, task.PipelineID)
+ if err != nil {
+ return errors.Wrapf(err, "failed to get pipeline")
+ }
+ if pipeline == nil {
+ return errors.Errorf("pipeline %v not found", task.PipelineID)
+ }
+
+ project, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{ResourceID: &pipeline.ProjectID})
+ if err != nil {
+ return errors.Wrapf(err, "failed to get project")
+ }
+ if project == nil {
+ return errors.Errorf("project %v not found", pipeline.ProjectID)
+ }
+
+ rolloutID := strconv.Itoa(pipeline.ID)
+ maxRunningTaskRunsPerRollout := int(project.Setting.GetParallelTasksPerRollout())
+ if maxRunningTaskRunsPerRollout <= 0 {
+ maxRunningTaskRunsPerRollout = defaultRolloutMaxRunningTaskRuns
+ }
+ if s.stateCfg.RolloutOutstandingTasks.Increment(rolloutID, maxRunningTaskRunsPerRollout) {
+ s.stateCfg.TaskRunSchedulerInfo.Store(taskRun.ID, &storepb.SchedulerInfo{
+ ReportTime: timestamppb.Now(),
+ WaitingCause: &storepb.SchedulerInfo_WaitingCause{
+ Cause: &storepb.SchedulerInfo_WaitingCause_ParallelTasksLimit{
+ ParallelTasksLimit: true,
+ },
+ },
+ })
+ continue
+ }
+
s.stateCfg.TaskRunSchedulerInfo.Delete(taskRun.ID)
s.stateCfg.RunningTaskRuns.Store(taskRun.ID, true)
@@ -474,6 +521,7 @@ func (s *SchedulerV2) runTaskRunOnce(ctx context.Context, taskRun *store.TaskRun
s.stateCfg.RunningDatabaseMigration.Delete(getDatabaseKey(task.InstanceID, *task.DatabaseName))
}
s.stateCfg.InstanceOutstandingConnections.Decrement(task.InstanceID)
+ s.stateCfg.RolloutOutstandingTasks.Decrement(strconv.Itoa(task.PipelineID))
}()
driverCtx, cancel := context.WithCancel(ctx)