Skip to content

Error in writing CDC data with Flink #2482

Closed

Description

@coolderli

I'm using Flink to write CDC data to iceberg, and I'm using the master branch.I came across the following problem.

Caused by: org.apache.iceberg.exceptions.ValidationException: Cannot determine history between starting snapshot null and current 8203201752131271868
	at org.apache.iceberg.exceptions.ValidationException.check(ValidationException.java:46)
	at org.apache.iceberg.MergingSnapshotProducer.validateDataFilesExist(MergingSnapshotProducer.java:313)
	at org.apache.iceberg.BaseRowDelta.validate(BaseRowDelta.java:95)
	at org.apache.iceberg.SnapshotProducer.apply(SnapshotProducer.java:162)
	at org.apache.iceberg.SnapshotProducer.lambda$commit$2(SnapshotProducer.java:283)
	at org.apache.iceberg.util.Tasks$Builder.runTaskWithRetry(Tasks.java:404)
	at org.apache.iceberg.util.Tasks$Builder.runSingleThreaded(Tasks.java:213)
	at org.apache.iceberg.util.Tasks$Builder.run(Tasks.java:197)
	at org.apache.iceberg.util.Tasks$Builder.run(Tasks.java:189)
	at org.apache.iceberg.SnapshotProducer.commit(SnapshotProducer.java:282)
	at org.apache.iceberg.flink.sink.IcebergFilesCommitter.commitOperation(IcebergFilesCommitter.java:298)
	at org.apache.iceberg.flink.sink.IcebergFilesCommitter.commitDeltaTxn(IcebergFilesCommitter.java:285)
	at org.apache.iceberg.flink.sink.IcebergFilesCommitter.commitUpToCheckpoint(IcebergFilesCommitter.java:210)
	at org.apache.iceberg.flink.sink.IcebergFilesCommitter.initializeState(IcebergFilesCommitter.java:147)
	at org.apache.flink.streaming.api.operators.StreamOperatorStateHandler.initializeOperatorState(StreamOperatorStateHandler.java:106)
	at org.apache.flink.streaming.api.operators.AbstractStreamOperator.initializeState(AbstractStreamOperator.java:285)
	at org.apache.flink.streaming.runtime.tasks.OperatorChain.initializeStateAndOpenOperators(OperatorChain.java:291)
	at org.apache.flink.streaming.runtime.tasks.StreamTask.lambda$beforeInvoke$0(StreamTask.java:473)
	at org.apache.flink.streaming.runtime.tasks.StreamTaskActionExecutor$1.runThrowing(StreamTaskActionExecutor.java:47)
	at org.apache.flink.streaming.runtime.tasks.StreamTask.beforeInvoke(StreamTask.java:469)
	at org.apache.flink.streaming.runtime.tasks.StreamTask.invoke(StreamTask.java:522)
	at org.apache.flink.runtime.taskmanager.Task.doRun(Task.java:721)
	at org.apache.flink.runtime.taskmanager.Task.run(Task.java:546)
	at java.lang.Thread.run(Thread.java:748)

When the Flink job is running, I start a spark job to rewrite data.This is my spark code

RewriteDataFilesActionResult rewriteDataFilesActionResult = Actions.forTable(sparkSession, table)
                        .rewriteDataFiles()
                        .targetSizeInBytes(TARGET_SIZE)
                        .splitLookback(SPLIT_LOOK_BACK)
                        .execute();

 List<String> orphanFiles = Actions.forTable(sparkSession, table)
                        .removeOrphanFiles()
                        .olderThan(expireOrphanTime)
                        .execute();

BaseExpireSnapshotsSparkAction expireSnapshotsSparkAction = new BaseExpireSnapshotsSparkAction(sparkSession, table);
                ExpireSnapshots.Result result = expireSnapshotsSparkAction
                        .expireOlderThan(expireSnapshotTime)
                        .retainLast(30)
                        .execute();

Can someone help me see this problem? Thanks.
Does RewriteAction rewrite some datafiles that Flink has not yet committed?

Activity

  1. openinx commented on Apr 26, 2021

    @openinx
    Member

    I checked the place that throw the ValidationException. The reason that we encountered this because we've expired the several latest snapshots, then there must be a snapshot whose parent snapshot is not null but we could not find the snapshot entry in TableMetadata#snapshotById.

    For this issue, I think we should fix the problem from two aspects:

    1. The validateDataFilesExist should stop to traverse the null snapshot even if it's a parent of others. ( I will close the Core: Fix ValidationException after expired snapshots #2511 because it's not the correct direction I think )
    2. The ExpireSnapshotsSparkAction will need to consider the format v2, I'm still not sure whether will it be able to expire the snapshots in v2. FYI @aokolnychyi
  2. jshmchenxi commented on May 18, 2021

    @jshmchenxi
    Contributor

    @openinx Hi, we have also run into this problem.
    Do you think it a good idea when expiring a snapshot, we should set the parentId of its children to null?
    In this way the validation can succeed and since parent snapshot is expired, setting parentId to null might be reasonable.

  3. added this to the Row-level Delete milestone on May 18, 2021
  4. openinx commented on May 18, 2021

    @openinx
    Member

    Do you think it a good idea when expiring a snapshot, we should set the parentId of its children to null?

    I think setting the parentId to be null could be one of the solution, but I'd like to check what's the specific PR if you have one.

  5. jshmchenxi commented on May 18, 2021

    @jshmchenxi
    Contributor

    @openinx I have created a PR and will test it in our environment.

  6. jshmchenxi commented on May 21, 2021

    @jshmchenxi
    Contributor

    I found that setting parentId to null will cause some tests to fail because it loses relationship between child and parent snapshots. I tried another approach in the PR : #2603 (comment)

  7. ayush-san commented on Aug 11, 2021

    @ayush-san

    should stop to traverse the null snapshot even if it's a parent of others

    @openinx I think we should calculate the startingSnapshotId inside the IcebergFilesCommitter. Since it does maintain checkpoint id and flink job id. We can add startingSnapshotId too in the summary.

    @rdblue What do you think? I think it might answer your question too. Since the committer will update the startingSnapshotId from the summary and validation will only consider the table history from that point

  8. rdblue commented on Aug 11, 2021

    @rdblue
    Contributor

    @ayush-san, I don't think I understand what you're suggesting.

    The PR was trying to support a narrow case where history is no longer available, but you don't actually need history because the starting snapshot is null. That's not all that useful because you probably have a non-null starting snapshot ID.

  9. ayush-san commented on Aug 11, 2021

    @ayush-san

    @rdblue The PR was trying to handle the case when the validation try to build history from the initial snapshot but that snapshot doesn't exist because of the expire-snapshot action

    Here, I am suggesting that we add the last snapshot id in the snapshot summary of expire-snapshot action and use that to set startingSnapshotId so that validation will only consider the table history from that point.

    We do something similar in IcebergFilesCommitter as we store the flink job id and checkpoint id in snapshot summary.

  10. rdblue commented on Aug 11, 2021

    @rdblue
    Contributor

    @ayush-san, let me make sure I understand. Say a table has snapshots 1, 2, 3, 4, and 5, and that there is a Flink streaming process that has consumed through snapshot 2. Then, someone expires snapshots and removes 1, 2, and 3 so the table only has snapshots 4 and 5. The validation that is failing looks at 5, then 4, then sees that there is no snapshot 3. Because it can't provide the rows appended in 3, it fails.

    It sounds like you're suggesting that we could keep track of the oldest snapshot in the table and use that for startingSnapshotId. So instead of the Flink job starting from snapshot 2 and reading snapshots 3, 4, and 5, it would start from snapshot 3 and read only 4 and 5. The problem is that this automatically skips rows from snapshot 3, which isn't correct.

    It also sounds like you're suggesting that we annotate a snapshot, which I don't think would be necessary because we could always traverse parents from the current table state and only process the snapshots that are available.

    What the committer is doing is different. The committer keeps the checkpoint ID in the snapshot summary so that it can check whether a given checkpoint has been committed to the table already. That makes commits exactly-once when the snapshot history is longer than the time the application was unable to commit.

  11. ayush-san commented on Aug 12, 2021

    @ayush-san

    It sounds like you're suggesting that we could keep track of the oldest snapshot in the table and use that for startingSnapshotId. So instead of the Flink job starting from snapshot 2 and reading snapshots 3, 4, and 5, it would start from snapshot 3 and read only 4 and 5. The problem is that this automatically skips rows from snapshot 3, which isn't correct.

    @rdblue So what should be the ideal way around this? Since running regular maintenance procedures(Compaction, expire-snapshots, deleting orphans files, etc) are really important for streaming pipelines as they will keep metadata size in check. But currently, we cannot run expire snapshots as the flink validation will fail.

    My logic for annotating a snapshot was that any reader/writer will know that the table doesn't contain all the snapshots, so they can update their execution according to that. Yes, we can always traverse parents from the current table state and only process the snapshots that are available but shouldn't an action that alters the table metadata should be the one providing this information?

  12. removed this from the Row-level Delete milestone on Aug 25, 2021
  13. rdblue commented on Oct 8, 2021

    @rdblue
    Contributor

    There seems to be a lot of confusion around this issue. It was just referenced again in this comment.

    The problem is not the behavior of the validation. That's doing the right thing for how it is configured. I think that the problem is that the validation is configured to look over the entire table history, which is clearly not correct.

    The problematic validation is validateDataFilesExist. That only needs to be used when adding position deletes because equality deletes do not reference specific data files. Since position deletes for a CDC stream are only added against data files that are being added, I don't think that validation even needs to be configured. We can simply remove these two lines:

    .validateDataFilesExist(ImmutableList.copyOf(result.referencedDataFiles()))
    .validateDeletedFiles();

    @openinx and @stevenzwu, what do you think?

  14. rdblue commented on Oct 19, 2021

    @rdblue
    Contributor

    I've marked the fix for inclusion in the 0.12.1 patch release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions