Wasm
Since Camel 4.5
Camel supports Wasm to allow using Expression or Predicate.
Wasm Options
The Wasm language supports the following options which are listed below.
| Name | Default | Java Type | Description |
|---|---|---|---|
| Required Set the module (the distributable, loadable, and executable unit of code in WebAssembly) resource that provides the expression function. | ||
| The class of the result type (type from output). | ||
|
| Whether to trim the source code to remove leading and trailing whitespaces and line breaks. | |
|
| Whether a result of the expression that is a String starting with resource: is loaded as a resource and its content becomes the result, e.g. a script that returns resource:file:order.json or resource:classpath:templates/order.json (a name without a scheme is a classpath resource). Off by default; the resource: prefix on the expression text itself is always resolved. Applies to the expression used as a value, not as a predicate. |
Writing A Wasm function
In Wasm, sharing objects between the host, in this case the JVM, and the Wasm module is deliberately restricted and as of today, it requires a number of steps:
-
From the host, call a function inside the webassembly module that allocates a block of memory and returns its address, then save it
-
From the host, write the data that should be exchanged with the Wasm module to the saved address
-
From the host, invoke the required function passing both the address where the data is written and its size
-
From the Wasm module, read the data and process it
-
From the host, release the memory when done
Providing functions for memory management
The module hosting the function must provide the functions to allocate/deallocate memory that must be named alloc and dealloc respectively.
Here’s an example of the mentioned functions implemented in Rust:
pub extern "C" fn alloc(size: u32) -> *mut u8 {
let mut buf = Vec::with_capacity(size as usize);
let ptr = buf.as_mut_ptr();
// tell Rust not to clean this up
mem::forget(buf);
ptr
}
pub unsafe extern "C" fn dealloc(ptr: &mut u8, len: i32) {
// Retakes the pointer which allows its memory to be freed.
let _ = Vec::from_raw_parts(ptr, 0, len as usize);
} Data shapes
It is not possible to share a Java object with the Wasm module directly, and as mentioned before, data exchange leverages Wasm’s memory that can be accessed by both the host and the guest runtimes. At this stage, the data structure that the component exchange with the Wasm function is a subset of the Apache Camel Message, containing headers the body encoded as a base64 string:
public static class Wrapper {
@JsonProperty
public Map<String, String> headers = new HashMap<>();
@JsonProperty
public byte[] body;
} Data processing
The component expects the processing function to have the following signature:
fn function(ptr: u32, len: u32) -> u64 -
it accepts two 32bit unsigned integers arguments
-
a pointer to the memory location when the input data has been written (
ptr) -
the size of the input data (
len)
-
-
it returns a 64bit unsigned integer where:
-
the first 32bit represents a pointer to the return data
-
the last 31bit represents the size of the return data
-
the most significant bit of the returned data size is reserved to signal an error, so if it is set, then the return data could contain an error message/code/etc
-
Here’s an example of a complete function:
#[derive(Serialize, Deserialize)]
struct Message {
headers: HashMap<String, serde_json::Value>,
#[serde(with = "Base64Standard")]
body: Vec<u8>,
}
#[cfg_attr(all(target_arch = "wasm32"), export_name = "transform")]
#[no_mangle]
pub extern fn transform(ptr: u32, len: u32) -> u64 {
let bytes = unsafe {
slice::from_raw_parts_mut(
ptr as *mut u8,
len as usize)
};
let msg: Message = serde_json::from_slice(bytes).unwrap();
let res = String::from_utf8(msg.body).unwrap().to_uppercase().as_bytes().to_vec();
let out_len = res.len();
let out_ptr = alloc(out_len as u32);
unsafe {
std::ptr::copy_nonoverlapping(
res.as_ptr(),
out_ptr,
out_len as usize)
};
return ((out_ptr as u64) << 32) | out_len as u64;
} Errors and module state
The module is instantiated once per wasm expression and that instance handles every message, one at a time, so data the module keeps in its memory or globals carries over from one message to the next.
-
When the function returns with the error bit set, the error data is released with
deallocusing its size without the error bit, the error data becomes the message of the exception, and the instance is kept. -
When the call fails without returning, for example on a Rust
panicor any other Wasm trap, the instance is discarded and a new one is created from the module for the next message. Whatever the failed call allocated can no longer be released, so keeping the instance would grow its memory with every failure. Any state the module kept in the discarded instance is lost. On a route where every call fails this way, the module is instantiated again for every message (its data segments are copied and any start function runs), which costs more than a call on a kept instance.
Examples
Supposing we have compiled a Wasm module containing the function above, then it can be called in a Camel Route by its name and module resource location:
-
Java
-
XML
-
YAML
from("direct:in")
.transform()
.wasm("transform", "classpath://functions.wasm"); <route>
<from uri="direct:in"/>
<transform>
<wasm module="classpath://functions.wasm">transform</wasm>
</transform>
</route> - route:
from:
uri: direct:in
steps:
- transform:
expression:
wasm:
expression: transform
module: classpath://functions.wasm Using Wasm as a predicate
When the function is used as a Predicate, for example with the Filter EIP, its result is converted to a boolean depending on the result type:
-
With the default result type (
byte[]) orString, the returned bytes are read as a string and the standard Camel rules apply:trueandfalse(in any case) are parsed, empty data isfalse, and any other value istrue. So the function can simply returntrueorfalse. -
With the result type
Boolean, onlytrueandfalse(in any case) are accepted: any other value, including empty data, isfalse. -
With a Jackson
JsonNoderesult type, only the JSON booleantruematches.
-
Java
-
XML
-
YAML
from("direct:in")
.filter().wasm("is_valid", "classpath://functions.wasm")
.to("direct:valid"); <route>
<from uri="direct:in"/>
<filter>
<wasm module="classpath://functions.wasm">is_valid</wasm>
<to uri="direct:valid"/>
</filter>
</route> - route:
from:
uri: direct:in
steps:
- filter:
expression:
wasm:
expression: is_valid
module: classpath://functions.wasm
steps:
- to:
uri: direct:valid