229 lines
10 KiB
Java
229 lines
10 KiB
Java
package io.grpc.internal;
|
|
|
|
import com.google.common.base.MoreObjects;
|
|
import com.google.common.base.Preconditions;
|
|
import io.grpc.Attributes;
|
|
import io.grpc.ChannelLogger;
|
|
import io.grpc.ConnectivityState;
|
|
import io.grpc.ConnectivityStateInfo;
|
|
import io.grpc.EquivalentAddressGroup;
|
|
import io.grpc.LoadBalancer;
|
|
import io.grpc.LoadBalancerProvider;
|
|
import io.grpc.LoadBalancerRegistry;
|
|
import io.grpc.NameResolver;
|
|
import io.grpc.Status;
|
|
import io.grpc.internal.ServiceConfigUtil;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public final class AutoConfiguredLoadBalancerFactory {
|
|
private final String defaultPolicy;
|
|
private final LoadBalancerRegistry registry;
|
|
|
|
public AutoConfiguredLoadBalancerFactory(String str) {
|
|
this(LoadBalancerRegistry.getDefaultRegistry(), str);
|
|
}
|
|
|
|
AutoConfiguredLoadBalancerFactory(LoadBalancerRegistry loadBalancerRegistry, String str) {
|
|
this.registry = (LoadBalancerRegistry) Preconditions.checkNotNull(loadBalancerRegistry, "registry");
|
|
this.defaultPolicy = (String) Preconditions.checkNotNull(str, "defaultPolicy");
|
|
}
|
|
|
|
public final AutoConfiguredLoadBalancer newLoadBalancer(LoadBalancer.Helper helper) {
|
|
return new AutoConfiguredLoadBalancer(this, helper);
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
static final class NoopLoadBalancer extends LoadBalancer {
|
|
@Override // io.grpc.LoadBalancer
|
|
public final void handleNameResolutionError(Status status) {
|
|
}
|
|
|
|
@Override // io.grpc.LoadBalancer
|
|
@Deprecated
|
|
public final void handleResolvedAddressGroups(List<EquivalentAddressGroup> list, Attributes attributes) {
|
|
}
|
|
|
|
@Override // io.grpc.LoadBalancer
|
|
public final void handleResolvedAddresses(LoadBalancer.ResolvedAddresses resolvedAddresses) {
|
|
}
|
|
|
|
@Override // io.grpc.LoadBalancer
|
|
public final void shutdown() {
|
|
}
|
|
|
|
private NoopLoadBalancer() {
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public final class AutoConfiguredLoadBalancer {
|
|
private LoadBalancer delegate;
|
|
private LoadBalancerProvider delegateProvider;
|
|
private final LoadBalancer.Helper helper;
|
|
final AutoConfiguredLoadBalancerFactory this$0;
|
|
|
|
AutoConfiguredLoadBalancer(AutoConfiguredLoadBalancerFactory autoConfiguredLoadBalancerFactory, LoadBalancer.Helper helper) {
|
|
this.this$0 = autoConfiguredLoadBalancerFactory;
|
|
this.helper = helper;
|
|
LoadBalancerProvider provider = autoConfiguredLoadBalancerFactory.registry.getProvider(autoConfiguredLoadBalancerFactory.defaultPolicy);
|
|
this.delegateProvider = provider;
|
|
if (provider == null) {
|
|
StringBuilder sb = new StringBuilder("Could not find policy '");
|
|
sb.append(autoConfiguredLoadBalancerFactory.defaultPolicy);
|
|
sb.append("'. Make sure its implementation is either registered to LoadBalancerRegistry or included in META-INF/services/io.grpc.LoadBalancerProvider from your jar files.");
|
|
throw new IllegalStateException(sb.toString());
|
|
}
|
|
this.delegate = provider.newLoadBalancer(helper);
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public final Status tryHandleResolvedAddresses(LoadBalancer.ResolvedAddresses resolvedAddresses) {
|
|
List<EquivalentAddressGroup> addresses = resolvedAddresses.getAddresses();
|
|
Attributes attributes = resolvedAddresses.getAttributes();
|
|
ServiceConfigUtil.PolicySelection policySelection = (ServiceConfigUtil.PolicySelection) resolvedAddresses.getLoadBalancingPolicyConfig();
|
|
if (policySelection == null) {
|
|
try {
|
|
AutoConfiguredLoadBalancerFactory autoConfiguredLoadBalancerFactory = this.this$0;
|
|
policySelection = new ServiceConfigUtil.PolicySelection(autoConfiguredLoadBalancerFactory.getProviderOrThrow(autoConfiguredLoadBalancerFactory.defaultPolicy, "using default policy"), null);
|
|
} catch (PolicyException e) {
|
|
this.helper.updateBalancingState(ConnectivityState.TRANSIENT_FAILURE, new FailingPicker(Status.INTERNAL.withDescription(e.getMessage())));
|
|
this.delegate.shutdown();
|
|
this.delegateProvider = null;
|
|
this.delegate = new NoopLoadBalancer();
|
|
return Status.OK;
|
|
}
|
|
}
|
|
if (this.delegateProvider == null || !policySelection.provider.getPolicyName().equals(this.delegateProvider.getPolicyName())) {
|
|
this.helper.updateBalancingState(ConnectivityState.CONNECTING, new EmptyPicker());
|
|
this.delegate.shutdown();
|
|
LoadBalancerProvider loadBalancerProvider = policySelection.provider;
|
|
this.delegateProvider = loadBalancerProvider;
|
|
LoadBalancer loadBalancer = this.delegate;
|
|
this.delegate = loadBalancerProvider.newLoadBalancer(this.helper);
|
|
this.helper.getChannelLogger().log(ChannelLogger.ChannelLogLevel.INFO, "Load balancer changed from {0} to {1}", loadBalancer.getClass().getSimpleName(), this.delegate.getClass().getSimpleName());
|
|
}
|
|
Object obj = policySelection.config;
|
|
if (obj != null) {
|
|
this.helper.getChannelLogger().log(ChannelLogger.ChannelLogLevel.DEBUG, "Load-balancing config: {0}", policySelection.config);
|
|
}
|
|
LoadBalancer delegate = getDelegate();
|
|
if (resolvedAddresses.getAddresses().isEmpty() && !delegate.canHandleEmptyAddressListFromNameResolution()) {
|
|
Status status = Status.UNAVAILABLE;
|
|
StringBuilder sb = new StringBuilder("NameResolver returned no usable address. addrs=");
|
|
sb.append(addresses);
|
|
sb.append(", attrs=");
|
|
sb.append(attributes);
|
|
return status.withDescription(sb.toString());
|
|
}
|
|
delegate.handleResolvedAddresses(LoadBalancer.ResolvedAddresses.newBuilder().setAddresses(resolvedAddresses.getAddresses()).setAttributes(attributes).setLoadBalancingPolicyConfig(obj).build());
|
|
return Status.OK;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public final void handleNameResolutionError(Status status) {
|
|
getDelegate().handleNameResolutionError(status);
|
|
}
|
|
|
|
@Deprecated
|
|
final void handleSubchannelState(LoadBalancer.Subchannel subchannel, ConnectivityStateInfo connectivityStateInfo) {
|
|
getDelegate().handleSubchannelState(subchannel, connectivityStateInfo);
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public final void requestConnection() {
|
|
getDelegate().requestConnection();
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public final void shutdown() {
|
|
this.delegate.shutdown();
|
|
this.delegate = null;
|
|
}
|
|
|
|
final void setDelegate(LoadBalancer loadBalancer) {
|
|
this.delegate = loadBalancer;
|
|
}
|
|
|
|
final LoadBalancerProvider getDelegateProvider() {
|
|
return this.delegateProvider;
|
|
}
|
|
|
|
public final LoadBalancer getDelegate() {
|
|
return this.delegate;
|
|
}
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public LoadBalancerProvider getProviderOrThrow(String str, String str2) throws PolicyException {
|
|
LoadBalancerProvider provider = this.registry.getProvider(str);
|
|
if (provider != null) {
|
|
return provider;
|
|
}
|
|
StringBuilder sb = new StringBuilder("Trying to load '");
|
|
sb.append(str);
|
|
sb.append("' because ");
|
|
sb.append(str2);
|
|
sb.append(", but it's unavailable");
|
|
throw new PolicyException(sb.toString());
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public final NameResolver.ConfigOrError parseLoadBalancerPolicy(Map<String, ?> map) {
|
|
List<ServiceConfigUtil.LbConfig> unwrapLoadBalancingConfigList;
|
|
if (map != null) {
|
|
try {
|
|
unwrapLoadBalancingConfigList = ServiceConfigUtil.unwrapLoadBalancingConfigList(ServiceConfigUtil.getLoadBalancingConfigsFromServiceConfig(map));
|
|
} catch (RuntimeException e) {
|
|
return NameResolver.ConfigOrError.fromError(Status.UNKNOWN.withDescription("can't parse load balancer configuration").withCause(e));
|
|
}
|
|
} else {
|
|
unwrapLoadBalancingConfigList = null;
|
|
}
|
|
if (unwrapLoadBalancingConfigList == null || unwrapLoadBalancingConfigList.isEmpty()) {
|
|
return null;
|
|
}
|
|
return ServiceConfigUtil.selectLbPolicyFromList(unwrapLoadBalancingConfigList, this.registry);
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
/* loaded from: classes6.dex */
|
|
public static final class PolicyException extends Exception {
|
|
private static final long serialVersionUID = 1;
|
|
|
|
private PolicyException(String str) {
|
|
super(str);
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
static final class EmptyPicker extends LoadBalancer.SubchannelPicker {
|
|
private EmptyPicker() {
|
|
}
|
|
|
|
@Override // io.grpc.LoadBalancer.SubchannelPicker
|
|
public final LoadBalancer.PickResult pickSubchannel(LoadBalancer.PickSubchannelArgs pickSubchannelArgs) {
|
|
return LoadBalancer.PickResult.withNoResult();
|
|
}
|
|
|
|
public final String toString() {
|
|
return MoreObjects.toStringHelper((Class<?>) EmptyPicker.class).toString();
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
static final class FailingPicker extends LoadBalancer.SubchannelPicker {
|
|
private final Status failure;
|
|
|
|
FailingPicker(Status status) {
|
|
this.failure = status;
|
|
}
|
|
|
|
@Override // io.grpc.LoadBalancer.SubchannelPicker
|
|
public final LoadBalancer.PickResult pickSubchannel(LoadBalancer.PickSubchannelArgs pickSubchannelArgs) {
|
|
return LoadBalancer.PickResult.withError(this.failure);
|
|
}
|
|
}
|
|
}
|