prodriguezdefino commented on PR #40606:
URL: https://github.com/apache/beam/pull/40606#issuecomment-7303373339

   ### Design Overview
   
   To help with the review (and in the spirit of "Standards for Large Scale 
Agentic Changes (LSAC) in Beam"), here is a summary of the design in this PR, 
why it improves on `master`, and what workarounds it replaces.
   
   #### 1. Why `master` Deadlocks
   
   `BeamFnDataGrpcMultiplexer` shares one gRPC data stream across all active 
bundles.
   gRPC runs inbound `onNext` calls and outbound `onReady` callbacks on the 
same `SerializingExecutor`.
   On `master`, `InboundObserver.onNext` can block that executor in two ways:
   
   1. `CancellableQueue.put` blocks when a bundle queue reaches 100 messages.
   2. `consumerFuture.get(3, HOURS)` blocks when data arrives before 
`registerConsumer`.
   
   Once `onNext` blocks, gRPC cannot run `onReady` for any outbound write on 
that stream.
   Bundle threads then stall in `DirectStreamObserver` (up to 60 s backoff 
between polls) or hang forever in `BufferingStreamObserver` 
(`phaser.awaitAdvance` has no timeout).
   We hit this on Dataflow Runner v2 when the runner fused a Java step between 
two steps of another SDK.
   Dataflow logged `"Ignoring data element byte limit ... to avoid stuckness"` 
and the job crawled.
   
   #### 2. Why Current Workarounds Are Not Enough
   
   Without this change, teams use costly workarounds:
   
   - **Increase `data_buffer_size_limit` 
(`--experiments=data_buffer_size_limit=10000000`)**: `CancellableQueue` caps 
messages at 100, not by bytes. Chunks of 10 MB hit that limit 10x less often 
than the 1 MB default. In a 45.8 GB Rust-Java-Rust BigQuery copy job, the 1 MB 
default hit 30+ stalls up to 423 s (16m 07s). With 10 MB chunks, runtime fell 
to 8m 31s, but per-bundle buffers grow up to 1 GB and `OnReady` stalls still 
occur.
   - **Insert `Reshuffle` or `--experiments=disable_fusion`**: This adds 
shuffle I/O, extra latency, and higher Dataflow cost.
   - **Lower `max_bundle_size` or worker concurrency**: This lowers throughput 
and only reduces the chance of a stall.
   - **Enable `beam_fn_api_buffered_stream`**: This hides short bursts, but the 
stream hangs forever once the outbound buffer fills while `onNext` blocks.
   - **Split pipelines or rewrite transforms in one SDK**: This needs duplicate 
code and extra infrastructure.
   
   #### 3. Design of This PR
   
   The core rule is simple: `InboundObserver.onNext` never blocks the gRPC 
executor.
   
   - **Manual inbound flow control (`BeamFnDataGrpcMultiplexer`, 
`ForwardingClientResponseObserver`)**: `InboundObserver.beforeStart` calls 
`disableAutoRequestWithInitial(16)`. `inFlightInbound` tracks open permits. 
Each `Elements` message carries a callback (or an `AtomicInteger` countdown for 
multi-instruction batches) that calls `requestInbound(1)` after the bundle 
finishes the message.
   - **Per-instruction handoff without blocks (`ReceiverHolder`, 
`CancellableQueue.tryPut`, `BeamFnDataInboundObserver`)**: `ReceiverHolder` 
replaces `CompletableFuture` and synchronizes access to `receiver` and a FIFO 
`pending` queue. When the bundle has no consumer yet or 
`CancellableQueue.tryPut` returns `false`, `onNext` puts the message in 
`pending` and returns right away. Bundle callbacks call `drainPending()` before 
`requestInbound(1)` so FIFO order holds even during concurrent registration.
   - **Elastic window on outbound stall (`StalledPollHandler`, 
`DirectStreamObserver`, `BufferingStreamObserver`, 
`OutboundObserverFactory`)**: Both outbound observers check `isReady()` every 
50 ms while not ready. Every 500 ms of wait time, they call `onStalledPoll()`, 
which requests 1 extra inbound message up to `Math.max(1, receivers.size()) * 
100`. The window shrinks back to 16 as bundles drain.
   
   #### 4. Key Safety Rules
   
   - **No ABBA deadlock of locks**: `CancellableQueue.cancel(exception, 
onDrop)` collects the elements that it clears under the queue lock, releases 
that lock, and runs `onDrop` afterward.
   - **No permit leaks**: `awaitCompletion` saves the active message callback 
and runs it in `finally` for both `Exception` and `Error`. 
`ReceiverHolder.poison`, `close`, and `complete` also run all callbacks in 
`pending`. Late data for poisoned instructions returns `null` from 
`computeIfAbsent` and releases its permit right away.
   - **Full deadlock coverage**: A fix that removes only the full-queue block 
still lets `consumerFuture.get(3, HOURS)` starve `onReady`. 
`InboundUnregisteredConsumerReproTest` covers that case directly.
   
   #### 5. Performance Impact
   
   - **Throughput**: JMH `ProcessBundleBenchmark.testLargeBundle` shows a 5% to 
10% throughput gain and tighter variance compared to `master` on Apple Silicon 
and x86.
   - **Memory and stall recovery**: Pipelines can keep the 1 MB default 
`data_buffer_size_limit` instead of 10 MB chunks. Steady-state open inbound 
messages stay at 16 (~16 MB for each stream). Outbound observers see readiness 
within 50 ms instead of 1 s to 60 s backoffs.


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to