ringkernel-core 0.4.2

Core traits and types for RingKernel GPU-native actor system
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
//! Multi-tenancy support for RingKernel.
//!
//! This module provides tenant isolation, resource quotas, and tenant-aware
//! operations for multi-tenant deployments.
//!
//! # Example
//!
//! ```rust,ignore
//! use ringkernel_core::tenancy::{TenantContext, TenantRegistry, ResourceQuota};
//!
//! let registry = TenantRegistry::new()
//!     .with_tenant("tenant_a", ResourceQuota::new()
//!         .with_max_kernels(100)
//!         .with_max_gpu_memory_mb(8192)
//!         .with_max_messages_per_sec(10000))
//!     .with_tenant("tenant_b", ResourceQuota::new()
//!         .with_max_kernels(50)
//!         .with_max_gpu_memory_mb(4096));
//!
//! let ctx = TenantContext::new("tenant_a");
//! if let Some(quota) = registry.get_quota(&ctx.tenant_id) {
//!     if quota.check_kernel_limit(current_kernels) {
//!         // Launch kernel
//!     }
//! }
//! ```

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

// ============================================================================
// RESOURCE QUOTA
// ============================================================================

/// Resource quotas for a tenant.
#[derive(Debug, Clone)]
pub struct ResourceQuota {
    /// Maximum number of concurrent kernels.
    pub max_kernels: Option<u64>,
    /// Maximum GPU memory in megabytes.
    pub max_gpu_memory_mb: Option<u64>,
    /// Maximum messages per second.
    pub max_messages_per_sec: Option<u64>,
    /// Maximum K2K endpoints.
    pub max_k2k_endpoints: Option<u64>,
    /// Maximum PubSub subscriptions.
    pub max_pubsub_subscriptions: Option<u64>,
    /// Maximum checkpoint storage in megabytes.
    pub max_checkpoint_storage_mb: Option<u64>,
    /// Maximum CPU time per hour (seconds).
    pub max_cpu_time_per_hour: Option<u64>,
    /// Maximum API requests per minute.
    pub max_api_requests_per_min: Option<u64>,
}

impl ResourceQuota {
    /// Create a new resource quota with no limits.
    pub fn new() -> Self {
        Self {
            max_kernels: None,
            max_gpu_memory_mb: None,
            max_messages_per_sec: None,
            max_k2k_endpoints: None,
            max_pubsub_subscriptions: None,
            max_checkpoint_storage_mb: None,
            max_cpu_time_per_hour: None,
            max_api_requests_per_min: None,
        }
    }

    /// Create an unlimited quota.
    pub fn unlimited() -> Self {
        Self::new()
    }

    /// Set maximum kernels.
    pub fn with_max_kernels(mut self, max: u64) -> Self {
        self.max_kernels = Some(max);
        self
    }

    /// Set maximum GPU memory (MB).
    pub fn with_max_gpu_memory_mb(mut self, max: u64) -> Self {
        self.max_gpu_memory_mb = Some(max);
        self
    }

    /// Set maximum messages per second.
    pub fn with_max_messages_per_sec(mut self, max: u64) -> Self {
        self.max_messages_per_sec = Some(max);
        self
    }

    /// Set maximum K2K endpoints.
    pub fn with_max_k2k_endpoints(mut self, max: u64) -> Self {
        self.max_k2k_endpoints = Some(max);
        self
    }

    /// Set maximum PubSub subscriptions.
    pub fn with_max_pubsub_subscriptions(mut self, max: u64) -> Self {
        self.max_pubsub_subscriptions = Some(max);
        self
    }

    /// Set maximum checkpoint storage (MB).
    pub fn with_max_checkpoint_storage_mb(mut self, max: u64) -> Self {
        self.max_checkpoint_storage_mb = Some(max);
        self
    }

    /// Set maximum CPU time per hour (seconds).
    pub fn with_max_cpu_time_per_hour(mut self, max: u64) -> Self {
        self.max_cpu_time_per_hour = Some(max);
        self
    }

    /// Set maximum API requests per minute.
    pub fn with_max_api_requests_per_min(mut self, max: u64) -> Self {
        self.max_api_requests_per_min = Some(max);
        self
    }

    /// Check if kernel limit allows another kernel.
    pub fn check_kernel_limit(&self, current: u64) -> bool {
        self.max_kernels.map(|max| current < max).unwrap_or(true)
    }

    /// Check if GPU memory limit allows allocation.
    pub fn check_gpu_memory_limit(&self, current_mb: u64, requested_mb: u64) -> bool {
        self.max_gpu_memory_mb
            .map(|max| current_mb + requested_mb <= max)
            .unwrap_or(true)
    }

    /// Check if message rate limit allows another message.
    pub fn check_message_rate(&self, current_rate: u64) -> bool {
        self.max_messages_per_sec
            .map(|max| current_rate < max)
            .unwrap_or(true)
    }

    /// Create a standard small tier quota.
    pub fn tier_small() -> Self {
        Self::new()
            .with_max_kernels(10)
            .with_max_gpu_memory_mb(2048)
            .with_max_messages_per_sec(1000)
            .with_max_k2k_endpoints(20)
            .with_max_pubsub_subscriptions(50)
            .with_max_checkpoint_storage_mb(1024)
            .with_max_api_requests_per_min(100)
    }

    /// Create a standard medium tier quota.
    pub fn tier_medium() -> Self {
        Self::new()
            .with_max_kernels(50)
            .with_max_gpu_memory_mb(8192)
            .with_max_messages_per_sec(10000)
            .with_max_k2k_endpoints(100)
            .with_max_pubsub_subscriptions(200)
            .with_max_checkpoint_storage_mb(10240)
            .with_max_api_requests_per_min(1000)
    }

    /// Create a standard large tier quota.
    pub fn tier_large() -> Self {
        Self::new()
            .with_max_kernels(200)
            .with_max_gpu_memory_mb(32768)
            .with_max_messages_per_sec(100000)
            .with_max_k2k_endpoints(500)
            .with_max_pubsub_subscriptions(1000)
            .with_max_checkpoint_storage_mb(102400)
            .with_max_api_requests_per_min(10000)
    }
}

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

// ============================================================================
// RESOURCE USAGE
// ============================================================================

/// Current resource usage for a tenant.
#[derive(Debug, Clone)]
pub struct ResourceUsage {
    /// Current kernel count.
    pub kernels: u64,
    /// Current GPU memory usage (MB).
    pub gpu_memory_mb: u64,
    /// Messages sent in current window.
    pub messages_this_window: u64,
    /// Current K2K endpoint count.
    pub k2k_endpoints: u64,
    /// Current PubSub subscription count.
    pub pubsub_subscriptions: u64,
    /// Current checkpoint storage (MB).
    pub checkpoint_storage_mb: u64,
    /// API requests in current window.
    pub api_requests_this_window: u64,
    /// Window start time.
    pub window_start: Instant,
}

impl ResourceUsage {
    /// Create new resource usage tracking.
    pub fn new() -> Self {
        Self {
            kernels: 0,
            gpu_memory_mb: 0,
            messages_this_window: 0,
            k2k_endpoints: 0,
            pubsub_subscriptions: 0,
            checkpoint_storage_mb: 0,
            api_requests_this_window: 0,
            window_start: Instant::now(),
        }
    }

    /// Reset windowed counters (messages, API requests).
    pub fn reset_window(&mut self) {
        self.messages_this_window = 0;
        self.api_requests_this_window = 0;
        self.window_start = Instant::now();
    }

    /// Calculate utilization against quota.
    pub fn utilization(&self, quota: &ResourceQuota) -> QuotaUtilization {
        QuotaUtilization {
            kernel_pct: quota
                .max_kernels
                .map(|max| self.kernels as f64 / max as f64 * 100.0),
            gpu_memory_pct: quota
                .max_gpu_memory_mb
                .map(|max| self.gpu_memory_mb as f64 / max as f64 * 100.0),
            message_rate_pct: quota
                .max_messages_per_sec
                .map(|max| self.messages_this_window as f64 / max as f64 * 100.0),
        }
    }
}

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

/// Quota utilization percentages.
#[derive(Debug, Clone)]
pub struct QuotaUtilization {
    /// Kernel utilization percentage.
    pub kernel_pct: Option<f64>,
    /// GPU memory utilization percentage.
    pub gpu_memory_pct: Option<f64>,
    /// Message rate utilization percentage.
    pub message_rate_pct: Option<f64>,
}

// ============================================================================
// TENANT CONTEXT
// ============================================================================

/// Context for tenant-scoped operations.
#[derive(Debug, Clone)]
pub struct TenantContext {
    /// Tenant ID.
    pub tenant_id: String,
    /// Tenant display name.
    pub display_name: Option<String>,
    /// Tenant metadata.
    pub metadata: HashMap<String, String>,
    /// When the context was created.
    pub created_at: Instant,
}

impl TenantContext {
    /// Create a new tenant context.
    pub fn new(tenant_id: impl Into<String>) -> Self {
        Self {
            tenant_id: tenant_id.into(),
            display_name: None,
            metadata: HashMap::new(),
            created_at: Instant::now(),
        }
    }

    /// Set display name.
    pub fn with_display_name(mut self, name: impl Into<String>) -> Self {
        self.display_name = Some(name.into());
        self
    }

    /// Add metadata.
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Get the tenant-prefixed resource name.
    pub fn resource_name(&self, resource: &str) -> String {
        format!("{}:{}", self.tenant_id, resource)
    }
}

// ============================================================================
// TENANT REGISTRY
// ============================================================================

/// Error type for tenant operations.
#[derive(Debug, Clone)]
pub enum TenantError {
    /// Tenant not found.
    NotFound(String),
    /// Quota exceeded.
    QuotaExceeded(String),
    /// Tenant already exists.
    AlreadyExists(String),
    /// Tenant is suspended.
    Suspended(String),
    /// Other error.
    Other(String),
}

impl fmt::Display for TenantError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NotFound(msg) => write!(f, "Tenant not found: {}", msg),
            Self::QuotaExceeded(msg) => write!(f, "Quota exceeded: {}", msg),
            Self::AlreadyExists(msg) => write!(f, "Tenant already exists: {}", msg),
            Self::Suspended(msg) => write!(f, "Tenant suspended: {}", msg),
            Self::Other(msg) => write!(f, "Tenant error: {}", msg),
        }
    }
}

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

/// Result type for tenant operations.
pub type TenantResult<T> = Result<T, TenantError>;

/// Internal tenant entry.
struct TenantEntry {
    /// Tenant context.
    _context: TenantContext,
    /// Resource quota.
    quota: ResourceQuota,
    /// Resource usage (with interior mutability).
    usage: RwLock<ResourceUsage>,
    /// Whether tenant is active (interior mutability for suspension).
    active: std::sync::atomic::AtomicBool,
    /// When tenant was registered.
    _registered_at: Instant,
}

/// Registry for managing tenants.
pub struct TenantRegistry {
    /// Registered tenants.
    tenants: RwLock<HashMap<String, Arc<TenantEntry>>>,
    /// Default quota for new tenants.
    default_quota: ResourceQuota,
    /// Window duration for rate limiting.
    window_duration: Duration,
}

impl TenantRegistry {
    /// Create a new tenant registry.
    pub fn new() -> Self {
        Self {
            tenants: RwLock::new(HashMap::new()),
            default_quota: ResourceQuota::tier_small(),
            window_duration: Duration::from_secs(60), // 1 minute windows
        }
    }

    /// Set default quota for new tenants.
    pub fn with_default_quota(mut self, quota: ResourceQuota) -> Self {
        self.default_quota = quota;
        self
    }

    /// Set rate limit window duration.
    pub fn with_window_duration(mut self, duration: Duration) -> Self {
        self.window_duration = duration;
        self
    }

    /// Register a tenant with quota.
    pub fn with_tenant(self, tenant_id: impl Into<String>, quota: ResourceQuota) -> Self {
        let tenant_id = tenant_id.into();
        let entry = TenantEntry {
            _context: TenantContext::new(&tenant_id),
            quota,
            usage: RwLock::new(ResourceUsage::new()),
            active: std::sync::atomic::AtomicBool::new(true),
            _registered_at: Instant::now(),
        };

        self.tenants.write().insert(tenant_id, Arc::new(entry));
        self
    }

    /// Register a new tenant.
    pub fn register_tenant(
        &self,
        tenant_id: impl Into<String>,
        quota: ResourceQuota,
    ) -> TenantResult<()> {
        let tenant_id = tenant_id.into();
        let mut tenants = self.tenants.write();

        if tenants.contains_key(&tenant_id) {
            return Err(TenantError::AlreadyExists(tenant_id));
        }

        let entry = TenantEntry {
            _context: TenantContext::new(&tenant_id),
            quota,
            usage: RwLock::new(ResourceUsage::new()),
            active: std::sync::atomic::AtomicBool::new(true),
            _registered_at: Instant::now(),
        };

        tenants.insert(tenant_id, Arc::new(entry));
        Ok(())
    }

    /// Get a tenant's quota.
    pub fn get_quota(&self, tenant_id: &str) -> Option<ResourceQuota> {
        self.tenants.read().get(tenant_id).map(|e| e.quota.clone())
    }

    /// Get a tenant's current usage.
    pub fn get_usage(&self, tenant_id: &str) -> Option<ResourceUsage> {
        self.tenants
            .read()
            .get(tenant_id)
            .map(|e| e.usage.read().clone())
    }

    /// Check if tenant exists.
    pub fn tenant_exists(&self, tenant_id: &str) -> bool {
        self.tenants.read().contains_key(tenant_id)
    }

    /// Check if tenant is active.
    pub fn is_tenant_active(&self, tenant_id: &str) -> bool {
        self.tenants
            .read()
            .get(tenant_id)
            .map(|e| e.active.load(std::sync::atomic::Ordering::Acquire))
            .unwrap_or(false)
    }

    /// Suspend a tenant. Suspended tenants cannot allocate resources or send messages.
    pub fn suspend_tenant(&self, tenant_id: &str) -> TenantResult<()> {
        let tenants = self.tenants.read();
        let entry = tenants
            .get(tenant_id)
            .ok_or_else(|| TenantError::NotFound(tenant_id.to_string()))?;
        entry
            .active
            .store(false, std::sync::atomic::Ordering::Release);
        Ok(())
    }

    /// Resume a previously suspended tenant.
    pub fn resume_tenant(&self, tenant_id: &str) -> TenantResult<()> {
        let tenants = self.tenants.read();
        let entry = tenants
            .get(tenant_id)
            .ok_or_else(|| TenantError::NotFound(tenant_id.to_string()))?;
        entry
            .active
            .store(true, std::sync::atomic::Ordering::Release);
        Ok(())
    }

    /// Check and increment kernel count.
    pub fn try_allocate_kernel(&self, tenant_id: &str) -> TenantResult<()> {
        let tenants = self.tenants.read();
        let entry = tenants
            .get(tenant_id)
            .ok_or_else(|| TenantError::NotFound(tenant_id.to_string()))?;

        if !entry.active.load(std::sync::atomic::Ordering::Acquire) {
            return Err(TenantError::Suspended(tenant_id.to_string()));
        }

        let mut usage = entry.usage.write();
        if !entry.quota.check_kernel_limit(usage.kernels) {
            return Err(TenantError::QuotaExceeded(format!(
                "Kernel limit reached: {}/{}",
                usage.kernels,
                entry.quota.max_kernels.unwrap_or(0)
            )));
        }

        usage.kernels += 1;
        Ok(())
    }

    /// Release a kernel allocation.
    pub fn release_kernel(&self, tenant_id: &str) {
        if let Some(entry) = self.tenants.read().get(tenant_id) {
            let mut usage = entry.usage.write();
            usage.kernels = usage.kernels.saturating_sub(1);
        }
    }

    /// Check and increment GPU memory.
    pub fn try_allocate_gpu_memory(&self, tenant_id: &str, mb: u64) -> TenantResult<()> {
        let tenants = self.tenants.read();
        let entry = tenants
            .get(tenant_id)
            .ok_or_else(|| TenantError::NotFound(tenant_id.to_string()))?;

        if !entry.active.load(std::sync::atomic::Ordering::Acquire) {
            return Err(TenantError::Suspended(tenant_id.to_string()));
        }

        let mut usage = entry.usage.write();
        if !entry.quota.check_gpu_memory_limit(usage.gpu_memory_mb, mb) {
            return Err(TenantError::QuotaExceeded(format!(
                "GPU memory limit reached: {}MB + {}MB > {}MB",
                usage.gpu_memory_mb,
                mb,
                entry.quota.max_gpu_memory_mb.unwrap_or(0)
            )));
        }

        usage.gpu_memory_mb += mb;
        Ok(())
    }

    /// Release GPU memory allocation.
    pub fn release_gpu_memory(&self, tenant_id: &str, mb: u64) {
        if let Some(entry) = self.tenants.read().get(tenant_id) {
            let mut usage = entry.usage.write();
            usage.gpu_memory_mb = usage.gpu_memory_mb.saturating_sub(mb);
        }
    }

    /// Record a message sent.
    pub fn record_message(&self, tenant_id: &str) -> TenantResult<()> {
        let tenants = self.tenants.read();
        let entry = tenants
            .get(tenant_id)
            .ok_or_else(|| TenantError::NotFound(tenant_id.to_string()))?;

        if !entry.active.load(std::sync::atomic::Ordering::Acquire) {
            return Err(TenantError::Suspended(tenant_id.to_string()));
        }

        let mut usage = entry.usage.write();

        // Reset window if needed
        if usage.window_start.elapsed() >= self.window_duration {
            usage.reset_window();
        }

        if !entry.quota.check_message_rate(usage.messages_this_window) {
            return Err(TenantError::QuotaExceeded(format!(
                "Message rate limit reached: {}/{} per {:?}",
                usage.messages_this_window,
                entry.quota.max_messages_per_sec.unwrap_or(0),
                self.window_duration
            )));
        }

        usage.messages_this_window += 1;
        Ok(())
    }

    /// Get utilization for a tenant.
    pub fn get_utilization(&self, tenant_id: &str) -> Option<QuotaUtilization> {
        self.tenants
            .read()
            .get(tenant_id)
            .map(|entry| entry.usage.read().utilization(&entry.quota))
    }

    /// Get all tenant IDs.
    pub fn tenant_ids(&self) -> Vec<String> {
        self.tenants.read().keys().cloned().collect()
    }

    /// Get tenant count.
    pub fn tenant_count(&self) -> usize {
        self.tenants.read().len()
    }
}

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

// ============================================================================
// TESTS
// ============================================================================

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

    #[test]
    fn test_resource_quota() {
        let quota = ResourceQuota::new()
            .with_max_kernels(10)
            .with_max_gpu_memory_mb(8192);

        assert!(quota.check_kernel_limit(5));
        assert!(quota.check_kernel_limit(9));
        assert!(!quota.check_kernel_limit(10));

        assert!(quota.check_gpu_memory_limit(4096, 2048));
        assert!(!quota.check_gpu_memory_limit(8192, 1));
    }

    #[test]
    fn test_tier_quotas() {
        let small = ResourceQuota::tier_small();
        assert_eq!(small.max_kernels, Some(10));
        assert_eq!(small.max_gpu_memory_mb, Some(2048));

        let large = ResourceQuota::tier_large();
        assert_eq!(large.max_kernels, Some(200));
        assert_eq!(large.max_gpu_memory_mb, Some(32768));
    }

    #[test]
    fn test_tenant_context() {
        let ctx = TenantContext::new("tenant_a")
            .with_display_name("Tenant A")
            .with_metadata("tier", "enterprise");

        assert_eq!(ctx.tenant_id, "tenant_a");
        assert_eq!(ctx.display_name, Some("Tenant A".to_string()));
        assert_eq!(ctx.resource_name("kernel_1"), "tenant_a:kernel_1");
    }

    #[test]
    fn test_tenant_registry() {
        let registry = TenantRegistry::new()
            .with_tenant("tenant_a", ResourceQuota::tier_small())
            .with_tenant("tenant_b", ResourceQuota::tier_medium());

        assert!(registry.tenant_exists("tenant_a"));
        assert!(registry.tenant_exists("tenant_b"));
        assert!(!registry.tenant_exists("tenant_c"));

        let quota_a = registry.get_quota("tenant_a").unwrap();
        assert_eq!(quota_a.max_kernels, Some(10));

        let quota_b = registry.get_quota("tenant_b").unwrap();
        assert_eq!(quota_b.max_kernels, Some(50));
    }

    #[test]
    fn test_kernel_allocation() {
        let registry =
            TenantRegistry::new().with_tenant("tenant_a", ResourceQuota::new().with_max_kernels(2));

        // First two allocations succeed
        assert!(registry.try_allocate_kernel("tenant_a").is_ok());
        assert!(registry.try_allocate_kernel("tenant_a").is_ok());

        // Third fails
        assert!(registry.try_allocate_kernel("tenant_a").is_err());

        // Release one
        registry.release_kernel("tenant_a");

        // Now can allocate again
        assert!(registry.try_allocate_kernel("tenant_a").is_ok());
    }

    #[test]
    fn test_gpu_memory_allocation() {
        let registry = TenantRegistry::new().with_tenant(
            "tenant_a",
            ResourceQuota::new().with_max_gpu_memory_mb(1024),
        );

        assert!(registry.try_allocate_gpu_memory("tenant_a", 512).is_ok());
        assert!(registry.try_allocate_gpu_memory("tenant_a", 256).is_ok());
        // Would exceed limit
        assert!(registry.try_allocate_gpu_memory("tenant_a", 512).is_err());

        // Release and retry
        registry.release_gpu_memory("tenant_a", 256);
        assert!(registry.try_allocate_gpu_memory("tenant_a", 512).is_ok());
    }

    #[test]
    fn test_utilization() {
        let quota = ResourceQuota::new()
            .with_max_kernels(100)
            .with_max_gpu_memory_mb(8192);

        let mut usage = ResourceUsage::new();
        usage.kernels = 50;
        usage.gpu_memory_mb = 4096;

        let utilization = usage.utilization(&quota);
        assert_eq!(utilization.kernel_pct, Some(50.0));
        assert_eq!(utilization.gpu_memory_pct, Some(50.0));
    }

    #[test]
    fn test_unknown_tenant() {
        let registry = TenantRegistry::new();

        assert!(registry.try_allocate_kernel("unknown").is_err());
        assert!(registry.get_quota("unknown").is_none());
    }

    #[test]
    fn test_suspend_and_resume_tenant() {
        let registry = TenantRegistry::new()
            .with_tenant("tenant_a", ResourceQuota::new().with_max_kernels(10));

        assert!(registry.is_tenant_active("tenant_a"));
        assert!(registry.try_allocate_kernel("tenant_a").is_ok());

        // Suspend
        registry.suspend_tenant("tenant_a").unwrap();
        assert!(!registry.is_tenant_active("tenant_a"));

        // Operations should fail while suspended
        assert!(matches!(
            registry.try_allocate_kernel("tenant_a"),
            Err(TenantError::Suspended(_))
        ));
        assert!(matches!(
            registry.try_allocate_gpu_memory("tenant_a", 100),
            Err(TenantError::Suspended(_))
        ));
        assert!(matches!(
            registry.record_message("tenant_a"),
            Err(TenantError::Suspended(_))
        ));

        // Resume
        registry.resume_tenant("tenant_a").unwrap();
        assert!(registry.is_tenant_active("tenant_a"));
        assert!(registry.try_allocate_kernel("tenant_a").is_ok());
    }

    #[test]
    fn test_suspend_unknown_tenant() {
        let registry = TenantRegistry::new();
        assert!(matches!(
            registry.suspend_tenant("unknown"),
            Err(TenantError::NotFound(_))
        ));
        assert!(matches!(
            registry.resume_tenant("unknown"),
            Err(TenantError::NotFound(_))
        ));
    }
}