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
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
//! Disk-based cache storage backend
//!
//! Implements Pingora's `Storage` trait using the local filesystem. Each cached
//! response is stored as a pair of files (`.meta` + `.body`) distributed across
//! sharded subdirectories to keep per-directory inode counts manageable.
//!
//! # Directory layout
//!
//! ```text
//! <base_path>/
//!   shard-00/
//!     <2-char-hex-prefix>/
//!       <combined-hex-hash>.meta
//!       <combined-hex-hash>.body
//!     tmp/
//!   shard-01/
//!     ...
//! ```

use async_trait::async_trait;
use bytes::Bytes;
use pingora_cache::eviction::EvictionManager;
use pingora_cache::key::{CacheHashKey, CacheKey, CompactCacheKey};
use pingora_cache::meta::CacheMeta;
use pingora_cache::storage::{
    HandleHit, HandleMiss, HitHandler, MissFinishType, MissHandler, PurgeType, Storage,
};
use pingora_cache::trace::SpanHandle;
use pingora_core::{Error, ErrorType, Result};
use std::any::Any;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, error, info, warn};

// ============================================================================
// DiskCacheStorage
// ============================================================================

/// Disk-based cache storage backend implementing Pingora's `Storage` trait.
///
/// All disk I/O is performed via `tokio::task::spawn_blocking` to avoid
/// blocking the async runtime.
pub struct DiskCacheStorage {
    base_path: PathBuf,
    num_shards: u32,
    #[allow(dead_code)]
    max_size_bytes: usize,
    /// Tracks in-flight writes: combined_hash -> set of temp_ids
    inflight: Arc<RwLock<HashMap<String, HashSet<u64>>>>,
    next_temp_id: AtomicU64,
}

impl DiskCacheStorage {
    /// Create a new `DiskCacheStorage`.
    ///
    /// Creates the shard directory structure and cleans up any orphaned `.tmp`
    /// files left behind by interrupted writes.
    pub fn new(path: &Path, shards: u32, max_size: usize) -> Self {
        let base = path.to_path_buf();

        // Create shard dirs, hex-prefix subdirs, and tmp dirs
        for shard in 0..shards {
            let shard_dir = base.join(format!("shard-{:02}", shard));

            // Create all 256 hex-prefix subdirs
            for prefix in 0..=255u8 {
                let prefix_dir = shard_dir.join(format!("{:02x}", prefix));
                if let Err(e) = std::fs::create_dir_all(&prefix_dir) {
                    error!(path = %prefix_dir.display(), error = %e, "Failed to create prefix dir");
                }
            }

            // Create tmp dir and clean orphaned files
            let tmp_dir = shard_dir.join("tmp");
            if let Err(e) = std::fs::create_dir_all(&tmp_dir) {
                error!(path = %tmp_dir.display(), error = %e, "Failed to create tmp dir");
            } else {
                Self::clean_orphaned_tmp(&tmp_dir);
            }
        }

        info!(
            path = %base.display(),
            shards,
            max_size_mb = max_size / 1024 / 1024,
            "Disk cache storage initialized"
        );

        Self {
            base_path: base,
            num_shards: shards,
            max_size_bytes: max_size,
            inflight: Arc::new(RwLock::new(HashMap::new())),
            next_temp_id: AtomicU64::new(1),
        }
    }

    /// Remove orphaned .tmp files from a tmp directory.
    fn clean_orphaned_tmp(tmp_dir: &Path) {
        let entries = match std::fs::read_dir(tmp_dir) {
            Ok(e) => e,
            Err(_) => return,
        };
        let mut cleaned = 0u64;
        for entry in entries.flatten() {
            let path = entry.path();
            if path.extension().and_then(|e| e.to_str()) == Some("tmp") {
                if let Err(e) = std::fs::remove_file(&path) {
                    warn!(path = %path.display(), error = %e, "Failed to clean orphaned tmp file");
                } else {
                    cleaned += 1;
                }
            }
        }
        if cleaned > 0 {
            info!(dir = %tmp_dir.display(), cleaned, "Cleaned orphaned tmp files");
        }
    }

    // ========================================================================
    // Path helpers
    // ========================================================================

    /// Compute the shard index for a combined hex hash string.
    fn shard_for_key(combined: &str, num_shards: u32) -> u32 {
        // Use first two hex chars (one byte) to determine shard
        let byte = u8::from_str_radix(&combined[..2], 16).unwrap_or(0);
        (byte as u32) % num_shards
    }

    /// Compute the 2-char hex prefix subdirectory for a combined hex hash.
    fn prefix_for_key(combined: &str) -> &str {
        // Use chars 2..4 (second byte) as prefix subdir
        &combined[2..4]
    }

    /// Full path to the `.meta` file for a given combined hex hash.
    fn meta_path(&self, combined: &str) -> PathBuf {
        let shard = Self::shard_for_key(combined, self.num_shards);
        let prefix = Self::prefix_for_key(combined);
        self.base_path
            .join(format!("shard-{:02}", shard))
            .join(prefix)
            .join(format!("{}.meta", combined))
    }

    /// Full path to the `.body` file for a given combined hex hash.
    fn body_path(&self, combined: &str) -> PathBuf {
        let shard = Self::shard_for_key(combined, self.num_shards);
        let prefix = Self::prefix_for_key(combined);
        self.base_path
            .join(format!("shard-{:02}", shard))
            .join(prefix)
            .join(format!("{}.body", combined))
    }

    /// Path to the tmp directory for a given combined hex hash (shard-local).
    fn tmp_dir_for_key(&self, combined: &str) -> PathBuf {
        let shard = Self::shard_for_key(combined, self.num_shards);
        self.base_path
            .join(format!("shard-{:02}", shard))
            .join("tmp")
    }
}

// ============================================================================
// Meta file serialization helpers
// ============================================================================

/// Serialize CacheMeta to the on-disk format.
///
/// Format: `[4 bytes: internal_meta_len as u32 LE][internal_meta bytes][header bytes]`
fn serialize_meta_to_disk(meta: &CacheMeta) -> Result<Vec<u8>> {
    let (internal, header) = meta.serialize()?;
    let internal_len = internal.len() as u32;
    let mut buf = Vec::with_capacity(4 + internal.len() + header.len());
    buf.extend_from_slice(&internal_len.to_le_bytes());
    buf.extend_from_slice(&internal);
    buf.extend_from_slice(&header);
    Ok(buf)
}

/// Deserialize CacheMeta from the on-disk format.
fn deserialize_meta_from_disk(data: &[u8]) -> Result<CacheMeta> {
    if data.len() < 4 {
        return Error::e_explain(ErrorType::FileReadError, "meta file too short");
    }
    let internal_len = u32::from_le_bytes([data[0], data[1], data[2], data[3]]) as usize;
    if data.len() < 4 + internal_len {
        return Error::e_explain(ErrorType::FileReadError, "meta file truncated");
    }
    let internal = &data[4..4 + internal_len];
    let header = &data[4 + internal_len..];
    CacheMeta::deserialize(internal, header)
}

// ============================================================================
// DiskHitHandler
// ============================================================================

/// Hit handler for disk cache lookups.
///
/// Loads the full body into memory for seekable access.
pub struct DiskHitHandler {
    body: Vec<u8>,
    meta_size: usize,
    done: bool,
    range_start: usize,
    range_end: usize,
}

#[async_trait]
impl HandleHit for DiskHitHandler {
    async fn read_body(&mut self) -> Result<Option<Bytes>> {
        if self.done {
            return Ok(None);
        }
        self.done = true;
        Ok(Some(Bytes::copy_from_slice(
            &self.body[self.range_start..self.range_end],
        )))
    }

    async fn finish(
        self: Box<Self>,
        _storage: &'static (dyn Storage + Sync),
        _key: &CacheKey,
        _trace: &SpanHandle,
    ) -> Result<()> {
        Ok(())
    }

    fn can_seek(&self) -> bool {
        true
    }

    fn seek(&mut self, start: usize, end: Option<usize>) -> Result<()> {
        if start >= self.body.len() {
            return Error::e_explain(
                ErrorType::InternalError,
                format!("seek start out of range {} >= {}", start, self.body.len()),
            );
        }
        self.range_start = start;
        if let Some(end) = end {
            self.range_end = std::cmp::min(self.body.len(), end);
        }
        self.done = false;
        Ok(())
    }

    fn get_eviction_weight(&self) -> usize {
        self.meta_size + self.body.len()
    }

    fn as_any(&self) -> &(dyn Any + Send + Sync) {
        self
    }

    fn as_any_mut(&mut self) -> &mut (dyn Any + Send + Sync) {
        self
    }
}

// ============================================================================
// DiskMissHandler
// ============================================================================

/// Miss handler for disk cache writes.
///
/// Accumulates the response body in memory, then atomically writes both
/// `.meta` and `.body` files to disk via temp-file + rename.
pub struct DiskMissHandler {
    body_buffer: Vec<u8>,
    serialized_meta: Vec<u8>,
    combined: String,
    meta_path: PathBuf,
    body_path: PathBuf,
    tmp_dir: PathBuf,
    temp_id: u64,
    inflight: Arc<RwLock<HashMap<String, HashSet<u64>>>>,
    finished: bool,
}

#[async_trait]
impl HandleMiss for DiskMissHandler {
    async fn write_body(&mut self, data: Bytes, _eof: bool) -> Result<()> {
        self.body_buffer.extend_from_slice(&data);
        Ok(())
    }

    async fn finish(mut self: Box<Self>) -> Result<MissFinishType> {
        self.finished = true;
        let body = std::mem::take(&mut self.body_buffer);
        let meta = self.serialized_meta.clone();
        let meta_path = self.meta_path.clone();
        let body_path = self.body_path.clone();
        let tmp_dir = self.tmp_dir.clone();
        let temp_id = self.temp_id;

        let size = meta.len() + body.len();

        // Write to disk via spawn_blocking
        tokio::task::spawn_blocking(move || {
            let tmp_meta = tmp_dir.join(format!("{}.meta.tmp", temp_id));
            let tmp_body = tmp_dir.join(format!("{}.body.tmp", temp_id));

            // Write meta temp file
            if let Err(e) = std::fs::write(&tmp_meta, &meta) {
                error!(path = %tmp_meta.display(), error = %e, "Failed to write tmp meta");
                let _ = std::fs::remove_file(&tmp_meta);
                return Err(Error::explain(
                    ErrorType::WriteError,
                    format!("failed to write meta: {}", e),
                ));
            }

            // Write body temp file
            if let Err(e) = std::fs::write(&tmp_body, &body) {
                error!(path = %tmp_body.display(), error = %e, "Failed to write tmp body");
                let _ = std::fs::remove_file(&tmp_meta);
                let _ = std::fs::remove_file(&tmp_body);
                return Err(Error::explain(
                    ErrorType::WriteError,
                    format!("failed to write body: {}", e),
                ));
            }

            // Atomic rename meta
            if let Err(e) = std::fs::rename(&tmp_meta, &meta_path) {
                error!(error = %e, "Failed to rename tmp meta to final path");
                let _ = std::fs::remove_file(&tmp_meta);
                let _ = std::fs::remove_file(&tmp_body);
                return Err(Error::explain(
                    ErrorType::WriteError,
                    format!("failed to rename meta: {}", e),
                ));
            }

            // Atomic rename body
            if let Err(e) = std::fs::rename(&tmp_body, &body_path) {
                error!(error = %e, "Failed to rename tmp body to final path");
                // Meta already renamed; remove it to stay consistent
                let _ = std::fs::remove_file(&meta_path);
                return Err(Error::explain(
                    ErrorType::WriteError,
                    format!("failed to rename body: {}", e),
                ));
            }

            Ok(())
        })
        .await
        .map_err(|e| {
            Error::explain(
                ErrorType::InternalError,
                format!("spawn_blocking join error: {}", e),
            )
        })??;

        // Remove from inflight tracking
        {
            let mut inflight = self.inflight.write().await;
            if let Some(set) = inflight.get_mut(&self.combined) {
                set.remove(&self.temp_id);
                if set.is_empty() {
                    inflight.remove(&self.combined);
                }
            }
        }

        debug!(combined = %self.combined, size, "Disk cache entry written");
        Ok(MissFinishType::Created(size))
    }
}

impl Drop for DiskMissHandler {
    fn drop(&mut self) {
        if !self.finished {
            // Clean up inflight tracking if finish() was never called.
            // We can't do async in Drop, so use try_write.
            if let Ok(mut inflight) = self.inflight.try_write() {
                if let Some(set) = inflight.get_mut(&self.combined) {
                    set.remove(&self.temp_id);
                    if set.is_empty() {
                        inflight.remove(&self.combined);
                    }
                }
            }
        }
    }
}

// ============================================================================
// Storage trait implementation
// ============================================================================

#[async_trait]
impl Storage for DiskCacheStorage {
    async fn lookup(
        &'static self,
        key: &CacheKey,
        _trace: &SpanHandle,
    ) -> Result<Option<(CacheMeta, HitHandler)>> {
        let combined = key.combined();
        let meta_path = self.meta_path(&combined);
        let body_path = self.body_path(&combined);

        let result =
            tokio::task::spawn_blocking(move || -> Result<Option<(CacheMeta, HitHandler)>> {
                let meta_data = match std::fs::read(&meta_path) {
                    Ok(d) => d,
                    Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
                    Err(e) => {
                        debug!(error = %e, "Failed to read cache meta file");
                        return Ok(None);
                    }
                };

                let body_data = match std::fs::read(&body_path) {
                    Ok(d) => d,
                    Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
                    Err(e) => {
                        debug!(error = %e, "Failed to read cache body file");
                        return Ok(None);
                    }
                };

                let meta = match deserialize_meta_from_disk(&meta_data) {
                    Ok(m) => m,
                    Err(e) => {
                        warn!(error = %e, "Corrupted cache meta, removing entry");
                        let _ = std::fs::remove_file(&meta_path);
                        let _ = std::fs::remove_file(&body_path);
                        return Ok(None);
                    }
                };

                let body_len = body_data.len();
                let hit_handler = DiskHitHandler {
                    body: body_data,
                    meta_size: meta_data.len(),
                    done: false,
                    range_start: 0,
                    range_end: body_len,
                };

                Ok(Some((meta, Box::new(hit_handler) as HitHandler)))
            })
            .await
            .map_err(|e| {
                Error::explain(
                    ErrorType::InternalError,
                    format!("spawn_blocking join error: {}", e),
                )
            })??;

        Ok(result)
    }

    async fn get_miss_handler(
        &'static self,
        key: &CacheKey,
        meta: &CacheMeta,
        _trace: &SpanHandle,
    ) -> Result<MissHandler> {
        let combined = key.combined();
        let serialized_meta = serialize_meta_to_disk(meta)?;
        let meta_path = self.meta_path(&combined);
        let body_path = self.body_path(&combined);
        let tmp_dir = self.tmp_dir_for_key(&combined);
        let temp_id = self.next_temp_id.fetch_add(1, Ordering::Relaxed);

        // Register in inflight tracking
        {
            let mut inflight = self.inflight.write().await;
            inflight
                .entry(combined.clone())
                .or_default()
                .insert(temp_id);
        }

        Ok(Box::new(DiskMissHandler {
            body_buffer: Vec::new(),
            serialized_meta,
            combined,
            meta_path,
            body_path,
            tmp_dir,
            temp_id,
            inflight: self.inflight.clone(),
            finished: false,
        }))
    }

    async fn purge(
        &'static self,
        key: &CompactCacheKey,
        _purge_type: PurgeType,
        _trace: &SpanHandle,
    ) -> Result<bool> {
        let combined = key.combined();
        let meta_path = self.meta_path(&combined);
        let body_path = self.body_path(&combined);

        let removed = tokio::task::spawn_blocking(move || {
            let meta_removed = std::fs::remove_file(&meta_path).is_ok();
            let body_removed = std::fs::remove_file(&body_path).is_ok();
            meta_removed || body_removed
        })
        .await
        .map_err(|e| {
            Error::explain(
                ErrorType::InternalError,
                format!("spawn_blocking join error: {}", e),
            )
        })?;

        // Also remove from inflight tracking
        {
            let mut inflight = self.inflight.write().await;
            inflight.remove(&combined);
        }

        Ok(removed)
    }

    async fn update_meta(
        &'static self,
        key: &CacheKey,
        meta: &CacheMeta,
        _trace: &SpanHandle,
    ) -> Result<bool> {
        let combined = key.combined();
        let serialized = serialize_meta_to_disk(meta)?;
        let meta_path = self.meta_path(&combined);
        let tmp_dir = self.tmp_dir_for_key(&combined);

        tokio::task::spawn_blocking(move || {
            // Atomic rewrite: write to tmp, rename over existing
            let tmp_path = tmp_dir.join(format!("{}.meta.update.tmp", combined));
            std::fs::write(&tmp_path, &serialized).map_err(|e| {
                Error::explain(
                    ErrorType::WriteError,
                    format!("failed to write updated meta: {}", e),
                )
            })?;
            std::fs::rename(&tmp_path, &meta_path).map_err(|e| {
                let _ = std::fs::remove_file(&tmp_path);
                Error::explain(
                    ErrorType::WriteError,
                    format!("failed to rename updated meta: {}", e),
                )
            })?;
            Ok(true)
        })
        .await
        .map_err(|e| {
            Error::explain(
                ErrorType::InternalError,
                format!("spawn_blocking join error: {}", e),
            )
        })?
    }

    fn support_streaming_partial_write(&self) -> bool {
        false
    }

    fn as_any(&self) -> &(dyn Any + Send + Sync + 'static) {
        self
    }
}

// ============================================================================
// Eviction state rebuild
// ============================================================================

/// Scan disk entries and register them with the eviction manager.
///
/// This is called at startup to rebuild the LRU eviction state from the
/// files on disk.
pub async fn rebuild_eviction_state(
    base_path: &Path,
    num_shards: u32,
    eviction: &'static pingora_cache::eviction::simple_lru::Manager,
) {
    let base = base_path.to_path_buf();
    let result = tokio::task::spawn_blocking(move || {
        let mut count = 0usize;
        let mut total_size = 0usize;

        for shard in 0..num_shards {
            let shard_dir = base.join(format!("shard-{:02}", shard));

            for prefix in 0..=255u8 {
                let prefix_dir = shard_dir.join(format!("{:02x}", prefix));
                let entries = match std::fs::read_dir(&prefix_dir) {
                    Ok(e) => e,
                    Err(_) => continue,
                };

                for entry in entries.flatten() {
                    let path = entry.path();
                    let ext = path.extension().and_then(|e| e.to_str());
                    if ext != Some("body") {
                        continue;
                    }

                    // Extract combined hash from filename
                    let stem = match path.file_stem().and_then(|s| s.to_str()) {
                        Some(s) => s.to_string(),
                        None => continue,
                    };

                    // Get file size for weight
                    let body_size = match std::fs::metadata(&path) {
                        Ok(m) => m.len() as usize,
                        Err(_) => continue,
                    };

                    // Also add meta size
                    let meta_path = prefix_dir.join(format!("{}.meta", stem));
                    let meta_size = std::fs::metadata(&meta_path)
                        .map(|m| m.len() as usize)
                        .unwrap_or(0);

                    let size = body_size + meta_size;

                    // Reconstruct CompactCacheKey from the combined hex hash
                    if let Some(primary) = pingora_cache::key::str2hex(&stem) {
                        let compact = CompactCacheKey {
                            primary,
                            variance: None,
                            user_tag: "".into(),
                        };

                        // Admit to eviction manager (use epoch as fresh_until since
                        // we don't know the actual TTL without parsing meta)
                        let _ = eviction.admit(
                            compact,
                            size,
                            std::time::SystemTime::now() + std::time::Duration::from_secs(3600),
                        );

                        count += 1;
                        total_size += size;
                    }
                }
            }
        }

        (count, total_size)
    })
    .await;

    match result {
        Ok((count, total_size)) => {
            info!(
                entries = count,
                total_size_mb = total_size / 1024 / 1024,
                "Rebuilt disk cache eviction state"
            );
        }
        Err(e) => {
            error!(error = %e, "Failed to rebuild disk cache eviction state");
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use once_cell::sync::Lazy;
    use pingora_cache::trace::Span;
    use pingora_http::ResponseHeader;
    use std::time::SystemTime;
    use tempfile::TempDir;

    fn create_test_meta() -> CacheMeta {
        let mut header = ResponseHeader::build(200, None).unwrap();
        header.append_header("content-type", "text/plain").unwrap();
        header.append_header("x-test", "disk-cache").unwrap();
        CacheMeta::new(
            SystemTime::now() + std::time::Duration::from_secs(3600),
            SystemTime::now(),
            60,
            300,
            header,
        )
    }

    fn span() -> SpanHandle {
        Span::inactive().handle()
    }

    #[test]
    fn test_directory_creation() {
        let tmp = TempDir::new().unwrap();
        let _storage = DiskCacheStorage::new(tmp.path(), 4, 100 * 1024 * 1024);

        // Verify shard dirs exist
        for shard in 0..4u32 {
            let shard_dir = tmp.path().join(format!("shard-{:02}", shard));
            assert!(shard_dir.is_dir(), "shard dir should exist");

            // Verify some prefix dirs exist
            assert!(shard_dir.join("00").is_dir());
            assert!(shard_dir.join("ff").is_dir());
            assert!(shard_dir.join("a5").is_dir());

            // Verify tmp dir exists
            assert!(shard_dir.join("tmp").is_dir());
        }

        // Shard-04 should not exist
        assert!(!tmp.path().join("shard-04").exists());
    }

    #[test]
    fn test_path_helpers() {
        let tmp = TempDir::new().unwrap();
        let storage = DiskCacheStorage::new(tmp.path(), 16, 100 * 1024 * 1024);

        // "ab" prefix -> shard = 0xab % 16 = 11, prefix subdir = second byte
        let combined = "abcd1234567890abcdef1234567890ab";

        let shard = DiskCacheStorage::shard_for_key(combined, 16);
        assert_eq!(shard, 0xab % 16); // 171 % 16 = 11

        let prefix = DiskCacheStorage::prefix_for_key(combined);
        assert_eq!(prefix, "cd");

        let meta = storage.meta_path(combined);
        assert!(meta.to_str().unwrap().contains("shard-11"));
        assert!(meta.to_str().unwrap().contains("/cd/"));
        assert!(meta.to_str().unwrap().ends_with(".meta"));

        let body = storage.body_path(combined);
        assert!(body.to_str().unwrap().contains("shard-11"));
        assert!(body.to_str().unwrap().contains("/cd/"));
        assert!(body.to_str().unwrap().ends_with(".body"));
    }

    #[tokio::test]
    async fn test_write_and_read() {
        static STORAGE: Lazy<DiskCacheStorage> = Lazy::new(|| {
            let path = std::env::temp_dir().join("zentinel-disk-cache-test-write-read");
            let _ = std::fs::remove_dir_all(&path);
            DiskCacheStorage::new(&path, 4, 100 * 1024 * 1024)
        });
        let trace = &span();

        let key = CacheKey::new("", "test-write-read", "1");
        let meta = create_test_meta();

        // Lookup should return None initially
        let result = STORAGE.lookup(&key, trace).await.unwrap();
        assert!(result.is_none());

        // Write via miss handler
        let mut miss_handler = STORAGE.get_miss_handler(&key, &meta, trace).await.unwrap();
        miss_handler
            .write_body(b"hello "[..].into(), false)
            .await
            .unwrap();
        miss_handler
            .write_body(b"world"[..].into(), true)
            .await
            .unwrap();
        let finish_result = miss_handler.finish().await.unwrap();
        assert!(matches!(finish_result, MissFinishType::Created(_)));

        // Lookup should now return the cached entry
        let (read_meta, mut hit_handler) = STORAGE.lookup(&key, trace).await.unwrap().unwrap();
        assert_eq!(read_meta.response_header().status.as_u16(), 200);

        let body = hit_handler.read_body().await.unwrap().unwrap();
        assert_eq!(body.as_ref(), b"hello world");

        // Second read should return None
        let body2 = hit_handler.read_body().await.unwrap();
        assert!(body2.is_none());

        // Cleanup
        let _ = std::fs::remove_dir_all(
            std::env::temp_dir().join("zentinel-disk-cache-test-write-read"),
        );
    }

    #[tokio::test]
    async fn test_purge() {
        static STORAGE: Lazy<DiskCacheStorage> = Lazy::new(|| {
            let path = std::env::temp_dir().join("zentinel-disk-cache-test-purge");
            let _ = std::fs::remove_dir_all(&path);
            DiskCacheStorage::new(&path, 4, 100 * 1024 * 1024)
        });
        let trace = &span();

        let key = CacheKey::new("", "test-purge", "1");
        let meta = create_test_meta();

        // Write an entry
        let mut miss_handler = STORAGE.get_miss_handler(&key, &meta, trace).await.unwrap();
        miss_handler
            .write_body(b"purge-me"[..].into(), true)
            .await
            .unwrap();
        miss_handler.finish().await.unwrap();

        // Verify it's there
        assert!(STORAGE.lookup(&key, trace).await.unwrap().is_some());

        // Purge it
        let compact = key.to_compact();
        let purged = STORAGE
            .purge(&compact, PurgeType::Invalidation, trace)
            .await
            .unwrap();
        assert!(purged);

        // Verify it's gone
        assert!(STORAGE.lookup(&key, trace).await.unwrap().is_none());

        // Cleanup
        let _ =
            std::fs::remove_dir_all(std::env::temp_dir().join("zentinel-disk-cache-test-purge"));
    }

    #[tokio::test]
    async fn test_update_meta() {
        static STORAGE: Lazy<DiskCacheStorage> = Lazy::new(|| {
            let path = std::env::temp_dir().join("zentinel-disk-cache-test-update-meta");
            let _ = std::fs::remove_dir_all(&path);
            DiskCacheStorage::new(&path, 4, 100 * 1024 * 1024)
        });
        let trace = &span();

        let key = CacheKey::new("", "test-update-meta", "1");
        let meta = create_test_meta();

        // Write an entry
        let mut miss_handler = STORAGE.get_miss_handler(&key, &meta, trace).await.unwrap();
        miss_handler
            .write_body(b"body-data"[..].into(), true)
            .await
            .unwrap();
        miss_handler.finish().await.unwrap();

        // Create updated meta with different header
        let mut new_header = ResponseHeader::build(200, None).unwrap();
        new_header
            .append_header("content-type", "application/json")
            .unwrap();
        new_header.append_header("x-updated", "true").unwrap();
        let new_meta = CacheMeta::new(
            SystemTime::now() + std::time::Duration::from_secs(7200),
            SystemTime::now(),
            120,
            600,
            new_header,
        );

        // Update meta
        let updated = STORAGE.update_meta(&key, &new_meta, trace).await.unwrap();
        assert!(updated);

        // Verify updated meta
        let (read_meta, _hit) = STORAGE.lookup(&key, trace).await.unwrap().unwrap();
        let headers = read_meta.response_header().headers.clone();
        assert_eq!(headers.get("x-updated").unwrap().to_str().unwrap(), "true");

        // Cleanup
        let _ = std::fs::remove_dir_all(
            std::env::temp_dir().join("zentinel-disk-cache-test-update-meta"),
        );
    }

    #[tokio::test]
    async fn test_miss_handler_drop() {
        static STORAGE: Lazy<DiskCacheStorage> = Lazy::new(|| {
            let path = std::env::temp_dir().join("zentinel-disk-cache-test-miss-drop");
            let _ = std::fs::remove_dir_all(&path);
            DiskCacheStorage::new(&path, 4, 100 * 1024 * 1024)
        });
        let trace = &span();

        let key = CacheKey::new("", "test-miss-drop", "1");
        let meta = create_test_meta();

        // Create miss handler and write some data but don't finish
        {
            let mut miss_handler = STORAGE.get_miss_handler(&key, &meta, trace).await.unwrap();
            miss_handler
                .write_body(b"incomplete"[..].into(), false)
                .await
                .unwrap();
            // Drop without finish
        }

        // Verify no files were written
        assert!(STORAGE.lookup(&key, trace).await.unwrap().is_none());

        // Verify inflight tracking was cleaned up
        assert!(STORAGE.inflight.read().await.is_empty());

        // Cleanup
        let _ = std::fs::remove_dir_all(
            std::env::temp_dir().join("zentinel-disk-cache-test-miss-drop"),
        );
    }

    #[tokio::test]
    async fn test_corrupted_meta() {
        static STORAGE: Lazy<DiskCacheStorage> = Lazy::new(|| {
            let path = std::env::temp_dir().join("zentinel-disk-cache-test-corrupted");
            let _ = std::fs::remove_dir_all(&path);
            DiskCacheStorage::new(&path, 4, 100 * 1024 * 1024)
        });
        let trace = &span();

        let key = CacheKey::new("", "test-corrupted", "1");
        let combined = key.combined();

        // Write garbage to the meta file
        let meta_path = STORAGE.meta_path(&combined);
        let body_path = STORAGE.body_path(&combined);
        std::fs::write(&meta_path, b"not-valid-meta-data").unwrap();
        std::fs::write(&body_path, b"some-body").unwrap();

        // Lookup should gracefully return None
        let result = STORAGE.lookup(&key, trace).await.unwrap();
        assert!(result.is_none());

        // Corrupted files should have been cleaned up
        assert!(!meta_path.exists());
        assert!(!body_path.exists());

        // Cleanup
        let _ = std::fs::remove_dir_all(
            std::env::temp_dir().join("zentinel-disk-cache-test-corrupted"),
        );
    }

    #[test]
    fn test_orphan_cleanup() {
        let tmp = TempDir::new().unwrap();

        // Pre-create a shard with tmp dir and orphaned files
        let shard_tmp = tmp.path().join("shard-00").join("tmp");
        std::fs::create_dir_all(&shard_tmp).unwrap();
        std::fs::write(shard_tmp.join("orphan1.tmp"), b"data1").unwrap();
        std::fs::write(shard_tmp.join("orphan2.tmp"), b"data2").unwrap();
        // Non-tmp file should be left alone
        std::fs::write(shard_tmp.join("keep.txt"), b"keep").unwrap();

        assert!(shard_tmp.join("orphan1.tmp").exists());
        assert!(shard_tmp.join("orphan2.tmp").exists());

        // Creating storage should clean orphaned .tmp files
        let _storage = DiskCacheStorage::new(tmp.path(), 4, 100 * 1024 * 1024);

        assert!(!shard_tmp.join("orphan1.tmp").exists());
        assert!(!shard_tmp.join("orphan2.tmp").exists());
        assert!(shard_tmp.join("keep.txt").exists());
    }

    #[test]
    fn test_meta_serialization_roundtrip() {
        let meta = create_test_meta();
        let serialized = serialize_meta_to_disk(&meta).unwrap();
        let deserialized = deserialize_meta_from_disk(&serialized).unwrap();

        assert_eq!(
            meta.response_header().status.as_u16(),
            deserialized.response_header().status.as_u16(),
        );
    }
}