Skip to main content

apiary_runtime/
lib.rs

1//! Apiary runtime — node lifecycle, bee pool, and swarm coordination.
2//!
3//! This crate contains the [`ApiaryNode`] which is the main entry point
4//! for starting and running an Apiary compute node, the [`BeePool`]
5//! which manages isolated execution chambers (mason bee pattern), and the
6//! heartbeat / world view system for multi-node awareness.
7
8pub mod bee;
9pub mod behavioral;
10pub mod cache;
11pub mod heartbeat;
12pub mod node;
13
14pub use apiary_query::ApiaryQueryContext;
15pub use bee::{BeePool, BeeState, BeeStatus, MasonChamber};
16pub use behavioral::{
17    AbandonmentDecision, AbandonmentTracker, ColonyThermometer, TemperatureRegulation,
18};
19pub use cache::{CacheEntry, CellCache};
20pub use heartbeat::{
21    Heartbeat, HeartbeatWriter, NodeState, NodeStatus, WorldView, WorldViewBuilder,
22};
23pub use node::{ApiaryNode, ColonyStatus, SwarmNodeInfo, SwarmStatus};
24
25/// Convert WorldView to a vector of NodeInfo for distributed query planning.
26pub fn world_view_to_node_info(world_view: &WorldView) -> Vec<apiary_query::distributed::NodeInfo> {
27    world_view
28        .alive_nodes()
29        .iter()
30        .map(|node| {
31            // Extract cached cells from heartbeat
32            // Note: This clones the HashMap, which is acceptable for v1 since:
33            // 1. Heartbeats are updated every 5 seconds, not on every query
34            // 2. Typical cache sizes are small (dozens to hundreds of cells)
35            // 3. Query planning is not on the critical path
36            // Future optimization: use Arc<HashMap> if this becomes a bottleneck
37            let cached_cells = node.heartbeat.cache.cached_cells.clone();
38
39            apiary_query::distributed::NodeInfo {
40                node_id: node.node_id.clone(),
41                state: match node.state {
42                    NodeState::Alive => apiary_query::distributed::NodeState::Alive,
43                    NodeState::Suspect => apiary_query::distributed::NodeState::Suspect,
44                    NodeState::Dead => apiary_query::distributed::NodeState::Dead,
45                },
46                cores: node.heartbeat.capacity.cores,
47                memory_bytes: node.heartbeat.capacity.memory_total_bytes,
48                memory_per_bee: node.heartbeat.capacity.memory_per_bee,
49                target_cell_size: node.heartbeat.capacity.target_cell_size,
50                bees_total: node.heartbeat.load.bees_total,
51                bees_busy: node.heartbeat.load.bees_busy,
52                idle_bees: node.heartbeat.load.bees_idle,
53                cached_cells,
54            }
55        })
56        .collect()
57}