chronix-security 0.7.0

Security layer for Chronix — authentication, Cedar authorization, audit trail, and multi-tenancy
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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
//! Namespace registry — CRUD for tenant namespaces.
//!
//! Manages the lifecycle of namespaces: creation, deletion, listing,
//! and configuration updates. Each namespace encapsulates its own
//! quota limits and usage counters.
//!
//! Optionally persists namespace state to disk via JSON snapshots.

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::SystemTime;

use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use tracing::{debug, info};

use chronix_core::{NamespaceId, NamespaceQuota, NamespaceUsage};

use crate::tenant::error::{Result, TenantError};

/// Metadata for a registered namespace.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NamespaceInfo {
    /// Unique namespace identifier.
    pub id: NamespaceId,
    /// Human-readable description.
    pub description: String,
    /// Owner principal (user ID that created the namespace).
    pub owner: String,
    /// Resource quota limits.
    pub quota: NamespaceQuota,
    /// When the namespace was created (ms since epoch).
    pub created_at_ms: u64,
}

/// Current state of a namespace including live usage.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NamespaceState {
    /// Namespace metadata.
    pub info: NamespaceInfo,
    /// Real-time resource usage.
    pub usage: NamespaceUsage,
}

/// Thread-safe namespace registry.
///
/// Provides fast, concurrent access to namespace metadata and usage
/// counters using [`DashMap`] for lock-free reads.
///
/// # Default Namespace
///
/// The `default` namespace is always present and cannot be deleted.
/// It is used when multi-tenancy is disabled or no namespace header
/// is provided.
#[derive(Debug)]
pub struct NamespaceRegistry {
    /// Namespace states keyed by namespace name.
    namespaces: DashMap<String, NamespaceState>,
    /// Optional directory for persisting namespace state.
    persist_dir: Option<PathBuf>,
}

/// File name for the namespace snapshot inside the persist directory.
const SNAPSHOT_FILE: &str = "namespaces.json";

/// Atomic write-to-tmp + fsync + rename for namespace state.
fn write_snapshot(dir: &Path, states: &[NamespaceState]) -> Result<()> {
    let json = serde_json::to_string_pretty(states)
        .map_err(|e| TenantError::InvalidConfig(format!("cannot serialize namespaces: {e}")))?;

    let tmp_path = dir.join("namespaces.json.tmp");
    let final_path = dir.join(SNAPSHOT_FILE);
    std::fs::write(&tmp_path, &json)
        .map_err(|e| TenantError::Persist(format!("cannot write namespace snapshot: {e}")))?;

    // Clean up tmp file on fsync or rename failure to avoid
    // stale tmp files leaking on disk.
    let result = (|| -> Result<()> {
        let f = std::fs::File::open(&tmp_path).map_err(|e| {
            TenantError::Persist(format!("cannot open tmp snapshot for fsync: {e}"))
        })?;
        f.sync_all()
            .map_err(|e| TenantError::Persist(format!("cannot fsync namespace snapshot: {e}")))?;

        std::fs::rename(&tmp_path, &final_path)
            .map_err(|e| TenantError::Persist(format!("cannot rename namespace snapshot: {e}")))?;
        Ok(())
    })();

    if result.is_err() {
        let _ = std::fs::remove_file(&tmp_path);
    }

    result?;

    debug!("namespace snapshot saved");
    Ok(())
}
impl NamespaceRegistry {
    /// Create a new in-memory registry with only the `default` namespace.
    ///
    /// State is not persisted. Use `open` for durable registries.
    #[must_use]
    pub fn new() -> Self {
        let registry = Self {
            namespaces: DashMap::new(),
            persist_dir: None,
        };

        registry.ensure_default();
        registry
    }

    /// Open a durable namespace registry backed by a directory.
    ///
    /// If a previous snapshot exists, it is loaded. Otherwise the directory
    /// is created and an initial snapshot with only the `default` namespace
    /// is written.
    ///
    /// # Errors
    ///
    /// Returns an error if the directory cannot be created or the snapshot
    /// file is corrupt.
    pub fn open(dir: impl Into<PathBuf>) -> Result<Self> {
        let dir = dir.into();
        std::fs::create_dir_all(&dir)
            .map_err(|e| TenantError::InvalidConfig(format!("cannot create namespace dir: {e}")))?;

        let snapshot_path = dir.join(SNAPSHOT_FILE);
        let registry = if snapshot_path.exists() {
            let data = std::fs::read_to_string(&snapshot_path).map_err(|e| {
                TenantError::InvalidConfig(format!("cannot read namespace snapshot: {e}"))
            })?;
            let states: Vec<NamespaceState> = serde_json::from_str(&data).map_err(|e| {
                TenantError::InvalidConfig(format!("corrupt namespace snapshot: {e}"))
            })?;
            let namespaces = DashMap::new();
            for state in states {
                namespaces.insert(state.info.id.as_str().to_string(), state);
            }
            info!(count = namespaces.len(), "loaded namespace snapshot");
            Self {
                namespaces,
                persist_dir: Some(dir),
            }
        } else {
            Self {
                namespaces: DashMap::new(),
                persist_dir: Some(dir),
            }
        };

        registry.ensure_default();

        // Write initial snapshot if it didn't exist
        if !snapshot_path.exists() {
            registry.save_snapshot_inner()?;
        }

        Ok(registry)
    }

    /// Ensure the default namespace exists.
    fn ensure_default(&self) {
        if !self.namespaces.contains_key(NamespaceId::DEFAULT) {
            let default_info = NamespaceInfo {
                id: NamespaceId::default_namespace(),
                description: "Default namespace".to_string(),
                owner: "system".to_string(),
                quota: NamespaceQuota::default(),
                created_at_ms: now_ms(),
            };
            self.namespaces.insert(
                NamespaceId::DEFAULT.to_string(),
                NamespaceState {
                    info: default_info,
                    usage: NamespaceUsage::default(),
                },
            );
        }
    }

    /// Persist current namespace state to disk (atomic write-to-tmp + rename).
    ///
    /// No-op if the registry has no `persist_dir`.
    fn save_snapshot_inner(&self) -> Result<()> {
        let Some(dir) = &self.persist_dir else {
            return Ok(());
        };

        let states: Vec<NamespaceState> =
            self.namespaces.iter().map(|r| r.value().clone()).collect();

        write_snapshot(dir, &states)
    }

    /// Create a new namespace.
    ///
    /// # Errors
    ///
    /// Returns `NamespaceAlreadyExists` if a namespace with the same name
    /// already exists, or `Schema` if the name is invalid.
    pub fn create_namespace(
        &self,
        id: NamespaceId,
        description: impl Into<String>,
        owner: impl Into<String>,
        quota: NamespaceQuota,
    ) -> Result<NamespaceInfo> {
        let name = id.as_str().to_string();

        // Use DashMap::entry() to atomically check-and-insert, avoiding the
        // TOCTOU race between contains_key() and insert().
        use dashmap::mapref::entry::Entry;
        match self.namespaces.entry(name.clone()) {
            Entry::Occupied(_) => Err(TenantError::NamespaceAlreadyExists(name)),
            Entry::Vacant(vacant) => {
                let info = NamespaceInfo {
                    id,
                    description: description.into(),
                    owner: owner.into(),
                    quota,
                    created_at_ms: now_ms(),
                };

                let state = NamespaceState {
                    info: info.clone(),
                    usage: NamespaceUsage::default(),
                };

                vacant.insert(state);

                info!(namespace = %name, "namespace created");
                #[allow(clippy::cast_precision_loss)]
                metrics::gauge!("chronix_namespace_count").set(self.namespaces.len() as f64);

                // Synchronous, like the delete below. A create that is
                // acknowledged and then lost is the delete case mirrored:
                // the operator provisions a tenant, hands out a credential
                // bound to it, and after the next restart every request from
                // that tenant answers `400 namespace not found`. The
                // in-memory entry is rolled back so the running process does
                // not disagree with its own restart.
                if let Err(e) = self.save_snapshot_inner() {
                    self.namespaces.remove(&name);
                    #[allow(clippy::cast_precision_loss)]
                    metrics::gauge!("chronix_namespace_count").set(self.namespaces.len() as f64);
                    return Err(e);
                }
                Ok(info)
            }
        }
    }

    /// Delete a namespace.
    ///
    /// # Errors
    ///
    /// Returns `NamespaceNotFound` if the namespace does not exist.
    /// The `default` namespace cannot be deleted.
    pub fn delete_namespace(&self, name: &str) -> Result<NamespaceInfo> {
        if name == NamespaceId::DEFAULT {
            return Err(TenantError::InvalidConfig(
                "cannot delete the default namespace".to_string(),
            ));
        }

        let (_, state) = self
            .namespaces
            .remove(name)
            .ok_or_else(|| TenantError::NamespaceNotFound(name.to_string()))?;

        info!(namespace = %name, "namespace deleted");
        #[allow(clippy::cast_precision_loss)]
        metrics::gauge!("chronix_namespace_count").set(self.namespaces.len() as f64);

        // Persist synchronously — deletes are destructive and must be
        // durable before returning to the caller.
        self.save_snapshot_inner()?;
        Ok(state.info)
    }

    /// Get namespace info by name.
    ///
    /// # Errors
    ///
    /// Returns `NamespaceNotFound` if the namespace does not exist.
    pub fn get(&self, name: &str) -> Result<NamespaceState> {
        self.namespaces
            .get(name)
            .map(|r| r.value().clone())
            .ok_or_else(|| TenantError::NamespaceNotFound(name.to_string()))
    }

    /// Check if a namespace exists.
    #[must_use]
    pub fn exists(&self, name: &str) -> bool {
        self.namespaces.contains_key(name)
    }

    /// List all namespace names.
    #[must_use]
    pub fn list_names(&self) -> Vec<String> {
        self.namespaces.iter().map(|r| r.key().clone()).collect()
    }

    /// List all namespace states.
    #[must_use]
    pub fn list_all(&self) -> Vec<NamespaceState> {
        self.namespaces.iter().map(|r| r.value().clone()).collect()
    }

    /// Get the number of registered namespaces.
    #[must_use]
    pub fn count(&self) -> usize {
        self.namespaces.len()
    }

    /// Update the quota for a namespace.
    ///
    /// # Errors
    ///
    /// Returns `NamespaceNotFound` if the namespace does not exist.
    pub fn update_quota(&self, name: &str, quota: NamespaceQuota) -> Result<()> {
        // Validate quota upper bounds before applying.
        quota.validate().map_err(TenantError::InvalidConfig)?;
        {
            let mut entry = self
                .namespaces
                .get_mut(name)
                .ok_or_else(|| TenantError::NamespaceNotFound(name.to_string()))?;
            entry.value_mut().info.quota = quota;
        }
        debug!(namespace = %name, "quota updated");
        // Synchronous for the same reason: a quota the API reports as
        // applied and the next restart reverts is a setting that is accepted
        // and ignored.
        self.save_snapshot_inner()?;
        Ok(())
    }

    /// Get a reference to the usage counters for a namespace.
    ///
    /// Returns `None` if the namespace does not exist.
    #[must_use]
    pub fn get_usage(&self, name: &str) -> Option<NamespaceUsage> {
        self.namespaces.get(name).map(|r| r.value().usage.clone())
    }

    /// Update usage counters for a namespace.
    ///
    /// # Errors
    ///
    /// Returns `NamespaceNotFound` if the namespace does not exist.
    pub fn update_usage(&self, name: &str, usage: NamespaceUsage) -> Result<()> {
        let mut entry = self
            .namespaces
            .get_mut(name)
            .ok_or_else(|| TenantError::NamespaceNotFound(name.to_string()))?;

        entry.value_mut().usage = usage;
        Ok(())
    }

    /// Increment the series count for a namespace.
    ///
    /// Uses saturating addition to prevent `u64` overflow.
    ///
    /// # Errors
    ///
    /// Returns `NamespaceNotFound` if the namespace does not exist.
    pub fn increment_series(&self, name: &str, count: u64) -> Result<()> {
        let mut entry = self
            .namespaces
            .get_mut(name)
            .ok_or_else(|| TenantError::NamespaceNotFound(name.to_string()))?;

        entry.value_mut().usage.series_count =
            entry.value().usage.series_count.saturating_add(count);
        Ok(())
    }

    /// Increment the storage bytes for a namespace.
    ///
    /// Uses saturating addition to prevent `u64` overflow.
    ///
    /// # Errors
    ///
    /// Returns `NamespaceNotFound` if the namespace does not exist.
    pub fn increment_storage(&self, name: &str, bytes: u64) -> Result<()> {
        let mut entry = self
            .namespaces
            .get_mut(name)
            .ok_or_else(|| TenantError::NamespaceNotFound(name.to_string()))?;

        entry.value_mut().usage.storage_bytes =
            entry.value().usage.storage_bytes.saturating_add(bytes);
        Ok(())
    }

    /// Increment the measurement count for a namespace.
    ///
    /// Uses saturating addition to prevent `u32` overflow.
    ///
    /// # Errors
    ///
    /// Returns `NamespaceNotFound` if the namespace does not exist.
    pub fn increment_measurements(&self, name: &str, count: u32) -> Result<()> {
        let mut entry = self
            .namespaces
            .get_mut(name)
            .ok_or_else(|| TenantError::NamespaceNotFound(name.to_string()))?;

        entry.value_mut().usage.measurements =
            entry.value().usage.measurements.saturating_add(count);
        Ok(())
    }

    /// Atomically check write quota and increment usage counters.
    ///
    /// Holds the DashMap shard lock for the namespace across both the
    /// quota check and the usage update, eliminating the TOCTOU race
    /// between a separate `check_write()` and `increment_series()`.
    ///
    /// The ingestion rate is tracked as a **windowed counter** that resets
    /// once per second. This prevents the rate from accumulating
    /// indefinitely and permanently blocking writes.
    ///
    /// # Errors
    ///
    /// Returns `NamespaceNotFound` if the namespace does not exist.
    /// Returns `QuotaExceeded` if any resource limit would be breached.
    pub fn check_and_increment_write(
        &self,
        name: &str,
        new_series: u64,
        new_points: u64,
    ) -> Result<()> {
        let mut entry = self
            .namespaces
            .get_mut(name)
            .ok_or_else(|| TenantError::NamespaceNotFound(name.to_string()))?;

        // Read quota limits and current usage into local copies to avoid
        // overlapping borrows between value() and value_mut().
        let max_series = entry.value().info.quota.max_series_count;
        let max_rate = entry.value().info.quota.max_ingestion_rate;
        let max_storage = entry.value().info.quota.max_storage_bytes;
        let current_series = entry.value().usage.series_count;
        let current_storage = entry.value().usage.storage_bytes;

        // 1. Series count
        let projected_series = current_series.saturating_add(new_series);
        if projected_series > max_series {
            return Err(TenantError::QuotaExceeded {
                namespace: name.to_string(),
                resource: "series_count".to_string(),
                current: current_series,
                limit: max_series,
            });
        }

        // 2. Ingestion rate — windowed per second.
        //    Reset the counter if we've moved into a new 1-second window.
        let now_s = now_ms() / 1000;
        let last_window = entry.value().usage.ingestion_rate_window_s;
        let current_rate = if last_window == now_s {
            entry.value().usage.ingestion_rate
        } else {
            // New window — reset rate.
            0.0
        };

        #[allow(clippy::cast_precision_loss)]
        let projected_rate = current_rate + new_points as f64;
        #[allow(clippy::cast_precision_loss)]
        let rate_limit = max_rate as f64;
        if projected_rate > rate_limit {
            #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
            let current = current_rate as u64;
            return Err(TenantError::QuotaExceeded {
                namespace: name.to_string(),
                resource: "ingestion_rate".to_string(),
                current,
                limit: max_rate,
            });
        }

        // 3. Storage (soft check — bytes tracked asynchronously)
        if current_storage > max_storage {
            return Err(TenantError::QuotaExceeded {
                namespace: name.to_string(),
                resource: "storage_bytes".to_string(),
                current: current_storage,
                limit: max_storage,
            });
        }

        // All checks passed — atomically update usage while still holding
        // the shard lock.
        let state_mut = entry.value_mut();
        state_mut.usage.series_count = projected_series;
        state_mut.usage.ingestion_rate = projected_rate;
        state_mut.usage.ingestion_rate_window_s = now_s;

        Ok(())
    }

    /// Atomically check measurement quota and increment count.
    ///
    /// Like [`check_and_increment_write`](Self::check_and_increment_write),
    /// this holds the shard lock across check and update.
    ///
    /// # Errors
    ///
    /// Returns `NamespaceNotFound` if the namespace does not exist.
    /// Returns `QuotaExceeded` if the measurement limit would be breached.
    pub fn check_and_increment_measurement(&self, name: &str) -> Result<()> {
        let mut entry = self
            .namespaces
            .get_mut(name)
            .ok_or_else(|| TenantError::NamespaceNotFound(name.to_string()))?;

        let state = entry.value();
        let projected = u64::from(state.usage.measurements) + 1;
        let limit = u64::from(state.info.quota.max_measurements);

        if projected > limit {
            return Err(TenantError::QuotaExceeded {
                namespace: name.to_string(),
                resource: "measurements".to_string(),
                current: u64::from(state.usage.measurements),
                limit,
            });
        }

        entry.value_mut().usage.measurements = state.usage.measurements.saturating_add(1);
        Ok(())
    }

    /// Compute usage ratios for all namespaces.
    ///
    /// Returns a list of `(namespace, resource, ratio)` tuples suitable for
    /// metric emission or admin API responses. The ratio is `current / limit`
    /// (0.0 when the limit is 0).
    #[must_use]
    pub fn usage_ratios(&self) -> Vec<UsageRatio> {
        let mut ratios = Vec::new();

        for entry in &self.namespaces {
            let ns = entry.key();
            let state = entry.value();
            let q = &state.info.quota;
            let u = &state.usage;

            ratios.push(UsageRatio {
                namespace: ns.clone(),
                resource: "series_count".to_string(),
                current: u.series_count,
                limit: q.max_series_count,
                ratio: ratio(u.series_count, q.max_series_count),
            });
            ratios.push(UsageRatio {
                namespace: ns.clone(),
                resource: "storage_bytes".to_string(),
                current: u.storage_bytes,
                limit: q.max_storage_bytes,
                ratio: ratio(u.storage_bytes, q.max_storage_bytes),
            });
            ratios.push(UsageRatio {
                namespace: ns.clone(),
                resource: "measurements".to_string(),
                current: u64::from(u.measurements),
                limit: u64::from(q.max_measurements),
                ratio: ratio(u64::from(u.measurements), u64::from(q.max_measurements)),
            });
        }

        ratios
    }

    /// Emit `chronix_namespace_usage_ratio` gauges for all namespaces.
    ///
    /// Intended to be called periodically (e.g. every 10 s) from a
    /// background task so dashboards always have fresh data, not just
    /// on write paths.
    pub fn emit_usage_metrics(&self) {
        for r in self.usage_ratios() {
            metrics::gauge!(
                "chronix_namespace_usage_ratio",
                "namespace" => r.namespace,
                "resource" => r.resource,
            )
            .set(r.ratio);
        }
    }
}

/// Per-resource usage ratio for a namespace.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UsageRatio {
    /// Namespace identifier.
    pub namespace: String,
    /// Resource type (e.g. `"series_count"`, `"storage_bytes"`).
    pub resource: String,
    /// Current usage value.
    pub current: u64,
    /// Configured limit.
    pub limit: u64,
    /// Usage ratio (0.0–1.0+). Values > 1.0 indicate over-quota.
    pub ratio: f64,
}

/// Compute a safe ratio avoiding division by zero.
///
/// When the limit is zero and usage is non-zero, returns [`f64::MAX`]
/// to indicate over-quota. This avoids returning `f64::INFINITY` which
/// is not valid JSON (RFC 7159) and breaks `serde_json` serialization.
#[allow(clippy::cast_precision_loss)]
fn ratio(current: u64, limit: u64) -> f64 {
    if limit == 0 {
        return if current > 0 { f64::MAX } else { 0.0 };
    }
    current as f64 / limit as f64
}

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

fn now_ms() -> u64 {
    #[allow(clippy::cast_possible_truncation)]
    SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

/// Create a shared, reference-counted registry.
#[must_use]
pub fn shared_registry() -> Arc<NamespaceRegistry> {
    Arc::new(NamespaceRegistry::new())
}

#[cfg(test)]
mod tests {

    /// **Creating a tenant must be durable before it is acknowledged.**
    ///
    /// A delete was — with a comment saying destructive operations must be —
    /// and a create was not: it snapshotted *best effort* on a background
    /// thread and returned `Ok` regardless. So an operator could provision a
    /// namespace, be told it exists, hand out a credential bound to it, and
    /// find after the next restart that every request from that tenant
    /// answers `400 namespace not found`. The mirror of the delete case, and
    /// the asymmetry is the tell that only one of the two was thought about.
    #[test]
    fn creating_a_namespace_is_durable_or_it_fails() {
        let dir = tempfile::tempdir().unwrap();
        let registry = NamespaceRegistry::open(dir.path()).unwrap();

        // A directory where the snapshot wants to write its temp file.
        std::fs::create_dir_all(dir.path().join("namespaces.json.tmp")).unwrap();

        let err = registry.create_namespace(
            NamespaceId::new("tenant-a").unwrap(),
            "a tenant",
            "ops",
            NamespaceQuota::default(),
        );
        assert!(
            err.is_err(),
            "a namespace that cannot be persisted must not be acknowledged"
        );
    }

    /// The same for a quota change, which `PUT /namespaces/{name}/quota`
    /// returns as though it had taken effect.
    #[test]
    fn changing_a_quota_is_durable_or_it_fails() {
        let dir = tempfile::tempdir().unwrap();
        let registry = NamespaceRegistry::open(dir.path()).unwrap();
        registry
            .create_namespace(
                NamespaceId::new("tenant-a").unwrap(),
                "a tenant",
                "ops",
                NamespaceQuota::default(),
            )
            .unwrap();

        std::fs::create_dir_all(dir.path().join("namespaces.json.tmp")).unwrap();

        let quota = NamespaceQuota {
            max_series_count: 42,
            ..NamespaceQuota::default()
        };
        assert!(
            registry.update_quota("tenant-a", quota).is_err(),
            "a quota that cannot be persisted reverts at the next restart, so \
             reporting it as applied is a setting that is accepted and ignored"
        );
    }
    use super::*;

    #[test]
    fn default_namespace_exists_on_creation() {
        let registry = NamespaceRegistry::new();
        assert!(registry.exists(NamespaceId::DEFAULT));
        assert_eq!(registry.count(), 1);
    }

    #[test]
    fn create_and_get_namespace() {
        let registry = NamespaceRegistry::new();
        let id = NamespaceId::new("team-alpha").unwrap();
        let info = registry
            .create_namespace(id, "Team Alpha", "admin", NamespaceQuota::default())
            .unwrap();

        assert_eq!(info.id.as_str(), "team-alpha");
        assert_eq!(info.owner, "admin");
        assert_eq!(registry.count(), 2);

        let state = registry.get("team-alpha").unwrap();
        assert_eq!(state.info.description, "Team Alpha");
    }

    #[test]
    fn create_duplicate_fails() {
        let registry = NamespaceRegistry::new();
        let id = NamespaceId::new("my-ns").unwrap();
        registry
            .create_namespace(id, "first", "admin", NamespaceQuota::default())
            .unwrap();

        let id2 = NamespaceId::new("my-ns").unwrap();
        let err = registry
            .create_namespace(id2, "second", "admin", NamespaceQuota::default())
            .unwrap_err();

        assert!(matches!(err, TenantError::NamespaceAlreadyExists(_)));
    }

    #[test]
    fn delete_namespace() {
        let registry = NamespaceRegistry::new();
        let id = NamespaceId::new("temp-ns").unwrap();
        registry
            .create_namespace(id, "temp", "admin", NamespaceQuota::default())
            .unwrap();

        assert_eq!(registry.count(), 2);
        registry.delete_namespace("temp-ns").unwrap();
        assert_eq!(registry.count(), 1);
        assert!(!registry.exists("temp-ns"));
    }

    #[test]
    fn cannot_delete_default_namespace() {
        let registry = NamespaceRegistry::new();
        let err = registry.delete_namespace("default").unwrap_err();
        assert!(matches!(err, TenantError::InvalidConfig(_)));
    }

    #[test]
    fn delete_nonexistent_fails() {
        let registry = NamespaceRegistry::new();
        let err = registry.delete_namespace("nope").unwrap_err();
        assert!(matches!(err, TenantError::NamespaceNotFound(_)));
    }

    #[test]
    fn list_namespaces() {
        let registry = NamespaceRegistry::new();
        let id = NamespaceId::new("ns-a").unwrap();
        registry
            .create_namespace(id, "A", "admin", NamespaceQuota::default())
            .unwrap();
        let id = NamespaceId::new("ns-b").unwrap();
        registry
            .create_namespace(id, "B", "admin", NamespaceQuota::default())
            .unwrap();

        let mut names = registry.list_names();
        names.sort();
        assert_eq!(names, vec!["default", "ns-a", "ns-b"]);
    }

    #[test]
    fn update_quota() {
        let registry = NamespaceRegistry::new();
        let id = NamespaceId::new("quota-ns").unwrap();
        registry
            .create_namespace(id, "quota test", "admin", NamespaceQuota::default())
            .unwrap();

        let new_quota = NamespaceQuota {
            max_series_count: 500,
            max_ingestion_rate: 1000,
            max_storage_bytes: 1024,
            max_measurements: 10,
            max_request_rps: 0,
            max_request_burst: 0,
        };

        registry
            .update_quota("quota-ns", new_quota.clone())
            .unwrap();
        let state = registry.get("quota-ns").unwrap();
        assert_eq!(state.info.quota, new_quota);
    }

    #[test]
    fn usage_tracking() {
        let registry = NamespaceRegistry::new();
        let id = NamespaceId::new("usage-ns").unwrap();
        registry
            .create_namespace(id, "usage test", "admin", NamespaceQuota::default())
            .unwrap();

        registry.increment_series("usage-ns", 100).unwrap();
        registry.increment_storage("usage-ns", 4096).unwrap();
        registry.increment_measurements("usage-ns", 5).unwrap();

        let usage = registry.get_usage("usage-ns").unwrap();
        assert_eq!(usage.series_count, 100);
        assert_eq!(usage.storage_bytes, 4096);
        assert_eq!(usage.measurements, 5);
    }

    #[test]
    fn shared_registry_is_arc() {
        let reg = shared_registry();
        let reg2 = reg.clone();
        let id = NamespaceId::new("shared-test").unwrap();
        reg.create_namespace(id, "test", "admin", NamespaceQuota::default())
            .unwrap();
        assert!(reg2.exists("shared-test"));
    }

    #[test]
    fn namespace_id_validation() {
        assert!(NamespaceId::new("valid-name").is_ok());
        assert!(NamespaceId::new("a123").is_ok());
        assert!(NamespaceId::new("").is_err()); // empty
        assert!(NamespaceId::new("Invalid").is_err()); // uppercase
        assert!(NamespaceId::new("has space").is_err()); // space
        assert!(NamespaceId::new("-leading").is_err()); // leading hyphen
        assert!(NamespaceId::new("trailing-").is_err()); // trailing hyphen
        assert!(NamespaceId::new("a".repeat(64)).is_err()); // too long
        assert!(NamespaceId::new("a".repeat(63)).is_ok()); // max length ok
    }

    // ── Usage ratios ──────────────────────────────────────────

    #[test]
    fn usage_ratios_empty_registry() {
        let registry = NamespaceRegistry::new();
        let ratios = registry.usage_ratios();
        // Only "default" namespace → 3 resources
        assert_eq!(ratios.len(), 3);
        for r in &ratios {
            assert_eq!(r.namespace, "default");
            assert!((r.ratio - 0.0).abs() < f64::EPSILON);
        }
    }

    #[test]
    fn usage_ratios_with_usage() {
        let registry = NamespaceRegistry::new();
        let id = NamespaceId::new("metrics-ns").unwrap();
        let quota = NamespaceQuota {
            max_series_count: 100,
            max_ingestion_rate: 1000,
            max_storage_bytes: 10_000,
            max_measurements: 20,
            max_request_rps: 0,
            max_request_burst: 0,
        };
        registry
            .create_namespace(id, "test", "admin", quota)
            .unwrap();

        registry.increment_series("metrics-ns", 50).unwrap();
        registry.increment_storage("metrics-ns", 7500).unwrap();
        registry.increment_measurements("metrics-ns", 10).unwrap();

        let ratios = registry.usage_ratios();
        let ns_ratios: Vec<_> = ratios
            .iter()
            .filter(|r| r.namespace == "metrics-ns")
            .collect();
        assert_eq!(ns_ratios.len(), 3);

        let series = ns_ratios
            .iter()
            .find(|r| r.resource == "series_count")
            .unwrap();
        assert!((series.ratio - 0.5).abs() < f64::EPSILON);
        assert_eq!(series.current, 50);
        assert_eq!(series.limit, 100);

        let storage = ns_ratios
            .iter()
            .find(|r| r.resource == "storage_bytes")
            .unwrap();
        assert!((storage.ratio - 0.75).abs() < f64::EPSILON);

        let measurements = ns_ratios
            .iter()
            .find(|r| r.resource == "measurements")
            .unwrap();
        assert!((measurements.ratio - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn emit_usage_metrics_does_not_panic() {
        let registry = NamespaceRegistry::new();
        let id = NamespaceId::new("emit-ns").unwrap();
        registry
            .create_namespace(id, "test", "admin", NamespaceQuota::default())
            .unwrap();
        registry.increment_series("emit-ns", 42).unwrap();

        // Should not panic (metrics crate handles missing recorder gracefully).
        registry.emit_usage_metrics();
    }

    #[test]
    fn usage_ratio_zero_limit() {
        // current > 0, limit == 0 → over-quota → f64::MAX (JSON-safe)
        let r = ratio(100, 0);
        assert_eq!(r, f64::MAX);

        // current == 0, limit == 0 → 0.0
        let r = ratio(0, 0);
        assert!((r - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn usage_ratio_over_quota() {
        let r = ratio(200, 100);
        assert!((r - 2.0).abs() < f64::EPSILON);
    }

    #[test]
    fn open_creates_and_loads_snapshot() {
        let tmp = tempfile::tempdir().unwrap();
        let dir = tmp.path().join("namespaces");

        // Open creates directory and initial snapshot
        {
            let reg = NamespaceRegistry::open(&dir).unwrap();
            let id = NamespaceId::new("test-ns").unwrap();
            reg.create_namespace(id, "Test", "admin", NamespaceQuota::default())
                .unwrap();
            assert_eq!(reg.count(), 2);
        }

        // Reopen loads the snapshot
        {
            let reg = NamespaceRegistry::open(&dir).unwrap();
            assert_eq!(reg.count(), 2);
            assert!(reg.exists("test-ns"));
            assert!(reg.exists(NamespaceId::DEFAULT));
        }
    }

    #[test]
    fn open_delete_persists() {
        let tmp = tempfile::tempdir().unwrap();
        let dir = tmp.path().join("namespaces");

        {
            let reg = NamespaceRegistry::open(&dir).unwrap();
            let id = NamespaceId::new("ephemeral").unwrap();
            reg.create_namespace(id, "Ephemeral", "admin", NamespaceQuota::default())
                .unwrap();
            reg.delete_namespace("ephemeral").unwrap();
        }

        {
            let reg = NamespaceRegistry::open(&dir).unwrap();
            assert_eq!(reg.count(), 1);
            assert!(!reg.exists("ephemeral"));
        }
    }

    #[test]
    fn open_quota_update_persists() {
        let tmp = tempfile::tempdir().unwrap();
        let dir = tmp.path().join("namespaces");

        {
            let reg = NamespaceRegistry::open(&dir).unwrap();
            let id = NamespaceId::new("quotans").unwrap();
            reg.create_namespace(id, "Quota Test", "admin", NamespaceQuota::default())
                .unwrap();
            reg.update_quota(
                "quotans",
                NamespaceQuota {
                    max_series_count: 42,
                    ..NamespaceQuota::default()
                },
            )
            .unwrap();
        }

        {
            let reg = NamespaceRegistry::open(&dir).unwrap();
            let state = reg.get("quotans").unwrap();
            assert_eq!(state.info.quota.max_series_count, 42);
        }
    }
}