kvbm_engine/runtime/
builder.rs1use std::sync::Arc;
7
8use anyhow::Result;
9use dynamo_memory::nixl::NixlAgent;
10use kvbm_config::KvbmConfig;
11use tokio::runtime::{Handle, Runtime};
12use velo::Messenger;
13
14pub enum RuntimeHandle {
16 Owned(Arc<Runtime>),
18 Handle(Handle),
20}
21
22impl RuntimeHandle {
23 pub fn handle(&self) -> Handle {
25 match self {
26 RuntimeHandle::Owned(rt) => rt.handle().clone(),
27 RuntimeHandle::Handle(h) => h.clone(),
28 }
29 }
30}
31
32pub struct KvbmRuntimeBuilder {
38 config: KvbmConfig,
39 runtime: Option<RuntimeHandle>,
40 messenger: Option<Arc<Messenger>>,
41 nixl_agent: Option<NixlAgent>,
42}
43
44impl KvbmRuntimeBuilder {
45 pub fn new(config: KvbmConfig) -> Self {
47 Self {
48 config,
49 runtime: None,
50 messenger: None,
51 nixl_agent: None,
52 }
53 }
54
55 pub fn from_env() -> Result<Self, kvbm_config::ConfigError> {
57 Ok(Self::new(KvbmConfig::from_env()?))
58 }
59
60 pub fn from_json(json: &str) -> Result<Self, kvbm_config::ConfigError> {
65 Ok(Self::new(KvbmConfig::from_figment_with_json(json)?))
66 }
67
68 pub fn with_runtime(mut self, runtime: Arc<Runtime>) -> Self {
70 self.runtime = Some(RuntimeHandle::Owned(runtime));
71 self
72 }
73
74 pub fn with_runtime_handle(mut self, handle: Handle) -> Self {
76 self.runtime = Some(RuntimeHandle::Handle(handle));
77 self
78 }
79
80 pub fn with_messenger(mut self, messenger: Arc<Messenger>) -> Self {
82 self.messenger = Some(messenger);
83 self
84 }
85
86 pub fn with_nixl_agent(mut self, agent: NixlAgent) -> Self {
88 self.nixl_agent = Some(agent);
89 self
90 }
91
92 pub async fn build_leader(self) -> Result<super::KvbmRuntime> {
94 self.build_internal().await
95 }
96
97 pub async fn build_worker(self) -> Result<super::KvbmRuntime> {
99 self.build_internal().await
100 }
101
102 async fn build_internal(self) -> Result<super::KvbmRuntime> {
103 let runtime = match self.runtime {
105 Some(rt) => rt,
106 None => RuntimeHandle::Owned(Arc::new(self.config.tokio.build_runtime()?)),
107 };
108
109 let messenger = match self.messenger {
111 Some(m) => m,
112 None => self.config.messenger.build_messenger().await?,
113 };
114
115 let nixl_agent = match self.nixl_agent {
118 Some(agent) => Some(agent),
119 None => match &self.config.nixl {
120 Some(nixl_config) => {
121 let agent_name = format!("nixl-{}", messenger.instance_id());
122 let backend_config = nixl_config.clone().into();
123 Some(NixlAgent::from_nixl_backend_config(
124 &agent_name,
125 backend_config,
126 )?)
127 }
128 None => None, },
130 };
131
132 Ok(super::KvbmRuntime {
133 config: self.config,
134 runtime,
135 messenger,
136 nixl_agent,
137 })
138 }
139}