Skip to content

Commit

Permalink
xds: add fields for EDS server and LRS server in XdsConfig (#6287)
Browse files Browse the repository at this point in the history
  • Loading branch information
voidzcy committed Oct 18, 2019
1 parent eef47b2 commit 9dce879
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
20 changes: 20 additions & 0 deletions core/src/main/java/io/grpc/internal/ServiceConfigUtil.java
Expand Up @@ -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";
Expand Down Expand Up @@ -423,10 +426,27 @@ public static List<LbConfig> unwrapLoadBalancingConfigList(List<Map<String, ?>>
/**
* 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.
*/
Expand Down
54 changes: 54 additions & 0 deletions core/src/test/java/io/grpc/internal/ServiceConfigUtilTest.java
Expand Up @@ -106,6 +106,60 @@ public void getFallbackPolicyFromXdsConfig_null() throws Exception {
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\" : { "
Expand Down
32 changes: 28 additions & 4 deletions xds/src/main/java/io/grpc/xds/XdsLoadBalancerProvider.java
Expand Up @@ -82,7 +82,13 @@ static ConfigOrError parseLoadBalancingConfigPolicy(
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));
Expand Down Expand Up @@ -126,18 +132,31 @@ private static LbConfig selectSupportedLbPolicy(
* 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
Expand All @@ -146,6 +165,8 @@ public String toString() {
.add("balancerName", balancerName)
.add("childPolicy", childPolicy)
.add("fallbackPolicy", fallbackPolicy)
.add("edsServiceName", edsServiceName)
.add("lrsServerName", lrsServerName)
.toString();
}

Expand All @@ -157,12 +178,15 @@ public boolean equals(Object obj) {
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);
}
}
}
Expand Up @@ -161,7 +161,9 @@ public void parseLoadBalancingConfigPolicy() throws Exception {
+ "\"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 =
Expand All @@ -175,7 +177,9 @@ public void parseLoadBalancingConfigPolicy() throws Exception {
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")
);
}

Expand Down

0 comments on commit 9dce879

Please sign in to comment.