From 3bae385f5643e171e76ec28888677dc86a084842 Mon Sep 17 00:00:00 2001 From: David Justice Date: Wed, 29 Mar 2023 15:12:57 -0400 Subject: [PATCH 1/2] dapr key value implementation Signed-off-by: David Justice --- Cargo.lock | 15 +++++ Cargo.toml | 2 +- crates/keyvalue/Cargo.toml | 3 + crates/keyvalue/src/implementors/dapr.rs | 73 ++++++++++++++++++++++++ crates/keyvalue/src/implementors/mod.rs | 2 + crates/keyvalue/src/lib.rs | 6 ++ 6 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 crates/keyvalue/src/implementors/dapr.rs diff --git a/Cargo.lock b/Cargo.lock index 4464f4bd..adc5c7ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1582,6 +1582,20 @@ dependencies = [ "syn", ] +[[package]] +name = "dapr" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dd3c32c32d2077ed230fcd1449118eb3ea1f7651d1a6268b0901f75cc368a9f" +dependencies = [ + "async-trait", + "bytes 1.4.0", + "prost", + "prost-types", + "tonic", + "tonic-build", +] + [[package]] name = "data-encoding" version = "2.3.3" @@ -4314,6 +4328,7 @@ dependencies = [ "azure_storage 0.10.0", "azure_storage_blobs 0.10.0", "bytes 1.4.0", + "dapr", "futures", "redis", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index d6324d81..1f631ef3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ path = "src/lib.rs" slight-blob-store = { workspace = true, features = ["aws_s3"], optional = true } slight-core = { workspace = true } slight-runtime = { workspace = true } -slight-keyvalue = { workspace = true, features = ["filesystem", "awsdynamodb", "redis", "azblob"], optional = true} +slight-keyvalue = { workspace = true, features = ["filesystem", "awsdynamodb", "redis", "azblob", "dapr"], optional = true} slight-distributed-locking = { workspace = true, features = ["etcd"], optional = true} slight-messaging = { workspace = true, features = ["filesystem", "mosquitto", "azsbus", "natsio"], optional = true} slight-runtime-configs = { workspace = true, optional = true } diff --git a/crates/keyvalue/Cargo.toml b/crates/keyvalue/Cargo.toml index 86c4b73a..142bf4a0 100644 --- a/crates/keyvalue/Cargo.toml +++ b/crates/keyvalue/Cargo.toml @@ -31,6 +31,8 @@ aws-config = { version = "0.54", optional = true } aws-sdk-dynamodb = { version = "0.24", optional = true } # kv.redis deps redis = { version = "0.22", optional = true } +# kv.dapr deps +dapr = { version ="0.11.0", optional = true } [features] default = ["filesystem"] @@ -38,3 +40,4 @@ filesystem = ["serde_json"] azblob = ["azure_storage_blobs", "azure_storage", "bytes", "futures"] awsdynamodb = ["aws-config", "aws-sdk-dynamodb"] redis = ["dep:redis"] +dapr = ["dep:dapr"] diff --git a/crates/keyvalue/src/implementors/dapr.rs b/crates/keyvalue/src/implementors/dapr.rs new file mode 100644 index 00000000..e4055978 --- /dev/null +++ b/crates/keyvalue/src/implementors/dapr.rs @@ -0,0 +1,73 @@ +use std::cell::RefCell; +use std::fmt::{Debug, Formatter}; +use std::sync::{Arc}; +use tokio::sync::Mutex; +use anyhow::{bail, Result}; +use async_trait::async_trait; +use dapr::{Client, client::TonicClient}; +use slight_common::BasicState; +use slight_runtime_configs::get_from_state; + +use super::KeyvalueImplementor; + +/// This is the underlying struct behind the `Dapr` variant of the `KeyvalueImplementor` enum. +/// +/// As per its' usage in `KeyvalueImplementor`, it must `derive` `Debug`, and `Clone`. +#[derive(Clone)] +pub struct DaprImplementor { + client: Arc>>>, + container_name: String, +} + +impl Debug for DaprImplementor { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_str(format!("[DaprImplementor] container_name: {}", self.container_name).as_str()) + } +} + +impl DaprImplementor { + pub async fn new(slight_state: &BasicState, name: &str) -> Self { + let connection_string = get_from_state("DAPR_ADDRESS", slight_state).await.unwrap(); + let client = Client::connect(connection_string).await.unwrap(); + let container_name = name.to_string(); + let internal_mut_client = Arc::new(Mutex::new(RefCell::new(client))); + Self { + client: internal_mut_client, + container_name, + } + } +} + +#[async_trait] +impl KeyvalueImplementor for DaprImplementor { + async fn get(&self, key: &str) -> Result> { + let container = self.container_name.clone(); + let mut client = self.client.lock().await; + let res = client.get_mut().get_state(container, key.to_string(), None).await?; + + if res.data.is_empty() { + bail!("key not found"); + } + Ok(res.data) + } + + async fn set(&self, key: &str, value: &[u8]) -> Result<()> { + let container = self.container_name.clone(); + let mut client = self.client.as_ref().lock().await; + client.get_mut().save_state(container, vec![(key.to_string(), value.to_vec())]).await?; + + Ok(()) + } + + async fn keys(&self) -> Result> { + bail!("not implemented"); + } + + async fn delete(&self, key: &str) -> Result<()> { + let container = self.container_name.clone(); + let mut client = self.client.lock().await; + client.get_mut().delete_state(container, key.to_string(), None).await?; + + Ok(()) + } +} diff --git a/crates/keyvalue/src/implementors/mod.rs b/crates/keyvalue/src/implementors/mod.rs index c27c3fbb..55108c7a 100644 --- a/crates/keyvalue/src/implementors/mod.rs +++ b/crates/keyvalue/src/implementors/mod.rs @@ -9,6 +9,8 @@ pub mod azblob; pub mod filesystem; #[cfg(feature = "redis")] pub mod redis; +#[cfg(feature = "dapr")] +pub mod dapr; #[async_trait] pub trait KeyvalueImplementor { diff --git a/crates/keyvalue/src/lib.rs b/crates/keyvalue/src/lib.rs index 0898e0bd..2863b564 100644 --- a/crates/keyvalue/src/lib.rs +++ b/crates/keyvalue/src/lib.rs @@ -86,6 +86,10 @@ impl KeyvalueInner { KeyvalueImplementors::Redis => { Arc::new(redis::RedisImplementor::new(slight_state, name).await) } + #[cfg(feature = "dapr")] + KeyvalueImplementors::Dapr => { + Arc::new(dapr::DaprImplementor::new(slight_state, name).await) + } }, } } @@ -105,6 +109,8 @@ pub enum KeyvalueImplementors { AwsDynamoDb, #[cfg(feature = "redis")] Redis, + #[cfg(feature = "dapr")] + Dapr, } impl From<&str> for KeyvalueImplementors { From 98c45cdbf36edf038658f8cd797fae017213ed9d Mon Sep 17 00:00:00 2001 From: David Justice Date: Fri, 31 Mar 2023 15:00:02 -0400 Subject: [PATCH 2/2] add dapr kv types for slightfile parsing Signed-off-by: David Justice --- src/commands/run.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/commands/run.rs b/src/commands/run.rs index ea141616..069b7e1c 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -32,15 +32,17 @@ use wit_bindgen_wasmtime::wasmtime::Store; const BLOB_STORE_HOST_IMPLEMENTORS: [&str; 2] = [S3_CAPABILITY_NAME, AZBLOB_CAPABILITY_NAME]; #[cfg(feature = "keyvalue")] -const KEYVALUE_HOST_IMPLEMENTORS: [&str; 8] = [ +const KEYVALUE_HOST_IMPLEMENTORS: [&str; 10] = [ "kv.filesystem", "kv.azblob", "kv.awsdynamodb", "kv.redis", + "kv.dapr", "keyvalue.filesystem", "keyvalue.azblob", "keyvalue.awsdynamodb", "keyvalue.redis", + "keyvalue.dapr", ]; #[cfg(feature = "distributed-locking")]