Skip to content

Commit

Permalink
balancer: populate endpoint weight by edsbalancer for weighted_round_…
Browse files Browse the repository at this point in the history
…robin (#2945)
  • Loading branch information
alazarev authored and menghanl committed Aug 6, 2019
1 parent 92635fa commit a2bdfb4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
29 changes: 29 additions & 0 deletions balancer/weightedroundrobin/weightedroundrobin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
*
* Copyright 2019 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

// Package weightedroundrobin defines a weighted roundrobin balancer.
package weightedroundrobin

// Name is the name of weighted_round_robin balancer.
const Name = "weighted_round_robin"

// AddrInfo will be stored inside Address metadata in order to use weighted roundrobin
// balancer.
type AddrInfo struct {
Weight uint32
}
12 changes: 10 additions & 2 deletions balancer/xds/edsbalancer/edsbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/roundrobin"
"google.golang.org/grpc/balancer/weightedroundrobin"
"google.golang.org/grpc/balancer/xds/internal"
edspb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/eds"
endpointpb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/endpoint/endpoint"
Expand Down Expand Up @@ -227,9 +228,16 @@ func (xdsB *EDSBalancer) HandleEDSResponse(edsResp *edspb.ClusterLoadAssignment)
var newAddrs []resolver.Address
for _, lbEndpoint := range locality.GetLbEndpoints() {
socketAddress := lbEndpoint.GetEndpoint().GetAddress().GetSocketAddress()
newAddrs = append(newAddrs, resolver.Address{
address := resolver.Address{
Addr: net.JoinHostPort(socketAddress.GetAddress(), strconv.Itoa(int(socketAddress.GetPortValue()))),
})
}
if xdsB.subBalancerBuilder.Name() == weightedroundrobin.Name &&
lbEndpoint.GetLoadBalancingWeight().GetValue() != 0 {
address.Metadata = &weightedroundrobin.AddrInfo{
Weight: lbEndpoint.GetLoadBalancingWeight().GetValue(),
}
}
newAddrs = append(newAddrs, address)
}
var weightChanged, addrsChanged bool
config, ok := xdsB.lidToConfig[lid]
Expand Down

0 comments on commit a2bdfb4

Please sign in to comment.