Skip to content

Commit 766161a

Browse files
mtdowlingadwsingh
authored andcommitted
Update VM and rules engine to work with BDD
Now the VM supports running the bytecode of a specific condition or resolving a specific result. Control flow is owned by the BddEvaluator, and VM condition handling is done through a ConditionEvaluator.
1 parent 956fe45 commit 766161a

48 files changed

Lines changed: 35102 additions & 42161 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

aws/client/aws-client-rulesengine/build.gradle.kts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies {
1515

1616
testImplementation(libs.smithy.aws.traits)
1717
testImplementation(project(":aws:client:aws-client-restxml"))
18+
testImplementation(project(":aws:client:aws-client-restjson"))
1819
testImplementation(project(":client:dynamic-client"))
1920
}
2021

@@ -32,10 +33,10 @@ sourceSets {
3233
}
3334

3435
jmh {
35-
warmupIterations = 2
36+
warmupIterations = 3
3637
iterations = 5
3738
fork = 1
38-
profilers.add("async:output=flamegraph")
39+
profilers.add("async:output=flamegraph")
3940
// profilers.add("gc")
40-
duplicateClassesStrategy = DuplicatesStrategy.WARN
41+
duplicateClassesStrategy = DuplicatesStrategy.EXCLUDE // don't dump a bunch of warnings.
4142
}

aws/client/aws-client-rulesengine/src/jmh/java/software/amazon/smithy/java/aws/client/rulesengine/Bench.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,17 @@
99
import java.util.concurrent.TimeUnit;
1010
import org.openjdk.jmh.annotations.Benchmark;
1111
import org.openjdk.jmh.annotations.BenchmarkMode;
12-
import org.openjdk.jmh.annotations.Fork;
13-
import org.openjdk.jmh.annotations.Measurement;
1412
import org.openjdk.jmh.annotations.Mode;
1513
import org.openjdk.jmh.annotations.OutputTimeUnit;
1614
import org.openjdk.jmh.annotations.Scope;
1715
import org.openjdk.jmh.annotations.Setup;
1816
import org.openjdk.jmh.annotations.State;
19-
import org.openjdk.jmh.annotations.Warmup;
2017
import software.amazon.smithy.java.aws.client.core.settings.EndpointSettings;
2118
import software.amazon.smithy.java.client.core.auth.scheme.AuthSchemeResolver;
2219
import software.amazon.smithy.java.client.core.endpoint.EndpointResolver;
2320
import software.amazon.smithy.java.client.core.endpoint.EndpointResolverParams;
2421
import software.amazon.smithy.java.client.rulesengine.EndpointRulesPlugin;
25-
import software.amazon.smithy.java.client.rulesengine.RulesEngine;
22+
import software.amazon.smithy.java.client.rulesengine.RulesEngineBuilder;
2623
import software.amazon.smithy.java.context.Context;
2724
import software.amazon.smithy.java.core.serde.document.Document;
2825
import software.amazon.smithy.java.dynamicclient.DynamicClient;
@@ -38,9 +35,6 @@
3835
@State(Scope.Benchmark)
3936
@OutputTimeUnit(TimeUnit.NANOSECONDS)
4037
@BenchmarkMode(Mode.AverageTime)
41-
@Warmup(iterations = 2, time = 3)
42-
@Measurement(iterations = 3, time = 3)
43-
@Fork(1)
4438
public class Bench {
4539
private DynamicClient client;
4640
private EndpointResolver endpointResolver;
@@ -57,7 +51,7 @@ public void setup() {
5751
model = customizeS3Model(model);
5852
var service = model.expectShape(ShapeId.from("com.amazonaws.s3#AmazonS3"), ServiceShape.class);
5953

60-
var engine = new RulesEngine();
54+
var engine = new RulesEngineBuilder();
6155
var plugin = EndpointRulesPlugin.create(engine);
6256

6357
client = DynamicClient.builder()
@@ -69,11 +63,13 @@ public void setup() {
6963
endpointResolver = client.config().endpointResolver();
7064
var ctx = Context.create();
7165
ctx.put(EndpointSettings.REGION, "us-east-1");
66+
// ctx.put(EndpointRulesPlugin.ADDITIONAL_ENDPOINT_PARAMS, Map.of("ForcePathStyle", true));
7267

7368
var inputValue = client.createStruct(ShapeId.from("com.amazonaws.s3#GetObjectRequest"),
7469
Document.of(Map.of(
7570
"Bucket",
76-
Document.of("foo"),
71+
Document.of("miked"),
72+
//, Document.of("foo"),
7773
"Key",
7874
Document.of("bar"))));
7975
endpointParams = EndpointResolverParams.builder()

aws/client/aws-client-rulesengine/src/main/java/software/amazon/smithy/java/aws/client/rulesengine/AwsRulesBuiltin.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import java.util.HashMap;
99
import java.util.Map;
10-
import java.util.function.BiFunction;
1110
import java.util.function.Function;
1211
import software.amazon.smithy.java.aws.auth.api.identity.AwsCredentialsIdentity;
1312
import software.amazon.smithy.java.aws.client.core.settings.AccountIdSetting;
@@ -123,21 +122,12 @@ public Object apply(Context context) {
123122
}
124123
};
125124

126-
static final BiFunction<String, Context, Object> BUILTIN_PROVIDER = new BuiltinProvider();
127-
128-
private static final class BuiltinProvider implements BiFunction<String, Context, Object> {
129-
private final Map<String, Function<Context, Object>> providers = new HashMap<>();
130-
131-
private BuiltinProvider() {
132-
for (var e : values()) {
133-
providers.put(e.name, e);
134-
}
135-
}
136-
137-
@Override
138-
public Object apply(String name, Context context) {
139-
var match = providers.get(name);
140-
return match == null ? null : match.apply(context);
125+
static final Map<String, Function<Context, Object>> BUILTINS;
126+
static {
127+
var values = values();
128+
BUILTINS = new HashMap<>(values.length);
129+
for (var e : values) {
130+
BUILTINS.put(e.name, e);
141131
}
142132
}
143133

aws/client/aws-client-rulesengine/src/main/java/software/amazon/smithy/java/aws/client/rulesengine/AwsRulesExtension.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
package software.amazon.smithy.java.aws.client.rulesengine;
77

88
import java.util.Arrays;
9-
import java.util.List;
10-
import java.util.function.BiFunction;
9+
import java.util.Map;
10+
import java.util.function.Function;
1111
import software.amazon.smithy.java.client.rulesengine.RulesExtension;
1212
import software.amazon.smithy.java.client.rulesengine.RulesFunction;
1313
import software.amazon.smithy.java.context.Context;
@@ -21,12 +21,12 @@
2121
@SmithyUnstableApi
2222
public class AwsRulesExtension implements RulesExtension {
2323
@Override
24-
public BiFunction<String, Context, Object> getBuiltinProvider() {
25-
return AwsRulesBuiltin.BUILTIN_PROVIDER;
24+
public void putBuiltinProviders(Map<String, Function<Context, Object>> providers) {
25+
providers.putAll(AwsRulesBuiltin.BUILTINS);
2626
}
2727

2828
@Override
29-
public List<RulesFunction> getFunctions() {
29+
public Iterable<RulesFunction> getFunctions() {
3030
return Arrays.asList(AwsRulesFunction.values());
3131
}
3232
}

aws/client/aws-client-rulesengine/src/main/java/software/amazon/smithy/java/aws/client/rulesengine/AwsRulesFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public String toString() {
128128
}
129129

130130
@Override
131-
public int getOperandCount() {
131+
public int getArgumentCount() {
132132
return operands;
133133
}
134134

aws/client/aws-client-rulesengine/src/shared-resources/software/amazon/smithy/java/aws/client/rulesengine/s3.json

Lines changed: 31599 additions & 38373 deletions
Large diffs are not rendered by default.

aws/client/aws-client-rulesengine/src/test/java/software/amazon/smithy/java/aws/client/rulesengine/ResolverTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import software.amazon.smithy.java.client.core.interceptors.RequestHook;
2828
import software.amazon.smithy.java.client.rulesengine.EndpointRulesPlugin;
2929
import software.amazon.smithy.java.client.rulesengine.EndpointUtils;
30+
import software.amazon.smithy.java.client.rulesengine.RulesEngineBuilder;
3031
import software.amazon.smithy.java.client.rulesengine.RulesEvaluationError;
3132
import software.amazon.smithy.java.core.serde.document.Document;
3233
import software.amazon.smithy.java.dynamicclient.DynamicClient;
@@ -49,6 +50,9 @@ public class ResolverTest {
4950
private static EndpointRulesPlugin plugin;
5051
private static DynamicClient client;
5152

53+
private Map<String, Object> overrideMap = null;
54+
private Object inputParams = null;
55+
5256
// S3 requires a customization to remove buckets from the path :(
5357
private static Model customizeS3Model(Model m) {
5458
return ModelTransformer.create().mapShapes(m, s -> {
@@ -77,7 +81,8 @@ public static void before() throws Exception {
7781
.unwrap();
7882
model = customizeS3Model(model);
7983
service = model.expectShape(ShapeId.from("com.amazonaws.s3#AmazonS3"), ServiceShape.class);
80-
plugin = EndpointRulesPlugin.create();
84+
var engine = new RulesEngineBuilder();
85+
plugin = EndpointRulesPlugin.create(engine);
8186
client = DynamicClient.builder()
8287
.model(model)
8388
.service(service.getId())
@@ -95,7 +100,8 @@ public void caseRunner(EndpointTestCase test) {
95100
try {
96101
var result = resolveEndpoint(test, client);
97102
if (expectedError != null) {
98-
Assertions.fail("Expected ruleset to fail: " + test.getDocumentation() + " : " + expectedError);
103+
Assertions.fail("Expected ruleset to fail: " + test.getDocumentation()
104+
+ " : " + expectedError + " : but got " + result[0]);
99105
}
100106

101107
Endpoint ep = (Endpoint) result[0];
@@ -109,7 +115,10 @@ public void caseRunner(EndpointTestCase test) {
109115
// TODO: validate properties too.
110116
} catch (RulesEvaluationError e) {
111117
if (expectedError == null) {
112-
Assertions.fail("Expected ruleset to succeed: " + test.getDocumentation() + " : " + e, e);
118+
String msg = "Expected ruleset to succeed: " + test.getDocumentation();
119+
msg += " Input: " + inputParams + "\n";
120+
msg += " Overrides: " + overrideMap;
121+
Assertions.fail(msg, e);
113122
}
114123
}
115124
}
@@ -144,7 +153,7 @@ public void readBeforeTransmit(RequestHook<?, ?, ?> hook) {
144153

145154
var inputs = test.getOperationInputs().get(0);
146155
var name = inputs.getOperationName();
147-
var inputParams = EndpointUtils.convertNode(inputs.getOperationParams(), true);
156+
inputParams = EndpointUtils.convertNode(inputs.getOperationParams(), true);
148157

149158
if (!inputs.getBuiltInParams().isEmpty()) {
150159
inputs.getBuiltInParams().getStringMember("SDK::Endpoint").ifPresent(value -> {
@@ -179,8 +188,8 @@ public void readBeforeTransmit(RequestHook<?, ?, ?> hook) {
179188
override.putConfig(S3EndpointSettings.S3_FORCE_PATH_STYLE, value.getValue());
180189
});
181190

182-
override.putConfig(EndpointRulesPlugin.ADDITIONAL_ENDPOINT_PARAMS,
183-
(Map<String, Object>) EndpointUtils.convertNode(test.getParams(), true));
191+
overrideMap = (Map<String, Object>) EndpointUtils.convertNode(test.getParams(), true);
192+
override.putConfig(EndpointRulesPlugin.ADDITIONAL_ENDPOINT_PARAMS, overrideMap);
184193

185194
try {
186195
var document = Document.ofObject(inputParams);
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
plugins {
22
id("smithy-java.module-conventions")
3-
id("me.champeau.jmh") version "0.7.3"
43
}
54

65
description = "Implements the rules engine traits used to resolve endpoints"
@@ -16,13 +15,5 @@ dependencies {
1615

1716
testImplementation(project(":aws:client:aws-client-awsjson"))
1817
testImplementation(project(":client:dynamic-client"))
19-
}
20-
21-
jmh {
22-
warmupIterations = 2
23-
iterations = 5
24-
fork = 1
25-
// profilers.add("async:output=flamegraph")
26-
// profilers.add("gc")
27-
duplicateClassesStrategy = DuplicatesStrategy.WARN
18+
testImplementation(project(":aws:client:aws-client-rulesengine"))
2819
}

client/client-rulesengine/src/jmh/java/software/amazon/smithy/java/client/rulesengine/VmBench.java

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)