Skip to content

Commit 163e103

Browse files
committed
simplifying
1 parent 33d7f54 commit 163e103

File tree

5 files changed

+63
-43
lines changed

5 files changed

+63
-43
lines changed

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@
118118
import java.util.Objects;
119119
import java.util.Set;
120120
import java.util.concurrent.ExecutionException;
121-
import java.util.concurrent.Future;
122121
import java.util.concurrent.ScheduledExecutorService;
123122
import java.util.concurrent.TimeUnit;
124123
import java.util.concurrent.TimeoutException;
@@ -1972,7 +1971,37 @@ public void run() {
19721971
future.set(getSubscribedResourcesMetadataUnsafe(type));
19731972
}
19741973
});
1975-
return awaitSubscribedResourcesMetadata(future);
1974+
try {
1975+
return future.get(METADATA_SYNC_TIMEOUT_SEC, TimeUnit.SECONDS);
1976+
} catch (InterruptedException | ExecutionException | TimeoutException e) {
1977+
throw new MetadataLoadException(e);
1978+
}
1979+
}
1980+
1981+
@Override
1982+
Map<ResourceType, Map<String, ResourceMetadata>> getSubscribedResourcesMetadataSnapshot() {
1983+
final SettableFuture<Map<ResourceType, Map<String, ResourceMetadata>>> future =
1984+
SettableFuture.create();
1985+
syncContext.execute(new Runnable() {
1986+
@Override
1987+
public void run() {
1988+
// A map from ResourceType to a map (ResourceName: ResourceMetadata)
1989+
ImmutableMap.Builder<ResourceType, Map<String, ResourceMetadata>> metadataSnapshot =
1990+
ImmutableMap.builder();
1991+
for (ResourceType type : ResourceType.values()) {
1992+
if (type == ResourceType.UNKNOWN) {
1993+
continue;
1994+
}
1995+
metadataSnapshot.put(type, getSubscribedResourcesMetadataUnsafe(type));
1996+
}
1997+
future.set(metadataSnapshot.build());
1998+
}
1999+
});
2000+
try {
2001+
return future.get(METADATA_SYNC_TIMEOUT_SEC, TimeUnit.SECONDS);
2002+
} catch (InterruptedException | ExecutionException | TimeoutException e) {
2003+
throw new MetadataLoadException(e);
2004+
}
19762005
}
19772006

19782007
private Map<String, ResourceMetadata> getSubscribedResourcesMetadataUnsafe(ResourceType type) {
@@ -1983,19 +2012,6 @@ private Map<String, ResourceMetadata> getSubscribedResourcesMetadataUnsafe(Resou
19832012
return metadataMap.build();
19842013
}
19852014

1986-
private static Map<String, ResourceMetadata> awaitSubscribedResourcesMetadata(
1987-
Future<Map<String, ResourceMetadata>> future) {
1988-
try {
1989-
return future.get(METADATA_SYNC_TIMEOUT_SEC, TimeUnit.SECONDS);
1990-
} catch (InterruptedException e) {
1991-
throw new MetadataLoadException(e);
1992-
} catch (ExecutionException e) {
1993-
throw new MetadataLoadException(e);
1994-
} catch (TimeoutException e) {
1995-
throw new MetadataLoadException(e);
1996-
}
1997-
}
1998-
19992015
@Override
20002016
TlsContextManager getTlsContextManager() {
20012017
return tlsContextManager;

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,15 @@ private ClientStatusResponse getConfigDumpForRequest(ClientStatusRequest request
143143
static ClientConfig getClientConfigForXdsClient(XdsClient xdsClient) {
144144
ClientConfig.Builder builder = ClientConfig.newBuilder()
145145
.setNode(xdsClient.getBootstrapInfo().node().toEnvoyProtoNode());
146-
for (ResourceType type : ResourceType.values()) {
147-
if (type == ResourceType.UNKNOWN) {
148-
continue;
149-
}
150-
Map<String, ResourceMetadata> metadataMap = xdsClient.getSubscribedResourcesMetadata(type);
151-
for (String resourceName : metadataMap.keySet()) {
152-
ResourceMetadata metadata = metadataMap.get(resourceName);
146+
for (Map.Entry<ResourceType, Map<String, ResourceMetadata>> metadataSnapshotEntry
147+
: xdsClient.getSubscribedResourcesMetadataSnapshot().entrySet()) {
148+
ResourceType type = metadataSnapshotEntry.getKey();
149+
for (Map.Entry<String, ResourceMetadata> metadataEntry
150+
: metadataSnapshotEntry.getValue().entrySet()) {
151+
ResourceMetadata metadata = metadataEntry.getValue();
153152
GenericXdsConfig.Builder genericXdsConfigBuilder = GenericXdsConfig.newBuilder()
154153
.setTypeUrl(type.typeUrl())
155-
.setName(resourceName)
154+
.setName(metadataEntry.getKey())
156155
.setClientStatus(metadataStatusToClientStatus(metadata.getStatus()));
157156
if (metadata.getRawResource() != null) {
158157
genericXdsConfigBuilder
@@ -161,6 +160,7 @@ static ClientConfig getClientConfigForXdsClient(XdsClient xdsClient) {
161160
.setXdsConfig(metadata.getRawResource());
162161
}
163162
if (metadata.getStatus() == ResourceMetadataStatus.NACKED) {
163+
assert metadata.getErrorState() != null;
164164
genericXdsConfigBuilder
165165
.setErrorState(metadataUpdateFailureStateToProto(metadata.getErrorState()));
166166
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,15 @@ Map<String, ResourceMetadata> getSubscribedResourcesMetadata(ResourceType type)
498498
throw new UnsupportedOperationException();
499499
}
500500

501+
/**
502+
* Returns the map containing the {@link ResourceMetadata} of the subscribed resources for the
503+
* given resource type, indexed by the resource name.
504+
*/
505+
// Must be synchronized.
506+
Map<ResourceType, Map<String, ResourceMetadata>> getSubscribedResourcesMetadataSnapshot() {
507+
throw new UnsupportedOperationException();
508+
}
509+
501510
/**
502511
* Registers a data watcher for the given LDS resource.
503512
*/

xds/src/test/java/io/grpc/xds/ClientXdsClientTestBase.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,12 @@ protected static boolean matchErrorDetail(
379379

380380
private void verifySubscribedResourcesMetadataSizes(
381381
int ldsSize, int cdsSize, int rdsSize, int edsSize) {
382-
assertThat(xdsClient.getSubscribedResourcesMetadata(LDS)).hasSize(ldsSize);
383-
assertThat(xdsClient.getSubscribedResourcesMetadata(CDS)).hasSize(cdsSize);
384-
assertThat(xdsClient.getSubscribedResourcesMetadata(RDS)).hasSize(rdsSize);
385-
assertThat(xdsClient.getSubscribedResourcesMetadata(EDS)).hasSize(edsSize);
382+
Map<ResourceType, Map<String, ResourceMetadata>> subscribedResourcesMetadata =
383+
xdsClient.getSubscribedResourcesMetadataSnapshot();
384+
assertThat(subscribedResourcesMetadata.get(LDS)).hasSize(ldsSize);
385+
assertThat(subscribedResourcesMetadata.get(CDS)).hasSize(cdsSize);
386+
assertThat(subscribedResourcesMetadata.get(RDS)).hasSize(rdsSize);
387+
assertThat(subscribedResourcesMetadata.get(EDS)).hasSize(edsSize);
386388
}
387389

388390
/** Verify the resource requested, but not updated. */
@@ -435,7 +437,7 @@ private ResourceMetadata verifyResourceMetadata(
435437
ResourceType type, String resourceName, Any rawResource, ResourceMetadataStatus status,
436438
String versionInfo, long updateTimeNanos, boolean hasErrorState) {
437439
ResourceMetadata resourceMetadata =
438-
xdsClient.getSubscribedResourcesMetadata(type).get(resourceName);
440+
xdsClient.getSubscribedResourcesMetadataSnapshot().get(type).get(resourceName);
439441
assertThat(resourceMetadata).isNotNull();
440442
String name = type.toString() + " resource '" + resourceName + "' metadata field ";
441443
assertWithMessage(name + "status").that(resourceMetadata.getStatus()).isEqualTo(status);

xds/src/test/java/io/grpc/xds/CsdsServiceTest.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Bootstrapper.BootstrapInfo getBootstrapInfo() {
8181
}
8282

8383
@Override
84-
Map<String, ResourceMetadata> getSubscribedResourcesMetadata(ResourceType type) {
84+
Map<ResourceType, Map<String, ResourceMetadata>> getSubscribedResourcesMetadataSnapshot() {
8585
return ImmutableMap.of();
8686
}
8787
};
@@ -302,20 +302,13 @@ Bootstrapper.BootstrapInfo getBootstrapInfo() {
302302
}
303303

304304
@Override
305-
Map<String, ResourceMetadata> getSubscribedResourcesMetadata(ResourceType type) {
306-
switch (type) {
307-
case LDS:
308-
return ImmutableMap.of("subscribedResourceName." + type.name(), METADATA_ACKED_LDS);
309-
case RDS:
310-
return ImmutableMap.of("subscribedResourceName." + type.name(), METADATA_ACKED_RDS);
311-
case CDS:
312-
return ImmutableMap.of("subscribedResourceName." + type.name(), METADATA_ACKED_CDS);
313-
case EDS:
314-
return ImmutableMap.of("subscribedResourceName." + type.name(), METADATA_ACKED_EDS);
315-
case UNKNOWN:
316-
default:
317-
throw new AssertionError("Unexpected resource name");
318-
}
305+
Map<ResourceType, Map<String, ResourceMetadata>> getSubscribedResourcesMetadataSnapshot() {
306+
return new ImmutableMap.Builder<ResourceType, Map<String, ResourceMetadata>>()
307+
.put(LDS, ImmutableMap.of("subscribedResourceName.LDS", METADATA_ACKED_LDS))
308+
.put(RDS, ImmutableMap.of("subscribedResourceName.RDS", METADATA_ACKED_RDS))
309+
.put(CDS, ImmutableMap.of("subscribedResourceName.CDS", METADATA_ACKED_CDS))
310+
.put(EDS, ImmutableMap.of("subscribedResourceName.EDS", METADATA_ACKED_EDS))
311+
.build();
319312
}
320313
});
321314

0 commit comments

Comments
 (0)