Skip to content

Commit 84ffa79

Browse files
committed
chore(vector-common): Move spawn helpers out of the binary crate
Moves spawn_named and num_threads next to spawn_in_current_span. Call sites import them from vector_lib.
1 parent 863659a commit 84ffa79

10 files changed

Lines changed: 47 additions & 44 deletions

File tree

‎lib/vector-common/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ serde_json.workspace = true
5656
smallvec = { version = "1", default-features = false }
5757
strum.workspace = true
5858
stream-cancel = { version = "0.8.2", default-features = false }
59-
tokio = { workspace = true, features = ["macros", "rt", "time"] }
59+
tokio = { workspace = true, features = ["macros", "rt", "time", "tracing"] }
6060
tracing.workspace = true
6161
vrl.workspace = true
6262
vector-config.workspace = true

‎lib/vector-common/src/lib.rs‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,40 @@ where
153153
use tracing::Instrument as _;
154154
tokio::spawn(task.in_current_span())
155155
}
156+
157+
/// Spawn a task with the given name. The name is only used if
158+
/// built with [`tokio_unstable`][tokio_unstable].
159+
///
160+
/// [tokio_unstable]: https://docs.rs/tokio/latest/tokio/#unstable-features
161+
#[track_caller]
162+
pub fn spawn_named<T>(
163+
task: impl std::future::Future<Output = T> + Send + 'static,
164+
_name: &str,
165+
) -> tokio::task::JoinHandle<T>
166+
where
167+
T: Send + 'static,
168+
{
169+
#[cfg(tokio_unstable)]
170+
return tokio::task::Builder::new()
171+
.name(_name)
172+
.spawn(task)
173+
.expect("tokio task should spawn");
174+
175+
#[cfg(not(tokio_unstable))]
176+
tokio::spawn(task)
177+
}
178+
179+
/// Returns an estimate of the number of recommended threads that Vector should spawn.
180+
pub fn num_threads() -> usize {
181+
let count = match std::thread::available_parallelism() {
182+
Ok(count) => count,
183+
Err(error) => {
184+
tracing::warn!(
185+
message = "Failed to determine available parallelism for thread count, defaulting to 1.",
186+
%error
187+
);
188+
std::num::NonZeroUsize::MIN
189+
}
190+
};
191+
usize::from(count)
192+
}

‎lib/vector-lib/src/lib.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ pub use vector_common::{
1313
Error, NamedInternalEvent, Result, TimeZone, assert_event_data_eq, atomic, btreemap,
1414
byte_size_of, byte_size_of::ByteSizeOf, conversion, counter, encode_logfmt, extra_context,
1515
finalization, finalizer, gauge, histogram, id, impl_event_data_eq, internal_event, json_size,
16-
registered_event, request_metadata, sampling, sensitive_string, shutdown,
17-
spawn_in_current_span, stats, trigger,
16+
num_threads, registered_event, request_metadata, sampling, sensitive_string, shutdown,
17+
spawn_in_current_span, spawn_named, stats, trigger,
1818
};
1919
pub use vector_config as configurable;
2020
pub use vector_config::impl_generate_config_from_default;

‎src/app.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ pub fn build_runtime(
600600
rt_builder.max_blocking_threads(20_000);
601601
rt_builder.enable_all().thread_name(thread_name);
602602

603-
let threads = threads.unwrap_or_else(crate::num_threads);
603+
let threads = threads.unwrap_or_else(vector_lib::num_threads);
604604
if threads == 0 {
605605
error!("The `threads` argument must be greater or equal to 1.");
606606
return Err(exitcode::CONFIG);

‎src/lib.rs‎

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -243,37 +243,3 @@ pub fn get_hostname() -> std::io::Result<String> {
243243
}
244244

245245
pub(crate) use vector_lib::spawn_in_current_span;
246-
247-
/// Spawn a task with the given name. The name is only used if
248-
/// built with [`tokio_unstable`][tokio_unstable].
249-
///
250-
/// [tokio_unstable]: https://docs.rs/tokio/latest/tokio/#unstable-features
251-
#[track_caller]
252-
pub(crate) fn spawn_named<T>(
253-
task: impl std::future::Future<Output = T> + Send + 'static,
254-
_name: &str,
255-
) -> tokio::task::JoinHandle<T>
256-
where
257-
T: Send + 'static,
258-
{
259-
#[cfg(tokio_unstable)]
260-
return tokio::task::Builder::new()
261-
.name(_name)
262-
.spawn(task)
263-
.expect("tokio task should spawn");
264-
265-
#[cfg(not(tokio_unstable))]
266-
tokio::spawn(task)
267-
}
268-
269-
/// Returns an estimate of the number of recommended threads that Vector should spawn.
270-
pub fn num_threads() -> usize {
271-
let count = match std::thread::available_parallelism() {
272-
Ok(count) => count,
273-
Err(error) => {
274-
warn!(message = "Failed to determine available parallelism for thread count, defaulting to 1.", %error);
275-
std::num::NonZeroUsize::MIN
276-
}
277-
};
278-
usize::from(count)
279-
}

‎src/sources/aws_s3/sqs.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ impl Ingestor {
353353
max_number_of_messages: config.max_number_of_messages as i32,
354354
client_concurrency: config
355355
.client_concurrency
356-
.map_or_else(crate::num_threads, NonZeroUsize::get),
356+
.map_or_else(vector_lib::num_threads, NonZeroUsize::get),
357357
visibility_timeout_secs: config.visibility_timeout_secs as i32,
358358
delete_message: config.delete_message,
359359
delete_failed_message: config.delete_failed_message,

‎src/sources/aws_sqs/config.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl SourceConfig for AwsSqsConfig {
119119
poll_secs: self.poll_secs,
120120
concurrency: self
121121
.client_concurrency
122-
.map_or_else(crate::num_threads, NonZeroUsize::get),
122+
.map_or_else(vector_lib::num_threads, NonZeroUsize::get),
123123
visibility_timeout_secs: self.visibility_timeout_secs,
124124
delete_message: self.delete_message,
125125
acknowledgements,

‎src/sources/util/net/tcp/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ where
178178
let shutdown_clone = cx.shutdown.clone();
179179

180180
let request_limiter =
181-
RequestLimiter::new(MAX_IN_FLIGHT_EVENTS_TARGET, crate::num_threads());
181+
RequestLimiter::new(MAX_IN_FLIGHT_EVENTS_TARGET, vector_lib::num_threads());
182182

183183
listener
184184
.accept_stream_limited(max_connections)

‎src/topology/builder.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use vector_lib::{
3737
latency::LatencyRecorder,
3838
schema::Definition,
3939
source_sender::{DEFAULT_CHUNK_SIZE_EVENTS, SourceSenderItem},
40+
spawn_named,
4041
transform::update_runtime_schema_definition,
4142
};
4243
use vector_lib::{gauge, internal_event::GaugeName};
@@ -59,7 +60,6 @@ use crate::{
5960
event::{EventArray, EventContainer},
6061
internal_events::EventsReceived,
6162
shutdown::SourceShutdownCoordinator,
62-
spawn_named,
6363
topology::task::TaskError,
6464
transforms::{SyncTransform, TaskTransform, Transform, TransformOutputs, TransformOutputsBuf},
6565
utilization::{
@@ -121,7 +121,7 @@ pub(crate) fn set_ready_array_capacity(size: usize) {
121121
pub(crate) const TOPOLOGY_BUFFER_SIZE: NonZeroUsize = NonZeroUsize::new(100).unwrap();
122122

123123
static TRANSFORM_CONCURRENCY_LIMIT: LazyLock<usize> = LazyLock::new(|| {
124-
crate::app::worker_threads().map_or_else(crate::num_threads, NonZeroUsize::get)
124+
crate::app::worker_threads().map_or_else(vector_lib::num_threads, NonZeroUsize::get)
125125
});
126126

127127
const INTERNAL_SOURCES: [&str; 2] = ["internal_logs", "internal_metrics"];

‎src/topology/running.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use vector_lib::{
1818
gauge,
1919
internal_event::GaugeName,
2020
shutdown::ShutdownSignal,
21+
spawn_named,
2122
tap::topology::{TapOutput, TapResource, WatchRx, WatchTx},
2223
trigger::DisabledTrigger,
2324
};
@@ -37,7 +38,6 @@ use crate::{
3738
event::EventArray,
3839
shutdown::SourceShutdownCoordinator,
3940
signal::ShutdownError,
40-
spawn_named,
4141
utilization::UtilizationRegistry,
4242
};
4343

0 commit comments

Comments
 (0)