forked from infinyon/fluvio
-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: support WasmEdge as an alternative engine #1
Draft
xxchan
wants to merge
33
commits into
master
Choose a base branch
from
xxchan/wasmedge
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
fb5b2fe
init work: make the public API clear
xxchan 7a30006
implement filter transform
xxchan 685ae36
wip
xxchan 15686cc
make the public API clear
xxchan 1937438
move wasmtime_engine to a different mod
xxchan 6f1604e
fix wasi feature
xxchan 6b88555
rename wasmtime-engine -> wasmtime
xxchan cd038a3
Merge branch 'master' into xxchan/clena
xxchan 86326da
reorg
xxchan e959a9d
Merge branch 'xxchan/clena' into xxchan/wasmedge
xxchan f17b8ef
support init for wasmedge/common
xxchan 8b11252
Merge branch 'master' into xxchan/wasmedge
xxchan 11e1751
make SmartModuleInstance common
xxchan abd4fb6
make create_transform common & reorg trait imp to imp
xxchan 66bcbdd
more movement
xxchan 0132522
Merge branch 'master' into xxchan/wasmedge
xxchan da7a2e2
add other tests for wasmedge
xxchan 26d9053
support agg for common/wasmedge
xxchan 56de150
movement
xxchan 14c4c3c
refactor wasmtime to use the common code
xxchan 3d8f4bf
WasmTime -> Wasmtime
xxchan d0bb940
move transform unit tests to common
xxchan 7924834
rename Wasmedge -> WasmEdge
xxchan f2f55e8
minor tweaks
xxchan e6f7e2e
change features
xxchan 06d579f
rm wasmedge
xxchan 6ff943b
Merge branch 'master' into xxchan/wasmedge
xxchan 7e559c1
remove wasmedge
xxchan 7d23e1a
fmt
xxchan f5650e3
fmt with group_imports = "StdExternalCrate"
xxchan fe98b8f
try to make diff smaller
xxchan 136c572
make diff smaller
xxchan 72f4ed7
clippy
xxchan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
make create_transform common & reorg trait imp to imp
- Loading branch information
commit abd4fb61fb6df8e9858059a75569aaeb757dfd7e
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,153 @@ | ||
use tracing::debug; | ||
use wasmedge_sdk::error::HostFuncError; | ||
use wasmedge_sdk::types::Val; | ||
use wasmedge_sdk::{ | ||
Executor, Func, Module, Store, CallingFrame, WasmValue, Caller, ImportObjectBuilder, | ||
}; | ||
|
||
use crate::engine::wasmedge::instance::{RecordsCallBack, RecordsMemory}; | ||
|
||
use crate::engine::common::{WasmFn, WasmInstance}; | ||
use crate::engine::error::EngineError; | ||
|
||
use anyhow::Result; | ||
use fluvio_smartmodule::dataplane::smartmodule::{SmartModuleExtraParams}; | ||
use std::sync::Arc; | ||
|
||
pub(crate) struct WasmedgeInstance { | ||
instance: wasmedge_sdk::Instance, | ||
records_cb: Arc<super::instance::RecordsCallBack>, | ||
params: SmartModuleExtraParams, | ||
version: i16, | ||
} | ||
|
||
pub(crate) struct WasmedgeContext { | ||
pub engine: Executor, | ||
} | ||
|
||
pub type WasmedgeFn = Func; | ||
|
||
impl WasmInstance for WasmedgeInstance { | ||
type Context = WasmedgeContext; | ||
|
||
type Func = WasmedgeFn; | ||
|
||
fn get_fn(&self, name: &str, _ctx: &mut Self::Context) -> Result<Option<Self::Func>> { | ||
match self.instance.func(name) { | ||
// check type signature | ||
Some(func) => Ok(Some(func)), | ||
None => Ok(None), | ||
} | ||
} | ||
|
||
fn write_input<E: fluvio_protocol::Encoder>( | ||
&mut self, | ||
input: &E, | ||
ctx: &mut Self::Context, | ||
) -> Result<(i32, i32, i32)> { | ||
self.records_cb.clear(); | ||
let mut input_data = Vec::new(); | ||
input.encode(&mut input_data, self.version)?; | ||
debug!( | ||
len = input_data.len(), | ||
version = self.version, | ||
"input encoded" | ||
); | ||
let array_ptr = | ||
super::memory::copy_memory_to_instance(&ctx.engine, &self.instance, &input_data)?; | ||
let length = input_data.len(); | ||
Ok((array_ptr, length as i32, self.version as i32)) | ||
} | ||
|
||
fn read_output<D: fluvio_protocol::Decoder + Default>( | ||
&mut self, | ||
_ctx: &mut Self::Context, | ||
) -> Result<D> { | ||
let bytes = self | ||
.records_cb | ||
.get() | ||
.and_then(|m| m.copy_memory_from().ok()) | ||
.unwrap_or_default(); | ||
let mut output = D::default(); | ||
output.decode(&mut std::io::Cursor::new(bytes), self.version)?; | ||
Ok(output) | ||
} | ||
|
||
fn params(&self) -> SmartModuleExtraParams { | ||
self.params.clone() | ||
} | ||
} | ||
|
||
impl WasmFn for WasmedgeFn { | ||
type Context = WasmedgeContext; | ||
|
||
fn call(&self, ptr: i32, len: i32, version: i32, ctx: &mut Self::Context) -> Result<i32> { | ||
let res = self.call( | ||
&ctx.engine, | ||
vec![ | ||
Val::I32(ptr).into(), | ||
Val::I32(len).into(), | ||
Val::I32(version).into(), | ||
], | ||
)?; | ||
Ok(res[0].to_i32()) | ||
} | ||
} | ||
|
||
impl WasmedgeInstance { | ||
/// instantiate new module instance that contain context | ||
pub(crate) fn instantiate( | ||
store: &mut Store, | ||
executor: &mut Executor, | ||
module: Module, | ||
params: SmartModuleExtraParams, | ||
version: i16, | ||
) -> Result<Self, EngineError> { | ||
debug!("creating WasmModuleInstance"); | ||
let cb = Arc::new(RecordsCallBack::new()); | ||
let records_cb = cb.clone(); | ||
|
||
// See crates/fluvio-smartmodule-derive/src/generator/transform.rs for copy_records | ||
let copy_records_fn = move |caller: CallingFrame, | ||
inputs: Vec<WasmValue>| | ||
-> Result<Vec<WasmValue>, HostFuncError> { | ||
assert_eq!(inputs.len(), 2); | ||
let ptr = inputs[0].to_i32() as u32; | ||
let len = inputs[1].to_i32() as u32; | ||
|
||
debug!(len, "callback from wasm filter"); | ||
let caller = Caller::new(caller); | ||
let memory = caller.memory(0).unwrap(); | ||
|
||
let records = RecordsMemory { ptr, len, memory }; | ||
cb.set(records); | ||
Ok(vec![]) | ||
}; | ||
|
||
let import = ImportObjectBuilder::new() | ||
.with_func::<(i32, i32), ()>("copy_records", copy_records_fn) | ||
.map_err(|e| EngineError::Instantiate(e.into()))? | ||
.build("env") | ||
.map_err(|e| EngineError::Instantiate(e.into()))?; | ||
|
||
debug!("instantiating WASMtime"); | ||
store | ||
.register_import_module(executor, &import) | ||
.map_err(|e| EngineError::Instantiate(e.into()))?; | ||
let instance = store | ||
.register_active_module(executor, &module) | ||
.map_err(|e| EngineError::Instantiate(e.into()))?; | ||
|
||
// This is a hack to avoid them being dropped | ||
// FIXME: manage their lifetimes | ||
std::mem::forget(import); | ||
std::mem::forget(module); | ||
|
||
Ok(Self { | ||
instance, | ||
records_cb, | ||
params, | ||
version, | ||
}) | ||
} | ||
} |
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,11 @@ | ||
use std::convert::TryFrom; | ||
use std::fmt::Debug; | ||
|
||
use anyhow::{Result, Ok}; | ||
use fluvio_smartmodule::dataplane::smartmodule::{ | ||
SmartModuleInitInput, SmartModuleInitOutput, SmartModuleInitErrorStatus, | ||
}; | ||
|
||
use crate::engine::common::{WasmFn, WasmInstance}; | ||
|
||
use super::instance::SmartModuleInstanceContext; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you reorder terms in of:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's OK. Are you going to review the PR now? I will resolve such issues if general ideas LGTY.
P.S. this PR is generally complete except minor points.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you split this into PR just related to without WasmEdge? We need to discuss how to maintain WasmEdge related code.
Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's OK. But I'd like to elaborate a bit: only folder
wasmedge
is WasmEdge-specific code, and that's ~300 lines of simple code. To discuss how to maintain WasmEdge related code, and to get the ideas of how the common code work, it might be better to keep WasmEdge related code now. This can actually make it easier to review this PR.I can remove the WasmEdge related code after the code review if that's not decided.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like have review by team but can't seem to add reviewers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created a PR to the main repo infinyon#3257