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

Added hostcalls for the new builder api hostcalls #427

Merged
merged 22 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a39fc4f
bring over changes from old branch
computermouth Aug 29, 2024
d1acafb
Fix the declaration for lookup-result methods in trappable_imports
elliottt Aug 29, 2024
e309c33
start fleshing out builder api
computermouth Aug 29, 2024
d9f7172
complete port from the old branch
computermouth Aug 30, 2024
8fee6e6
more kv store hostcalls, although linkage seems to be failing
computermouth Aug 30, 2024
15298c0
open, list, insert, more or less working, need more work on error han…
computermouth Sep 4, 2024
7b2675b
fix up errors for inserts, compiles clean
computermouth Sep 10, 2024
00d2de9
finished error reporting for deletes
computermouth Sep 10, 2024
4c85485
everything but ttl and regression tests
computermouth Sep 11, 2024
910d316
all tests pass, but infinite recursion when deleting in delete
computermouth Sep 11, 2024
29bf88d
fixed list ttl deletion, all tests pass
computermouth Sep 12, 2024
be83cf2
fix for old sdk on new hostcalls
computermouth Sep 12, 2024
1de2b41
Merge remote-tracking branch 'origin/main' into byoung/kv-builder
computermouth Sep 12, 2024
5e226c9
clippy
computermouth Sep 12, 2024
ce13ff0
add base64 to the cargo.lock
computermouth Sep 12, 2024
7a47c14
Added in the OOB check
computermouth Sep 13, 2024
318e575
write out metadata length, even on failure
computermouth Sep 13, 2024
e2170f9
cleanups as per @ulyssa
computermouth Sep 17, 2024
5557028
added unit tests for kv store hostcalls
computermouth Sep 18, 2024
c22e3a8
Implement the component-side hostcalls for KV Store (#430)
computermouth Sep 26, 2024
2154eb0
Merge remote-tracking branch 'origin/main' into byoung/kv-builder
computermouth Sep 27, 2024
3ece358
re-make adapter
computermouth Sep 27, 2024
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/tests/trap-test/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ wasmtime-wasi = { workspace = true }
wasmtime-wasi-nn = { workspace = true }
wat = { workspace = true }
wiggle = { workspace = true }
base64.workspace = true
ulyssa marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
tempfile = "3.6.0"
Expand Down
74 changes: 74 additions & 0 deletions lib/compute-at-edge-abi/compute-at-edge.witx
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@
)
)

;; NOTE: These are deprecated, use the fastly_kv_store hostcalls
(module $fastly_object_store
(@interface func (export "open")
(param $name string)
Expand Down Expand Up @@ -820,6 +821,79 @@
)
)

(module $fastly_kv_store
(@interface func (export "open")
(param $name string)
(result $err (expected $kv_store_handle (error $fastly_status)))
)

(@interface func (export "lookup")
(param $store $kv_store_handle)
(param $key string)
(param $lookup_config_mask $kv_lookup_config_options)
(param $lookup_configuration (@witx pointer $kv_lookup_config))
(param $handle_out (@witx pointer $kv_store_lookup_handle))
(result $err (expected (error $fastly_status)))
)

(@interface func (export "lookup_wait")
(param $handle $kv_store_lookup_handle)
(param $body_handle_out (@witx pointer $body_handle))
(param $metadata_buf (@witx pointer (@witx char8)))
(param $metadata_buf_len (@witx usize))
(param $nwritten_out (@witx pointer (@witx usize)))
(param $generation_out (@witx pointer u32))
(param $kv_error_out (@witx pointer $kv_error))
(result $err (expected (error $fastly_status)))
)

(@interface func (export "insert")
(param $store $kv_store_handle)
(param $key string)
(param $body_handle $body_handle)
(param $insert_config_mask $kv_insert_config_options)
(param $insert_configuration (@witx pointer $kv_insert_config))
(param $handle_out (@witx pointer $kv_store_insert_handle))
(result $err (expected (error $fastly_status)))
)

(@interface func (export "insert_wait")
(param $handle $kv_store_insert_handle)
(param $kv_error_out (@witx pointer $kv_error))
(result $err (expected (error $fastly_status)))
)

(@interface func (export "delete")
(param $store $kv_store_handle)
(param $key string)
(param $delete_config_mask $kv_delete_config_options)
(param $delete_configuration (@witx pointer $kv_delete_config))
(param $handle_out (@witx pointer $kv_store_delete_handle))
(result $err (expected (error $fastly_status)))
)

(@interface func (export "delete_wait")
(param $handle $kv_store_delete_handle)
(param $kv_error_out (@witx pointer $kv_error))
(result $err (expected (error $fastly_status)))
)

(@interface func (export "list")
(param $store $kv_store_handle)
(param $list_config_mask $kv_list_config_options)
(param $list_configuration (@witx pointer $kv_list_config))
(param $handle_out (@witx pointer $kv_store_list_handle))
(result $err (expected (error $fastly_status)))
)

(@interface func (export "list_wait")
(param $handle $kv_store_list_handle)
(param $body_handle_out (@witx pointer $body_handle))
(param $kv_error_out (@witx pointer $kv_error))
(result $err (expected (error $fastly_status)))
)
)

(module $fastly_secret_store
(@interface func (export "open")
(param $name string)
Expand Down
114 changes: 110 additions & 4 deletions lib/compute-at-edge-abi/typenames.witx
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,26 @@
(typename $endpoint_handle (handle))
;;; A handle to an Edge Dictionary.
(typename $dictionary_handle (handle))
;;; A handle to an Object Store.
;;; (DEPRECATED) A handle to an Object Store.
(typename $object_store_handle (handle))
;;; A handle to a pending KV lookup request.
;;; (DEPRECATED) A handle to a pending KV lookup request.
(typename $pending_kv_lookup_handle (handle))
;;; A handle to a pending KV insert request.
;;; (DEPRECATED) A handle to a pending KV insert request.
(typename $pending_kv_insert_handle (handle))
;;; A handle to a pending KV delete request.
;;; (DEPRECATED) A handle to a pending KV delete request.
(typename $pending_kv_delete_handle (handle))
;;; (DEPRECATED) A handle to a pending KV list.
(typename $pending_kv_list_handle (handle))
;;; A handle to an KV Store.
(typename $kv_store_handle (handle))
;;; A handle to a KV Store lookup.
(typename $kv_store_lookup_handle (handle))
;;; A handle to a KV Store insert.
(typename $kv_store_insert_handle (handle))
;;; A handle to a KV Store delete.
(typename $kv_store_delete_handle (handle))
;;; A handle to a KV Store list.
(typename $kv_store_list_handle (handle))
;;; A handle to a Secret Store.
(typename $secret_store_handle (handle))
;;; A handle to an individual secret.
Expand Down Expand Up @@ -385,3 +397,97 @@
(field $workspace_len u32)
)
)

(typename $kv_lookup_config_options
(flags (@witx repr u32)
$reserved
))

(typename $kv_lookup_config
(record
(field $reserved u32)
))

(typename $kv_delete_config_options
(flags (@witx repr u32)
$reserved
))

(typename $kv_delete_config
(record
(field $reserved u32)
))

(typename $kv_insert_config_options
(flags (@witx repr u32)
$reserved
$background_fetch
$if_generation_match
$metadata
$time_to_live_sec
))

(typename $kv_insert_mode
(enum (@witx tag u32)
$overwrite
$add
$append
$prepend))

(typename $kv_insert_config
(record
(field $mode $kv_insert_mode)
(field $if_generation_match u32)
(field $metadata (@witx pointer (@witx char8)))
(field $metadata_len u32)
(field $time_to_live_sec u32)
))

(typename $kv_list_config_options
(flags (@witx repr u32)
$reserved
$cursor
$limit
$prefix
))

(typename $kv_list_mode
(enum (@witx tag u32)
$strong
$eventual))

(typename $kv_list_config
(record
(field $mode $kv_list_mode)
(field $cursor (@witx pointer (@witx char8)))
(field $cursor_len u32)
(field $limit u32)
(field $prefix (@witx pointer (@witx char8)))
(field $prefix_len u32)
))

(typename $kv_error
(enum (@witx tag u32)
;;; The $kv_error has not been set.
$uninitialized
;;; There was no error.
$ok
;;; KV store cannot or will not process the request due to something that is perceived to be a client error
;;; This will map to the api's 400 codes
$bad_request
;;; KV store cannot find the requested resource
;;; This will map to the api's 404 codes
$not_found
;;; KV store cannot fulfill the request, as definied by the client's prerequisites (ie. if-generation-match)
;;; This will map to the api's 412 codes
$precondition_failed
;;; The size limit for a KV store key was exceeded.
;;; This will map to the api's 413 codes
$payload_too_large
;;; The system encountered an unexpected internal error.
;;; This will map to all remaining http error codes
$internal_error
;;; Too many requests have been made to the KV store.
;;; This will map to the api's 429 codes
$too_many_requests
))
19 changes: 18 additions & 1 deletion lib/src/component/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use {
crate::{
config::ClientCertError,
error::{self, HandleError},
object_store::{KeyValidationError, ObjectStoreError},
object_store::{KeyValidationError, KvStoreError, ObjectStoreError},
wiggle_abi::{DictionaryError, SecretStoreError},
},
http::{
Expand Down Expand Up @@ -130,6 +130,22 @@ impl From<ObjectStoreError> for types::Error {
}
}

impl From<KvStoreError> for types::Error {
fn from(err: KvStoreError) -> Self {
use KvStoreError::*;
match err {
Uninitialized => panic!("{}", err),
ulyssa marked this conversation as resolved.
Show resolved Hide resolved
Ok => panic!("{}", err),
ulyssa marked this conversation as resolved.
Show resolved Hide resolved
BadRequest => types::Error::InvalidArgument,
NotFound => types::Error::OptionalNone,
PreconditionFailed => types::Error::InvalidArgument,
PayloadTooLarge => types::Error::InvalidArgument,
InternalError => types::Error::InvalidArgument,
TooManyRequests => types::Error::InvalidArgument,
}
}
}

impl From<KeyValidationError> for types::Error {
fn from(_: KeyValidationError) -> Self {
types::Error::GenericError
Expand Down Expand Up @@ -177,6 +193,7 @@ impl From<error::Error> for types::Error {
// We delegate to some error types' own implementation of `to_fastly_status`.
Error::DictionaryError(e) => e.into(),
Error::ObjectStoreError(e) => e.into(),
Error::KvStoreError(e) => e.into(),
Error::SecretStoreError(e) => e.into(),
// All other hostcall errors map to a generic `ERROR` value.
Error::AbiVersionMismatch
Expand Down
42 changes: 29 additions & 13 deletions lib/src/component/kv_store.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,55 @@
use {
super::fastly::api::{http_body, kv_store, types},
super::types::TrappableError,
crate::linking::ComponentCtx,
wasmtime_wasi::WasiView,
};

pub struct LookupResult;
pub struct LookupResult {
body: http_body::BodyHandle,
metadata: Option<Vec<u8>>,
generation: u32,
}

#[async_trait::async_trait]
impl kv_store::HostLookupResult for ComponentCtx {
async fn body(
&mut self,
_self_: wasmtime::component::Resource<kv_store::LookupResult>,
) -> http_body::BodyHandle {
todo!()
rep: wasmtime::component::Resource<kv_store::LookupResult>,
) -> wasmtime::Result<http_body::BodyHandle> {
Ok(self.table().get(&rep)?.body)
}

async fn metadata(
&mut self,
_self_: wasmtime::component::Resource<kv_store::LookupResult>,
_max_len: u64,
) -> Result<Option<Vec<u8>>, types::Error> {
todo!()
rep: wasmtime::component::Resource<kv_store::LookupResult>,
max_len: u64,
) -> Result<Option<Vec<u8>>, TrappableError> {
let res = self.table().get(&rep)?;
let Some(md) = res.metadata.as_ref() else {
return Ok(None);
};

if md.len() > max_len as usize {
return Err(types::Error::BufferLen(md.len() as u64).into());
}

Ok(self.table().get_mut(&rep)?.metadata.take())
ulyssa marked this conversation as resolved.
Show resolved Hide resolved
}

async fn generation(
&mut self,
_self_: wasmtime::component::Resource<kv_store::LookupResult>,
) -> u32 {
todo!()
rep: wasmtime::component::Resource<kv_store::LookupResult>,
) -> wasmtime::Result<u32> {
Ok(self.table().get(&rep)?.generation)
}

fn drop(
&mut self,
_rep: wasmtime::component::Resource<kv_store::LookupResult>,
rep: wasmtime::component::Resource<kv_store::LookupResult>,
) -> wasmtime::Result<()> {
todo!()
self.table().delete(rep)?;
Ok(())
}
}

Expand Down
6 changes: 5 additions & 1 deletion lib/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ component::bindgen!({
async: true,
with: {
"fastly:api/uap/user-agent": uap::UserAgent,
"fastly:api/kv-store/lookup-result": kv_store::LookupResult,

"wasi:clocks": wasmtime_wasi::bindings::clocks,
"wasi:random": wasmtime_wasi::bindings::random,
Expand All @@ -18,7 +19,10 @@ component::bindgen!({
},

trappable_imports: [
"header-values-get"
"header-values-get",
"[method]lookup-result.body",
"[method]lookup-result.metadata",
"[method]lookup-result.generation"
],
});

Expand Down
Loading
Loading