From c4b296c4b86cf7a4c5f8a29dd6534a89a5c7685c Mon Sep 17 00:00:00 2001 From: Aaron Siddhartha Mondal Date: Sun, 5 Nov 2023 15:36:52 +0100 Subject: [PATCH] Migrate from lazy_static to once_cell (#377) The latter is more idiomatic and will soon be integrated into the Rust standard library. --- Cargo.Bazel.lock | 10 +++++----- Cargo.lock | 2 +- Cargo.toml | 2 +- cas/store/BUILD | 12 ++++++------ cas/store/s3_store.rs | 7 +++---- cas/store/tests/filesystem_store_test.rs | 10 +++------- cas/worker/BUILD | 4 ++-- cas/worker/tests/running_actions_manager_test.rs | 12 +++++------- gencargo/filesystem_store_test/Cargo.toml | 2 +- gencargo/running_actions_manager_test/Cargo.toml | 2 +- gencargo/s3_store/Cargo.toml | 2 +- tools/cargo_shared.bzl | 6 +++--- 12 files changed, 32 insertions(+), 39 deletions(-) diff --git a/Cargo.Bazel.lock b/Cargo.Bazel.lock index b6794f429..fbfb1263b 100644 --- a/Cargo.Bazel.lock +++ b/Cargo.Bazel.lock @@ -1,5 +1,5 @@ { - "checksum": "ffa058436431ad11db44bfbd1a61c7e419838e474661896794dc25b1d63f9993", + "checksum": "e639852098069997f943c9f51c62356a6faa0254614942e6d6d6fc71174e5a39", "crates": { "addr2line 0.21.0": { "name": "addr2line", @@ -2634,10 +2634,6 @@ "id": "json5 0.4.1", "target": "json5" }, - { - "id": "lazy_static 1.4.0", - "target": "lazy_static" - }, { "id": "log 0.4.20", "target": "log" @@ -2666,6 +2662,10 @@ "id": "nix 0.26.4", "target": "nix" }, + { + "id": "once_cell 1.18.0", + "target": "once_cell" + }, { "id": "parking_lot 0.12.1", "target": "parking_lot" diff --git a/Cargo.lock b/Cargo.lock index d6ddb8090..ce798e3bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -500,7 +500,6 @@ dependencies = [ "http", "hyper", "json5", - "lazy_static", "log", "lru", "lz4_flex", @@ -508,6 +507,7 @@ dependencies = [ "memory-stats", "mock_instant", "nix", + "once_cell", "parking_lot", "pin-project-lite", "pretty_assertions", diff --git a/Cargo.toml b/Cargo.toml index 979923822..5fd55441a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -116,7 +116,6 @@ hex = "0.4.3" http = "^0.2" hyper = "0.14.27" json5 = "0.4.1" -lazy_static = "1.4.0" log = "0.4.19" lru = "0.10.1" lz4_flex = "0.11.1" @@ -124,6 +123,7 @@ maplit = "1.0.2" memory-stats = "1.1.0" mock_instant = "0.3.1" nix = "0.26.2" +once_cell = "1.18.0" parking_lot = "0.12.1" pin-project-lite = "0.2.10" pretty_assertions = "1.4.0" diff --git a/cas/store/BUILD b/cas/store/BUILD index 4a20765c7..7cc2a7321 100644 --- a/cas/store/BUILD +++ b/cas/store/BUILD @@ -195,9 +195,9 @@ rust_library( "//util:error", "//util:fastcdc", "@crate_index//:bincode", + "@crate_index//:blake3", "@crate_index//:futures", "@crate_index//:serde", - "@crate_index//:blake3", "@crate_index//:tokio", "@crate_index//:tokio-util", ], @@ -239,7 +239,7 @@ rust_library( "@crate_index//:bytes", "@crate_index//:futures", "@crate_index//:http", - "@crate_index//:lazy_static", + "@crate_index//:once_cell", "@crate_index//:rand", "@crate_index//:rusoto_core", "@crate_index//:rusoto_s3", @@ -357,8 +357,8 @@ rust_test( name = "shard_store_test", srcs = ["tests/shard_store_test.rs"], deps = [ - ":shard_store", ":memory_store", + ":shard_store", ":traits", "//config", "//util:common", @@ -373,7 +373,7 @@ rust_test( name = "memory_store_test", srcs = ["tests/memory_store_test.rs"], env = { - "RUST_TEST_THREADS": "1", # This test can't run in parallel. + "RUST_TEST_THREADS": "1", # This test can't run in parallel. }, deps = [ ":memory_store", @@ -464,6 +464,7 @@ rust_test( rust_test( name = "filesystem_store_test", srcs = ["tests/filesystem_store_test.rs"], + proc_macro_deps = ["@crate_index//:async-trait"], deps = [ ":filesystem_store", ":traits", @@ -475,13 +476,12 @@ rust_test( "@crate_index//:async-lock", "@crate_index//:filetime", "@crate_index//:futures", - "@crate_index//:lazy_static", + "@crate_index//:once_cell", "@crate_index//:pretty_assertions", "@crate_index//:rand", "@crate_index//:tokio", "@crate_index//:tokio-stream", ], - proc_macro_deps = ["@crate_index//:async-trait"], ) rust_test( diff --git a/cas/store/s3_store.rs b/cas/store/s3_store.rs index 591154eda..896e304fb 100644 --- a/cas/store/s3_store.rs +++ b/cas/store/s3_store.rs @@ -24,7 +24,7 @@ use futures::future::{try_join_all, FutureExt}; use futures::stream::{unfold, FuturesUnordered}; use futures::TryStreamExt; use http::status::StatusCode; -use lazy_static::lazy_static; +use once_cell::sync::Lazy; use rand::{rngs::OsRng, Rng}; use rusoto_core::credential::DefaultCredentialsProvider; use rusoto_core::request::{DispatchSignedRequest, DispatchSignedRequestFuture}; @@ -92,9 +92,8 @@ impl DispatchSignedRequest for ThrottledDispatcher { } } -lazy_static! { - static ref SHARED_CLIENT: Mutex> = Mutex::new(Arc::new(ThrottledDispatcher::new())); -} +static SHARED_CLIENT: Lazy>> = + Lazy::new(|| Mutex::new(Arc::new(ThrottledDispatcher::new()))); fn should_retry(result: Result>) -> RetryResult where diff --git a/cas/store/tests/filesystem_store_test.rs b/cas/store/tests/filesystem_store_test.rs index 934f041a8..1a8026e10 100644 --- a/cas/store/tests/filesystem_store_test.rs +++ b/cas/store/tests/filesystem_store_test.rs @@ -32,7 +32,7 @@ use filetime::{set_file_atime, FileTime}; use futures::executor::block_on; use futures::task::Poll; use futures::{poll, Future}; -use lazy_static::lazy_static; +use once_cell::sync::Lazy; use rand::{thread_rng, Rng}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::sync::Barrier; @@ -703,9 +703,7 @@ mod filesystem_store_tests { let small_digest = DigestInfo::try_new(HASH1, VALUE1.len())?; let big_digest = DigestInfo::try_new(HASH1, BIG_VALUE.len())?; - lazy_static! { - static ref UNREFED_DIGESTS: Mutex> = Mutex::new(Vec::new()); - } + static UNREFED_DIGESTS: Lazy>> = Lazy::new(|| Mutex::new(Vec::new())); struct LocalHooks {} impl FileEntryHooks for LocalHooks { fn on_unref(file_entry: &Fe) { @@ -753,9 +751,7 @@ mod filesystem_store_tests { let content_path = make_temp_path("content_path"); let temp_path = make_temp_path("temp_path"); - lazy_static! { - static ref FILE_DELETED_BARRIER: Arc = Arc::new(Barrier::new(2)); - } + static FILE_DELETED_BARRIER: Lazy> = Lazy::new(|| Arc::new(Barrier::new(2))); struct LocalHooks {} impl FileEntryHooks for LocalHooks { diff --git a/cas/worker/BUILD b/cas/worker/BUILD index 269c2b438..03e00d5b3 100644 --- a/cas/worker/BUILD +++ b/cas/worker/BUILD @@ -29,13 +29,13 @@ rust_library( srcs = ["running_actions_manager.rs"], proc_macro_deps = ["@crate_index//:async-trait"], deps = [ - "//config", "//cas/scheduler:action_messages", "//cas/store", "//cas/store:ac_utils", "//cas/store:fast_slow_store", "//cas/store:filesystem_store", "//cas/store:grpc_store", + "//config", "//proto", "//util:common", "//util:error", @@ -147,7 +147,7 @@ rust_test( "//util:common", "//util:error", "@crate_index//:futures", - "@crate_index//:lazy_static", + "@crate_index//:once_cell", "@crate_index//:pretty_assertions", "@crate_index//:prost", "@crate_index//:prost-types", diff --git a/cas/worker/tests/running_actions_manager_test.rs b/cas/worker/tests/running_actions_manager_test.rs index 131fa378d..172c5d971 100644 --- a/cas/worker/tests/running_actions_manager_test.rs +++ b/cas/worker/tests/running_actions_manager_test.rs @@ -27,7 +27,7 @@ use std::sync::{Arc, Mutex}; use std::time::{Duration, SystemTime, UNIX_EPOCH}; use futures::{FutureExt, TryFutureExt}; -use lazy_static::lazy_static; +use once_cell::sync::Lazy; use prost::Message; use rand::{thread_rng, Rng}; use tokio::sync::oneshot; @@ -2047,12 +2047,10 @@ exit 1 } type StaticOneshotTuple = Mutex<(Option>, Option>)>; - lazy_static! { - static ref TIMEOUT_ONESHOT: StaticOneshotTuple = { - let (tx, rx) = oneshot::channel(); - Mutex::new((Some(tx), Some(rx))) - }; - } + static TIMEOUT_ONESHOT: Lazy = Lazy::new(|| { + let (tx, rx) = oneshot::channel(); + Mutex::new((Some(tx), Some(rx))) + }); let root_work_directory = make_temp_path("root_work_directory"); fs::create_dir_all(&root_work_directory).await?; diff --git a/gencargo/filesystem_store_test/Cargo.toml b/gencargo/filesystem_store_test/Cargo.toml index 5527ccac9..bb30a7371 100644 --- a/gencargo/filesystem_store_test/Cargo.toml +++ b/gencargo/filesystem_store_test/Cargo.toml @@ -23,7 +23,7 @@ async-lock = { workspace = true } async-trait = { workspace = true } filetime = { workspace = true } futures = { workspace = true } -lazy_static = { workspace = true } +once_cell = { workspace = true } pretty_assertions = { workspace = true } rand = { workspace = true } tokio = { workspace = true } diff --git a/gencargo/running_actions_manager_test/Cargo.toml b/gencargo/running_actions_manager_test/Cargo.toml index eec2a3b97..e4aadbc11 100644 --- a/gencargo/running_actions_manager_test/Cargo.toml +++ b/gencargo/running_actions_manager_test/Cargo.toml @@ -20,7 +20,7 @@ doctest = false [dependencies] futures = { workspace = true } -lazy_static = { workspace = true } +once_cell = { workspace = true } pretty_assertions = { workspace = true } prost = { workspace = true } prost-types = { workspace = true } diff --git a/gencargo/s3_store/Cargo.toml b/gencargo/s3_store/Cargo.toml index 4cfff843f..2a0770216 100644 --- a/gencargo/s3_store/Cargo.toml +++ b/gencargo/s3_store/Cargo.toml @@ -23,7 +23,7 @@ async-trait = { workspace = true } bytes = { workspace = true } futures = { workspace = true } http = { workspace = true } -lazy_static = { workspace = true } +once_cell = { workspace = true } rand = { workspace = true } rusoto_core = { workspace = true } rusoto_s3 = { workspace = true } diff --git a/tools/cargo_shared.bzl b/tools/cargo_shared.bzl index 89e4eb080..4dcd7bc76 100644 --- a/tools/cargo_shared.bzl +++ b/tools/cargo_shared.bzl @@ -102,9 +102,6 @@ PACKAGES = { "byteorder": { "version": "1.4.3", }, - "lazy_static": { - "version": "1.4.0", - }, "filetime": { "version": "0.2.21", }, @@ -179,4 +176,7 @@ PACKAGES = { "formatx": { "version": "0.2.1", }, + "once_cell": { + "version": "1.18.0", + }, }