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 com.google.common.base.Strings; 21 import io.grpc.LoadBalancer; 22 import io.grpc.LoadBalancerProvider; 23 import io.grpc.NameResolver; 24 import io.grpc.NameResolver.ConfigOrError; 25 import io.grpc.Status; 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 { 36 private static final String NO_CONFIG = "no service config"; 37 private static final String SHUFFLE_ADDRESS_LIST_KEY = "shuffleAddressList"; 38 private static final String CONFIG_FLAG_NAME = "GRPC_EXPERIMENTAL_PICKFIRST_LB_CONFIG"; 39 @VisibleForTesting 40 static boolean enablePickFirstConfig = !Strings.isNullOrEmpty(System.getenv(CONFIG_FLAG_NAME)); 41 42 @Override isAvailable()43 public boolean isAvailable() { 44 return true; 45 } 46 47 @Override getPriority()48 public int getPriority() { 49 return 5; 50 } 51 52 @Override getPolicyName()53 public String getPolicyName() { 54 return "pick_first"; 55 } 56 57 @Override newLoadBalancer(LoadBalancer.Helper helper)58 public LoadBalancer newLoadBalancer(LoadBalancer.Helper helper) { 59 return new PickFirstLoadBalancer(helper); 60 } 61 62 @Override parseLoadBalancingPolicyConfig( Map<String, ?> rawLoadBalancingPolicyConfig)63 public ConfigOrError parseLoadBalancingPolicyConfig( 64 Map<String, ?> rawLoadBalancingPolicyConfig) { 65 if (enablePickFirstConfig) { 66 try { 67 return ConfigOrError.fromConfig( 68 new PickFirstLoadBalancerConfig(JsonUtil.getBoolean(rawLoadBalancingPolicyConfig, 69 SHUFFLE_ADDRESS_LIST_KEY))); 70 } catch (RuntimeException e) { 71 return ConfigOrError.fromError( 72 Status.UNAVAILABLE.withCause(e).withDescription( 73 "Failed parsing configuration for " + getPolicyName())); 74 } 75 } else { 76 return ConfigOrError.fromConfig(NO_CONFIG); 77 } 78 } 79 } 80