core: Never have null PF Index

This prevents many null checks and combines two code paths, with no
additional allocations.
This commit is contained in:
Eric Anderson 2024-08-15 17:01:14 -07:00
parent 778a00b623
commit 8bd97953ad
1 changed files with 5 additions and 10 deletions

View File

@ -61,7 +61,7 @@ final class PickFirstLeafLoadBalancer extends LoadBalancer {
static final int CONNECTION_DELAY_INTERVAL_MS = 250; static final int CONNECTION_DELAY_INTERVAL_MS = 250;
private final Helper helper; private final Helper helper;
private final Map<SocketAddress, SubchannelData> subchannels = new HashMap<>(); private final Map<SocketAddress, SubchannelData> subchannels = new HashMap<>();
private Index addressIndex; private final Index addressIndex = new Index(ImmutableList.of());
private int numTf = 0; private int numTf = 0;
private boolean firstPass = true; private boolean firstPass = true;
@Nullable @Nullable
@ -122,9 +122,7 @@ final class PickFirstLeafLoadBalancer extends LoadBalancer {
final ImmutableList<EquivalentAddressGroup> newImmutableAddressGroups = final ImmutableList<EquivalentAddressGroup> newImmutableAddressGroups =
ImmutableList.<EquivalentAddressGroup>builder().addAll(cleanServers).build(); ImmutableList.<EquivalentAddressGroup>builder().addAll(cleanServers).build();
if (addressIndex == null) { if (rawConnectivityState == READY) {
addressIndex = new Index(newImmutableAddressGroups);
} else if (rawConnectivityState == READY) {
// If the previous ready subchannel exists in new address list, // If the previous ready subchannel exists in new address list,
// keep this connection and don't create new subchannels // keep this connection and don't create new subchannels
SocketAddress previousAddress = addressIndex.getCurrentAddress(); SocketAddress previousAddress = addressIndex.getCurrentAddress();
@ -207,9 +205,7 @@ final class PickFirstLeafLoadBalancer extends LoadBalancer {
subchannelData.getSubchannel().shutdown(); subchannelData.getSubchannel().shutdown();
} }
subchannels.clear(); subchannels.clear();
if (addressIndex != null) { addressIndex.updateGroups(ImmutableList.of());
addressIndex.updateGroups(ImmutableList.of());
}
rawConnectivityState = TRANSIENT_FAILURE; rawConnectivityState = TRANSIENT_FAILURE;
updateBalancingState(TRANSIENT_FAILURE, new Picker(PickResult.withError(error))); updateBalancingState(TRANSIENT_FAILURE, new Picker(PickResult.withError(error)));
} }
@ -372,7 +368,7 @@ final class PickFirstLeafLoadBalancer extends LoadBalancer {
*/ */
@Override @Override
public void requestConnection() { public void requestConnection() {
if (addressIndex == null || !addressIndex.isValid() || rawConnectivityState == SHUTDOWN ) { if (!addressIndex.isValid() || rawConnectivityState == SHUTDOWN) {
return; return;
} }
@ -477,8 +473,7 @@ final class PickFirstLeafLoadBalancer extends LoadBalancer {
} }
private boolean isPassComplete() { private boolean isPassComplete() {
if (addressIndex == null || addressIndex.isValid() if (addressIndex.isValid() || subchannels.size() < addressIndex.size()) {
|| subchannels.size() < addressIndex.size()) {
return false; return false;
} }
for (SubchannelData sc : subchannels.values()) { for (SubchannelData sc : subchannels.values()) {