Victor Babenko created FLINK-41801:
--------------------------------------
Summary: Pulsar source drops messages when a fetch times out while
a message is being dispatched
Key: FLINK-41801
URL: https://issues.apache.org/jira/browse/FLINK-41801
Project: Flink
Issue Type: Bug
Components: Connectors / Pulsar
Affects Versions: pulsar-5.0.0
Reporter: Victor Babenko
h3. Symptom
The Pulsar source loses messages on an otherwise healthy topic. Nothing fails:
the job keeps running, the subscription backlog drains normally, and the
broker's redelivery metrics stay flat. The only trace is one of these Pulsar
client log lines on the task manager, at the moment the message is lost:
{noformat}
Race condition detected. Consumer: ... message was dropped
{noformat}
{noformat}
... pulled out a null conpletableFuture object ...
{noformat}
A job reading a partitioned topic with steady traffic and the default fetch
settings logged about 25 of these a day, each one a message that never reached
Flink.
h3. Root cause
Since FLINK-40676 the split reader uses Consumer#receiveAsync() and waits for
the future with a timeout. When the wait times out, it completes the client's
future exceptionally:
https://github.com/apache/flink-connector-pulsar/blob/23890912234897a58f75b1ec7a435fc70ca41579/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/reader/PulsarPartitionSplitReader.java#L169-L183
The Pulsar client treats an externally completed or cancelled receive future as
a cancel and removes it from its pending-receive queue. That removal runs on
the fetcher thread, while the client's internal executor hands incoming
messages to pending futures on its own thread (ConsumerImpl#messageReceived and
notifyPendingReceivedCallback, ConsumerBase#pollPendingReceive in Pulsar
4.0.14). The two race:
* If the executor has already polled the future, its complete() call fails
because the reader completed it first, and the client logs "Race condition
detected ... message was dropped".
* If the removal lands between the executor's "is there a pending receive?"
check and its poll, the poll returns null and the client logs "pulled out a
null conpletableFuture object".
In both cases the client has already taken the message off its incoming queue,
so it's gone. The subscription is Exclusive and the reader acks cumulatively on
checkpoint, so the broker never redelivers it, and a later checkpoint acks past
it.
Calling future.cancel(false) instead goes through the same cancellation
handler, so it has the same race. Newer Pulsar clients keep the same dispatch
logic, so a client upgrade doesn't help. Releases before 5.0 use the
synchronous receive(timeout), which polls the incoming queue directly and never
creates a pending future, so they aren't affected.
h3. Trigger conditions
* Any Pulsar source on the Flink 2 line (current main), with receiverQueueSize
> 0 (the default is 1000).
* A message arrives within a few microseconds of
pulsar.source.fetchOneMessageTime (or the fetch deadline) expiring. The more
fetch timeouts a topic has, the more often this happens, so moderate, bursty
traffic hits it more than a saturated or idle topic.
A deterministic reproduction needs control of the client's executor, so the PR
includes a unit test that drives a fake consumer through the client's own
CompletableFutureCancellationHandler. It shows that both the current timeout
handling and cancel(false) drop the message.
h3. Proposed fix
Never complete or cancel the client's receive future during normal fetching.
The reader keeps at most one outstanding receiveAsync() future across fetch()
calls: a timed-out wait leaves the future pending, and the next fetch waits on
the same future. A message the client hands over during a timeout is then
returned by the next fetch. This matches what the client expects from a single
caller with one receive outstanding.
On close(), the reader closes the consumer first, which fails any pending
receive and acks nothing, so a message in flight at close is redelivered after
the seek on restore. If a message had already completed the future after the
last fetch, close releases it, which matters only when message pooling is on.
h3. Known limit
With receiverQueueSize=0 the client uses ZeroQueueConsumerImpl, which grants
one flow permit per receiveAsync() call and doesn't grant a new one after a
reconnect. A pending receive kept across a reconnect would then stall the
consumer. The fix rejects receiverQueueSize=0 for the source with a clear
error. A zero queue can't receive batched messages anyway, which the connector
otherwise supports.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)