Skip to content

Commit 8bd9795

Browse files
committed
core: Never have null PF Index
This prevents many null checks and combines two code paths, with no additional allocations.
1 parent 778a00b commit 8bd9795

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

core/src/main/java/io/grpc/internal/PickFirstLeafLoadBalancer.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ final class PickFirstLeafLoadBalancer extends LoadBalancer {
6161
static final int CONNECTION_DELAY_INTERVAL_MS = 250;
6262
private final Helper helper;
6363
private final Map<SocketAddress, SubchannelData> subchannels = new HashMap<>();
64-
private Index addressIndex;
64+
private final Index addressIndex = new Index(ImmutableList.of());
6565
private int numTf = 0;
6666
private boolean firstPass = true;
6767
@Nullable
@@ -122,9 +122,7 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
122122
final ImmutableList<EquivalentAddressGroup> newImmutableAddressGroups =
123123
ImmutableList.<EquivalentAddressGroup>builder().addAll(cleanServers).build();
124124

125-
if (addressIndex == null) {
126-
addressIndex = new Index(newImmutableAddressGroups);
127-
} else if (rawConnectivityState == READY) {
125+
if (rawConnectivityState == READY) {
128126
// If the previous ready subchannel exists in new address list,
129127
// keep this connection and don't create new subchannels
130128
SocketAddress previousAddress = addressIndex.getCurrentAddress();
@@ -207,9 +205,7 @@ public void handleNameResolutionError(Status error) {
207205
subchannelData.getSubchannel().shutdown();
208206
}
209207
subchannels.clear();
210-
if (addressIndex != null) {
211-
addressIndex.updateGroups(ImmutableList.of());
212-
}
208+
addressIndex.updateGroups(ImmutableList.of());
213209
rawConnectivityState = TRANSIENT_FAILURE;
214210
updateBalancingState(TRANSIENT_FAILURE, new Picker(PickResult.withError(error)));
215211
}
@@ -372,7 +368,7 @@ private void shutdownRemaining(SubchannelData activeSubchannelData) {
372368
*/
373369
@Override
374370
public void requestConnection() {
375-
if (addressIndex == null || !addressIndex.isValid() || rawConnectivityState == SHUTDOWN ) {
371+
if (!addressIndex.isValid() || rawConnectivityState == SHUTDOWN) {
376372
return;
377373
}
378374

@@ -477,8 +473,7 @@ private SubchannelData createNewSubchannel(SocketAddress addr, Attributes attrs)
477473
}
478474

479475
private boolean isPassComplete() {
480-
if (addressIndex == null || addressIndex.isValid()
481-
|| subchannels.size() < addressIndex.size()) {
476+
if (addressIndex.isValid() || subchannels.size() < addressIndex.size()) {
482477
return false;
483478
}
484479
for (SubchannelData sc : subchannels.values()) {

0 commit comments

Comments
 (0)