@@ -52,7 +52,11 @@ func getDefaultWeightPreference(clusters []*clusterv1alpha1.Cluster) *policyv1al
5252}
5353
5454func calAvailableReplicas (clusters []* clusterv1alpha1.Cluster , spec * workv1alpha2.ResourceBindingSpec ) []workv1alpha2.TargetCluster {
55+ // availableTargetClusters stores the result of estimated replicas for each clusters
5556 availableTargetClusters := make ([]workv1alpha2.TargetCluster , len (clusters ))
57+ // clusterIndexToEstimatorPriority key refers to index of cluster slice,
58+ // value refers to the EstimatorPriority of who gave its estimated result.
59+ clusterIndexToEstimatorPriority := make (map [int ]estimatorclient.EstimatorPriority )
5660
5761 // Set the boundary.
5862 for i := range availableTargetClusters {
@@ -68,22 +72,52 @@ func calAvailableReplicas(clusters []*clusterv1alpha1.Cluster, spec *workv1alpha
6872 return availableTargetClusters
6973 }
7074
71- // Get the minimum value of MaxAvailableReplicas in terms of all estimators .
72- estimators := estimatorclient .GetReplicaEstimators ()
75+ // Get all replicaEstimators, which are stored in TreeMap .
76+ replicaEstimators := estimatorclient .GetReplicaEstimators ()
7377 ctx := context .WithValue (context .TODO (), util .ContextKeyObject ,
7478 fmt .Sprintf ("kind=%s, name=%s/%s" , spec .Resource .Kind , spec .Resource .Namespace , spec .Resource .Name ))
75- for _ , estimator := range estimators {
76- res , err := estimator .MaxAvailableReplicas (ctx , clusters , spec .ReplicaRequirements )
77- if err != nil {
78- klog .Errorf ("Max cluster available replicas error: %v" , err )
79- continue
79+
80+ // List all replicaEstimators in ascending order of priority. The estimators are grouped with different priorities,
81+ // e.g: [priority:10, {estimators:[es1, es3]}, {priority:20, estimators:[es2, es4]}, ...]
82+ estimatorGroups := replicaEstimators .Values ()
83+
84+ // Iterate the estimator groups in descending order of priority
85+ for i := len (estimatorGroups ) - 1 ; i >= 0 ; i -- {
86+ // if higher-priority estimators have formed a full result of member clusters, no longer to call lower-priority estimator.
87+ if len (clusterIndexToEstimatorPriority ) == len (clusters ) {
88+ break
8089 }
81- for i := range res {
82- if res [i ].Replicas == estimatorclient .UnauthenticReplica {
90+ // get a group of estimators corresponding to that specific priority.
91+ estimatorsWithSamePriority := estimatorGroups [i ].(map [string ]estimatorclient.ReplicaEstimator )
92+ // iterate through these estimators with the same priority.
93+ for _ , estimator := range estimatorsWithSamePriority {
94+ res , err := estimator .MaxAvailableReplicas (ctx , clusters , spec .ReplicaRequirements )
95+ if err != nil {
96+ klog .Errorf ("Max cluster available replicas error: %v" , err )
8397 continue
8498 }
85- if availableTargetClusters [i ].Name == res [i ].Name && availableTargetClusters [i ].Replicas > res [i ].Replicas {
86- availableTargetClusters [i ].Replicas = res [i ].Replicas
99+ for i := range res {
100+ // the result of this cluster estimated failed, ignore the corresponding result
101+ if res [i ].Replicas == estimatorclient .UnauthenticReplica {
102+ continue
103+ }
104+ // the cluster name not match, ignore, which hardly ever happens
105+ if res [i ].Name != availableTargetClusters [i ].Name {
106+ klog .Errorf ("unexpected cluster name in the result of estimator with %d priority, " +
107+ "expected: %s, got: %s" , estimator .Priority (), availableTargetClusters [i ].Name , res [i ].Name )
108+ continue
109+ }
110+ // the result of this cluster has already been estimated by higher-priority estimator,
111+ // ignore the corresponding result by this estimator
112+ if priority , ok := clusterIndexToEstimatorPriority [i ]; ok && estimator .Priority () < priority {
113+ continue
114+ }
115+ // if multiple estimators are called, choose the minimum value of each estimated result,
116+ // record the priority of result provider.
117+ if res [i ].Replicas < availableTargetClusters [i ].Replicas {
118+ availableTargetClusters [i ].Replicas = res [i ].Replicas
119+ clusterIndexToEstimatorPriority [i ] = estimator .Priority ()
120+ }
87121 }
88122 }
89123 }
0 commit comments