|
| 1 | +use dashmap::DashMap; |
| 2 | +use once_cell::sync::Lazy; |
| 3 | +use std::collections::HashMap; |
| 4 | +use std::sync::atomic::AtomicU64; |
| 5 | +use std::sync::{Arc, Mutex}; |
| 6 | +use tokio::runtime::Runtime as TokioRuntime; |
| 7 | +use tokio::time::Duration; |
| 8 | + |
| 9 | +pub mod buffer; |
| 10 | +pub mod logging; |
| 11 | +pub mod mailbox; |
| 12 | +pub mod network; |
| 13 | +pub mod pid; |
| 14 | +pub mod registry; |
| 15 | +pub mod supervisor; |
| 16 | + |
| 17 | +pub mod behavior; |
| 18 | +pub mod send; |
| 19 | +pub mod spawn; |
| 20 | +pub mod types; |
| 21 | + |
| 22 | +#[cfg(feature = "vortex")] |
| 23 | +pub mod vortex; |
| 24 | +#[cfg(feature = "vortex")] |
| 25 | +pub mod vortex_rt; |
| 26 | + |
| 27 | +use pid::Pid; |
| 28 | +use types::VirtualActorSpec; |
| 29 | + |
| 30 | +#[cfg(feature = "vortex")] |
| 31 | +use vortex::{VortexEngine, VortexGhostPolicy}; |
| 32 | + |
| 33 | +/// A global, multi-threaded Tokio runtime shared by all Iris instances. |
| 34 | +pub(crate) static RUNTIME: Lazy<TokioRuntime> = Lazy::new(|| { |
| 35 | + tokio::runtime::Builder::new_multi_thread() |
| 36 | + .enable_all() |
| 37 | + .build() |
| 38 | + .expect("Failed to create Iris Tokio Runtime") |
| 39 | +}); |
| 40 | + |
| 41 | +/// Lightweight runtime for spawning actors and managing distributed nodes. |
| 42 | +#[derive(Clone)] |
| 43 | +pub struct Runtime { |
| 44 | + pub(crate) slab: Arc<Mutex<pid::SlabAllocator>>, |
| 45 | + pub(crate) mailboxes: Arc<DashMap<Pid, mailbox::MailboxSender>>, |
| 46 | + pub(crate) supervisor: Arc<supervisor::Supervisor>, |
| 47 | + pub(crate) observers: Arc<DashMap<Pid, Arc<Mutex<Vec<mailbox::Message>>>>>, |
| 48 | + pub(crate) network: Arc<Mutex<Option<network::NetworkManager>>>, |
| 49 | + // network configuration (timeouts/limits/backoff) |
| 50 | + pub(crate) network_io_timeout: Arc<Mutex<Duration>>, |
| 51 | + pub(crate) network_max_payload: Arc<Mutex<usize>>, |
| 52 | + pub(crate) network_max_name_len: Arc<Mutex<usize>>, |
| 53 | + pub(crate) monitor_backoff_factor: Arc<Mutex<f64>>, |
| 54 | + pub(crate) monitor_backoff_max: Arc<Mutex<Duration>>, |
| 55 | + pub(crate) monitor_failure_threshold: Arc<Mutex<usize>>, |
| 56 | + #[cfg(feature = "vortex")] |
| 57 | + pub(crate) vortex_engine: Option<Arc<Mutex<VortexEngine>>>, |
| 58 | + #[cfg(feature = "vortex")] |
| 59 | + pub(crate) vortex_watcher: Option<Arc<vortex::VortexWatcher>>, |
| 60 | + pub(crate) registry: Arc<registry::NameRegistry>, |
| 61 | + /// Mapping for locally‑spawned proxies that forward to remote actors. |
| 62 | + pub(crate) remote_proxies: Arc<DashMap<Pid, (String, Pid)>>, |
| 63 | + /// Reverse lookup from (address, remote_pid) -> local proxy PID. |
| 64 | + pub(crate) proxy_by_remote: Arc<DashMap<(String, Pid), Pid>>, |
| 65 | + /// Behavior version per actor PID (starts at 1). |
| 66 | + pub(crate) behavior_versions: Arc<DashMap<Pid, u64>>, |
| 67 | + /// Recent hot-swapped pointers used for rollback (capped). |
| 68 | + pub(crate) behavior_history: Arc<DashMap<Pid, Vec<usize>>>, |
| 69 | + /// Optional per-path supervisors (shallow supervisors keyed by path). |
| 70 | + pub(crate) path_supervisors: Arc<DashMap<String, Arc<supervisor::Supervisor>>>, |
| 71 | + /// Maps a child PID to its parent PID for structured concurrency. |
| 72 | + pub(crate) parent_of: Arc<DashMap<Pid, Pid>>, |
| 73 | + /// Tracks direct children of each parent PID. |
| 74 | + pub(crate) children_by_parent: Arc<DashMap<Pid, Vec<Pid>>>, |
| 75 | + /// Capacity for bounded mailboxes. |
| 76 | + pub(crate) bounded_capacity: Arc<DashMap<Pid, usize>>, |
| 77 | + /// Overflow policies for bounded mailboxes; default is DropNew if absent. |
| 78 | + pub(crate) overflow_policy: Arc<DashMap<Pid, mailbox::OverflowPolicy>>, |
| 79 | + /// Lazy/virtual actor specs reserved by PID and activated on first send. |
| 80 | + pub(crate) virtual_specs: Arc<DashMap<Pid, VirtualActorSpec>>, |
| 81 | + /// Per-virtual-actor activation lock to prevent duplicate activation races. |
| 82 | + pub(crate) virtual_activate_locks: Arc<DashMap<Pid, Arc<Mutex<()>>>>, |
| 83 | + /// Track last known backpressure level for each pid, to emit signals on change. |
| 84 | + pub(crate) backpressure_state: Arc<DashMap<Pid, mailbox::BackpressureLevel>>, |
| 85 | + // Runtime-configurable limits for Python GIL-release behavior |
| 86 | + pub(crate) release_gil_max_threads: Arc<Mutex<usize>>, |
| 87 | + pub(crate) gil_pool_size: Arc<Mutex<usize>>, |
| 88 | + pub(crate) release_gil_strict: Arc<Mutex<bool>>, |
| 89 | + // Timers: map from timer id -> cancellation sender |
| 90 | + pub(crate) timers: Arc<Mutex<HashMap<u64, tokio::sync::oneshot::Sender<()>>>>, |
| 91 | + pub(crate) timer_counter: Arc<AtomicU64>, |
| 92 | + #[cfg(feature = "vortex")] |
| 93 | + pub(crate) vortex_ghost_counter: Arc<AtomicU64>, |
| 94 | + #[cfg(feature = "vortex")] |
| 95 | + pub(crate) vortex_auto_replay_count: Arc<AtomicU64>, |
| 96 | + #[cfg(feature = "vortex")] |
| 97 | + pub(crate) vortex_auto_primary_wins: Arc<AtomicU64>, |
| 98 | + #[cfg(feature = "vortex")] |
| 99 | + pub(crate) vortex_auto_ghost_wins: Arc<AtomicU64>, |
| 100 | + #[cfg(feature = "vortex")] |
| 101 | + pub(crate) vortex_auto_policy: Arc<Mutex<VortexGhostPolicy>>, |
| 102 | + #[cfg(feature = "vortex")] |
| 103 | + pub(crate) vortex_genetic_budgeting_enabled: Arc<Mutex<bool>>, |
| 104 | + #[cfg(feature = "vortex")] |
| 105 | + pub(crate) vortex_genetic_thresholds: Arc<Mutex<(f64, f64)>>, |
| 106 | + #[cfg(feature = "vortex")] |
| 107 | + pub(crate) vortex_isolation_disallowed_ops: Arc<Mutex<std::collections::HashSet<u8>>>, |
| 108 | + #[cfg(feature = "vortex")] |
| 109 | + pub(crate) vortex_genetic_history: Arc<DashMap<Pid, (usize, usize)>>, |
| 110 | +} |
| 111 | + |
| 112 | +impl Default for Runtime { |
| 113 | + fn default() -> Self { |
| 114 | + Self::new() |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl Runtime { |
| 119 | + /// Create a new runtime instance and initialize the networking and registry sub-systems. |
| 120 | + pub fn new() -> Self { |
| 121 | + logging::init_logger(); |
| 122 | + |
| 123 | + #[cfg(feature = "pyo3")] |
| 124 | + { |
| 125 | + pyo3::prepare_freethreaded_python(); |
| 126 | + } |
| 127 | + |
| 128 | + let rt = Runtime { |
| 129 | + slab: Arc::new(Mutex::new(pid::SlabAllocator::new())), |
| 130 | + mailboxes: Arc::new(DashMap::new()), |
| 131 | + supervisor: Arc::new(supervisor::Supervisor::new()), |
| 132 | + observers: Arc::new(DashMap::new()), |
| 133 | + network: Arc::new(Mutex::new(None)), |
| 134 | + registry: Arc::new(registry::NameRegistry::new()), |
| 135 | + path_supervisors: Arc::new(DashMap::new()), |
| 136 | + parent_of: Arc::new(DashMap::new()), |
| 137 | + children_by_parent: Arc::new(DashMap::new()), |
| 138 | + bounded_capacity: Arc::new(DashMap::new()), |
| 139 | + overflow_policy: Arc::new(DashMap::new()), |
| 140 | + backpressure_state: Arc::new(DashMap::new()), |
| 141 | + virtual_specs: Arc::new(DashMap::new()), |
| 142 | + virtual_activate_locks: Arc::new(DashMap::new()), |
| 143 | + release_gil_max_threads: Arc::new(Mutex::new(0)), |
| 144 | + gil_pool_size: Arc::new(Mutex::new(8)), |
| 145 | + release_gil_strict: Arc::new(Mutex::new(false)), |
| 146 | + timers: Arc::new(Mutex::new(HashMap::new())), |
| 147 | + timer_counter: Arc::new(AtomicU64::new(0)), |
| 148 | + #[cfg(feature = "vortex")] |
| 149 | + vortex_ghost_counter: Arc::new(AtomicU64::new(1)), |
| 150 | + #[cfg(feature = "vortex")] |
| 151 | + vortex_auto_replay_count: Arc::new(AtomicU64::new(0)), |
| 152 | + #[cfg(feature = "vortex")] |
| 153 | + vortex_auto_primary_wins: Arc::new(AtomicU64::new(0)), |
| 154 | + #[cfg(feature = "vortex")] |
| 155 | + vortex_auto_ghost_wins: Arc::new(AtomicU64::new(0)), |
| 156 | + #[cfg(feature = "vortex")] |
| 157 | + vortex_auto_policy: Arc::new(Mutex::new(VortexGhostPolicy::FirstSafePointWins)), |
| 158 | + #[cfg(feature = "vortex")] |
| 159 | + vortex_genetic_budgeting_enabled: Arc::new(Mutex::new(false)), |
| 160 | + #[cfg(feature = "vortex")] |
| 161 | + vortex_genetic_thresholds: Arc::new(Mutex::new((0.4, 0.7))), |
| 162 | + #[cfg(feature = "vortex")] |
| 163 | + vortex_isolation_disallowed_ops: Arc::new(Mutex::new(std::collections::HashSet::new())), |
| 164 | + #[cfg(feature = "vortex")] |
| 165 | + vortex_genetic_history: Arc::new(DashMap::new()), |
| 166 | + network_io_timeout: Arc::new(Mutex::new(Duration::from_secs(5))), |
| 167 | + network_max_payload: Arc::new(Mutex::new(1024 * 1024)), |
| 168 | + network_max_name_len: Arc::new(Mutex::new(1024)), |
| 169 | + monitor_backoff_factor: Arc::new(Mutex::new(2.0)), |
| 170 | + monitor_backoff_max: Arc::new(Mutex::new(Duration::from_secs(60))), |
| 171 | + monitor_failure_threshold: Arc::new(Mutex::new(1)), |
| 172 | + remote_proxies: Arc::new(DashMap::new()), |
| 173 | + proxy_by_remote: Arc::new(DashMap::new()), |
| 174 | + behavior_versions: Arc::new(DashMap::new()), |
| 175 | + behavior_history: Arc::new(DashMap::new()), |
| 176 | + #[cfg(feature = "vortex")] |
| 177 | + vortex_engine: Some(Arc::new(Mutex::new(VortexEngine::new()))), |
| 178 | + #[cfg(feature = "vortex")] |
| 179 | + vortex_watcher: Some(Arc::new(vortex::VortexWatcher::new())), |
| 180 | + }; |
| 181 | + |
| 182 | + let net_manager = network::NetworkManager::new(Arc::new(rt.clone())); |
| 183 | + *rt.network.lock().unwrap() = Some(net_manager); |
| 184 | + |
| 185 | + rt |
| 186 | + } |
| 187 | + |
| 188 | + /// Set runtime limits for GIL release handling. |
| 189 | + /// |
| 190 | + /// `max_threads = 0` forces pooled mode (no dedicated thread per actor). |
| 191 | + pub fn set_release_gil_limits(&self, max_threads: usize, pool_size: usize) { |
| 192 | + *self.release_gil_max_threads.lock().unwrap() = max_threads; |
| 193 | + *self.gil_pool_size.lock().unwrap() = pool_size; |
| 194 | + } |
| 195 | + |
| 196 | + /// Enable or disable strict failure mode: when true, spawning an actor with |
| 197 | + /// `release_gil=true` will return an error if the dedicated-thread limit is exceeded. |
| 198 | + pub fn set_release_gil_strict(&self, strict: bool) { |
| 199 | + *self.release_gil_strict.lock().unwrap() = strict; |
| 200 | + } |
| 201 | + |
| 202 | + /// Get the current release_gil limits (max_threads, pool_size). |
| 203 | + pub fn get_release_gil_limits(&self) -> (usize, usize) { |
| 204 | + ( |
| 205 | + *self.release_gil_max_threads.lock().unwrap(), |
| 206 | + *self.gil_pool_size.lock().unwrap(), |
| 207 | + ) |
| 208 | + } |
| 209 | + |
| 210 | + /// Returns whether strict failure mode is enabled. |
| 211 | + pub fn is_release_gil_strict(&self) -> bool { |
| 212 | + *self.release_gil_strict.lock().unwrap() |
| 213 | + } |
| 214 | +} |
0 commit comments