-
Notifications
You must be signed in to change notification settings - Fork 6
feat: adding pipeline options #799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 8 commits
36b7d94
ad66112
4a7bc9e
d298e75
54639d1
c2d0fc2
4de31f9
c2f0b56
7a014b2
cc9b6e6
4886d37
3b8cf87
b6a6139
037f74c
811a1c3
91dfe3e
f84ba71
5c6ecae
2871d18
4ee2c6a
532200d
8b2ffa0
b535ec0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ import ( | |
|
|
||
| type FilterWeigherPipeline[RequestType FilterWeigherPipelineRequest] interface { | ||
| // Run the scheduling pipeline with the given request. | ||
| // Call-time options are read from request.GetOptions(). | ||
| Run(request RequestType) (v1alpha1.DecisionResult, error) | ||
| } | ||
|
|
||
|
|
@@ -263,6 +264,10 @@ func (s *filterWeigherPipeline[RequestType]) sortHostsByWeights(weights map[stri | |
|
|
||
| // Evaluate the pipeline and return a list of hosts in order of preference. | ||
| func (p *filterWeigherPipeline[RequestType]) Run(request RequestType) (v1alpha1.DecisionResult, error) { | ||
| opts := request.GetOptions() | ||
| if err := opts.Validate(); err != nil { | ||
| return v1alpha1.DecisionResult{}, err | ||
| } | ||
| slogArgs := request.GetTraceLogArgs() | ||
| slogArgsAny := make([]any, 0, len(slogArgs)) | ||
| for _, arg := range slogArgs { | ||
|
|
@@ -297,6 +302,21 @@ func (p *filterWeigherPipeline[RequestType]) Run(request RequestType) (v1alpha1. | |
| hosts := p.sortHostsByWeights(outWeights) | ||
| traceLog.Info("scheduler: sorted hosts", "hosts", hosts) | ||
|
|
||
| if opts.MaxCandidates > 0 && len(hosts) > opts.MaxCandidates { | ||
| traceLog.Info("scheduler: trimming candidate list", "maxCandidates", opts.MaxCandidates, "before", len(hosts)) | ||
| hosts = hosts[:opts.MaxCandidates] | ||
| // Drop trimmed hosts from outWeights so AggregatedOutWeights stays consistent. | ||
| kept := make(map[string]struct{}, len(hosts)) | ||
| for _, h := range hosts { | ||
| kept[h] = struct{}{} | ||
| } | ||
| for host := range outWeights { | ||
| if _, ok := kept[host]; !ok { | ||
| delete(outWeights, host) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide logging here so we see what's going on. |
||
| // Collect some metrics about the pipeline execution. | ||
| go p.monitor.observePipelineResult(request, hosts) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright SAP SE | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package lib | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "github.com/cobaltcore-dev/cortex/api/v1alpha1" | ||
| ) | ||
|
|
||
| // Options configure the behavior of a single pipeline run at call time. | ||
| // These are distinct from per-step YAML options (FilterWeigherPipelineStepOpts), | ||
| // which are static and set when the pipeline is initialized. | ||
| type Options struct { | ||
| // ReadOnly means the pipeline run does not modify shared scheduling state (reservations, | ||
| // history, inflight records). Concurrent read-only runs are safe under a shared read lock. | ||
| // Note: the controller may still write the Decision status after Run() regardless of this flag. | ||
| ReadOnly bool | ||
| // LockReservations prevents reservation unlocking, e.g. in the capacity filter. | ||
| // Set when finding hosts for new reservations (failover, CR) to see true available capacity. | ||
| LockReservations bool | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be more generic such as
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure I get this comment, probably better to discuss live |
||
| // AssumeEmptyHosts treats all hosts as having no running VMs. | ||
| AssumeEmptyHosts bool | ||
| // IgnoredReservationTypes lists reservation types the capacity filter skips entirely. | ||
| IgnoredReservationTypes []v1alpha1.ReservationType | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we make this a substruct such as |
||
| // MaxCandidates limits the number of hosts returned after weighing. 0 means no limit. | ||
| MaxCandidates int | ||
|
|
||
| // RecordHistory records the placement decision in placement history. | ||
| // Replaces pipeline.Spec.CreateHistory once pipelines consolidate. | ||
| RecordHistory bool | ||
| // CreateInflight creates pessimistic blocking reservations for all returned candidates. | ||
| CreateInflight bool | ||
| } | ||
|
|
||
| // Validate checks for mutually exclusive or inconsistent option combinations. | ||
| func (o Options) Validate() error { | ||
| if o.ReadOnly && o.RecordHistory { | ||
| return errors.New("ReadOnly and RecordHistory are mutually exclusive: read-only runs must not write scheduling history") | ||
| } | ||
| if o.ReadOnly && o.CreateInflight { | ||
| return errors.New("ReadOnly and CreateInflight are mutually exclusive: read-only runs must not create inflight reservations") | ||
| } | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright SAP SE | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package lib | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestOptions_Validate(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| opts Options | ||
| wantErr bool | ||
| }{ | ||
| {"zero value is valid", Options{}, false}, | ||
| {"write run with history", Options{RecordHistory: true}, false}, | ||
| {"write run with inflight", Options{CreateInflight: true}, false}, | ||
| {"read-only run, no side effects", Options{ReadOnly: true}, false}, | ||
| {"ReadOnly + RecordHistory is invalid", Options{ReadOnly: true, RecordHistory: true}, true}, | ||
| {"ReadOnly + CreateInflight is invalid", Options{ReadOnly: true, CreateInflight: true}, true}, | ||
| {"ReadOnly + both invalid", Options{ReadOnly: true, RecordHistory: true, CreateInflight: true}, true}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := tt.opts.Validate() | ||
| if tt.wantErr && err == nil { | ||
| t.Error("expected error, got nil") | ||
| } | ||
| if !tt.wantErr && err != nil { | ||
| t.Errorf("expected no error, got %v", err) | ||
| } | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,7 +7,6 @@ import ( | |||||||||||||||||
| "context" | ||||||||||||||||||
| "encoding/json" | ||||||||||||||||||
| "errors" | ||||||||||||||||||
| "fmt" | ||||||||||||||||||
| "sync" | ||||||||||||||||||
| "time" | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -38,8 +37,9 @@ type FilterWeigherPipelineController struct { | |||||||||||||||||
| // Toolbox shared between all pipeline controllers. | ||||||||||||||||||
| lib.BasePipelineController[lib.FilterWeigherPipeline[api.ExternalSchedulerRequest]] | ||||||||||||||||||
|
|
||||||||||||||||||
| // Mutex to only allow one process at a time | ||||||||||||||||||
| processMu sync.Mutex | ||||||||||||||||||
| // Mutex to only allow one process at a time. | ||||||||||||||||||
| // Read-only runs (opts.ReadOnly == true) acquire a read lock; write runs acquire the full lock. | ||||||||||||||||||
| processMu sync.RWMutex | ||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||
|
|
||||||||||||||||||
| // Monitor to pass down to all pipelines. | ||||||||||||||||||
| Monitor lib.FilterWeigherPipelineMonitor | ||||||||||||||||||
|
|
@@ -54,13 +54,23 @@ func (c *FilterWeigherPipelineController) PipelineType() v1alpha1.PipelineType { | |||||||||||||||||
|
|
||||||||||||||||||
| // Callback executed when kubernetes asks to reconcile a decision resource. | ||||||||||||||||||
| func (c *FilterWeigherPipelineController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||||||||||||||||||
| c.processMu.Lock() | ||||||||||||||||||
| defer c.processMu.Unlock() | ||||||||||||||||||
|
|
||||||||||||||||||
| // Peek at the decision before acquiring the lock so we can choose the right lock type. | ||||||||||||||||||
| // Read-only runs can proceed concurrently; write runs need the exclusive lock. | ||||||||||||||||||
| decision := &v1alpha1.Decision{} | ||||||||||||||||||
| if err := c.Get(ctx, req.NamespacedName, decision); err != nil { | ||||||||||||||||||
| return ctrl.Result{}, client.IgnoreNotFound(err) | ||||||||||||||||||
| } | ||||||||||||||||||
| if c.peekReadOnly(decision) { | ||||||||||||||||||
| c.processMu.RLock() | ||||||||||||||||||
| defer c.processMu.RUnlock() | ||||||||||||||||||
| } else { | ||||||||||||||||||
| c.processMu.Lock() | ||||||||||||||||||
| defer c.processMu.Unlock() | ||||||||||||||||||
| // Re-fetch after acquiring the exclusive lock to see consistent state. | ||||||||||||||||||
| if err := c.Get(ctx, req.NamespacedName, decision); err != nil { | ||||||||||||||||||
| return ctrl.Result{}, client.IgnoreNotFound(err) | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| old := decision.DeepCopy() | ||||||||||||||||||
| if err := c.process(ctx, decision); err != nil { | ||||||||||||||||||
| return ctrl.Result{}, err | ||||||||||||||||||
|
|
@@ -74,13 +84,16 @@ func (c *FilterWeigherPipelineController) Reconcile(ctx context.Context, req ctr | |||||||||||||||||
|
|
||||||||||||||||||
| // Process the decision from the API. Should create and return the updated decision. | ||||||||||||||||||
| func (c *FilterWeigherPipelineController) ProcessNewDecisionFromAPI(ctx context.Context, decision *v1alpha1.Decision) error { | ||||||||||||||||||
| c.processMu.Lock() | ||||||||||||||||||
| defer c.processMu.Unlock() | ||||||||||||||||||
|
|
||||||||||||||||||
| pipelineConf, ok := c.PipelineConfigs[decision.Spec.PipelineRef.Name] | ||||||||||||||||||
| if !ok { | ||||||||||||||||||
| return fmt.Errorf("pipeline %s not configured", decision.Spec.PipelineRef.Name) | ||||||||||||||||||
| // Read-only runs share the cached decision state; no re-fetch needed because they | ||||||||||||||||||
| // don't observe writes from concurrent exclusive-lock runs. | ||||||||||||||||||
| if c.peekReadOnly(decision) { | ||||||||||||||||||
| c.processMu.RLock() | ||||||||||||||||||
| defer c.processMu.RUnlock() | ||||||||||||||||||
| } else { | ||||||||||||||||||
| c.processMu.Lock() | ||||||||||||||||||
| defer c.processMu.Unlock() | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| err := c.process(ctx, decision) | ||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| meta.SetStatusCondition(&decision.Status.Conditions, metav1.Condition{ | ||||||||||||||||||
|
|
@@ -97,9 +110,6 @@ func (c *FilterWeigherPipelineController) ProcessNewDecisionFromAPI(ctx context. | |||||||||||||||||
| Message: "pipeline run succeeded", | ||||||||||||||||||
| }) | ||||||||||||||||||
| } | ||||||||||||||||||
| if pipelineConf.Spec.CreateHistory { | ||||||||||||||||||
| c.upsertHistory(ctx, decision, err) | ||||||||||||||||||
| } | ||||||||||||||||||
| return err | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -166,7 +176,14 @@ func (c *FilterWeigherPipelineController) process(ctx context.Context, decision | |||||||||||||||||
| log.Info("gathered all placement candidates", "numHosts", len(request.Hosts)) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Fill RecordHistory from config if the caller didn't set it. | ||||||||||||||||||
| if !request.Options.RecordHistory { | ||||||||||||||||||
| request.Options.RecordHistory = pipelineConf.Spec.CreateHistory | ||||||||||||||||||
| } | ||||||||||||||||||
| result, err := pipeline.Run(request) | ||||||||||||||||||
| if request.Options.RecordHistory { | ||||||||||||||||||
| c.upsertHistory(ctx, decision, err) | ||||||||||||||||||
| } | ||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
Comment on lines
179
to
+182
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Read-only requests can still write history under The lock choice now depends on Suggested guard result, err := pipeline.Run(request)
- if !request.Options.SkipHistory {
+ if !request.Options.ReadOnly && !request.Options.SkipHistory {
c.upsertHistory(ctx, decision, err)
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| log.Error(err, "failed to run pipeline") | ||||||||||||||||||
| return err | ||||||||||||||||||
|
|
@@ -182,7 +199,19 @@ func (c *FilterWeigherPipelineController) process(ctx context.Context, decision | |||||||||||||||||
| return nil | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // The base controller will delegate the pipeline creation down to this method. | ||||||||||||||||||
| // peekReadOnly determines whether a decision should use a read lock instead of | ||||||||||||||||||
| // the exclusive write lock. Defaults to false (exclusive) on any parse error. | ||||||||||||||||||
| func (c *FilterWeigherPipelineController) peekReadOnly(decision *v1alpha1.Decision) bool { | ||||||||||||||||||
| if decision.Spec.NovaRaw == nil { | ||||||||||||||||||
| return false | ||||||||||||||||||
| } | ||||||||||||||||||
| var request api.ExternalSchedulerRequest | ||||||||||||||||||
| if err := json.Unmarshal(decision.Spec.NovaRaw.Raw, &request); err != nil { | ||||||||||||||||||
| return false | ||||||||||||||||||
| } | ||||||||||||||||||
| return request.Options.ReadOnly | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| func (c *FilterWeigherPipelineController) InitPipeline( | ||||||||||||||||||
| ctx context.Context, | ||||||||||||||||||
| p v1alpha1.Pipeline, | ||||||||||||||||||
|
|
||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.