You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "andygrove (via GitHub)" <gi...@apache.org> on 2026/01/17 16:27:02 UTC
[PR] perf: add connection pooling for shuffle reader remote fetches [datafusion-ballista]
andygrove opened a new pull request, #1388:
URL: https://github.com/apache/datafusion-ballista/pull/1388
## Summary
Adds a `BallistaClientPool` that caches gRPC connections to executors by (host, port). This avoids the overhead of establishing new connections for each partition fetch during shuffle reads.
### Changes
- Add `BallistaClientPool` struct in `client.rs` with:
- `get_or_connect(host, port, max_message_size)` - gets cached connection or creates new one
- `remove(host, port)` - removes a connection (useful for error recovery)
- `clear()` - clears all cached connections
- Add `global_client_pool()` function for shared pool access across all shuffle reads
- Update `fetch_partition_remote()` in shuffle_reader.rs to use the connection pool
### Implementation Details
The pool uses a read-write lock pattern with `parking_lot::RwLock`:
- **Fast path**: read lock to check for existing connection (no contention for cache hits)
- **Slow path**: create connection without holding lock, then write lock to cache
Race handling: if multiple tasks try to connect to the same host simultaneously, each creates a connection but only one gets cached. The others detect this and return the cached one, dropping their extra connection. This is a rare case and avoids holding locks during async connection establishment.
### Motivation
This addresses the TODO comment in shuffle_reader.rs:
```rust
// TODO for shuffle client connections, we should avoid creating new connections again and again.
// And we should also avoid to keep alive too many connections for long time.
```
Connection establishment involves TCP handshake, TLS negotiation (if enabled), and HTTP/2 setup. Reusing connections eliminates this overhead for repeated fetches from the same executor.
## Note
This PR was created with AI assistance using [Claude Code](https://claude.ai/code). All changes were reviewed and approved by a human maintainer.
## Test plan
- [x] Existing shuffle_reader tests pass (`cargo test -p ballista-core shuffle_reader`)
- [x] Existing client tests pass (`cargo test -p ballista-core client`)
- [ ] Manual testing with distributed queries to verify connection reuse
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: add connection pooling for shuffle reader remote fetches [datafusion-ballista]
Posted by "milenkovicm (via GitHub)" <gi...@apache.org>.
milenkovicm commented on PR #1388:
URL: https://github.com/apache/datafusion-ballista/pull/1388#issuecomment-3783625252
closes #736 i believe
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: add connection pooling for shuffle reader remote fetches [datafusion-ballista]
Posted by "milenkovicm (via GitHub)" <gi...@apache.org>.
milenkovicm commented on PR #1388:
URL: https://github.com/apache/datafusion-ballista/pull/1388#issuecomment-4322146554
closing in favour of #1578
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: add connection pooling for shuffle reader remote fetches [datafusion-ballista]
Posted by "milenkovicm (via GitHub)" <gi...@apache.org>.
milenkovicm commented on PR #1388:
URL: https://github.com/apache/datafusion-ballista/pull/1388#issuecomment-3939084387
@sqlbenchmark tpch --iterations 5 --scale-factor 10
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: add connection pooling for shuffle reader remote fetches [datafusion-ballista]
Posted by "milenkovicm (via GitHub)" <gi...@apache.org>.
milenkovicm commented on code in PR #1388:
URL: https://github.com/apache/datafusion-ballista/pull/1388#discussion_r2701480074
##########
ballista/core/src/client.rs:
##########
@@ -287,6 +289,122 @@ impl BallistaClient {
}
}
+/// A connection pool for reusing `BallistaClient` connections to executors.
+///
+/// This pool caches connections by (host, port) to avoid the overhead of
+/// establishing new gRPC connections for each partition fetch during shuffle reads.
+/// Connections are stored indefinitely and reused across multiple fetch operations.
+///
+/// # Thread Safety
+///
+/// The pool uses a `RwLock` to allow concurrent reads while ensuring exclusive
+/// access during connection creation. The `BallistaClient` itself is `Clone`
+/// (wrapping an `Arc`), so cloned clients share the underlying connection.
+#[derive(Default)]
+pub struct BallistaClientPool {
Review Comment:
just wonder would it make sense to use some off-the-shelf like `deadpool` or `r2d2` instead of implementing our own ?
If i'm not mistaken, current implementation can, in theory, leak connections; connection will be removed from the pool only when it fails, but, in theory, we can have a executor removed/replaced and connection never get chance to fail
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: add connection pooling for shuffle reader remote fetches [datafusion-ballista]
Posted by "milenkovicm (via GitHub)" <gi...@apache.org>.
milenkovicm closed pull request #1388: perf: add connection pooling for shuffle reader remote fetches
URL: https://github.com/apache/datafusion-ballista/pull/1388
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: add connection pooling for shuffle reader remote fetches [datafusion-ballista]
Posted by "milenkovicm (via GitHub)" <gi...@apache.org>.
milenkovicm commented on PR #1388:
URL: https://github.com/apache/datafusion-ballista/pull/1388#issuecomment-3765410233
i guess we can't use `r2d2` or `deadpool` as they pool instances of the same destination, interfaces have `get()` method to return a pooled instance and we need `get(address)`
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: add connection pooling for shuffle reader remote fetches [datafusion-ballista]
Posted by "milenkovicm (via GitHub)" <gi...@apache.org>.
milenkovicm commented on PR #1388:
URL: https://github.com/apache/datafusion-ballista/pull/1388#issuecomment-3764250459
there is a test https://github.com/apache/datafusion-ballista/blob/fce60b73533195abfbaf73e3ee361f1ea4a398b6/ballista/client/tests/context_checks.rs#L918
which forces remote read always, not sure if that would cover your last task in the list.
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: add connection pooling for shuffle reader remote fetches [datafusion-ballista]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #1388:
URL: https://github.com/apache/datafusion-ballista/pull/1388#discussion_r2701693445
##########
ballista/core/src/client.rs:
##########
@@ -287,6 +289,122 @@ impl BallistaClient {
}
}
+/// A connection pool for reusing `BallistaClient` connections to executors.
+///
+/// This pool caches connections by (host, port) to avoid the overhead of
+/// establishing new gRPC connections for each partition fetch during shuffle reads.
+/// Connections are stored indefinitely and reused across multiple fetch operations.
+///
+/// # Thread Safety
+///
+/// The pool uses a `RwLock` to allow concurrent reads while ensuring exclusive
+/// access during connection creation. The `BallistaClient` itself is `Clone`
+/// (wrapping an `Arc`), so cloned clients share the underlying connection.
+#[derive(Default)]
+pub struct BallistaClientPool {
Review Comment:
I pushed a TTL improvement, but moved this to drfraft for now
##########
ballista/core/src/client.rs:
##########
@@ -287,6 +289,122 @@ impl BallistaClient {
}
}
+/// A connection pool for reusing `BallistaClient` connections to executors.
+///
+/// This pool caches connections by (host, port) to avoid the overhead of
+/// establishing new gRPC connections for each partition fetch during shuffle reads.
+/// Connections are stored indefinitely and reused across multiple fetch operations.
+///
+/// # Thread Safety
+///
+/// The pool uses a `RwLock` to allow concurrent reads while ensuring exclusive
+/// access during connection creation. The `BallistaClient` itself is `Clone`
+/// (wrapping an `Arc`), so cloned clients share the underlying connection.
+#[derive(Default)]
+pub struct BallistaClientPool {
Review Comment:
I pushed a TTL improvement, but moved this to draft for now
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: add connection pooling for shuffle reader remote fetches [datafusion-ballista]
Posted by "milenkovicm (via GitHub)" <gi...@apache.org>.
milenkovicm commented on PR #1388:
URL: https://github.com/apache/datafusion-ballista/pull/1388#issuecomment-3939082291
@sqlbenchmark run tpch -s 10 -i 3
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: add connection pooling for shuffle reader remote fetches [datafusion-ballista]
Posted by "milenkovicm (via GitHub)" <gi...@apache.org>.
milenkovicm commented on PR #1388:
URL: https://github.com/apache/datafusion-ballista/pull/1388#issuecomment-3764236724
is there a chance we cover #1367 somehow as well,
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: add connection pooling for shuffle reader remote fetches [datafusion-ballista]
Posted by "sqlbenchmark (via GitHub)" <gi...@apache.org>.
sqlbenchmark commented on PR #1388:
URL: https://github.com/apache/datafusion-ballista/pull/1388#issuecomment-3939106864
## Ballista TPC-H Benchmark Results
**PR:** #1388 - fix: add TTL-based expiration to connection pool to prevent leaks
**PR Commit:** `9420caf2`
**Base Commit:** `debc6273` (main)
**Scale Factor:** SF10
**Iterations:** 5
### Results
Could not parse structured results. Raw output:
**Main branch:**
```
.0 ms and returned 27840 rows
Query 16 iteration 4 took 364.9 ms and returned 27840 rows
Query 16 avg time: 365.4 ms
Query 17 iteration 0 took 3713.8 ms and returned 1 rows
Query 17 iteration 1 took 3816.0 ms and returned 1 rows
Query 17 iteration 2 took 3715.6 ms and returned 1 rows
Query 17 iteration 3 took 3651.5 ms and returned 1 rows
Query 17 iteration 4 took 3770.3 ms and returned 1 rows
Query 17 avg time: 3733.4 ms
Query 18 iteration 0 took 5777.4 ms and returned 100 rows
Query 18 iteration 1 took 5364.7 ms and returned 100 rows
Query 18 iteration 2 took 5265.4 ms and returned 100 rows
Query 18 iteration 3 took 5725.3 ms and returned 100 rows
Query 18 iteration 4 took 5624.8 ms and returned 100 rows
Query 18 avg time: 5551.5 ms
Query 19 iteration 0 took 1390.9 ms and returned 1 rows
Query 19 iteration 1 took 1341.9 ms and returned 1 rows
Query 19 iteration 2 took 1332.0 ms and returned 1 rows
Query 19 iteration 3 took 1330.5 ms and returned 1 rows
Query 19 iteration 4 took 1292.8 ms and returned 1 rows
Query 19 avg time: 1337.6 ms
Query 20 iteration 0 took 1087.7 ms and returned 1804 rows
Query 20 iteration 1 took 1023.4 ms and returned 1804 rows
Query 20 iteration 2 took 1032.1 ms and returned 1804 rows
Query 20 iteration 3 took 1033.5 ms and returned 1804 rows
Query 20 iteration 4 took 1032.9 ms and returned 1804 rows
Query 20 avg time: 1041.9 ms
Query 21 iteration 0 took 4583.2 ms and returned 100 rows
Query 21 iteration 1 took 4528.3 ms and returned 100 rows
Query 21 iteration 2 took 4635.9 ms and returned 100 rows
Query 21 iteration 3 took 4633.3 ms and returned 100 rows
Query 21 iteration 4 took 5304.0 ms and returned 100 rows
Query 21 avg time: 4736.9 ms
Query 22 iteration 0 took 363.6 ms and returned 7 rows
Query 22 iteration 1 took 313.6 ms and returned 7 rows
Query 22 iteration 2 took 311.3 ms and returned 7 rows
Query 22 iteration 3 took 312.6 ms and returned 7 rows
Query 22 iteration 4 took 312.9 ms and returned 7 rows
Query 22 avg time: 322.8 ms
```
**PR branch:**
```
Finished `release` profile [optimized] target(s) in 0.19s
Running `target/release/tpch benchmark ballista --host localhost --port 50050 --path /app/data --format parquet --iterations 5`
error: The following required arguments were not provided:
--query <query>
USAGE:
tpch benchmark ballista --batch-size <batch-size> --format <file-format> --host <host> --iterations <iterations> --partitions <partitions> --path <path> --port <port> --query <query>
For more information try --help
```
---
*Automated benchmark run by dfbench*
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: add connection pooling for shuffle reader remote fetches [datafusion-ballista]
Posted by "sqlbenchmark (via GitHub)" <gi...@apache.org>.
sqlbenchmark commented on PR #1388:
URL: https://github.com/apache/datafusion-ballista/pull/1388#issuecomment-3939083657
## @sqlbenchmark usage
This bot is whitelisted for DataFusion committers.
### Commands
```
@sqlbenchmark tpch [--iterations N] [--scale-factor N] [--baseline BRANCH] [--baseline-pr N] [-c key=value ...]
@sqlbenchmark criterion [--bench NAME] [--filter PATTERN] [--baseline BRANCH] [--baseline-pr N]
@sqlbenchmark help
```
### Examples
```
@sqlbenchmark tpch
@sqlbenchmark tpch --iterations 5 --scale-factor 10
@sqlbenchmark tpch -c datafusion.execution.batch_size=4096
@sqlbenchmark tpch --baseline my-branch
@sqlbenchmark criterion
@sqlbenchmark criterion --bench sql_planner
@sqlbenchmark criterion --baseline my-branch
@sqlbenchmark criterion --bench sort_shuffle --baseline-pr 1430
```
---
*Automated by dfbench*
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] perf: add connection pooling for shuffle reader remote fetches [datafusion-ballista]
Posted by "milenkovicm (via GitHub)" <gi...@apache.org>.
milenkovicm commented on PR #1388:
URL: https://github.com/apache/datafusion-ballista/pull/1388#issuecomment-3980564362
I spent some time with this, the reason being with latest changes TPCH (SF10) starts braking on my machine, as the process is getting out of ephemeral client ports (around 16K on macos) and macos has 1 min `TIME_WAIT` before port can be re-assigned.
so the ininital approach was to cache channel per address
```rust
static CHANNEL_CACHE: LazyLock<DashMap<String, Channel>> =
LazyLock::new(DashMap::new);
```
clone it and re-use it. Aparently this does not work well if there is a lot of data to be transferred from server to client (everything get stuck) `initial_connection_window_size` & `initial_stream_window_size` help in some cases, not in all.
Looks like single tcp connection can't be pushed to transfer as much data as shuffle files generate.
Alternative approach is to keep multiple connections for same address and then load balance across them
```rust
static CHANNEL_CACHE: LazyLock<DashMap<String, Vec<Channel>>> =
LazyLock::new(DashMap::new);
```
apparently connection re-use is not as trivial as i initially thought
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org