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

deduplicate addresses within a single EAG #11342

Merged
merged 1 commit into from
Jun 27, 2024
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
26 changes: 23 additions & 3 deletions core/src/main/java/io/grpc/internal/PickFirstLeafLoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
handleNameResolutionError(unavailableStatus);
return unavailableStatus;
}

List<EquivalentAddressGroup> cleanServers = new ArrayList<>();

for (EquivalentAddressGroup eag : servers) {
if (eag == null) {
Status unavailableStatus = Status.UNAVAILABLE.withDescription(
Expand All @@ -100,6 +103,7 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
handleNameResolutionError(unavailableStatus);
return unavailableStatus;
}
cleanServers.add(removeDuplicateAddresses(eag));
Copy link
Member

Choose a reason for hiding this comment

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

If DNS returned duplicate addresses, we'd have two EAGs with one address each. This removeDuplicateAddresses() would do nothing to remove the duplication across those EAGs. Don't we need that?

}

// Since we have a new set of addresses, we are again at first pass
Expand All @@ -112,15 +116,14 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
PickFirstLeafLoadBalancerConfig config
= (PickFirstLeafLoadBalancerConfig) resolvedAddresses.getLoadBalancingPolicyConfig();
if (config.shuffleAddressList != null && config.shuffleAddressList) {
servers = new ArrayList<>(servers);
Collections.shuffle(servers,
Collections.shuffle(cleanServers,
config.randomSeed != null ? new Random(config.randomSeed) : new Random());
}
}

// Make sure we're storing our own list rather than what was passed in
final ImmutableList<EquivalentAddressGroup> newImmutableAddressGroups =
ImmutableList.<EquivalentAddressGroup>builder().addAll(servers).build();
ImmutableList.<EquivalentAddressGroup>builder().addAll(cleanServers).build();

if (addressIndex == null) {
addressIndex = new Index(newImmutableAddressGroups);
Expand Down Expand Up @@ -178,6 +181,23 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
return Status.OK;
}

private static EquivalentAddressGroup removeDuplicateAddresses(EquivalentAddressGroup eag) {
Set<SocketAddress> addressSet = new HashSet<>();
ArrayList<SocketAddress> addrs = new ArrayList<>(); // maintains order

for (SocketAddress address : eag.getAddresses()) {
if (addressSet.add(address)) {
addrs.add(address);
}
}

if (addressSet.size() == eag.getAddresses().size()) {
return eag;
}

return new EquivalentAddressGroup(addrs, eag.getAttributes());
}

@Override
public void handleNameResolutionError(Status error) {
if (rawConnectivityState == SHUTDOWN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,41 @@ public void pickAfterResolutionAfterTransientValue() {
assertEquals(error, pickerCaptor.getValue().pickSubchannel(mockArgs).getStatus());
}

@Test
public void pickAWithDupAddressesUpDownUp() {
InOrder inOrder = inOrder(mockHelper);
SocketAddress socketAddress = servers.get(0).getAddresses().get(0);
EquivalentAddressGroup badEag = new EquivalentAddressGroup(
Lists.newArrayList(socketAddress, socketAddress), affinity);
List<EquivalentAddressGroup> newServers = Lists.newArrayList(badEag);

loadBalancer.acceptResolvedAddresses(
ResolvedAddresses.newBuilder().setAddresses(newServers).setAttributes(affinity).build());
verify(mockHelper).updateBalancingState(eq(CONNECTING), pickerCaptor.capture());
verify(mockHelper).createSubchannel(createArgsCaptor.capture());
verify(mockSubchannel1).start(stateListenerCaptor.capture());
SubchannelStateListener stateListener = stateListenerCaptor.getValue();

reset(mockHelper);

// An error has happened.
Status error = Status.UNAVAILABLE.withDescription("boom!");
stateListener.onSubchannelState(ConnectivityStateInfo.forTransientFailure(error));
inOrder.verify(mockHelper).updateBalancingState(eq(TRANSIENT_FAILURE), pickerCaptor.capture());
inOrder.verify(mockHelper).refreshNameResolution();
assertEquals(error, pickerCaptor.getValue().pickSubchannel(mockArgs).getStatus());

// Transition from TRANSIENT_ERROR to CONNECTING should also be ignored.
stateListener.onSubchannelState(ConnectivityStateInfo.forNonError(CONNECTING));
verifyNoMoreInteractions(mockHelper);
assertEquals(error, pickerCaptor.getValue().pickSubchannel(mockArgs).getStatus());

// Transition from CONNECTING to READY .
stateListener.onSubchannelState(ConnectivityStateInfo.forNonError(READY));
inOrder.verify(mockHelper).updateBalancingState(eq(READY), pickerCaptor.capture());
assertEquals(Status.OK, pickerCaptor.getValue().pickSubchannel(mockArgs).getStatus());
}

@Test
public void nameResolutionError() {
Status error = Status.NOT_FOUND.withDescription("nameResolutionError");
Expand Down
Loading