Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadbalancer: fix redundant code #228

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions plugin/loadbalancer/common/half_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
18 changes: 6 additions & 12 deletions plugin/loadbalancer/hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -86,7 +78,9 @@ func (g *LoadBalancer) ChooseInstance(criteria *loadbalancer.Criteria,
// 按照权重区间来寻找
targetIndex := search.BinarySearch(weightedSlice, uint64(targetValue))
instanceIndex := targetInstances.GetInstances()[targetIndex]
instance = svcInstances.GetInstances()[instanceIndex.Index]

instance := inputInstances.GetServiceClusters().GetServiceInstances().GetInstances()[instanceIndex.Index]

return instance, nil
}

Expand Down
14 changes: 4 additions & 10 deletions plugin/loadbalancer/maglev/maglev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
15 changes: 4 additions & 11 deletions plugin/loadbalancer/ringhash/ringhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
Loading