Skip to content

Commit

Permalink
core: Never have null PF Index
Browse files Browse the repository at this point in the history
This prevents many null checks and combines two code paths, with no
additional allocations.
  • Loading branch information
ejona86 committed Aug 21, 2024
1 parent 778a00b commit 8bd9795
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions core/src/main/java/io/grpc/internal/PickFirstLeafLoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final class PickFirstLeafLoadBalancer extends LoadBalancer {
static final int CONNECTION_DELAY_INTERVAL_MS = 250;
private final Helper helper;
private final Map<SocketAddress, SubchannelData> subchannels = new HashMap<>();
private Index addressIndex;
private final Index addressIndex = new Index(ImmutableList.of());
private int numTf = 0;
private boolean firstPass = true;
@Nullable
Expand Down Expand Up @@ -122,9 +122,7 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
final ImmutableList<EquivalentAddressGroup> newImmutableAddressGroups =
ImmutableList.<EquivalentAddressGroup>builder().addAll(cleanServers).build();

if (addressIndex == null) {
addressIndex = new Index(newImmutableAddressGroups);
} else if (rawConnectivityState == READY) {
if (rawConnectivityState == READY) {
// If the previous ready subchannel exists in new address list,
// keep this connection and don't create new subchannels
SocketAddress previousAddress = addressIndex.getCurrentAddress();
Expand Down Expand Up @@ -207,9 +205,7 @@ public void handleNameResolutionError(Status error) {
subchannelData.getSubchannel().shutdown();
}
subchannels.clear();
if (addressIndex != null) {
addressIndex.updateGroups(ImmutableList.of());
}
addressIndex.updateGroups(ImmutableList.of());
rawConnectivityState = TRANSIENT_FAILURE;
updateBalancingState(TRANSIENT_FAILURE, new Picker(PickResult.withError(error)));
}
Expand Down Expand Up @@ -372,7 +368,7 @@ private void shutdownRemaining(SubchannelData activeSubchannelData) {
*/
@Override
public void requestConnection() {
if (addressIndex == null || !addressIndex.isValid() || rawConnectivityState == SHUTDOWN ) {
if (!addressIndex.isValid() || rawConnectivityState == SHUTDOWN) {
return;
}

Expand Down Expand Up @@ -477,8 +473,7 @@ private SubchannelData createNewSubchannel(SocketAddress addr, Attributes attrs)
}

private boolean isPassComplete() {
if (addressIndex == null || addressIndex.isValid()
|| subchannels.size() < addressIndex.size()) {
if (addressIndex.isValid() || subchannels.size() < addressIndex.size()) {
return false;
}
for (SubchannelData sc : subchannels.values()) {
Expand Down

0 comments on commit 8bd9795

Please sign in to comment.