Skip to content

Commit 90969b3

Browse files
authored
core: make service config error handling optional implementation (#6564)
1 parent 6b04fc1 commit 90969b3

31 files changed

+10073
-22
lines changed

api/src/main/java/io/grpc/LoadBalancer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ public abstract class LoadBalancer {
112112
* The load-balancing config converted from an JSON object injected by the GRPC library.
113113
*
114114
* <p>{@link NameResolver}s should not produce this attribute.
115+
*
116+
* <p>Deprecated: LB implementations should use parsed object from {@link
117+
* LoadBalancerProvider#parseLoadBalancingPolicyConfig(Map)} instead of raw config.
115118
*/
119+
@Deprecated
116120
@NameResolver.ResolutionResultAttr
117121
public static final Attributes.Key<Map<String, ?>> ATTR_LOAD_BALANCING_CONFIG =
118122
Attributes.Key.create("io.grpc.LoadBalancer.loadBalancingConfig");

api/src/main/java/io/grpc/NameResolver.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ public abstract static class Listener2 implements Listener {
316316
@Deprecated
317317
public final void onAddresses(
318318
List<EquivalentAddressGroup> servers, @ResolutionResultAttr Attributes attributes) {
319+
// TODO(jihuncho) need to promote Listener2 if we want to use ConfigOrError
319320
onResult(
320321
ResolutionResult.newBuilder().setAddresses(servers).setAttributes(attributes).build());
321322
}
@@ -330,8 +331,8 @@ public final void onAddresses(
330331
public abstract void onResult(ResolutionResult resolutionResult);
331332

332333
/**
333-
* Handles an error from the resolver. The listener is responsible for eventually invoking
334-
* {@link NameResolver#refresh()} to re-attempt resolution.
334+
* Handles a name resolving error from the resolver. The listener is responsible for eventually
335+
* invoking {@link NameResolver#refresh()} to re-attempt resolution.
335336
*
336337
* @param error a non-OK status
337338
* @since 1.21.0
@@ -897,6 +898,23 @@ public Status getError() {
897898
return status;
898899
}
899900

901+
@Override
902+
public boolean equals(Object o) {
903+
if (this == o) {
904+
return true;
905+
}
906+
if (o == null || getClass() != o.getClass()) {
907+
return false;
908+
}
909+
ConfigOrError that = (ConfigOrError) o;
910+
return Objects.equal(status, that.status) && Objects.equal(config, that.config);
911+
}
912+
913+
@Override
914+
public int hashCode() {
915+
return Objects.hashCode(status, config);
916+
}
917+
900918
@Override
901919
public String toString() {
902920
if (config != null) {

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ public static ManagedChannelBuilder<?> forTarget(String target) {
9393
private static final long DEFAULT_RETRY_BUFFER_SIZE_IN_BYTES = 1L << 24; // 16M
9494
private static final long DEFAULT_PER_RPC_BUFFER_LIMIT_IN_BYTES = 1L << 20; // 1M
9595

96+
@VisibleForTesting
97+
static final String ENABLE_SERVICE_CONFIG_ERROR_HANDLING_PROPERTY =
98+
"io.grpc.internal.ManagedChannelImpl.enableServiceConfigErrorHandling";
99+
private static final boolean DEFAULT_ENABLE_SERVICE_CONFIG_ERROR_HANDLING =
100+
Boolean.parseBoolean(
101+
System.getProperty(ENABLE_SERVICE_CONFIG_ERROR_HANDLING_PROPERTY, "false"));
102+
96103
ObjectPool<? extends Executor> executorPool = DEFAULT_EXECUTOR_POOL;
97104

98105
ObjectPool<? extends Executor> offloadExecutorPool = DEFAULT_EXECUTOR_POOL;
@@ -152,6 +159,8 @@ public static ManagedChannelBuilder<?> forTarget(String target) {
152159
@Nullable
153160
ProxyDetector proxyDetector;
154161

162+
boolean enableServiceConfigErrorHandling = DEFAULT_ENABLE_SERVICE_CONFIG_ERROR_HANDLING;
163+
155164
/**
156165
* Sets the maximum message size allowed for a single gRPC frame. If an inbound messages
157166
* larger than this limit is received it will not be processed and the RPC will fail with
@@ -454,6 +463,16 @@ public T disableServiceConfigLookUp() {
454463
return thisT();
455464
}
456465

466+
/**
467+
* Enables service config error handling implemented in {@link ManagedChannelImpl2}. By default,
468+
* it is disabled unless system property {@link #ENABLE_SERVICE_CONFIG_ERROR_HANDLING_PROPERTY} is
469+
* set to {@code "true"}.
470+
*/
471+
protected T enableServiceConfigErrorHandling() {
472+
this.enableServiceConfigErrorHandling = true;
473+
return thisT();
474+
}
475+
457476
/**
458477
* Disable or enable stats features. Enabled by default.
459478
*
@@ -514,6 +533,17 @@ protected String checkAuthority(String authority) {
514533

515534
@Override
516535
public ManagedChannel build() {
536+
if (this.enableServiceConfigErrorHandling) {
537+
return new ManagedChannelOrphanWrapper(new ManagedChannelImpl2(
538+
this,
539+
buildTransportFactory(),
540+
// TODO(carl-mastrangelo): Allow clients to pass this in
541+
new ExponentialBackoffPolicy.Provider(),
542+
SharedResourcePool.forResource(GrpcUtil.SHARED_CHANNEL_EXECUTOR),
543+
GrpcUtil.STOPWATCH_SUPPLIER,
544+
getEffectiveInterceptors(),
545+
TimeProvider.SYSTEM_TIME_PROVIDER));
546+
}
517547
return new ManagedChannelOrphanWrapper(new ManagedChannelImpl(
518548
this,
519549
buildTransportFactory(),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.logging.Logger;
4646
import javax.annotation.Nullable;
4747

48+
@SuppressWarnings("deprecation") // migrate to AutoConfiguredLoadBalancerFactory2 is required
4849
public final class AutoConfiguredLoadBalancerFactory {
4950
private static final Logger logger =
5051
Logger.getLogger(AutoConfiguredLoadBalancerFactory.class.getName());

0 commit comments

Comments
 (0)