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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! Redis Bridge
//!
//! Forwards messages between local Redis (on-robot) and control plane Redis (cloud).
//! This enables K8s-like distributed architecture where nodes can be scheduled
//! on robots or in the cloud.
//!
//! # Architecture
//!
//! ```text
//! Robot:
//! Local Redis (localhost:6379)
//! ↓↑ (bridged topics)
//! RedisBridge
//! ↓↑ (network)
//! Cloud:
//! Control Plane Redis (shared across fleet)
//! ↓↑
//! Remote Nodes (K8s deployments)
//! ```
//!
//! # Topic Routing
//!
//! - **Local topics**: Stay on local Redis (e.g., `/sensor/camera/rgb` - high frequency)
//! - **Bridged topics**: Forwarded to control plane (e.g., `/system/heartbeat`, `/telemetry/*`)
//! - **Wildcard subscriptions**: Remote nodes can subscribe to `*/camera/rgb` (all robots)
//!
//! # Configuration
//!
//! ```yaml
//! bridge:
//! enabled: true
//! local_topics:
//! - "/sensor/camera/*" # Keep local (high frequency)
//! - "/sensor/lidar/*" # Keep local
//! bridged_topics:
//! - "/system/*" # Bridge to cloud
//! - "/telemetry/*" # Bridge to cloud
//! - "/logs/*" # Bridge to cloud
//! ```
use crate::error::{Mecha10Error, Result};
use glob::Pattern;
use mecha10_messaging::MessageBus;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tokio::sync::RwLock;
use tokio::task::JoinHandle;
use tracing::{debug, error, info, warn};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BridgeConfig {
pub enabled: bool,
pub local_topics: Vec<String>,
pub bridged_topics: Vec<String>,
#[serde(default)]
pub robot_id: Option<String>,
}
impl Default for BridgeConfig {
fn default() -> Self {
Self {
enabled: true,
local_topics: vec![
"/sensor/camera/*".to_string(),
"/sensor/lidar/*".to_string(),
"/sensor/imu/*".to_string(),
],
bridged_topics: vec![
"/system/*".to_string(),
"/telemetry/*".to_string(),
"/logs/*".to_string(),
"/fleet/*".to_string(),
],
robot_id: None,
}
}
}
impl BridgeConfig {
/// Check if a topic should be bridged to control plane
pub fn should_bridge(&self, topic: &str) -> bool {
if !self.enabled {
return false;
}
let matches_bridged = self.bridged_topics.iter().any(|pattern| {
if let Ok(glob) = Pattern::new(pattern) {
glob.matches(topic)
} else {
warn!("Invalid glob pattern in bridged_topics: {}", pattern);
false
}
});
let matches_local = self.local_topics.iter().any(|pattern| {
if let Ok(glob) = Pattern::new(pattern) {
glob.matches(topic)
} else {
warn!("Invalid glob pattern in local_topics: {}", pattern);
false
}
});
matches_bridged && !matches_local
}
/// Get the prefixed topic for control plane
/// Example: "/camera/rgb" → "robot-123/camera/rgb"
pub fn get_control_plane_topic(&self, topic: &str) -> String {
if let Some(robot_id) = &self.robot_id {
format!("{}{}", robot_id, topic)
} else {
topic.to_string()
}
}
}
struct ForwarderTask {
handle: JoinHandle<()>,
topic: String,
}
impl ForwarderTask {
fn new(handle: JoinHandle<()>, topic: String) -> Self {
Self { handle, topic }
}
fn abort(&self) {
self.handle.abort();
}
}
pub struct RedisBridge {
config: BridgeConfig,
local_bus: Arc<RwLock<MessageBus>>,
control_plane_bus: Arc<RwLock<MessageBus>>,
tasks: Arc<RwLock<Vec<ForwarderTask>>>,
shutdown: Arc<RwLock<bool>>,
}
impl RedisBridge {
/// Create a new Redis bridge
///
/// # Arguments
///
/// * `config` - Bridge configuration
/// * `local_bus` - Local Redis message bus
/// * `control_plane_bus` - Control plane Redis message bus
///
/// # Example
///
/// ```rust,no_run
/// # use mecha10_core::redis_bridge::{BridgeConfig, RedisBridge};
/// # use mecha10_messaging::MessageBus;
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let config = BridgeConfig::default();
/// let local_bus = MessageBus::connect("redis://localhost:6379", "robot-1").await?;
/// let cp_bus = MessageBus::connect("redis://control-plane:6379", "robot-1").await?;
///
/// let bridge = RedisBridge::new(config, local_bus, cp_bus);
/// bridge.start().await?;
/// # Ok(())
/// # }
/// ```
pub fn new(config: BridgeConfig, local_bus: MessageBus, control_plane_bus: MessageBus) -> Self {
Self {
config,
local_bus: Arc::new(RwLock::new(local_bus)),
control_plane_bus: Arc::new(RwLock::new(control_plane_bus)),
tasks: Arc::new(RwLock::new(Vec::new())),
shutdown: Arc::new(RwLock::new(false)),
}
}
/// Start the bridge
///
/// Spawns forwarder tasks for each bridged topic pattern.
/// Messages matching bridged patterns will be forwarded from local to control plane.
pub async fn start(&self) -> Result<()> {
if !self.config.enabled {
info!("Redis bridge is disabled in configuration");
return Ok(());
}
info!(
"Starting Redis bridge with {} bridged topic patterns",
self.config.bridged_topics.len()
);
for pattern in &self.config.bridged_topics {
self.start_forwarder(pattern).await?;
}
info!("Redis bridge started successfully");
Ok(())
}
/// Start a forwarder task for a specific topic pattern
async fn start_forwarder(&self, pattern: &str) -> Result<()> {
debug!("Starting forwarder for pattern: {}", pattern);
let topic = pattern.to_string();
let config = self.config.clone();
let local_bus = Arc::clone(&self.local_bus);
let control_plane_bus = Arc::clone(&self.control_plane_bus);
let shutdown = Arc::clone(&self.shutdown);
let handle = tokio::spawn(async move {
if let Err(e) = Self::forward_messages(topic.clone(), config, local_bus, control_plane_bus, shutdown).await
{
error!("Forwarder task error for topic {}: {}", topic, e);
}
});
let mut tasks = self.tasks.write().await;
tasks.push(ForwarderTask::new(handle, pattern.to_string()));
Ok(())
}
/// Forward messages from local to control plane
#[allow(clippy::too_many_arguments)]
async fn forward_messages(
topic: String,
config: BridgeConfig,
local_bus: Arc<RwLock<MessageBus>>,
control_plane_bus: Arc<RwLock<MessageBus>>,
shutdown: Arc<RwLock<bool>>,
) -> Result<()> {
let mut local = local_bus.write().await;
let mut subscriber = local
.subscribe::<serde_json::Value>(&topic, "bridge")
.await
.map_err(|e| Mecha10Error::MessagingError {
message: format!("Failed to subscribe to topic {}: {}", topic, e),
suggestion: "Check local Redis connection".to_string(),
})?;
drop(local);
debug!("Subscribed to local topic: {}", topic);
while !*shutdown.read().await {
if let Some(msg) = subscriber.recv().await {
debug!("Received message on topic {} from {}", msg.topic, msg.publisher);
let cp_topic = config.get_control_plane_topic(&topic);
let mut cp_bus = control_plane_bus.write().await;
if let Err(e) = cp_bus.publish(&cp_topic, &msg.payload).await {
warn!(
"Failed to forward message from {} to control plane topic {}: {}",
topic, cp_topic, e
);
} else {
debug!("Forwarded message to control plane topic: {}", cp_topic);
}
drop(cp_bus);
if let Err(e) = msg.ack().await {
warn!("Failed to acknowledge message on topic {}: {}", topic, e);
}
}
}
debug!("Forwarder task stopped for topic: {}", topic);
Ok(())
}
/// Stop the bridge gracefully
///
/// Stops all forwarder tasks and waits for them to complete.
pub async fn stop(&self) -> Result<()> {
info!("Stopping Redis bridge...");
*self.shutdown.write().await = true;
let tasks = self.tasks.read().await;
for task in tasks.iter() {
debug!("Stopping forwarder task for topic: {}", task.topic);
task.abort();
}
drop(tasks);
self.tasks.write().await.clear();
info!("Redis bridge stopped");
Ok(())
}
/// Check if the bridge is running
pub async fn is_running(&self) -> bool {
!*self.shutdown.read().await
}
/// Get the number of active forwarder tasks
pub async fn active_tasks(&self) -> usize {
self.tasks.read().await.len()
}
}