Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

LivingCcj
Copy link
Contributor

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?:

karmada-schedule: optimize the scheduler step,  invoking calculate `availableRelicas` is reduced to once, improve the performance of karmada-schedule

@Copilot Copilot AI review requested due to automatic review settings August 3, 2025 12:42
@karmada-bot karmada-bot added the kind/feature Categorizes issue or PR as related to a new feature. label Aug 3, 2025
@karmada-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign xishanyongye-chang for approval. For more information see the Kubernetes Code Review Process.

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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@karmada-bot karmada-bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 3, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a 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 as dynamicScaleUp and dynamicFreshScale, 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, and pkg/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

  1. 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.

Copy link
Contributor

@Copilot Copilot AI left a 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 {
Copy link
Preview

Copilot AI Aug 3, 2025

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.

Suggested change
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 {
Copy link
Preview

Copilot AI Aug 3, 2025

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.

Suggested change
for j, _ := range groupClustersInfo.Clusters {
for j := range groupClustersInfo.Clusters {

Copilot uses AI. Check for mistakes.

Comment on lines 48 to 53
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
}
Copy link
Preview

Copilot AI Aug 3, 2025

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.

Suggested change
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 {
Copy link
Preview

Copilot AI Aug 3, 2025

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.

Suggested change
state.buildAvailableClusters(func(_ []*clusterv1alpha1.Cluster, spec *workv1alpha2.ResourceBindingSpec) []workv1alpha2.TargetCluster {
state.buildAvailableClusters(func(spec *workv1alpha2.ResourceBindingSpec) []workv1alpha2.TargetCluster {

Copilot uses AI. Check for mistakes.

@@ -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
Copy link
Preview

Copilot AI Aug 3, 2025

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.

Suggested change
//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)
Copy link
Preview

Copilot AI Aug 3, 2025

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.

Suggested change
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.

@LivingCcj LivingCcj changed the title optimize karmada scheduler step Improve the performance of karmada-scheduler Aug 3, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines 47 to 55
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
}
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
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)
}
}

Comment on lines +93 to +99
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)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
}

Comment on lines 70 to +71
for i, cluster := range clusters {
targetClusters[i] = workv1alpha2.TargetCluster{Name: cluster.Name}
targetClusters[i] = workv1alpha2.TargetCluster{Name: cluster.Cluster.Name}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Accessing the Cluster field within the loop introduces an extra level of indirection. It might be beneficial to store the cluster name directly when creating the targetClusters slice to avoid this indirection during access.

targetClusters[i] = workv1alpha2.TargetCluster{Name: cluster.Cluster.Name}

Comment on lines 122 to 132
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
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants