mod.rs 3.25 KB
Newer Older
Ryan Olson's avatar
Ryan Olson committed
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
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! KVBM Runtime - composed infrastructure for kvbm operations.
//!
//! The runtime contains the minimal shared components needed to construct
//! all downstream managers and services:
//! - Tokio runtime (for async execution)
//! - NixlAgent (for RDMA/UCX transfers)
//! - Nova (for distributed RPC)
//!
//! # Usage
//!
//! ```rust,ignore
//! // Build from environment (leader role)
//! let runtime = KvbmRuntime::from_env_leader().await?;
//!
//! // Build with custom config and injected components
//! let config = KvbmConfig::extract_from(
//!     KvbmConfig::figment()
//!         .merge(("nova.backend.tcp_port", 8080u16))
//! )?;
//! let runtime = KvbmRuntime::builder(config)
//!     .with_runtime_handle(Handle::current())
//!     .build_leader()
//!     .await?;
//!
//! // Use runtime components
//! let transfer_mgr = TransferManager::builder()
//!     .nixl_agent(runtime.nixl_agent().clone())
//!     .event_system(runtime.event_system().clone())
//!     .build()?;
//! ```

mod builder;

pub use builder::{KvbmRuntimeBuilder, RuntimeHandle};

use std::sync::Arc;

use dynamo_memory::nixl::NixlAgent;
use kvbm_config::KvbmConfig;
use tokio::runtime::Handle;
use velo::Messenger;

/// KVBM Runtime - composed infrastructure for kvbm operations.
///
/// Contains the minimal shared components needed to construct
/// all downstream managers and services:
/// - Tokio runtime (for async execution)
/// - NixlAgent (for RDMA/UCX transfers)
/// - Nova (for distributed RPC)
///
/// The `LocalEventSystem` is available via `event_system()` which
/// returns the system from Nova.
pub struct KvbmRuntime {
    pub(crate) config: KvbmConfig,
    pub(crate) runtime: RuntimeHandle,
    pub(crate) messenger: Arc<Messenger>,
    pub(crate) nixl_agent: Option<NixlAgent>,
}

impl KvbmRuntime {
    /// Create a builder for customized construction.
    pub fn builder(config: KvbmConfig) -> KvbmRuntimeBuilder {
        KvbmRuntimeBuilder::new(config)
    }

    /// Quick construction from environment (for leader role).
    pub async fn from_env_leader() -> anyhow::Result<Self> {
        KvbmRuntimeBuilder::from_env()?.build_leader().await
    }

    /// Quick construction from environment (for worker role).
    pub async fn from_env_worker() -> anyhow::Result<Self> {
        KvbmRuntimeBuilder::from_env()?.build_worker().await
    }

    /// Get the configuration.
    pub fn config(&self) -> &KvbmConfig {
        &self.config
    }

    /// Get the tokio runtime handle.
    pub fn handle(&self) -> Handle {
        self.runtime.handle()
    }

    /// Get the tokio runtime handle (convenience alias for handle()).
    pub fn tokio(&self) -> Handle {
        self.handle()
    }

    /// Get Messenger.
    pub fn messenger(&self) -> &Arc<Messenger> {
        &self.messenger
    }

    /// Get NixlAgent for RDMA/UCX transfers.
    /// Returns None if NixL is disabled in config.
    pub fn nixl_agent(&self) -> Option<&NixlAgent> {
        self.nixl_agent.as_ref()
    }

    /// Get the event manager for worker coordination and transfer notifications.
    pub fn event_system(&self) -> Arc<velo::EventManager> {
        Arc::new(self.messenger.event_manager())
    }
}