Skip to content

Commit 917485d

Browse files
nanjingboyzhenjunMaXunzhuoseeflood
authored
feat: dynamic load wasm file(#191) (#577)
* feat(wasm): dynamic load & update & unload wasm * docs(wasm): update docs for wasm * chore: code style * chore: code style * chore: import code style * feat(wasm): release resources after used instance released * refactor: refactor logic for dynamic wasm load * fix: fix import error * fix: fix request body error * chore: code style * fix: fix lint error * fix unit test * feat(wasm): add some tests * chore: code style * feat(wasm): fix actuator test and add wasm test * feat(wasm): update wasm factory test * feat(wasm): add wasm factory install test * feat(wasm): update wasm factory test * feat(wasm): improve code logic * chore: code style * wasm init build tag * update import * update import * update import Co-authored-by: Marco <[email protected]> Co-authored-by: Xunzhuo <[email protected]> Co-authored-by: seeflood <[email protected]>
1 parent 9e83f05 commit 917485d

39 files changed

+1085
-171
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,16 @@ You can try the quickstart demos below to get started with Layotto. In addition,
9696
| feature | status | quick start | desc |
9797
| -------------- | :----: | :---------------------------------------------------: | -------------------------------------------------------------------- |
9898
| Go (TinyGo) || [demo](https://mosn.io/layotto/#/en/start/wasm/start) | Compile Code written by TinyGo to \*.wasm and run in Layotto |
99-
| Rust | TODO | TODO | Compile Code written by Rust to \*.wasm and run in Layotto |
100-
| AssemblyScript | TODO | TODO | Compile Code written by AssemblyScript to \*.wasm and run in Layotto |
99+
| Rust | | [demo](https://mosn.io/layotto/#/en/start/wasm/start) | Compile Code written by Rust to \*.wasm and run in Layotto |
100+
| AssemblyScript | | [demo](https://mosn.io/layotto/#/en/start/wasm/start) | Compile Code written by AssemblyScript to \*.wasm and run in Layotto |
101101

102102
### As a FaaS(Serverless) runtime (Layotto + WebAssembly + k8s)
103103

104104
| feature | status | quick start | desc |
105105
| -------------- | :----: | :---------------------------------------------------: | ------------------------------------------------------------------------------------------ |
106106
| Go (TinyGo) || [demo](https://mosn.io/layotto/#/en/start/faas/start) | Compile Code written by TinyGo to \*.wasm and run in Layotto And Scheduled by k8s. |
107-
| Rust | TODO | TODO | Compile Code written by Rust to \*.wasm and run in Layotto And Scheduled by k8s. |
108-
| AssemblyScript | TODO | TODO | Compile Code written by AssemblyScript to \*.wasm and run in Layotto And Scheduled by k8s. |
107+
| Rust | | [demo](https://mosn.io/layotto/#/en/start/faas/start) | Compile Code written by Rust to \*.wasm and run in Layotto And Scheduled by k8s. |
108+
| AssemblyScript | | [demo](https://mosn.io/layotto/#/en/start/faas/start) | Compile Code written by AssemblyScript to \*.wasm and run in Layotto And Scheduled by k8s. |
109109

110110
## Presentations
111111

cmd/layotto/main.go

+4
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,13 @@ import (
151151
_ "mosn.io/pkg/buffer"
152152

153153
_ "mosn.io/layotto/pkg/filter/network/tcpcopy"
154+
_ "mosn.io/layotto/pkg/filter/stream/wasm/http"
154155
l8_grpc "mosn.io/layotto/pkg/grpc"
155156
"mosn.io/layotto/pkg/runtime"
156157
_ "mosn.io/layotto/pkg/wasm"
158+
_ "mosn.io/layotto/pkg/wasm/install"
159+
_ "mosn.io/layotto/pkg/wasm/uninstall"
160+
_ "mosn.io/layotto/pkg/wasm/update"
157161

158162
_ "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/router/v3"
159163
_ "mosn.io/mosn/istio/istio1106"

cmd/layotto_multiple_api/main.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,20 @@ import (
3838
secretstore_env "github.com/dapr/components-contrib/secretstores/local/env"
3939
secretstore_file "github.com/dapr/components-contrib/secretstores/local/file"
4040

41+
_ "mosn.io/layotto/pkg/filter/stream/wasm/http"
4142
"mosn.io/layotto/pkg/grpc/default_api"
43+
mock_state "mosn.io/layotto/pkg/mock/components/state"
4244
secretstores_loader "mosn.io/layotto/pkg/runtime/secretstores"
45+
_ "mosn.io/layotto/pkg/wasm"
46+
_ "mosn.io/layotto/pkg/wasm/install"
47+
_ "mosn.io/layotto/pkg/wasm/uninstall"
48+
_ "mosn.io/layotto/pkg/wasm/update"
4349

4450
"mosn.io/layotto/components/file/local"
4551
"mosn.io/layotto/components/file/s3/alicloud"
4652
"mosn.io/layotto/components/file/s3/aws"
4753
"mosn.io/layotto/components/file/s3/minio"
4854

49-
mock_state "mosn.io/layotto/pkg/mock/components/state"
50-
5155
dbindings "github.com/dapr/components-contrib/bindings"
5256
"github.com/dapr/components-contrib/bindings/http"
5357
"mosn.io/pkg/log"
@@ -154,11 +158,6 @@ import (
154158
_ "mosn.io/mosn/pkg/wasm/runtime/wasmer"
155159
_ "mosn.io/pkg/buffer"
156160

157-
_ "mosn.io/layotto/pkg/filter/network/tcpcopy"
158-
l8_grpc "mosn.io/layotto/pkg/grpc"
159-
"mosn.io/layotto/pkg/runtime"
160-
_ "mosn.io/layotto/pkg/wasm"
161-
162161
_ "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/router/v3"
163162
_ "mosn.io/mosn/istio/istio1106"
164163
_ "mosn.io/mosn/istio/istio1106/filter/stream/jwtauthn"
@@ -198,6 +197,10 @@ import (
198197
_ "mosn.io/mosn/pkg/upstream/healthcheck"
199198
_ "mosn.io/mosn/pkg/upstream/servicediscovery/dubbod"
200199

200+
_ "mosn.io/layotto/pkg/filter/network/tcpcopy"
201+
l8_grpc "mosn.io/layotto/pkg/grpc"
202+
"mosn.io/layotto/pkg/runtime"
203+
201204
helloworld_api "mosn.io/layotto/cmd/layotto_multiple_api/helloworld"
202205
_ "mosn.io/layotto/diagnostics/exporter_iml"
203206
)

demo/faas/config.json

+27
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,36 @@
8181
}
8282
}
8383
]
84+
},
85+
{
86+
"name": "wasm_filter",
87+
"address": "0.0.0.0:34998",
88+
"bind_port": true,
89+
"filter_chains": [
90+
{
91+
"filters": [
92+
{
93+
"type": "proxy",
94+
"config": {
95+
"downstream_protocol": "Http1",
96+
"upstream_protocol": "Http1",
97+
"router_config_name": "wasm_router"
98+
}
99+
}
100+
]
101+
}
102+
],
103+
"stream_filters": [
104+
{
105+
"type": "wasm_filter"
106+
}
107+
]
84108
}
85109
],
86110
"routers": [
111+
{
112+
"router_config_name": "wasm_router"
113+
},
87114
{
88115
"router_config_name":"function_router",
89116
"virtual_hosts":[{

docs/en/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,16 @@ You can try the quickstart demos below to get started with Layotto. In addition,
9494
| feature | status | quick start | desc |
9595
| -------------- | :----: | :---------------------------------------------------: | -------------------------------------------------------------------- |
9696
| Go (TinyGo) || [demo](https://mosn.io/layotto/#/en/start/wasm/start) | Compile Code written by TinyGo to \*.wasm and run in Layotto |
97-
| Rust | TODO | TODO | Compile Code written by Rust to \*.wasm and run in Layotto |
98-
| AssemblyScript | TODO | TODO | Compile Code written by AssemblyScript to \*.wasm and run in Layotto |
97+
| Rust | | [demo](https://mosn.io/layotto/#/en/start/wasm/start) | Compile Code written by Rust to \*.wasm and run in Layotto |
98+
| AssemblyScript | | [demo](https://mosn.io/layotto/#/en/start/wasm/start) | Compile Code written by AssemblyScript to \*.wasm and run in Layotto |
9999

100100
### As a FaaS(Serverless) runtime (Layotto + WebAssembly + k8s)
101101

102102
| feature | status | quick start | desc |
103103
| -------------- | :----: | :---------------------------------------------------: | ------------------------------------------------------------------------------------------ |
104104
| Go (TinyGo) || [demo](https://mosn.io/layotto/#/en/start/faas/start) | Compile Code written by TinyGo to \*.wasm and run in Layotto And Scheduled by k8s. |
105-
| Rust | TODO | TODO | Compile Code written by Rust to \*.wasm and run in Layotto And Scheduled by k8s. |
106-
| AssemblyScript | TODO | TODO | Compile Code written by AssemblyScript to \*.wasm and run in Layotto And Scheduled by k8s. |
105+
| Rust | | [demo](https://mosn.io/layotto/#/en/start/faas/start) | Compile Code written by Rust to \*.wasm and run in Layotto And Scheduled by k8s. |
106+
| AssemblyScript | | [demo](https://mosn.io/layotto/#/en/start/faas/start) | Compile Code written by AssemblyScript to \*.wasm and run in Layotto And Scheduled by k8s. |
107107

108108
## Presentations
109109

docs/en/start/faas/start.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ The example only needs a Redis server that can be used normally. As for where it
5959

6060
**Note2: Need to modify the path of the wasm file in `./demo/faas/config.json` to `/home/docker/function_1.wasm` and `/home/docker/function_2.wasm`**
6161

62+
**Note3: We can also load WASM file dynamically. For details, see [WASM Dynamic Load](https://mosn.io/layotto/#/en/start/wasm/start?id=dynamic-load)**
63+
6264
#### D、Compile & install containerd-shim-layotto-v2
6365

6466
```
@@ -101,7 +103,7 @@ sudo systemctl restart containerd
101103
#### A、Start Layotto
102104

103105
```
104-
> minikube ssh
106+
> minikube ssh
105107
> layotto start -c /home/docker/config.json
106108
```
107109

@@ -165,7 +167,7 @@ There are 100 inventories for book1.
165167
The FaaS model is currently in the POC stage, and the features are not complete. It will be improved in the following aspects in the future:
166168
1. Limit the maximum resources that can be used when the function is running, such as cpu, heap, stack, etc.
167169
2. The functions loaded and run by different Layotto can call each other.
168-
3. Fully integrate into the k8s ecology, such as reporting the use of resources to k8s, so that k8s can perform better scheduling.
170+
3. Fully integrate into the k8s ecology, such as reporting the use of resources to k8s, so that k8s can perform better scheduling.
169171
4. Add more Runtime ABI.
170172

171173
If you are interested in FaaS or have any questions or ideas, please leave a message in the issue area, we can build FaaS together!

docs/en/start/wasm/start.md

+47-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ And if it becomes like this:
1111

1212
![img.png](../../../img/wasm/img.png)
1313

14-
If developers no longer develop sdk (jar package), change to develop .wasm files and support independent upgrade and deployment, there will be no pain to push the users to upgrade.
14+
If developers no longer develop sdk (jar package), change to develop .wasm files and support independent upgrade and deployment, there will be no pain to push the users to upgrade.
1515

1616
When you want to upgrade, you can release it on the operation platform. There is no need to restart the app and sidecar.
1717

@@ -21,7 +21,7 @@ Layotto can load the compiled WASM files automatically, and interacts with them
2121

2222
#### step 1. start redis server and write test data
2323

24-
The example only needs a Redis server that can be used normally. As for where it is installed, there is no special restriction. It can be a virtual machine, a local machine or a server.
24+
The example only needs a Redis server that can be used normally. As for where it is installed, there is no special restriction. It can be a virtual machine, a local machine or a server.
2525

2626
Here, we run redis with docker:
2727

@@ -93,6 +93,51 @@ This http request will access the wasm module in Layotto. The wasm module will c
9393
docker rm -f redis-test
9494
```
9595

96+
### Dynamic Load
97+
98+
We can specify the WASM file to be loaded in `./demo/faas/config.json` config file:
99+
100+
```json
101+
"config": {
102+
"function1": {
103+
"name": "function1",
104+
"instance_num": 1,
105+
"vm_config": {
106+
"engine": "wasmer",
107+
"path": "demo/faas/code/golang/client/function_1.wasm"
108+
}
109+
},
110+
"function2": {
111+
"name": "function2",
112+
"instance_num": 1,
113+
"vm_config": {
114+
"engine": "wasmer",
115+
"path": "demo/faas/code/golang/server/function_2.wasm"
116+
}
117+
}
118+
}
119+
```
120+
121+
We can also install, update, and uninstall WASM file dynamically through the following Apis.
122+
123+
#### Install
124+
125+
```shell
126+
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"id_1","instance_num":2,"vm_config":{"engine":"wasmer","path":"demo/faas/code/golang/client/function_1.wasm"}}' http://127.0.0.1:34998/wasm/install
127+
```
128+
129+
#### Update Instance Number
130+
131+
```shell
132+
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"id_1","instance_num":2}' http://127.0.0.1:34998/wasm/update
133+
```
134+
135+
#### Uninstall
136+
137+
```shell
138+
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"id_1"}' http://127.0.0.1:34998/wasm/uninstall
139+
```
140+
96141
### Note
97142

98143
This feature is still in the experimental stage, and the implementation of the WASM interactive API in the community is not uniform enough, so if you have any needs for this module, please post it in the issue area, we will build WASM together!

docs/zh/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,17 @@ Layotto 提供了多种语言版本的 SDK,SDK 通过 gRPC 与 Layotto 进行
107107

108108
| feature | status | quick start | desc |
109109
| -------------- | :----: | :---------------------------------------------------: | ---------------------------------------------------------------- |
110-
| Go (TinyGo) | | [demo](https://mosn.io/layotto/#/zh/start/wasm/start) | 把用 TinyGo 开发的代码编译成 \*.wasm 文件跑在 Layotto 上 |
111-
| Rust | 待开发 | 待开发 | 把用 Rust 开发的代码编译成 \*.wasm 文件跑在 Layotto 上 |
112-
| AssemblyScript | 待开发 | 待开发 | 把用 AssemblyScript 开发的代码编译成 \*.wasm 文件跑在 Layotto 上 |
110+
| Go (TinyGo) || [demo](https://mosn.io/layotto/#/zh/start/wasm/start) | 把用 TinyGo 开发的代码编译成 \*.wasm 文件跑在 Layotto 上 |
111+
| Rust | | [demo](https://mosn.io/layotto/#/zh/start/wasm/start) | 把用 Rust 开发的代码编译成 \*.wasm 文件跑在 Layotto 上 |
112+
| AssemblyScript | | [demo](https://mosn.io/layotto/#/zh/start/wasm/start) | 把用 AssemblyScript 开发的代码编译成 \*.wasm 文件跑在 Layotto 上 |
113113

114114
### 作为 Serverless 的运行时,通过 WebAssembly (WASM) 写 FaaS
115115

116116
| feature | status | quick start | desc |
117117
| -------------- | :----: | :---------------------------------------------------: | ----------------------------------------------------------------------------------------- |
118-
| Go (TinyGo) | | [demo](https://mosn.io/layotto/#/zh/start/faas/start) | 把用 TinyGo 开发的代码编译成 \*.wasm 文件跑在 Layotto 上,并且使用 k8s 进行调度。 |
119-
| Rust | 待开发 | 待开发 | 把用 Rust 开发的代码编译成 \*.wasm 文件跑在 Layotto 上,并且使用 k8s 进行调度。 |
120-
| AssemblyScript | 待开发 | 待开发 | 把用 AssemblyScript 开发的代码编译成 \*.wasm 文件跑在 Layotto 上,并且使用 k8s 进行调度。 |
118+
| Go (TinyGo) || [demo](https://mosn.io/layotto/#/zh/start/faas/start) | 把用 TinyGo 开发的代码编译成 \*.wasm 文件跑在 Layotto 上,并且使用 k8s 进行调度。 |
119+
| Rust | | [demo](https://mosn.io/layotto/#/zh/start/faas/start) | 把用 Rust 开发的代码编译成 \*.wasm 文件跑在 Layotto 上,并且使用 k8s 进行调度。 |
120+
| AssemblyScript | | [demo](https://mosn.io/layotto/#/zh/start/faas/start) | 把用 AssemblyScript 开发的代码编译成 \*.wasm 文件跑在 Layotto 上,并且使用 k8s 进行调度。 |
121121

122122
## Landscapes
123123

docs/zh/start/faas/start.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Layotto支持加载并运行以 wasm 为载体的 Function,并支持Function
2626
### 三、环境搭建
2727

2828
#### A、安装&运行 Redis
29-
29+
3030
这里只是需要一个可以正常使用 Redis 即可,至于 Redis 安装在哪里没有特别限制,可以是虚拟机里,也可以是本机或者服务器,这里以安装在 mac 为例进行介绍。
3131

3232
```
@@ -59,6 +59,8 @@ Layotto支持加载并运行以 wasm 为载体的 Function,并支持Function
5959

6060
**注2:需要把`./demo/faas/config.json`中的 wasm 文件的路径修改为`/home/docker/function_1.wasm``/home/docker/function_2.wasm`, 两个wasm文件在后面会被自动注入。**
6161

62+
**注3:也可动态加载 WASM 文件,详情参见:[WASM 动态注册](https://mosn.io/layotto/#/zh/start/wasm/start?id=动态注册)**
63+
6264
#### D、安装 containerd-shim-layotto-v2
6365

6466
```
@@ -101,7 +103,7 @@ sudo systemctl restart containerd
101103
#### A、启动 Layotto
102104

103105
```
104-
> minikube ssh
106+
> minikube ssh
105107
> layotto start -c /home/docker/config.json
106108
```
107109

@@ -159,15 +161,15 @@ There are 100 inventories for book1.
159161
2.启动Layotto时,redis连接失败,打印 "occurs an error: redis store: error connecting to redis at":
160162

161163
检查redis的配置,看是否redis配置错误造成的。
162-
164+
163165

164166

165167
### 说明
166168

167169
目前整套 FaaS 模型处于 POC 阶段,功能还不够完善,后续会在以下几个方向上进一步探索完善:
168170
1. 对函数运行时可使用的最大资源进行限制,如cpu,heap,stack等。
169171
2. 由不同 Layotto 加载运行的函数之间可以互相调用。
170-
3. 充分融入k8s生态,比如上报使用资源给k8s,让k8s进行更好的调度。
172+
3. 充分融入k8s生态,比如上报使用资源给k8s,让k8s进行更好的调度。
171173
4. 增加更多的 Runtime ABI。
172174

173175
如果你对 FaaS 感兴趣或者有任何疑问或者想法,欢迎在 issue 区留言,我们一起建设 FaaS !

docs/zh/start/wasm/start.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Layotto支持加载编译好的WASM文件,并通过`proxy_abi_version_0_2_0`
2828
docker run -d --name redis-test -p 6379:6379 redis
2929
```
3030

31-
调用 Redis 容器中的 redis-cli,执行`set book1 100`
31+
调用 Redis 容器中的 redis-cli,执行`set book1 100`
3232

3333
```shell
3434
docker exec -i redis-test redis-cli set book1 100
@@ -90,6 +90,51 @@ There are 100 inventories for book1.
9090
docker rm -f redis-test
9191
```
9292

93+
### 动态注册
94+
95+
除了在 `./demo/faas/config.json` 中指定要加载的 WASM 文件外(比如以下配置):
96+
97+
```json
98+
"config": {
99+
"function1": {
100+
"name": "function1",
101+
"instance_num": 1,
102+
"vm_config": {
103+
"engine": "wasmer",
104+
"path": "demo/faas/code/golang/client/function_1.wasm"
105+
}
106+
},
107+
"function2": {
108+
"name": "function2",
109+
"instance_num": 1,
110+
"vm_config": {
111+
"engine": "wasmer",
112+
"path": "demo/faas/code/golang/server/function_2.wasm"
113+
}
114+
}
115+
}
116+
```
117+
118+
我们也可通过以下接口来动态的加载、更新、卸载 WASM 文件。
119+
120+
#### 加载
121+
122+
```shell
123+
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"id_1","instance_num":2,"vm_config":{"engine":"wasmer","path":"demo/faas/code/golang/client/function_1.wasm"}}' http://127.0.0.1:34998/wasm/install
124+
```
125+
126+
#### 更新实例数
127+
128+
```shell
129+
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"id_1","instance_num":2}' http://127.0.0.1:34998/wasm/update
130+
```
131+
132+
#### 卸载
133+
134+
```shell
135+
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"id_1"}' http://127.0.0.1:34998/wasm/uninstall
136+
```
137+
93138
### 说明
94139

95140
该功能目前仍处于试验阶段,社区里对于WASM跟宿主的交互API也不够统一,因此如果您有该模块的需求欢迎发表在issue区,我们一起建设WASM!

0 commit comments

Comments
 (0)