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] Optimize snapshotManager code eliminate duplicate logic. #4827

Open
wants to merge 2 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
198 changes: 75 additions & 123 deletions paimon-core/src/main/java/org/apache/paimon/utils/SnapshotManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.paimon.Changelog;
import org.apache.paimon.Snapshot;
import org.apache.paimon.annotation.VisibleForTesting;
import org.apache.paimon.fs.FileIO;
import org.apache.paimon.fs.FileStatus;
import org.apache.paimon.fs.Path;
Expand Down Expand Up @@ -290,15 +291,11 @@ private Snapshot changelogOrSnapshot(long snapshotId) {
return earliest - 1;
}

while (earliest < latest) {
long mid = (earliest + latest + 1) / 2;
if (changelogOrSnapshot(mid).timeMillis() < timestampMills) {
earliest = mid;
} else {
latest = mid - 1;
}
}
return earliest;
return binarySearch(
earliest,
latest,
id -> changelogOrSnapshot(id).timeMillis() < timestampMills,
false);
}

/**
Expand All @@ -316,22 +313,12 @@ private Snapshot changelogOrSnapshot(long snapshotId) {
if (earliestSnapShot.timeMillis() > timestampMills) {
return earliestSnapShot;
}
Snapshot finalSnapshot = null;
while (earliest <= latest) {
long mid = earliest + (latest - earliest) / 2; // Avoid overflow
Snapshot snapshot = snapshot(mid);
long commitTime = snapshot.timeMillis();
if (commitTime > timestampMills) {
latest = mid - 1; // Search in the left half
} else if (commitTime < timestampMills) {
earliest = mid + 1; // Search in the right half
finalSnapshot = snapshot;
} else {
finalSnapshot = snapshot; // Found the exact match
break;
}
}
return finalSnapshot;

Long resultId =
binarySearch(
earliest, latest, id -> snapshot(id).timeMillis() <= timestampMills, false);

return resultId == null ? null : snapshot(resultId);
}

/**
Expand All @@ -349,22 +336,12 @@ private Snapshot changelogOrSnapshot(long snapshotId) {
if (latestSnapShot.timeMillis() < timestampMills) {
return null;
}
Snapshot finalSnapshot = null;
while (earliest <= latest) {
long mid = earliest + (latest - earliest) / 2; // Avoid overflow
Snapshot snapshot = snapshot(mid);
long commitTime = snapshot.timeMillis();
if (commitTime > timestampMills) {
latest = mid - 1; // Search in the left half
finalSnapshot = snapshot;
} else if (commitTime < timestampMills) {
earliest = mid + 1; // Search in the right half
} else {
finalSnapshot = snapshot; // Found the exact match
break;
}
}
return finalSnapshot;

Long resultId =
binarySearch(
earliest, latest, id -> snapshot(id).timeMillis() >= timestampMills, true);

return resultId == null ? null : snapshot(resultId);
}

public @Nullable Snapshot earlierOrEqualWatermark(long watermark) {
Expand All @@ -375,55 +352,21 @@ private Snapshot changelogOrSnapshot(long snapshotId) {
if (earliest == null || latest == null || snapshot(latest).watermark() == Long.MIN_VALUE) {
return null;
}
Long earliestWatermark = null;
// find the first snapshot with watermark
if ((earliestWatermark = snapshot(earliest).watermark()) == null) {
while (earliest < latest) {
earliest++;
earliestWatermark = snapshot(earliest).watermark();
if (earliestWatermark != null) {
break;
}
}
}
if (earliestWatermark == null) {
Long firstValidId = findFirstSnapshotWithWatermark(earliest, latest);
if (firstValidId == null) {
return null;
}

if (earliestWatermark >= watermark) {
return snapshot(earliest);
}
Snapshot finalSnapshot = null;
Long resultId =
binarySearch(
firstValidId,
latest,
id ->
snapshot(id).watermark() != null
&& snapshot(id).watermark() <= watermark,
false);

while (earliest <= latest) {
long mid = earliest + (latest - earliest) / 2; // Avoid overflow
Snapshot snapshot = snapshot(mid);
Long commitWatermark = snapshot.watermark();
if (commitWatermark == null) {
// find the first snapshot with watermark
while (mid >= earliest) {
mid--;
commitWatermark = snapshot(mid).watermark();
if (commitWatermark != null) {
break;
}
}
}
if (commitWatermark == null) {
earliest = mid + 1;
} else {
if (commitWatermark > watermark) {
latest = mid - 1; // Search in the left half
} else if (commitWatermark < watermark) {
earliest = mid + 1; // Search in the right half
finalSnapshot = snapshot;
} else {
finalSnapshot = snapshot; // Found the exact match
break;
}
}
}
return finalSnapshot;
return resultId == null ? null : snapshot(resultId);
}

public @Nullable Snapshot laterOrEqualWatermark(long watermark) {
Expand All @@ -434,55 +377,64 @@ private Snapshot changelogOrSnapshot(long snapshotId) {
if (earliest == null || latest == null || snapshot(latest).watermark() == Long.MIN_VALUE) {
return null;
}
Long earliestWatermark = null;
// find the first snapshot with watermark
if ((earliestWatermark = snapshot(earliest).watermark()) == null) {
while (earliest < latest) {
earliest++;
earliestWatermark = snapshot(earliest).watermark();
if (earliestWatermark != null) {
break;
}
}
}
if (earliestWatermark == null) {

Long firstValidId = findFirstSnapshotWithWatermark(earliest, latest);
if (firstValidId == null) {
return null;
}

if (earliestWatermark >= watermark) {
return snapshot(earliest);
if (snapshot(firstValidId).watermark() >= watermark) {
return snapshot(firstValidId);
}
Snapshot finalSnapshot = null;

Long resultId =
binarySearch(
earliest,
latest,
id ->
snapshot(id).watermark() != null
&& snapshot(id).watermark() >= watermark,
true);

return resultId == null ? null : snapshot(resultId);
}

private Long findFirstSnapshotWithWatermark(Long earliest, Long latest) {
while (earliest <= latest) {
long mid = earliest + (latest - earliest) / 2; // Avoid overflow
Snapshot snapshot = snapshot(mid);
Long commitWatermark = snapshot.watermark();
if (commitWatermark == null) {
// find the first snapshot with watermark
while (mid >= earliest) {
mid--;
commitWatermark = snapshot(mid).watermark();
if (commitWatermark != null) {
break;
}
}
Long watermark = snapshot(earliest).watermark();
if (watermark != null) {
return earliest;
}
if (commitWatermark == null) {
earliest = mid + 1;
earliest++;
}
return null;
}

@VisibleForTesting
public @Nullable Long binarySearch(
Long start,
Long end,
java.util.function.Predicate<Long> condition,
boolean findEarliest) {
Long result = null;
while (start <= end) {
long mid = start + (end - start) / 2;
if (condition.test(mid)) {
result = mid;
if (findEarliest) {
end = mid - 1;
} else {
start = mid + 1;
}
} else {
if (commitWatermark > watermark) {
latest = mid - 1; // Search in the left half
finalSnapshot = snapshot;
} else if (commitWatermark < watermark) {
earliest = mid + 1; // Search in the right half
if (findEarliest) {
start = mid + 1;
} else {
finalSnapshot = snapshot; // Found the exact match
break;
end = mid - 1;
}
}
}
return finalSnapshot;
return result;
}

public long snapshotCount() throws IOException {
Expand Down
Loading
Loading