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

switch balancer based on service config info #1670

Merged
merged 5 commits into from
Nov 17, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion balancer/roundrobin/roundrobin.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (*rrBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balan
}

func (*rrBuilder) Name() string {
return "roundrobin"
return "round_robin"
}

type rrBalancer struct {
Expand Down
2 changes: 1 addition & 1 deletion balancer/roundrobin/roundrobin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"google.golang.org/grpc/test/leakcheck"
)

var rr = balancer.Get("roundrobin")
var rr = balancer.Get("round_robin")

type testServer struct {
testpb.TestServiceServer
Expand Down
4 changes: 2 additions & 2 deletions balancer_switching_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ func TestSwitchBalancer(t *testing.T) {
t.Fatalf("check pickfirst returned non-nil error: %v", err)
}
// Switch to roundrobin.
cc.switchBalancer("roundrobin")
cc.handleServiceConfig(`{"loadBalancingPolicy": "round_robin"}`)
if err := checkRoundRobin(cc, servers); err != nil {
t.Fatalf("check roundrobin returned non-nil error: %v", err)
}
// Switch to pickfirst.
cc.switchBalancer("pickfirst")
cc.handleServiceConfig(`{"loadBalancingPolicy": "pick_first"}`)
if err := checkPickFirst(cc, servers); err != nil {
t.Fatalf("check pickfirst returned non-nil error: %v", err)
}
Expand Down
8 changes: 5 additions & 3 deletions clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,7 @@ func (cc *ClientConn) handleResolvedAddrs(addrs []resolver.Address, err error) {

// switchBalancer starts the switching from current balancer to the balancer with name.
func (cc *ClientConn) switchBalancer(name string) {
cc.mu.Lock()
defer cc.mu.Unlock()

if cc.conns == nil {
return
}
Expand Down Expand Up @@ -818,9 +817,12 @@ func (cc *ClientConn) handleServiceConfig(js string) error {
return err
}
cc.mu.Lock()
defer cc.mu.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't defer.
defer is slow (golang/go#14939)

cc.scRaw = js
cc.sc = sc
cc.mu.Unlock()
if sc.LB != nil {
cc.switchBalancer(*sc.LB)
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pickfirst.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (*pickfirstBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions
}

func (*pickfirstBuilder) Name() string {
return "pickfirst"
return "pick_first"
}

type pickfirstBalancer struct {
Expand Down
14 changes: 7 additions & 7 deletions test/end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ type env struct {
network string // The type of network such as tcp, unix, etc.
security string // The security protocol such as TLS, SSH, etc.
httpHandler bool // whether to use the http.Handler ServerTransport; requires TLS
balancer string // One of "roundrobin", "pickfirst", "v1", or "".
balancer string // One of "round_robin", "pick_first", "v1", or "".
customDialer func(string, string, time.Duration) (net.Conn, error)
}

Expand All @@ -398,9 +398,9 @@ func (e env) dialer(addr string, timeout time.Duration) (net.Conn, error) {
var (
tcpClearEnv = env{name: "tcp-clear-v1-balancer", network: "tcp", balancer: "v1"}
tcpTLSEnv = env{name: "tcp-tls-v1-balancer", network: "tcp", security: "tls", balancer: "v1"}
tcpClearRREnv = env{name: "tcp-clear", network: "tcp", balancer: "roundrobin"}
tcpTLSRREnv = env{name: "tcp-tls", network: "tcp", security: "tls", balancer: "roundrobin"}
handlerEnv = env{name: "handler-tls", network: "tcp", security: "tls", httpHandler: true, balancer: "roundrobin"}
tcpClearRREnv = env{name: "tcp-clear", network: "tcp", balancer: "round_robin"}
tcpTLSRREnv = env{name: "tcp-tls", network: "tcp", security: "tls", balancer: "round_robin"}
handlerEnv = env{name: "handler-tls", network: "tcp", security: "tls", httpHandler: true, balancer: "round_robin"}
noBalancerEnv = env{name: "no-balancer", network: "tcp", security: "tls"}
allEnv = []env{tcpClearEnv, tcpTLSEnv, tcpClearRREnv, tcpTLSRREnv, handlerEnv, noBalancerEnv}
)
Expand Down Expand Up @@ -682,7 +682,7 @@ func (te *test) clientConn() *grpc.ClientConn {
default:
opts = append(opts, grpc.WithInsecure())
}
// TODO(bar) switch balancer case "pickfirst".
// TODO(bar) switch balancer case "pick_first".
var scheme string
if te.resolverScheme == "" {
scheme = "passthrough:///"
Expand All @@ -692,8 +692,8 @@ func (te *test) clientConn() *grpc.ClientConn {
switch te.e.balancer {
case "v1":
opts = append(opts, grpc.WithBalancer(grpc.RoundRobin(nil)))
case "roundrobin":
rr := balancer.Get("roundrobin")
case "round_robin":
rr := balancer.Get("round_robin")
if rr == nil {
te.t.Fatalf("got nil when trying to get roundrobin balancer builder")
}
Expand Down