From 6c730e7496826ab926b4b5b33c9019cce0778436 Mon Sep 17 00:00:00 2001 From: Hiram Chirino Date: Thu, 20 Mar 2025 20:36:05 -0400 Subject: [PATCH] Move the vmConfig and pluginConfig state to the Handler. Signed-off-by: Hiram Chirino --- .../io/roastedroot/proxywasm/ProxyWasm.java | 45 +++---------------- .../proxywasm/examples/EchoHttpBodyTest.java | 6 +-- .../proxywasm/examples/HttpHeadersTest.java | 5 ++- .../proxywasm/examples/HttpRoutingTest.java | 8 ++-- .../examples/JsonValidationTest.java | 3 +- .../proxywasm/examples/MockHandler.java | 29 ++++++++++++ .../proxywasm/examples/SharedQueueTest.java | 10 ++--- .../examples/VmPluginConfigurationTest.java | 9 ++-- .../proxywasm/jaxrs/PluginHandler.java | 18 ++++++++ .../proxywasm/jaxrs/WasmPlugin.java | 10 +++-- 10 files changed, 80 insertions(+), 63 deletions(-) diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ProxyWasm.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ProxyWasm.java index 630c1b9..0838dbe 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ProxyWasm.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/ProxyWasm.java @@ -13,7 +13,6 @@ import com.dylibso.chicory.wasm.types.MemoryLimits; import com.dylibso.chicory.wasm.types.ValueType; import java.io.Closeable; -import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -24,8 +23,6 @@ public final class ProxyWasm implements Closeable { private final ABI abi; private final Handler pluginHandler; - private final byte[] pluginConfig; - private final byte[] vmConfig; private final WasiPreview1 wasi; private final AtomicInteger nextContextID = new AtomicInteger(1); @@ -38,8 +35,6 @@ public final class ProxyWasm implements Closeable { private byte[] httpCallResponseBody; private ProxyWasm(Builder other) throws StartException { - this.vmConfig = other.vmConfig; - this.pluginConfig = other.pluginConfig; this.pluginHandler = Objects.requireNonNullElse(other.pluginHandler, new Handler() {}); this.wasi = other.wasi; this.abi = other.abi; @@ -64,10 +59,14 @@ public void start() throws StartException { this.pluginContext = new PluginContext(this, pluginHandler); registerContext(pluginContext, 0); - if (!this.abi.proxyOnVmStart(pluginContext.id(), vmConfig.length)) { + + byte[] vmConfig = this.pluginHandler.getVmConfig(); + if (!this.abi.proxyOnVmStart(pluginContext.id(), len(vmConfig))) { throw new StartException("proxy_on_vm_start failed"); } - if (!this.abi.proxyOnConfigure(pluginContext.id(), pluginConfig.length)) { + + byte[] pluginConfig = this.pluginHandler.getPluginConfig(); + if (!this.abi.proxyOnConfigure(pluginContext.id(), len(pluginConfig))) { throw new StartException("proxy_on_configure failed"); } } @@ -87,16 +86,6 @@ protected Handler next() { return activeContext.handler(); } - @Override - public byte[] getVmConfig() { - return vmConfig; - } - - @Override - public byte[] getPluginConfig() { - return pluginConfig; - } - @Override public WasmResult setEffectiveContextID(int contextID) { Context context = contexts.get(contextID); @@ -225,8 +214,6 @@ public static class Builder implements Cloneable { private final ABI abi = new ABI(); private WasiPreview1 wasi; - private byte[] vmConfig = new byte[0]; - private byte[] pluginConfig = new byte[0]; private Handler pluginHandler; private ImportMemory memory; private WasiOptions wasiOptions; @@ -251,26 +238,6 @@ public Builder withStart(boolean start) { return this; } - public ProxyWasm.Builder withVmConfig(byte[] vmConfig) { - this.vmConfig = vmConfig; - return this; - } - - public ProxyWasm.Builder withVmConfig(String vmConfig) { - this.vmConfig = vmConfig.getBytes(StandardCharsets.UTF_8); - return this; - } - - public ProxyWasm.Builder withPluginConfig(byte[] pluginConfig) { - this.pluginConfig = pluginConfig; - return this; - } - - public ProxyWasm.Builder withPluginConfig(String pluginConfig) { - this.pluginConfig = pluginConfig.getBytes(StandardCharsets.UTF_8); - return this; - } - public ProxyWasm.Builder withPluginHandler(Handler vmHandler) { this.pluginHandler = vmHandler; return this; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/EchoHttpBodyTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/EchoHttpBodyTest.java index 6d61e8a..4201ee2 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/EchoHttpBodyTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/EchoHttpBodyTest.java @@ -31,9 +31,9 @@ public class EchoHttpBodyTest { @BeforeEach void setUp() throws StartException { this.handler = new MockHandler(); - ProxyWasm.Builder builder = ProxyWasm.builder(); - builder.withPluginConfig("echo"); - this.proxyWasm = builder.build(module); + handler.setPluginConfig("echo"); + + this.proxyWasm = ProxyWasm.builder().withPluginHandler(handler).build(module); this.httpContext = proxyWasm.createHttpContext(handler); } diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpHeadersTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpHeadersTest.java index 355b470..0f53925 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpHeadersTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpHeadersTest.java @@ -57,7 +57,8 @@ public void onHttpResponseHeaders() throws StartException { var config = String.format( "{\"header\": \"%s\", \"value\": \"%s\"}", "x-wasm-header", "x-value"); - try (var proxyWasm = ProxyWasm.builder().withPluginConfig(config).build(module)) { + handler.setPluginConfig(config); + try (var proxyWasm = ProxyWasm.builder().withPluginHandler(handler).build(module)) { int id = 0; try (var host = proxyWasm.createHttpContext(handler)) { id = host.id(); @@ -82,6 +83,8 @@ public void onHttpResponseHeaders() throws StartException { // Check logs handler.assertSortedLogsEqual( String.format("%d finished", id), + "loading plugin config", + "header from config: x-wasm-header = x-value", "response header <-- key2: value2", "response header <-- key1: value1", "adding header: x-wasm-header=x-value", diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpRoutingTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpRoutingTest.java index ce177d3..348bafd 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpRoutingTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/HttpRoutingTest.java @@ -21,8 +21,8 @@ public class HttpRoutingTest { @Test public void canary() throws StartException { var handler = new MockHandler(); - ProxyWasm.Builder builder = - ProxyWasm.builder().withPluginHandler(handler).withPluginConfig(new byte[] {2}); + handler.setPluginConfig(new byte[] {2}); + ProxyWasm.Builder builder = ProxyWasm.builder().withPluginHandler(handler); try (var host = builder.build(module)) { try (var context = host.createHttpContext(handler)) { @@ -47,9 +47,9 @@ public void canary() throws StartException { @Test public void nonCanary() throws StartException { var handler = new MockHandler(); + handler.setPluginConfig(new byte[] {1}); var module = Parser.parse(Path.of("./src/test/go-examples/http_routing/main.wasm")); - ProxyWasm.Builder builder = - ProxyWasm.builder().withPluginHandler(handler).withPluginConfig(new byte[] {1}); + ProxyWasm.Builder builder = ProxyWasm.builder().withPluginHandler(handler); try (var host = builder.build(module)) { try (var context = host.createHttpContext(handler)) { diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/JsonValidationTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/JsonValidationTest.java index 9d13575..1fa401e 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/JsonValidationTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/JsonValidationTest.java @@ -147,7 +147,8 @@ class OnHttpRequestBody { @BeforeEach void setUp() throws StartException { var config = "{\"requiredKeys\": [\"my_key\"]}"; - this.host = ProxyWasm.builder().withPluginConfig(config).build(module); + handler.setPluginConfig(config); + this.host = ProxyWasm.builder().withPluginHandler(handler).build(module); this.context = host.createHttpContext(handler); } diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MockHandler.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MockHandler.java index bb6b2fe..e27cb10 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MockHandler.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/MockHandler.java @@ -16,6 +16,7 @@ import io.roastedroot.proxywasm.StreamType; import io.roastedroot.proxywasm.WasmException; import io.roastedroot.proxywasm.WasmResult; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -67,6 +68,8 @@ public HttpResponse( private byte[] downStreamData = new byte[0]; private byte[] upstreamData = new byte[0]; private byte[] grpcReceiveBuffer = new byte[0]; + private byte[] vmConfig; + private byte[] pluginConfig; static final boolean DEBUG = "true".equals(System.getenv("DEBUG")); @@ -119,6 +122,32 @@ public void assertLogsDoNotContain(String... message) { } } + @Override + public byte[] getVmConfig() { + return vmConfig; + } + + public void setVmConfig(byte[] vmConfig) { + this.vmConfig = vmConfig; + } + + public void setVmConfig(String vmConfig) { + this.vmConfig = vmConfig.getBytes(StandardCharsets.UTF_8); + } + + @Override + public byte[] getPluginConfig() { + return pluginConfig; + } + + public void setPluginConfig(byte[] pluginConfig) { + this.pluginConfig = pluginConfig; + } + + public void setPluginConfig(String pluginConfig) { + this.pluginConfig = pluginConfig.getBytes(StandardCharsets.UTF_8); + } + @Override public WasmResult setTickPeriodMilliseconds(int tickPeriodMilliseconds) { this.tickPeriodMilliseconds = tickPeriodMilliseconds; diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/SharedQueueTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/SharedQueueTest.java index 4fb8b73..80caac6 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/SharedQueueTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/SharedQueueTest.java @@ -57,12 +57,12 @@ public void testOnPluginStart() throws StartException, WasmException { // Create and configure the http_request_headers receiver instance var receiverHandler1 = new MockHandler(sharedData); + receiverHandler1.setPluginConfig("http_request_headers"); receiverHandler1.setProperty(List.of("vm_id"), bytes(receiverVmId)); var receiverHost1 = deferClose( ProxyWasm.builder() .withPluginHandler(receiverHandler1) - .withPluginConfig("http_request_headers") .build(receiverModule)); var requestHeadersQueueId = @@ -76,12 +76,12 @@ public void testOnPluginStart() throws StartException, WasmException { // Create and configure the http_response_headers receiver instance var receiverHandler2 = new MockHandler(sharedData); + receiverHandler2.setPluginConfig("http_response_headers"); receiverHandler2.setProperty(List.of("vm_id"), bytes(receiverVmId)); var receiverHost2 = deferClose( ProxyWasm.builder() .withPluginHandler(receiverHandler2) - .withPluginConfig("http_response_headers") .build(receiverModule)); var responseHeadersQueueId = @@ -97,14 +97,12 @@ public void testOnPluginStart() throws StartException, WasmException { // Create and configure the sender instance var senderHandler = new MockHandler(sharedData); + senderHandler.setPluginConfig("http"); var senderVmId = "sender"; senderHandler.setProperty(List.of("vm_id"), bytes(senderVmId)); var senderHost = deferClose( - ProxyWasm.builder() - .withPluginHandler(senderHandler) - .withPluginConfig("http") - .build(senderModule)); + ProxyWasm.builder().withPluginHandler(senderHandler).build(senderModule)); senderHandler.assertLogsContain( String.format("contextID=%d is configured for %s", senderHost.contextId(), "http")); diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/VmPluginConfigurationTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/VmPluginConfigurationTest.java index 5ea95d3..c43bc62 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/VmPluginConfigurationTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/VmPluginConfigurationTest.java @@ -22,12 +22,11 @@ public class VmPluginConfigurationTest { @Test public void test() throws StartException { var handler = new MockHandler(); + handler.setPluginConfig("plugin_config"); + handler.setVmConfig("vm_config"); + handler.setProperty(List.of("plugin_name"), bytes("vm_plugin_configuration")); - ProxyWasm.Builder builder = - ProxyWasm.builder() - .withPluginConfig("plugin_config") - .withVmConfig("vm_config") - .withPluginHandler(handler); + ProxyWasm.Builder builder = ProxyWasm.builder().withPluginHandler(handler); try (var ignored = builder.build(module)) { assertEquals( diff --git a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/PluginHandler.java b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/PluginHandler.java index 0e76cb7..bdd4976 100644 --- a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/PluginHandler.java +++ b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/PluginHandler.java @@ -48,6 +48,24 @@ public void close() { } } + // ////////////////////////////////////////////////////////////////////// + // Plugin config + // ////////////////////////////////////////////////////////////////////// + + byte[] vmConfig; + + @Override + public byte[] getVmConfig() { + return vmConfig; + } + + byte[] pluginConfig; + + @Override + public byte[] getPluginConfig() { + return pluginConfig; + } + // ////////////////////////////////////////////////////////////////////// // Properties // ////////////////////////////////////////////////////////////////////// diff --git a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPlugin.java b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPlugin.java index cad5ed8..11fe92c 100644 --- a/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPlugin.java +++ b/proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPlugin.java @@ -1,5 +1,7 @@ package io.roastedroot.proxywasm.jaxrs; +import static io.roastedroot.proxywasm.Helpers.bytes; + import com.dylibso.chicory.runtime.ImportMemory; import com.dylibso.chicory.runtime.Instance; import com.dylibso.chicory.wasm.WasmModule; @@ -101,22 +103,22 @@ public WasmPlugin.Builder withShared(boolean shared) { } public WasmPlugin.Builder withVmConfig(byte[] vmConfig) { - proxyWasmBuilder = proxyWasmBuilder.withVmConfig(vmConfig); + this.handler.vmConfig = vmConfig; return this; } public WasmPlugin.Builder withVmConfig(String vmConfig) { - proxyWasmBuilder = proxyWasmBuilder.withVmConfig(vmConfig); + this.handler.vmConfig = bytes(vmConfig); return this; } public WasmPlugin.Builder withPluginConfig(byte[] pluginConfig) { - proxyWasmBuilder = proxyWasmBuilder.withPluginConfig(pluginConfig); + this.handler.pluginConfig = pluginConfig; return this; } public WasmPlugin.Builder withPluginConfig(String pluginConfig) { - proxyWasmBuilder = proxyWasmBuilder.withPluginConfig(pluginConfig); + this.handler.pluginConfig = bytes(pluginConfig); return this; }