From db1657d4807798ad78a113cc95dc7414577b50fa Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Tue, 11 Apr 2023 12:28:11 +0200 Subject: [PATCH] Hook up per-item expiration to moka --- .github/workflows/build.yml | 2 +- Cargo.lock | 81 ++++++++++--------- crates/symbolicator-service/Cargo.toml | 2 +- .../src/caching/memory.rs | 36 ++++++++- gocd/pipelines/symbolicator-canary.yaml | 2 +- 5 files changed, 78 insertions(+), 45 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 312299934..eea21cc33 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,7 +3,7 @@ name: Release build on: push: branches: - - release/** + - not-release/** jobs: linux: diff --git a/Cargo.lock b/Cargo.lock index a239f4f4e..89c67075c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,9 +40,9 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "0.7.20" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +checksum = "67fc08ce920c31afb70f013dcce1bfc3a3195de6a228474e45e1f145b36f8d04" dependencies = [ "memchr", ] @@ -565,9 +565,9 @@ dependencies = [ [[package]] name = "axum" -version = "0.6.15" +version = "0.6.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b32c5ea3aabaf4deb5f5ced2d688ec0844c881c9e6c696a8b769a05fc691e62" +checksum = "113713495a32dd0ab52baf5c10044725aa3aec00b31beda84218e469029b72a3" dependencies = [ "async-trait", "axum-core", @@ -761,9 +761,9 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.12.0" +version = "3.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" +checksum = "9b1ce199063694f33ffb7dd4e0ee620741495c32833cde5aa08f02a0bf96f0c8" [[package]] name = "bytecount" @@ -934,9 +934,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.2.2" +version = "4.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b802d85aaf3a1cdb02b224ba472ebdea62014fccfcb269b95a4d76443b5ee5a" +checksum = "956ac1f6381d8d82ab4684768f89c0ea3afe66925ceadb4eeb3fc452ffc55d62" dependencies = [ "clap_builder", "clap_derive", @@ -945,9 +945,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.2.2" +version = "4.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14a1a858f532119338887a4b8e1af9c60de8249cd7bafd68036a489e261e37b6" +checksum = "84080e799e54cff944f4b4a4b0e71630b0e0443b25b985175c7dddc1a859b749" dependencies = [ "anstream", "anstyle", @@ -1047,9 +1047,9 @@ dependencies = [ [[package]] name = "cpufeatures" -version = "0.2.6" +version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "280a9f2d8b3a38871a3c8a46fb80db65e5e5ed97da80c4d08bf27fb63e35e181" +checksum = "3e4c1eaa2012c47becbbad2ab175484c2a84d1185b566fb2cc5b8707343dfe58" dependencies = [ "libc", ] @@ -1736,9 +1736,9 @@ dependencies = [ [[package]] name = "h2" -version = "0.3.17" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66b91535aa35fea1523ad1b86cb6b53c28e0ae566ba4a460f4457e936cad7c6f" +checksum = "17f8a914c2987b688368b5138aa05321db91f4090cf26118185672ad588bce21" dependencies = [ "bytes", "fnv", @@ -2298,9 +2298,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.141" +version = "0.2.142" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" +checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" [[package]] name = "libloading" @@ -2329,9 +2329,9 @@ checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" [[package]] name = "linux-raw-sys" -version = "0.3.1" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f" +checksum = "36eb31c1778188ae1e64398743890d0877fef36d11521ac60406b42016e8c2cf" [[package]] name = "lock_api" @@ -2560,9 +2560,8 @@ dependencies = [ [[package]] name = "moka" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0d3b8e76a2e4b17de765db9432e377a171c42fbe0512b8bc860ff1bfe2e273b" +version = "0.11.0" +source = "git+https://github.com/moka-rs/moka.git#358a71ed1ed15b2353ff511d6004771a326edf15" dependencies = [ "async-io", "async-lock", @@ -2744,9 +2743,9 @@ checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" [[package]] name = "openssl" -version = "0.10.50" +version = "0.10.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e30d8bc91859781f0a943411186324d580f2bbeb71b452fe91ae344806af3f1" +checksum = "97ea2d98598bf9ada7ea6ee8a30fb74f9156b63bbe495d64ec2b87c269d2dda3" dependencies = [ "bitflags", "cfg-if", @@ -2776,9 +2775,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-sys" -version = "0.9.85" +version = "0.9.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d3d193fb1488ad46ffe3aaabc912cc931d02ee8518fe2959aea8ef52718b0c0" +checksum = "992bac49bdbab4423199c654a5515bd2a6c6a23bf03f2dd3bdb7e5ae6259bc69" dependencies = [ "cc", "libc", @@ -3029,9 +3028,9 @@ dependencies = [ [[package]] name = "polling" -version = "2.7.0" +version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4be1c66a6add46bff50935c313dae30a5030cf8385c5206e8a95e9e9def974aa" +checksum = "4b2d323e8ca7996b3e23126511a523f7e62924d93ecd5ae73b333815b0eb3dce" dependencies = [ "autocfg", "bitflags", @@ -3274,13 +3273,13 @@ dependencies = [ [[package]] name = "regex" -version = "1.7.3" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b1f693b24f6ac912f4893ef08244d70b6067480d2f1a46e950c9691e6749d1d" +checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370" dependencies = [ "aho-corasick", "memchr", - "regex-syntax", + "regex-syntax 0.7.1", ] [[package]] @@ -3289,7 +3288,7 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" dependencies = [ - "regex-syntax", + "regex-syntax 0.6.29", ] [[package]] @@ -3298,6 +3297,12 @@ version = "0.6.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" +[[package]] +name = "regex-syntax" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c" + [[package]] name = "reqwest" version = "0.11.12" @@ -3365,9 +3370,9 @@ dependencies = [ [[package]] name = "rustc-demangle" -version = "0.1.22" +version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4a36c42d1873f9a77c53bde094f9664d9891bc604a45b4798fd2c389ed12e5b" +checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustc-hash" @@ -3395,9 +3400,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.37.11" +version = "0.37.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85597d61f83914ddeba6a47b3b8ffe7365107221c2e557ed94426489fefb5f77" +checksum = "d9b864d3c18a5785a05953adeed93e2dca37ed30f18e69bba9f30079d51f363f" dependencies = [ "bitflags", "errno", @@ -4510,7 +4515,7 @@ name = "symbolicli" version = "23.4.0" dependencies = [ "anyhow", - "clap 4.2.2", + "clap 4.2.4", "dirs", "prettytable-rs", "reqwest", @@ -4985,9 +4990,9 @@ dependencies = [ [[package]] name = "tracing-subscriber" -version = "0.3.16" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70" +checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" dependencies = [ "matchers", "nu-ansi-term", diff --git a/crates/symbolicator-service/Cargo.toml b/crates/symbolicator-service/Cargo.toml index 46766a1c1..6f5f93c4b 100644 --- a/crates/symbolicator-service/Cargo.toml +++ b/crates/symbolicator-service/Cargo.toml @@ -29,7 +29,7 @@ jsonwebtoken = "8.1.0" lazy_static = "1.4.0" minidump = "0.16.0" minidump-processor = "0.16.0" -moka = { version = "0.10", features = ["future"] } +moka = { git = "https://github.com/moka-rs/moka.git", features = ["future"] } parking_lot = "0.12.0" regex = "1.5.5" reqwest = { version = "0.11.0", features = ["gzip", "json", "stream", "trust-dns"] } diff --git a/crates/symbolicator-service/src/caching/memory.rs b/crates/symbolicator-service/src/caching/memory.rs index 896abf931..a9545a382 100644 --- a/crates/symbolicator-service/src/caching/memory.rs +++ b/crates/symbolicator-service/src/caching/memory.rs @@ -67,13 +67,42 @@ impl Clone for Cacher { } } +/// A struct implementing [`moka::Expiry`] that uses the [`InMemoryItem`] [`Instant`] as the explicit +/// expiration time. +struct CacheExpiration; +fn saturating_duration_since(now: Instant, target: Instant) -> Option { + // NOTE: the argument to `checked_duration_since` is the *earlier* instant. + // `target` is in the future, and `now` is the earlier one. + Some(target.checked_duration_since(now).unwrap_or_default()) +} + +impl moka::Expiry> for CacheExpiration { + fn expire_after_create( + &self, + _key: &CacheKey, + value: &InMemoryItem, + current_time: Instant, + ) -> Option { + saturating_duration_since(current_time, value.0) + } + + fn expire_after_update( + &self, + _key: &CacheKey, + value: &InMemoryItem, + current_time: Instant, + _current_duration: Option, + ) -> Option { + saturating_duration_since(current_time, value.0) + } +} + impl Cacher { pub fn new(config: Cache, shared_cache: SharedCacheRef) -> Self { let cache = InMemoryCache::builder() .max_capacity(config.in_memory_capacity) .name(config.name().as_ref()) - // NOTE: even though we have a per-item TTL, we still want to have a hard limit here - .time_to_live(Duration::from_secs(60 * 60)) + .expire_after(CacheExpiration) // NOTE: we count all the bookkeeping structures to the weight as well .weigher(|_k, v| { let value_size = @@ -367,12 +396,11 @@ impl Cacher { (expiration.as_instant(), item) }); - let replace_if = |v: &InMemoryItem| Instant::now() >= v.0; let entry = self .cache .entry_by_ref(&cache_key) - .or_insert_with_if(init, replace_if) + .or_insert_with(init) .await; if !entry.is_fresh() { diff --git a/gocd/pipelines/symbolicator-canary.yaml b/gocd/pipelines/symbolicator-canary.yaml index f6741cdec..695f6cbe9 100644 --- a/gocd/pipelines/symbolicator-canary.yaml +++ b/gocd/pipelines/symbolicator-canary.yaml @@ -16,7 +16,7 @@ pipelines: symbolicator_repo: git: git@github.com:getsentry/symbolicator.git shallow_clone: true - branch: master + branch: release/moka-expiry destination: symbolicator stages: - checks: