-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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 to grpclb when at least one address is grpclb address #1692
Conversation
dbd74c5
to
d58d623
Compare
clientconn.go
Outdated
@@ -600,6 +600,7 @@ type ClientConn struct { | |||
// Keepalive parameter can be updated if a GoAway is received. | |||
mkp keepalive.ClientParameters | |||
curBalancerName string | |||
preBalancerName string // previous balancer nam. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*namE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
clientconn.go
Outdated
newBalancerName = pickfirstName | ||
} | ||
for _, a := range addrs { | ||
if a.Type == resolver.GRPCLB && newBalancerName != grpclbName { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't want "newBalancerName != grpclbName
". We should break
if any Type
is resolver.GRPCLB
, and re-assigning newBalancerName to grpclbName is not worth optimizing away (actually it's more optimal than the extra branch).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
clientconn.go
Outdated
// Switch from non-grpclb to grpclb. | ||
newBalancerName = grpclbName | ||
break | ||
} else if a.Type == resolver.Backend && newBalancerName == grpclbName { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we wanted to go with gRPCLB if any address is a gRPCLB address? This will switch away if both kinds are returned and we were previously using gRPCLB.
Maybe we should have a test where the gRPCLB backend changes (but both before and after are gRPCLB).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right!
Fixed and added a test.
clientconn.go
Outdated
cc.balancerWrapper.handleResolvedAddrs(addrs, nil) | ||
} | ||
|
||
// switchBalancer starts the switching from current balancer to the balancer with name. | ||
// switchBalancer starts the switching from current balancer to the balancer | ||
// with the given name. It will NOT send the current address list to the new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will NOT send the current address list to the new balancer.
Why? How do we bootstrap the new balancer then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Caller of this function should do that. Comment updated.
clientconn.go
Outdated
@@ -688,7 +700,6 @@ func (cc *ClientConn) switchBalancer(name string) { | |||
grpclog.Infoln("ignoring service config balancer configuration: WithBalancer DialOption used instead") | |||
return | |||
} | |||
|
|||
if cc.curBalancerName == name { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this go above the log about switching?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea here is, if balancer is set by a dial option, all switch balancer call will be no-op.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, but we don't need to log that we aren't switching if the name doesn't change, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
balancer_switching_test.go
Outdated
time.Sleep(time.Millisecond) | ||
} | ||
if !isPickFirst { | ||
t.Fatalf("after 1 second, cc.balancer is of type %v, not pick_first", cc.curBalancerName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is 1s long enough for these tests on travis? I'd rather give them 5-10 to avoid potential flakiness -- they stop early when the condition occurs, so it's not harmful to give them longer unless the timing is important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
d7b0bf5
to
4c706ca
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review.
All done. PTAL.
clientconn.go
Outdated
@@ -600,6 +600,7 @@ type ClientConn struct { | |||
// Keepalive parameter can be updated if a GoAway is received. | |||
mkp keepalive.ClientParameters | |||
curBalancerName string | |||
preBalancerName string // previous balancer nam. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
clientconn.go
Outdated
cc.balancerWrapper.handleResolvedAddrs(addrs, nil) | ||
} | ||
|
||
// switchBalancer starts the switching from current balancer to the balancer with name. | ||
// switchBalancer starts the switching from current balancer to the balancer | ||
// with the given name. It will NOT send the current address list to the new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Caller of this function should do that. Comment updated.
clientconn.go
Outdated
@@ -688,7 +700,6 @@ func (cc *ClientConn) switchBalancer(name string) { | |||
grpclog.Infoln("ignoring service config balancer configuration: WithBalancer DialOption used instead") | |||
return | |||
} | |||
|
|||
if cc.curBalancerName == name { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea here is, if balancer is set by a dial option, all switch balancer call will be no-op.
balancer_switching_test.go
Outdated
time.Sleep(time.Millisecond) | ||
} | ||
if !isPickFirst { | ||
t.Fatalf("after 1 second, cc.balancer is of type %v, not pick_first", cc.curBalancerName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
clientconn.go
Outdated
// Switch from non-grpclb to grpclb. | ||
newBalancerName = grpclbName | ||
break | ||
} else if a.Type == resolver.Backend && newBalancerName == grpclbName { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right!
Fixed and added a test.
clientconn.go
Outdated
newBalancerName = pickfirstName | ||
} | ||
for _, a := range addrs { | ||
if a.Type == resolver.GRPCLB && newBalancerName != grpclbName { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
clientconn.go
Outdated
} | ||
var newBalancerName string | ||
if isGRPCLB { | ||
newBalancerName = grpclbName |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional, but it's a go-ism:
newBalancerName := grpclbName
if !isGRPCLB {
...
}
but it's a little awkward in this case, so either is probably fine.
clientconn.go
Outdated
@@ -719,15 +719,14 @@ func (cc *ClientConn) switchBalancer(name string) { | |||
} | |||
grpclog.Infof("ClientConn switching balancer to %q", name) | |||
|
|||
if cc.dopts.balancerBuilder != nil { | |||
grpclog.Infoln("ignoring balancer switching: WithBalancer DialOption used instead") | |||
if strings.ToLower(cc.curBalancerName) == strings.ToLower(name) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather move this above the info log on 720 also, but if you feel strongly then OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
This change is