Skip to content

Commit c5bf128

Browse files
committed
address comments
1 parent 4b626ad commit c5bf128

19 files changed

Lines changed: 692 additions & 444 deletions

interop-testing/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ dependencies {
5050
project(':grpc-inprocess'),
5151
project(':grpc-core'),
5252
libraries.mockito.core,
53-
libraries.okhttp,
54-
libraries.cel.compiler
53+
libraries.okhttp
5554

5655
signature (libraries.signature.java) {
5756
artifact {

xds/src/main/java/io/grpc/xds/internal/matcher/CelCommon.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@
1818

1919
import dev.cel.common.CelAbstractSyntaxTree;
2020
import dev.cel.common.CelOptions;
21+
import dev.cel.common.ast.CelReference;
2122
import dev.cel.runtime.CelRuntime;
2223
import dev.cel.runtime.CelRuntimeFactory;
24+
import dev.cel.runtime.CelStandardFunctions;
25+
import dev.cel.runtime.CelStandardFunctions.StandardFunction;
26+
import dev.cel.runtime.standard.AddOperator.AddOverload;
27+
import java.util.Map;
2328

2429
/**
2530
* Shared utilities for CEL-based matchers and extractors.
@@ -31,17 +36,16 @@ final class CelCommon {
3136
.build();
3237

3338

34-
private static final dev.cel.runtime.CelStandardFunctions FUNCTIONS =
35-
dev.cel.runtime.CelStandardFunctions.newBuilder()
39+
private static final CelStandardFunctions FUNCTIONS =
40+
CelStandardFunctions.newBuilder()
3641
.filterFunctions((func, over) -> {
37-
if (func == dev.cel.runtime.CelStandardFunctions.StandardFunction.STRING) {
42+
if (func == StandardFunction.STRING) {
3843
return false;
3944
}
40-
if (func == dev.cel.runtime.CelStandardFunctions.StandardFunction.ADD) {
41-
return !over.equals(
42-
(Object) dev.cel.runtime.standard.AddOperator.AddOverload.ADD_STRING)
43-
&& !over.equals(
44-
(Object) dev.cel.runtime.standard.AddOperator.AddOverload.ADD_LIST);
45+
if (func == StandardFunction.ADD) {
46+
// TODO: fix this, remove (object) casting when we upgrade to 0.12.0
47+
return !over.equals((Object) AddOverload.ADD_STRING)
48+
&& !over.equals((Object) AddOverload.ADD_LIST);
4549
}
4650
return true;
4751
})
@@ -56,9 +60,9 @@ final class CelCommon {
5660
private CelCommon() {}
5761

5862
static void checkAllowedVariables(CelAbstractSyntaxTree ast) {
59-
for (java.util.Map.Entry<Long, dev.cel.common.ast.CelReference> entry :
63+
for (Map.Entry<Long, CelReference> entry :
6064
ast.getReferenceMap().entrySet()) {
61-
dev.cel.common.ast.CelReference ref = entry.getValue();
65+
CelReference ref = entry.getValue();
6266
// If overload_id is empty, it's a variable reference or type name.
6367
// We only support "request".
6468
if (!ref.value().isPresent() && ref.overloadIds().isEmpty()) {

xds/src/main/java/io/grpc/xds/internal/matcher/CelStateMatcher.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package io.grpc.xds.internal.matcher;
1818

1919
import com.github.xds.core.v3.TypedExtensionConfig;
20+
import com.github.xds.type.matcher.v3.CelMatcher; // Proto
21+
import com.github.xds.type.v3.CelExpression;
2022
import dev.cel.common.CelAbstractSyntaxTree;
2123
import dev.cel.common.CelProtoAbstractSyntaxTree;
2224
import dev.cel.runtime.CelEvaluationException;
@@ -25,9 +27,9 @@
2527
* Matcher for CEL expressions handling xDS CEL Matcher extension.
2628
*/
2729
final class CelStateMatcher implements Matcher {
28-
private final CelMatcher compiledEndpoint;
30+
private final io.grpc.xds.internal.matcher.CelMatcher compiledEndpoint;
2931

30-
CelStateMatcher(CelMatcher compiledEndpoint) {
32+
CelStateMatcher(io.grpc.xds.internal.matcher.CelMatcher compiledEndpoint) {
3133
this.compiledEndpoint = compiledEndpoint;
3234
}
3335

@@ -47,21 +49,22 @@ public Class<?> inputType() {
4749

4850
static final class Provider implements MatcherProvider {
4951
@Override
50-
public Matcher getMatcher(TypedExtensionConfig config) {
52+
public CelStateMatcher getMatcher(TypedExtensionConfig config) {
5153
try {
52-
com.github.xds.type.matcher.v3.CelMatcher celProto = config.getTypedConfig()
53-
.unpack(com.github.xds.type.matcher.v3.CelMatcher.class);
54+
CelMatcher celProto = config.getTypedConfig()
55+
.unpack(CelMatcher.class);
5456
if (!celProto.hasExprMatch()) {
5557
throw new IllegalArgumentException("CelMatcher must have expr_match");
5658
}
57-
com.github.xds.type.v3.CelExpression expr = celProto.getExprMatch();
59+
CelExpression expr = celProto.getExprMatch();
5860
if (!expr.hasCelExprChecked()) {
5961
throw new IllegalArgumentException("CelMatcher must have cel_expr_checked");
6062
}
6163
CelAbstractSyntaxTree ast =
6264
CelProtoAbstractSyntaxTree.fromCheckedExpr(
6365
expr.getCelExprChecked()).getAst();
64-
CelMatcher compiled = CelMatcher.compile(ast);
66+
io.grpc.xds.internal.matcher.CelMatcher compiled =
67+
io.grpc.xds.internal.matcher.CelMatcher.compile(ast);
6568

6669
return new CelStateMatcher(compiled);
6770
} catch (Exception e) {

xds/src/main/java/io/grpc/xds/internal/matcher/CelStringExtractor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import dev.cel.common.types.SimpleType;
2121
import dev.cel.runtime.CelEvaluationException;
2222
import dev.cel.runtime.CelRuntime;
23+
import dev.cel.runtime.CelVariableResolver;
2324

2425
/**
2526
* Executes compiled CEL expressions that extract a string.
@@ -52,8 +53,8 @@ public static CelStringExtractor compile(CelAbstractSyntaxTree ast)
5253
*/
5354
public String extract(Object input) throws CelEvaluationException {
5455
Object result;
55-
if (input instanceof dev.cel.runtime.CelVariableResolver) {
56-
result = program.eval((dev.cel.runtime.CelVariableResolver) input);
56+
if (input instanceof CelVariableResolver) {
57+
result = program.eval((CelVariableResolver) input);
5758
} else if (input instanceof java.util.Map) {
5859
@SuppressWarnings("unchecked")
5960
java.util.Map<String, ?> mapInput = (java.util.Map<String, ?>) input;

xds/src/main/java/io/grpc/xds/internal/matcher/HeaderMatchInput.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ final class HeaderMatchInput implements MatchInput {
5151
}
5252

5353
@Override
54-
public Object apply(MatchContext context) {
54+
public String apply(MatchContext context) {
5555
if ("te".equals(headerName)) {
5656
return null;
5757
}
@@ -88,7 +88,7 @@ public Class<?> outputType() {
8888

8989
static final class Provider implements MatchInputProvider {
9090
@Override
91-
public MatchInput getInput(TypedExtensionConfig config) {
91+
public HeaderMatchInput getInput(TypedExtensionConfig config) {
9292
try {
9393
HttpRequestHeaderMatchInput proto = config.getTypedConfig()
9494
.unpack(HttpRequestHeaderMatchInput.class);

xds/src/main/java/io/grpc/xds/internal/matcher/HttpAttributesCelMatchInput.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class HttpAttributesCelMatchInput implements MatchInput {
2828
private HttpAttributesCelMatchInput() {}
2929

3030
@Override
31-
public Object apply(MatchContext context) {
31+
public GrpcCelEnvironment apply(MatchContext context) {
3232
return new GrpcCelEnvironment(context);
3333
}
3434

@@ -39,7 +39,7 @@ public Class<?> outputType() {
3939

4040
static final class Provider implements MatchInputProvider {
4141
@Override
42-
public MatchInput getInput(TypedExtensionConfig config) {
42+
public HttpAttributesCelMatchInput getInput(TypedExtensionConfig config) {
4343
return INSTANCE;
4444
}
4545

xds/src/main/java/io/grpc/xds/internal/matcher/MatchInput.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,5 @@ public interface MatchInput {
3434
/**
3535
* Returns the type of value extracted by this input.
3636
*/
37-
default Class<?> outputType() {
38-
return Object.class;
39-
}
37+
Class<?> outputType();
4038
}

xds/src/main/java/io/grpc/xds/internal/matcher/MatchResult.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,52 @@
2222
import java.util.ArrayList;
2323
import java.util.Collections;
2424
import java.util.List;
25+
import javax.annotation.Nullable;
2526

2627
/**
2728
* Result of a matching operation.
2829
*/
2930
public final class MatchResult {
30-
public final List<TypedExtensionConfig> actions;
31+
@Nullable
32+
public final TypedExtensionConfig action;
33+
public final List<TypedExtensionConfig> keepMatchingActions;
3134
public final boolean matched;
3235

33-
private MatchResult(List<TypedExtensionConfig> actions, boolean matched) {
34-
this.actions = checkNotNull(actions, "actions");
36+
private MatchResult(
37+
@Nullable TypedExtensionConfig action,
38+
List<TypedExtensionConfig> keepMatchingActions,
39+
boolean matched) {
40+
this.action = action;
41+
this.keepMatchingActions =
42+
Collections.unmodifiableList(
43+
new ArrayList<>(checkNotNull(keepMatchingActions, "keepMatchingActions")));
3544
this.matched = matched;
3645
}
3746

47+
/**
48+
* Creates a result indicating a successful match with a terminal action.
49+
*/
50+
public static MatchResult create(
51+
@Nullable TypedExtensionConfig action,
52+
List<TypedExtensionConfig> keepMatchingActions) {
53+
return new MatchResult(action, keepMatchingActions, true);
54+
}
55+
56+
/**
57+
* Creates a result indicating a match with a terminal action and no accumulated actions.
58+
*/
3859
public static MatchResult create(TypedExtensionConfig action) {
39-
return new MatchResult(Collections.singletonList(action), true);
60+
return new MatchResult(action, Collections.emptyList(), true);
4061
}
4162

42-
public static MatchResult create(List<TypedExtensionConfig> actions) {
43-
return new MatchResult(new ArrayList<>(actions), true);
63+
/**
64+
* Creates a result indicating no terminal match, but potentially with accumulated actions.
65+
*/
66+
public static MatchResult noMatch(List<TypedExtensionConfig> keepMatchingActions) {
67+
return new MatchResult(null, keepMatchingActions, false);
4468
}
4569

4670
public static MatchResult noMatch() {
47-
return new MatchResult(Collections.emptyList(), false);
71+
return new MatchResult(null, Collections.emptyList(), false);
4872
}
4973
}

xds/src/main/java/io/grpc/xds/internal/matcher/MatcherList.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@
2121
import io.grpc.xds.internal.matcher.MatcherRunner.MatchContext;
2222
import java.util.ArrayList;
2323
import java.util.List;
24+
import java.util.function.Predicate;
2425
import javax.annotation.Nullable;
2526

2627
final class MatcherList extends UnifiedMatcher {
2728
private final List<FieldMatcher> matchers;
28-
@Nullable private final OnMatch onNoMatch;
29+
@Nullable
30+
private final OnMatch onNoMatch;
2931

3032
MatcherList(Matcher.MatcherList proto, @Nullable Matcher.OnMatch onNoMatchProto,
31-
java.util.function.Predicate<String> actionValidator) {
33+
Predicate<String> actionValidator) {
3234
if (proto.getMatchersCount() == 0) {
3335
throw new IllegalArgumentException("MatcherList must contain at least one FieldMatcher");
3436
}
@@ -50,45 +52,43 @@ public MatchResult match(MatchContext context, int depth) {
5052
}
5153

5254
List<TypedExtensionConfig> accumulated = new ArrayList<>();
53-
boolean matchedAtLeastOnce = false;
5455
for (FieldMatcher matcher : matchers) {
5556
if (matcher.matches(context)) {
5657
MatchResult result = matcher.onMatch.evaluate(context, depth);
57-
if (result.matched) {
58-
accumulated.addAll(result.actions);
59-
matchedAtLeastOnce = true;
60-
}
58+
accumulated.addAll(result.keepMatchingActions);
6159

62-
if (!matcher.onMatch.keepMatching) {
63-
if (!matchedAtLeastOnce) {
64-
return MatchResult.noMatch();
60+
if (result.matched) {
61+
if (matcher.onMatch.keepMatching) {
62+
if (result.action != null) {
63+
accumulated.add(result.action);
64+
}
65+
} else {
66+
return MatchResult.create(result.action, accumulated);
67+
}
68+
} else {
69+
if (!matcher.onMatch.keepMatching) {
70+
return MatchResult.noMatch(accumulated);
6571
}
66-
break;
6772
}
6873
}
6974
}
7075

71-
if (!matchedAtLeastOnce) {
72-
if (onNoMatch != null) {
73-
MatchResult noMatchResult = onNoMatch.evaluate(context, depth);
74-
if (noMatchResult.matched) {
75-
accumulated.addAll(noMatchResult.actions);
76-
matchedAtLeastOnce = true;
77-
}
76+
if (onNoMatch != null) {
77+
MatchResult noMatchResult = onNoMatch.evaluate(context, depth);
78+
accumulated.addAll(noMatchResult.keepMatchingActions);
79+
if (noMatchResult.matched) {
80+
return MatchResult.create(noMatchResult.action, accumulated);
7881
}
7982
}
80-
if (matchedAtLeastOnce) {
81-
return MatchResult.create(accumulated);
82-
}
83-
return MatchResult.noMatch();
83+
return MatchResult.noMatch(accumulated);
8484
}
8585

8686
private static final class FieldMatcher {
8787
private final PredicateEvaluator predicate;
8888
private final OnMatch onMatch;
8989

9090
FieldMatcher(Matcher.MatcherList.FieldMatcher proto,
91-
java.util.function.Predicate<String> actionValidator) {
91+
Predicate<String> actionValidator) {
9292
this.predicate = PredicateEvaluator.fromProto(proto.getPredicate());
9393
this.onMatch = new OnMatch(proto.getOnMatch(), actionValidator);
9494
}

0 commit comments

Comments
 (0)