Victor Babenko created FLINK-41800:
--------------------------------------
Summary: Pulsar source emits messages twice after the consumer
reconnects to a broker
Key: FLINK-41800
URL: https://issues.apache.org/jira/browse/FLINK-41800
Project: Flink
Issue Type: Bug
Components: Connectors / Pulsar
Affects Versions: pulsar-4.2.0, pulsar-5.0.0
Reporter: Victor Babenko
h3. Symptom
After a Pulsar consumer reconnects, for example when a broker unloads the
namespace bundle that owns the topic, the source emits up to one checkpoint
interval of messages a second time. There's no failover, restore, or error in
the Flink job. A job consuming a partitioned topic saw thousands of duplicates
on one partition, with the two copies emitted seconds apart, right after a
bundle unload during a traffic burst.
h3. Root cause
The source subscribes with an Exclusive subscription and acks cumulatively only
when a checkpoint completes. When the client reconnects, the broker redelivers
everything after the subscription's mark-delete position, which is the last
completed checkpoint. The reader has to drop the messages it already emitted.
On main, the reader has a guard for this, but it compares each message against
two IDs that can't catch a reconnect:
https://github.com/apache/flink-connector-pulsar/blob/23890912234897a58f75b1ec7a435fc70ca41579/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/reader/PulsarPartitionSplitReader.java#L157
https://github.com/apache/flink-connector-pulsar/blob/23890912234897a58f75b1ec7a435fc70ca41579/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/reader/PulsarPartitionSplitReader.java#L203-L208
* latestMessageIdInTheCurrentFetch is a local variable that resets on every
fetch(), so it only catches a redelivery that starts inside the same fetch.
* registeredSplit.getLatestConsumedId() is final on the split and stays at the
position from when the split was assigned or restored. The emitted position is
tracked on the split state in the source reader, which the split reader never
sees.
On v4.2 and v4.1 there is no guard at all: fetch() emits whatever the consumer
returns.
https://github.com/apache/flink-connector-pulsar/blob/52faecf8b5897c55dde57cbef5371538fa2ae40e/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/reader/PulsarPartitionSplitReader.java#L132-L156
h3. Trigger conditions
* Any event that makes the consumer reconnect without the Flink job restarting:
a namespace bundle unload or split, a broker restart or rolling upgrade, or
load-manager rebalancing.
* Messages consumed since the last completed checkpoint. The number of
duplicates is roughly the throughput times the time since the last checkpoint,
so a long checkpoint interval and a busy topic make it worse. On a quiet topic
you might not notice.
To reproduce: start a job reading a persistent topic with a long checkpoint
interval (a few minutes), produce at a steady rate, then run "pulsar-admin
topics unload <topic>" between checkpoints. The sink receives the messages
since the last checkpoint twice.
h3. Proposed fix
Keep the last fetched message ID on the split reader for the life of the split,
and skip (and release) any message at or before it. This extends the existing
comparison from one fetch to the whole split.
It's safe because the source always uses an Exclusive subscription, so the
broker delivers in order, and each split reader owns exactly one split for its
lifetime, so the field never needs a reset. On restore a new reader is created
and the existing start-position check covers it.
We considered seeking the consumer to the last emitted ID after a reconnect
instead. The consumer API doesn't report reconnects, so that needs a hook into
client internals, and a seek itself disconnects and redelivers.
On v4.2 the fix adds the same field and comparison. The start-position check
isn't needed there, because on restore the reader resets the subscription
cursor to just after the checkpointed message before it subscribes.
h3. Known limits
The ID comparison assumes message IDs increase on the topic. Two configurations
break that, so the fix doesn't apply the guard to them:
* Non-persistent topics give every message the same ledger and entry ID, so the
comparison would skip every message after the first. (The current within-fetch
guard on main already drops them.)
* With retryEnable=true the consumer reads the topic and its retry topic
together, and the two have separate ID spaces.
For both, the reader keeps the current behavior, which means they still see
duplicates after a reconnect.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)