-
Notifications
You must be signed in to change notification settings - Fork 380
perf: Typed Bucket Scaling #2420
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e5b6efe
perf: Typed Bucket Scaling
ryan-mist b9baca7
abstract qps, bucket size function
ryan-mist ac162d3
cleanup testing
ryan-mist 0593b03
Merge branch 'main' into bucket-scaling
ryan-mist da69de7
update to reflect other PR changes
ryan-mist 13fe9d6
Merge branch 'main' into bucket-scaling
ryan-mist bebdc66
fix rebase
ryan-mist 9374b1a
consts for min/max reconciles & fix GetTypedBucketConfigs arg
ryan-mist 544efcc
Merge branch 'main' into bucket-scaling
ryan-mist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| Copyright The Kubernetes Authors. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package controller | ||
ryan-mist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "context" | ||
| "math" | ||
|
|
||
| "github.com/samber/lo" | ||
|
|
||
| "sigs.k8s.io/karpenter/pkg/operator/options" | ||
| ) | ||
|
|
||
| // cpuCount calculates CPU count in cores from context options (in millicores) | ||
| func cpuCount(ctx context.Context) int { | ||
| return int(math.Ceil(float64(options.FromContext(ctx).CPURequests) / 1000.0)) | ||
| } | ||
|
|
||
| // LinearScaleReconciles calculates maxConcurrentReconciles using linear scaling | ||
| func LinearScaleReconciles(ctx context.Context, minReconciles int, maxReconciles int) int { | ||
| cpuCount := cpuCount(ctx) | ||
| // At 1 core: minReconciles; At 60 cores: maxReconciles | ||
| slope := float64(maxReconciles-minReconciles) / 59.0 | ||
| result := int(slope*float64(cpuCount-1)) + minReconciles | ||
| // Clamp to ensure we stay within bounds | ||
| return lo.Clamp(result, minReconciles, maxReconciles) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| Copyright The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package controller_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
|
|
||
| "sigs.k8s.io/karpenter/pkg/operator/options" | ||
| "sigs.k8s.io/karpenter/pkg/utils/controller" | ||
| ) | ||
|
|
||
| func TestReconciles(t *testing.T) { | ||
| RegisterFailHandler(Fail) | ||
| RunSpecs(t, "Reconciles") | ||
| } | ||
|
|
||
| func contextWithCPURequests(cpuRequests int64) context.Context { | ||
| opts := &options.Options{ | ||
| CPURequests: cpuRequests, | ||
| } | ||
| return opts.ToContext(context.Background()) | ||
| } | ||
|
|
||
| var _ = Describe("Reconciles", func() { | ||
| minReconciles := 10 | ||
| maxReconciles := 1000 | ||
| Context("LinearScaleReconciles Calculations", func() { | ||
| It("should calculate minReconciles for 0.5 CPU core", func() { | ||
| ctx := contextWithCPURequests(0.5 * 1000.0) | ||
| result := controller.LinearScaleReconciles(ctx, minReconciles, maxReconciles) | ||
| Expect(result).To(Equal(minReconciles)) | ||
| }) | ||
| It("should calculate minReconciles for 1 CPU core", func() { | ||
| ctx := contextWithCPURequests(1 * 1000) | ||
| result := controller.LinearScaleReconciles(ctx, minReconciles, maxReconciles) | ||
| Expect(result).To(Equal(minReconciles)) | ||
| }) | ||
| It("should calculate maxReconciles for 60 CPU cores", func() { | ||
| ctx := contextWithCPURequests(60 * 1000) | ||
| result := controller.LinearScaleReconciles(ctx, minReconciles, maxReconciles) | ||
| Expect(result).To(Equal(maxReconciles)) | ||
| }) | ||
| It("should calculate maxReconciles for 100 CPU cores", func() { | ||
| ctx := contextWithCPURequests(100 * 1000) | ||
| result := controller.LinearScaleReconciles(ctx, minReconciles, maxReconciles) | ||
| Expect(result).To(Equal(maxReconciles)) | ||
| }) | ||
| It("should follow the linear scaling formula", func() { | ||
| ctx := contextWithCPURequests(15 * 1000) // 15 cores | ||
| result := controller.LinearScaleReconciles(ctx, minReconciles, maxReconciles) | ||
| // At 15 cores | ||
| // slope = (maxReconciles - minReconciles)/59 = (1000-10)/59 = 990/59 = ~16.78 | ||
| // result = int(slope * (cores - 1)) + minReconciles ~= 16.78 * (15-1) + 10 = 234 + 10 = 244 | ||
| expected := 244 | ||
| Expect(result).To(Equal(expected)) | ||
| }) | ||
| It("should handle fractional CPU cores correctly", func() { | ||
| ctx := contextWithCPURequests(1.5 * 1000.0) // 1.5 cores | ||
| result := controller.LinearScaleReconciles(ctx, minReconciles, maxReconciles) | ||
| // At 2 cores (ceil(1.5)) | ||
| // slope = (maxReconciles - minReconciles)/59 = (1000-10)/59 = 990/59 = ~16.78 | ||
| // result = int(slope * (cores - 1)) + minReconciles ~= 16.78 * (2-1) + 10 = 16 + 10 = 26 | ||
| expected := 26 | ||
| Expect(result).To(Equal(expected)) | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.