xds: Improve XdsNR's selectConfig() variable handling · grpc/grpc-java@199a7ea
@@ -384,17 +384,17 @@ static boolean matchHostName(String hostName, String pattern) {
384384private final class ConfigSelector extends InternalConfigSelector {
385385@Override
386386public Result selectConfig(PickSubchannelArgs args) {
387-String cluster = null;
388-ClientInterceptor filters = null; // null iff cluster is null
389-RouteData selectedRoute = null;
390387RoutingConfig routingCfg;
388+RouteData selectedRoute;
389+String cluster;
390+ClientInterceptor filters;
391391Metadata headers = args.getHeaders();
392+String path = "/" + args.getMethodDescriptor().getFullMethodName();
392393do {
393394routingCfg = routingConfig;
395+selectedRoute = null;
394396for (RouteData route : routingCfg.routes) {
395-if (RoutingUtils.matchRoute(
396-route.routeMatch, "/" + args.getMethodDescriptor().getFullMethodName(),
397-headers, random)) {
397+if (RoutingUtils.matchRoute(route.routeMatch, path, headers, random)) {
398398selectedRoute = route;
399399break;
400400 }
@@ -412,13 +412,14 @@ public Result selectConfig(PickSubchannelArgs args) {
412412cluster = prefixedClusterName(action.cluster());
413413filters = selectedRoute.filterChoices.get(0);
414414 } else if (action.weightedClusters() != null) {
415+// XdsRouteConfigureResource verifies the total weight will not be 0 or exceed uint32
415416long totalWeight = 0;
416417for (ClusterWeight weightedCluster : action.weightedClusters()) {
417418totalWeight += weightedCluster.weight();
418419 }
419420long select = random.nextLong(totalWeight);
420421long accumulator = 0;
421-for (int i = 0; i < action.weightedClusters().size(); i++) {
422+for (int i = 0; ; i++) {
422423ClusterWeight weightedCluster = action.weightedClusters().get(i);
423424accumulator += weightedCluster.weight();
424425if (select < accumulator) {
@@ -431,22 +432,24 @@ public Result selectConfig(PickSubchannelArgs args) {
431432cluster =
432433prefixedClusterSpecifierPluginName(action.namedClusterSpecifierPluginConfig().name());
433434filters = selectedRoute.filterChoices.get(0);
435+ } else {
436+// updateRoutes() discards routes with unknown actions
437+throw new AssertionError();
434438 }
435439 } while (!retainCluster(cluster));
440+441+final RouteAction routeAction = selectedRoute.routeAction;
436442Long timeoutNanos = null;
437443if (enableTimeout) {
438-if (selectedRoute != null) {
439-timeoutNanos = selectedRoute.routeAction.timeoutNano();
440- }
444+timeoutNanos = routeAction.timeoutNano();
441445if (timeoutNanos == null) {
442446timeoutNanos = routingCfg.fallbackTimeoutNano;
443447 }
444448if (timeoutNanos <= 0) {
445449timeoutNanos = null;
446450 }
447451 }
448-RetryPolicy retryPolicy =
449-selectedRoute == null ? null : selectedRoute.routeAction.retryPolicy();
452+RetryPolicy retryPolicy = routeAction.retryPolicy();
450453// TODO(chengyuanzhang): avoid service config generation and parsing for each call.
451454Map<String, ?> rawServiceConfig =
452455generateServiceConfigWithMethodConfig(timeoutNanos, retryPolicy);
@@ -459,8 +462,7 @@ public Result selectConfig(PickSubchannelArgs args) {
459462"Failed to parse service config (method config)"));
460463 }
461464final String finalCluster = cluster;
462-final long hash = generateHash(selectedRoute.routeAction.hashPolicies(), headers);
463-RouteData finalSelectedRoute = selectedRoute;
465+final long hash = generateHash(routeAction.hashPolicies(), headers);
464466class ClusterSelectionInterceptor implements ClientInterceptor {
465467@Override
466468public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
@@ -469,7 +471,7 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
469471CallOptions callOptionsForCluster =
470472callOptions.withOption(CLUSTER_SELECTION_KEY, finalCluster)
471473 .withOption(RPC_HASH_KEY, hash);
472-if (finalSelectedRoute.routeAction.autoHostRewrite()) {
474+if (routeAction.autoHostRewrite()) {
473475callOptionsForCluster = callOptionsForCluster.withOption(AUTO_HOST_REWRITE_KEY, true);
474476 }
475477return new SimpleForwardingClientCall<ReqT, RespT>(
@@ -757,6 +759,8 @@ private void updateRoutes(List<VirtualHost> virtualHosts, long httpMaxStreamDura
757759 }
758760ClientInterceptor filters = createFilters(filterConfigs, virtualHost, route, null);
759761routesData.add(new RouteData(route.routeMatch(), route.routeAction(), filters));
762+ } else {
763+// Discard route
760764 }
761765 }
762766