Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: dynamic load wasm file #191

Closed
zhenjunMa opened this issue Aug 23, 2021 · 2 comments · Fixed by #577
Closed

New Feature: dynamic load wasm file #191

zhenjunMa opened this issue Aug 23, 2021 · 2 comments · Fixed by #577
Assignees
Labels
help wanted Extra attention is needed kind/enhancement New feature or request wasm WebAssembly technology

Comments

@zhenjunMa
Copy link
Contributor

1. What would you like to be added

Currently Layotto only supports static loading *.wasm files, that means which *.wasm files to be loaded must be written in the configuration file at the beginning. Take the demo/wasm/config.json configuration file as an example , The following configuration means that Layotto needs to load wasm_1_1.wasm, wasm_1_2.wasm, wasm_2.wasm.

"config": {
  "id_1_1": {
    "name": "id_1",
    "instance_num": 1,
    "vm_config": {
      "engine": "wasmer",
      "path": "demo/wasm/code/golang/wasm_1_1.wasm"
    }
  },
  "id_1_2": {
    "name": "id_1",
    "instance_num": 2,
    "vm_config": {
      "engine": "wasmer",
      "path": "demo/wasm/code/golang/wasm_1_2.wasm"
    }
  },
  "id_2": {
    "name": "id_2",
    "instance_num": 1,
    "vm_config": {
      "engine": "wasmer",
      "path": "demo/wasm/code/golang/wasm_2.wasm"
    }
  }
}

2. Why is this needed

This is the problem that Layotto faces in the FaaS scenario. When Layotto is started as the base for Function, it doesn't know which *.wasm files to load, and it needs to wait for schedule center to call them to load *.wasm file.

image

3. Design draft

A. interface definition

Layotto needs to provide an HTTP interface. Considering the unified management of the Listener, we do not recommend calling the native listener API in the code, but handing it over to MOSN management. This can also reduce the amount of development, specific configuration and Code development can refer to the actuator module implemented before.

actuator user doc
actuator HTTP configuration
actuator HTTP biz logic

The expected method of calling the loading interface is as follows:

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"id_2","instance_num":1,"vm_config":{"engine":"wasmer","path":"demo/wasm/code/golang/wasm_2.wasm"}}
'  http://localhost: 34998/wasm/install

The expected way to call the uninstall interface is as follows:

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"id_2"}
'  http://localhost: 34998/wasm/uninstall

Expansion point: Here you can also consider not uninstalling all of them, but just reducing the number of instances, similar to the expansion and contraction of Function.

B、Loading file

Here you can ignore the scenario of letting Layotto download the wasm file in the server, just assume that the target file has been saved in a certain path of the target node, we just need let Layotto load and run this file according to the configuration.

中文

一、需要实现什么功能

目前Layotto只支持静态加载*.wasm文件,也就是说要加载哪些*.wasm文件,必须一开始就写在配置文件中,以demo/wasm/config.json配置文件为例,如下配置表示Layotto需要加载wasm_1_1.wasm,wasm_1_2.wasm,wasm_2.wasm三个*.wasm文件,并指明了它们对应的配置。

"config": {
  "id_1_1": {
    "name": "id_1",
    "instance_num": 1,
    "vm_config": {
      "engine": "wasmer",
      "path": "demo/wasm/code/golang/wasm_1_1.wasm"
    }
  },
  "id_1_2": {
    "name": "id_1",
    "instance_num": 2,
    "vm_config": {
      "engine": "wasmer",
      "path": "demo/wasm/code/golang/wasm_1_2.wasm"
    }
  },
  "id_2": {
    "name": "id_2",
    "instance_num": 1,
    "vm_config": {
      "engine": "wasmer",
      "path": "demo/wasm/code/golang/wasm_2.wasm"
    }
  }
}

现在,我们需要让Layotto能支持通过接口调用的方式动态加载*.wasm文件,并按照对应的配置信息来运行。

二、做这个功能的价值

这是Layotto在FaaS场景中面临的问题,当Layotto作为FaaS运行的基座启动以后,它并不能预先知道要加载哪些*.wasm文件,需要依赖调度中心根据待发布的Function所需要的资源计算好目标节点以后,再调用节点上面运行的Layotto接口来让它动态加载并运行目标函数。

image

三、方案概述

A、接口定义

Layotto需要对外提供HTTP接口,考虑到对Listener的统一管理,我们不推荐在代码中调用原生的监听端口函数,而是要交给MOSN管理,这种方式同样也可以减少开发量,具体的配置及代码开发可以参考之前实现的actuator模块。
actuator使用文档
actuator HTTP服务的配置方式
actuator HTTP服务的处理逻辑

预期的加载接口调用方式如下:

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"id_2","instance_num":1,"vm_config":{"engine":"wasmer","path":"demo/wasm/code/golang/wasm_2.wasm"}}
'  http://localhost: 34998/wasm/install

预期的卸载接口调用方式如下:

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"id_2"}
'  http://localhost: 34998/wasm/uninstall

扩展点:这里也可以考虑不全部卸载掉,而只是减少实例数,类似于Function的扩缩容。

B、文件加载

这里可以先不考虑让Layotto去远端下载wasm文件的场景,只需假定目标文件已经提前保存在目标节点的某个路径下面,按照自定义配置加载运行即可。

@nanjingboy
Copy link
Contributor

@seeflood it looks very interesing, please assign it to me ^_^

@seeflood
Copy link
Member

@nanjingboy Cool !
Assigned to u

@zhenjunMa zhenjunMa added the wasm WebAssembly technology label May 26, 2022
@seeflood seeflood linked a pull request Jun 4, 2022 that will close this issue
seeflood added a commit that referenced this issue Jul 16, 2022
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed kind/enhancement New feature or request wasm WebAssembly technology
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants