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

[core] Fix race condition for earliest snapshot #4930

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions paimon-core/src/main/java/org/apache/paimon/Changelog.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import javax.annotation.Nullable;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

Expand Down Expand Up @@ -105,9 +106,19 @@ public static Changelog fromJson(String json) {
}

public static Changelog fromPath(FileIO fileIO, Path path) {
try {
return tryFromPath(fileIO, path);
} catch (FileNotFoundException e) {
throw new RuntimeException("Fails to read changelog from path " + path, e);
}
}

public static Changelog tryFromPath(FileIO fileIO, Path path) throws FileNotFoundException {
try {
String json = fileIO.readFileUtf8(path);
return Changelog.fromJson(json);
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw new RuntimeException("Fails to read changelog from path " + path, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,8 @@ public Result scan(SnapshotReader reader) {
|| endTimestamp < earliestSnapshot.timeMillis()) {
return new NoSnapshot();
}
// in org.apache.paimon.utils.SnapshotManager.earlierOrEqualTimeMills
// 1. if earliestSnapshotId or latestSnapshotId is null startingSnapshotId will be null
// 2. if earliestSnapShot.timeMillis() > startTimestamp startingSnapshotId will be
// earliestSnapShotId
// if earliestSnapShot.timeMillis() > startTimestamp we should include the earliestSnapShot
// data
Long startSnapshotId =
(startingSnapshotId == null || earliestSnapshot.timeMillis() > startTimestamp)
? earliestSnapshot.id() - 1
: startingSnapshotId;
(startingSnapshotId == null) ? earliestSnapshot.id() - 1 : startingSnapshotId;
Snapshot endSnapshot = snapshotManager.earlierOrEqualTimeMills(endTimestamp);
Long endSnapshotId = (endSnapshot == null) ? latestSnapshot.id() : endSnapshot.id();
IncrementalStartingScanner incrementalStartingScanner =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ public SnapshotReader configure(SnapshotReader snapshotReader) {

@Nullable
public static Snapshot timeTravelToTimestamp(SnapshotManager snapshotManager, long timestamp) {
return snapshotManager.earlierOrEqualTimeMills(timestamp);
Snapshot snapshot = snapshotManager.earlierOrEqualTimeMills(timestamp);
if (snapshot == null) {
Long earliest = snapshotManager.earliestSnapshotId();
if (earliest != null) {
Snapshot earliestSnapShot = snapshotManager.snapshot(earliest);
if (earliestSnapShot.timeMillis() > timestamp) {
snapshot = earliestSnapShot;
}
}
}
return snapshot;
}
}
Loading