Skip to content

Commit 3255438

Browse files
committed
adjust the Priority of General and Accurate Estimator of Karmada Scheduler
Signed-off-by: chaosi-zju <[email protected]>
1 parent 628cb95 commit 3255438

File tree

4 files changed

+21
-31
lines changed

4 files changed

+21
-31
lines changed

pkg/estimator/client/accurate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232

3333
// RegisterSchedulerEstimator will register a SchedulerEstimator.
3434
func RegisterSchedulerEstimator(se *SchedulerEstimator) {
35-
replicaEstimators["scheduler-estimator"] = se
35+
replicaEstimators = append(replicaEstimators, se)
3636
unschedulableReplicaEstimators["scheduler-estimator"] = se
3737
}
3838

@@ -68,7 +68,7 @@ func (se *SchedulerEstimator) MaxAvailableReplicas(
6868
}
6969

7070
func (se *SchedulerEstimator) GetPriority() EstimatorPriority {
71-
return High
71+
return Accurate
7272
}
7373

7474
// GetUnschedulableReplicas gets the unschedulable replicas which belong to a specified workload by calling karmada-scheduler-estimator.

pkg/estimator/client/general.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232

3333
// GeneralEstimator is the default replica estimator.
3434
func init() {
35-
replicaEstimators["general-estimator"] = NewGeneralEstimator()
35+
replicaEstimators = append(replicaEstimators, NewGeneralEstimator())
3636
}
3737

3838
// GeneralEstimator is a normal estimator in terms of cluster ResourceSummary.
@@ -54,7 +54,7 @@ func (ge *GeneralEstimator) MaxAvailableReplicas(_ context.Context, clusters []*
5454
}
5555

5656
func (ge *GeneralEstimator) GetPriority() EstimatorPriority {
57-
return Middle
57+
return General
5858
}
5959

6060
func (ge *GeneralEstimator) maxAvailableReplicas(cluster *clusterv1alpha1.Cluster, replicaRequirements *workv1alpha2.ReplicaRequirements) int32 {

pkg/estimator/client/interface.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
const UnauthenticReplica = -1
3131

3232
var (
33-
replicaEstimators = map[string]ReplicaEstimator{}
33+
replicaEstimators []ReplicaEstimator
3434
unschedulableReplicaEstimators = map[string]UnschedulableReplicaEstimator{}
3535
)
3636

@@ -46,7 +46,7 @@ type UnschedulableReplicaEstimator interface {
4646
}
4747

4848
// GetReplicaEstimators returns all replica estimators.
49-
func GetReplicaEstimators() map[string]ReplicaEstimator {
49+
func GetReplicaEstimators() []ReplicaEstimator {
5050
return replicaEstimators
5151
}
5252

@@ -59,11 +59,10 @@ func GetUnschedulableReplicaEstimators() map[string]UnschedulableReplicaEstimato
5959
// 1. Higher-priority estimator can directly override the result given by lower-priority estimator
6060
// 2. Lower replicas estimated will be chosen if the priority of estimator are same
6161
// 3. Lower-priority estimator would be ignored if the result was already given by higher-priority estimator
62-
type EstimatorPriority int
62+
type EstimatorPriority int32
6363

6464
const (
65-
Undefined EstimatorPriority = iota
66-
Low
67-
Middle
68-
High
65+
Rough EstimatorPriority = 100
66+
General EstimatorPriority = 200
67+
Accurate EstimatorPriority = 300
6968
)

pkg/scheduler/core/util.go

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"math"
23+
"sort"
2324

2425
"k8s.io/apimachinery/pkg/util/sets"
2526
"k8s.io/klog/v2"
@@ -53,22 +54,13 @@ func getDefaultWeightPreference(clusters []*clusterv1alpha1.Cluster) *policyv1al
5354

5455
func calAvailableReplicas(clusters []*clusterv1alpha1.Cluster, spec *workv1alpha2.ResourceBindingSpec) []workv1alpha2.TargetCluster {
5556
availableTargetClusters := make([]workv1alpha2.TargetCluster, len(clusters))
56-
// availableResultPriority indicates which priority Estimator gives the scheduling result for each cluster, respectively
57-
// 1. Higher-priority estimator can directly override the result given by lower-priority estimator
58-
// 2. Lower replicas estimated will be chosen if the priority of estimator are same
59-
// 3. Lower-priority estimator would be ignored if the result was already given by higher-priority estimator
60-
availableResultPriority := make([]estimatorclient.EstimatorPriority, len(clusters))
6157

6258
// Set the boundary.
6359
for i := range availableTargetClusters {
6460
availableTargetClusters[i].Name = clusters[i].Name
6561
availableTargetClusters[i].Replicas = math.MaxInt32
6662
}
6763

68-
for i := range availableResultPriority {
69-
availableResultPriority[i] = estimatorclient.Undefined
70-
}
71-
7264
// For non-workload, like ServiceAccount, ConfigMap, Secret and etc, it's unnecessary to calculate available replicas in member clusters.
7365
// See issue: https://github.com/karmada-io/karmada/issues/3743.
7466
if spec.Replicas == 0 {
@@ -79,30 +71,29 @@ func calAvailableReplicas(clusters []*clusterv1alpha1.Cluster, spec *workv1alpha
7971

8072
// Get the minimum value of MaxAvailableReplicas in terms of all estimators.
8173
estimators := estimatorclient.GetReplicaEstimators()
74+
sort.Slice(estimators, func(i, j int) bool {
75+
return estimators[i].GetPriority() > estimators[j].GetPriority()
76+
})
77+
78+
priorityOfAvailableEstimator := estimatorclient.EstimatorPriority(math.MinInt32)
8279
ctx := context.WithValue(context.TODO(), util.ContextKeyObject,
8380
fmt.Sprintf("kind=%s, name=%s/%s", spec.Resource.Kind, spec.Resource.Namespace, spec.Resource.Name))
8481
for _, estimator := range estimators {
82+
if estimator.GetPriority() < priorityOfAvailableEstimator {
83+
break
84+
}
8585
res, err := estimator.MaxAvailableReplicas(ctx, clusters, spec.ReplicaRequirements)
8686
if err != nil {
8787
klog.Errorf("Max cluster available replicas error: %v", err)
8888
continue
8989
}
90+
priorityOfAvailableEstimator = estimator.GetPriority()
9091
for i := range res {
91-
if res[i].Name != availableTargetClusters[i].Name {
92-
continue
93-
}
9492
if res[i].Replicas == estimatorclient.UnauthenticReplica {
9593
continue
9694
}
97-
// means there are already replicas calculated by higher-priority estimator
98-
if estimator.GetPriority() < availableResultPriority[i] {
99-
continue
100-
}
101-
// override if current estimator has higher priority and take lower replicas is estimator priority are same
102-
if (estimator.GetPriority() == availableResultPriority[i] && res[i].Replicas < availableTargetClusters[i].Replicas) ||
103-
estimator.GetPriority() > availableResultPriority[i] {
95+
if availableTargetClusters[i].Name == res[i].Name && availableTargetClusters[i].Replicas > res[i].Replicas {
10496
availableTargetClusters[i].Replicas = res[i].Replicas
105-
availableResultPriority[i] = estimator.GetPriority()
10697
}
10798
}
10899
}

0 commit comments

Comments
 (0)