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

grpclb: fix a bug in handling server address updates. #2754

Merged
merged 1 commit into from
Mar 1, 2017
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: 10 additions & 7 deletions grpclb/src/main/java/io/grpc/grpclb/GrpclbLoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,16 @@ private void handleResponse(LoadBalanceResponse response) {
EquivalentAddressGroup eag = new EquivalentAddressGroup(address);
// TODO(zhangkun83): save the LB token and insert it to the application RPCs' headers.
if (!newSubchannelMap.containsKey(eag)) {
Attributes subchannelAttrs = Attributes.newBuilder()
.set(STATE_INFO,
new AtomicReference<ConnectivityStateInfo>(
ConnectivityStateInfo.forNonError(IDLE)))
.build();
Subchannel subchannel = helper.createSubchannel(eag, subchannelAttrs);
subchannel.requestConnection();
Subchannel subchannel = subchannels.get(eag);
if (subchannel == null) {
Attributes subchannelAttrs = Attributes.newBuilder()
.set(STATE_INFO,
new AtomicReference<ConnectivityStateInfo>(
ConnectivityStateInfo.forNonError(IDLE)))
.build();
subchannel = helper.createSubchannel(eag, subchannelAttrs);
subchannel.requestConnection();
}
newSubchannelMap.put(eag, subchannel);
}
newRoundRobinList.add(eag);
Expand Down
28 changes: 20 additions & 8 deletions grpclb/src/test/java/io/grpc/grpclb/GrpclbLoadBalancerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -601,15 +601,20 @@ public void grpclbWorking() {
assertSame(error1, picker6.result.getStatus());

// Update backends, with a drop entry
List<InetSocketAddress> backends2 = Arrays.asList(
new InetSocketAddress("127.0.0.1", 2030), null);
List<InetSocketAddress> backends2 =
Copy link
Member

Choose a reason for hiding this comment

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

This test code may be too long. Maybe it's testing multiple things that could be separated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree the state of this test method is not ideal. Although it's testing multiple behaviors, every test depends the state left by the the previous tests as prerequisites, therefore it's non-trivial to split them apart, and I don't have the time to do it.

Arrays.asList(
new InetSocketAddress("127.0.0.1", 2030), // New address
null, // drop
new InetSocketAddress("127.0.0.1", 2010), // Existing address
new InetSocketAddress("127.0.0.1", 2030)); // New address appearing second time
verify(subchannel1, never()).shutdown();
verify(subchannel2, never()).shutdown();

lbResponseObserver.onNext(buildLbResponse(backends2));
verify(subchannel1).shutdown();
verify(subchannel2).shutdown();
verify(subchannel1).shutdown(); // not in backends2, closed
verify(subchannel2, never()).shutdown(); // backends2[2], will be kept

inOrder.verify(helper, never()).createSubchannel(
eq(new EquivalentAddressGroup(backends2.get(2))), any(Attributes.class));
inOrder.verify(helper).createSubchannel(
eq(new EquivalentAddressGroup(backends2.get(0))), any(Attributes.class));
assertEquals(1, mockSubchannels.size());
Expand All @@ -620,16 +625,23 @@ public void grpclbWorking() {
RoundRobinPicker picker7 = (RoundRobinPicker) pickerCaptor.getValue();
assertRoundRobinList(picker7, (Subchannel) null);

// State updates on obsolete subchannels will have no effect
// State updates on obsolete subchannel1 will have no effect
deliverSubchannelState(subchannel1, ConnectivityStateInfo.forNonError(READY));
deliverSubchannelState(
subchannel1, ConnectivityStateInfo.forTransientFailure(Status.UNAVAILABLE));
deliverSubchannelState(subchannel1, ConnectivityStateInfo.forNonError(SHUTDOWN));
deliverSubchannelState(subchannel2, ConnectivityStateInfo.forNonError(SHUTDOWN));
inOrder.verifyNoMoreInteractions();

deliverSubchannelState(subchannel3, ConnectivityStateInfo.forNonError(READY));
inOrder.verify(helper).updatePicker(pickerCaptor.capture());
RoundRobinPicker picker8 = (RoundRobinPicker) pickerCaptor.getValue();
assertRoundRobinList(picker8, subchannel3, null);
// subchannel2 is still IDLE, thus not in the active list
assertRoundRobinList(picker8, subchannel3, null, subchannel3);
// subchannel2 becomes READY and makes it into the list
deliverSubchannelState(subchannel2, ConnectivityStateInfo.forNonError(READY));
inOrder.verify(helper).updatePicker(pickerCaptor.capture());
RoundRobinPicker picker9 = (RoundRobinPicker) pickerCaptor.getValue();
assertRoundRobinList(picker9, subchannel3, null, subchannel2, subchannel3);

verify(subchannel3, never()).shutdown();
assertFalse(oobChannel.isShutdown());
Expand Down