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

feat: support WasmEdge as an alternative engine #1

Draft
wants to merge 33 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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 Feb 26, 2023
7a30006
implement filter transform
xxchan Feb 26, 2023
685ae36
wip
xxchan Apr 9, 2023
15686cc
make the public API clear
xxchan Feb 26, 2023
1937438
move wasmtime_engine to a different mod
xxchan Apr 15, 2023
6f1604e
fix wasi feature
xxchan Apr 15, 2023
6b88555
rename wasmtime-engine -> wasmtime
xxchan Apr 15, 2023
cd038a3
Merge branch 'master' into xxchan/clena
xxchan Apr 18, 2023
86326da
reorg
xxchan Apr 18, 2023
e959a9d
Merge branch 'xxchan/clena' into xxchan/wasmedge
xxchan Apr 18, 2023
f17b8ef
support init for wasmedge/common
xxchan Apr 18, 2023
8b11252
Merge branch 'master' into xxchan/wasmedge
xxchan Apr 18, 2023
11e1751
make SmartModuleInstance common
xxchan Apr 18, 2023
abd4fb6
make create_transform common & reorg trait imp to imp
xxchan Apr 18, 2023
66bcbdd
more movement
xxchan Apr 18, 2023
0132522
Merge branch 'master' into xxchan/wasmedge
xxchan Apr 29, 2023
da7a2e2
add other tests for wasmedge
xxchan Apr 29, 2023
26d9053
support agg for common/wasmedge
xxchan Apr 29, 2023
56de150
movement
xxchan Apr 29, 2023
14c4c3c
refactor wasmtime to use the common code
xxchan Apr 29, 2023
3d8f4bf
WasmTime -> Wasmtime
xxchan Apr 29, 2023
d0bb940
move transform unit tests to common
xxchan Apr 29, 2023
7924834
rename Wasmedge -> WasmEdge
xxchan Apr 29, 2023
f2f55e8
minor tweaks
xxchan Apr 29, 2023
e6f7e2e
change features
xxchan May 15, 2023
06d579f
rm wasmedge
xxchan May 31, 2023
6ff943b
Merge branch 'master' into xxchan/wasmedge
xxchan May 31, 2023
7e559c1
remove wasmedge
xxchan May 31, 2023
7d23e1a
fmt
xxchan May 31, 2023
f5650e3
fmt with group_imports = "StdExternalCrate"
xxchan May 31, 2023
fe98b8f
try to make diff smaller
xxchan May 31, 2023
136c572
make diff smaller
xxchan May 31, 2023
72f4ed7
clippy
xxchan May 31, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move transform unit tests to common
  • Loading branch information
xxchan committed Apr 29, 2023
commit d0bb940084f4376e206edf5abcb9f10c50d239e2
5 changes: 3 additions & 2 deletions crates/fluvio-smartengine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ description = "The official Fluvio SmartEngine"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
engine = ["wasmtime-engine", "wasmedge-engine"]
engine = []
wasi = ["wasmtime-wasi", "wasmtime-engine", "engine"]
transformation = ["serde_json", "serde_yaml"]
default = ["engine"]
Expand All @@ -25,7 +25,8 @@ thiserror = { workspace = true }
anyhow = { workspace = true }
# Use the Clone trait
wasmedge-sdk = { git = "https://github.com/WasmEdge/WasmEdge", rev = "fe5fe2c", optional = true }
xxchan marked this conversation as resolved.
Show resolved Hide resolved
serde = { workspace = true, features = ['derive'] }
# std is needed for ser/de AtomicU64
serde = { workspace = true, features = ['derive', 'std'] }
serde_json = { workspace = true, optional = true }
serde_yaml = { workspace = true, default-features = false, optional = true }
cfg-if = { workspace = true }
Expand Down
63 changes: 63 additions & 0 deletions crates/fluvio-smartengine/src/engine/common/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use super::{WasmFn, WasmInstance};
use anyhow::Result;
use fluvio_smartmodule::dataplane::smartmodule::{
SmartModuleInitInput, SmartModuleInitErrorStatus, SmartModuleInitOutput,
};

pub(crate) const INIT_FN_NAME: &str = "init";

pub(crate) struct SmartModuleInit<F: WasmFn>(F);

impl<F: WasmFn> std::fmt::Debug for SmartModuleInit<F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "InitFn")
}
}

impl<F: WasmFn + Send + Sync> SmartModuleInit<F> {
pub(crate) fn try_instantiate<I>(
instance: &mut I,
ctx: &mut <I as WasmInstance>::Context,
) -> Result<Option<Self>>
where
I: WasmInstance<Func = F>,
F: WasmFn<Context = I::Context>,
{
match instance.get_fn(INIT_FN_NAME, ctx)? {
Some(func) => Ok(Some(Self(func))),
None => Ok(None),
}
}
}

impl<F: WasmFn + Send + Sync> SmartModuleInit<F> {
/// initialize SmartModule
pub(crate) fn initialize<I>(
&mut self,
input: SmartModuleInitInput,
instance: &mut I,
ctx: &mut I::Context,
) -> Result<()>
where
I: WasmInstance,
F: WasmFn<Context = I::Context>,
{
let (ptr, len, version) = instance.write_input(&input, ctx)?;
let init_output = self.0.call(ptr, len, version, ctx)?;

if init_output < 0 {
let internal_error = SmartModuleInitErrorStatus::try_from(init_output)
.unwrap_or(SmartModuleInitErrorStatus::UnknownError);

match internal_error {
SmartModuleInitErrorStatus::InitError => {
let output: SmartModuleInitOutput = instance.read_output(ctx)?;
Err(output.error.into())
}
_ => Err(internal_error.into()),
}
} else {
Ok(())
}
}
}
87 changes: 87 additions & 0 deletions crates/fluvio-smartengine/src/engine/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
mod init;
mod transforms;
pub(crate) use init::*;
pub(crate) use transforms::*;

use anyhow::Result;
use fluvio_protocol::{Decoder, Encoder};
use fluvio_smartmodule::dataplane::smartmodule::{
SmartModuleExtraParams, SmartModuleInitInput, SmartModuleInput, SmartModuleOutput,
};

pub(crate) trait WasmInstance {
type Context;
type Func: WasmFn<Context = Self::Context> + Send + Sync + 'static;

fn params(&self) -> SmartModuleExtraParams;

fn get_fn(&self, name: &str, ctx: &mut Self::Context) -> Result<Option<Self::Func>>;

fn write_input<E: Encoder>(
&mut self,
input: &E,
ctx: &mut Self::Context,
) -> Result<(i32, i32, i32)>;
fn read_output<D: Decoder + Default>(&mut self, ctx: &mut Self::Context) -> Result<D>;
}

/// All smartmodule wasm functions have the same ABI:
/// `(ptr: *mut u8, len: usize, version: i16) -> i32`, which is `(param i32 i32 i32) (result i32)` in wasm.
pub(crate) trait WasmFn {
type Context;
fn call(&self, ptr: i32, len: i32, version: i32, ctx: &mut Self::Context) -> Result<i32>;
}

pub(crate) struct SmartModuleInstance<I: WasmInstance<Func = F>, F: WasmFn> {
pub instance: I,
pub transform: Box<dyn DowncastableTransform<I>>,
pub init: Option<SmartModuleInit<F>>,
}

impl<I: WasmInstance<Func = F>, F: WasmFn + Send + Sync> SmartModuleInstance<I, F> {
#[cfg(test)]
#[allow(clippy::borrowed_box)]
pub(crate) fn transform(&self) -> &Box<dyn DowncastableTransform<I>> {
&self.transform
}

#[cfg(test)]
pub(crate) fn get_init(&self) -> &Option<SmartModuleInit<F>> {
&self.init
}

pub(crate) fn new(
instance: I,
init: Option<SmartModuleInit<F>>,
transform: Box<dyn DowncastableTransform<I>>,
) -> Self {
Self {
instance,
init,
transform,
}
}

pub(crate) fn process(
&mut self,
input: SmartModuleInput,
ctx: &mut I::Context,
) -> Result<SmartModuleOutput> {
self.transform.process(input, &mut self.instance, ctx)
}

pub fn init<C>(&mut self, ctx: &mut I::Context) -> Result<()>
where
I: WasmInstance<Context = C>,
F: WasmFn<Context = C>,
{
if let Some(init) = &mut self.init {
let input = SmartModuleInitInput {
params: self.instance.params(),
};
init.initialize(input, &mut self.instance, ctx)
} else {
Ok(())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@ mod test {

const SM_AGGEGRATE: &str = "fluvio_smartmodule_aggregate";

type AggregateTransform =
crate::engine::common::AggregateTransform<crate::engine::wasmedge::instance::WasmedgeFn>;
cfg_if::cfg_if! {

if #[cfg(feature = "wasmtime-engine")] {
type AggregateTransform =
crate::engine::common::AggregateTransform<crate::engine::wasmtime::instance::WasmtimeFn>;
} else if #[cfg(feature = "wasmedge-engine")] {
type AggregateTransform =
crate::engine::common::AggregateTransform<crate::engine::wasmedge::instance::WasmedgeFn>;
} else {
compile_error!("no engine specified");
}
}

#[ignore]
#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ mod test {

const SM_FILTER_MAP: &str = "fluvio_smartmodule_filter_map";


#[ignore]
#[test]
fn test_filter_map() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod test {
use crate::engine::{SmartEngine, SmartModuleChainBuilder};
use crate::engine::config::SmartModuleConfig;
use crate::engine::fixture::read_wasm_module;

const SM_MAP: &str = "fluvio_smartmodule_map";

#[ignore]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,20 @@
use std::any::Any;

use anyhow::Result;
use fluvio_protocol::{Decoder, Encoder};
use fluvio_smartmodule::dataplane::smartmodule::{
SmartModuleExtraParams, SmartModuleInitErrorStatus, SmartModuleInitInput,
SmartModuleInitOutput, SmartModuleInput, SmartModuleOutput, SmartModuleTransformErrorStatus,
SmartModuleInput, SmartModuleOutput, SmartModuleTransformErrorStatus,
SmartModuleAggregateInput, SmartModuleAggregateOutput,
};
use tracing::debug;

use crate::SmartModuleInitialData;

use super::error::EngineError;

pub(crate) trait WasmInstance {
type Context;
type Func: WasmFn<Context = Self::Context> + Send + Sync + 'static;

fn params(&self) -> SmartModuleExtraParams;
use crate::{SmartModuleInitialData, engine::error::EngineError};

fn get_fn(&self, name: &str, ctx: &mut Self::Context) -> Result<Option<Self::Func>>;
use super::{WasmInstance, WasmFn};

fn write_input<E: Encoder>(
&mut self,
input: &E,
ctx: &mut Self::Context,
) -> Result<(i32, i32, i32)>;
fn read_output<D: Decoder + Default>(&mut self, ctx: &mut Self::Context) -> Result<D>;
}

/// All smartmodule wasm functions have the same ABI:
/// `(ptr: *mut u8, len: usize, version: i16) -> i32`, which is `(param i32 i32 i32) (result i32)` in wasm.
pub(crate) trait WasmFn {
type Context;
fn call(&self, ptr: i32, len: i32, version: i32, ctx: &mut Self::Context) -> Result<i32>;
}
mod filter;
mod map;
mod filter_map;
mod array_map;
mod aggregate;

pub(crate) trait SmartModuleTransform<I: WasmInstance>: Send + Sync {
/// transform records
Expand Down Expand Up @@ -222,115 +202,3 @@ where
Err(EngineError::UnknownSmartModule.into())
}
}

pub(crate) const INIT_FN_NAME: &str = "init";

pub(crate) struct SmartModuleInit<F: WasmFn>(F);

impl<F: WasmFn> std::fmt::Debug for SmartModuleInit<F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "InitFn")
}
}

impl<F: WasmFn + Send + Sync> SmartModuleInit<F> {
pub(crate) fn try_instantiate<I>(
instance: &mut I,
ctx: &mut <I as WasmInstance>::Context,
) -> Result<Option<Self>>
where
I: WasmInstance<Func = F>,
F: WasmFn<Context = I::Context>,
{
match instance.get_fn(INIT_FN_NAME, ctx)? {
Some(func) => Ok(Some(Self(func))),
None => Ok(None),
}
}
}

impl<F: WasmFn + Send + Sync> SmartModuleInit<F> {
/// initialize SmartModule
pub(crate) fn initialize<I>(
&mut self,
input: SmartModuleInitInput,
instance: &mut I,
ctx: &mut I::Context,
) -> Result<()>
where
I: WasmInstance,
F: WasmFn<Context = I::Context>,
{
let (ptr, len, version) = instance.write_input(&input, ctx)?;
let init_output = self.0.call(ptr, len, version, ctx)?;

if init_output < 0 {
let internal_error = SmartModuleInitErrorStatus::try_from(init_output)
.unwrap_or(SmartModuleInitErrorStatus::UnknownError);

match internal_error {
SmartModuleInitErrorStatus::InitError => {
let output: SmartModuleInitOutput = instance.read_output(ctx)?;
Err(output.error.into())
}
_ => Err(internal_error.into()),
}
} else {
Ok(())
}
}
}

pub(crate) struct SmartModuleInstance<I: WasmInstance<Func = F>, F: WasmFn> {
pub instance: I,
pub transform: Box<dyn DowncastableTransform<I>>,
pub init: Option<SmartModuleInit<F>>,
}

impl<I: WasmInstance<Func = F>, F: WasmFn + Send + Sync> SmartModuleInstance<I, F> {
#[cfg(test)]
#[allow(clippy::borrowed_box)]
pub(crate) fn transform(&self) -> &Box<dyn DowncastableTransform<I>> {
&self.transform
}

#[cfg(test)]
pub(crate) fn get_init(&self) -> &Option<SmartModuleInit<F>> {
&self.init
}

pub(crate) fn new(
instance: I,
init: Option<SmartModuleInit<F>>,
transform: Box<dyn DowncastableTransform<I>>,
) -> Self {
Self {
instance,
init,
transform,
}
}

pub(crate) fn process(
&mut self,
input: SmartModuleInput,
ctx: &mut I::Context,
) -> Result<SmartModuleOutput> {
self.transform.process(input, &mut self.instance, ctx)
}

pub fn init<C>(&mut self, ctx: &mut I::Context) -> Result<()>
where
I: WasmInstance<Context = C>,
F: WasmFn<Context = C>,
{
if let Some(init) = &mut self.init {
let input = SmartModuleInitInput {
params: self.instance.params(),
};
init.initialize(input, &mut self.instance, ctx)
} else {
Ok(())
}
}
}
Loading