From 0e1fc4b604edaf220b456c90dbd9af28ef1ea0be Mon Sep 17 00:00:00 2001 From: haozhicui Date: Thu, 9 Jan 2025 11:24:18 +0800 Subject: [PATCH 1/2] loadbalancer: fix redundant code Signed-off-by: haozhicui --- plugin/loadbalancer/common/half_open.go | 17 +++++++++++++++++ plugin/loadbalancer/hash/hash.go | 16 ++++------------ plugin/loadbalancer/maglev/maglev.go | 14 ++++---------- plugin/loadbalancer/ringhash/ringhash.go | 15 ++++----------- 4 files changed, 29 insertions(+), 33 deletions(-) diff --git a/plugin/loadbalancer/common/half_open.go b/plugin/loadbalancer/common/half_open.go index 80c7d5d1..b1925077 100644 --- a/plugin/loadbalancer/common/half_open.go +++ b/plugin/loadbalancer/common/half_open.go @@ -19,6 +19,7 @@ package common import ( "github.com/polarismesh/polaris-go/pkg/model" + "github.com/polarismesh/polaris-go/pkg/plugin/loadbalancer" ) // SelectAvailableInstanceSet select available instance set @@ -35,3 +36,19 @@ func SelectAvailableInstanceSet(clsValue *model.ClusterValue, hasLimitedInstance targetInstances := clsValue.GetInstancesSet(hasLimitedInstances, includeHalfOpen) return targetInstances } + +// SelectAvailableInstanceSetFromCriteria select available instance from criteria +func SelectAvailableInstanceSetFromCriteria(criteria *loadbalancer.Criteria, + inputInstances model.ServiceInstances) (*model.InstanceSet, error) { + cluster := criteria.Cluster + svcClusters := inputInstances.GetServiceClusters() + clusterValue := cluster.GetClusterValue() + targetInstances := SelectAvailableInstanceSet(clusterValue, cluster.HasLimitedInstances, + cluster.IncludeHalfOpen) + if targetInstances.TotalWeight() == 0 { + return nil, model.NewSDKError(model.ErrCodeAPIInstanceNotFound, nil, + "instances of %s in cluster %s all weight 0 (instance count %d) in load balance", + svcClusters.GetServiceKey(), *cluster, targetInstances.Count()) + } + return targetInstances, nil +} diff --git a/plugin/loadbalancer/hash/hash.go b/plugin/loadbalancer/hash/hash.go index 02d91f0b..cec7c344 100644 --- a/plugin/loadbalancer/hash/hash.go +++ b/plugin/loadbalancer/hash/hash.go @@ -65,17 +65,9 @@ func (g *LoadBalancer) Destroy() error { // ChooseInstance 获取单个服务实例 func (g *LoadBalancer) ChooseInstance(criteria *loadbalancer.Criteria, inputInstances model.ServiceInstances) (model.Instance, error) { - cluster := criteria.Cluster - svcClusters := inputInstances.GetServiceClusters() - clusterValue := cluster.GetClusterValue() - var instance model.Instance - svcInstances := svcClusters.GetServiceInstances() - targetInstances := lbcommon.SelectAvailableInstanceSet(clusterValue, cluster.HasLimitedInstances, - cluster.IncludeHalfOpen) - if targetInstances.TotalWeight() == 0 { - return nil, model.NewSDKError(model.ErrCodeAPIInstanceNotFound, nil, - "instances of %s in cluster %s all weight 0 (instance count %d) in load balance", - svcClusters.GetServiceKey(), *cluster, targetInstances.Count()) + targetInstances, err := lbcommon.SelectAvailableInstanceSetFromCriteria(criteria, inputInstances) + if err != nil { + return nil, err } hashValue, err := lbcommon.CalcHashValue(criteria, g.hashFunc) if err != nil { @@ -86,7 +78,7 @@ func (g *LoadBalancer) ChooseInstance(criteria *loadbalancer.Criteria, // 按照权重区间来寻找 targetIndex := search.BinarySearch(weightedSlice, uint64(targetValue)) instanceIndex := targetInstances.GetInstances()[targetIndex] - instance = svcInstances.GetInstances()[instanceIndex.Index] + instance := inputInstances.GetInstances()[instanceIndex.Index] return instance, nil } diff --git a/plugin/loadbalancer/maglev/maglev.go b/plugin/loadbalancer/maglev/maglev.go index 44694527..8a2e2884 100644 --- a/plugin/loadbalancer/maglev/maglev.go +++ b/plugin/loadbalancer/maglev/maglev.go @@ -79,17 +79,11 @@ func (m *MaglevLoadBalancer) getOrBuildHashRing(instSet *model.InstanceSet) (mod // ChooseInstance 获取单个服务实例 func (m *MaglevLoadBalancer) ChooseInstance(criteria *loadbalancer.Criteria, inputInstances model.ServiceInstances) (model.Instance, error) { - cluster := criteria.Cluster - svcClusters := inputInstances.GetServiceClusters() - clusterValue := cluster.GetClusterValue() - svcInstances := svcClusters.GetServiceInstances() - targetInstances := lbcommon.SelectAvailableInstanceSet(clusterValue, cluster.HasLimitedInstances, - cluster.IncludeHalfOpen) - if targetInstances.TotalWeight() == 0 { - return nil, model.NewSDKError(model.ErrCodeAPIInstanceNotFound, nil, - "instances of %s in cluster %s all weight 0 (instance count %d) in load balance", - svcClusters.GetServiceKey(), *cluster, targetInstances.Count()) + targetInstances, err := lbcommon.SelectAvailableInstanceSetFromCriteria(criteria, inputInstances) + if err != nil { + return nil, err } + svcInstances := inputInstances.GetServiceClusters().GetServiceInstances() selector, err := m.getOrBuildHashRing(targetInstances) if err != nil { return nil, model.NewSDKError(model.ErrCodeInternalError, err, "fail to build maglev table") diff --git a/plugin/loadbalancer/ringhash/ringhash.go b/plugin/loadbalancer/ringhash/ringhash.go index d28a7dc8..ac4fc6ff 100644 --- a/plugin/loadbalancer/ringhash/ringhash.go +++ b/plugin/loadbalancer/ringhash/ringhash.go @@ -78,16 +78,9 @@ func (k *KetamaLoadBalancer) getOrBuildHashRing(instSet *model.InstanceSet) (mod // ChooseInstance 获取单个服务实例 func (k *KetamaLoadBalancer) ChooseInstance(criteria *loadbalancer.Criteria, inputInstances model.ServiceInstances) (model.Instance, error) { - cluster := criteria.Cluster - svcClusters := inputInstances.GetServiceClusters() - clusterValue := cluster.GetClusterValue() - svcInstances := svcClusters.GetServiceInstances() - targetInstances := lbcommon.SelectAvailableInstanceSet(clusterValue, cluster.HasLimitedInstances, - cluster.IncludeHalfOpen) - if targetInstances.TotalWeight() == 0 { - return nil, model.NewSDKError(model.ErrCodeAPIInstanceNotFound, nil, - "instances of %s in cluster %s all weight 0 (instance count %d) in load balance", - svcClusters.GetServiceKey(), *cluster, targetInstances.Count()) + targetInstances, err := lbcommon.SelectAvailableInstanceSetFromCriteria(criteria, inputInstances) + if err != nil { + return nil, err } selector, err := k.getOrBuildHashRing(targetInstances) if err != nil { @@ -101,7 +94,7 @@ func (k *KetamaLoadBalancer) ChooseInstance(criteria *loadbalancer.Criteria, criteria.ReplicateInfo.Nodes = nodes.GetInstances() } - instance := svcInstances.GetInstances()[index] + instance := inputInstances.GetInstances()[index] return instance, nil } From c3b50c78289d4ab3a10c4b809e66294d1de870df Mon Sep 17 00:00:00 2001 From: haozhicui Date: Tue, 21 Jan 2025 17:41:07 +0800 Subject: [PATCH 2/2] fix ChooseInstance Signed-off-by: haozhicui --- plugin/loadbalancer/hash/hash.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugin/loadbalancer/hash/hash.go b/plugin/loadbalancer/hash/hash.go index cec7c344..c744338f 100644 --- a/plugin/loadbalancer/hash/hash.go +++ b/plugin/loadbalancer/hash/hash.go @@ -78,7 +78,9 @@ func (g *LoadBalancer) ChooseInstance(criteria *loadbalancer.Criteria, // 按照权重区间来寻找 targetIndex := search.BinarySearch(weightedSlice, uint64(targetValue)) instanceIndex := targetInstances.GetInstances()[targetIndex] - instance := inputInstances.GetInstances()[instanceIndex.Index] + + instance := inputInstances.GetServiceClusters().GetServiceInstances().GetInstances()[instanceIndex.Index] + return instance, nil }