forked from apache/shenyu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUE apache#4612] init shenyu-plugin-wasm-api module
- Loading branch information
1 parent
a026635
commit 254ea20
Showing
17 changed files
with
351 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Licensed to the Apache Software Foundation (ASF) under one or more | ||
~ contributor license agreements. See the NOTICE file distributed with | ||
~ this work for additional information regarding copyright ownership. | ||
~ The ASF licenses this file to You under the Apache License, Version 2.0 | ||
~ (the "License"); you may not use this file except in compliance with | ||
~ the License. You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>org.apache.shenyu</groupId> | ||
<artifactId>shenyu-plugin</artifactId> | ||
<version>2.6.2-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>shenyu-plugin-wasm-api</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.shenyu</groupId> | ||
<artifactId>shenyu-plugin-base</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.github.kawamuray.wasmtime</groupId> | ||
<artifactId>wasmtime-java</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.module</groupId> | ||
<artifactId>jackson-module-kotlin</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# How to build the wasm file | ||
|
||
1. install rustup | ||
|
||
2. install rust | ||
|
||
3. generate the wasm file | ||
|
||
```shell | ||
cd {shenyu}/shenyu-plugin/shenyu-plugin-wasm-api/src/test/rust-wasm-filter | ||
cargo build --target wasm32-wasi --release | ||
``` | ||
|
||
then you will see the wasm file | ||
in `{shenyu}/shenyu-plugin/shenyu-plugin-wasm-api/src/test/rust-wasm-filter/target/wasm32-wasi/release/rust_filter.wasm` | ||
|
||
4. rename the wasm file | ||
|
||
rename the file to `org.apache.shenyu.plugin.wasm.api.AbstractWasmPluginTest$RustWasmPlugin.wasm` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...rc/main/java/org/apache/shenyu/plugin/wasm/base/handler/AbstractWasmDiscoveryHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.shenyu.plugin.wasm.base.handler; | ||
|
||
import io.github.kawamuray.wasmtime.WasmFunctions; | ||
import io.github.kawamuray.wasmtime.WasmValType; | ||
import org.apache.shenyu.common.dto.DiscoverySyncData; | ||
import org.apache.shenyu.plugin.base.handler.DiscoveryUpstreamDataHandler; | ||
import org.apache.shenyu.plugin.wasm.api.exception.ShenyuWasmInitException; | ||
import org.apache.shenyu.plugin.wasm.api.loader.WasmLoader; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* upstreamList data change. | ||
*/ | ||
public abstract class AbstractWasmDiscoveryHandler extends WasmLoader implements DiscoveryUpstreamDataHandler { | ||
|
||
protected static final Map<Long, DiscoverySyncData> ARGUMENTS = new ConcurrentHashMap<>(); | ||
|
||
protected static final String METHOD_NAME = "handlerDiscoveryUpstreamData"; | ||
|
||
@Override | ||
public void handlerDiscoveryUpstreamData(final DiscoverySyncData discoverySyncData) { | ||
super.getWasmExtern(METHOD_NAME) | ||
.map(handlerDiscoveryUpstreamData -> { | ||
// WASI cannot easily pass Java objects like JNI, here we pass Long as arg | ||
// then we can get the argument by Long | ||
final Long argumentId = getArgumentId(discoverySyncData); | ||
ARGUMENTS.put(argumentId, discoverySyncData); | ||
// call WASI function | ||
WasmFunctions.consumer(super.getStore(), handlerDiscoveryUpstreamData.func(), WasmValType.I64) | ||
.accept(argumentId); | ||
ARGUMENTS.remove(argumentId); | ||
return argumentId; | ||
}).orElseThrow(() -> new ShenyuWasmInitException(METHOD_NAME + " function not find in wasm file: " + getWasmName())); | ||
} | ||
|
||
protected abstract Long getArgumentId(DiscoverySyncData discoverySyncData); | ||
} |
77 changes: 77 additions & 0 deletions
77
...src/main/java/org/apache/shenyu/plugin/wasm/base/handler/AbstractWasmMetaDataHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.shenyu.plugin.wasm.base.handler; | ||
|
||
import io.github.kawamuray.wasmtime.Extern; | ||
import io.github.kawamuray.wasmtime.WasmFunctions; | ||
import io.github.kawamuray.wasmtime.WasmValType; | ||
import org.apache.shenyu.common.dto.MetaData; | ||
import org.apache.shenyu.plugin.base.handler.MetaDataHandler; | ||
import org.apache.shenyu.plugin.wasm.api.exception.ShenyuWasmInitException; | ||
import org.apache.shenyu.plugin.wasm.api.loader.WasmLoader; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* The type abstract wasm metadata subscriber. | ||
*/ | ||
public abstract class AbstractWasmMetaDataHandler extends WasmLoader implements MetaDataHandler { | ||
|
||
protected static final Map<Long, MetaData> ARGUMENTS = new ConcurrentHashMap<>(); | ||
|
||
protected static final String HANDLE_METHOD_NAME = "handle"; | ||
|
||
protected static final String REMOVE_METHOD_NAME = "remove"; | ||
|
||
protected static final String REFRESH_METHOD_NAME = "refresh"; | ||
|
||
@Override | ||
public void handle(final MetaData metaData) { | ||
super.getWasmExtern(HANDLE_METHOD_NAME) | ||
.map(handle -> callWASI(metaData, handle)) | ||
.orElseThrow(() -> new ShenyuWasmInitException(HANDLE_METHOD_NAME + " function not find in wasm file: " + getWasmName())); | ||
} | ||
|
||
@Override | ||
public void remove(final MetaData metaData) { | ||
super.getWasmExtern(REMOVE_METHOD_NAME) | ||
.map(remove -> callWASI(metaData, remove)) | ||
.orElseThrow(() -> new ShenyuWasmInitException(REMOVE_METHOD_NAME + " function not find in wasm file: " + getWasmName())); | ||
} | ||
|
||
private Long callWASI(final MetaData metaData, final Extern execute) { | ||
// WASI cannot easily pass Java objects like JNI, here we pass Long as arg | ||
// then we can get the argument by Long | ||
final Long argumentId = getArgumentId(metaData); | ||
ARGUMENTS.put(argumentId, metaData); | ||
// call WASI function | ||
WasmFunctions.consumer(super.getStore(), execute.func(), WasmValType.I64) | ||
.accept(argumentId); | ||
ARGUMENTS.remove(argumentId); | ||
return argumentId; | ||
} | ||
|
||
protected abstract Long getArgumentId(MetaData metaData); | ||
|
||
@Override | ||
public void refresh() { | ||
super.getWasmExtern(REFRESH_METHOD_NAME) | ||
.ifPresent(refresh -> WasmFunctions.consumer(super.getStore(), refresh.func()).accept()); | ||
} | ||
} |
Oops, something went wrong.