Skip to content

Commit d2f855c

Browse files
authored
chore(deps): Drop usage of once_cell::LazyLock (#21511)
1 parent 3014920 commit d2f855c

File tree

46 files changed

+190
-211
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+190
-211
lines changed

Cargo.lock

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ default-run = "vector"
1212
autobenches = false # our benchmarks are not runnable on their own either way
1313
# Minimum supported rust version
1414
# See docs/DEVELOPING.md for policy
15-
rust-version = "1.79"
15+
rust-version = "1.80"
1616

1717
[[bin]]
1818
name = "vector"
@@ -329,7 +329,6 @@ async-nats = { version = "0.33.0", default-features = false, optional = true }
329329
nkeys = { version = "0.4.4", default-features = false, optional = true }
330330
nom = { version = "7.1.3", default-features = false, optional = true }
331331
notify = { version = "6.1.1", default-features = false, features = ["macos_fsevent"] }
332-
once_cell = { version = "1.20", default-features = false }
333332
openssl = { version = "0.10.66", default-features = false, features = ["vendored"] }
334333
openssl-probe = { version = "0.1.5", default-features = false }
335334
ordered-float = { version = "4.3.0", default-features = false }

STYLE.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,10 @@ ordering.
280280
When there is a need or desire to share global state, there are a few options depending on the
281281
required constraints.
282282

283-
If you're working with data that is _lazily initialized_ but _never changes after initialization_,
284-
we prefer **[`once_cell`](https://docs.rs/once_cell)**. It is slightly faster than
285-
[`lazy_static`](https://docs.rs/lazy-static), and additionally provides a richer API than both
286-
`lazy_static` and the standard library variants, such as `std::sync::Once`. Additionally, there is
287-
[active work happening](https://github.com/rust-lang/rust/issues/74465) to migrate the types in
288-
`once_cell` into `std::sync` directly, which will be easier to switch to if we're already using
289-
`once_cell`.
283+
If you're working with data that _never changes after initialization_,
284+
we prefer `std::sync::OnceLock` over **[`once_cell`](https://docs.rs/once_cell)** or
285+
[`lazy_static`](https://docs.rs/lazy-static). It is slightly faster and provides a richer API than
286+
`lazy_static`, and has equivalent features to the `once_cell` version.
290287

291288
If you're working with data that _changes over time_, but has a very high read-to-write ratio, such
292289
as _many readers_, but _one writer_ and infrequent writes, we prefer

clippy.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ disallowed-methods = [
99
disallowed-types = [
1010
{ path = "once_cell::sync::OnceCell", reason = "Use `std::sync::OnceLock` instead." },
1111
{ path = "once_cell::unsync::OnceCell", reason = "Use `std::cell::OnceCell` instead." },
12+
{ path = "once_cell::sync::Lazy", reason = "Use `std::sync::LazyLock` instead." },
13+
{ path = "once_cell::unsync::Lazy", reason = "Use `std::sync::LazyCell` instead." },
1214
]

lib/codecs/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ dyn-clone = { version = "1", default-features = false }
1919
influxdb-line-protocol = { version = "2", default-features = false }
2020
lookup = { package = "vector-lookup", path = "../vector-lookup", default-features = false, features = ["test"] }
2121
memchr = { version = "2", default-features = false }
22-
once_cell = { version = "1.20", default-features = false }
2322
ordered-float = { version = "4.3.0", default-features = false }
2423
prost.workspace = true
2524
prost-reflect.workspace = true

lib/codecs/src/gelf.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Contains common definitions for GELF codec support
22
3-
use once_cell::sync::Lazy;
3+
use std::sync::LazyLock;
4+
45
use regex::Regex;
56
use vrl::owned_value_path;
67
use vrl::path::OwnedTargetPath;
@@ -54,21 +55,23 @@ pub(crate) struct GelfTargetPaths {
5455
}
5556

5657
/// Lazily initialized singleton.
57-
pub(crate) static GELF_TARGET_PATHS: Lazy<GelfTargetPaths> = Lazy::new(|| GelfTargetPaths {
58-
version: OwnedTargetPath::event(owned_value_path!(gelf_fields::VERSION)),
59-
host: OwnedTargetPath::event(owned_value_path!(gelf_fields::HOST)),
60-
full_message: OwnedTargetPath::event(owned_value_path!(gelf_fields::FULL_MESSAGE)),
61-
level: OwnedTargetPath::event(owned_value_path!(gelf_fields::LEVEL)),
62-
facility: OwnedTargetPath::event(owned_value_path!(gelf_fields::FACILITY)),
63-
line: OwnedTargetPath::event(owned_value_path!(gelf_fields::LINE)),
64-
file: OwnedTargetPath::event(owned_value_path!(gelf_fields::FILE)),
65-
short_message: OwnedTargetPath::event(owned_value_path!(gelf_fields::SHORT_MESSAGE)),
66-
});
58+
pub(crate) static GELF_TARGET_PATHS: LazyLock<GelfTargetPaths> =
59+
LazyLock::new(|| GelfTargetPaths {
60+
version: OwnedTargetPath::event(owned_value_path!(gelf_fields::VERSION)),
61+
host: OwnedTargetPath::event(owned_value_path!(gelf_fields::HOST)),
62+
full_message: OwnedTargetPath::event(owned_value_path!(gelf_fields::FULL_MESSAGE)),
63+
level: OwnedTargetPath::event(owned_value_path!(gelf_fields::LEVEL)),
64+
facility: OwnedTargetPath::event(owned_value_path!(gelf_fields::FACILITY)),
65+
line: OwnedTargetPath::event(owned_value_path!(gelf_fields::LINE)),
66+
file: OwnedTargetPath::event(owned_value_path!(gelf_fields::FILE)),
67+
short_message: OwnedTargetPath::event(owned_value_path!(gelf_fields::SHORT_MESSAGE)),
68+
});
6769

6870
/// Regex for matching valid field names in the encoder. According to the original spec by graylog,
6971
/// must contain only word chars, periods and dashes. Additional field names must also be prefixed
7072
/// with an `_` , however that is intentionally omitted from this regex to be checked separately
7173
/// to create a specific error message.
7274
/// As Graylog itself will produce GELF with any existing field names on the Graylog GELF Output,
7375
/// vector is more lenient, too, at least allowing the additional `@` character.
74-
pub static VALID_FIELD_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[\w\.\-@]*$").unwrap());
76+
pub static VALID_FIELD_REGEX: LazyLock<Regex> =
77+
LazyLock::new(|| Regex::new(r"^[\w\.\-@]*$").unwrap());

lib/vector-buffers/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ crossbeam-queue = "0.3.11"
4343
hdrhistogram = "7.5.4"
4444
metrics-tracing-context.workspace = true
4545
metrics-util = { workspace = true, features = ["debugging"] }
46-
once_cell = "1.20"
4746
proptest = "1.5"
4847
quickcheck = "1.0"
4948
rand = "0.8.5"

lib/vector-buffers/src/test/helpers.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::{future::Future, path::Path, str::FromStr};
1+
use std::{future::Future, path::Path, str::FromStr, sync::LazyLock};
22

3-
use once_cell::sync::Lazy;
43
use temp_dir::TempDir;
54
use tracing_fluent_assertions::{AssertionRegistry, AssertionsLayer};
65
use tracing_subscriber::{filter::LevelFilter, layer::SubscriberExt, Layer, Registry};
@@ -72,7 +71,7 @@ pub fn install_tracing_helpers() -> AssertionRegistry {
7271
//
7372
// TODO: At some point, we might be able to write a simple derive macro that does this for us, and
7473
// configures the other necessary bits, but for now.... by hand will get the job done.
75-
static ASSERTION_REGISTRY: Lazy<AssertionRegistry> = Lazy::new(|| {
74+
static ASSERTION_REGISTRY: LazyLock<AssertionRegistry> = LazyLock::new(|| {
7675
let assertion_registry = AssertionRegistry::default();
7776
let assertions_layer = AssertionsLayer::new(&assertion_registry);
7877

lib/vector-config-common/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ license = "MPL-2.0"
77
[dependencies]
88
convert_case = { version = "0.6", default-features = false }
99
darling = { version = "0.20", default-features = false, features = ["suggestions"] }
10-
once_cell = { version = "1", default-features = false, features = ["std"] }
1110
proc-macro2 = { version = "1.0", default-features = false }
1211
serde.workspace = true
1312
serde_json.workspace = true

lib/vector-config-common/src/human_friendly.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use std::collections::{HashMap, HashSet};
2+
use std::sync::LazyLock;
23

34
use convert_case::{Boundary, Case, Converter};
4-
use once_cell::sync::Lazy;
55

66
/// Well-known replacements.
77
///
88
/// Replacements are instances of strings with unique capitalization that cannot be achieved
99
/// programmatically, as well as the potential insertion of additional characters, such as the
1010
/// replacement of "pubsub" with "Pub/Sub".
11-
static WELL_KNOWN_REPLACEMENTS: Lazy<HashMap<String, &'static str>> = Lazy::new(|| {
11+
static WELL_KNOWN_REPLACEMENTS: LazyLock<HashMap<String, &'static str>> = LazyLock::new(|| {
1212
let pairs = vec![
1313
("eventstoredb", "EventStoreDB"),
1414
("mongodb", "MongoDB"),
@@ -43,7 +43,7 @@ static WELL_KNOWN_REPLACEMENTS: Lazy<HashMap<String, &'static str>> = Lazy::new(
4343
/// Acronyms are distinct from replacements because they should be entirely capitalized (i.e. "aws"
4444
/// or "aWs" or "Aws" should always be replaced with "AWS") whereas replacements may insert
4545
/// additional characters or capitalize specific characters within the original string.
46-
static WELL_KNOWN_ACRONYMS: Lazy<HashSet<String>> = Lazy::new(|| {
46+
static WELL_KNOWN_ACRONYMS: LazyLock<HashSet<String>> = LazyLock::new(|| {
4747
let acronyms = &[
4848
"api", "amqp", "aws", "ec2", "ecs", "gcp", "hec", "http", "https", "nats", "nginx", "s3",
4949
"sqs", "tls", "ssl", "otel", "gelf", "csv", "json", "rfc3339", "lz4", "us", "eu", "bsd",

lib/vector-core/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ metrics-tracing-context.workspace = true
3535
metrics-util.workspace = true
3636
mlua = { version = "0.9.9", default-features = false, features = ["lua54", "send", "vendored"], optional = true }
3737
no-proxy = { version = "0.3.5", default-features = false, features = ["serialize"] }
38-
once_cell = { version = "1.20", default-features = false }
3938
ordered-float = { version = "4.3.0", default-features = false }
4039
openssl = { version = "0.10.66", default-features = false, features = ["vendored"] }
4140
parking_lot = { version = "0.12.3", default-features = false }

lib/vector-core/src/config/log_schema.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use std::sync::OnceLock;
1+
use std::sync::{LazyLock, OnceLock};
22

33
use lookup::lookup_v2::OptionalTargetPath;
44
use lookup::{OwnedTargetPath, OwnedValuePath};
5-
use once_cell::sync::Lazy;
65
use vector_config::configurable_component;
76

87
static LOG_SCHEMA: OnceLock<LogSchema> = OnceLock::new();
9-
static LOG_SCHEMA_DEFAULT: Lazy<LogSchema> = Lazy::new(LogSchema::default);
8+
static LOG_SCHEMA_DEFAULT: LazyLock<LogSchema> = LazyLock::new(LogSchema::default);
109

1110
const MESSAGE: &str = "message";
1211
const TIMESTAMP: &str = "timestamp";

lib/vector-core/src/config/telemetry.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use std::sync::OnceLock;
1+
use std::sync::{LazyLock, OnceLock};
22

3-
use once_cell::sync::Lazy;
43
use vector_common::request_metadata::GroupedCountByteSize;
54
use vector_config::configurable_component;
65

76
static TELEMETRY: OnceLock<Telemetry> = OnceLock::new();
8-
static TELEMETRY_DEFAULT: Lazy<Telemetry> = Lazy::new(Telemetry::default);
7+
static TELEMETRY_DEFAULT: LazyLock<Telemetry> = LazyLock::new(Telemetry::default);
98

109
/// Loads the telemetry options from configurations and sets the global options.
1110
/// Once this is done, configurations can be correctly loaded using configured

lib/vector-core/src/event/log_event.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
use crate::event::util::log::all_fields_skip_array_elements;
2-
use bytes::Bytes;
3-
use chrono::Utc;
41
use std::{
52
collections::HashMap,
63
convert::{TryFrom, TryInto},
74
fmt::Debug,
85
iter::FromIterator,
96
mem::size_of,
107
num::NonZeroUsize,
11-
sync::Arc,
8+
sync::{Arc, LazyLock},
129
};
1310

11+
use crate::event::util::log::all_fields_skip_array_elements;
12+
use bytes::Bytes;
13+
use chrono::Utc;
14+
1415
use crossbeam_utils::atomic::AtomicCell;
1516
use lookup::{lookup_v2::TargetPath, metadata_path, path, PathPrefix};
16-
use once_cell::sync::Lazy;
1717
use serde::{Deserialize, Serialize, Serializer};
1818
use vector_common::{
1919
byte_size_of::ByteSizeOf,
@@ -36,7 +36,7 @@ use crate::config::{log_schema, telemetry};
3636
use crate::event::util::log::{all_fields, all_metadata_fields};
3737
use crate::event::MaybeAsLogMut;
3838

39-
static VECTOR_SOURCE_TYPE_PATH: Lazy<Option<OwnedTargetPath>> = Lazy::new(|| {
39+
static VECTOR_SOURCE_TYPE_PATH: LazyLock<Option<OwnedTargetPath>> = LazyLock::new(|| {
4040
Some(OwnedTargetPath::metadata(owned_value_path!(
4141
"vector",
4242
"source_type"
@@ -761,7 +761,7 @@ struct TracingTargetPaths {
761761
}
762762

763763
/// Lazily initialized singleton.
764-
static TRACING_TARGET_PATHS: Lazy<TracingTargetPaths> = Lazy::new(|| TracingTargetPaths {
764+
static TRACING_TARGET_PATHS: LazyLock<TracingTargetPaths> = LazyLock::new(|| TracingTargetPaths {
765765
timestamp: OwnedTargetPath::event(owned_value_path!("timestamp")),
766766
kind: OwnedTargetPath::event(owned_value_path!("metadata", "kind")),
767767
level: OwnedTargetPath::event(owned_value_path!("metadata", "level")),

lib/vector-core/src/event/util/log/all_fields.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
use once_cell::sync::Lazy;
1+
use std::{collections::btree_map, fmt::Write as _, iter, slice, sync::LazyLock};
2+
23
use regex::Regex;
34
use serde::{Serialize, Serializer};
4-
use std::{collections::btree_map, fmt::Write as _, iter, slice};
55
use vrl::path::PathPrefix;
66

77
use crate::event::{KeyString, ObjectMap, Value};
88

9-
static IS_VALID_PATH_SEGMENT: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-zA-Z0-9_]+$").unwrap());
9+
static IS_VALID_PATH_SEGMENT: LazyLock<Regex> =
10+
LazyLock::new(|| Regex::new(r"^[a-zA-Z0-9_]+$").unwrap());
1011

1112
/// Iterates over all paths in form `a.b[0].c[1]` in alphabetical order
1213
/// and their corresponding values.

src/api/schema/components/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ pub mod transform;
66
use std::{
77
cmp,
88
collections::{HashMap, HashSet},
9+
sync::LazyLock,
910
};
1011

1112
use async_graphql::{Enum, InputObject, Interface, Object, Subscription};
12-
use once_cell::sync::Lazy;
1313
use tokio_stream::{wrappers::BroadcastStream, Stream, StreamExt};
1414
use vector_lib::internal_event::DEFAULT_OUTPUT;
1515

@@ -223,8 +223,8 @@ enum ComponentChanged {
223223
Removed(Component),
224224
}
225225

226-
static COMPONENT_CHANGED: Lazy<tokio::sync::broadcast::Sender<ComponentChanged>> =
227-
Lazy::new(|| {
226+
static COMPONENT_CHANGED: LazyLock<tokio::sync::broadcast::Sender<ComponentChanged>> =
227+
LazyLock::new(|| {
228228
let (tx, _) = tokio::sync::broadcast::channel(10);
229229
tx
230230
});

src/api/schema/components/state.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
use std::{
22
collections::{HashMap, HashSet},
3-
sync::{Arc, RwLock},
3+
sync::{Arc, LazyLock, RwLock},
44
};
55

6-
use once_cell::sync::Lazy;
7-
86
use super::{sink, source, transform, Component};
97
use crate::config::{ComponentKey, OutputId};
108

119
pub const INVARIANT: &str = "Couldn't acquire lock on Vector components. Please report this.";
1210

13-
pub static COMPONENTS: Lazy<Arc<RwLock<HashMap<ComponentKey, Component>>>> =
14-
Lazy::new(|| Arc::new(RwLock::new(HashMap::new())));
11+
pub static COMPONENTS: LazyLock<Arc<RwLock<HashMap<ComponentKey, Component>>>> =
12+
LazyLock::new(|| Arc::new(RwLock::new(HashMap::new())));
1513

1614
/// Filter components with the provided `map_func`
1715
pub fn filter_components<T>(map_func: impl Fn((&ComponentKey, &Component)) -> Option<T>) -> Vec<T> {

src/app.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![allow(missing_docs)]
2+
use std::sync::atomic::{AtomicUsize, Ordering};
23
use std::{num::NonZeroUsize, path::PathBuf, process::ExitStatus, time::Duration};
34

45
use exitcode::ExitCode;
56
use futures::StreamExt;
6-
use once_cell::race::OnceNonZeroUsize;
77
use tokio::runtime::{self, Runtime};
88
use tokio::sync::{broadcast::error::RecvError, MutexGuard};
99
use tokio_stream::wrappers::UnboundedReceiverStream;
@@ -30,7 +30,11 @@ use std::os::unix::process::ExitStatusExt;
3030
use std::os::windows::process::ExitStatusExt;
3131
use tokio::runtime::Handle;
3232

33-
pub static WORKER_THREADS: OnceNonZeroUsize = OnceNonZeroUsize::new();
33+
static WORKER_THREADS: AtomicUsize = AtomicUsize::new(0);
34+
35+
pub fn worker_threads() -> Option<NonZeroUsize> {
36+
NonZeroUsize::new(WORKER_THREADS.load(Ordering::Relaxed))
37+
}
3438

3539
pub struct ApplicationConfig {
3640
pub config_paths: Vec<config::ConfigPath>,
@@ -451,14 +455,14 @@ pub fn build_runtime(threads: Option<usize>, thread_name: &str) -> Result<Runtim
451455
rt_builder.enable_all().thread_name(thread_name);
452456

453457
let threads = threads.unwrap_or_else(crate::num_threads);
454-
let threads = NonZeroUsize::new(threads).ok_or_else(|| {
458+
if threads == 0 {
455459
error!("The `threads` argument must be greater or equal to 1.");
456-
exitcode::CONFIG
457-
})?;
460+
return Err(exitcode::CONFIG);
461+
}
458462
WORKER_THREADS
459-
.set(threads)
460-
.expect("double thread initialization");
461-
rt_builder.worker_threads(threads.get());
463+
.compare_exchange(0, threads, Ordering::Acquire, Ordering::Relaxed)
464+
.unwrap_or_else(|_| panic!("double thread initialization"));
465+
rt_builder.worker_threads(threads);
462466

463467
debug!(messaged = "Building runtime.", worker_threads = threads);
464468
Ok(rt_builder.build().expect("Unable to create async runtime"))

0 commit comments

Comments
 (0)