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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! Host Functions
//!
//! Host functions that plugins can call to interact with the proxy.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};

use parking_lot::RwLock;

/// Host function registry
pub struct HostFunctionRegistry {
    /// Registered functions by namespace
    functions: RwLock<HashMap<String, HashMap<String, HostFunction>>>,

    /// Call statistics
    stats: RwLock<HostFunctionStats>,
}

impl HostFunctionRegistry {
    /// Create a new registry with default functions
    pub fn new() -> Self {
        let registry = Self {
            functions: RwLock::new(HashMap::new()),
            stats: RwLock::new(HostFunctionStats::default()),
        };

        // Register default functions
        registry.register_defaults();
        registry
    }

    /// Register default host functions
    fn register_defaults(&self) {
        // Helios namespace - core functions
        self.register("helios", "log", HostFunction::Log);
        self.register("helios", "metric_inc", HostFunction::MetricInc);
        self.register("helios", "metric_gauge", HostFunction::MetricGauge);
        self.register("helios", "get_config", HostFunction::GetConfig);
        self.register("helios", "get_time", HostFunction::GetTime);

        // Query namespace
        self.register("query", "execute", HostFunction::QueryExecute);
        self.register("query", "prepare", HostFunction::QueryPrepare);
        self.register("query", "get_tables", HostFunction::QueryGetTables);
        self.register("query", "normalize", HostFunction::QueryNormalize);

        // Cache namespace
        self.register("cache", "get", HostFunction::CacheGet);
        self.register("cache", "set", HostFunction::CacheSet);
        self.register("cache", "delete", HostFunction::CacheDelete);
        self.register("cache", "exists", HostFunction::CacheExists);

        // HTTP namespace (requires http_fetch permission)
        self.register("http", "fetch", HostFunction::HttpFetch);
        self.register("http", "post", HostFunction::HttpPost);

        // Crypto namespace
        self.register("crypto", "hash", HostFunction::CryptoHash);
        self.register("crypto", "hmac", HostFunction::CryptoHmac);
        self.register("crypto", "random", HostFunction::CryptoRandom);

        // KV namespace (plugin-local storage)
        self.register("kv", "get", HostFunction::KvGet);
        self.register("kv", "set", HostFunction::KvSet);
        self.register("kv", "delete", HostFunction::KvDelete);
        self.register("kv", "list", HostFunction::KvList);
    }

    /// Register a host function
    pub fn register(&self, namespace: &str, name: &str, function: HostFunction) {
        let mut functions = self.functions.write();
        functions
            .entry(namespace.to_string())
            .or_insert_with(HashMap::new)
            .insert(name.to_string(), function);
    }

    /// Get a host function
    pub fn get(&self, namespace: &str, name: &str) -> Option<HostFunction> {
        let functions = self.functions.read();
        functions
            .get(namespace)
            .and_then(|ns| ns.get(name))
            .cloned()
    }

    /// Check if a function exists
    pub fn exists(&self, namespace: &str, name: &str) -> bool {
        let functions = self.functions.read();
        functions
            .get(namespace)
            .map(|ns| ns.contains_key(name))
            .unwrap_or(false)
    }

    /// List all functions in a namespace
    pub fn list_namespace(&self, namespace: &str) -> Vec<String> {
        let functions = self.functions.read();
        functions
            .get(namespace)
            .map(|ns| ns.keys().cloned().collect())
            .unwrap_or_default()
    }

    /// List all namespaces
    pub fn list_namespaces(&self) -> Vec<String> {
        let functions = self.functions.read();
        functions.keys().cloned().collect()
    }

    /// Record a function call
    pub fn record_call(&self, namespace: &str, name: &str, duration: Duration, success: bool) {
        let mut stats = self.stats.write();
        let key = format!("{}:{}", namespace, name);

        let entry = stats.calls.entry(key).or_insert_with(FunctionCallStats::default);
        entry.total_calls += 1;
        entry.total_duration += duration;

        if success {
            entry.successful_calls += 1;
        } else {
            entry.failed_calls += 1;
        }

        if duration > entry.max_duration {
            entry.max_duration = duration;
        }
    }

    /// Get call statistics
    pub fn get_stats(&self) -> HostFunctionStats {
        self.stats.read().clone()
    }
}

impl Default for HostFunctionRegistry {
    fn default() -> Self {
        Self::new()
    }
}

/// Host function types
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HostFunction {
    // Helios core
    Log,
    MetricInc,
    MetricGauge,
    GetConfig,
    GetTime,

    // Query operations
    QueryExecute,
    QueryPrepare,
    QueryGetTables,
    QueryNormalize,

    // Cache operations
    CacheGet,
    CacheSet,
    CacheDelete,
    CacheExists,

    // HTTP operations
    HttpFetch,
    HttpPost,

    // Crypto operations
    CryptoHash,
    CryptoHmac,
    CryptoRandom,

    // KV storage
    KvGet,
    KvSet,
    KvDelete,
    KvList,

    // Custom function
    Custom(String),
}

impl HostFunction {
    /// Get the required permission for this function
    pub fn required_permission(&self) -> Option<super::sandbox::Permission> {
        use super::sandbox::Permission;

        match self {
            // No permission required
            HostFunction::Log => None,
            HostFunction::GetTime => None,
            HostFunction::GetConfig => None,

            // Metrics permission
            HostFunction::MetricInc | HostFunction::MetricGauge => Some(Permission::Metrics),

            // Query permission
            HostFunction::QueryExecute
            | HostFunction::QueryPrepare
            | HostFunction::QueryGetTables
            | HostFunction::QueryNormalize => Some(Permission::QueryExecute),

            // Cache permissions
            HostFunction::CacheGet | HostFunction::CacheExists => Some(Permission::CacheRead),
            HostFunction::CacheSet | HostFunction::CacheDelete => Some(Permission::CacheWrite),

            // HTTP permission
            HostFunction::HttpFetch | HostFunction::HttpPost => Some(Permission::HttpFetch),

            // Crypto permission
            HostFunction::CryptoHash | HostFunction::CryptoHmac | HostFunction::CryptoRandom => {
                Some(Permission::Crypto)
            }

            // KV permissions
            HostFunction::KvGet | HostFunction::KvList => Some(Permission::KvRead),
            HostFunction::KvSet | HostFunction::KvDelete => Some(Permission::KvWrite),

            // Custom functions require custom permission
            HostFunction::Custom(_) => Some(Permission::Custom("custom".to_string())),
        }
    }

    /// Get the function signature (for documentation)
    pub fn signature(&self) -> &'static str {
        match self {
            HostFunction::Log => "log(level: i32, message_ptr: i32, message_len: i32)",
            HostFunction::MetricInc => "metric_inc(name_ptr: i32, name_len: i32, value: f64)",
            HostFunction::MetricGauge => "metric_gauge(name_ptr: i32, name_len: i32, value: f64)",
            HostFunction::GetConfig => "get_config(key_ptr: i32, key_len: i32) -> i32",
            HostFunction::GetTime => "get_time() -> i64",

            HostFunction::QueryExecute => {
                "query_execute(query_ptr: i32, query_len: i32) -> i32"
            }
            HostFunction::QueryPrepare => {
                "query_prepare(query_ptr: i32, query_len: i32) -> i32"
            }
            HostFunction::QueryGetTables => {
                "query_get_tables(query_ptr: i32, query_len: i32) -> i32"
            }
            HostFunction::QueryNormalize => {
                "query_normalize(query_ptr: i32, query_len: i32) -> i32"
            }

            HostFunction::CacheGet => "cache_get(key_ptr: i32, key_len: i32) -> i32",
            HostFunction::CacheSet => {
                "cache_set(key_ptr: i32, key_len: i32, value_ptr: i32, value_len: i32, ttl: i64)"
            }
            HostFunction::CacheDelete => "cache_delete(key_ptr: i32, key_len: i32)",
            HostFunction::CacheExists => "cache_exists(key_ptr: i32, key_len: i32) -> i32",

            HostFunction::HttpFetch => "http_fetch(url_ptr: i32, url_len: i32) -> i32",
            HostFunction::HttpPost => {
                "http_post(url_ptr: i32, url_len: i32, body_ptr: i32, body_len: i32) -> i32"
            }

            HostFunction::CryptoHash => {
                "crypto_hash(algo_ptr: i32, algo_len: i32, data_ptr: i32, data_len: i32) -> i32"
            }
            HostFunction::CryptoHmac => {
                "crypto_hmac(key_ptr: i32, key_len: i32, data_ptr: i32, data_len: i32) -> i32"
            }
            HostFunction::CryptoRandom => "crypto_random(len: i32) -> i32",

            HostFunction::KvGet => "kv_get(key_ptr: i32, key_len: i32) -> i32",
            HostFunction::KvSet => {
                "kv_set(key_ptr: i32, key_len: i32, value_ptr: i32, value_len: i32)"
            }
            HostFunction::KvDelete => "kv_delete(key_ptr: i32, key_len: i32)",
            HostFunction::KvList => "kv_list(prefix_ptr: i32, prefix_len: i32) -> i32",

            HostFunction::Custom(_) => "custom(...)",
        }
    }
}

/// Host function call statistics
#[derive(Debug, Clone, Default)]
pub struct HostFunctionStats {
    /// Per-function statistics
    pub calls: HashMap<String, FunctionCallStats>,
}

/// Per-function call statistics
#[derive(Debug, Clone, Default)]
pub struct FunctionCallStats {
    /// Total calls
    pub total_calls: u64,

    /// Successful calls
    pub successful_calls: u64,

    /// Failed calls
    pub failed_calls: u64,

    /// Total duration
    pub total_duration: Duration,

    /// Maximum duration
    pub max_duration: Duration,
}

impl FunctionCallStats {
    /// Get average duration
    pub fn avg_duration(&self) -> Duration {
        if self.total_calls == 0 {
            Duration::ZERO
        } else {
            self.total_duration / self.total_calls as u32
        }
    }

    /// Get success rate
    pub fn success_rate(&self) -> f64 {
        if self.total_calls == 0 {
            1.0
        } else {
            self.successful_calls as f64 / self.total_calls as f64
        }
    }
}

/// Host function context (passed to function implementations)
pub struct HostFunctionContext {
    /// Plugin name
    pub plugin_name: String,

    /// Request ID
    pub request_id: String,

    /// Plugin memory (for reading/writing)
    pub memory: Arc<RwLock<Vec<u8>>>,

    /// Plugin configuration
    pub config: HashMap<String, serde_json::Value>,

    /// Call start time
    pub start_time: Instant,
}

impl HostFunctionContext {
    /// Read a string from plugin memory
    pub fn read_string(&self, ptr: i32, len: i32) -> Result<String, HostFunctionError> {
        let memory = self.memory.read();
        let start = ptr as usize;
        let end = start + len as usize;

        if end > memory.len() {
            return Err(HostFunctionError::MemoryAccessError(
                "Read out of bounds".to_string(),
            ));
        }

        String::from_utf8(memory[start..end].to_vec())
            .map_err(|e| HostFunctionError::InvalidData(e.to_string()))
    }

    /// Read bytes from plugin memory
    pub fn read_bytes(&self, ptr: i32, len: i32) -> Result<Vec<u8>, HostFunctionError> {
        let memory = self.memory.read();
        let start = ptr as usize;
        let end = start + len as usize;

        if end > memory.len() {
            return Err(HostFunctionError::MemoryAccessError(
                "Read out of bounds".to_string(),
            ));
        }

        Ok(memory[start..end].to_vec())
    }

    /// Write bytes to plugin memory
    pub fn write_bytes(&self, ptr: i32, data: &[u8]) -> Result<(), HostFunctionError> {
        let mut memory = self.memory.write();
        let start = ptr as usize;
        let end = start + data.len();

        if end > memory.len() {
            return Err(HostFunctionError::MemoryAccessError(
                "Write out of bounds".to_string(),
            ));
        }

        memory[start..end].copy_from_slice(data);
        Ok(())
    }

    /// Allocate memory in plugin
    pub fn allocate(&self, size: usize) -> Result<i32, HostFunctionError> {
        let mut memory = self.memory.write();
        let ptr = memory.len() as i32;
        let new_size = memory.len() + size;
        memory.resize(new_size, 0);
        Ok(ptr)
    }

    /// Get elapsed time since call start
    pub fn elapsed(&self) -> Duration {
        self.start_time.elapsed()
    }
}

/// Host function error
#[derive(Debug, Clone)]
pub enum HostFunctionError {
    /// Memory access error
    MemoryAccessError(String),

    /// Invalid data
    InvalidData(String),

    /// Permission denied
    PermissionDenied(String),

    /// Function not found
    FunctionNotFound(String),

    /// Execution error
    ExecutionError(String),

    /// Timeout
    Timeout,
}

impl std::fmt::Display for HostFunctionError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            HostFunctionError::MemoryAccessError(msg) => write!(f, "Memory access error: {}", msg),
            HostFunctionError::InvalidData(msg) => write!(f, "Invalid data: {}", msg),
            HostFunctionError::PermissionDenied(msg) => write!(f, "Permission denied: {}", msg),
            HostFunctionError::FunctionNotFound(msg) => write!(f, "Function not found: {}", msg),
            HostFunctionError::ExecutionError(msg) => write!(f, "Execution error: {}", msg),
            HostFunctionError::Timeout => write!(f, "Timeout"),
        }
    }
}

impl std::error::Error for HostFunctionError {}

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

    #[test]
    fn test_host_function_registry_new() {
        let registry = HostFunctionRegistry::new();

        // Check default functions are registered
        assert!(registry.exists("helios", "log"));
        assert!(registry.exists("cache", "get"));
        assert!(registry.exists("query", "execute"));
    }

    #[test]
    fn test_host_function_registry_list() {
        let registry = HostFunctionRegistry::new();

        let namespaces = registry.list_namespaces();
        assert!(namespaces.contains(&"helios".to_string()));
        assert!(namespaces.contains(&"cache".to_string()));
        assert!(namespaces.contains(&"query".to_string()));

        let helios_funcs = registry.list_namespace("helios");
        assert!(helios_funcs.contains(&"log".to_string()));
    }

    #[test]
    fn test_host_function_required_permission() {
        assert!(HostFunction::Log.required_permission().is_none());
        assert!(HostFunction::HttpFetch.required_permission().is_some());
        assert!(HostFunction::CacheGet.required_permission().is_some());
    }

    #[test]
    fn test_host_function_signature() {
        let sig = HostFunction::Log.signature();
        assert!(sig.contains("log"));
        assert!(sig.contains("level"));
    }

    #[test]
    fn test_function_call_stats() {
        let mut stats = FunctionCallStats::default();
        stats.total_calls = 10;
        stats.successful_calls = 9;
        stats.failed_calls = 1;
        stats.total_duration = Duration::from_millis(100);

        assert_eq!(stats.avg_duration(), Duration::from_millis(10));
        assert!((stats.success_rate() - 0.9).abs() < 0.001);
    }

    #[test]
    fn test_host_function_context_memory() {
        let ctx = HostFunctionContext {
            plugin_name: "test".to_string(),
            request_id: "req-1".to_string(),
            memory: Arc::new(RwLock::new(vec![0u8; 1024])),
            config: HashMap::new(),
            start_time: Instant::now(),
        };

        // Write and read back
        ctx.write_bytes(0, b"hello").unwrap();
        let read = ctx.read_bytes(0, 5).unwrap();
        assert_eq!(read, b"hello");

        // Read string
        let s = ctx.read_string(0, 5).unwrap();
        assert_eq!(s, "hello");
    }

    #[test]
    fn test_host_function_context_out_of_bounds() {
        let ctx = HostFunctionContext {
            plugin_name: "test".to_string(),
            request_id: "req-1".to_string(),
            memory: Arc::new(RwLock::new(vec![0u8; 10])),
            config: HashMap::new(),
            start_time: Instant::now(),
        };

        // Try to read beyond memory
        let result = ctx.read_bytes(5, 10);
        assert!(result.is_err());
    }

    #[test]
    fn test_record_call() {
        let registry = HostFunctionRegistry::new();

        registry.record_call("helios", "log", Duration::from_micros(50), true);
        registry.record_call("helios", "log", Duration::from_micros(100), true);
        registry.record_call("helios", "log", Duration::from_micros(75), false);

        let stats = registry.get_stats();
        let log_stats = stats.calls.get("helios:log").unwrap();

        assert_eq!(log_stats.total_calls, 3);
        assert_eq!(log_stats.successful_calls, 2);
        assert_eq!(log_stats.failed_calls, 1);
        assert_eq!(log_stats.max_duration, Duration::from_micros(100));
    }
}