Skip to content

Commit

Permalink
fix: update the calc of group score
Browse files Browse the repository at this point in the history
Signed-off-by: ipsum-0320 <[email protected]>
  • Loading branch information
ipsum-0320 committed Oct 16, 2024
1 parent f846d9a commit 096df98
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
38 changes: 24 additions & 14 deletions pkg/scheduler/core/spreadconstraint/group_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ limitations under the License.
package spreadconstraint

import (
"k8s.io/utils/ptr"

clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/scheduler/framework"
"k8s.io/utils/ptr"
"math"
)

// GroupClustersInfo indicate the cluster global view
Expand Down Expand Up @@ -109,9 +109,9 @@ func groupClustersBasedTopology(
}
groupClustersInfo.calAvailableReplicasFunc = calAvailableReplicasFunc
groupClustersInfo.generateClustersInfo(clustersScore, rbSpec)
groupClustersInfo.generateZoneInfo(spreadConstraints, 10)
groupClustersInfo.generateRegionInfo(spreadConstraints, 10)
groupClustersInfo.generateProviderInfo(spreadConstraints, 10)
groupClustersInfo.generateZoneInfo(spreadConstraints)
groupClustersInfo.generateRegionInfo(spreadConstraints)
groupClustersInfo.generateProviderInfo(spreadConstraints)

return groupClustersInfo
}
Expand All @@ -132,12 +132,22 @@ func (info *GroupClustersInfo) calcWight(clusters ClusterDetailInfo) int64 {
return clusters.AvailableReplicas
}

func (info *GroupClustersInfo) calcGroupScore(clusters []ClusterDetailInfo, betaScore int64) int64 {
func (info *GroupClustersInfo) calcGroupScore(clusters []ClusterDetailInfo) int64 {
// Group Score = sum(Cluster Score × Weight)
var score int64
for _, cluster := range clusters {
// avoid the score is 0
score += (cluster.Score + betaScore) * info.calcWight(cluster)
// cluster.Score == 100 or cluster.Score == 0
weight := info.calcWight(cluster)
// check, Avoid integer out of bounds.
if weight > math.MaxInt64/100 {
weight = math.MaxInt64
} else {
weight = weight * 100
}
if score > math.MaxInt64-(cluster.Score+weight) {
return math.MaxInt64
}
score += cluster.Score + weight
}
return score / int64(len(clusters))
}
Expand Down Expand Up @@ -167,7 +177,7 @@ func (info *GroupClustersInfo) generateClustersInfo(clustersScore framework.Clus
})
}

func (info *GroupClustersInfo) generateZoneInfo(spreadConstraints []policyv1alpha1.SpreadConstraint, betaScore int64) {
func (info *GroupClustersInfo) generateZoneInfo(spreadConstraints []policyv1alpha1.SpreadConstraint) {
if !IsSpreadConstraintExisted(spreadConstraints, policyv1alpha1.SpreadByFieldZone) {
return
}
Expand All @@ -193,12 +203,12 @@ func (info *GroupClustersInfo) generateZoneInfo(spreadConstraints []policyv1alph
}

for zone, zoneInfo := range info.Zones {
zoneInfo.Score = info.calcGroupScore(zoneInfo.Clusters, betaScore)
zoneInfo.Score = info.calcGroupScore(zoneInfo.Clusters)
info.Zones[zone] = zoneInfo
}
}

func (info *GroupClustersInfo) generateRegionInfo(spreadConstraints []policyv1alpha1.SpreadConstraint, betaScore int64) {
func (info *GroupClustersInfo) generateRegionInfo(spreadConstraints []policyv1alpha1.SpreadConstraint) {
if !IsSpreadConstraintExisted(spreadConstraints, policyv1alpha1.SpreadByFieldRegion) {
return
}
Expand Down Expand Up @@ -227,12 +237,12 @@ func (info *GroupClustersInfo) generateRegionInfo(spreadConstraints []policyv1al
}

for region, regionInfo := range info.Regions {
regionInfo.Score = info.calcGroupScore(regionInfo.Clusters, betaScore)
regionInfo.Score = info.calcGroupScore(regionInfo.Clusters)
info.Regions[region] = regionInfo
}
}

func (info *GroupClustersInfo) generateProviderInfo(spreadConstraints []policyv1alpha1.SpreadConstraint, betaScore int64) {
func (info *GroupClustersInfo) generateProviderInfo(spreadConstraints []policyv1alpha1.SpreadConstraint) {
if !IsSpreadConstraintExisted(spreadConstraints, policyv1alpha1.SpreadByFieldProvider) {
return
}
Expand Down Expand Up @@ -267,7 +277,7 @@ func (info *GroupClustersInfo) generateProviderInfo(spreadConstraints []policyv1
}

for provider, providerInfo := range info.Providers {
providerInfo.Score = info.calcGroupScore(providerInfo.Clusters, betaScore)
providerInfo.Score = info.calcGroupScore(providerInfo.Clusters)
info.Providers[provider] = providerInfo
}
}
Expand Down
26 changes: 13 additions & 13 deletions pkg/scheduler/core/spreadconstraint/group_clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func generateArgs() []GroupScoreArgs {
id: 2,
clusters1: generateClusterScores(3, []int64{100, 100, 100}, []int64{630141, 421020, 507178}),
clusters2: generateClusterScores(3, []int64{0, 0, 0}, []int64{448783, 963163, 693666}),
want: true,
want: false,
},
{
id: 3,
Expand Down Expand Up @@ -301,19 +301,19 @@ func generateArgs() []GroupScoreArgs {
id: 10,
clusters1: generateClusterScores(5, []int64{100, 100, 100, 100, 100}, []int64{568, 48, 396, 403, 219}),
clusters2: generateClusterScores(5, []int64{100, 100, 0, 0, 0}, []int64{794, 466, 401, 56, 579}),
want: true,
want: false,
},
{
id: 11,
clusters1: generateClusterScores(5, []int64{100, 100, 0, 0, 0}, []int64{812, 673, 290, 418, 546}),
clusters2: generateClusterScores(5, []int64{100, 0, 0, 0, 0}, []int64{759, 834, 621, 237, 490}),
want: true,
want: false,
},
{
id: 12,
clusters1: generateClusterScores(3, []int64{0, 0, 0}, []int64{142, 658, 913}),
clusters2: generateClusterScores(3, []int64{100, 100, 100}, []int64{84, 297, 466}),
want: false,
want: true,
},
{
id: 13,
Expand All @@ -331,7 +331,7 @@ func generateArgs() []GroupScoreArgs {
id: 15,
clusters1: generateClusterScores(2, []int64{100, 0}, []int64{520, 810}),
clusters2: generateClusterScores(2, []int64{100, 0}, []int64{630, 700}),
want: false,
want: true,
},
{
id: 16,
Expand All @@ -343,7 +343,7 @@ func generateArgs() []GroupScoreArgs {
id: 17,
clusters1: generateClusterScores(5, []int64{100, 100, 0, 0, 0}, []int64{372, 461, 550, 639, 728}),
clusters2: generateClusterScores(5, []int64{0, 0, 0, 0, 0}, []int64{817, 906, 995, 184, 273}),
want: true,
want: false,
},
{
id: 18,
Expand All @@ -355,25 +355,25 @@ func generateArgs() []GroupScoreArgs {
id: 19,
clusters1: generateClusterScores(4, []int64{100, 100, 0, 0}, []int64{1020, 2040, 3060, 4080}),
clusters2: generateClusterScores(4, []int64{100, 0, 0, 0}, []int64{1530, 2550, 3570, 4590}),
want: true,
want: false,
},
{
id: 20,
clusters1: generateClusterScores(5, []int64{100, 100, 100, 100, 100}, []int64{15, 30, 45, 60, 75}),
clusters2: generateClusterScores(5, []int64{100, 100, 100, 0, 0}, []int64{20, 35, 50, 65, 80}),
want: true,
want: false,
},
{
id: 21,
clusters1: generateClusterScores(5, []int64{100, 100, 0, 0, 0}, []int64{314, 159, 265, 358, 979}),
clusters2: generateClusterScores(5, []int64{100, 0, 0, 0, 0}, []int64{323, 846, 264, 338, 327}),
want: true,
want: false,
},
{
id: 22,
clusters1: generateClusterScores(3, []int64{100, 100, 100}, []int64{271, 828, 182}),
clusters2: generateClusterScores(3, []int64{0, 0, 0}, []int64{459, 230, 781}),
want: true,
want: false,
},
{
id: 23,
Expand Down Expand Up @@ -415,7 +415,7 @@ func generateArgs() []GroupScoreArgs {
id: 29,
clusters1: generateClusterScores(3, []int64{100, 0, 0}, []int64{211, 706, 798}),
clusters2: generateClusterScores(3, []int64{100, 100, 0}, []int64{214, 808, 651}),
want: false,
want: true,
},
{
id: 30,
Expand All @@ -438,8 +438,8 @@ func Test_CalcGroupScore(t *testing.T) {

for _, tt := range tests {
t.Run("", func(t *testing.T) {
score1 := groupClustersInfo.calcGroupScore(tt.clusters1, 10)
score2 := groupClustersInfo.calcGroupScore(tt.clusters2, 10)
score1 := groupClustersInfo.calcGroupScore(tt.clusters1)
score2 := groupClustersInfo.calcGroupScore(tt.clusters2)
t.Logf("test ID: %v, score1 = %v, score2 = %v, score1 >= score 2 res: %v", tt.id, score1, score2, score1 > score2)
if tt.want != (score1 >= score2) {
t.Errorf("test ID: %v, score1 = %v, score2 = %v, score1 >= score 2 want %v, but res is %v", tt.id, score1, score2, tt.want, score1 > score2)
Expand Down

0 comments on commit 096df98

Please sign in to comment.