heliosdb-proxy 0.4.1

HeliosProxy - Intelligent connection router and failover manager for HeliosDB and PostgreSQL
Documentation
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! Query Router
//!
//! Routes queries to appropriate nodes based on hints and policies.

use super::{
    HintParser, ParsedHints, RouteTarget,
    NodeFilter, NodeCriteria, NodeInfo, FilterResult,
    RoutingConfig, RoutingError, RoutingMetrics, Result,
};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

/// Query router - routes queries to appropriate nodes
pub struct QueryRouter {
    /// Hint parser
    parser: HintParser,
    /// Node filter
    filter: NodeFilter,
    /// Available nodes
    nodes: Arc<RwLock<Vec<NodeInfo>>>,
    /// Routing metrics
    metrics: Arc<RoutingMetrics>,
    /// Configuration
    config: RoutingConfig,
    /// Round-robin counter for load balancing
    rr_counter: std::sync::atomic::AtomicU64,
}

impl QueryRouter {
    /// Create a new query router
    pub fn new(config: RoutingConfig) -> Self {
        let filter = NodeFilter::new(config.clone());

        Self {
            parser: HintParser::new(),
            filter,
            nodes: Arc::new(RwLock::new(Vec::new())),
            metrics: Arc::new(RoutingMetrics::new()),
            config,
            rr_counter: std::sync::atomic::AtomicU64::new(0),
        }
    }

    /// Route a query
    pub async fn route(&self, query: &str) -> RoutingDecision {
        let start = Instant::now();

        // Parse hints
        let hints = self.parser.parse(query);

        // Validate hints
        if let Err(e) = hints.validate() {
            self.metrics.record_invalid_hints();
            return RoutingDecision::error(e.to_string());
        }

        // Determine if this is a write query
        let is_write = self.is_write_query(query);

        // Build criteria from hints
        let mut criteria = if !hints.is_empty() {
            NodeCriteria::from_hints(&hints)
        } else if is_write {
            self.filter.default_criteria_for_write()
        } else {
            self.filter.default_criteria_for_read()
        };

        // For writes without explicit routing, force primary
        if is_write && criteria.route.is_none() {
            criteria.route = Some(RouteTarget::Primary);
        }

        // Get nodes and filter
        let nodes = self.nodes.read().await;
        let filter_result = self.filter.filter(&nodes, &criteria);

        // Build decision
        let decision = if filter_result.has_matches() {
            let selected = self.select_node(&filter_result);
            self.metrics.record_routing(
                criteria.route,
                !hints.is_empty(),
                start.elapsed(),
            );

            RoutingDecision {
                target_node: Some(selected.name.clone()),
                hints: hints.clone(),
                reason: RoutingReason::Routed {
                    target: criteria.route,
                    filters_applied: filter_result.reasons.clone(),
                },
                elapsed: start.elapsed(),
                is_write,
            }
        } else {
            // No matching nodes - try fallback
            let fallback = self.try_fallback(&nodes, is_write);

            if let Some(node) = fallback {
                self.metrics.record_fallback();
                RoutingDecision {
                    target_node: Some(node.name.clone()),
                    hints: hints.clone(),
                    reason: RoutingReason::Fallback {
                        original_filters: filter_result.reasons.clone(),
                    },
                    elapsed: start.elapsed(),
                    is_write,
                }
            } else {
                self.metrics.record_no_nodes();
                RoutingDecision {
                    target_node: None,
                    hints: hints.clone(),
                    reason: RoutingReason::NoNodes {
                        filters: filter_result.reasons.clone(),
                    },
                    elapsed: start.elapsed(),
                    is_write,
                }
            }
        };

        decision
    }

    /// Route with explicit hints (for use by other modules)
    pub async fn route_with_criteria(&self, criteria: &NodeCriteria) -> Result<String> {
        let nodes = self.nodes.read().await;
        let filter_result = self.filter.filter(&nodes, criteria);

        filter_result
            .require_match("routing")
            .map(|n| n.name.clone())
    }

    /// Check if query is a write operation
    pub fn is_write_query(&self, query: &str) -> bool {
        if !self.config.default.auto_detect_writes {
            return false;
        }

        let upper = query.trim().to_uppercase();
        let first_word = upper.split_whitespace().next().unwrap_or("");

        matches!(
            first_word,
            "INSERT" | "UPDATE" | "DELETE" | "CREATE" | "ALTER" | "DROP" |
            "TRUNCATE" | "GRANT" | "REVOKE" | "MERGE" | "UPSERT" |
            "BEGIN" | "START" | "COMMIT" | "ROLLBACK" | "SAVEPOINT" |
            "LOCK" | "PREPARE" | "EXECUTE" | "DEALLOCATE"
        )
    }

    /// Select a node from eligible nodes using load balancing
    fn select_node<'a>(&self, result: &FilterResult<'a>) -> &'a NodeInfo {
        if result.eligible.is_empty() {
            panic!("select_node called with no eligible nodes");
        }

        if result.eligible.len() == 1 {
            return result.eligible[0];
        }

        // Simple round-robin for now
        let idx = self.rr_counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        let selected_idx = (idx as usize) % result.eligible.len();
        result.eligible[selected_idx]
    }

    /// Try to find a fallback node
    fn try_fallback<'a>(&self, nodes: &'a [NodeInfo], is_write: bool) -> Option<&'a NodeInfo> {
        if is_write {
            // For writes, only primary is acceptable
            nodes.iter().find(|n| n.role == super::node_filter::NodeRole::Primary && n.healthy)
        } else {
            // For reads, try any healthy node
            nodes.iter().find(|n| n.healthy && n.enabled)
        }
    }

    /// Strip hints from query for backend execution
    pub fn strip_hints(&self, query: &str) -> String {
        if self.config.hints.strip_hints {
            self.parser.strip(query)
        } else {
            query.to_string()
        }
    }

    /// Parse hints from query (for external use)
    pub fn parse_hints(&self, query: &str) -> ParsedHints {
        self.parser.parse(query)
    }

    /// Add a node
    pub async fn add_node(&self, node: NodeInfo) {
        self.nodes.write().await.push(node);
    }

    /// Remove a node by name
    pub async fn remove_node(&self, name: &str) {
        self.nodes.write().await.retain(|n| n.name != name);
    }

    /// Update node state
    pub async fn update_node<F>(&self, name: &str, f: F)
    where
        F: FnOnce(&mut NodeInfo),
    {
        let mut nodes = self.nodes.write().await;
        if let Some(node) = nodes.iter_mut().find(|n| n.name == name) {
            f(node);
        }
    }

    /// Get metrics
    pub fn metrics(&self) -> &RoutingMetrics {
        &self.metrics
    }

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

/// Routing decision result
#[derive(Debug, Clone)]
pub struct RoutingDecision {
    /// Target node name (None if no node available)
    pub target_node: Option<String>,
    /// Parsed hints from query
    pub hints: ParsedHints,
    /// Reason for routing decision
    pub reason: RoutingReason,
    /// Time taken to make decision
    pub elapsed: Duration,
    /// Whether this is a write query
    pub is_write: bool,
}

impl RoutingDecision {
    /// Create an error decision
    pub fn error(message: String) -> Self {
        Self {
            target_node: None,
            hints: ParsedHints::default(),
            reason: RoutingReason::Error { message },
            elapsed: Duration::ZERO,
            is_write: false,
        }
    }

    /// Check if routing succeeded
    pub fn is_success(&self) -> bool {
        self.target_node.is_some()
    }

    /// Get the target node or error
    pub fn require_target(&self) -> Result<&str> {
        self.target_node
            .as_deref()
            .ok_or_else(|| RoutingError::NoMatchingNodes(self.reason.to_string()))
    }

    /// Get a summary string
    pub fn summary(&self) -> String {
        match &self.reason {
            RoutingReason::Routed { target, .. } => {
                format!(
                    "Routed to {} ({:?}) in {:?}",
                    self.target_node.as_deref().unwrap_or("unknown"),
                    target,
                    self.elapsed
                )
            }
            RoutingReason::Fallback { .. } => {
                format!(
                    "Fallback to {} in {:?}",
                    self.target_node.as_deref().unwrap_or("unknown"),
                    self.elapsed
                )
            }
            RoutingReason::NoNodes { filters } => {
                format!("No nodes available (filters: {:?})", filters)
            }
            RoutingReason::Error { message } => {
                format!("Error: {}", message)
            }
        }
    }
}

/// Reason for routing decision
#[derive(Debug, Clone)]
pub enum RoutingReason {
    /// Successfully routed
    Routed {
        target: Option<RouteTarget>,
        filters_applied: Vec<String>,
    },
    /// Fallback used due to no matches
    Fallback {
        original_filters: Vec<String>,
    },
    /// No nodes available
    NoNodes {
        filters: Vec<String>,
    },
    /// Error occurred
    Error {
        message: String,
    },
}

impl std::fmt::Display for RoutingReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RoutingReason::Routed { target, .. } => {
                write!(f, "routed to {:?}", target)
            }
            RoutingReason::Fallback { .. } => {
                write!(f, "fallback")
            }
            RoutingReason::NoNodes { filters } => {
                write!(f, "no nodes ({})", filters.join(", "))
            }
            RoutingReason::Error { message } => {
                write!(f, "error: {}", message)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use super::super::node_filter::SyncMode;

    async fn setup_router() -> QueryRouter {
        let router = QueryRouter::new(RoutingConfig::default());

        // Add test nodes
        router.add_node(NodeInfo::primary("primary")).await;
        router.add_node(NodeInfo::standby("standby-sync-1", SyncMode::Sync)).await;
        router.add_node(NodeInfo::standby("standby-async-1", SyncMode::Async)
            .with_lag(100)).await;
        router.add_node(NodeInfo::standby("standby-async-2", SyncMode::Async)
            .with_lag(200)).await;

        router
    }

    #[tokio::test]
    async fn test_route_read_query() {
        let router = setup_router().await;

        let decision = router.route("SELECT * FROM users").await;

        assert!(decision.is_success());
        assert!(!decision.is_write);
    }

    #[tokio::test]
    async fn test_route_write_query() {
        let router = setup_router().await;

        let decision = router.route("INSERT INTO users (name) VALUES ('test')").await;

        assert!(decision.is_success());
        assert!(decision.is_write);
        assert_eq!(decision.target_node.as_deref(), Some("primary"));
    }

    #[tokio::test]
    async fn test_route_with_primary_hint() {
        let router = setup_router().await;

        let decision = router.route("/*helios:route=primary*/ SELECT * FROM users").await;

        assert!(decision.is_success());
        assert_eq!(decision.target_node.as_deref(), Some("primary"));
    }

    #[tokio::test]
    async fn test_route_with_sync_hint() {
        let router = setup_router().await;

        let decision = router.route("/*helios:route=sync*/ SELECT * FROM users").await;

        assert!(decision.is_success());
        assert_eq!(decision.target_node.as_deref(), Some("standby-sync-1"));
    }

    #[tokio::test]
    async fn test_route_with_node_hint() {
        let router = setup_router().await;

        let decision = router.route("/*helios:node=standby-async-1*/ SELECT * FROM users").await;

        assert!(decision.is_success());
        assert_eq!(decision.target_node.as_deref(), Some("standby-async-1"));
    }

    #[tokio::test]
    async fn test_route_with_lag_hint() {
        let router = setup_router().await;

        let decision = router.route("/*helios:route=async,lag=150ms*/ SELECT * FROM users").await;

        assert!(decision.is_success());
        // Should only match standby-async-1 (100ms lag)
        assert_eq!(decision.target_node.as_deref(), Some("standby-async-1"));
    }

    #[tokio::test]
    async fn test_route_no_matching_nodes() {
        let router = setup_router().await;

        let decision = router.route("/*helios:node=nonexistent*/ SELECT * FROM users").await;

        // Should fallback
        assert!(decision.is_success()); // Fallback finds a node
    }

    #[tokio::test]
    async fn test_is_write_query() {
        let router = QueryRouter::new(RoutingConfig::default());

        assert!(router.is_write_query("INSERT INTO users VALUES (1)"));
        assert!(router.is_write_query("UPDATE users SET name = 'test'"));
        assert!(router.is_write_query("DELETE FROM users"));
        assert!(router.is_write_query("CREATE TABLE test (id INT)"));
        assert!(router.is_write_query("BEGIN"));
        assert!(router.is_write_query("COMMIT"));

        assert!(!router.is_write_query("SELECT * FROM users"));
        assert!(!router.is_write_query("WITH cte AS (SELECT 1) SELECT * FROM cte"));
    }

    #[tokio::test]
    async fn test_strip_hints() {
        let router = QueryRouter::new(RoutingConfig::default());

        let stripped = router.strip_hints("/*helios:route=primary*/ SELECT * FROM users");
        assert_eq!(stripped, "SELECT * FROM users");
    }

    #[tokio::test]
    async fn test_invalid_hint_combination() {
        let router = setup_router().await;

        let decision = router.route(
            "/*helios:route=async,consistency=strong*/ SELECT * FROM users"
        ).await;

        // Should return error due to invalid combination
        assert!(!decision.is_success());
    }

    #[tokio::test]
    async fn test_metrics_tracking() {
        let router = setup_router().await;

        // Make some routing decisions
        router.route("SELECT * FROM users").await;
        router.route("/*helios:route=primary*/ SELECT * FROM accounts").await;
        router.route("INSERT INTO users VALUES (1)").await;

        let stats = router.metrics().snapshot();
        assert!(stats.total_routed >= 3);
        assert!(stats.with_hints >= 1);
    }
}