16
16
17
17
package io .grpc .xds ;
18
18
19
- import com .google .common .annotations .VisibleForTesting ;
19
+ import static com .google .common .base .Preconditions .checkNotNull ;
20
+
20
21
import com .google .common .base .MoreObjects ;
21
22
import com .google .common .base .Objects ;
22
23
import com .google .common .collect .ImmutableMap ;
30
31
import io .grpc .internal .JsonUtil ;
31
32
import io .grpc .internal .ServiceConfigUtil ;
32
33
import io .grpc .internal .ServiceConfigUtil .LbConfig ;
34
+ import io .grpc .internal .ServiceConfigUtil .PolicySelection ;
35
+ import java .util .ArrayList ;
33
36
import java .util .List ;
34
37
import java .util .Map ;
35
38
import javax .annotation .Nullable ;
42
45
@ Internal
43
46
public final class XdsLoadBalancerProvider extends LoadBalancerProvider {
44
47
45
- static final String XDS_POLICY_NAME = "xds_experimental" ;
46
-
47
- private static final LbConfig DEFAULT_FALLBACK_POLICY =
48
- new LbConfig ("round_robin" , ImmutableMap .<String , Void >of ());
48
+ private static final String XDS_POLICY_NAME = "xds_experimental" ;
49
49
50
50
@ Override
51
51
public boolean isAvailable () {
@@ -78,8 +78,38 @@ static ConfigOrError parseLoadBalancingConfigPolicy(
78
78
Map <String , ?> rawLoadBalancingPolicyConfig , LoadBalancerRegistry registry ) {
79
79
try {
80
80
String cluster = JsonUtil .getString (rawLoadBalancingPolicyConfig , "cluster" );
81
- LbConfig childPolicy = selectChildPolicy (rawLoadBalancingPolicyConfig , registry );
82
- LbConfig fallbackPolicy = selectFallbackPolicy (rawLoadBalancingPolicyConfig , registry );
81
+
82
+ LbConfig roundRobinConfig = new LbConfig ("round_robin" , ImmutableMap .<String , Object >of ());
83
+ List <LbConfig > endpointPickingConfigs = ServiceConfigUtil .unwrapLoadBalancingConfigList (
84
+ JsonUtil .getListOfObjects (rawLoadBalancingPolicyConfig , "endpointPickingPolicy" ));
85
+ if (endpointPickingConfigs == null ) {
86
+ endpointPickingConfigs = new ArrayList <>(1 );
87
+ } else {
88
+ endpointPickingConfigs = new ArrayList <>(endpointPickingConfigs );
89
+ }
90
+ endpointPickingConfigs .add (roundRobinConfig );
91
+ ConfigOrError childConfigOrError =
92
+ ServiceConfigUtil .selectLbPolicyFromList (endpointPickingConfigs , registry );
93
+ if (childConfigOrError .getError () != null ) {
94
+ return childConfigOrError ;
95
+ }
96
+ PolicySelection childPolicy = (PolicySelection ) childConfigOrError .getConfig ();
97
+
98
+ List <LbConfig > fallbackConfigs = ServiceConfigUtil .unwrapLoadBalancingConfigList (
99
+ JsonUtil .getListOfObjects (rawLoadBalancingPolicyConfig , "fallbackPolicy" ));
100
+ if (fallbackConfigs == null ) {
101
+ fallbackConfigs = new ArrayList <>(1 );
102
+ } else {
103
+ fallbackConfigs = new ArrayList <>(fallbackConfigs );
104
+ }
105
+ fallbackConfigs .add (roundRobinConfig );
106
+ ConfigOrError fallbackConfigOrError =
107
+ ServiceConfigUtil .selectLbPolicyFromList (fallbackConfigs , registry );
108
+ if (fallbackConfigOrError .getError () != null ) {
109
+ return fallbackConfigOrError ;
110
+ }
111
+ PolicySelection fallbackPolicy = (PolicySelection ) fallbackConfigOrError .getConfig ();
112
+
83
113
String edsServiceName = JsonUtil .getString (rawLoadBalancingPolicyConfig , "edsServiceName" );
84
114
String lrsServerName =
85
115
JsonUtil .getString (rawLoadBalancingPolicyConfig , "lrsLoadReportingServerName" );
@@ -92,51 +122,16 @@ static ConfigOrError parseLoadBalancingConfigPolicy(
92
122
}
93
123
}
94
124
95
- @ VisibleForTesting
96
- static LbConfig selectFallbackPolicy (
97
- Map <String , ?> rawLoadBalancingPolicyConfig , LoadBalancerRegistry lbRegistry ) {
98
- List <LbConfig > fallbackConfigs = ServiceConfigUtil .unwrapLoadBalancingConfigList (
99
- JsonUtil .getListOfObjects (rawLoadBalancingPolicyConfig , "fallbackPolicy" ));
100
- LbConfig fallbackPolicy = selectSupportedLbPolicy (fallbackConfigs , lbRegistry );
101
- return fallbackPolicy == null ? DEFAULT_FALLBACK_POLICY : fallbackPolicy ;
102
- }
103
-
104
- @ Nullable
105
- @ VisibleForTesting
106
- static LbConfig selectChildPolicy (
107
- Map <String , ?> rawLoadBalancingPolicyConfig , LoadBalancerRegistry lbRegistry ) {
108
- List <LbConfig > childConfigs = ServiceConfigUtil .unwrapLoadBalancingConfigList (
109
- JsonUtil .getListOfObjects (rawLoadBalancingPolicyConfig , "childPolicy" ));
110
- return selectSupportedLbPolicy (childConfigs , lbRegistry );
111
- }
112
-
113
- @ Nullable
114
- private static LbConfig selectSupportedLbPolicy (
115
- @ Nullable List <LbConfig > lbConfigs , LoadBalancerRegistry lbRegistry ) {
116
- if (lbConfigs == null ) {
117
- return null ;
118
- }
119
- for (LbConfig lbConfig : lbConfigs ) {
120
- String lbPolicy = lbConfig .getPolicyName ();
121
- if (lbRegistry .getProvider (lbPolicy ) != null ) {
122
- return lbConfig ;
123
- }
124
- }
125
- return null ;
126
- }
127
-
128
125
/**
129
126
* Represents a successfully parsed and validated LoadBalancingConfig for XDS.
130
127
*/
131
128
static final class XdsConfig {
132
129
// FIXME(chengyuanzhang): make cluster name required.
133
130
@ Nullable
134
131
final String cluster ;
135
- // TODO(carl-mastrangelo): make these Object's containing the fully parsed child configs.
136
- @ Nullable
137
- final LbConfig childPolicy ;
132
+ final PolicySelection endpointPickingPolicy ;
138
133
@ Nullable
139
- final LbConfig fallbackPolicy ;
134
+ final PolicySelection fallbackPolicy ;
140
135
// Optional. Name to use in EDS query. If not present, defaults to the server name from the
141
136
// target URI.
142
137
@ Nullable
@@ -149,12 +144,12 @@ static final class XdsConfig {
149
144
150
145
XdsConfig (
151
146
@ Nullable String cluster ,
152
- @ Nullable LbConfig childPolicy ,
153
- @ Nullable LbConfig fallbackPolicy ,
147
+ PolicySelection endpointPickingPolicy ,
148
+ @ Nullable PolicySelection fallbackPolicy ,
154
149
@ Nullable String edsServiceName ,
155
150
@ Nullable String lrsServerName ) {
156
151
this .cluster = cluster ;
157
- this .childPolicy = childPolicy ;
152
+ this .endpointPickingPolicy = checkNotNull ( endpointPickingPolicy , "endpointPickingPolicy" ) ;
158
153
this .fallbackPolicy = fallbackPolicy ;
159
154
this .edsServiceName = edsServiceName ;
160
155
this .lrsServerName = lrsServerName ;
@@ -164,7 +159,7 @@ static final class XdsConfig {
164
159
public String toString () {
165
160
return MoreObjects .toStringHelper (this )
166
161
.add ("cluster" , cluster )
167
- .add ("childPolicy " , childPolicy )
162
+ .add ("endpointPickingPolicy " , endpointPickingPolicy )
168
163
.add ("fallbackPolicy" , fallbackPolicy )
169
164
.add ("edsServiceName" , edsServiceName )
170
165
.add ("lrsServerName" , lrsServerName )
@@ -178,15 +173,16 @@ public boolean equals(Object obj) {
178
173
}
179
174
XdsConfig that = (XdsConfig ) obj ;
180
175
return Objects .equal (this .cluster , that .cluster )
181
- && Objects .equal (this .childPolicy , that .childPolicy )
176
+ && Objects .equal (this .endpointPickingPolicy , that .endpointPickingPolicy )
182
177
&& Objects .equal (this .fallbackPolicy , that .fallbackPolicy )
183
178
&& Objects .equal (this .edsServiceName , that .edsServiceName )
184
179
&& Objects .equal (this .lrsServerName , that .lrsServerName );
185
180
}
186
181
187
182
@ Override
188
183
public int hashCode () {
189
- return Objects .hashCode (cluster , childPolicy , fallbackPolicy , edsServiceName , lrsServerName );
184
+ return Objects .hashCode (
185
+ cluster , endpointPickingPolicy , fallbackPolicy , edsServiceName , lrsServerName );
190
186
}
191
187
}
192
188
}
0 commit comments