Skip to main content

kvbm_engine/runtime/
mod.rs

1// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! KVBM Runtime - composed infrastructure for kvbm operations.
5//!
6//! The runtime contains the minimal shared components needed to construct
7//! all downstream managers and services:
8//! - Tokio runtime (for async execution)
9//! - NixlAgent (for RDMA/UCX transfers)
10//! - Nova (for distributed RPC)
11//!
12//! # Usage
13//!
14//! ```rust,ignore
15//! // Build from environment (leader role)
16//! let runtime = KvbmRuntime::from_env_leader().await?;
17//!
18//! // Build with custom config and injected components
19//! let config = KvbmConfig::extract_from(
20//!     KvbmConfig::figment()
21//!         .merge(("nova.backend.tcp_port", 8080u16))
22//! )?;
23//! let runtime = KvbmRuntime::builder(config)
24//!     .with_runtime_handle(Handle::current())
25//!     .build_leader()
26//!     .await?;
27//!
28//! // Use runtime components
29//! let transfer_mgr = TransferManager::builder()
30//!     .nixl_agent(runtime.nixl_agent().clone())
31//!     .event_system(runtime.event_system().clone())
32//!     .build()?;
33//! ```
34
35mod builder;
36
37pub use builder::{KvbmRuntimeBuilder, RuntimeHandle};
38
39use std::sync::Arc;
40
41use dynamo_memory::nixl::NixlAgent;
42use kvbm_config::KvbmConfig;
43use tokio::runtime::Handle;
44use velo::Messenger;
45
46/// KVBM Runtime - composed infrastructure for kvbm operations.
47///
48/// Contains the minimal shared components needed to construct
49/// all downstream managers and services:
50/// - Tokio runtime (for async execution)
51/// - NixlAgent (for RDMA/UCX transfers)
52/// - Nova (for distributed RPC)
53///
54/// The `LocalEventSystem` is available via `event_system()` which
55/// returns the system from Nova.
56pub struct KvbmRuntime {
57    pub(crate) config: KvbmConfig,
58    pub(crate) runtime: RuntimeHandle,
59    pub(crate) messenger: Arc<Messenger>,
60    pub(crate) nixl_agent: Option<NixlAgent>,
61}
62
63impl KvbmRuntime {
64    /// Create a builder for customized construction.
65    pub fn builder(config: KvbmConfig) -> KvbmRuntimeBuilder {
66        KvbmRuntimeBuilder::new(config)
67    }
68
69    /// Quick construction from environment (for leader role).
70    pub async fn from_env_leader() -> anyhow::Result<Self> {
71        KvbmRuntimeBuilder::from_env()?.build_leader().await
72    }
73
74    /// Quick construction from environment (for worker role).
75    pub async fn from_env_worker() -> anyhow::Result<Self> {
76        KvbmRuntimeBuilder::from_env()?.build_worker().await
77    }
78
79    /// Get the configuration.
80    pub fn config(&self) -> &KvbmConfig {
81        &self.config
82    }
83
84    /// Get the tokio runtime handle.
85    pub fn handle(&self) -> Handle {
86        self.runtime.handle()
87    }
88
89    /// Get the tokio runtime handle (convenience alias for handle()).
90    pub fn tokio(&self) -> Handle {
91        self.handle()
92    }
93
94    /// Get Messenger.
95    pub fn messenger(&self) -> &Arc<Messenger> {
96        &self.messenger
97    }
98
99    /// Get NixlAgent for RDMA/UCX transfers.
100    /// Returns None if NixL is disabled in config.
101    pub fn nixl_agent(&self) -> Option<&NixlAgent> {
102        self.nixl_agent.as_ref()
103    }
104
105    /// Get the event manager for worker coordination and transfer notifications.
106    pub fn event_system(&self) -> Arc<velo::EventManager> {
107        Arc::new(self.messenger.event_manager())
108    }
109}