[
https://issues.apache.org/jira/browse/FLINK-41671?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18151161#comment-18151161
]
Martijn Visser commented on FLINK-41671:
----------------------------------------
I looked into this further. It reproduces deterministically with the real
operators: a task with two input channels, WatermarkTimestampAssigner chained
into WatermarkCompactingSinkMaterializer, heap and RocksDB:
{code}
ch0: +I(id=1, pk=7)
ch0: WM 100, ch1: WM 100 -> emits +I(1)
ch1: +U(id=2, pk=7) // replacement, upstream subtask still at WM 100
ch0: WM 200 // the change happened after 200
ch1: WM 200 -> compacts {id=1, id=2}: DO ERROR throws
ch0: -U(id=1, pk=7)
{code}
With DO NOTHING the same input leaves pk 7 without a row. p=1 finishes.
The assigner runs after the primary key shuffle and stamps with the combined
watermark, not the watermark of the arrival channel as the FLIP describes.
Changing that doesn't help: stamping per channel still fails the repro script 8
out of 8 times, and it also fails without a join (Rank -> Rank).
I don't think this can be fixed in the sink. If id=2 really was inserted before
200 and id=1 really retracted after it, the sink gets exactly the same input,
so it can't tell a real conflict from a false one. Watermarks aren't aligned
across input channels: a subtask whose other input lags forwards records from
after W while its own watermark is still below W. Carrying a stamp from where
the change is made breaks at a join whose inputs are at different watermarks.
In a TLA+ model, compacting on aligned checkpoint barriers holds.
Workaround on 2.3.0: use ON CONFLICT DO DEDUPLICATE when the upsert key differs
from the sink's primary key and parallelism is above 1.
So the question becomes if compacting on aligned checkpoint barriers (Option 2
in the FLIP) is still an option, or if DO ERROR/NOTHING should be restricted
for these plans.
> ON CONFLICT DO ERROR reports false conflicts and DO NOTHING keeps stale rows
> when an update moves between join subtasks
> -----------------------------------------------------------------------------------------------------------------------
>
> Key: FLINK-41671
> URL: https://issues.apache.org/jira/browse/FLINK-41671
> Project: Flink
> Issue Type: Bug
> Components: Table SQL / Runtime
> Affects Versions: 2.3.0
> Reporter: Martijn Visser
> Priority: Critical
>
> {{WatermarkTimestampAssigner}} stamps each record with its own subtask's
> combined watermark when it arrives. When one input change retracts a row on
> one join subtask and inserts its replacement on another, the two can get
> different stamps, and {{WatermarkCompactingSinkMaterializer}} compacts the
> old and new row without the retraction. DO ERROR then fails on a key that
> never has two rows: the left side has one row per from_account and the join
> adds at most one match. DO NOTHING keeps the stale row and drops the new one,
> so the result stays wrong after end of input. The script below fails within
> two seconds at parallelism 4. It finishes at parallelism 1, and without ON
> CONFLICT.
> {code:sql}
> SET 'parallelism.default' = '4';
> SET 'table.dml-sync' = 'true';
> CREATE TABLE transactions (
> id BIGINT,
> from_account AS CAST(MOD(id, 10) AS INT),
> to_account AS CAST(MOD(id * 7 + id / 10, 10) AS INT),
> amount AS CAST(id + 1 AS DOUBLE),
> ts AS TIMESTAMPADD(MILLISECOND, CAST(id * 100 AS INT), TIMESTAMP
> '2021-01-01 00:00:00.000'),
> WATERMARK FOR ts AS ts - INTERVAL '5' SECOND
> ) WITH (
> 'connector' = 'datagen',
> 'fields.id.kind' = 'sequence',
> 'fields.id.start' = '0',
> 'fields.id.end' = '19999',
> 'rows-per-second' = '5000'
> );
> CREATE TABLE sink (
> from_account INT,
> id BIGINT,
> to_account INT,
> credits DOUBLE,
> PRIMARY KEY (from_account) NOT ENFORCED
> ) WITH ('connector' = 'blackhole');
> INSERT INTO sink
> SELECT l.from_account, l.id, l.to_account, c.credits
> FROM (
> SELECT id, from_account, to_account
> FROM (
> SELECT id, from_account, to_account,
> ROW_NUMBER() OVER (PARTITION BY from_account ORDER BY ts DESC, id DESC)
> AS rn
> FROM transactions)
> WHERE rn = 1) AS l
> JOIN (
> SELECT to_account AS account, SUM(amount) AS credits
> FROM transactions
> GROUP BY to_account) AS c
> ON l.to_account = c.account
> ON CONFLICT DO ERROR;
> {code}
> Fails with: Primary key constraint violation: multiple distinct records with
> the same primary key detected. Conflicting key: [from_account=4].
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)