Skip to content

Commit cbe2659

Browse files
committed
Inject value from XdsClientMetricReporterImpl and addressed review comments
1 parent 4831431 commit cbe2659

File tree

9 files changed

+269
-318
lines changed

9 files changed

+269
-318
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ final class SharedXdsClientPoolProvider implements XdsClientPoolFactory {
5252
private static final boolean LOG_XDS_NODE_ID = Boolean.parseBoolean(
5353
System.getenv("GRPC_LOG_XDS_NODE_ID"));
5454
private static final Logger log = Logger.getLogger(XdsClientImpl.class.getName());
55+
private static final ExponentialBackoffPolicy.Provider BACKOFF_POLICY_PROVIDER =
56+
new ExponentialBackoffPolicy.Provider();
5557

5658
private final Bootstrapper bootstrapper;
5759
private final Object lock = new Object();
@@ -121,8 +123,6 @@ private static class SharedXdsClientPoolProviderHolder {
121123
@VisibleForTesting
122124
protected class RefCountedXdsClientObjectPool implements ObjectPool<XdsClient> {
123125

124-
private final ExponentialBackoffPolicy.Provider backoffPolicyProvider =
125-
new ExponentialBackoffPolicy.Provider();
126126
private final BootstrapInfo bootstrapInfo;
127127
private final String target; // The target associated with the xDS client.
128128
private final XdsClientMetricReporterImpl xdsClientMetricReporter;
@@ -139,7 +139,7 @@ protected class RefCountedXdsClientObjectPool implements ObjectPool<XdsClient> {
139139
MetricRecorder metricRecorder) {
140140
this.bootstrapInfo = checkNotNull(bootstrapInfo);
141141
this.target = target;
142-
this.xdsClientMetricReporter = new XdsClientMetricReporterImpl(metricRecorder);
142+
this.xdsClientMetricReporter = new XdsClientMetricReporterImpl(metricRecorder, target);
143143
}
144144

145145
@Override
@@ -154,12 +154,11 @@ public XdsClient getObject() {
154154
DEFAULT_XDS_TRANSPORT_FACTORY,
155155
bootstrapInfo,
156156
scheduler,
157-
backoffPolicyProvider,
157+
BACKOFF_POLICY_PROVIDER,
158158
GrpcUtil.STOPWATCH_SUPPLIER,
159159
TimeProvider.SYSTEM_TIME_PROVIDER,
160160
MessagePrinter.INSTANCE,
161161
new TlsContextManagerImpl(bootstrapInfo),
162-
getTarget(),
163162
xdsClientMetricReporter);
164163
xdsClientMetricReporter.setXdsClient(xdsClient);
165164
}

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class XdsClientMetricReporterImpl implements XdsClientMetricReporter {
5252
private static final LongGaugeMetricInstrument RESOURCES_GAUGE;
5353

5454
private final MetricRecorder metricRecorder;
55+
private final String target;
5556
@Nullable
5657
private Registration gaugeRegistration = null;
5758
@Nullable
@@ -90,21 +91,22 @@ public class XdsClientMetricReporterImpl implements XdsClientMetricReporter {
9091
"grpc.xds.resource_type"), Collections.emptyList(), false);
9192
}
9293

93-
XdsClientMetricReporterImpl(MetricRecorder metricRecorder) {
94+
XdsClientMetricReporterImpl(MetricRecorder metricRecorder, String target) {
9495
this.metricRecorder = metricRecorder;
96+
this.target = target;
9597
}
9698

9799
@Override
98100
public void reportResourceUpdates(long validResourceCount, long invalidResourceCount,
99-
String target, String xdsServer, String resourceType) {
101+
String xdsServer, String resourceType) {
100102
metricRecorder.addLongCounter(RESOURCE_UPDATES_VALID_COUNTER, validResourceCount,
101103
Arrays.asList(target, xdsServer, resourceType), Collections.emptyList());
102104
metricRecorder.addLongCounter(RESOURCE_UPDATES_INVALID_COUNTER, invalidResourceCount,
103105
Arrays.asList(target, xdsServer, resourceType), Collections.emptyList());
104106
}
105107

106108
@Override
107-
public void reportServerFailure(long serverFailure, String target, String xdsServer) {
109+
public void reportServerFailure(long serverFailure, String xdsServer) {
108110
metricRecorder.addLongCounter(SERVER_FAILURE_COUNTER, serverFailure,
109111
Arrays.asList(target, xdsServer), Collections.emptyList());
110112
}
@@ -127,7 +129,7 @@ void close() {
127129
}
128130

129131
void reportCallbackMetrics(BatchRecorder recorder) {
130-
MetricReporterCallback callback = new MetricReporterCallback(recorder);
132+
MetricReporterCallback callback = new MetricReporterCallback(recorder, target);
131133
try {
132134
SettableFuture<Void> reportResourceCountsCompleted = this.xdsClient.reportResourceCounts(
133135
callback);
@@ -148,21 +150,23 @@ void reportCallbackMetrics(BatchRecorder recorder) {
148150
static final class MetricReporterCallback implements ResourceCallback,
149151
ServerConnectionCallback {
150152
private final BatchRecorder recorder;
153+
private final String target;
151154

152-
MetricReporterCallback(BatchRecorder recorder) {
155+
MetricReporterCallback(BatchRecorder recorder, String target) {
153156
this.recorder = recorder;
157+
this.target = target;
154158
}
155159

156-
// TODO(@dnvindhya): include the "authority" label once xds.authority is available.
160+
// TODO(dnvindhya): include the "authority" label once xds.authority is available.
157161
@Override
158-
public void reportResourceCountGauge(long resourceCount, String cacheState, String resourceType,
159-
String target) {
162+
public void reportResourceCountGauge(long resourceCount, String cacheState,
163+
String resourceType) {
160164
recorder.recordLongGauge(RESOURCES_GAUGE, resourceCount,
161165
Arrays.asList(target, cacheState, resourceType), Collections.emptyList());
162166
}
163167

164168
@Override
165-
public void reportServerConnectionGauge(boolean isConnected, String target, String xdsServer) {
169+
public void reportServerConnectionGauge(boolean isConnected, String xdsServer) {
166170
recorder.recordLongGauge(CONNECTED_GAUGE, isConnected ? 1 : 0,
167171
Arrays.asList(target, xdsServer), Collections.emptyList());
168172
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ final class XdsNameResolver extends NameResolver {
126126
private final ConcurrentMap<String, ClusterRefState> clusterRefs = new ConcurrentHashMap<>();
127127
private final ConfigSelector configSelector = new ConfigSelector();
128128
private final long randomChannelId;
129+
private final MetricRecorder metricRecorder;
129130

130131
private volatile RoutingConfig routingConfig = RoutingConfig.empty;
131-
private volatile MetricRecorder metricRecorder;
132132
private Listener2 listener;
133133
private ObjectPool<XdsClient> xdsClientPool;
134134
private XdsClient xdsClient;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ public void run() {
172172

173173
private void internalStart() {
174174
try {
175+
// TODO(dnvindhya): Add "#server" as "grpc.target" attribute value for
176+
// xDS enabled servers.
175177
xdsClientPool = xdsClientPoolFactory.getOrCreate("", new MetricRecorder() {});
176178
} catch (Exception e) {
177179
StatusException statusException = Status.UNAVAILABLE.withDescription(

xds/src/main/java/io/grpc/xds/client/XdsClient.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,13 @@ public Map<Bootstrapper.ServerInfo, LoadReportClient> getServerLrsClientMap() {
381381

382382
/** Callback used to report gauge metric value for resources. */
383383
public interface ResourceCallback {
384-
// TODO(@dnvindhya): include the "authority" label once xds.authority is available.
385-
void reportResourceCountGauge(long resourceCount, String cacheState, String resourceType,
386-
String target);
384+
// TODO(dnvindhya): include the "authority" label once xds.authority is available.
385+
void reportResourceCountGauge(long resourceCount, String cacheState, String resourceType);
387386
}
388387

389388
/** Callback used to report a gauge metric value for server connections. */
390389
public interface ServerConnectionCallback {
391-
void reportServerConnectionGauge(boolean isConnected, String target, String xdsServer);
390+
void reportServerConnectionGauge(boolean isConnected, String xdsServer);
392391
}
393392

394393
/**

xds/src/main/java/io/grpc/xds/client/XdsClientImpl.java

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ public void uncaughtException(Thread t, Throwable e) {
101101
private final XdsLogger logger;
102102
private volatile boolean isShutdown;
103103
private final MessagePrettyPrinter messagePrinter;
104-
private final String target;
105104
private final XdsClientMetricReporter metricReporter;
106105

107106
public XdsClientImpl(
@@ -113,7 +112,6 @@ public XdsClientImpl(
113112
TimeProvider timeProvider,
114113
MessagePrettyPrinter messagePrinter,
115114
Object securityConfig,
116-
String target,
117115
XdsClientMetricReporter metricReporter) {
118116
this.xdsTransportFactory = xdsTransportFactory;
119117
this.bootstrapInfo = bootstrapInfo;
@@ -123,7 +121,6 @@ public XdsClientImpl(
123121
this.timeProvider = timeProvider;
124122
this.messagePrinter = messagePrinter;
125123
this.securityConfig = securityConfig;
126-
this.target = target;
127124
this.metricReporter = metricReporter;
128125
logId = InternalLogId.allocate("xds-client", null);
129126
logger = XdsLogger.withLogId(logId);
@@ -148,7 +145,7 @@ private void handleStreamClosed(Status error, ServerInfo serverInfo) {
148145
syncContext.throwIfNotInThisSynchronizationContext();
149146
cleanUpResourceTimers();
150147
if (!error.isOk()) {
151-
metricReporter.reportServerFailure(1L, target, serverInfo.target());
148+
metricReporter.reportServerFailure(1L, serverInfo.target());
152149
for (Map<String, ResourceSubscriber<? extends ResourceUpdate>> subscriberMap :
153150
resourceSubscribers.values()) {
154151
for (ResourceSubscriber<? extends ResourceUpdate> subscriber : subscriberMap.values()) {
@@ -474,7 +471,7 @@ private <T extends ResourceUpdate> void handleResourceUpdate(
474471
Map<String, ParsedResource<T>> parsedResources = result.parsedResources;
475472
Set<String> invalidResources = result.invalidResources;
476473
metricReporter.reportResourceUpdates(Long.valueOf(parsedResources.size()),
477-
Long.valueOf(invalidResources.size()), target,
474+
Long.valueOf(invalidResources.size()),
478475
args.getServerInfo().target(), xdsResourceType.typeUrl());
479476

480477
List<String> errors = result.errors;
@@ -539,7 +536,7 @@ public SettableFuture<Void> reportServerConnections(ServerConnectionCallback cal
539536
syncContext.execute(() -> {
540537
serverCpClientMap.forEach((serverInfo, controlPlaneClient) ->
541538
callback.reportServerConnectionGauge(
542-
controlPlaneClient.hasWorkingAdsStream(), target, serverInfo.target()));
539+
controlPlaneClient.hasWorkingAdsStream(), serverInfo.target()));
543540
future.set(null);
544541
});
545542
return future;
@@ -549,9 +546,17 @@ public SettableFuture<Void> reportServerConnections(ServerConnectionCallback cal
549546
public SettableFuture<Void> reportResourceCounts(ResourceCallback callback) {
550547
SettableFuture<Void> future = SettableFuture.create();
551548
syncContext.execute(() -> {
552-
Map<XdsResourceType<?>, Map<String, Long>> resourceCountsByType =
553-
getResourceCountsByType();
554-
reportResourceCountsToCallback(callback, resourceCountsByType);
549+
for (XdsResourceType<? extends ResourceUpdate> resourceType : resourceSubscribers.keySet()) {
550+
Map<String, Long> resourceCountsByState = new HashMap<>();
551+
for (ResourceSubscriber<? extends ResourceUpdate> subscriber :
552+
resourceSubscribers.get(resourceType).values()) {
553+
String cacheState = cacheStateFromResourceStatus(subscriber.metadata,
554+
subscriber.data != null);
555+
resourceCountsByState.compute(cacheState, (k, v) -> (v == null) ? 1 : v + 1);
556+
}
557+
resourceCountsByState.forEach((cacheState, count) ->
558+
callback.reportResourceCountGauge(count, cacheState, resourceType.typeUrl()));
559+
}
555560
future.set(null);
556561
});
557562
return future;
@@ -563,36 +568,6 @@ private String cacheStateFromResourceStatus(ResourceMetadata metadata, boolean i
563568
? status + "_but_cached" : status;
564569
}
565570

566-
/** Calculates number of resources by ResourceType and ResourceSubscriber.metadata.status. */
567-
Map<XdsResourceType<?>, Map<String, Long>> getResourceCountsByType() {
568-
Map<XdsResourceType<?>, Map<String, Long>> resourceCountsByType = new HashMap<>();
569-
for (XdsResourceType<? extends ResourceUpdate> resourceType : resourceSubscribers.keySet()) {
570-
Map<String, Long> resourceCountsByState = new HashMap<>();
571-
for (ResourceSubscriber<? extends ResourceUpdate> subscriber :
572-
resourceSubscribers.get(resourceType).values()) {
573-
String cacheState = cacheStateFromResourceStatus(subscriber.metadata,
574-
subscriber.data != null);
575-
resourceCountsByState.compute(cacheState, (k, v) -> (v == null) ? 1 : v + 1);
576-
}
577-
resourceCountsByType.put(resourceType, resourceCountsByState);
578-
}
579-
return resourceCountsByType;
580-
}
581-
582-
/** Reports resource counts using the provided ResourceCallback. */
583-
void reportResourceCountsToCallback(ResourceCallback callback,
584-
Map<XdsResourceType<?>, Map<String, Long>> resourceCountsByType) {
585-
for (Map.Entry<XdsResourceType<?>, Map<String, Long>> entry :
586-
resourceCountsByType.entrySet()) {
587-
XdsResourceType<?> resourceType = entry.getKey();
588-
Map<String, Long> resourceCountsByState = entry.getValue();
589-
// TODO(@dnvindhya): include the "authority" label once authority is available here.
590-
resourceCountsByState.forEach((cacheState, count) ->
591-
callback.reportResourceCountGauge(count, cacheState, resourceType.typeUrl(), target));
592-
}
593-
}
594-
595-
596571
/** Tracks a single subscribed resource. */
597572
private final class ResourceSubscriber<T extends ResourceUpdate> {
598573
@Nullable private final ServerInfo serverInfo;

xds/src/main/java/io/grpc/xds/client/XdsClientMetricReporter.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,20 @@ public interface XdsClientMetricReporter {
2929
*
3030
* @param validResourceCount Number of resources that were valid.
3131
* @param invalidResourceCount Number of resources that were invalid.
32-
* @param target Target of the gRPC channel.
3332
* @param xdsServer Target URI of the xDS server with which the XdsClient is communicating.
3433
* @param resourceType Type of XDS resource (e.g., "envoy.config.listener.v3.Listener").
3534
*/
3635
default void reportResourceUpdates(long validResourceCount, long invalidResourceCount,
37-
String target, String xdsServer, String resourceType) {
36+
String xdsServer, String resourceType) {
3837
}
3938

4039
/**
4140
* Reports number of xDS servers going from healthy to unhealthy.
4241
*
4342
* @param serverFailure Number of xDS server failures.
44-
* @param target Target of the gRPC channel.
4543
* @param xdsServer Target URI of the xDS server with which the XdsClient is communicating.
4644
*/
47-
default void reportServerFailure(long serverFailure, String target, String xdsServer) {
45+
default void reportServerFailure(long serverFailure, String xdsServer) {
4846
}
4947

5048
}

0 commit comments

Comments
 (0)