geoffreyclaude commented on code in PR #14547: URL: https://github.com/apache/datafusion/pull/14547#discussion_r1954913978
########## datafusion/common-runtime/src/join_set.rs: ########## @@ -0,0 +1,207 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::future::Future; +use std::task::{Context, Poll}; +use tokio::runtime::Handle; +use tokio::task::JoinSet as TokioJoinSet; +use tokio::task::{AbortHandle, Id, JoinError, LocalSet}; + +#[cfg(feature = "tracing")] +mod trace_utils { + use std::future::Future; + use tracing_futures::Instrument; + + /// Instruments the provided future with the current tracing span. + pub fn trace_future<F, T>(future: F) -> impl Future<Output = T> + Send + where + F: Future<Output = T> + Send + 'static, + T: Send, + { + future.in_current_span() + } + + /// Wraps the provided blocking function to execute within the current tracing span. + pub fn trace_block<F, T>(f: F) -> impl FnOnce() -> T + Send + 'static + where + F: FnOnce() -> T + Send + 'static, + T: Send, + { + let span = tracing::Span::current(); + move || span.in_scope(f) + } +} + +/// A wrapper around Tokio's [`JoinSet`](tokio::task::JoinSet) that transparently forwards all public API calls +/// while optionally instrumenting spawned tasks and blocking closures with the current tracing span +/// when the `trace` feature is enabled. +#[derive(Debug)] +pub struct JoinSet<T> { + inner: TokioJoinSet<T>, +} + +impl<T> Default for JoinSet<T> { + fn default() -> Self { + Self::new() + } +} + +impl<T> JoinSet<T> { + /// [JoinSet::new](tokio::task::JoinSet::new) - Create a new JoinSet. + pub fn new() -> Self { + Self { + inner: TokioJoinSet::new(), + } + } + + /// [JoinSet::len](tokio::task::JoinSet::len) - Return the number of tasks. + pub fn len(&self) -> usize { + self.inner.len() + } + + /// [JoinSet::is_empty](tokio::task::JoinSet::is_empty) - Check if the JoinSet is empty. + pub fn is_empty(&self) -> bool { + self.inner.is_empty() + } +} +impl<T: 'static> JoinSet<T> { + /// [JoinSet::spawn](tokio::task::JoinSet::spawn) - Spawn a new task. + pub fn spawn<F>(&mut self, task: F) -> AbortHandle Review Comment: Question: All public stable methods of the `tokio::task::JoinSet` are wrapped. Should only the methods used in DataFusion be wrapped for conciseness? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org