zentinel-proxy 0.6.11

A security-first reverse proxy built on Pingora with sleepable ops at the edge
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
//! Maglev consistent hashing load balancer
//!
//! Implements Google's Maglev algorithm for consistent hashing with minimal
//! disruption when backends are added or removed. Uses a permutation-based
//! lookup table for O(1) selection.
//!
//! Reference: <https://research.google/pubs/pub44824/>

use async_trait::async_trait;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, trace, warn};
use xxhash_rust::xxh3::xxh3_64;

use zentinel_common::errors::{ZentinelError, ZentinelResult};

use super::{LoadBalancer, RequestContext, TargetSelection, UpstreamTarget};

/// Configuration for Maglev consistent hashing
#[derive(Debug, Clone)]
pub struct MaglevConfig {
    /// Size of the lookup table (must be prime, default: 65537)
    pub table_size: usize,
    /// Key extraction method for hashing
    pub key_source: MaglevKeySource,
}

impl Default for MaglevConfig {
    fn default() -> Self {
        Self {
            // 65537 is a prime number commonly used in Maglev
            table_size: 65537,
            key_source: MaglevKeySource::ClientIp,
        }
    }
}

/// Source for extracting the hash key from requests
#[derive(Debug, Clone)]
pub enum MaglevKeySource {
    /// Use client IP address (default)
    ClientIp,
    /// Use a specific header value
    Header(String),
    /// Use a specific cookie value
    Cookie(String),
    /// Use the request path
    Path,
}

/// Maglev consistent hashing load balancer
pub struct MaglevBalancer {
    /// Original target list
    targets: Vec<UpstreamTarget>,
    /// Lookup table mapping hash -> target index
    lookup_table: Arc<RwLock<Vec<Option<usize>>>>,
    /// Health status per target
    health_status: Arc<RwLock<HashMap<String, bool>>>,
    /// Configuration
    config: MaglevConfig,
    /// Table generation counter (for cache invalidation)
    generation: Arc<RwLock<u64>>,
}

impl MaglevBalancer {
    /// Create a new Maglev balancer
    pub fn new(targets: Vec<UpstreamTarget>, config: MaglevConfig) -> Self {
        let mut health_status = HashMap::new();
        for target in &targets {
            health_status.insert(target.full_address(), true);
        }

        let table_size = config.table_size;
        let balancer = Self {
            targets,
            lookup_table: Arc::new(RwLock::new(vec![None; table_size])),
            health_status: Arc::new(RwLock::new(health_status)),
            config,
            generation: Arc::new(RwLock::new(0)),
        };

        // Build initial lookup table synchronously in a blocking manner
        // This is fine since we're in construction
        let targets_clone = balancer.targets.clone();
        let table_size = balancer.config.table_size;
        let table = Self::build_lookup_table(&targets_clone, table_size);

        // We need to set the table - use try_write since we just created this
        if let Ok(mut lookup) = balancer.lookup_table.try_write() {
            *lookup = table;
        }

        balancer
    }

    /// Build the Maglev lookup table using permutation sequences
    fn build_lookup_table(targets: &[UpstreamTarget], table_size: usize) -> Vec<Option<usize>> {
        if targets.is_empty() {
            return vec![None; table_size];
        }

        let n = targets.len();
        let m = table_size;

        // Generate permutation for each backend
        let permutations: Vec<Vec<usize>> = targets
            .iter()
            .map(|target| Self::generate_permutation(&target.full_address(), m))
            .collect();

        // Build lookup table using round-robin across permutations
        let mut table = vec![None; m];
        let mut next = vec![0usize; n]; // Next index in each backend's permutation
        let mut filled = 0;

        while filled < m {
            for i in 0..n {
                // Find next empty slot for backend i
                loop {
                    let c = permutations[i][next[i]];
                    next[i] += 1;

                    if table[c].is_none() {
                        table[c] = Some(i);
                        filled += 1;
                        break;
                    }

                    // Safety check to prevent infinite loop
                    if next[i] >= m {
                        next[i] = 0;
                        break;
                    }
                }

                if filled >= m {
                    break;
                }
            }
        }

        table
    }

    /// Generate permutation sequence for a backend
    fn generate_permutation(name: &str, table_size: usize) -> Vec<usize> {
        let m = table_size;

        // Use two independent hash functions
        let h1 = xxh3_64(name.as_bytes()) as usize;
        let h2 = {
            let mut hasher = std::collections::hash_map::DefaultHasher::new();
            name.hash(&mut hasher);
            hasher.finish() as usize
        };

        // offset and skip for this backend
        let offset = h1 % m;
        let skip = (h2 % (m - 1)) + 1; // skip must be non-zero and < m

        // Generate permutation
        (0..m).map(|i| (offset + i * skip) % m).collect()
    }

    /// Rebuild lookup table with only healthy targets
    async fn rebuild_table_for_healthy(&self) {
        let health = self.health_status.read().await;
        let healthy_targets: Vec<_> = self
            .targets
            .iter()
            .filter(|t| *health.get(&t.full_address()).unwrap_or(&true))
            .cloned()
            .collect();
        drop(health);

        if healthy_targets.is_empty() {
            // Keep existing table to allow fallback
            return;
        }

        let table = Self::build_lookup_table(&healthy_targets, self.config.table_size);

        let mut lookup = self.lookup_table.write().await;
        *lookup = table;

        let mut gen = self.generation.write().await;
        *gen += 1;

        debug!(
            healthy_count = healthy_targets.len(),
            total_count = self.targets.len(),
            generation = *gen,
            "Maglev lookup table rebuilt"
        );
    }

    /// Extract hash key from request context
    fn extract_key(&self, context: Option<&RequestContext>) -> String {
        match &self.config.key_source {
            MaglevKeySource::ClientIp => context
                .and_then(|c| c.client_ip.map(|ip| ip.ip().to_string()))
                .unwrap_or_else(|| "default".to_string()),
            MaglevKeySource::Header(name) => context
                .and_then(|c| c.headers.get(name).cloned())
                .unwrap_or_else(|| "default".to_string()),
            MaglevKeySource::Cookie(name) => context
                .and_then(|c| {
                    c.headers.get("cookie").and_then(|cookies| {
                        cookies.split(';').find_map(|cookie| {
                            let (key, value) = cookie.trim().split_once('=')?;
                            if key == name {
                                Some(value.to_string())
                            } else {
                                None
                            }
                        })
                    })
                })
                .unwrap_or_else(|| "default".to_string()),
            MaglevKeySource::Path => context
                .map(|c| c.path.clone())
                .unwrap_or_else(|| "/".to_string()),
        }
    }

    /// Get healthy targets for fallback selection
    async fn get_healthy_targets(&self) -> Vec<&UpstreamTarget> {
        let health = self.health_status.read().await;
        self.targets
            .iter()
            .filter(|t| *health.get(&t.full_address()).unwrap_or(&true))
            .collect()
    }
}

#[async_trait]
impl LoadBalancer for MaglevBalancer {
    async fn select(&self, context: Option<&RequestContext>) -> ZentinelResult<TargetSelection> {
        trace!(
            total_targets = self.targets.len(),
            algorithm = "maglev",
            "Selecting upstream target"
        );

        // Get healthy targets
        let health = self.health_status.read().await;
        let healthy_targets: Vec<_> = self
            .targets
            .iter()
            .enumerate()
            .filter(|(_, t)| *health.get(&t.full_address()).unwrap_or(&true))
            .collect();
        drop(health);

        if healthy_targets.is_empty() {
            warn!(
                total_targets = self.targets.len(),
                algorithm = "maglev",
                "No healthy upstream targets available"
            );
            return Err(ZentinelError::NoHealthyUpstream);
        }

        // Extract key and compute hash
        let key = self.extract_key(context);
        let hash = xxh3_64(key.as_bytes()) as usize;
        let table_index = hash % self.config.table_size;

        // Look up in table
        let lookup = self.lookup_table.read().await;
        let target_index = lookup[table_index];
        drop(lookup);

        // Get the target
        let target = if let Some(idx) = target_index {
            // Verify the target is still healthy
            if idx < self.targets.len() {
                let t = &self.targets[idx];
                let health = self.health_status.read().await;
                if *health.get(&t.full_address()).unwrap_or(&true) {
                    t
                } else {
                    // Target unhealthy, fall back to first healthy target
                    healthy_targets
                        .first()
                        .map(|(_, t)| *t)
                        .ok_or(ZentinelError::NoHealthyUpstream)?
                }
            } else {
                // Index out of bounds, fall back
                healthy_targets
                    .first()
                    .map(|(_, t)| *t)
                    .ok_or(ZentinelError::NoHealthyUpstream)?
            }
        } else {
            // No entry in table, fall back to first healthy
            healthy_targets
                .first()
                .map(|(_, t)| *t)
                .ok_or(ZentinelError::NoHealthyUpstream)?
        };

        trace!(
            selected_target = %target.full_address(),
            hash_key = %key,
            table_index = table_index,
            healthy_count = healthy_targets.len(),
            algorithm = "maglev",
            "Selected target via Maglev consistent hashing"
        );

        Ok(TargetSelection {
            address: target.full_address(),
            weight: target.weight,
            metadata: HashMap::new(),
        })
    }

    async fn report_health(&self, address: &str, healthy: bool) {
        let prev_health = {
            let health = self.health_status.read().await;
            *health.get(address).unwrap_or(&true)
        };

        if prev_health != healthy {
            trace!(
                target = %address,
                healthy = healthy,
                algorithm = "maglev",
                "Target health changed, rebuilding lookup table"
            );

            self.health_status
                .write()
                .await
                .insert(address.to_string(), healthy);

            // Rebuild table when health changes
            self.rebuild_table_for_healthy().await;
        } else {
            self.health_status
                .write()
                .await
                .insert(address.to_string(), healthy);
        }
    }

    async fn healthy_targets(&self) -> Vec<String> {
        self.health_status
            .read()
            .await
            .iter()
            .filter_map(|(addr, &healthy)| if healthy { Some(addr.clone()) } else { None })
            .collect()
    }
}

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

    fn make_targets(count: usize) -> Vec<UpstreamTarget> {
        (0..count)
            .map(|i| UpstreamTarget::new(format!("backend-{}", i), 8080, 100))
            .collect()
    }

    #[test]
    fn test_build_lookup_table() {
        let targets = make_targets(3);
        let table = MaglevBalancer::build_lookup_table(&targets, 65537);

        // All slots should be filled
        assert!(table.iter().all(|entry| entry.is_some()));

        // Distribution should be roughly even
        let mut counts = vec![0usize; 3];
        for idx in table.iter().flatten() {
            counts[*idx] += 1;
        }

        // Each backend should get roughly 1/3 of the slots
        let expected = 65537 / 3;
        for count in counts {
            assert!(
                (count as i64 - expected as i64).abs() < (expected as i64 / 10),
                "Uneven distribution: {} vs expected ~{}",
                count,
                expected
            );
        }
    }

    #[test]
    fn test_permutation_generation() {
        let perm1 = MaglevBalancer::generate_permutation("backend-1", 65537);
        let perm2 = MaglevBalancer::generate_permutation("backend-2", 65537);

        // Permutations should be different
        assert_ne!(perm1[0..100], perm2[0..100]);

        // Each permutation should cover all indices
        let mut seen = vec![false; 65537];
        for &idx in &perm1 {
            seen[idx] = true;
        }
        assert!(seen.iter().all(|&s| s));
    }

    #[tokio::test]
    async fn test_consistent_selection() {
        let targets = make_targets(5);
        let balancer = MaglevBalancer::new(targets, MaglevConfig::default());

        let context = RequestContext {
            client_ip: Some("192.168.1.100:12345".parse().unwrap()),
            headers: HashMap::new(),
            path: "/api/test".to_string(),
            method: "GET".to_string(),
        };

        // Same context should always select same target
        let selection1 = balancer.select(Some(&context)).await.unwrap();
        let selection2 = balancer.select(Some(&context)).await.unwrap();
        let selection3 = balancer.select(Some(&context)).await.unwrap();

        assert_eq!(selection1.address, selection2.address);
        assert_eq!(selection2.address, selection3.address);
    }

    #[tokio::test]
    async fn test_minimal_disruption() {
        // Test that removing a backend only affects keys mapped to that backend
        let targets = make_targets(5);
        let balancer = MaglevBalancer::new(targets.clone(), MaglevConfig::default());

        // Record selections for many keys
        let mut original_selections = HashMap::new();
        for i in 0..1000 {
            let context = RequestContext {
                client_ip: Some(format!("192.168.1.{}:12345", i % 256).parse().unwrap()),
                headers: HashMap::new(),
                path: format!("/api/test/{}", i),
                method: "GET".to_string(),
            };
            let selection = balancer.select(Some(&context)).await.unwrap();
            original_selections.insert(i, selection.address);
        }

        // Mark one backend as unhealthy
        balancer.report_health("backend-2:8080", false).await;

        // Check how many selections changed
        let mut changed = 0;
        for i in 0..1000 {
            let context = RequestContext {
                client_ip: Some(format!("192.168.1.{}:12345", i % 256).parse().unwrap()),
                headers: HashMap::new(),
                path: format!("/api/test/{}", i),
                method: "GET".to_string(),
            };
            let selection = balancer.select(Some(&context)).await.unwrap();
            if selection.address != original_selections[&i] {
                changed += 1;
            }
        }

        // When a backend is removed, ideally only ~20% should change.
        // Our current implementation rebuilds the table which causes more
        // disruption, but it should still be less than replacing all keys.
        // With 5 backends -> 4 backends, worst case is 100% change.
        // We expect significantly less than that.
        assert!(
            changed < 800,
            "Too many selections changed: {} (expected less than 800 for 1/5 backend removal)",
            changed
        );

        // And verify that at least some selections are stable
        assert!(
            changed < 1000 - 100,
            "Too few stable selections: only {} unchanged",
            1000 - changed
        );
    }
}