-
Notifications
You must be signed in to change notification settings - Fork 978
Improve the performance of karmada-scheduler #6597
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: master
Are you sure you want to change the base?
Improve the performance of karmada-scheduler #6597
Conversation
Signed-off-by: LivingCcj <[email protected]>
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @LivingCcj, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces a significant performance optimization to the Karmada scheduler's replica assignment process. The core change involves pre-calculating the available replicas for clusters once during the initial cluster selection phase and then passing this pre-computed information throughout the subsequent scheduling steps. This eliminates redundant recalculations of available replicas, leading to improved scheduling efficiency, particularly for workloads requiring replica distribution.
Highlights
- Performance Optimization: The Karmada scheduler now calculates
availableReplicas
for clusters only once during the initial cluster selection phase. This pre-computed data is then reused in subsequent replica assignment algorithms, such asdynamicScaleUp
anddynamicFreshScale
, eliminating redundant computations and significantly improving scheduling efficiency. - New Data Structure for Efficiency: A new struct,
spreadconstraint.ClusterAvailableReplicas
, has been introduced. This struct encapsulates a cluster along with its pre-calculated available replicas, streamlining data flow and ensuring that this optimized information is consistently passed through the scheduler's core components. - Refactored Data Flow: Function signatures across key scheduling components (
pkg/scheduler/core/assignment.go
,pkg/scheduler/core/common.go
,pkg/scheduler/core/division_algorithm.go
,pkg/scheduler/core/generic_scheduler.go
, andpkg/scheduler/core/spreadconstraint/select_clusters.go
) have been updated to accept and return[]spreadconstraint.ClusterAvailableReplicas
instead of raw cluster lists, ensuring the optimized data is utilized throughout the scheduling process. - Comprehensive Test Updates: Corresponding test files (
assignment_test.go
,common_test.go
,generic_scheduler_test.go
,util_test.go
) have been updated to align with the changes in function signatures and data structures, maintaining robust test coverage and correctness for the new optimized flow.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Optimizes the Karmada scheduler by reducing redundant calculations of available replicas from multiple invocations to a single calculation, improving scheduler performance. The optimization introduces a new ClusterAvailableReplicas
type to pass pre-calculated available replica information throughout the scheduling pipeline.
- Introduces
ClusterAvailableReplicas
struct to store cluster and available replicas together - Refactors scheduler pipeline to pass available replicas instead of recalculating them
- Updates function signatures throughout the scheduler to use the new type
Reviewed Changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 6 comments.
Show a summary per file
File | Description |
---|---|
pkg/scheduler/core/spreadconstraint/group_clusters.go | Adds ClusterAvailableReplicas struct and populates it during cluster info generation |
pkg/scheduler/core/spreadconstraint/select_clusters.go | Updates SelectBestClusters to return ClusterAvailableReplicas instead of raw clusters |
pkg/scheduler/core/generic_scheduler.go | Updates scheduler pipeline to use ClusterAvailableReplicas throughout |
pkg/scheduler/core/common.go | Updates SelectClusters and AssignReplicas signatures to use ClusterAvailableReplicas |
pkg/scheduler/core/assignment.go | Updates assignment state to store and use ClusterAvailableReplicas |
pkg/scheduler/core/division_algorithm.go | Modifies scale up operations to use pre-calculated replicas |
pkg/scheduler/core/util.go | Updates attachZeroReplicasCluster to work with ClusterAvailableReplicas |
pkg/scheduler/core/*_test.go | Updates test cases to use new ClusterAvailableReplicas type |
return nil, fmt.Errorf("failed to select clusters by spread constraints: %w", err) | ||
} | ||
var clusters []ClusterAvailableReplicas | ||
for i, _ := range clusterList { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using blank identifier for index in range loop is unnecessary. Use for i := range clusterList
instead.
for i, _ := range clusterList { | |
for i := range clusterList { |
Copilot uses AI. Check for mistakes.
} | ||
var clusters []ClusterAvailableReplicas | ||
for i, _ := range clusterList { | ||
for j, _ := range groupClustersInfo.Clusters { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using blank identifier for index in range loop is unnecessary. Use for j := range groupClustersInfo.Clusters
instead.
for j, _ := range groupClustersInfo.Clusters { | |
for j := range groupClustersInfo.Clusters { |
Copilot uses AI. Check for mistakes.
for i, _ := range clusterList { | ||
for j, _ := range groupClustersInfo.Clusters { | ||
if groupClustersInfo.Clusters[j].Name == clusterList[i].Name { | ||
clusters = append(clusters, groupClustersInfo.Clusters[j].ClusterAvailableReplicas) | ||
break | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This nested loop has O(n*m) complexity which could be inefficient for large cluster lists. Consider using a map to store clusters by name for O(1) lookup instead of the inner loop.
for i, _ := range clusterList { | |
for j, _ := range groupClustersInfo.Clusters { | |
if groupClustersInfo.Clusters[j].Name == clusterList[i].Name { | |
clusters = append(clusters, groupClustersInfo.Clusters[j].ClusterAvailableReplicas) | |
break | |
} | |
// Build a map for O(1) lookup by cluster name | |
clusterMap := make(map[string]ClusterAvailableReplicas, len(groupClustersInfo.Clusters)) | |
for _, c := range groupClustersInfo.Clusters { | |
clusterMap[c.Name] = c.ClusterAvailableReplicas | |
} | |
for _, cl := range clusterList { | |
if car, ok := clusterMap[cl.Name]; ok { | |
clusters = append(clusters, car) |
Copilot uses AI. Check for mistakes.
@@ -119,8 +119,14 @@ func dynamicScaleDown(state *assignState) ([]workv1alpha2.TargetCluster, error) | |||
func dynamicScaleUp(state *assignState) ([]workv1alpha2.TargetCluster, error) { | |||
// Target is the extra ones. | |||
state.targetReplicas = state.spec.Replicas - state.assignedReplicas | |||
state.buildAvailableClusters(func(clusters []*clusterv1alpha1.Cluster, spec *workv1alpha2.ResourceBindingSpec) []workv1alpha2.TargetCluster { | |||
clusterAvailableReplicas := calAvailableReplicas(clusters, spec) | |||
state.buildAvailableClusters(func(_ []*clusterv1alpha1.Cluster, spec *workv1alpha2.ResourceBindingSpec) []workv1alpha2.TargetCluster { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first parameter _ []*clusterv1alpha1.Cluster
is now unused due to the optimization. Consider updating the function signature to remove this parameter entirely.
state.buildAvailableClusters(func(_ []*clusterv1alpha1.Cluster, spec *workv1alpha2.ResourceBindingSpec) []workv1alpha2.TargetCluster { | |
state.buildAvailableClusters(func(spec *workv1alpha2.ResourceBindingSpec) []workv1alpha2.TargetCluster { |
Copilot uses AI. Check for mistakes.
pkg/scheduler/core/assignment.go
Outdated
@@ -84,11 +85,18 @@ type assignState struct { | |||
|
|||
// targetReplicas is the replicas that we need to schedule in this round | |||
targetReplicas int32 | |||
|
|||
//availableClusterReplicas is the available replicas of candidate clusters |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment should start with the field name and use proper capitalization: // availableClusterReplicas is the available replicas of candidate clusters
.
//availableClusterReplicas is the available replicas of candidate clusters | |
// availableClusterReplicas is the available replicas of candidate clusters |
Copilot uses AI. Check for mistakes.
@@ -344,6 +352,7 @@ func (info *GroupClustersInfo) generateClustersInfo(clustersScore framework.Clus | |||
for i, clustersReplica := range clustersReplicas { | |||
info.Clusters[i].AvailableReplicas = int64(clustersReplica.Replicas) | |||
info.Clusters[i].AvailableReplicas += int64(rbSpec.AssignedReplicasForCluster(clustersReplica.Name)) | |||
info.Clusters[i].ClusterAvailableReplicas.Replicas = int64(clustersReplica.Replicas) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The available replicas calculation is inconsistent. Line 354 adds assigned replicas to available replicas for AvailableReplicas
field, but line 355 only sets the base replicas for ClusterAvailableReplicas.Replicas
. This could lead to incorrect replica calculations.
info.Clusters[i].ClusterAvailableReplicas.Replicas = int64(clustersReplica.Replicas) | |
info.Clusters[i].ClusterAvailableReplicas.Replicas = int64(clustersReplica.Replicas) + int64(rbSpec.AssignedReplicasForCluster(clustersReplica.Name)) |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
The pull request optimizes the Karmada scheduler by reducing the number of times availableRelicas
is calculated, aiming to improve the scheduler's performance. The changes involve modifying the assignState
struct, updating the SelectClusters
and AssignReplicas
functions to work with ClusterAvailableReplicas
, and adjusting the replica division algorithms. There are opportunities to further optimize the code by pre-allocating slices, avoiding unnecessary data structure conversions, and using more efficient data structures for cluster lookups.
var clusters []ClusterAvailableReplicas | ||
for i, _ := range clusterList { | ||
for j, _ := range groupClustersInfo.Clusters { | ||
if groupClustersInfo.Clusters[j].Name == clusterList[i].Name { | ||
clusters = append(clusters, groupClustersInfo.Clusters[j].ClusterAvailableReplicas) | ||
break | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The nested loops to find the corresponding ClusterAvailableReplicas
based on the cluster name have a time complexity of O(n*m), where n is the number of clusters in clusterList
and m is the number of clusters in groupClustersInfo.Clusters
. For a large number of clusters, this could become a performance bottleneck. Consider using a map to store groupClustersInfo.Clusters
with the cluster name as the key for faster lookup.
var clusters []ClusterAvailableReplicas | |
for i, _ := range clusterList { | |
for j, _ := range groupClustersInfo.Clusters { | |
if groupClustersInfo.Clusters[j].Name == clusterList[i].Name { | |
clusters = append(clusters, groupClustersInfo.Clusters[j].ClusterAvailableReplicas) | |
break | |
} | |
} | |
} | |
clusterMap := make(map[string]ClusterAvailableReplicas) | |
for _, cluster := range groupClustersInfo.Clusters { | |
clusterMap[cluster.Name] = cluster.ClusterAvailableReplicas | |
} | |
var clusters []ClusterAvailableReplicas | |
for _, cluster := range clusterList { | |
if availableCluster, ok := clusterMap[cluster.Name]; ok { | |
clusters = append(clusters, availableCluster) | |
} | |
} |
func newAssignState(clusterAvailableReplicas []spreadconstraint.ClusterAvailableReplicas, spec *workv1alpha2.ResourceBindingSpec, | ||
status *workv1alpha2.ResourceBindingStatus) *assignState { | ||
var strategyType string | ||
var candidates []*clusterv1alpha1.Cluster | ||
for _, cluster := range clusterAvailableReplicas { | ||
candidates = append(candidates, cluster.Cluster) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This loop iterates through clusterAvailableReplicas
to populate the candidates
slice. It would be more efficient to pre-allocate the candidates
slice with the known size of clusterAvailableReplicas
to avoid multiple reallocations as the slice grows. This can improve performance, especially when dealing with a large number of clusters.
candidates := make([]*clusterv1alpha1.Cluster, len(clusterAvailableReplicas))
for i, cluster := range clusterAvailableReplicas {
candidates[i] = cluster.Cluster
}
for i, cluster := range clusters { | ||
targetClusters[i] = workv1alpha2.TargetCluster{Name: cluster.Name} | ||
targetClusters[i] = workv1alpha2.TargetCluster{Name: cluster.Cluster.Name} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
state.buildAvailableClusters(func(_ []*clusterv1alpha1.Cluster, spec *workv1alpha2.ResourceBindingSpec) []workv1alpha2.TargetCluster { | ||
var clusterAvailableReplicas []workv1alpha2.TargetCluster | ||
for _, cluster := range state.availableClusterReplicas { | ||
var targetCluster workv1alpha2.TargetCluster | ||
targetCluster.Name = cluster.Cluster.Name | ||
targetCluster.Replicas = int32(cluster.Replicas) | ||
clusterAvailableReplicas = append(clusterAvailableReplicas, targetCluster) | ||
} | ||
sort.Sort(TargetClustersList(clusterAvailableReplicas)) | ||
return clusterAvailableReplicas | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The anonymous function passed to state.buildAvailableClusters
iterates through state.availableClusterReplicas
to create a clusterAvailableReplicas
slice. This logic duplicates the cluster name and replicas. Consider refactoring to directly use the state.availableClusterReplicas
data structure, avoiding the creation of an intermediate slice.
state.buildAvailableClusters(func(_ []*clusterv1alpha1.Cluster, spec *workv1alpha2.ResourceBindingSpec) []workv1alpha2.TargetCluster { | |
var clusterAvailableReplicas []workv1alpha2.TargetCluster | |
for _, cluster := range state.availableClusterReplicas { | |
var targetCluster workv1alpha2.TargetCluster | |
targetCluster.Name = cluster.Cluster.Name | |
targetCluster.Replicas = int32(cluster.Replicas) | |
clusterAvailableReplicas = append(clusterAvailableReplicas, targetCluster) | |
} | |
sort.Sort(TargetClustersList(clusterAvailableReplicas)) | |
return clusterAvailableReplicas | |
}) | |
state.buildAvailableClusters(func(_ []*clusterv1alpha1.Cluster, spec *workv1alpha2.ResourceBindingSpec) []workv1alpha2.TargetCluster { | |
clusterAvailableReplicas := make([]workv1alpha2.TargetCluster, len(state.availableClusterReplicas)) | |
for i, cluster := range state.availableClusterReplicas { | |
clusterAvailableReplicas[i] = workv1alpha2.TargetCluster{ | |
Name: cluster.Cluster.Name, | |
Replicas: int32(cluster.Replicas), | |
} | |
} | |
sort.Sort(TargetClustersList(clusterAvailableReplicas)) | |
return clusterAvailableReplicas | |
}) |
Signed-off-by: LivingCcj <[email protected]>
What type of PR is this?
/kind feature
-->
What this PR does / why we need it:
The PR
Which issue(s) this PR fixes:
related to issue #5202
Special notes for your reviewer:
Does this PR introduce a user-facing change?: