Skip to content

Commit 602aece

Browse files
authored
xds: avoid unnecessary dns lookup (#11932)
1 parent 1219706 commit 602aece

File tree

3 files changed

+82
-49
lines changed

3 files changed

+82
-49
lines changed

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import io.grpc.xds.client.EnvoyProtoData;
2828
import io.grpc.xds.internal.security.SslContextProviderSupplier;
2929
import java.net.InetAddress;
30-
import java.net.UnknownHostException;
3130
import java.util.Objects;
3231
import javax.annotation.Nullable;
3332

@@ -150,9 +149,9 @@ abstract static class CidrRange {
150149

151150
abstract int prefixLen();
152151

153-
static CidrRange create(String addressPrefix, int prefixLen) throws UnknownHostException {
152+
static CidrRange create(InetAddress addressPrefix, int prefixLen) {
154153
return new AutoValue_EnvoyServerProtoData_CidrRange(
155-
InetAddress.getByName(addressPrefix), prefixLen);
154+
addressPrefix, prefixLen);
156155
}
157156
}
158157

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.auto.value.AutoValue;
2626
import com.google.common.annotations.VisibleForTesting;
2727
import com.google.common.collect.ImmutableList;
28+
import com.google.common.net.InetAddresses;
2829
import com.google.protobuf.Any;
2930
import com.google.protobuf.InvalidProtocolBufferException;
3031
import com.google.protobuf.Message;
@@ -43,7 +44,6 @@
4344
import io.grpc.xds.Filter.FilterConfig;
4445
import io.grpc.xds.XdsListenerResource.LdsUpdate;
4546
import io.grpc.xds.client.XdsResourceType;
46-
import java.net.UnknownHostException;
4747
import java.util.ArrayList;
4848
import java.util.Collection;
4949
import java.util.HashSet;
@@ -446,16 +446,18 @@ private static FilterChainMatch parseFilterChainMatch(
446446
try {
447447
for (io.envoyproxy.envoy.config.core.v3.CidrRange range : proto.getPrefixRangesList()) {
448448
prefixRanges.add(
449-
CidrRange.create(range.getAddressPrefix(), range.getPrefixLen().getValue()));
449+
CidrRange.create(InetAddresses.forString(range.getAddressPrefix()),
450+
range.getPrefixLen().getValue()));
450451
}
451452
for (io.envoyproxy.envoy.config.core.v3.CidrRange range
452453
: proto.getSourcePrefixRangesList()) {
453-
sourcePrefixRanges.add(
454-
CidrRange.create(range.getAddressPrefix(), range.getPrefixLen().getValue()));
454+
sourcePrefixRanges.add(CidrRange.create(
455+
InetAddresses.forString(range.getAddressPrefix()), range.getPrefixLen().getValue()));
455456
}
456-
} catch (UnknownHostException e) {
457-
throw new ResourceInvalidException("Failed to create CidrRange", e);
457+
} catch (IllegalArgumentException ex) {
458+
throw new ResourceInvalidException("Failed to create CidrRange", ex);
458459
}
460+
459461
ConnectionSourceType sourceType;
460462
switch (proto.getSourceType()) {
461463
case ANY:

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

+72-40
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import com.google.common.collect.ImmutableList;
2727
import com.google.common.collect.ImmutableMap;
28+
import com.google.common.net.InetAddresses;
2829
import com.google.common.util.concurrent.SettableFuture;
2930
import io.grpc.ServerInterceptor;
3031
import io.grpc.internal.TestUtils.NoopChannelLogger;
@@ -58,7 +59,6 @@
5859
import io.netty.handler.codec.http2.Http2Settings;
5960
import java.net.InetSocketAddress;
6061
import java.net.SocketAddress;
61-
import java.net.UnknownHostException;
6262
import java.util.ArrayList;
6363
import java.util.Collections;
6464
import java.util.HashMap;
@@ -318,7 +318,8 @@ public void destPrefixRangeMatch() throws Exception {
318318
EnvoyServerProtoData.FilterChainMatch filterChainMatchWithMatch =
319319
EnvoyServerProtoData.FilterChainMatch.create(
320320
0,
321-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.1.2.0", 24)),
321+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
322+
InetAddresses.forString("10.1.2.0"), 24)),
322323
ImmutableList.of(),
323324
ImmutableList.of(),
324325
EnvoyServerProtoData.ConnectionSourceType.ANY,
@@ -360,7 +361,8 @@ public void destPrefixRangeMismatch_returnDefaultFilterChain()
360361
EnvoyServerProtoData.FilterChainMatch filterChainMatchWithMismatch =
361362
EnvoyServerProtoData.FilterChainMatch.create(
362363
0,
363-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.2.2.0", 24)),
364+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
365+
InetAddresses.forString("10.2.2.0"), 24)),
364366
ImmutableList.of(),
365367
ImmutableList.of(),
366368
EnvoyServerProtoData.ConnectionSourceType.ANY,
@@ -403,7 +405,8 @@ public void dest0LengthPrefixRange()
403405
EnvoyServerProtoData.FilterChainMatch filterChainMatch0Length =
404406
EnvoyServerProtoData.FilterChainMatch.create(
405407
0,
406-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.2.2.0", 0)),
408+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
409+
InetAddresses.forString("10.2.2.0"), 0)),
407410
ImmutableList.of(),
408411
ImmutableList.of(),
409412
EnvoyServerProtoData.ConnectionSourceType.ANY,
@@ -444,7 +447,8 @@ public void destPrefixRange_moreSpecificWins()
444447
EnvoyServerProtoData.FilterChainMatch filterChainMatchLessSpecific =
445448
EnvoyServerProtoData.FilterChainMatch.create(
446449
0,
447-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.1.2.0", 24)),
450+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
451+
InetAddresses.forString("10.1.2.0"), 24)),
448452
ImmutableList.of(),
449453
ImmutableList.of(),
450454
EnvoyServerProtoData.ConnectionSourceType.ANY,
@@ -461,7 +465,8 @@ public void destPrefixRange_moreSpecificWins()
461465
EnvoyServerProtoData.FilterChainMatch filterChainMatchMoreSpecific =
462466
EnvoyServerProtoData.FilterChainMatch.create(
463467
0,
464-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.1.2.2", 31)),
468+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
469+
InetAddresses.forString("10.1.2.2"), 31)),
465470
ImmutableList.of(),
466471
ImmutableList.of(),
467472
EnvoyServerProtoData.ConnectionSourceType.ANY,
@@ -519,7 +524,8 @@ public void destPrefixRange_emptyListLessSpecific()
519524
EnvoyServerProtoData.FilterChainMatch filterChainMatchMoreSpecific =
520525
EnvoyServerProtoData.FilterChainMatch.create(
521526
0,
522-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("8.0.0.0", 5)),
527+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
528+
InetAddresses.forString("8.0.0.0"), 5)),
523529
ImmutableList.of(),
524530
ImmutableList.of(),
525531
EnvoyServerProtoData.ConnectionSourceType.ANY,
@@ -559,7 +565,8 @@ public void destPrefixRangeIpv6_moreSpecificWins()
559565
EnvoyServerProtoData.FilterChainMatch filterChainMatchLessSpecific =
560566
EnvoyServerProtoData.FilterChainMatch.create(
561567
0,
562-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("FE80:0:0:0:0:0:0:0", 60)),
568+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
569+
InetAddresses.forString("FE80:0:0:0:0:0:0:0"), 60)),
563570
ImmutableList.of(),
564571
ImmutableList.of(),
565572
EnvoyServerProtoData.ConnectionSourceType.ANY,
@@ -577,7 +584,8 @@ public void destPrefixRangeIpv6_moreSpecificWins()
577584
EnvoyServerProtoData.FilterChainMatch.create(
578585
0,
579586
ImmutableList.of(
580-
EnvoyServerProtoData.CidrRange.create("FE80:0000:0000:0000:0202:0:0:0", 80)),
587+
EnvoyServerProtoData.CidrRange.create(
588+
InetAddresses.forString("FE80:0000:0000:0000:0202:0:0:0"), 80)),
581589
ImmutableList.of(),
582590
ImmutableList.of(),
583591
EnvoyServerProtoData.ConnectionSourceType.ANY,
@@ -620,8 +628,10 @@ public void destPrefixRange_moreSpecificWith2Wins()
620628
EnvoyServerProtoData.FilterChainMatch.create(
621629
0,
622630
ImmutableList.of(
623-
EnvoyServerProtoData.CidrRange.create("10.1.2.0", 24),
624-
EnvoyServerProtoData.CidrRange.create(LOCAL_IP, 32)),
631+
EnvoyServerProtoData.CidrRange.create(
632+
InetAddresses.forString("10.1.2.0"), 24),
633+
EnvoyServerProtoData.CidrRange.create(
634+
InetAddresses.forString(LOCAL_IP), 32)),
625635
ImmutableList.of(),
626636
ImmutableList.of(),
627637
EnvoyServerProtoData.ConnectionSourceType.ANY,
@@ -638,7 +648,8 @@ public void destPrefixRange_moreSpecificWith2Wins()
638648
EnvoyServerProtoData.FilterChainMatch filterChainMatchLessSpecific =
639649
EnvoyServerProtoData.FilterChainMatch.create(
640650
0,
641-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.1.2.2", 31)),
651+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
652+
InetAddresses.forString("10.1.2.2"), 31)),
642653
ImmutableList.of(),
643654
ImmutableList.of(),
644655
EnvoyServerProtoData.ConnectionSourceType.ANY,
@@ -763,8 +774,10 @@ public void sourcePrefixRange_moreSpecificWith2Wins()
763774
ImmutableList.of(),
764775
ImmutableList.of(),
765776
ImmutableList.of(
766-
EnvoyServerProtoData.CidrRange.create("10.4.2.0", 24),
767-
EnvoyServerProtoData.CidrRange.create(REMOTE_IP, 32)),
777+
EnvoyServerProtoData.CidrRange.create(
778+
InetAddresses.forString("10.4.2.0"), 24),
779+
EnvoyServerProtoData.CidrRange.create(
780+
InetAddresses.forString(REMOTE_IP), 32)),
768781
EnvoyServerProtoData.ConnectionSourceType.ANY,
769782
ImmutableList.of(),
770783
ImmutableList.of(),
@@ -781,7 +794,8 @@ public void sourcePrefixRange_moreSpecificWith2Wins()
781794
0,
782795
ImmutableList.of(),
783796
ImmutableList.of(),
784-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.4.2.2", 31)),
797+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
798+
InetAddresses.forString("10.4.2.2"), 31)),
785799
EnvoyServerProtoData.ConnectionSourceType.ANY,
786800
ImmutableList.of(),
787801
ImmutableList.of(),
@@ -811,8 +825,7 @@ filterChainLessSpecific, randomConfig("no-match")),
811825
}
812826

813827
@Test
814-
public void sourcePrefixRange_2Matchers_expectException()
815-
throws UnknownHostException {
828+
public void sourcePrefixRange_2Matchers_expectException() {
816829
ChannelHandler next = new ChannelInboundHandlerAdapter() {
817830
@Override
818831
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
@@ -831,8 +844,10 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
831844
ImmutableList.of(),
832845
ImmutableList.of(),
833846
ImmutableList.of(
834-
EnvoyServerProtoData.CidrRange.create("10.4.2.0", 24),
835-
EnvoyServerProtoData.CidrRange.create("192.168.10.2", 32)),
847+
EnvoyServerProtoData.CidrRange.create(
848+
InetAddresses.forString("10.4.2.0"), 24),
849+
EnvoyServerProtoData.CidrRange.create(
850+
InetAddresses.forString("192.168.10.2"), 32)),
836851
EnvoyServerProtoData.ConnectionSourceType.ANY,
837852
ImmutableList.of(),
838853
ImmutableList.of(),
@@ -848,7 +863,8 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
848863
0,
849864
ImmutableList.of(),
850865
ImmutableList.of(),
851-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.4.2.0", 24)),
866+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
867+
InetAddresses.forString("10.4.2.0"), 24)),
852868
EnvoyServerProtoData.ConnectionSourceType.ANY,
853869
ImmutableList.of(),
854870
ImmutableList.of(),
@@ -890,8 +906,10 @@ public void sourcePortMatch_exactMatchWinsOverEmptyList() throws Exception {
890906
ImmutableList.of(),
891907
ImmutableList.of(),
892908
ImmutableList.of(
893-
EnvoyServerProtoData.CidrRange.create("10.4.2.0", 24),
894-
EnvoyServerProtoData.CidrRange.create("10.4.2.2", 31)),
909+
EnvoyServerProtoData.CidrRange.create(
910+
InetAddresses.forString("10.4.2.0"), 24),
911+
EnvoyServerProtoData.CidrRange.create(
912+
InetAddresses.forString("10.4.2.2"), 31)),
895913
EnvoyServerProtoData.ConnectionSourceType.ANY,
896914
ImmutableList.of(),
897915
ImmutableList.of(),
@@ -908,7 +926,8 @@ public void sourcePortMatch_exactMatchWinsOverEmptyList() throws Exception {
908926
0,
909927
ImmutableList.of(),
910928
ImmutableList.of(),
911-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.4.2.2", 31)),
929+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
930+
InetAddresses.forString("10.4.2.2"), 31)),
912931
EnvoyServerProtoData.ConnectionSourceType.ANY,
913932
ImmutableList.of(7000, 15000),
914933
ImmutableList.of(),
@@ -966,7 +985,8 @@ public void filterChain_5stepMatch() throws Exception {
966985
PORT,
967986
ImmutableList.of(),
968987
ImmutableList.of(),
969-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(REMOTE_IP, 32)),
988+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
989+
InetAddresses.forString(REMOTE_IP), 32)),
970990
EnvoyServerProtoData.ConnectionSourceType.ANY,
971991
ImmutableList.of(),
972992
ImmutableList.of(),
@@ -981,9 +1001,11 @@ public void filterChain_5stepMatch() throws Exception {
9811001
EnvoyServerProtoData.FilterChainMatch filterChainMatch2 =
9821002
EnvoyServerProtoData.FilterChainMatch.create(
9831003
0,
984-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.1.2.0", 30)),
1004+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
1005+
InetAddresses.forString("10.1.2.0"), 30)),
9851006
ImmutableList.of(),
986-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.4.0.0", 16)),
1007+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
1008+
InetAddresses.forString("10.4.0.0"), 16)),
9871009
EnvoyServerProtoData.ConnectionSourceType.ANY,
9881010
ImmutableList.of(),
9891011
ImmutableList.of(),
@@ -997,8 +1019,10 @@ public void filterChain_5stepMatch() throws Exception {
9971019
EnvoyServerProtoData.FilterChainMatch.create(
9981020
0,
9991021
ImmutableList.of(
1000-
EnvoyServerProtoData.CidrRange.create("192.168.2.0", 24),
1001-
EnvoyServerProtoData.CidrRange.create("10.1.2.0", 30)),
1022+
EnvoyServerProtoData.CidrRange.create(
1023+
InetAddresses.forString("192.168.2.0"), 24),
1024+
EnvoyServerProtoData.CidrRange.create(
1025+
InetAddresses.forString("10.1.2.0"), 30)),
10021026
ImmutableList.of(),
10031027
ImmutableList.of(),
10041028
EnvoyServerProtoData.ConnectionSourceType.SAME_IP_OR_LOOPBACK,
@@ -1015,10 +1039,13 @@ public void filterChain_5stepMatch() throws Exception {
10151039
EnvoyServerProtoData.FilterChainMatch.create(
10161040
0,
10171041
ImmutableList.of(
1018-
EnvoyServerProtoData.CidrRange.create("10.1.0.0", 16),
1019-
EnvoyServerProtoData.CidrRange.create("10.1.2.0", 30)),
1042+
EnvoyServerProtoData.CidrRange.create(
1043+
InetAddresses.forString("10.1.0.0"), 16),
1044+
EnvoyServerProtoData.CidrRange.create(
1045+
InetAddresses.forString("10.1.2.0"), 30)),
10201046
ImmutableList.of(),
1021-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.4.2.0", 24)),
1047+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
1048+
InetAddresses.forString("10.4.2.0"), 24)),
10221049
EnvoyServerProtoData.ConnectionSourceType.EXTERNAL,
10231050
ImmutableList.of(16000, 9000),
10241051
ImmutableList.of(),
@@ -1034,12 +1061,16 @@ public void filterChain_5stepMatch() throws Exception {
10341061
EnvoyServerProtoData.FilterChainMatch.create(
10351062
0,
10361063
ImmutableList.of(
1037-
EnvoyServerProtoData.CidrRange.create("10.1.0.0", 16),
1038-
EnvoyServerProtoData.CidrRange.create("10.1.2.0", 30)),
1064+
EnvoyServerProtoData.CidrRange.create(
1065+
InetAddresses.forString("10.1.0.0"), 16),
1066+
EnvoyServerProtoData.CidrRange.create(
1067+
InetAddresses.forString("10.1.2.0"), 30)),
10391068
ImmutableList.of(),
10401069
ImmutableList.of(
1041-
EnvoyServerProtoData.CidrRange.create("10.4.2.0", 24),
1042-
EnvoyServerProtoData.CidrRange.create("192.168.2.0", 24)),
1070+
EnvoyServerProtoData.CidrRange.create(
1071+
InetAddresses.forString("10.4.2.0"), 24),
1072+
EnvoyServerProtoData.CidrRange.create(
1073+
InetAddresses.forString("192.168.2.0"), 24)),
10431074
EnvoyServerProtoData.ConnectionSourceType.ANY,
10441075
ImmutableList.of(15000, 8000),
10451076
ImmutableList.of(),
@@ -1053,7 +1084,8 @@ public void filterChain_5stepMatch() throws Exception {
10531084
EnvoyServerProtoData.FilterChainMatch filterChainMatch6 =
10541085
EnvoyServerProtoData.FilterChainMatch.create(
10551086
0,
1056-
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.1.2.0", 29)),
1087+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
1088+
InetAddresses.forString("10.1.2.0"), 29)),
10571089
ImmutableList.of(),
10581090
ImmutableList.of(),
10591091
EnvoyServerProtoData.ConnectionSourceType.ANY,
@@ -1105,8 +1137,8 @@ public void filterChainMatch_unsupportedMatchers() throws Exception {
11051137
EnvoyServerProtoData.FilterChainMatch filterChainMatch1 =
11061138
EnvoyServerProtoData.FilterChainMatch.create(
11071139
0 /* destinationPort */,
1108-
ImmutableList.of(
1109-
EnvoyServerProtoData.CidrRange.create("10.1.0.0", 16)) /* prefixRange */,
1140+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
1141+
InetAddresses.forString("10.1.0.0"), 16)) /* prefixRange */,
11101142
ImmutableList.of("managed-mtls", "h2") /* applicationProtocol */,
11111143
ImmutableList.of() /* sourcePrefixRanges */,
11121144
EnvoyServerProtoData.ConnectionSourceType.ANY /* sourceType */,
@@ -1117,8 +1149,8 @@ public void filterChainMatch_unsupportedMatchers() throws Exception {
11171149
EnvoyServerProtoData.FilterChainMatch filterChainMatch2 =
11181150
EnvoyServerProtoData.FilterChainMatch.create(
11191151
0 /* destinationPort */,
1120-
ImmutableList.of(
1121-
EnvoyServerProtoData.CidrRange.create("10.0.0.0", 8)) /* prefixRange */,
1152+
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
1153+
InetAddresses.forString("10.0.0.0"), 8)) /* prefixRange */,
11221154
ImmutableList.of() /* applicationProtocol */,
11231155
ImmutableList.of() /* sourcePrefixRanges */,
11241156
EnvoyServerProtoData.ConnectionSourceType.ANY /* sourceType */,

0 commit comments

Comments
 (0)