Skip to content

Commit d7ef02a

Browse files
committed
Merge branch 'master' of https://github.com/grpc/grpc-java
2 parents 0f96110 + 4725ced commit d7ef02a

23 files changed

Lines changed: 733 additions & 84 deletions

android/src/main/java/io/grpc/android/UdsChannelBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.grpc.ExperimentalApi;
2222
import io.grpc.InsecureChannelCredentials;
2323
import io.grpc.ManagedChannelBuilder;
24+
import io.grpc.internal.GrpcUtil;
2425
import java.lang.reflect.InvocationTargetException;
2526
import javax.annotation.Nullable;
2627
import javax.net.SocketFactory;
@@ -81,7 +82,7 @@ public static ManagedChannelBuilder<?> forPath(String path, Namespace namespace)
8182
OKHTTP_CHANNEL_BUILDER_CLASS
8283
.getMethod("socketFactory", SocketFactory.class)
8384
.invoke(builder, new UdsSocketFactory(path, namespace));
84-
return builder;
85+
return builder.proxyDetector(GrpcUtil.NOOP_PROXY_DETECTOR);
8586
} catch (IllegalAccessException e) {
8687
throw new RuntimeException("Failed to create OkHttpChannelBuilder", e);
8788
} catch (NoSuchMethodException e) {

binder/src/main/java/io/grpc/binder/internal/BinderClientTransport.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,6 @@ public final class BinderClientTransport extends BinderTransport
8787
@GuardedBy("this")
8888
private ScheduledFuture<?> readyTimeoutFuture; // != null iff timeout scheduled.
8989

90-
@GuardedBy("this")
91-
@Nullable
92-
private ListenableFuture<Status> authResultFuture; // null before we check auth.
93-
94-
@GuardedBy("this")
95-
@Nullable
96-
private ListenableFuture<Status> preAuthResultFuture; // null before we pre-auth.
9790

9891
/**
9992
* Constructs a new transport instance.
@@ -193,7 +186,8 @@ private void preAuthorize(ServiceInfo serviceInfo) {
193186
// unauthorized server a chance to run, but the connection will still fail by SecurityPolicy
194187
// check later in handshake. Pre-auth remains effective at mitigating abuse because malware
195188
// can't typically control the exact timing of its installation.
196-
preAuthResultFuture = checkServerAuthorizationAsync(serviceInfo.applicationInfo.uid);
189+
ListenableFuture<Status> preAuthResultFuture =
190+
register(checkServerAuthorizationAsync(serviceInfo.applicationInfo.uid));
197191
Futures.addCallback(
198192
preAuthResultFuture,
199193
new FutureCallback<Status>() {
@@ -314,12 +308,6 @@ void notifyTerminated() {
314308
readyTimeoutFuture.cancel(false);
315309
readyTimeoutFuture = null;
316310
}
317-
if (preAuthResultFuture != null) {
318-
preAuthResultFuture.cancel(false); // No effect if already complete.
319-
}
320-
if (authResultFuture != null) {
321-
authResultFuture.cancel(false); // No effect if already complete.
322-
}
323311
serviceBinding.unbind();
324312
clientTransportListener.transportTerminated();
325313
}
@@ -339,7 +327,8 @@ protected void handleSetupTransport(Parcel parcel) {
339327
} else {
340328
restrictIncomingBinderToCallsFrom(remoteUid);
341329
attributes = setSecurityAttrs(attributes, remoteUid);
342-
authResultFuture = checkServerAuthorizationAsync(remoteUid);
330+
ListenableFuture<Status> authResultFuture =
331+
register(checkServerAuthorizationAsync(remoteUid));
343332
Futures.addCallback(
344333
authResultFuture,
345334
new FutureCallback<Status>() {
@@ -398,6 +387,7 @@ protected void handlePingResponse(Parcel parcel) {
398387
pingTracker.onPingResponse(parcel.readInt());
399388
}
400389

390+
401391
private static ClientStream newFailingClientStream(
402392
Status failure, Attributes attributes, Metadata headers, ClientStreamTracer[] tracers) {
403393
StatsTraceContext statsTraceContext =

binder/src/main/java/io/grpc/binder/internal/BinderTransport.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@
4545
import java.util.ArrayList;
4646
import java.util.Iterator;
4747
import java.util.LinkedHashSet;
48+
import java.util.List;
4849
import java.util.Map;
4950
import java.util.NoSuchElementException;
5051
import java.util.concurrent.ConcurrentHashMap;
52+
import java.util.concurrent.Future;
5153
import java.util.concurrent.ScheduledExecutorService;
5254
import java.util.logging.Level;
5355
import java.util.logging.Logger;
@@ -166,6 +168,9 @@ protected enum TransportState {
166168
@GuardedBy("this")
167169
private final LinkedHashSet<Integer> callIdsToNotifyWhenReady = new LinkedHashSet<>();
168170

171+
@GuardedBy("this")
172+
private final List<Future<?>> ownedFutures = new ArrayList<>(); // To cancel upon terminate.
173+
169174
@GuardedBy("this")
170175
protected Attributes attributes;
171176

@@ -249,6 +254,13 @@ void releaseExecutors() {
249254
executorServicePool.returnObject(scheduledExecutorService);
250255
}
251256

257+
// Registers the specified future for eventual safe cancellation upon shutdown/terminate.
258+
@GuardedBy("this")
259+
protected final <T extends Future<?>> T register(T future) {
260+
ownedFutures.add(future);
261+
return future;
262+
}
263+
252264
@GuardedBy("this")
253265
boolean inState(TransportState transportState) {
254266
return this.transportState == transportState;
@@ -299,6 +311,8 @@ final void shutdownInternal(Status shutdownStatus, boolean forceTerminate) {
299311
sendShutdownTransaction();
300312
ArrayList<Inbound<?>> calls = new ArrayList<>(ongoingCalls.values());
301313
ongoingCalls.clear();
314+
ArrayList<Future<?>> futuresToCancel = new ArrayList<>(ownedFutures);
315+
ownedFutures.clear();
302316
scheduledExecutorService.execute(
303317
() -> {
304318
for (Inbound<?> inbound : calls) {
@@ -310,6 +324,11 @@ final void shutdownInternal(Status shutdownStatus, boolean forceTerminate) {
310324
notifyTerminated();
311325
}
312326
releaseExecutors();
327+
328+
for (Future<?> future : futuresToCancel) {
329+
// Not holding any locks here just in case some listener runs on a direct Executor.
330+
future.cancel(false); // No effect if already isDone().
331+
}
313332
});
314333
}
315334
}

xds/src/main/java/io/grpc/xds/CdsLoadBalancer2.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,17 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
122122
result.maxConcurrentRequests(),
123123
result.upstreamTlsContext(),
124124
result.filterMetadata(),
125-
result.outlierDetection());
125+
result.outlierDetection(),
126+
result.backendMetricPropagation());
126127
} else {
127128
instance = DiscoveryMechanism.forLogicalDns(
128129
clusterName,
129130
result.dnsHostName(),
130131
result.lrsServerInfo(),
131132
result.maxConcurrentRequests(),
132133
result.upstreamTlsContext(),
133-
result.filterMetadata());
134+
result.filterMetadata(),
135+
result.backendMetricPropagation());
134136
}
135137
gracefulConfig = GracefulSwitchLoadBalancer.createLoadBalancingPolicyConfig(
136138
lbRegistry.getProvider(CLUSTER_RESOLVER_POLICY_NAME),

xds/src/main/java/io/grpc/xds/ClusterImplLoadBalancer.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.grpc.xds;
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
20+
import static io.grpc.xds.client.LoadStatsManager2.isEnabledOrcaLrsPropagation;
2021

2122
import com.google.common.annotations.VisibleForTesting;
2223
import com.google.common.base.MoreObjects;
@@ -46,6 +47,7 @@
4647
import io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext;
4748
import io.grpc.xds.ThreadSafeRandom.ThreadSafeRandomImpl;
4849
import io.grpc.xds.XdsNameResolverProvider.CallCounterProvider;
50+
import io.grpc.xds.client.BackendMetricPropagation;
4951
import io.grpc.xds.client.Bootstrapper.ServerInfo;
5052
import io.grpc.xds.client.LoadStatsManager2.ClusterDropStats;
5153
import io.grpc.xds.client.LoadStatsManager2.ClusterLocalityStats;
@@ -149,6 +151,7 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
149151
childLbHelper.updateMaxConcurrentRequests(config.maxConcurrentRequests);
150152
childLbHelper.updateSslContextProviderSupplier(config.tlsContext);
151153
childLbHelper.updateFilterMetadata(config.filterMetadata);
154+
childLbHelper.updateBackendMetricPropagation(config.backendMetricPropagation);
152155

153156
childSwitchLb.handleResolvedAddresses(
154157
resolvedAddresses.toBuilder()
@@ -209,6 +212,8 @@ private final class ClusterImplLbHelper extends ForwardingLoadBalancerHelper {
209212
private Map<String, Struct> filterMetadata = ImmutableMap.of();
210213
@Nullable
211214
private final ServerInfo lrsServerInfo;
215+
@Nullable
216+
private BackendMetricPropagation backendMetricPropagation;
212217

213218
private ClusterImplLbHelper(AtomicLong inFlights, @Nullable ServerInfo lrsServerInfo) {
214219
this.inFlights = checkNotNull(inFlights, "inFlights");
@@ -321,7 +326,7 @@ private ClusterLocality createClusterLocalityFromAttributes(Attributes addressAt
321326
(lrsServerInfo == null)
322327
? null
323328
: xdsClient.addClusterLocalityStats(lrsServerInfo, cluster,
324-
edsServiceName, locality);
329+
edsServiceName, locality, backendMetricPropagation);
325330

326331
return new ClusterLocality(localityStats, localityName);
327332
}
@@ -371,6 +376,11 @@ private void updateFilterMetadata(Map<String, Struct> filterMetadata) {
371376
this.filterMetadata = ImmutableMap.copyOf(filterMetadata);
372377
}
373378

379+
private void updateBackendMetricPropagation(
380+
@Nullable BackendMetricPropagation backendMetricPropagation) {
381+
this.backendMetricPropagation = backendMetricPropagation;
382+
}
383+
374384
private class RequestLimitingSubchannelPicker extends SubchannelPicker {
375385
private final SubchannelPicker delegate;
376386
private final List<DropOverload> dropPolicies;
@@ -506,11 +516,19 @@ private OrcaPerRpcListener(ClusterLocalityStats stats) {
506516
}
507517

508518
/**
509-
* Copies {@link MetricReport#getNamedMetrics()} to {@link ClusterLocalityStats} such that it is
510-
* included in the snapshot for the LRS report sent to the LRS server.
519+
* Copies ORCA metrics from {@link MetricReport} to {@link ClusterLocalityStats}
520+
* such that they are included in the snapshot for the LRS report sent to the LRS server.
521+
* This includes both top-level metrics (CPU, memory, application utilization) and named
522+
* metrics, filtered according to the backend metric propagation configuration.
511523
*/
512524
@Override
513525
public void onLoadReport(MetricReport report) {
526+
if (isEnabledOrcaLrsPropagation) {
527+
stats.recordTopLevelMetrics(
528+
report.getCpuUtilization(),
529+
report.getMemoryUtilization(),
530+
report.getApplicationUtilization());
531+
}
514532
stats.recordBackendLoadMetricStats(report.getNamedMetrics());
515533
}
516534
}

xds/src/main/java/io/grpc/xds/ClusterImplLoadBalancerProvider.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.grpc.Status;
3232
import io.grpc.xds.Endpoints.DropOverload;
3333
import io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext;
34+
import io.grpc.xds.client.BackendMetricPropagation;
3435
import io.grpc.xds.client.Bootstrapper.ServerInfo;
3536
import java.util.ArrayList;
3637
import java.util.Collections;
@@ -98,11 +99,14 @@ static final class ClusterImplConfig {
9899
// Provides the direct child policy and its config.
99100
final Object childConfig;
100101
final Map<String, Struct> filterMetadata;
102+
@Nullable
103+
final BackendMetricPropagation backendMetricPropagation;
101104

102105
ClusterImplConfig(String cluster, @Nullable String edsServiceName,
103106
@Nullable ServerInfo lrsServerInfo, @Nullable Long maxConcurrentRequests,
104107
List<DropOverload> dropCategories, Object childConfig,
105-
@Nullable UpstreamTlsContext tlsContext, Map<String, Struct> filterMetadata) {
108+
@Nullable UpstreamTlsContext tlsContext, Map<String, Struct> filterMetadata,
109+
@Nullable BackendMetricPropagation backendMetricPropagation) {
106110
this.cluster = checkNotNull(cluster, "cluster");
107111
this.edsServiceName = edsServiceName;
108112
this.lrsServerInfo = lrsServerInfo;
@@ -112,6 +116,7 @@ static final class ClusterImplConfig {
112116
this.dropCategories = Collections.unmodifiableList(
113117
new ArrayList<>(checkNotNull(dropCategories, "dropCategories")));
114118
this.childConfig = checkNotNull(childConfig, "childConfig");
119+
this.backendMetricPropagation = backendMetricPropagation;
115120
}
116121

117122
@Override

xds/src/main/java/io/grpc/xds/ClusterResolverLoadBalancer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig;
4545
import io.grpc.xds.XdsConfig.XdsClusterConfig;
4646
import io.grpc.xds.XdsEndpointResource.EdsUpdate;
47+
// import io.grpc.xds.client.BackendMetricPropagation;]
4748
import io.grpc.xds.client.Locality;
4849
import io.grpc.xds.client.XdsLogger;
4950
import io.grpc.xds.client.XdsLogger.XdsLogLevel;
@@ -336,7 +337,7 @@ private static Map<String, PriorityChildConfig> generatePriorityChildConfigs(
336337
new ClusterImplConfig(
337338
discovery.cluster, discovery.edsServiceName, discovery.lrsServerInfo,
338339
discovery.maxConcurrentRequests, dropOverloads, endpointLbConfig,
339-
discovery.tlsContext, discovery.filterMetadata);
340+
discovery.tlsContext, discovery.filterMetadata, discovery.backendMetricPropagation);
340341
LoadBalancerProvider clusterImplLbProvider =
341342
lbRegistry.getProvider(XdsLbPolicies.CLUSTER_IMPL_POLICY_NAME);
342343
Object priorityChildPolicy = GracefulSwitchLoadBalancer.createLoadBalancingPolicyConfig(

xds/src/main/java/io/grpc/xds/ClusterResolverLoadBalancerProvider.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.grpc.Status;
3131
import io.grpc.xds.EnvoyServerProtoData.OutlierDetection;
3232
import io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext;
33+
import io.grpc.xds.client.BackendMetricPropagation;
3334
import io.grpc.xds.client.Bootstrapper.ServerInfo;
3435
import java.util.Map;
3536
import java.util.Objects;
@@ -152,6 +153,8 @@ static final class DiscoveryMechanism {
152153
@Nullable
153154
final OutlierDetection outlierDetection;
154155
final Map<String, Struct> filterMetadata;
156+
@Nullable
157+
final BackendMetricPropagation backendMetricPropagation;
155158

156159
enum Type {
157160
EDS,
@@ -161,7 +164,8 @@ enum Type {
161164
private DiscoveryMechanism(String cluster, Type type, @Nullable String edsServiceName,
162165
@Nullable String dnsHostName, @Nullable ServerInfo lrsServerInfo,
163166
@Nullable Long maxConcurrentRequests, @Nullable UpstreamTlsContext tlsContext,
164-
Map<String, Struct> filterMetadata, @Nullable OutlierDetection outlierDetection) {
167+
Map<String, Struct> filterMetadata, @Nullable OutlierDetection outlierDetection,
168+
@Nullable BackendMetricPropagation backendMetricPropagation) {
165169
this.cluster = checkNotNull(cluster, "cluster");
166170
this.type = checkNotNull(type, "type");
167171
this.edsServiceName = edsServiceName;
@@ -171,27 +175,33 @@ private DiscoveryMechanism(String cluster, Type type, @Nullable String edsServic
171175
this.tlsContext = tlsContext;
172176
this.filterMetadata = ImmutableMap.copyOf(checkNotNull(filterMetadata, "filterMetadata"));
173177
this.outlierDetection = outlierDetection;
178+
this.backendMetricPropagation = backendMetricPropagation;
174179
}
175180

176181
static DiscoveryMechanism forEds(String cluster, @Nullable String edsServiceName,
177182
@Nullable ServerInfo lrsServerInfo, @Nullable Long maxConcurrentRequests,
178183
@Nullable UpstreamTlsContext tlsContext, Map<String, Struct> filterMetadata,
179-
OutlierDetection outlierDetection) {
180-
return new DiscoveryMechanism(cluster, Type.EDS, edsServiceName, null, lrsServerInfo,
181-
maxConcurrentRequests, tlsContext, filterMetadata, outlierDetection);
184+
OutlierDetection outlierDetection,
185+
@Nullable BackendMetricPropagation backendMetricPropagation) {
186+
return new DiscoveryMechanism(cluster, Type.EDS, edsServiceName,
187+
null, lrsServerInfo, maxConcurrentRequests, tlsContext,
188+
filterMetadata, outlierDetection, backendMetricPropagation);
182189
}
183190

184191
static DiscoveryMechanism forLogicalDns(String cluster, String dnsHostName,
185192
@Nullable ServerInfo lrsServerInfo, @Nullable Long maxConcurrentRequests,
186-
@Nullable UpstreamTlsContext tlsContext, Map<String, Struct> filterMetadata) {
193+
@Nullable UpstreamTlsContext tlsContext, Map<String, Struct> filterMetadata,
194+
@Nullable BackendMetricPropagation backendMetricPropagation) {
187195
return new DiscoveryMechanism(cluster, Type.LOGICAL_DNS, null, dnsHostName,
188-
lrsServerInfo, maxConcurrentRequests, tlsContext, filterMetadata, null);
196+
lrsServerInfo, maxConcurrentRequests, tlsContext, filterMetadata, null,
197+
backendMetricPropagation);
189198
}
190199

191200
@Override
192201
public int hashCode() {
193202
return Objects.hash(cluster, type, lrsServerInfo, maxConcurrentRequests, tlsContext,
194-
edsServiceName, dnsHostName, filterMetadata, outlierDetection);
203+
edsServiceName, dnsHostName, filterMetadata,
204+
outlierDetection, backendMetricPropagation);
195205
}
196206

197207
@Override

0 commit comments

Comments
 (0)