Skip to content

Commit 58eeb5e

Browse files
committed
--zarrman-cache-bytes
1 parent 8336a6b commit 58eeb5e

File tree

6 files changed

+40
-28
lines changed

6 files changed

+40
-28
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ In Development
44
- Added doc comments to much of the code
55
- Return 502 status when a backend returns an invalid response
66
- Require `--api-url` (and other URLs retrieved from APIs) to be HTTP(S)
7+
- Format all log lines as JSON
8+
- Add logging of Zarr manifest cache events
9+
- Limit Zarr manifest cache by total size of entries
10+
- Add a `-Z`/`--zarrman-cache-bytes` option for setting the cache size
11+
- Expire idle Zarr manifest cache entries
12+
- Log Zarr manifest cache entries every hour
713

814
v0.4.0 (2024-07-09)
915
-------------------

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,7 @@ Options
119119
- `-T <TITLE>`, `--title <TITLE>` — Specify the site name to use in HTML/web
120120
views of collections (used inside `<title>`'s and as the root breadcrumb
121121
text) [default: dandidav]
122+
123+
- `-Z <INT>`, `--zarrman-cache-bytes <INT>` — Specify the maximum number of
124+
bytes of parsed Zarr manifest files to store in the Zarr manifest cache at
125+
once [default: 100 MiB]

src/consts.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ pub(crate) static FAST_NOT_EXIST: &[&str] = &[".bzr", ".git", ".nols", ".svn"];
6161
/// Interval between periodic logging of the Zarr manifest cache's contents
6262
pub(crate) const ZARR_MANIFEST_CACHE_DUMP_PERIOD: Duration = Duration::from_secs(3600);
6363

64+
/// Default size of the Zarr manifest cache; the cache is limited to storing no
65+
/// more than this many bytes of parsed manifests at once
66+
pub(crate) const ZARR_MANIFEST_CACHE_TOTAL_BYTES: u64 = 100 * 1024 * 1024; // 100 MiB
67+
6468
#[cfg(test)]
6569
mod tests {
6670
use super::*;

src/main.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ mod paths;
99
mod s3;
1010
mod streamutil;
1111
mod zarrman;
12-
use crate::consts::{
13-
CSS_CONTENT_TYPE, DEFAULT_API_URL, SERVER_VALUE, ZARR_MANIFEST_CACHE_DUMP_PERIOD,
14-
};
12+
use crate::consts::*;
1513
use crate::dandi::DandiClient;
1614
use crate::dav::{DandiDav, Templater};
1715
use crate::httputil::HttpUrl;
@@ -67,6 +65,11 @@ struct Arguments {
6765
/// Site name to use in HTML collection pages
6866
#[arg(short = 'T', long, default_value = env!("CARGO_PKG_NAME"))]
6967
title: String,
68+
69+
/// Limit the Zarr manifest cache to storing no more than this many bytes
70+
/// of parsed manifests at once
71+
#[arg(short = 'Z', long, default_value_t = ZARR_MANIFEST_CACHE_TOTAL_BYTES, value_name = "INT")]
72+
zarrman_cache_bytes: u64,
7073
}
7174

7275
// See
@@ -99,7 +102,7 @@ fn main() -> anyhow::Result<()> {
99102
async fn run() -> anyhow::Result<()> {
100103
let args = Arguments::parse();
101104
let dandi = DandiClient::new(args.api_url)?;
102-
let zarrfetcher = ManifestFetcher::new()?;
105+
let zarrfetcher = ManifestFetcher::new(args.zarrman_cache_bytes)?;
103106
zarrfetcher.install_periodic_dump(ZARR_MANIFEST_CACHE_DUMP_PERIOD);
104107
let zarrman = ZarrManClient::new(zarrfetcher);
105108
let templater = Templater::new(args.title)?;

src/zarrman/consts.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,5 @@ pub(super) static MANIFEST_ROOT_URL: &str =
1919
/// `{ENTRY_DOWNLOAD_PREFIX}/{zarr_id}/{entry_path}`.
2020
pub(super) static ENTRY_DOWNLOAD_PREFIX: &str = "https://dandiarchive.s3.amazonaws.com/zarr/";
2121

22-
/// Limit the manifest cache to storing no more than this many bytes of parsed
23-
/// manifests at once
24-
pub(super) const MANIFEST_CACHE_TOTAL_BYTES: u64 = 100 * 1024 * 1024; // 100 MiB
25-
2622
/// Expire any manifest cache entries that haven't been accessed for this long
2723
pub(super) const MANIFEST_CACHE_IDLE_EXPIRY: Duration = Duration::from_secs(300);

src/zarrman/fetcher.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::consts::{MANIFEST_CACHE_IDLE_EXPIRY, MANIFEST_CACHE_TOTAL_BYTES, MANIFEST_ROOT_URL};
1+
use super::consts::{MANIFEST_CACHE_IDLE_EXPIRY, MANIFEST_ROOT_URL};
22
use super::manifest::Manifest;
33
use super::resources::ManifestPath;
44
use super::util::{Index, ZarrManError};
@@ -33,26 +33,25 @@ impl ManifestFetcher {
3333
/// # Errors
3434
///
3535
/// Returns an error if construction of the inner `reqwest::Client` fails
36-
pub(crate) fn new() -> Result<Self, BuildClientError> {
36+
pub(crate) fn new(cache_size: u64) -> Result<Self, BuildClientError> {
3737
let inner = Client::new()?;
38-
let cache: Cache<ManifestPath, Arc<Manifest>> =
39-
CacheBuilder::new(MANIFEST_CACHE_TOTAL_BYTES)
40-
.name("zarr-manifests")
41-
.weigher(|_, manifest: &Arc<Manifest>| {
42-
u32::try_from(manifest.get_size()).unwrap_or(u32::MAX)
43-
})
44-
.time_to_idle(MANIFEST_CACHE_IDLE_EXPIRY)
45-
.eviction_listener(|path, manifest, cause| {
46-
tracing::debug!(
47-
cache_event = "evict",
48-
cache = "zarr-manifests",
49-
manifest = %path,
50-
manifest_size = manifest.get_size(),
51-
?cause,
52-
"Zarr manifest evicted from cache",
53-
);
54-
})
55-
.build();
38+
let cache: Cache<ManifestPath, Arc<Manifest>> = CacheBuilder::new(cache_size)
39+
.name("zarr-manifests")
40+
.weigher(|_, manifest: &Arc<Manifest>| {
41+
u32::try_from(manifest.get_size()).unwrap_or(u32::MAX)
42+
})
43+
.time_to_idle(MANIFEST_CACHE_IDLE_EXPIRY)
44+
.eviction_listener(|path, manifest, cause| {
45+
tracing::debug!(
46+
cache_event = "evict",
47+
cache = "zarr-manifests",
48+
manifest = %path,
49+
manifest_size = manifest.get_size(),
50+
?cause,
51+
"Zarr manifest evicted from cache",
52+
);
53+
})
54+
.build();
5655
let manifest_root_url = MANIFEST_ROOT_URL
5756
.parse::<HttpUrl>()
5857
.expect("MANIFEST_ROOT_URL should be a valid HTTP URL");

0 commit comments

Comments
 (0)