Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[connector] Fix partition state update and filter non-existent partitions on restore #359

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

codope
Copy link

@codope codope commented Feb 8, 2025

Purpose

Linked issue: close #348

This PR fixes an issue where removed partitions were still retained in the source reader’s state. Upon recovery, the source attempted to re-subscribe to non-existent partitions, leading to a PartitionNotExistException.

Changes

  • Mark Finished Splits on Partition Removal:
    Active bounded splits are now marked as finished when a partition removal event is received. This prevents removed partitions from being restored.

  • Remove Pending Snapshot Splits and Unsubscribe Buckets:
    Pending snapshot splits and subscribed log buckets associated with removed partitions are removed and unsubscribed respectively.

  • Filter Non-existent Partitions During Restore:
    The subscribeLog method now catches PartitionNotExistException and logs a warning instead of failing, filtering out any non-existent partitions on restore.

Tests

Added FlinkTableSourceFailOverITCase integration test.

API and Format

None

Documentation

NA

@CLAassistant
Copy link

CLAassistant commented Feb 8, 2025

CLA assistant check
All committers have signed the CLA.

- Mark active bounded splits as finished on partition removal.
- Remove pending snapshot splits and unsubscribe dropped log buckets.
- Catch PartitionNotExistException during subscribeLog to log a warning.
@codope codope force-pushed the handle-partition-remove-event branch from 3814713 to 06d5104 Compare February 19, 2025 13:00
@wuchong wuchong self-requested a review February 19, 2025 14:25
// Traverse the exception chain to check for PartitionNotExistException.
Throwable cause = e;
boolean partitionNotExist = false;
while (cause != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can replace the while loop using

boolean partitionNotExist = ExceptionUtils.findThrowable(e, PartitionNotExistException.class).isPresent();

} catch (IOException e) {
LOG.warn(
"Failed to close current bounded split for removed partition {}.",
currentBucket.getPartitionId(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can also use removedPartitions.get(currentBucket.getPartitionId()) to print the partition name rather than id.

LOG.warn(
"Partition {} does not exist when subscribing to log for split {}. Skipping subscription.",
partitionId,
split.splitId());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should return to avoid unnecessary subscribe the partition.

@wuchong
Copy link
Member

wuchong commented Feb 20, 2025

Thanks @codope for the contribution! The fixing looks good to me. I pushed a commit to improve the IT case so that:

  1. avoid Thread.sleep(..) as it will lead tests unstable if CPU is high.
  2. Verify with data instead of verifying with no exception.
  3. the new IT case can also reproduce the original exception mentioned in issue description.

Could you help to review the changes @luoyuxia ?

Copy link
Collaborator

@luoyuxia luoyuxia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @codope @wuchong Overall LGTM. Just left minor comments.

@@ -262,17 +287,42 @@ public Set<TableBucket> removePartitions(Map<Long, String> removedPartitions) {
// todo, may consider to close the current snapshot reader if
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The todo comment can be removed now since in this pr, we wil close currentBoundedSplit

@@ -145,6 +149,12 @@ public RecordsWithSplitIds<RecordAndPos> fetch() throws IOException {
new HashSet<>(emptyLogSplits), flinkSourceReaderMetrics);
emptyLogSplits.clear();
return records;
} else if (!removedSplits.isEmpty()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can check !removedSplits.isEmpty() at the beginning so that the flink enumerate can remove the state of removed splits as soon as possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can move the code block at the beginning of fetch() method, because the currentBoundedSplitReader may still work in progress.

try {
if (partitionId != null) {
// Try to subscribe using the partition id.
logScanner.subscribe(partitionId, bucket, startingOffset);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:
I think we can limit the try catch scope to only this line since only subscribe a partition, the partitionNotExist will be thrown.
I'd like suggest to be:

            if (partitionId != null) {
                // Try to subscribe using the partition id.
                try {
                    logScanner.subscribe(partitionId, bucket, startingOffset);
                } catch (Exception e) {
                    // the PartitionNotExistException may still happens when partition is removed
                    // but Flink source reader failover before aware of it
                    // Traverse the exception chain to check for PartitionNotExistException.
                    boolean partitionNotExist =
                            ExceptionUtils.findThrowable(e, PartitionNotExistException.class)
                                    .isPresent();
                    if (partitionNotExist) {
                        // mark the not exist partition to be removed
                        removedSplits.add(split.splitId());
                        LOG.warn(
                                "Partition {} does not exist when subscribing to log for split {}. Skipping subscription.",
                                partitionId,
                                split.splitId());
                        return;
                    }
                }
            } else {
                // If no partition id, subscribe by bucket only.
                logScanner.subscribe(bucket, startingOffset);
            }

ExceptionUtils.findThrowable(e, PartitionNotExistException.class)
.isPresent();
if (partitionNotExist) {
LOG.warn(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also should mark split as removed since the partition is removed and so that flink enumerator can remove this split state.

@wuchong
Copy link
Member

wuchong commented Feb 21, 2025

@codope do you have time to address @luoyuxia 's comments? If you are busy, I can help with that.

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.

PartitionNotExistException will be thrown when restore from checkpoint
4 participants