Skip to content

Commit f43a75a

Browse files
committed
No eager buffering in network connections
1 parent 14b3b56 commit f43a75a

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/worker/worker_connection_pool.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use datafusion::execution::memory_pool::{MemoryConsumer, MemoryReservation};
2222
use datafusion::physical_expr_common::metrics::{ExecutionPlanMetricsSet, MetricValue};
2323
use datafusion::physical_plan::metrics::{MetricBuilder, Time};
2424
use futures::stream::BoxStream;
25-
use futures::{Stream, StreamExt, TryFutureExt, TryStreamExt};
25+
use futures::{FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt};
2626
use http::Extensions;
2727
use pin_project::{pin_project, pinned_drop};
2828
use prost::Message;
@@ -160,6 +160,7 @@ struct RemoteWorkerConnection {
160160
cancel_token: CancellationToken,
161161
per_partition_rx: DashMap<usize, UnboundedReceiver<WorkerMsg>>,
162162

163+
first_poll_notify: Arc<Notify>,
163164
// Signals the demux task that buffered memory has been freed by a consumer.
164165
mem_available_notify: Arc<Notify>,
165166

@@ -246,6 +247,9 @@ impl RemoteWorkerConnection {
246247
let mem_available_notify = Arc::new(Notify::new());
247248
let mem_available_notify_for_task = Arc::clone(&mem_available_notify);
248249

250+
let first_poll_notify = Arc::new(Notify::new());
251+
let first_poll_notify_for_task = Arc::clone(&first_poll_notify);
252+
249253
// Cancellation token allows us to stop the background task promptly when all partition
250254
// streams are dropped (e.g., when the query is cancelled).
251255
let cancel_token = CancellationToken::new();
@@ -255,6 +259,12 @@ impl RemoteWorkerConnection {
255259
// fan them out to the appropriate `per_partition_rx` based on the "partition" declared
256260
// in each individual record batch flight metadata.
257261
let task = SpawnedTask::spawn(async move {
262+
tokio::select! {
263+
biased;
264+
_ = cancel.cancelled() => return,
265+
_ = first_poll_notify_for_task.notified() => {}
266+
}
267+
258268
let mut client = match channel_resolver.get_worker_client_for_url(&url).await {
259269
Ok(v) => v,
260270
Err(err) => {
@@ -364,6 +374,7 @@ impl RemoteWorkerConnection {
364374
not_consumed_streams: Arc::new(AtomicUsize::new(per_partition_rx.len())),
365375
per_partition_rx,
366376
mem_available_notify,
377+
first_poll_notify,
367378

368379
// metrics stuff
369380
memory_reservation: memory_reservation_clone,
@@ -392,7 +403,13 @@ impl WorkerConnection for RemoteWorkerConnection {
392403
let task = Arc::clone(&self.task);
393404
let cancel_token = self.cancel_token.clone();
394405

395-
let stream = UnboundedReceiverStream::new(partition_receiver);
406+
let first_poll_notify = Arc::clone(&self.first_poll_notify);
407+
let stream = async move {
408+
first_poll_notify.notify_one();
409+
UnboundedReceiverStream::new(partition_receiver)
410+
}
411+
.flatten_stream();
412+
396413
let stream = stream.map_err(|err| FlightError::Tonic(Box::new(err)));
397414
let reservation = Arc::clone(&self.memory_reservation);
398415
let mem_available_notify = Arc::clone(&self.mem_available_notify);

src/worker/worker_service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub struct Worker {
4848
impl Default for Worker {
4949
fn default() -> Self {
5050
let cache = Cache::builder()
51-
.time_to_idle(Duration::from_secs(60))
51+
.time_to_idle(Duration::from_mins(10))
5252
.build();
5353
Self {
5454
runtime: Arc::new(RuntimeEnv::default()),

0 commit comments

Comments
 (0)