• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #19102

14 Mar 2024 03:53AM UTC coverage: 88.259% (-0.05%) from 88.311%
#19102

push

github

web-flow
core: Eliminate NPE seen in PickFirstLeafLoadBalancer (#11013)

ref b/329420531

31150 of 35294 relevant lines covered (88.26%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.0
/../core/src/main/java/io/grpc/internal/PickFirstLoadBalancerProvider.java
1
/*
2
 * Copyright 2018 The gRPC Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package io.grpc.internal;
18

19
import com.google.common.annotations.VisibleForTesting;
20
import io.grpc.LoadBalancer;
21
import io.grpc.LoadBalancerProvider;
22
import io.grpc.NameResolver;
23
import io.grpc.NameResolver.ConfigOrError;
24
import io.grpc.Status;
25
import io.grpc.internal.PickFirstLeafLoadBalancer.PickFirstLeafLoadBalancerConfig;
26
import io.grpc.internal.PickFirstLoadBalancer.PickFirstLoadBalancerConfig;
27
import java.util.Map;
28

29
/**
30
 * Provider for the "pick_first" balancing policy.
31
 *
32
 * <p>This provides no load-balancing over the addresses from the {@link NameResolver}.  It walks
33
 * down the address list and sticks to the first that works.
34
 */
35
public final class PickFirstLoadBalancerProvider extends LoadBalancerProvider {
1✔
36
  private static final String SHUFFLE_ADDRESS_LIST_KEY = "shuffleAddressList";
37

38
  static boolean enableNewPickFirst =
1✔
39
      GrpcUtil.getFlag("GRPC_EXPERIMENTAL_ENABLE_NEW_PICK_FIRST", true);
1✔
40

41
  @Override
42
  public boolean isAvailable() {
43
    return true;
1✔
44
  }
45

46
  @Override
47
  public int getPriority() {
48
    return 5;
1✔
49
  }
50

51
  @Override
52
  public String getPolicyName() {
53
    return "pick_first";
1✔
54
  }
55

56
  @Override
57
  public LoadBalancer newLoadBalancer(LoadBalancer.Helper helper) {
58
    if (enableNewPickFirst) {
1✔
59
      return new PickFirstLeafLoadBalancer(helper);
1✔
60
    } else {
61
      return new PickFirstLoadBalancer(helper);
×
62
    }
63
  }
64

65
  @Override
66
  public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawLbPolicyConfig) {
67
    try {
68
      Object config = getLbPolicyConfig(rawLbPolicyConfig);
1✔
69
      return ConfigOrError.fromConfig(config);
1✔
70
    } catch (RuntimeException e) {
×
71
      return ConfigOrError.fromError(
×
72
          Status.UNAVAILABLE.withCause(e).withDescription(
×
73
              "Failed parsing configuration for " + getPolicyName()));
×
74
    }
75
  }
76

77
  private static Object getLbPolicyConfig(Map<String, ?> rawLbPolicyConfig) {
78
    Boolean shuffleAddressList = JsonUtil.getBoolean(rawLbPolicyConfig, SHUFFLE_ADDRESS_LIST_KEY);
1✔
79
    if (enableNewPickFirst) {
1✔
80
      return new PickFirstLeafLoadBalancerConfig(shuffleAddressList);
1✔
81
    } else {
82
      return new PickFirstLoadBalancerConfig(shuffleAddressList);
×
83
    }
84
  }
85

86
  @VisibleForTesting
87
  public static boolean isEnabledNewPickFirst() {
88
    return enableNewPickFirst;
1✔
89
  }
90
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc