-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[core] Fix race condition for earliest snapshot #4930
Open
yunfengzhou-hub
wants to merge
2
commits into
apache:master
Choose a base branch
from
yunfengzhou-hub:snapshot-earliest-race
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,11 +163,28 @@ public Snapshot tryGetSnapshot(long snapshotId) throws FileNotFoundException { | |
return snapshot; | ||
} | ||
|
||
private @Nullable Snapshot tryGetEarliestSnapshotLaterThanOrEqualTo( | ||
long snapshotId, long stopSnapshotId) { | ||
while (snapshotId <= stopSnapshotId) { | ||
try { | ||
return tryGetSnapshot(snapshotId); | ||
} catch (FileNotFoundException e) { | ||
snapshotId++; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public Changelog changelog(long snapshotId) { | ||
Path changelogPath = longLivedChangelogPath(snapshotId); | ||
return Changelog.fromPath(fileIO, changelogPath); | ||
} | ||
|
||
private Changelog tryGetChangelog(long snapshotId) throws FileNotFoundException { | ||
Path changelogPath = longLivedChangelogPath(snapshotId); | ||
return Changelog.tryFromPath(fileIO, changelogPath); | ||
} | ||
|
||
public Changelog longLivedChangelog(long snapshotId) { | ||
return Changelog.fromPath(fileIO, longLivedChangelogPath(snapshotId)); | ||
} | ||
|
@@ -209,7 +226,27 @@ public boolean longLivedChangelogExists(long snapshotId) { | |
|
||
public @Nullable Snapshot earliestSnapshot() { | ||
Long snapshotId = earliestSnapshotId(); | ||
return snapshotId == null ? null : snapshot(snapshotId); | ||
if (snapshotId == null) { | ||
return null; | ||
} | ||
|
||
Long latestSnapshotId = null; | ||
do { | ||
try { | ||
return tryGetSnapshot(snapshotId); | ||
} catch (FileNotFoundException e) { | ||
LOG.warn( | ||
"The earliest snapshot was once identified but disappeared. " | ||
+ "It might have been expired by other jobs operating on this table. " | ||
+ "Searching for the second earliest snapshot instead. "); | ||
snapshotId++; | ||
if (latestSnapshotId == null) { | ||
latestSnapshotId = latestSnapshotId(); | ||
} | ||
} | ||
} while (latestSnapshotId != null && snapshotId <= latestSnapshotId); | ||
|
||
return null; | ||
} | ||
|
||
public @Nullable Long earliestSnapshotId() { | ||
|
@@ -268,25 +305,47 @@ private Snapshot changelogOrSnapshot(long snapshotId) { | |
} | ||
} | ||
|
||
private Snapshot tryGetChangelogOrSnapshot(long snapshotId) throws FileNotFoundException { | ||
if (longLivedChangelogExists(snapshotId)) { | ||
return tryGetChangelog(snapshotId); | ||
} else { | ||
return tryGetSnapshot(snapshotId); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the latest snapshot earlier than the timestamp mills. A non-existent snapshot may be | ||
* returned if all snapshots are equal to or later than the timestamp mills. | ||
*/ | ||
public @Nullable Long earlierThanTimeMills(long timestampMills, boolean startFromChangelog) { | ||
Long earliestSnapshot = earliestSnapshotId(); | ||
Long earliestSnapshotId = earliestSnapshotId(); | ||
Long earliest; | ||
if (startFromChangelog) { | ||
Long earliestChangelog = earliestLongLivedChangelogId(); | ||
earliest = earliestChangelog == null ? earliestSnapshot : earliestChangelog; | ||
Long earliestChangelogId = earliestLongLivedChangelogId(); | ||
earliest = earliestChangelogId == null ? earliestSnapshotId : earliestChangelogId; | ||
} else { | ||
earliest = earliestSnapshot; | ||
earliest = earliestSnapshotId; | ||
} | ||
Long latest = latestSnapshotId(); | ||
if (earliest == null || latest == null) { | ||
return null; | ||
} | ||
|
||
if (changelogOrSnapshot(earliest).timeMillis() >= timestampMills) { | ||
Snapshot earliestSnapshot = null; | ||
while (earliest <= latest) { | ||
try { | ||
earliestSnapshot = tryGetChangelogOrSnapshot(earliest); | ||
break; | ||
} catch (FileNotFoundException e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a log here? |
||
LOG.warn( | ||
"The earliest snapshot or changelog was once identified but disappeared. " | ||
+ "It might have been expired by other jobs operating on this table. " | ||
+ "Searching for the second earliest snapshot or changelog instead. "); | ||
earliest++; | ||
} | ||
} | ||
|
||
if (earliestSnapshot == null || earliestSnapshot.timeMillis() >= timestampMills) { | ||
return earliest - 1; | ||
} | ||
|
||
|
@@ -312,8 +371,9 @@ private Snapshot changelogOrSnapshot(long snapshotId) { | |
return null; | ||
} | ||
|
||
Snapshot earliestSnapShot = snapshot(earliest); | ||
if (earliestSnapShot.timeMillis() > timestampMills) { | ||
Snapshot earliestSnapShot = tryGetEarliestSnapshotLaterThanOrEqualTo(earliest, latest); | ||
|
||
if (earliestSnapShot == null || earliestSnapShot.timeMillis() > timestampMills) { | ||
return earliestSnapShot; | ||
} | ||
Snapshot finalSnapshot = null; | ||
|
@@ -375,12 +435,23 @@ private Snapshot changelogOrSnapshot(long snapshotId) { | |
if (earliest == null || latest == null || snapshot(latest).watermark() == Long.MIN_VALUE) { | ||
return null; | ||
} | ||
|
||
Snapshot earliestSnapShot = tryGetEarliestSnapshotLaterThanOrEqualTo(earliest, latest); | ||
|
||
if (earliestSnapShot == null) { | ||
return null; | ||
} | ||
|
||
Long earliestWatermark = null; | ||
// find the first snapshot with watermark | ||
if ((earliestWatermark = snapshot(earliest).watermark()) == null) { | ||
if ((earliestWatermark = earliestSnapShot.watermark()) == null) { | ||
while (earliest < latest) { | ||
earliest++; | ||
earliestWatermark = snapshot(earliest).watermark(); | ||
Snapshot snapshot = tryGetEarliestSnapshotLaterThanOrEqualTo(earliest, latest); | ||
if (snapshot == null) { | ||
continue; | ||
} | ||
earliestWatermark = snapshot.watermark(); | ||
if (earliestWatermark != null) { | ||
break; | ||
} | ||
|
@@ -391,7 +462,7 @@ private Snapshot changelogOrSnapshot(long snapshotId) { | |
} | ||
|
||
if (earliestWatermark >= watermark) { | ||
return snapshot(earliest); | ||
return tryGetEarliestSnapshotLaterThanOrEqualTo(earliest, latest); | ||
} | ||
Snapshot finalSnapshot = null; | ||
|
||
|
@@ -434,9 +505,16 @@ private Snapshot changelogOrSnapshot(long snapshotId) { | |
if (earliest == null || latest == null || snapshot(latest).watermark() == Long.MIN_VALUE) { | ||
return null; | ||
} | ||
|
||
Snapshot earliestSnapShot = tryGetEarliestSnapshotLaterThanOrEqualTo(earliest, latest); | ||
|
||
if (earliestSnapShot == null) { | ||
return null; | ||
} | ||
|
||
Long earliestWatermark = null; | ||
// find the first snapshot with watermark | ||
if ((earliestWatermark = snapshot(earliest).watermark()) == null) { | ||
if ((earliestWatermark = earliestSnapShot.watermark()) == null) { | ||
while (earliest < latest) { | ||
earliest++; | ||
earliestWatermark = snapshot(earliest).watermark(); | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a log here?