/
lib.rs
405 lines (364 loc) · 13.4 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Redis implementation for wrpc:keyvalue.
//!
//! This implementation is multi-threaded and operations between different actors
//! use different connections and can run in parallel.
//! A single connection is shared by all instances of the same actor id (public key),
//! so there may be some brief lock contention if several instances of the same actor
//! are simultaneously attempting to communicate with redis. See documentation
//! on the [exec](#exec) function for more information.
use core::num::NonZeroU64;
use std::collections::HashMap;
use std::sync::Arc;
use anyhow::{bail, Context as _};
use redis::aio::ConnectionManager;
use redis::{Cmd, FromRedisValue};
use tokio::sync::RwLock;
use tracing::{debug, error, info, instrument, warn};
use wasmcloud_provider_sdk::core::HostData;
use wasmcloud_provider_sdk::{
get_connection, load_host_data, propagate_trace_for_ctx, run_provider, Context, LinkConfig,
Provider,
};
use exports::wrpc::keyvalue;
wit_bindgen_wrpc::generate!();
/// Default URL to use to connect to Redis
const DEFAULT_CONNECT_URL: &str = "redis://127.0.0.1:6379/";
/// Configuration key that will be used to search for Redis config
const CONFIG_REDIS_URL_KEY: &str = "URL";
type Result<T, E = keyvalue::store::Error> = core::result::Result<T, E>;
#[derive(Clone)]
pub enum DefaultConnection {
ClientConfig(HashMap<String, String>),
Conn(ConnectionManager),
}
/// Redis `wrpc:keyvalue` provider implementation.
#[derive(Clone)]
pub struct KvRedisProvider {
// store redis connections per source ID
sources: Arc<RwLock<HashMap<String, ConnectionManager>>>,
// default connection, which may be uninitialized
default_connection: Arc<RwLock<DefaultConnection>>,
}
pub async fn run() -> anyhow::Result<()> {
KvRedisProvider::run().await
}
impl KvRedisProvider {
pub async fn run() -> anyhow::Result<()> {
let HostData { config, .. } = load_host_data().context("failed to load host data")?;
let provider = KvRedisProvider::new(config.clone());
let shutdown = run_provider(provider.clone(), "keyvalue-redis-provider")
.await
.context("failed to run provider")?;
let connection = get_connection();
serve(
&connection.get_wrpc_client(connection.provider_key()),
provider,
shutdown,
)
.await
}
#[must_use]
pub fn new(initial_config: HashMap<String, String>) -> Self {
KvRedisProvider {
sources: Arc::default(),
default_connection: Arc::new(RwLock::new(DefaultConnection::ClientConfig(
initial_config,
))),
}
}
#[instrument(level = "trace", skip_all)]
async fn get_default_connection(&self) -> anyhow::Result<ConnectionManager> {
if let DefaultConnection::Conn(conn) = &*self.default_connection.read().await {
Ok(conn.clone())
} else {
let mut default_conn = self.default_connection.write().await;
match &mut *default_conn {
DefaultConnection::Conn(conn) => Ok(conn.clone()),
DefaultConnection::ClientConfig(cfg) => {
let conn = redis::Client::open(retrieve_default_url(cfg))
.context("failed to construct default Redis client")?
.get_connection_manager()
.await
.context("failed to construct Redis connection manager")?;
*default_conn = DefaultConnection::Conn(conn.clone());
Ok(conn)
}
}
}
}
#[instrument(level = "debug", skip(self))]
async fn invocation_conn(&self, context: Option<Context>) -> anyhow::Result<ConnectionManager> {
if let Some(ref source_id) = context.and_then(|Context { component, .. }| component) {
let sources = self.sources.read().await;
let Some(conn) = sources.get(source_id) else {
error!(source_id, "no Redis connection found for component");
bail!("No Redis connection found for actor [{source_id}]. Please ensure the URL supplied in the link definition is a valid Redis URL")
};
Ok(conn.clone())
} else {
self.get_default_connection().await.map_err(|err| {
error!(error = ?err, "failed to get default connection for invocation");
err
})
}
}
/// Execute Redis async command
async fn exec_cmd<T: FromRedisValue>(
&self,
context: Option<Context>,
cmd: &mut Cmd,
) -> Result<T, keyvalue::store::Error> {
let mut conn = self
.invocation_conn(context)
.await
.map_err(|err| keyvalue::store::Error::Other(format!("{err:#}")))?;
match cmd.query_async(&mut conn).await {
Ok(v) => Ok(v),
Err(e) => {
error!("failed to execute Redis command: {e}");
Err(keyvalue::store::Error::Other(format!(
"failed to execute Redis command: {e}"
)))
}
}
}
}
impl keyvalue::store::Handler<Option<Context>> for KvRedisProvider {
#[instrument(level = "debug", skip(self))]
async fn delete(
&self,
context: Option<Context>,
bucket: String,
key: String,
) -> anyhow::Result<Result<()>> {
propagate_trace_for_ctx!(context);
// TODO: Use bucket
_ = bucket;
Ok(self.exec_cmd(context, &mut Cmd::del(key)).await)
}
#[instrument(level = "debug", skip(self))]
async fn exists(
&self,
context: Option<Context>,
bucket: String,
key: String,
) -> anyhow::Result<Result<bool>> {
propagate_trace_for_ctx!(context);
// TODO: Use bucket
_ = bucket;
Ok(self.exec_cmd(context, &mut Cmd::exists(key)).await)
}
#[instrument(level = "debug", skip(self))]
async fn get(
&self,
context: Option<Context>,
bucket: String,
key: String,
) -> anyhow::Result<Result<Option<Vec<u8>>>> {
propagate_trace_for_ctx!(context);
// TODO: Use bucket
_ = bucket;
match self
.exec_cmd::<redis::Value>(context, &mut Cmd::get(key))
.await
{
Ok(redis::Value::Nil) => Ok(Ok(None)),
Ok(redis::Value::Data(buf)) => Ok(Ok(Some(buf))),
Ok(_) => Ok(Err(keyvalue::store::Error::Other(
"invalid data type returned by Redis".into(),
))),
Err(err) => Ok(Err(err)),
}
}
#[instrument(level = "debug", skip(self))]
async fn set(
&self,
context: Option<Context>,
bucket: String,
key: String,
value: Vec<u8>,
) -> anyhow::Result<Result<()>> {
propagate_trace_for_ctx!(context);
// TODO: Use bucket
_ = bucket;
Ok(self.exec_cmd(context, &mut Cmd::set(key, value)).await)
}
#[instrument(level = "debug", skip(self))]
async fn list_keys(
&self,
context: Option<Context>,
bucket: String,
cursor: Option<u64>,
) -> anyhow::Result<Result<keyvalue::store::KeyResponse>> {
propagate_trace_for_ctx!(context);
// TODO: Use bucket
_ = bucket;
match self
.exec_cmd(
context,
redis::cmd("SCAN").cursor_arg(cursor.unwrap_or_default()),
)
.await
{
Ok((cursor, keys)) => Ok(Ok(keyvalue::store::KeyResponse {
keys,
cursor: NonZeroU64::new(cursor).map(Into::into),
})),
Err(err) => Ok(Err(err)),
}
}
}
impl keyvalue::atomics::Handler<Option<Context>> for KvRedisProvider {
/// Increments a numeric value, returning the new value
#[instrument(level = "debug", skip(self))]
async fn increment(
&self,
context: Option<Context>,
bucket: String,
key: String,
delta: u64,
) -> anyhow::Result<Result<u64, keyvalue::store::Error>> {
propagate_trace_for_ctx!(context);
// TODO: Use bucket
_ = bucket;
Ok(self
.exec_cmd::<u64>(context, &mut Cmd::incr(key, delta))
.await)
}
}
impl keyvalue::batch::Handler<Option<Context>> for KvRedisProvider {
async fn get_many(
&self,
ctx: Option<Context>,
bucket: String,
keys: Vec<String>,
) -> anyhow::Result<Result<Vec<Option<(String, Vec<u8>)>>>> {
// TODO: Use bucket
_ = bucket;
Ok(self.exec_cmd(ctx, &mut Cmd::mget(&keys)).await)
}
async fn set_many(
&self,
ctx: Option<Context>,
bucket: String,
items: Vec<(String, Vec<u8>)>,
) -> anyhow::Result<Result<()>> {
// TODO: Use bucket
_ = bucket;
Ok(self.exec_cmd(ctx, &mut Cmd::mset(&items)).await)
}
async fn delete_many(
&self,
ctx: Option<Context>,
bucket: String,
keys: Vec<String>,
) -> anyhow::Result<Result<()>> {
// TODO: Use bucket
_ = bucket;
Ok(self.exec_cmd(ctx, &mut Cmd::del(&keys)).await)
}
}
/// Handle provider control commands
impl Provider for KvRedisProvider {
/// Provider should perform any operations needed for a new link,
/// including setting up per-actor resources, and checking authorization.
/// If the link is allowed, return true, otherwise return false to deny the link.
#[instrument(level = "debug", skip(self, config))]
async fn receive_link_config_as_target(
&self,
LinkConfig {
source_id, config, ..
}: LinkConfig<'_>,
) -> anyhow::Result<()> {
let conn = if let Some(url) = config
.keys()
.find(|k| k.eq_ignore_ascii_case(CONFIG_REDIS_URL_KEY))
.and_then(|url_key| config.get(url_key))
{
match redis::Client::open(url.to_string()) {
Ok(client) => match client.get_connection_manager().await {
Ok(conn) => {
info!(url, "established link");
conn
}
Err(err) => {
warn!(
url,
?err,
"Could not create Redis connection manager for source [{source_id}], keyvalue operations will fail",
);
bail!("failed to create redis connection manager");
}
},
Err(err) => {
warn!(
?err,
"Could not create Redis client for source [{source_id}], keyvalue operations will fail",
);
bail!("failed to create redis client");
}
}
} else {
self.get_default_connection().await.map_err(|err| {
error!(error = ?err, "failed to get default connection for link");
err
})?
};
let mut sources = self.sources.write().await;
sources.insert(source_id.to_string(), conn);
Ok(())
}
/// Handle notification that a link is dropped - close the connection
#[instrument(level = "info", skip(self))]
async fn delete_link(&self, source_id: &str) -> anyhow::Result<()> {
let mut aw = self.sources.write().await;
if let Some(conn) = aw.remove(source_id) {
debug!(
component_id = source_id,
"redis closing connection for component"
);
drop(conn);
}
Ok(())
}
/// Handle shutdown request by closing all connections
async fn shutdown(&self) -> anyhow::Result<()> {
let mut aw = self.sources.write().await;
// empty the actor link data and stop all servers
for (_, conn) in aw.drain() {
drop(conn);
}
Ok(())
}
}
/// Fetch the default URL to use for connecting to Redis from the configuration, defaulting
/// to `DEFAULT_CONNECT_URL` if no URL is found in the configuration.
pub fn retrieve_default_url(config: &HashMap<String, String>) -> String {
// To aid in user experience, find the URL key in the config that matches "URL" in a case-insensitive manner
let config_supplied_url = config
.keys()
.find(|k| k.eq_ignore_ascii_case(CONFIG_REDIS_URL_KEY))
.and_then(|url_key| config.get(url_key));
if let Some(url) = config_supplied_url {
debug!(url, "using Redis URL from config");
url.to_string()
} else {
debug!(DEFAULT_CONNECT_URL, "using default Redis URL");
DEFAULT_CONNECT_URL.to_string()
}
}
#[cfg(test)]
mod test {
use std::collections::HashMap;
use crate::retrieve_default_url;
const PROPER_URL: &str = "redis://127.0.0.1:6379";
#[test]
fn can_deserialize_config_case_insensitive() {
let lowercase_config = HashMap::from_iter([("url".to_string(), PROPER_URL.to_string())]);
let uppercase_config = HashMap::from_iter([("URL".to_string(), PROPER_URL.to_string())]);
let initial_caps_config = HashMap::from_iter([("Url".to_string(), PROPER_URL.to_string())]);
assert_eq!(PROPER_URL, retrieve_default_url(&lowercase_config));
assert_eq!(PROPER_URL, retrieve_default_url(&uppercase_config));
assert_eq!(PROPER_URL, retrieve_default_url(&initial_caps_config));
}
}