Skip to content

Commit 28d263e

Browse files
committed
Move the vmConfig and pluginConfig state to the Handler.
Signed-off-by: Hiram Chirino <hiram@hiramchirino.com>
1 parent 915bd54 commit 28d263e

File tree

10 files changed

+80
-63
lines changed

10 files changed

+80
-63
lines changed

proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ProxyWasm.java

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.dylibso.chicory.wasm.types.MemoryLimits;
1414
import com.dylibso.chicory.wasm.types.ValueType;
1515
import java.io.Closeable;
16-
import java.nio.charset.StandardCharsets;
1716
import java.util.HashMap;
1817
import java.util.List;
1918
import java.util.Map;
@@ -24,8 +23,6 @@ public final class ProxyWasm implements Closeable {
2423

2524
private final ABI abi;
2625
private final Handler pluginHandler;
27-
private final byte[] pluginConfig;
28-
private final byte[] vmConfig;
2926
private final WasiPreview1 wasi;
3027

3128
private final AtomicInteger nextContextID = new AtomicInteger(1);
@@ -38,8 +35,6 @@ public final class ProxyWasm implements Closeable {
3835
private byte[] httpCallResponseBody;
3936

4037
private ProxyWasm(Builder other) throws StartException {
41-
this.vmConfig = other.vmConfig;
42-
this.pluginConfig = other.pluginConfig;
4338
this.pluginHandler = Objects.requireNonNullElse(other.pluginHandler, new Handler() {});
4439
this.wasi = other.wasi;
4540
this.abi = other.abi;
@@ -64,10 +59,14 @@ public void start() throws StartException {
6459

6560
this.pluginContext = new PluginContext(this, pluginHandler);
6661
registerContext(pluginContext, 0);
67-
if (!this.abi.proxyOnVmStart(pluginContext.id(), vmConfig.length)) {
62+
63+
byte[] vmConfig = this.pluginHandler.getVmConfig();
64+
if (!this.abi.proxyOnVmStart(pluginContext.id(), len(vmConfig))) {
6865
throw new StartException("proxy_on_vm_start failed");
6966
}
70-
if (!this.abi.proxyOnConfigure(pluginContext.id(), pluginConfig.length)) {
67+
68+
byte[] pluginConfig = this.pluginHandler.getPluginConfig();
69+
if (!this.abi.proxyOnConfigure(pluginContext.id(), len(pluginConfig))) {
7170
throw new StartException("proxy_on_configure failed");
7271
}
7372
}
@@ -87,16 +86,6 @@ protected Handler next() {
8786
return activeContext.handler();
8887
}
8988

90-
@Override
91-
public byte[] getVmConfig() {
92-
return vmConfig;
93-
}
94-
95-
@Override
96-
public byte[] getPluginConfig() {
97-
return pluginConfig;
98-
}
99-
10089
@Override
10190
public WasmResult setEffectiveContextID(int contextID) {
10291
Context context = contexts.get(contextID);
@@ -225,8 +214,6 @@ public static class Builder implements Cloneable {
225214
private final ABI abi = new ABI();
226215
private WasiPreview1 wasi;
227216

228-
private byte[] vmConfig = new byte[0];
229-
private byte[] pluginConfig = new byte[0];
230217
private Handler pluginHandler;
231218
private ImportMemory memory;
232219
private WasiOptions wasiOptions;
@@ -251,26 +238,6 @@ public Builder withStart(boolean start) {
251238
return this;
252239
}
253240

254-
public ProxyWasm.Builder withVmConfig(byte[] vmConfig) {
255-
this.vmConfig = vmConfig;
256-
return this;
257-
}
258-
259-
public ProxyWasm.Builder withVmConfig(String vmConfig) {
260-
this.vmConfig = vmConfig.getBytes(StandardCharsets.UTF_8);
261-
return this;
262-
}
263-
264-
public ProxyWasm.Builder withPluginConfig(byte[] pluginConfig) {
265-
this.pluginConfig = pluginConfig;
266-
return this;
267-
}
268-
269-
public ProxyWasm.Builder withPluginConfig(String pluginConfig) {
270-
this.pluginConfig = pluginConfig.getBytes(StandardCharsets.UTF_8);
271-
return this;
272-
}
273-
274241
public ProxyWasm.Builder withPluginHandler(Handler vmHandler) {
275242
this.pluginHandler = vmHandler;
276243
return this;

proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/EchoHttpBodyTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public class EchoHttpBodyTest {
3131
@BeforeEach
3232
void setUp() throws StartException {
3333
this.handler = new MockHandler();
34-
ProxyWasm.Builder builder = ProxyWasm.builder();
35-
builder.withPluginConfig("echo");
36-
this.proxyWasm = builder.build(module);
34+
handler.setPluginConfig("echo");
35+
36+
this.proxyWasm = ProxyWasm.builder().withPluginHandler(handler).build(module);
3737
this.httpContext = proxyWasm.createHttpContext(handler);
3838
}
3939

proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpHeadersTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public void onHttpResponseHeaders() throws StartException {
5757
var config =
5858
String.format(
5959
"{\"header\": \"%s\", \"value\": \"%s\"}", "x-wasm-header", "x-value");
60-
try (var proxyWasm = ProxyWasm.builder().withPluginConfig(config).build(module)) {
60+
handler.setPluginConfig(config);
61+
try (var proxyWasm = ProxyWasm.builder().withPluginHandler(handler).build(module)) {
6162
int id = 0;
6263
try (var host = proxyWasm.createHttpContext(handler)) {
6364
id = host.id();
@@ -82,6 +83,8 @@ public void onHttpResponseHeaders() throws StartException {
8283
// Check logs
8384
handler.assertSortedLogsEqual(
8485
String.format("%d finished", id),
86+
"loading plugin config",
87+
"header from config: x-wasm-header = x-value",
8588
"response header <-- key2: value2",
8689
"response header <-- key1: value1",
8790
"adding header: x-wasm-header=x-value",

proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpRoutingTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class HttpRoutingTest {
2121
@Test
2222
public void canary() throws StartException {
2323
var handler = new MockHandler();
24-
ProxyWasm.Builder builder =
25-
ProxyWasm.builder().withPluginHandler(handler).withPluginConfig(new byte[] {2});
24+
handler.setPluginConfig(new byte[] {2});
25+
ProxyWasm.Builder builder = ProxyWasm.builder().withPluginHandler(handler);
2626
try (var host = builder.build(module)) {
2727
try (var context = host.createHttpContext(handler)) {
2828

@@ -47,9 +47,9 @@ public void canary() throws StartException {
4747
@Test
4848
public void nonCanary() throws StartException {
4949
var handler = new MockHandler();
50+
handler.setPluginConfig(new byte[] {1});
5051
var module = Parser.parse(Path.of("./src/test/go-examples/http_routing/main.wasm"));
51-
ProxyWasm.Builder builder =
52-
ProxyWasm.builder().withPluginHandler(handler).withPluginConfig(new byte[] {1});
52+
ProxyWasm.Builder builder = ProxyWasm.builder().withPluginHandler(handler);
5353
try (var host = builder.build(module)) {
5454
try (var context = host.createHttpContext(handler)) {
5555

proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/JsonValidationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ class OnHttpRequestBody {
147147
@BeforeEach
148148
void setUp() throws StartException {
149149
var config = "{\"requiredKeys\": [\"my_key\"]}";
150-
this.host = ProxyWasm.builder().withPluginConfig(config).build(module);
150+
handler.setPluginConfig(config);
151+
this.host = ProxyWasm.builder().withPluginHandler(handler).build(module);
151152
this.context = host.createHttpContext(handler);
152153
}
153154

proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MockHandler.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.roastedroot.proxywasm.StreamType;
1717
import io.roastedroot.proxywasm.WasmException;
1818
import io.roastedroot.proxywasm.WasmResult;
19+
import java.nio.charset.StandardCharsets;
1920
import java.util.ArrayList;
2021
import java.util.HashMap;
2122
import java.util.List;
@@ -67,6 +68,8 @@ public HttpResponse(
6768
private byte[] downStreamData = new byte[0];
6869
private byte[] upstreamData = new byte[0];
6970
private byte[] grpcReceiveBuffer = new byte[0];
71+
private byte[] vmConfig;
72+
private byte[] pluginConfig;
7073

7174
static final boolean DEBUG = "true".equals(System.getenv("DEBUG"));
7275

@@ -119,6 +122,32 @@ public void assertLogsDoNotContain(String... message) {
119122
}
120123
}
121124

125+
@Override
126+
public byte[] getVmConfig() {
127+
return vmConfig;
128+
}
129+
130+
public void setVmConfig(byte[] vmConfig) {
131+
this.vmConfig = vmConfig;
132+
}
133+
134+
public void setVmConfig(String vmConfig) {
135+
this.vmConfig = vmConfig.getBytes(StandardCharsets.UTF_8);
136+
}
137+
138+
@Override
139+
public byte[] getPluginConfig() {
140+
return pluginConfig;
141+
}
142+
143+
public void setPluginConfig(byte[] pluginConfig) {
144+
this.pluginConfig = pluginConfig;
145+
}
146+
147+
public void setPluginConfig(String pluginConfig) {
148+
this.pluginConfig = pluginConfig.getBytes(StandardCharsets.UTF_8);
149+
}
150+
122151
@Override
123152
public WasmResult setTickPeriodMilliseconds(int tickPeriodMilliseconds) {
124153
this.tickPeriodMilliseconds = tickPeriodMilliseconds;

proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/SharedQueueTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ public void testOnPluginStart() throws StartException, WasmException {
5757

5858
// Create and configure the http_request_headers receiver instance
5959
var receiverHandler1 = new MockHandler(sharedData);
60+
receiverHandler1.setPluginConfig("http_request_headers");
6061
receiverHandler1.setProperty(List.of("vm_id"), bytes(receiverVmId));
6162
var receiverHost1 =
6263
deferClose(
6364
ProxyWasm.builder()
6465
.withPluginHandler(receiverHandler1)
65-
.withPluginConfig("http_request_headers")
6666
.build(receiverModule));
6767

6868
var requestHeadersQueueId =
@@ -76,12 +76,12 @@ public void testOnPluginStart() throws StartException, WasmException {
7676

7777
// Create and configure the http_response_headers receiver instance
7878
var receiverHandler2 = new MockHandler(sharedData);
79+
receiverHandler2.setPluginConfig("http_response_headers");
7980
receiverHandler2.setProperty(List.of("vm_id"), bytes(receiverVmId));
8081
var receiverHost2 =
8182
deferClose(
8283
ProxyWasm.builder()
8384
.withPluginHandler(receiverHandler2)
84-
.withPluginConfig("http_response_headers")
8585
.build(receiverModule));
8686

8787
var responseHeadersQueueId =
@@ -97,14 +97,12 @@ public void testOnPluginStart() throws StartException, WasmException {
9797

9898
// Create and configure the sender instance
9999
var senderHandler = new MockHandler(sharedData);
100+
senderHandler.setPluginConfig("http");
100101
var senderVmId = "sender";
101102
senderHandler.setProperty(List.of("vm_id"), bytes(senderVmId));
102103
var senderHost =
103104
deferClose(
104-
ProxyWasm.builder()
105-
.withPluginHandler(senderHandler)
106-
.withPluginConfig("http")
107-
.build(senderModule));
105+
ProxyWasm.builder().withPluginHandler(senderHandler).build(senderModule));
108106
senderHandler.assertLogsContain(
109107
String.format("contextID=%d is configured for %s", senderHost.contextId(), "http"));
110108

proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/VmPluginConfigurationTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ public class VmPluginConfigurationTest {
2222
@Test
2323
public void test() throws StartException {
2424
var handler = new MockHandler();
25+
handler.setPluginConfig("plugin_config");
26+
handler.setVmConfig("vm_config");
27+
2528
handler.setProperty(List.of("plugin_name"), bytes("vm_plugin_configuration"));
26-
ProxyWasm.Builder builder =
27-
ProxyWasm.builder()
28-
.withPluginConfig("plugin_config")
29-
.withVmConfig("vm_config")
30-
.withPluginHandler(handler);
29+
ProxyWasm.Builder builder = ProxyWasm.builder().withPluginHandler(handler);
3130
try (var ignored = builder.build(module)) {
3231

3332
assertEquals(

proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/PluginHandler.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ public void close() {
4848
}
4949
}
5050

51+
// //////////////////////////////////////////////////////////////////////
52+
// Plugin config
53+
// //////////////////////////////////////////////////////////////////////
54+
55+
byte[] vmConfig;
56+
57+
@Override
58+
public byte[] getVmConfig() {
59+
return vmConfig;
60+
}
61+
62+
byte[] pluginConfig;
63+
64+
@Override
65+
public byte[] getPluginConfig() {
66+
return pluginConfig;
67+
}
68+
5169
// //////////////////////////////////////////////////////////////////////
5270
// Properties
5371
// //////////////////////////////////////////////////////////////////////

proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPlugin.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.roastedroot.proxywasm.jaxrs;
22

3+
import static io.roastedroot.proxywasm.Helpers.bytes;
4+
35
import com.dylibso.chicory.runtime.ImportMemory;
46
import com.dylibso.chicory.runtime.Instance;
57
import com.dylibso.chicory.wasm.WasmModule;
@@ -101,22 +103,22 @@ public WasmPlugin.Builder withShared(boolean shared) {
101103
}
102104

103105
public WasmPlugin.Builder withVmConfig(byte[] vmConfig) {
104-
proxyWasmBuilder = proxyWasmBuilder.withVmConfig(vmConfig);
106+
this.handler.vmConfig = vmConfig;
105107
return this;
106108
}
107109

108110
public WasmPlugin.Builder withVmConfig(String vmConfig) {
109-
proxyWasmBuilder = proxyWasmBuilder.withVmConfig(vmConfig);
111+
this.handler.vmConfig = bytes(vmConfig);
110112
return this;
111113
}
112114

113115
public WasmPlugin.Builder withPluginConfig(byte[] pluginConfig) {
114-
proxyWasmBuilder = proxyWasmBuilder.withPluginConfig(pluginConfig);
116+
this.handler.pluginConfig = pluginConfig;
115117
return this;
116118
}
117119

118120
public WasmPlugin.Builder withPluginConfig(String pluginConfig) {
119-
proxyWasmBuilder = proxyWasmBuilder.withPluginConfig(pluginConfig);
121+
this.handler.pluginConfig = bytes(pluginConfig);
120122
return this;
121123
}
122124

0 commit comments

Comments
 (0)