Skip to main content

vv_agent/runtime/backends/distributed/
backend.rs

1use std::sync::Arc;
2
3use crate::runtime::state::StateStore;
4
5use super::super::RuntimeRecipe;
6use super::dispatch::CycleDispatcher;
7
8const DEFAULT_CYCLE_NAME: &str = "vv_agent.distributed.run_single_cycle";
9
10#[derive(Clone)]
11pub struct DistributedBackend {
12    pub(super) runtime_recipe: Option<RuntimeRecipe>,
13    pub(super) state_store: Option<Arc<dyn StateStore>>,
14    pub(super) cycle_dispatcher: Option<Arc<dyn CycleDispatcher>>,
15    pub(super) cycle_name: String,
16}
17
18impl std::fmt::Debug for DistributedBackend {
19    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        formatter
21            .debug_struct("DistributedBackend")
22            .field("runtime_recipe", &self.runtime_recipe)
23            .field("has_state_store", &self.state_store.is_some())
24            .field("has_cycle_dispatcher", &self.cycle_dispatcher.is_some())
25            .field("cycle_name", &self.cycle_name)
26            .finish()
27    }
28}
29
30impl DistributedBackend {
31    pub fn inline_fallback() -> Self {
32        Self {
33            runtime_recipe: None,
34            state_store: None,
35            cycle_dispatcher: None,
36            cycle_name: DEFAULT_CYCLE_NAME.to_string(),
37        }
38    }
39
40    pub fn distributed(runtime_recipe: RuntimeRecipe) -> Self {
41        Self {
42            runtime_recipe: Some(runtime_recipe),
43            state_store: None,
44            cycle_dispatcher: None,
45            cycle_name: DEFAULT_CYCLE_NAME.to_string(),
46        }
47    }
48
49    pub fn distributed_with_dispatcher(
50        runtime_recipe: RuntimeRecipe,
51        state_store: Arc<dyn StateStore>,
52        cycle_dispatcher: Arc<dyn CycleDispatcher>,
53    ) -> Self {
54        Self {
55            runtime_recipe: Some(runtime_recipe),
56            state_store: Some(state_store),
57            cycle_dispatcher: Some(cycle_dispatcher),
58            cycle_name: DEFAULT_CYCLE_NAME.to_string(),
59        }
60    }
61
62    pub fn with_cycle_name(mut self, cycle_name: impl Into<String>) -> Self {
63        self.cycle_name = cycle_name.into();
64        self
65    }
66
67    pub fn runtime_recipe(&self) -> Option<&RuntimeRecipe> {
68        self.runtime_recipe.as_ref()
69    }
70
71    pub fn state_store(&self) -> Option<&Arc<dyn StateStore>> {
72        self.state_store.as_ref()
73    }
74
75    pub fn cycle_name(&self) -> &str {
76        &self.cycle_name
77    }
78
79    pub fn parallel_map<T, R, F>(&self, function: F, items: Vec<T>) -> Vec<R>
80    where
81        F: Fn(T) -> R,
82    {
83        items.into_iter().map(function).collect()
84    }
85}