Skip to content

Commit 5881cb6

Browse files
committed
Move interfaces for reporting gauge values to XdsClient and update unit tests
1 parent 4f01651 commit 5881cb6

File tree

6 files changed

+119
-134
lines changed

6 files changed

+119
-134
lines changed

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

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import io.grpc.MetricRecorder.BatchRecorder;
2828
import io.grpc.MetricRecorder.Registration;
2929
import io.grpc.xds.client.XdsClient;
30+
import io.grpc.xds.client.XdsClient.ResourceCallback;
31+
import io.grpc.xds.client.XdsClient.ServerConnectionCallback;
3032
import io.grpc.xds.client.XdsClientMetricReporter;
3133
import java.util.Arrays;
3234
import java.util.Collections;
@@ -125,15 +127,15 @@ void close() {
125127
}
126128

127129
void reportCallbackMetrics(BatchRecorder recorder) {
128-
CallbackMetricReporter callbackMetricReporter = createCallbackMetricReporter(recorder);
130+
MetricReporterCallback callback = new MetricReporterCallback(recorder);
129131
try {
130-
SettableFuture<Void> ret = this.xdsClient.reportResourceCounts(
131-
callbackMetricReporter);
132+
SettableFuture<Void> reportResourceCountsCompleted = this.xdsClient.reportResourceCounts(
133+
callback);
134+
SettableFuture<Void> reportServerConnectionsCompleted =
135+
this.xdsClient.reportServerConnections(callback);
132136
// Normally this shouldn't take long, but adding a timeout to avoid indefinite blocking
133-
Void unused = ret.get(5, TimeUnit.SECONDS);
134-
SettableFuture<Void> ret1 = this.xdsClient.reportServerConnections(callbackMetricReporter);
135-
// Normally this shouldn't take long, but adding a timeout to avoid indefinite blocking
136-
Void unused1 = ret1.get(5, TimeUnit.SECONDS);
137+
Void unused1 = reportResourceCountsCompleted.get(5, TimeUnit.SECONDS);
138+
Void unused2 = reportServerConnectionsCompleted.get(5, TimeUnit.SECONDS);
137139
} catch (Exception e) {
138140
if (e instanceof InterruptedException) {
139141
Thread.currentThread().interrupt(); // re-set the current thread's interruption state
@@ -142,35 +144,27 @@ void reportCallbackMetrics(BatchRecorder recorder) {
142144
}
143145
}
144146

145-
/**
146-
* Allows injecting a custom {@link CallbackMetricReporter} for testing purposes.
147-
*/
148-
@VisibleForTesting
149-
CallbackMetricReporter createCallbackMetricReporter(BatchRecorder recorder) {
150-
return new CallbackMetricReporterImpl(recorder);
151-
}
152-
153147
@VisibleForTesting
154-
static final class CallbackMetricReporterImpl implements
155-
XdsClientMetricReporter.CallbackMetricReporter {
148+
static final class MetricReporterCallback implements ResourceCallback,
149+
ServerConnectionCallback {
156150
private final BatchRecorder recorder;
157151

158-
CallbackMetricReporterImpl(BatchRecorder recorder) {
152+
MetricReporterCallback(BatchRecorder recorder) {
159153
this.recorder = recorder;
160154
}
161155

162-
@Override
163-
public void reportServerConnections(int isConnected, String target, String xdsServer) {
164-
recorder.recordLongGauge(CONNECTED_GAUGE, isConnected, Arrays.asList(target, xdsServer),
165-
Collections.emptyList());
166-
}
167-
168156
// TODO(@dnvindhya): include the "authority" label once xds.authority is available.
169157
@Override
170-
public void reportResourceCounts(long resourceCount, String cacheState, String resourceType,
158+
public void reportResourceCountGauge(long resourceCount, String cacheState, String resourceType,
171159
String target) {
172160
recorder.recordLongGauge(RESOURCES_GAUGE, resourceCount,
173161
Arrays.asList(target, cacheState, resourceType), Collections.emptyList());
174162
}
163+
164+
@Override
165+
public void reportServerConnectionGauge(int isConnected, String target, String xdsServer) {
166+
recorder.recordLongGauge(CONNECTED_GAUGE, isConnected, Arrays.asList(target, xdsServer),
167+
Collections.emptyList());
168+
}
175169
}
176170
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import io.grpc.ExperimentalApi;
3030
import io.grpc.Status;
3131
import io.grpc.xds.client.Bootstrapper.ServerInfo;
32-
import io.grpc.xds.client.XdsClientMetricReporter.CallbackMetricReporter;
3332
import java.net.URI;
3433
import java.net.URISyntaxException;
3534
import java.util.ArrayList;
@@ -380,8 +379,20 @@ public Map<Bootstrapper.ServerInfo, LoadReportClient> getServerLrsClientMap() {
380379
throw new UnsupportedOperationException();
381380
}
382381

382+
/** Callback used to report gauge metric value for resources. */
383+
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);
387+
}
388+
389+
/** Callback used to report a gauge metric value for server connections. */
390+
public interface ServerConnectionCallback {
391+
void reportServerConnectionGauge(int isConnected, String target, String xdsServer);
392+
}
393+
383394
/**
384-
* Reports the number of resources in each cache state through {@link CallbackMetricReporter}.
395+
* Reports the number of resources in each cache state.
385396
*
386397
* <p>Cache state is determined by two factors:
387398
* <ul>
@@ -390,16 +401,14 @@ public Map<Bootstrapper.ServerInfo, LoadReportClient> getServerLrsClientMap() {
390401
* resource.
391402
* </ul>
392403
*/
393-
public SettableFuture<Void> reportResourceCounts(CallbackMetricReporter callbackMetricReporter) {
404+
public SettableFuture<Void> reportResourceCounts(ResourceCallback callback) {
394405
throw new UnsupportedOperationException();
395406
}
396407

397408
/**
398-
* Reports whether xDS client has a working ADS stream to xDS server. Reporting is done through
399-
* {@link CallbackMetricReporter}.
409+
* Reports whether xDS client has a working ADS stream to xDS server.
400410
*/
401-
public SettableFuture<Void> reportServerConnections(
402-
CallbackMetricReporter callbackMetricReporter) {
411+
public SettableFuture<Void> reportServerConnections(ServerConnectionCallback callback) {
403412
throw new UnsupportedOperationException();
404413
}
405414

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
import io.grpc.xds.client.Bootstrapper.ServerInfo;
4343
import io.grpc.xds.client.XdsClient.ResourceMetadata.ResourceMetadataStatus;
4444
import io.grpc.xds.client.XdsClient.ResourceStore;
45-
import io.grpc.xds.client.XdsClientMetricReporter;
46-
import io.grpc.xds.client.XdsClientMetricReporter.CallbackMetricReporter;
4745
import io.grpc.xds.client.XdsLogger.XdsLogLevel;
4846
import java.net.URI;
4947
import java.util.Collection;
@@ -536,12 +534,11 @@ private <T extends ResourceUpdate> void handleResourceUpdate(
536534
}
537535

538536
@Override
539-
public SettableFuture<Void> reportServerConnections(
540-
CallbackMetricReporter callbackMetricReporter) {
537+
public SettableFuture<Void> reportServerConnections(ServerConnectionCallback callback) {
541538
SettableFuture<Void> future = SettableFuture.create();
542539
syncContext.execute(() -> {
543540
serverCpClientMap.forEach((serverInfo, controlPlaneClient) ->
544-
callbackMetricReporter.reportServerConnections(
541+
callback.reportServerConnectionGauge(
545542
controlPlaneClient.hasWorkingAdsStream() ? 1 : 0,
546543
target,
547544
serverInfo.target()));
@@ -551,12 +548,12 @@ public SettableFuture<Void> reportServerConnections(
551548
}
552549

553550
@Override
554-
public SettableFuture<Void> reportResourceCounts(CallbackMetricReporter callbackMetricReporter) {
551+
public SettableFuture<Void> reportResourceCounts(ResourceCallback callback) {
555552
SettableFuture<Void> future = SettableFuture.create();
556553
syncContext.execute(() -> {
557554
Map<XdsResourceType<?>, Map<String, Long>> resourceCountsByType =
558555
getResourceCountsByType();
559-
reportResourceCountsToCallback(callbackMetricReporter, resourceCountsByType);
556+
reportResourceCountsToCallback(callback, resourceCountsByType);
560557
});
561558
future.set(null);
562559
return future;
@@ -568,9 +565,7 @@ private String cacheStateFromResourceStatus(ResourceMetadata metadata, boolean i
568565
? status + "_but_cached" : status;
569566
}
570567

571-
/**
572-
* Calculates number of resources by ResourceType and ResourceSubscriber.metadata.status.
573-
*/
568+
/** Calculates number of resources by ResourceType and ResourceSubscriber.metadata.status. */
574569
Map<XdsResourceType<?>, Map<String, Long>> getResourceCountsByType() {
575570
Map<XdsResourceType<?>, Map<String, Long>> resourceCountsByType = new HashMap<>();
576571
for (XdsResourceType<? extends ResourceUpdate> resourceType : resourceSubscribers.keySet()) {
@@ -586,28 +581,21 @@ Map<XdsResourceType<?>, Map<String, Long>> getResourceCountsByType() {
586581
return resourceCountsByType;
587582
}
588583

589-
/**
590-
* Reports resource counts using the provided callbackMetricReporter.
591-
*/
592-
void reportResourceCountsToCallback(CallbackMetricReporter callbackMetricReporter,
584+
/** Reports resource counts using the provided ResourceCallback. */
585+
void reportResourceCountsToCallback(ResourceCallback callback,
593586
Map<XdsResourceType<?>, Map<String, Long>> resourceCountsByType) {
594587
for (Map.Entry<XdsResourceType<?>, Map<String, Long>> entry :
595588
resourceCountsByType.entrySet()) {
596589
XdsResourceType<?> resourceType = entry.getKey();
597590
Map<String, Long> resourceCountsByState = entry.getValue();
598591
// TODO(@dnvindhya): include the "authority" label once authority is available here.
599592
resourceCountsByState.forEach((cacheState, count) ->
600-
callbackMetricReporter.reportResourceCounts(
601-
count,
602-
cacheState, resourceType.typeUrl(), target
603-
));
593+
callback.reportResourceCountGauge(count, cacheState, resourceType.typeUrl(), target));
604594
}
605595
}
606596

607597

608-
/**
609-
* Tracks a single subscribed resource.
610-
*/
598+
/** Tracks a single subscribed resource. */
611599
private final class ResourceSubscriber<T extends ResourceUpdate> {
612600
@Nullable private final ServerInfo serverInfo;
613601
@Nullable private final ControlPlaneClient controlPlaneClient;

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,4 @@ default void reportResourceUpdates(long validResourceCount, long invalidResource
4747
default void reportServerFailure(long serverFailure, String target, String xdsServer) {
4848
}
4949

50-
/**
51-
* Interface for reporting metrics through callback.
52-
*
53-
*/
54-
interface CallbackMetricReporter {
55-
56-
57-
// TODO(@dnvindhya): include the "authority" label once xds.authority is available.
58-
default void reportResourceCounts(long resourceCount, String cacheState, String resourceType,
59-
String target) {
60-
}
61-
62-
63-
default void reportServerConnections(int isConnected, String target, String xdsServer) {
64-
}
65-
}
6650
}

0 commit comments

Comments
 (0)