Skip to content

[ISSUE #153] fix(remoting): send asynchronously with bounded in-flight capacity and retry transient seek failures - #154

Merged
lizhimins merged 1 commit into
apache:mainfrom
lizhimins:fix-source-startup-seek-retry
Nov 2, 2026
Merged

lizhimins merged 1 commit into
apache:mainfrom
lizhimins:fix-source-startup-seek-retry

Conversation

@lizhimins

Copy link
Copy Markdown
Member

What is the purpose of the change

Fixes the two defects reported in #153, both observed during a Kubernetes acceptance run (Flink 1.20.3, RocketMQ 5.5.1 direct-connect and a proxy-fronted commercial distribution, 4 KiB messages, TM 1 / 2 / 4 / 8):

  1. The sink's "async" send is synchronous on the task thread. InnerProducerImpl.send() used CompletableFuture.supplyAsync(..., Runnable::run), which executes the blocking producer.send() on the calling thread and returns an already-completed future. Single-slot throughput was capped at 1 / send RTT (Proxy Sink TM8: 16,397 rec/s at 7.9% TM CPU, zero CFS throttling), send.pending.max could never accumulate in-flight sends, flush() never waited for real acknowledgements, and close() did not drain at all. Synchronous submission errors also leaked the semaphore permit, and checkAsyncException() consumed the stored failure with getAndSet(null), losing it after the first throw.

  2. Startup seek fails the job on transient transport errors. InnerConsumerImpl.seek() wrapped every MQClientException into FlinkRuntimeException; a RemotingSendRequestException during startup exhausted the 3-restart budget and drove the job to terminal FAILED against a cluster that served traffic normally seconds later.

Brief change log

  • InnerProducerImpl: true async producer.send(message, SendCallback) on a bounded asyncSenderExecutor (executor.num threads, send.pending.max queue bound); the future completes only from the callback and fails on non-SEND_OK results and synchronous submission errors (restoring the interrupt flag for InterruptedException); the executor is shut down with a sendMsgTimeout bound on close.
  • RocketMQWriter: release the permit when the async-failure check throws after acquisition; keep async failures sticky (get() instead of getAndSet(null)); bound flush() by producer.send.timeout and fail with a recorded timeout instead of spinning forever; close() flushes pending sends before closing the producer; validate send.pending.max / send.timeout are positive.
  • RocketMQWriter metrics: sendPendingRecords gauge and a sendCompletionRt gauge exposing a fixed 1024-bucket (250 μs) completion-time histogram plus count / total-nanos / failed counters, used to measure send completion RT (avg / p99) in the acceptance runs.
  • InnerConsumerImpl.seek(): retry up to 2 times with 100 ms backoff only when the direct cause is RemotingConnectException, RemotingSendRequestException or RemotingTimeoutException; other failures propagate immediately and interruption is preserved.
  • Tests: callback-only completion (out-of-order callbacks), selector argument retention, non-acknowledged send failure, synchronous submission failure, executor bounds and shutdown; failed-callback unblock with sticky failure across write / flush / close, flush waiting for all callbacks, permit return on submission exception; seek recovery / exhaustion / non-transport / interruption boundaries. Existing writer tests now stub InitContext.metricGroup() because the constructor registers gauges.

Verifications

  • JDK 11: spotless:check green; the five affected test classes pass (27 tests, 0 failures); the full remoting regression was run on the identical code during the acceptance campaign (155 tests, 0 failures, 6 pre-existing skips).
  • Acceptance matrix, 180 s warmup / 300 s sampling per scenario, 4 KiB, 32 in-flight / 2 sender threads:
    • Sink TM 1 / 2 / 4 / 8, direct-connect: 29,668 / 56,905 / 106,638 / 193,513 rec/s at 99.7–99.8% TM CPU.
    • Sink TM 1 / 2 / 4 / 8, proxy (12-core limit): 31,098 / 63,063 / 121,650 / 200,301 rec/s (before, synchronous: 2,528 / 6,269 / 11,527 / 16,397 rec/s at <10% TM CPU).
    • End-to-end TM8: 143,025 rec/s direct, 157,493 rec/s proxy; failure callbacks 0 and sampling-window checkpoint failures 0 in all 16 scenarios.
    • Send completion RT avg 0.6–2.0 ms, p99 (250 μs bucket upper bound) ≤ 2.0 ms in the formal Sink scenarios.
  • Source TM8 startup with the seek retry: complete 302 s windows with 0 job restarts and 0 container restarts (before: RemotingSendRequestException exhausted the restart budget → terminal FAILED).

Closes #153

…d retry transient seek failures

- Replace the calling-thread synchronous send with a true async SendCallback
  path on a bounded sender executor; fail the future on non-SEND_OK results
  and synchronous submission errors, and shut the executor down on close.
- Enforce send.pending.max with a semaphore, return the permit when the
  async-failure check throws, keep async failures sticky, bound flush by
  producer send.timeout, and drain pending sends before closing the writer.
- Expose sendCompletionRt and sendPendingRecords writer gauges.
- Retry source seek up to two times with 100 ms backoff on connect,
  send-request and timeout transport failures; propagate other failures
  and interrupts immediately.
- Cover callback completion, permit accounting, flush/close semantics and
  seek retry boundaries with unit tests.
@lizhimins
lizhimins merged commit 7ba7d0d into apache:main Nov 2, 2026
2 checks passed
@lizhimins
lizhimins deleted the fix-source-startup-seek-retry branch November 2, 2026 03:33
lizhimins added a commit that referenced this pull request Nov 2, 2026
* [ISSUE #155] refactor: migrate to the new Source/Sink API only

Flink 2.x removed SourceFunction, SinkFunction, TwoPhaseCommittingSink,
TableSchema and the ConfigOption-typed Configuration accessors. The
connector still ships the legacy implementations next to the FLIP-27 /
sink2 ones, so it cannot compile against 2.x and every fix lands twice.

Following apache/flink-connector-kafka (new API only, zero legacy,
version differences isolated in branches), migrate the shared codebase
to the new API while staying on Flink 1.20.3 / Java 11:

- Delete the legacy source/sink paths and their tests; the FLIP-27
  Source and the sink2 Sink + SupportsCommitter become the only runtime
  paths. The SQL factory keeps accepting the existing DDL options but
  resolves them to the new implementations.
- TableSchema / SchemaValidator -> ResolvedSchema; Configuration
  getXxx(ConfigOption) / setXxx -> get / set.
- Sink.InitContext and TypeInformation.createSerializer(ExecutionConfig)
  are still abstract in 1.20; implement the deprecated overloads as
  delegations. The 1.20 runtime calls the WriterInitContext /
  SerializerConfig overloads, so the main code path is already the 2.x
  one.
- The async-send bounding and seek-retry behavior of #154 is preserved.

Semantics are unchanged: at-least-once / exactly-once guarantees, offset
management from Flink state, checkpoint-aligned transaction commits and
the #154 permit accounting all stay as they are; this is an API
migration, not a behavior change.

Verified: mvn clean install green on JDK 17 targeting Java 11 (remoting
146 / grpc 80 unit tests, checkstyle and spotless clean), both SQL
fat-jars produced.

This is the branch point for the 1.x maintenance line (#155): a follow-up
commit switches main to Flink 2.2.1 / Java 17 by flipping flink.version
and dropping the deprecated-overload bridges above.

* [ISSUE #155] style: apply spotless formatting under JDK 11

The JDK 17 leg of the build silently skips spotless ("Spotless check
skipped"), so the four files reformatted here only surfaced on the JDK 11
leg, which is the one that actually runs the formatter. Wrap them to the
100-column google-java-format output; no behavioural change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

remoting sink: async send runs synchronously on the task thread and startup seek fails on transient transport errors

1 participant