orcher-sdk 0.7.0

Rust SDK for Orcher, a durable workflow engine: workflows, tasks, actors, workers and a client
Documentation
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! Low-level client for reading and writing actor state.
//!
//! [`ActorStateClient`] talks to the Orcher server over gRPC, or to an in-memory
//! [`MockStateBackend`] in tests. The server is the source of truth; the worker only
//! keeps an optional read cache.
//!
//! ## Architecture
//!
//! ```text
//! Worker (SDK)                    Server
//! ┌─────────────────┐            ┌──────────────────┐
//! │ ActorContext    │            │  ActorService    │
//! │   ↓             │            │                  │
//! │ StateManager    │   gRPC     │  State storage   │
//! │   ↓             │  ──────>   │                  │
//! │ ActorStateClient│            │                  │
//! └─────────────────┘            └──────────────────┘
//! ```
//!
//! State calls use the `GetState`, `SetState`, `DeleteState`, and `ListStateKeys`
//! RPCs of the `orcher.v1.ActorService` gRPC service.

use crate::error::{Error, Result};
use moka::future::Cache;
use orcher_proto::orcher::v1::{
    actor_service_client::ActorServiceClient, DeleteStateRequest, GetStateRequest,
    ListStateKeysRequest, SetStateRequest,
};
use serde::{de::DeserializeOwned, Serialize};
use std::sync::Arc;
use std::time::Duration;
use tonic::transport::{Channel, Endpoint};

use super::mock::MockStateBackend;

/// Configuration for [`ActorStateClient`].
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ActorStateClientConfig {
    /// Server URL, such as `"http://localhost:8080"`.
    pub server_url: String,

    /// Per-request timeout.
    pub timeout: Duration,

    /// Not used by this client: failed requests are not retried.
    pub max_retries: u32,

    /// Cache state values in the worker.
    ///
    /// Only this client's own writes and deletes update the cache, so a cached
    /// value can be stale if another worker writes the same key.
    pub enable_cache: bool,

    /// How long a cached value is kept.
    pub cache_ttl: Duration,

    /// Maximum number of cached entries; the least recently used are evicted first.
    pub max_cache_size: u64,

    /// Not sent by this client.
    pub auth_token: Option<String>,
}

impl Default for ActorStateClientConfig {
    fn default() -> Self {
        Self {
            server_url: "http://localhost:8080".to_string(),
            timeout: Duration::from_secs(10),
            max_retries: 3,
            enable_cache: true,
            cache_ttl: Duration::from_secs(60),
            max_cache_size: 1_000,
            auth_token: None,
        }
    }
}

/// Reads and writes actor state on the server, with an optional local cache.
///
/// Values are JSON-encoded. Each call carries the execution ID of the running
/// operation. Cloning is cheap; clones share the connection and the cache.
///
/// # Examples
///
/// ```rust,no_run
/// use orcher_sdk::actor::{ActorStateClient, ActorStateClientConfig};
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> orcher_sdk::Result<()> {
/// let mut config = ActorStateClientConfig::default();
/// config.server_url = "http://localhost:50051".to_string();
/// let client = ActorStateClient::new(config).await?;
///
/// let items: Vec<String> = client
///     .get_state("ShoppingCart", "user-123", "cart", "exec-456")
///     .await?
///     .unwrap_or_default();
///
/// client
///     .set_state("ShoppingCart", "user-123", "cart", &items, "exec-456", None)
///     .await?;
/// # Ok(())
/// # }
/// ```
pub struct ActorStateClient {
    backend: StateBackend,

    config: ActorStateClientConfig,

    /// Keyed by `{actor_name}:{key}:{state_key}`; `None` when caching is disabled.
    cache: Option<Arc<Cache<String, Vec<u8>>>>,
}

/// Where state is actually stored.
enum StateBackend {
    /// The Orcher server.
    Grpc(ActorServiceClient<Channel>),

    /// In-memory storage for tests.
    Mock(MockStateBackend),
}

impl ActorStateClient {
    /// Connects to the server described by `config`.
    ///
    /// The connection attempt itself times out after 5 seconds.
    ///
    /// # Errors
    ///
    /// Returns an error if the URL is invalid or the connection fails.
    pub async fn new(config: ActorStateClientConfig) -> Result<Self> {
        let endpoint = Endpoint::from_shared(config.server_url.clone())
            .map_err(|e| Error::Network(format!("Invalid server URL: {}", e)))?
            .timeout(config.timeout)
            .connect_timeout(Duration::from_secs(5));

        let channel = endpoint
            .connect()
            .await
            .map_err(|e| Error::Network(format!("Failed to connect to server: {}", e)))?;

        // State up to the configured message limit, not tonic's 4 MiB.
        let max = orcher_sdk_core::limits::default_max_message_bytes();
        let grpc_client = ActorServiceClient::new(channel)
            .max_decoding_message_size(max)
            .max_encoding_message_size(max);

        let cache = if config.enable_cache {
            Some(Arc::new(
                Cache::builder()
                    .max_capacity(config.max_cache_size)
                    .time_to_live(config.cache_ttl)
                    .build(),
            ))
        } else {
            None
        };

        Ok(Self {
            backend: StateBackend::Grpc(grpc_client),
            config,
            cache,
        })
    }

    /// Creates a client backed by fresh in-memory storage, for tests.
    ///
    /// No server is needed and caching is disabled.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use orcher_sdk::actor::ActorStateClient;
    ///
    /// # #[tokio::main(flavor = "current_thread")]
    /// # async fn main() -> orcher_sdk::Result<()> {
    /// let client = ActorStateClient::new_mock();
    ///
    /// // Behaves like a server-backed client.
    /// client.set_state("Counter", "key1", "count", &3i64, "exec-1", None).await?;
    /// assert_eq!(client.get_state::<i64>("Counter", "key1", "count", "exec-1").await?, Some(3));
    /// # Ok(())
    /// # }
    /// ```
    pub fn new_mock() -> Self {
        Self {
            backend: StateBackend::Mock(MockStateBackend::new()),
            config: ActorStateClientConfig::default(),
            cache: None, // Cache disabled so tests always observe the backend.
        }
    }

    /// Creates a test client over an existing in-memory backend.
    ///
    /// Clients built from clones of the same backend share storage, so a test
    /// can inspect or seed state that handlers see.
    pub fn new_mock_with_backend(backend: MockStateBackend) -> Self {
        Self {
            backend: StateBackend::Mock(backend),
            config: ActorStateClientConfig::default(),
            cache: None,
        }
    }

    /// Reads `state_key` of the instance `key` of `actor_name`.
    ///
    /// Returns `None` if the key does not exist or holds an empty value. Served from
    /// the cache when possible; a value fetched from the server is cached.
    ///
    /// # Errors
    ///
    /// Returns an error if the RPC fails or the value cannot be decoded as `T`.
    pub async fn get_state<T>(
        &self,
        actor_name: &str,
        key: &str,
        state_key: &str,
        execution_id: &str,
    ) -> Result<Option<T>>
    where
        T: DeserializeOwned,
    {
        let cache_key = format!("{}:{}:{}", actor_name, key, state_key);

        if let Some(cache) = &self.cache {
            if let Some(cached_bytes) = cache.get(&cache_key).await {
                return match serde_json::from_slice(&cached_bytes) {
                    Ok(value) => Ok(Some(value)),
                    Err(e) => Err(Error::Serialization(format!(
                        "Failed to deserialize cached state: {}",
                        e
                    ))),
                };
            }
        }

        let value_bytes = match &self.backend {
            StateBackend::Grpc(client) => {
                let request = tonic::Request::new(GetStateRequest {
                    actor_name: actor_name.to_string(),
                    key: key.to_string(),
                    state_key: state_key.to_string(),
                    execution_id: execution_id.to_string(),
                });

                let mut grpc_client = client.clone();
                let response = grpc_client
                    .get_state(request)
                    .await
                    .map_err(|e| Error::Network(format!("gRPC GetState failed: {}", e)))?;

                let state_response = response.into_inner();

                // An empty value is treated as absent.
                if !state_response.exists || state_response.value.is_empty() {
                    return Ok(None);
                }

                state_response.value
            }
            StateBackend::Mock(mock) => {
                let storage_key = format!("actor:{}:{}:{}", actor_name, key, state_key);
                match mock.get(&storage_key).await {
                    Some(bytes) => bytes,
                    None => return Ok(None),
                }
            }
        };

        if let Some(cache) = &self.cache {
            cache.insert(cache_key, value_bytes.clone()).await;
        }

        serde_json::from_slice(&value_bytes)
            .map(Some)
            .map_err(|e| Error::Serialization(format!("Failed to deserialize state: {}", e)))
    }

    /// Reads a state value together with its version.
    ///
    /// Pass the version to [`set_state`](Self::set_state) as `expected_version` for an
    /// optimistic-concurrency write. Always reads from the backend, bypassing the cache.
    /// If the key does not exist, the value is `None` and the version is whatever the
    /// server reports (empty with the mock backend, which has no versions).
    ///
    /// # Errors
    ///
    /// Returns an error if the RPC fails or the value cannot be decoded as `T`.
    pub async fn get_state_with_version<T>(
        &self,
        actor_name: &str,
        key: &str,
        state_key: &str,
        execution_id: &str,
    ) -> Result<(Option<T>, String)>
    where
        T: DeserializeOwned,
    {
        match &self.backend {
            StateBackend::Grpc(client) => {
                let request = tonic::Request::new(GetStateRequest {
                    actor_name: actor_name.to_string(),
                    key: key.to_string(),
                    state_key: state_key.to_string(),
                    execution_id: execution_id.to_string(),
                });

                let mut grpc_client = client.clone();
                let response = grpc_client
                    .get_state(request)
                    .await
                    .map_err(|e| Error::Network(format!("gRPC GetState failed: {}", e)))?;

                let state_response = response.into_inner();
                let version = state_response.version.clone();

                if !state_response.exists || state_response.value.is_empty() {
                    return Ok((None, version));
                }

                let value = serde_json::from_slice(&state_response.value).map_err(|e| {
                    Error::Serialization(format!("Failed to deserialize state: {}", e))
                })?;

                Ok((Some(value), version))
            }
            StateBackend::Mock(mock) => {
                let storage_key = format!("actor:{}:{}:{}", actor_name, key, state_key);
                match mock.get(&storage_key).await {
                    Some(bytes) => {
                        let value = serde_json::from_slice(&bytes).map_err(|e| {
                            Error::Serialization(format!("Failed to deserialize state: {}", e))
                        })?;
                        Ok((Some(value), String::new()))
                    }
                    None => Ok((None, String::new())),
                }
            }
        }
    }

    /// Writes `value` to `state_key` of the instance `key` of `actor_name`.
    ///
    /// With `expected_version`, the server only applies the write if the stored
    /// version matches. The mock backend ignores it. On success the cache is updated.
    ///
    /// # Errors
    ///
    /// Returns an error if `value` cannot be encoded, the RPC fails, or the server
    /// rejects the write (including a version mismatch).
    pub async fn set_state<T>(
        &self,
        actor_name: &str,
        key: &str,
        state_key: &str,
        value: &T,
        execution_id: &str,
        expected_version: Option<&str>,
    ) -> Result<()>
    where
        T: Serialize,
    {
        let cache_key = format!("{}:{}:{}", actor_name, key, state_key);

        let value_bytes = serde_json::to_vec(value)
            .map_err(|e| Error::Serialization(format!("Failed to serialize state: {}", e)))?;

        match &self.backend {
            StateBackend::Grpc(client) => {
                let request = tonic::Request::new(SetStateRequest {
                    actor_name: actor_name.to_string(),
                    key: key.to_string(),
                    state_key: state_key.to_string(),
                    value: value_bytes.clone(),
                    execution_id: execution_id.to_string(),
                    expected_version: expected_version.unwrap_or("").to_string(),
                });

                let mut grpc_client = client.clone();
                let response = grpc_client
                    .set_state(request)
                    .await
                    .map_err(|e| Error::Network(format!("gRPC SetState failed: {}", e)))?;

                let set_response = response.into_inner();
                if !set_response.success {
                    return Err(Error::Actor(format!(
                        "Failed to set state: {}",
                        set_response.error_message
                    )));
                }
            }
            StateBackend::Mock(mock) => {
                let storage_key = format!("actor:{}:{}:{}", actor_name, key, state_key);
                mock.set(&storage_key, value_bytes.clone()).await;
            }
        }

        if let Some(cache) = &self.cache {
            cache.insert(cache_key, value_bytes).await;
        }

        Ok(())
    }

    /// Deletes `state_key` of the instance `key` of `actor_name`.
    ///
    /// Returns whether the key existed. The cache entry is dropped before the
    /// RPC, so it is gone even if the RPC fails.
    ///
    /// # Errors
    ///
    /// Returns an error if the RPC fails.
    pub async fn delete_state(
        &self,
        actor_name: &str,
        key: &str,
        state_key: &str,
        execution_id: &str,
    ) -> Result<bool> {
        let cache_key = format!("{}:{}:{}", actor_name, key, state_key);

        if let Some(cache) = &self.cache {
            cache.remove(&cache_key).await;
        }

        match &self.backend {
            StateBackend::Grpc(client) => {
                let request = tonic::Request::new(DeleteStateRequest {
                    actor_name: actor_name.to_string(),
                    key: key.to_string(),
                    state_key: state_key.to_string(),
                    execution_id: execution_id.to_string(),
                });

                let mut grpc_client = client.clone();
                let response = grpc_client
                    .delete_state(request)
                    .await
                    .map_err(|e| Error::Network(format!("gRPC DeleteState failed: {}", e)))?;

                let delete_response = response.into_inner();
                Ok(delete_response.existed)
            }
            StateBackend::Mock(mock) => {
                let storage_key = format!("actor:{}:{}:{}", actor_name, key, state_key);
                Ok(mock.delete(&storage_key).await)
            }
        }
    }

    /// Lists the state keys of the instance `key` of `actor_name`.
    ///
    /// With `prefix`, only keys starting with it are returned. Keys are returned
    /// without the `actor:{actor_name}:{key}:` storage prefix. Always reads from the
    /// backend.
    ///
    /// # Errors
    ///
    /// Returns an error if the RPC fails.
    pub async fn list_state_keys(
        &self,
        actor_name: &str,
        key: &str,
        execution_id: &str,
        prefix: Option<&str>,
    ) -> Result<Vec<String>> {
        match &self.backend {
            StateBackend::Grpc(client) => {
                let request = tonic::Request::new(ListStateKeysRequest {
                    actor_name: actor_name.to_string(),
                    key: key.to_string(),
                    execution_id: execution_id.to_string(),
                    prefix: prefix.unwrap_or_default().to_string(),
                });

                let mut grpc_client = client.clone();
                let response = grpc_client
                    .list_state_keys(request)
                    .await
                    .map_err(|e| Error::Network(format!("gRPC ListStateKeys failed: {}", e)))?;

                let list_response = response.into_inner();

                Ok(list_response.keys)
            }
            StateBackend::Mock(mock) => {
                let prefix_str = format!("actor:{}:{}:{}", actor_name, key, prefix.unwrap_or(""));
                let keys = mock.list_keys(&prefix_str).await;

                // Return bare state keys, matching what the server returns.
                let state_keys = keys
                    .into_iter()
                    .filter_map(|k| {
                        k.strip_prefix(&format!("actor:{}:{}:", actor_name, key))
                            .map(|s| s.to_string())
                    })
                    .collect();

                Ok(state_keys)
            }
        }
    }

    /// Drops every cached value.
    pub async fn clear_cache(&self) {
        if let Some(cache) = &self.cache {
            cache.invalidate_all();
        }
    }

    /// Drops the cached value of one state key, so the next read goes to the backend.
    pub async fn invalidate_cache(&self, actor_name: &str, key: &str, state_key: &str) {
        if let Some(cache) = &self.cache {
            let cache_key = format!("{}:{}:{}", actor_name, key, state_key);
            cache.remove(&cache_key).await;
        }
    }
}

impl Clone for ActorStateClient {
    fn clone(&self) -> Self {
        Self {
            backend: match &self.backend {
                StateBackend::Grpc(client) => StateBackend::Grpc(client.clone()),
                StateBackend::Mock(mock) => StateBackend::Mock(mock.clone()),
            },
            config: self.config.clone(),
            cache: self.cache.clone(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_default() {
        let config = ActorStateClientConfig::default();
        assert_eq!(config.server_url, "http://localhost:8080");
        assert!(config.enable_cache);
    }

    #[test]
    fn test_cache_key_format() {
        let cache_key = format!("{}:{}:{}", "ShoppingCart", "user-123", "cart");
        assert_eq!(cache_key, "ShoppingCart:user-123:cart");
    }

    #[tokio::test]
    async fn test_client_config() {
        let config = ActorStateClientConfig {
            server_url: "http://localhost:9999".to_string(),
            timeout: Duration::from_secs(5),
            enable_cache: false,
            ..Default::default()
        };

        assert_eq!(config.server_url, "http://localhost:9999");
        assert!(!config.enable_cache);
    }
}