Skip to content

Commit 24e1886

Browse files
committed
Retrieve timeout value from RouteAction's max_stream_duration.
1 parent 7032d4c commit 24e1886

File tree

2 files changed

+57
-60
lines changed

2 files changed

+57
-60
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import java.util.List;
4343
import java.util.Map;
4444
import java.util.Objects;
45-
import java.util.concurrent.TimeUnit;
4645
import javax.annotation.Nullable;
4746

4847
/**
@@ -1217,14 +1216,15 @@ static StructOrError<RouteAction> fromEnvoyProtoRouteAction(
12171216
return StructOrError.fromError(
12181217
"Unknown cluster specifier: " + proto.getClusterSpecifierCase());
12191218
}
1220-
long timeoutNano = TimeUnit.SECONDS.toNanos(15L); // default 15s
1221-
if (proto.hasMaxGrpcTimeout()) {
1222-
timeoutNano = Durations.toNanos(proto.getMaxGrpcTimeout());
1223-
} else if (proto.hasTimeout()) {
1224-
timeoutNano = Durations.toNanos(proto.getTimeout());
1225-
}
1226-
if (timeoutNano == 0) {
1227-
timeoutNano = Long.MAX_VALUE;
1219+
long timeoutNano = 0;
1220+
if (proto.hasMaxStreamDuration()) {
1221+
io.envoyproxy.envoy.config.route.v3.RouteAction.MaxStreamDuration maxStreamDuration
1222+
= proto.getMaxStreamDuration();
1223+
if (maxStreamDuration.hasGrpcTimeoutHeaderMax()) {
1224+
timeoutNano = Durations.toNanos(maxStreamDuration.getGrpcTimeoutHeaderMax());
1225+
} else if (maxStreamDuration.hasMaxStreamDuration()) {
1226+
timeoutNano = Durations.toNanos(maxStreamDuration.getMaxStreamDuration());
1227+
}
12281228
}
12291229
return StructOrError.fromStruct(new RouteAction(timeoutNano, cluster, weightedClusters));
12301230
}

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

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.envoyproxy.envoy.config.core.v3.RuntimeFractionalPercent;
3030
import io.envoyproxy.envoy.config.route.v3.QueryParameterMatcher;
3131
import io.envoyproxy.envoy.config.route.v3.RedirectAction;
32+
import io.envoyproxy.envoy.config.route.v3.RouteAction.MaxStreamDuration;
3233
import io.envoyproxy.envoy.config.route.v3.WeightedCluster;
3334
import io.envoyproxy.envoy.type.matcher.v3.RegexMatcher;
3435
import io.envoyproxy.envoy.type.v3.FractionalPercent;
@@ -49,7 +50,6 @@
4950
import io.grpc.xds.RouteMatch.PathMatcher;
5051
import java.util.Arrays;
5152
import java.util.Collections;
52-
import java.util.concurrent.TimeUnit;
5353
import javax.annotation.Nullable;
5454
import org.junit.Test;
5555
import org.junit.runner.RunWith;
@@ -213,7 +213,7 @@ public void convertRoute() {
213213
new Route(
214214
new RouteMatch(new PathMatcher("/service/method", null, null),
215215
Collections.<HeaderMatcher>emptyList(), null),
216-
new RouteAction(TimeUnit.SECONDS.toNanos(15L), "cluster-foo", null)));
216+
new RouteAction(0, "cluster-foo", null)));
217217

218218
io.envoyproxy.envoy.config.route.v3.Route unsupportedProto =
219219
io.envoyproxy.envoy.config.route.v3.Route.newBuilder()
@@ -399,74 +399,71 @@ public void convertRouteMatch_withRuntimeFraction() {
399399
}
400400

401401
@Test
402-
public void convertRouteAction() {
403-
// cluster_specifier = cluster, default timeout
404-
io.envoyproxy.envoy.config.route.v3.RouteAction proto1 =
402+
public void convertRouteAction_cluster() {
403+
io.envoyproxy.envoy.config.route.v3.RouteAction proto =
405404
io.envoyproxy.envoy.config.route.v3.RouteAction.newBuilder()
406405
.setCluster("cluster-foo")
407406
.build();
408-
StructOrError<RouteAction> struct1 = RouteAction.fromEnvoyProtoRouteAction(proto1);
407+
StructOrError<RouteAction> struct1 = RouteAction.fromEnvoyProtoRouteAction(proto);
409408
assertThat(struct1.getErrorDetail()).isNull();
410-
assertThat(struct1.getStruct().getTimeoutNano())
411-
.isEqualTo(TimeUnit.SECONDS.toNanos(15L)); // default value
412409
assertThat(struct1.getStruct().getCluster()).isEqualTo("cluster-foo");
413410
assertThat(struct1.getStruct().getWeightedCluster()).isNull();
411+
}
414412

415-
// cluster_specifier = cluster, infinity timeout
416-
io.envoyproxy.envoy.config.route.v3.RouteAction proto2 =
417-
io.envoyproxy.envoy.config.route.v3.RouteAction.newBuilder()
418-
.setMaxGrpcTimeout(Durations.fromNanos(0))
419-
.setTimeout(Durations.fromMicros(20L))
420-
.setCluster("cluster-foo")
421-
.build();
422-
StructOrError<RouteAction> struct2 = RouteAction.fromEnvoyProtoRouteAction(proto2);
423-
assertThat(struct2.getStruct().getTimeoutNano())
424-
.isEqualTo(Long.MAX_VALUE); // infinite
425-
426-
// cluster_specifier = cluster, infinity timeout
427-
io.envoyproxy.envoy.config.route.v3.RouteAction proto3 =
428-
io.envoyproxy.envoy.config.route.v3.RouteAction.newBuilder()
429-
.setTimeout(Durations.fromNanos(0))
430-
.setCluster("cluster-foo")
431-
.build();
432-
StructOrError<RouteAction> struct3 = RouteAction.fromEnvoyProtoRouteAction(proto3);
433-
assertThat(struct3.getStruct().getTimeoutNano()).isEqualTo(Long.MAX_VALUE); // infinite
434-
435-
// cluster_specifier = cluster_header
436-
io.envoyproxy.envoy.config.route.v3.RouteAction proto4 =
437-
io.envoyproxy.envoy.config.route.v3.RouteAction.newBuilder()
438-
.setClusterHeader("cluster-bar")
439-
.build();
440-
StructOrError<RouteAction> struct4 = RouteAction.fromEnvoyProtoRouteAction(proto4);
441-
assertThat(struct4).isNull();
442-
443-
// cluster_specifier = weighted_cluster
444-
io.envoyproxy.envoy.config.route.v3.RouteAction proto5 =
413+
@Test
414+
public void convertRouteAction_weightedCluster() {
415+
io.envoyproxy.envoy.config.route.v3.RouteAction proto =
445416
io.envoyproxy.envoy.config.route.v3.RouteAction.newBuilder()
446-
.setMaxGrpcTimeout(Durations.fromSeconds(6L))
447-
.setTimeout(Durations.fromMicros(20L))
448417
.setWeightedClusters(
449418
WeightedCluster.newBuilder()
450419
.addClusters(
451420
WeightedCluster.ClusterWeight
452421
.newBuilder()
453-
.setName("cluster-baz")
454-
.setWeight(UInt32Value.newBuilder().setValue(100))))
422+
.setName("cluster-foo")
423+
.setWeight(UInt32Value.newBuilder().setValue(30)))
424+
.addClusters(WeightedCluster.ClusterWeight
425+
.newBuilder()
426+
.setName("cluster-bar")
427+
.setWeight(UInt32Value.newBuilder().setValue(70))))
455428
.build();
456-
StructOrError<RouteAction> struct5 = RouteAction.fromEnvoyProtoRouteAction(proto5);
429+
StructOrError<RouteAction> struct5 = RouteAction.fromEnvoyProtoRouteAction(proto);
457430
assertThat(struct5.getErrorDetail()).isNull();
458-
assertThat(struct5.getStruct().getTimeoutNano())
459-
.isEqualTo(TimeUnit.SECONDS.toNanos(6L));
460431
assertThat(struct5.getStruct().getCluster()).isNull();
461-
assertThat(struct5.getStruct().getWeightedCluster())
462-
.containsExactly(new ClusterWeight("cluster-baz", 100));
432+
assertThat(struct5.getStruct().getWeightedCluster()).containsExactly(
433+
new ClusterWeight("cluster-foo", 30), new ClusterWeight("cluster-bar", 70));
434+
}
463435

464-
// cluster_specifier unset
465-
io.envoyproxy.envoy.config.route.v3.RouteAction unsetProto =
436+
@Test
437+
public void convertRouteAction_unspecifiedClusterError() {
438+
io.envoyproxy.envoy.config.route.v3.RouteAction proto =
466439
io.envoyproxy.envoy.config.route.v3.RouteAction.getDefaultInstance();
467-
StructOrError<RouteAction> unsetStruct = RouteAction.fromEnvoyProtoRouteAction(unsetProto);
468-
assertThat(unsetStruct.getErrorDetail()).isNotNull();
440+
StructOrError<RouteAction> unsetStruct = RouteAction.fromEnvoyProtoRouteAction(proto);
469441
assertThat(unsetStruct.getStruct()).isNull();
442+
assertThat(unsetStruct.getErrorDetail()).isNotNull();
443+
}
444+
445+
@Test
446+
public void convertRouteAction_timeoutByMaxStreamDuration() {
447+
io.envoyproxy.envoy.config.route.v3.RouteAction proto =
448+
io.envoyproxy.envoy.config.route.v3.RouteAction.newBuilder()
449+
.setCluster("cluster-foo")
450+
.setMaxStreamDuration(
451+
MaxStreamDuration.newBuilder()
452+
.setGrpcTimeoutHeaderMax(Durations.fromMicros(20L))
453+
.setMaxStreamDuration(Durations.fromSeconds(5L)))
454+
.build();
455+
StructOrError<RouteAction> struct2 = RouteAction.fromEnvoyProtoRouteAction(proto);
456+
assertThat(struct2.getStruct().getTimeoutNano()).isEqualTo(Durations.fromSeconds(5L));
457+
}
458+
459+
@Test
460+
public void convertRouteAction_timeoutUnset() {
461+
io.envoyproxy.envoy.config.route.v3.RouteAction proto =
462+
io.envoyproxy.envoy.config.route.v3.RouteAction.newBuilder()
463+
.setCluster("cluster-foo")
464+
.build();
465+
StructOrError<RouteAction> struct2 = RouteAction.fromEnvoyProtoRouteAction(proto);
466+
assertThat(struct2.getStruct().getTimeoutNano()).isEqualTo(0);
470467
}
471468

472469
@Test

0 commit comments

Comments
 (0)