xds: add fields for EDS server and LRS server in XdsConfig (#6287)

This commit is contained in:
Chengyuan Zhang 2019-10-18 13:33:56 -07:00 committed by GitHub
parent eef47b26b8
commit 9dce879742
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 108 additions and 6 deletions

View File

@ -48,9 +48,12 @@ public final class ServiceConfigUtil {
private static final String SERVICE_CONFIG_METHOD_CONFIG_KEY = "methodConfig";
private static final String SERVICE_CONFIG_LOAD_BALANCING_POLICY_KEY = "loadBalancingPolicy";
private static final String SERVICE_CONFIG_LOAD_BALANCING_CONFIG_KEY = "loadBalancingConfig";
// TODO(chengyuanzhang): delete this key after shifting to use bootstrap.
private static final String XDS_CONFIG_BALANCER_NAME_KEY = "balancerName";
private static final String XDS_CONFIG_CHILD_POLICY_KEY = "childPolicy";
private static final String XDS_CONFIG_FALLBACK_POLICY_KEY = "fallbackPolicy";
private static final String XDS_CONFIG_EDS_SERVICE_NAME = "edsServiceName";
private static final String XDS_CONFIG_LRS_SERVER_NAME = "lrsLoadReportingServerName";
private static final String SERVICE_CONFIG_STICKINESS_METADATA_KEY = "stickinessMetadataKey";
private static final String METHOD_CONFIG_NAME_KEY = "name";
private static final String METHOD_CONFIG_TIMEOUT_KEY = "timeout";
@ -423,10 +426,27 @@ public final class ServiceConfigUtil {
/**
* Extracts the loadbalancer name from xds loadbalancer config.
*/
// TODO(chengyuanzhang): delete after shifting to use bootstrap.
public static String getBalancerNameFromXdsConfig(Map<String, ?> rawXdsConfig) {
return JsonUtil.getString(rawXdsConfig, XDS_CONFIG_BALANCER_NAME_KEY);
}
/**
* Extract the server name to use in EDS query.
*/
@Nullable
public static String getEdsServiceNameFromXdsConfig(Map<String, ?> rawXdsConfig) {
return JsonUtil.getString(rawXdsConfig, XDS_CONFIG_EDS_SERVICE_NAME);
}
/**
* Extract the LRS server name to send load reports to.
*/
@Nullable
public static String getLrsServerNameFromXdsConfig(Map<String, ?> rawXdsConfig) {
return JsonUtil.getString(rawXdsConfig, XDS_CONFIG_LRS_SERVER_NAME);
}
/**
* Extracts list of child policies from xds loadbalancer config.
*/

View File

@ -106,6 +106,60 @@ public class ServiceConfigUtilTest {
assertThat(fallbackPolicies).isNull();
}
@Test
public void getEdsServiceNameFromXdsConfig() throws Exception {
String rawLbConfig = "{"
+ "\"balancerName\" : \"dns:///balancer.example.com:8080\","
+ "\"childPolicy\" : [{\"round_robin\" : {}}, {\"lbPolicy2\" : {\"key\" : \"val\"}}],"
+ "\"fallbackPolicy\" : [{\"lbPolicy3\" : {\"key\" : \"val\"}}, {\"lbPolicy4\" : {}}],"
+ "\"edsServiceName\" : \"dns:///eds.service.com:8080\""
+ "}";
String edsServiceName = ServiceConfigUtil.getEdsServiceNameFromXdsConfig(
checkObject(JsonParser.parse(rawLbConfig)));
assertThat(edsServiceName).isEqualTo("dns:///eds.service.com:8080");
}
@Test
public void getEdsServiceNameFromXdsConfig_null() throws Exception {
String rawLbConfig = "{"
+ "\"balancerName\" : \"dns:///balancer.example.com:8080\","
+ "\"childPolicy\" : [{\"round_robin\" : {}}, {\"lbPolicy2\" : {\"key\" : \"val\"}}],"
+ "\"fallbackPolicy\" : [{\"lbPolicy3\" : {\"key\" : \"val\"}}, {\"lbPolicy4\" : {}}]"
+ "}";
String edsServiceName = ServiceConfigUtil.getEdsServiceNameFromXdsConfig(
checkObject(JsonParser.parse(rawLbConfig)));
assertThat(edsServiceName).isNull();
}
@Test
public void getLrsServerNameFromXdsConfig() throws Exception {
String rawLbConfig = "{"
+ "\"balancerName\" : \"dns:///balancer.example.com:8080\","
+ "\"childPolicy\" : [{\"round_robin\" : {}}, {\"lbPolicy2\" : {\"key\" : \"val\"}}],"
+ "\"fallbackPolicy\" : [{\"lbPolicy3\" : {\"key\" : \"val\"}}, {\"lbPolicy4\" : {}}],"
+ "\"lrsLoadReportingServerName\" : \"dns:///lrs.service.com:8080\""
+ "}";
String lrsServerName = ServiceConfigUtil.getLrsServerNameFromXdsConfig(
checkObject(JsonParser.parse(rawLbConfig)));
assertThat(lrsServerName).isEqualTo("dns:///lrs.service.com:8080");
}
@Test
public void getLrsServerNameFromXdsConfig_null() throws Exception {
String rawLbConfig = "{"
+ "\"balancerName\" : \"dns:///balancer.example.com:8080\","
+ "\"childPolicy\" : [{\"round_robin\" : {}}, {\"lbPolicy2\" : {\"key\" : \"val\"}}],"
+ "\"fallbackPolicy\" : [{\"lbPolicy3\" : {\"key\" : \"val\"}}, {\"lbPolicy4\" : {}}]"
+ "}";
String lrsServerName = ServiceConfigUtil.getLrsServerNameFromXdsConfig(
checkObject(JsonParser.parse(rawLbConfig)));
assertThat(lrsServerName).isNull();
}
@Test
public void unwrapLoadBalancingConfig() throws Exception {
String lbConfig = "{\"xds_experimental\" : { "

View File

@ -82,7 +82,13 @@ public final class XdsLoadBalancerProvider extends LoadBalancerProvider {
ServiceConfigUtil.getBalancerNameFromXdsConfig(rawLoadBalancingPolicyConfig);
LbConfig childPolicy = selectChildPolicy(rawLoadBalancingPolicyConfig, registry);
LbConfig fallbackPolicy = selectFallbackPolicy(rawLoadBalancingPolicyConfig, registry);
return ConfigOrError.fromConfig(new XdsConfig(newBalancerName, childPolicy, fallbackPolicy));
String edsServiceName =
ServiceConfigUtil.getEdsServiceNameFromXdsConfig(rawLoadBalancingPolicyConfig);
String lrsServerName =
ServiceConfigUtil.getLrsServerNameFromXdsConfig(rawLoadBalancingPolicyConfig);
return ConfigOrError.fromConfig(
new XdsConfig(
newBalancerName, childPolicy, fallbackPolicy, edsServiceName, lrsServerName));
} catch (RuntimeException e) {
return ConfigOrError.fromError(
Status.UNKNOWN.withDescription("Failed to parse config " + e.getMessage()).withCause(e));
@ -126,18 +132,31 @@ public final class XdsLoadBalancerProvider extends LoadBalancerProvider {
* Represents a successfully parsed and validated LoadBalancingConfig for XDS.
*/
static final class XdsConfig {
// TODO(chengyuanzhang): delete after shifting to use bootstrap.
final String balancerName;
// TODO(carl-mastrangelo): make these Object's containing the fully parsed child configs.
@Nullable
final LbConfig childPolicy;
@Nullable
final LbConfig fallbackPolicy;
// Optional. Name to use in EDS query. If not present, defaults to the server name from the
// target URI.
@Nullable
final String edsServiceName;
// Optional. LRS server to send load reports to. If not present, load reporting will be
// disabled. If set to the empty string, load reporting will be sent to the same server that
// we obtained CDS data from.
@Nullable
final String lrsServerName;
XdsConfig(
String balancerName, @Nullable LbConfig childPolicy, @Nullable LbConfig fallbackPolicy) {
String balancerName, @Nullable LbConfig childPolicy, @Nullable LbConfig fallbackPolicy,
@Nullable String edsServiceName, @Nullable String lrsServerName) {
this.balancerName = checkNotNull(balancerName, "balancerName");
this.childPolicy = childPolicy;
this.fallbackPolicy = fallbackPolicy;
this.edsServiceName = edsServiceName;
this.lrsServerName = lrsServerName;
}
@Override
@ -146,6 +165,8 @@ public final class XdsLoadBalancerProvider extends LoadBalancerProvider {
.add("balancerName", balancerName)
.add("childPolicy", childPolicy)
.add("fallbackPolicy", fallbackPolicy)
.add("edsServiceName", edsServiceName)
.add("lrsServerName", lrsServerName)
.toString();
}
@ -157,12 +178,15 @@ public final class XdsLoadBalancerProvider extends LoadBalancerProvider {
XdsConfig that = (XdsConfig) obj;
return Objects.equal(this.balancerName, that.balancerName)
&& Objects.equal(this.childPolicy, that.childPolicy)
&& Objects.equal(this.fallbackPolicy, that.fallbackPolicy);
&& Objects.equal(this.fallbackPolicy, that.fallbackPolicy)
&& Objects.equal(this.edsServiceName, that.edsServiceName)
&& Objects.equal(this.lrsServerName, that.lrsServerName);
}
@Override
public int hashCode() {
return Objects.hashCode(balancerName, childPolicy, fallbackPolicy);
return Objects.hashCode(
balancerName, childPolicy, fallbackPolicy, edsServiceName, lrsServerName);
}
}
}

View File

@ -161,7 +161,9 @@ public class XdsLoadBalancerProviderTest {
+ "\"balancerName\" : \"dns:///balancer.example.com:8080\","
+ "\"childPolicy\" : [{\"lbPolicy3\" : {\"key\" : \"val\"}}, {\"supported_1\" : {}}],"
+ "\"fallbackPolicy\" : [{\"unsupported\" : {}}, {\"round_robin\" : {\"key\" : \"val\"}},"
+ "{\"supported_2\" : {\"key\" : \"val\"}}]"
+ "{\"supported_2\" : {\"key\" : \"val\"}}],"
+ "\"edsServiceName\" : \"dns:///eds.service.com:8080\","
+ "\"lrsLoadReportingServerName\" : \"dns:///lrs.service.com:8080\""
+ "}";
Map<String, ?> rawlbConfigMap = checkObject(JsonParser.parse(rawLbConfig));
ConfigOrError configOrError =
@ -175,7 +177,9 @@ public class XdsLoadBalancerProviderTest {
ServiceConfigUtil.unwrapLoadBalancingConfig(
checkObject(JsonParser.parse("{\"supported_1\" : {}}"))),
ServiceConfigUtil.unwrapLoadBalancingConfig(
checkObject(JsonParser.parse("{\"round_robin\" : {\"key\" : \"val\"}}"))))
checkObject(JsonParser.parse("{\"round_robin\" : {\"key\" : \"val\"}}"))),
"dns:///eds.service.com:8080",
"dns:///lrs.service.com:8080")
);
}