1818
1919import com .google .common .annotations .VisibleForTesting ;
2020import com .google .common .util .concurrent .ListenableFuture ;
21- import com .google .common .util .concurrent .SettableFuture ;
22- import io .grpc .Internal ;
2321import io .grpc .LongCounterMetricInstrument ;
2422import io .grpc .LongGaugeMetricInstrument ;
2523import io .grpc .MetricInstrumentRegistry ;
2826import io .grpc .MetricRecorder .BatchRecorder ;
2927import io .grpc .MetricRecorder .Registration ;
3028import io .grpc .xds .client .XdsClient ;
31- import io .grpc .xds .client .XdsClient .ResourceCallback ;
3229import io .grpc .xds .client .XdsClient .ResourceMetadata ;
3330import io .grpc .xds .client .XdsClient .ResourceMetadata .ResourceMetadataStatus ;
3431import io .grpc .xds .client .XdsClient .ServerConnectionCallback ;
3734import java .util .Arrays ;
3835import java .util .Collections ;
3936import java .util .HashMap ;
40- import java .util .Locale ;
4137import java .util .Map ;
4238import java .util .concurrent .ExecutionException ;
39+ import java .util .concurrent .Future ;
4340import java .util .concurrent .TimeUnit ;
4441import java .util .concurrent .TimeoutException ;
4542import java .util .logging .Level ;
4946/**
5047 * XdsClientMetricReporter implementation.
5148 */
52- @ Internal
5349final class XdsClientMetricReporterImpl implements XdsClientMetricReporter {
5450
5551 private static final Logger logger = Logger .getLogger (
@@ -139,21 +135,18 @@ void close() {
139135 void reportCallbackMetrics (BatchRecorder recorder , XdsClient xdsClient ) {
140136 MetricReporterCallback callback = new MetricReporterCallback (recorder , target );
141137 try {
142- SettableFuture <Void > reportServerConnectionsCompleted =
143- xdsClient .reportServerConnections (callback );
138+ Future <Void > reportServerConnectionsCompleted = xdsClient .reportServerConnections (callback );
144139
145140 ListenableFuture <Map <XdsResourceType <?>, Map <String , ResourceMetadata >>>
146141 getResourceMetadataCompleted = xdsClient .getSubscribedResourcesMetadataSnapshot ();
147142
148143 Map <XdsResourceType <?>, Map <String , ResourceMetadata >> metadataByType =
149144 getResourceMetadataCompleted .get (10 , TimeUnit .SECONDS );
150145
151- SettableFuture <Void > reportResourceCountsCompleted = computeAndReportResourceCounts (
152- metadataByType , callback );
146+ computeAndReportResourceCounts (metadataByType , callback );
153147
154148 // Normally this shouldn't take long, but adding a timeout to avoid indefinite blocking
155- Void unused1 = reportServerConnectionsCompleted .get (5 , TimeUnit .SECONDS );
156- Void unused2 = reportResourceCountsCompleted .get (5 , TimeUnit .SECONDS );
149+ Void unused = reportServerConnectionsCompleted .get (5 , TimeUnit .SECONDS );
157150 } catch (ExecutionException | TimeoutException | InterruptedException e ) {
158151 if (e instanceof InterruptedException ) {
159152 Thread .currentThread ().interrupt (); // re-set the current thread's interruption state
@@ -162,11 +155,9 @@ void reportCallbackMetrics(BatchRecorder recorder, XdsClient xdsClient) {
162155 }
163156 }
164157
165- private SettableFuture < Void > computeAndReportResourceCounts (
158+ private void computeAndReportResourceCounts (
166159 Map <XdsResourceType <?>, Map <String , ResourceMetadata >> metadataByType ,
167160 MetricReporterCallback callback ) {
168- SettableFuture <Void > future = SettableFuture .create ();
169-
170161 for (Map .Entry <XdsResourceType <?>, Map <String , ResourceMetadata >> metadataByTypeEntry :
171162 metadataByType .entrySet ()) {
172163 XdsResourceType <?> type = metadataByTypeEntry .getKey ();
@@ -180,20 +171,26 @@ private SettableFuture<Void> computeAndReportResourceCounts(
180171 resourceCountsByState .forEach ((cacheState , count ) ->
181172 callback .reportResourceCountGauge (count , cacheState , type .typeUrl ()));
182173 }
183- future .set (null );
184- return future ;
185174 }
186175
187176 private static String cacheStateFromResourceStatus (ResourceMetadataStatus metadataStatus ,
188177 boolean isResourceCached ) {
189- String status = metadataStatus .toString ().toLowerCase (Locale .ROOT );
190- return metadataStatus == ResourceMetadataStatus .NACKED && isResourceCached
191- ? status + "_but_cached" : status ;
178+ switch (metadataStatus ) {
179+ case REQUESTED :
180+ return "requested" ;
181+ case DOES_NOT_EXIST :
182+ return "does_not_exist" ;
183+ case ACKED :
184+ return "acked" ;
185+ case NACKED :
186+ return isResourceCached ? "nacked_but_cached" : "nacked" ;
187+ default :
188+ return "unknown" ;
189+ }
192190 }
193191
194192 @ VisibleForTesting
195- static final class MetricReporterCallback implements ResourceCallback ,
196- ServerConnectionCallback {
193+ static final class MetricReporterCallback implements ServerConnectionCallback {
197194 private final BatchRecorder recorder ;
198195 private final String target ;
199196
@@ -203,7 +200,6 @@ static final class MetricReporterCallback implements ResourceCallback,
203200 }
204201
205202 // TODO(dnvindhya): include the "authority" label once xds.authority is available.
206- @ Override
207203 public void reportResourceCountGauge (long resourceCount , String cacheState ,
208204 String resourceType ) {
209205 recorder .recordLongGauge (RESOURCES_GAUGE , resourceCount ,
0 commit comments