-
Notifications
You must be signed in to change notification settings - Fork 919
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose assignReplicas and selectClusters function in scheduler
Signed-off-by: chaosi-zju <[email protected]>
- Loading branch information
1 parent
bf1098b
commit 89a4ed1
Showing
3 changed files
with
153 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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,57 @@ | ||
package core | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
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/core/spreadconstraint" | ||
"github.com/karmada-io/karmada/pkg/scheduler/framework" | ||
"github.com/karmada-io/karmada/pkg/scheduler/metrics" | ||
) | ||
|
||
func SelectClusters(clustersScore framework.ClusterScoreList, | ||
placement *policyv1alpha1.Placement, spec *workv1alpha2.ResourceBindingSpec) ([]*clusterv1alpha1.Cluster, error) { | ||
startTime := time.Now() | ||
defer metrics.ScheduleStep(metrics.ScheduleStepSelect, startTime) | ||
|
||
groupClustersInfo := spreadconstraint.GroupClustersWithScore(clustersScore, placement, spec, calAvailableReplicas) | ||
return spreadconstraint.SelectBestClusters(placement, groupClustersInfo, spec.Replicas) | ||
} | ||
|
||
func AssignReplicas( | ||
clusters []*clusterv1alpha1.Cluster, | ||
placement *policyv1alpha1.Placement, | ||
object *workv1alpha2.ResourceBindingSpec, | ||
) ([]workv1alpha2.TargetCluster, error) { | ||
startTime := time.Now() | ||
defer metrics.ScheduleStep(metrics.ScheduleStepAssignReplicas, startTime) | ||
|
||
if len(clusters) == 0 { | ||
return nil, fmt.Errorf("no clusters available to schedule") | ||
} | ||
|
||
if object.Replicas > 0 { | ||
state := newAssignState(clusters, placement, object) | ||
assignFunc, ok := assignFuncMap[state.strategyType] | ||
if !ok { | ||
// should never happen at present | ||
return nil, fmt.Errorf("unsupported replica scheduling strategy, replicaSchedulingType: %s, replicaDivisionPreference: %s, "+ | ||
"please try another scheduling strategy", placement.ReplicaSchedulingType(), placement.ReplicaScheduling.ReplicaDivisionPreference) | ||
} | ||
assignResults, err := assignFunc(state) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return removeZeroReplicasCluster(assignResults), nil | ||
} | ||
|
||
// If not workload, assign all clusters without considering replicas. | ||
targetClusters := make([]workv1alpha2.TargetCluster, len(clusters)) | ||
for i, cluster := range clusters { | ||
targetClusters[i] = workv1alpha2.TargetCluster{Name: cluster.Name} | ||
} | ||
return targetClusters, nil | ||
} |
This file contains 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 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,92 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
|
||
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/test/helper" | ||
) | ||
|
||
func Test_genericScheduler_AssignReplicas(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
retryTimes int | ||
clusters []*clusterv1alpha1.Cluster | ||
placement *policyv1alpha1.Placement | ||
object *workv1alpha2.ResourceBindingSpec | ||
wants [][]workv1alpha2.TargetCluster | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "replica 3, static weighted 1:1, retry 10 times", | ||
retryTimes: 10, | ||
clusters: []*clusterv1alpha1.Cluster{ | ||
helper.NewCluster(ClusterMember1), | ||
helper.NewCluster(ClusterMember2), | ||
}, | ||
object: &workv1alpha2.ResourceBindingSpec{ | ||
Replicas: 3, | ||
}, | ||
placement: &policyv1alpha1.Placement{ | ||
ReplicaScheduling: &policyv1alpha1.ReplicaSchedulingStrategy{ | ||
ReplicaSchedulingType: policyv1alpha1.ReplicaSchedulingTypeDivided, | ||
ReplicaDivisionPreference: policyv1alpha1.ReplicaDivisionPreferenceWeighted, | ||
WeightPreference: &policyv1alpha1.ClusterPreferences{ | ||
StaticWeightList: []policyv1alpha1.StaticClusterWeight{ | ||
{TargetCluster: policyv1alpha1.ClusterAffinity{ClusterNames: []string{ClusterMember1}}, Weight: 1}, | ||
{TargetCluster: policyv1alpha1.ClusterAffinity{ClusterNames: []string{ClusterMember2}}, Weight: 1}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
wants: [][]workv1alpha2.TargetCluster{ | ||
{ | ||
{Name: ClusterMember1, Replicas: 1}, | ||
{Name: ClusterMember2, Replicas: 2}, | ||
}, | ||
{ | ||
{Name: ClusterMember1, Replicas: 2}, | ||
{Name: ClusterMember2, Replicas: 1}, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := &genericScheduler{} | ||
resultHitCounts := make(map[int]int) | ||
|
||
// retry for multi times, the actual result must within tt.wants, and also cover tt.wants | ||
for i := 0; i < tt.retryTimes; i++ { | ||
got, err := g.assignReplicas(tt.clusters, tt.placement, tt.object) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("AssignReplicas() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if tt.wantErr { | ||
continue | ||
} | ||
hitIdx := -1 | ||
for idx, want := range tt.wants { | ||
if helper.IsScheduleResultEqual(got, want) { | ||
hitIdx = idx | ||
break | ||
} | ||
} | ||
if hitIdx >= 0 { | ||
resultHitCounts[hitIdx]++ | ||
} else { | ||
t.Errorf("AssignReplicas() got = %v, wants %v", got, tt.wants) | ||
return | ||
} | ||
} | ||
|
||
if len(resultHitCounts) < len(tt.wants) { | ||
t.Errorf("want %d possible result, but got %d possible result", len(tt.wants), len(resultHitCounts)) | ||
} | ||
}) | ||
} | ||
} |