vv_agent/runtime/backends/distributed/
backend.rs1use 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 with_cycle_task_name(self, cycle_task_name: impl Into<String>) -> Self {
68 self.with_cycle_name(cycle_task_name)
69 }
70
71 pub fn runtime_recipe(&self) -> Option<&RuntimeRecipe> {
72 self.runtime_recipe.as_ref()
73 }
74
75 pub fn state_store(&self) -> Option<&Arc<dyn StateStore>> {
76 self.state_store.as_ref()
77 }
78
79 pub fn cycle_name(&self) -> &str {
80 &self.cycle_name
81 }
82
83 pub fn cycle_task_name(&self) -> &str {
84 self.cycle_name()
85 }
86
87 pub fn parallel_map<T, R, F>(&self, function: F, items: Vec<T>) -> Vec<R>
88 where
89 F: Fn(T) -> R,
90 {
91 items.into_iter().map(function).collect()
92 }
93}