Skip to content

Commit

Permalink
Log cache contents every hour
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Aug 5, 2024
1 parent ab2b452 commit ade0877
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ smartstring = "1.0.1"
tera = { version = "1.20.0", default-features = false }
thiserror = "1.0.63"
time = { version = "0.3.36", features = ["formatting", "macros", "parsing", "serde"] }
tokio = { version = "1.39.2", features = ["macros", "net", "rt-multi-thread"] }
tokio = { version = "1.39.2", features = ["macros", "net", "rt-multi-thread", "time"] }
tower = "0.4.13"
tower-http = { version = "0.5.2", features = ["set-header", "trace"] }
tracing = "0.1.40"
Expand Down Expand Up @@ -199,7 +199,7 @@ ignored_unit_patterns = "deny"
impl_trait_in_params = "deny"
implicit_clone = "deny"
imprecise_flops = "deny"
infinite_loop = "deny"
#infinite_loop = "deny"
index_refutable_slice = "deny"
invalid_upcast_comparisons = "deny"
items_after_statements = "deny"
Expand Down
4 changes: 4 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Constants and program-wide compile-time configuration
use std::time::Duration;
use time::{format_description::FormatItem, macros::format_description};

/// The "User-Agent" value sent in outgoing HTTP requests
Expand Down Expand Up @@ -57,6 +58,9 @@ pub(crate) static HTML_TIMESTAMP_FORMAT: &[FormatItem<'_>] =
/// This list must be kept in sorted order; this is enforced by a test below.
pub(crate) static FAST_NOT_EXIST: &[&str] = &[".bzr", ".git", ".nols", ".svn"];

/// Interval between periodic logging of the Zarr manifest cache's contents
pub(crate) const ZARR_MANIFEST_CACHE_DUMP_PERIOD: Duration = Duration::from_secs(3600);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ mod paths;
mod s3;
mod streamutil;
mod zarrman;
use crate::consts::{CSS_CONTENT_TYPE, DEFAULT_API_URL, SERVER_VALUE};
use crate::consts::{
CSS_CONTENT_TYPE, DEFAULT_API_URL, SERVER_VALUE, ZARR_MANIFEST_CACHE_DUMP_PERIOD,
};
use crate::dandi::DandiClient;
use crate::dav::{DandiDav, Templater};
use crate::zarrman::{ManifestFetcher, ZarrManClient};
Expand Down Expand Up @@ -97,6 +99,7 @@ async fn run() -> anyhow::Result<()> {
let args = Arguments::parse();
let dandi = DandiClient::new(args.api_url)?;
let zarrfetcher = ManifestFetcher::new()?;
zarrfetcher.install_periodic_dump(ZARR_MANIFEST_CACHE_DUMP_PERIOD);
let zarrman = ZarrManClient::new(zarrfetcher);

Check warning on line 103 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L101-L103

Added lines #L101 - L103 were not covered by tests
let templater = Templater::new(args.title)?;
let dav = Arc::new(DandiDav {
Expand Down
52 changes: 52 additions & 0 deletions src/zarrman/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use moka::{
future::{Cache, CacheBuilder},
ops::compute::{CompResult, Op},
};
use serde::Serialize;
use std::sync::Arc;
use std::time::Duration;
use url::Url;

/// A client for fetching & caching data from the manifest tree
Expand Down Expand Up @@ -134,4 +136,54 @@ impl ManifestFetcher {
};
Ok(entry.into_value())
}

pub(crate) fn install_periodic_dump(&self, period: Duration) {
let this = self.clone();
let mut schedule = tokio::time::interval(period);
schedule.reset(); // Don't tick immediately
schedule.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
tokio::spawn({
async move {

Check warning on line 146 in src/zarrman/fetcher.rs

View check run for this annotation

Codecov / codecov/patch

src/zarrman/fetcher.rs#L140-L146

Added lines #L140 - L146 were not covered by tests
loop {
schedule.tick().await;
this.log_cache();

Check warning on line 149 in src/zarrman/fetcher.rs

View check run for this annotation

Codecov / codecov/patch

src/zarrman/fetcher.rs#L148-L149

Added lines #L148 - L149 were not covered by tests
}
}
});
}

Check warning on line 153 in src/zarrman/fetcher.rs

View check run for this annotation

Codecov / codecov/patch

src/zarrman/fetcher.rs#L151-L153

Added lines #L151 - L153 were not covered by tests

pub(crate) fn log_cache(&self) {
let entries = self
.cache
.iter()
.map(|(path, manifest)| EntryStat {
manifest_path: path.to_string(),
size: manifest.get_size(),
})
.collect::<Vec<_>>();
match serde_json::to_string(&entries) {
Ok(entries_json) => {
tracing::debug!(

Check warning on line 166 in src/zarrman/fetcher.rs

View check run for this annotation

Codecov / codecov/patch

src/zarrman/fetcher.rs#L155-L166

Added lines #L155 - L166 were not covered by tests
cache_event = "dump",
cache = "zarr-manifests",
%entries_json,
"Dumping cached manifests and their sizes",

Check warning on line 170 in src/zarrman/fetcher.rs

View check run for this annotation

Codecov / codecov/patch

src/zarrman/fetcher.rs#L170

Added line #L170 was not covered by tests
);
}
Err(e) => {
tracing::warn!(

Check warning on line 174 in src/zarrman/fetcher.rs

View check run for this annotation

Codecov / codecov/patch

src/zarrman/fetcher.rs#L173-L174

Added lines #L173 - L174 were not covered by tests
cache_event = "dump-error",
cache = "zarr-manifests",
error = %e,
"Failed to serialize cache contents as JSON",

Check warning on line 178 in src/zarrman/fetcher.rs

View check run for this annotation

Codecov / codecov/patch

src/zarrman/fetcher.rs#L178

Added line #L178 was not covered by tests
);
}
}
}

Check warning on line 182 in src/zarrman/fetcher.rs

View check run for this annotation

Codecov / codecov/patch

src/zarrman/fetcher.rs#L182

Added line #L182 was not covered by tests
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
struct EntryStat {
manifest_path: String,
size: usize,
}
2 changes: 2 additions & 0 deletions tools/zarr-cache-stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ def process_logs(logfiles: Iterable[Path]) -> Events:
)
)
last_accesses.pop(manifest_path, None)
case "dump" | "dump-error":
pass
case other:
log.warning(
"Invalid 'cache_event' field value %r: %s", other, lg.line
Expand Down

0 comments on commit ade0877

Please sign in to comment.