-
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] Support parallel reading of local orphan clean #4320
Merged
Merged
Changes from all commits
Commits
Show all changes
5 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
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 |
---|---|---|
|
@@ -42,8 +42,10 @@ | |
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.BinaryOperator; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
@@ -52,6 +54,8 @@ | |
import static org.apache.paimon.utils.BranchManager.DEFAULT_MAIN_BRANCH; | ||
import static org.apache.paimon.utils.BranchManager.branchPath; | ||
import static org.apache.paimon.utils.FileUtils.listVersionedFiles; | ||
import static org.apache.paimon.utils.ThreadPoolUtils.createCachedThreadPool; | ||
import static org.apache.paimon.utils.ThreadPoolUtils.randomlyOnlyExecute; | ||
|
||
/** Manager for {@link Snapshot}, providing utility methods related to paths and snapshot hints. */ | ||
public class SnapshotManager implements Serializable { | ||
|
@@ -414,8 +418,7 @@ public List<Path> snapshotPaths(Predicate<Long> predicate) throws IOException { | |
} | ||
|
||
public Iterator<Snapshot> snapshotsWithinRange( | ||
Optional<Long> optionalMaxSnapshotId, Optional<Long> optionalMinSnapshotId) | ||
throws IOException { | ||
Optional<Long> optionalMaxSnapshotId, Optional<Long> optionalMinSnapshotId) { | ||
Long lowerBoundSnapshotId = earliestSnapshotId(); | ||
Long upperBoundSnapshotId = latestSnapshotId(); | ||
Long lowerId; | ||
|
@@ -457,7 +460,7 @@ public Iterator<Snapshot> snapshotsWithinRange( | |
|
||
public Iterator<Changelog> changelogs() throws IOException { | ||
return listVersionedFiles(fileIO, changelogDirectory(), CHANGELOG_PREFIX) | ||
.map(snapshotId -> changelog(snapshotId)) | ||
.map(this::changelog) | ||
.sorted(Comparator.comparingLong(Changelog::id)) | ||
.iterator(); | ||
} | ||
|
@@ -469,38 +472,62 @@ public Iterator<Changelog> changelogs() throws IOException { | |
public List<Snapshot> safelyGetAllSnapshots() throws IOException { | ||
List<Path> paths = | ||
listVersionedFiles(fileIO, snapshotDirectory(), SNAPSHOT_PREFIX) | ||
.map(id -> snapshotPath(id)) | ||
.map(this::snapshotPath) | ||
.collect(Collectors.toList()); | ||
|
||
List<Snapshot> snapshots = new ArrayList<>(); | ||
for (Path path : paths) { | ||
try { | ||
snapshots.add(Snapshot.fromJson(fileIO.readFileUtf8(path))); | ||
} catch (FileNotFoundException ignored) { | ||
} | ||
} | ||
List<Snapshot> snapshots = Collections.synchronizedList(new ArrayList<>(paths.size())); | ||
collectSnapshots( | ||
path -> { | ||
try { | ||
snapshots.add(Snapshot.fromJson(fileIO.readFileUtf8(path))); | ||
} catch (IOException e) { | ||
if (!(e instanceof FileNotFoundException)) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
}, | ||
paths); | ||
|
||
return snapshots; | ||
} | ||
|
||
public List<Changelog> safelyGetAllChangelogs() throws IOException { | ||
List<Path> paths = | ||
listVersionedFiles(fileIO, changelogDirectory(), CHANGELOG_PREFIX) | ||
.map(id -> longLivedChangelogPath(id)) | ||
.map(this::longLivedChangelogPath) | ||
.collect(Collectors.toList()); | ||
|
||
List<Changelog> changelogs = new ArrayList<>(); | ||
for (Path path : paths) { | ||
try { | ||
String json = fileIO.readFileUtf8(path); | ||
changelogs.add(Changelog.fromJson(json)); | ||
} catch (FileNotFoundException ignored) { | ||
} | ||
} | ||
List<Changelog> changelogs = Collections.synchronizedList(new ArrayList<>(paths.size())); | ||
collectSnapshots( | ||
path -> { | ||
try { | ||
changelogs.add(Changelog.fromJson(fileIO.readFileUtf8(path))); | ||
} catch (IOException e) { | ||
if (!(e instanceof FileNotFoundException)) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
}, | ||
paths); | ||
|
||
return changelogs; | ||
} | ||
|
||
private void collectSnapshots(Consumer<Path> pathConsumer, List<Path> paths) | ||
throws IOException { | ||
ExecutorService executor = | ||
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. Why not use ThreadPoolUtils#createCachedThreadPool? 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. fix |
||
createCachedThreadPool( | ||
Runtime.getRuntime().availableProcessors(), "SNAPSHOT_COLLECTOR"); | ||
|
||
try { | ||
randomlyOnlyExecute(executor, pathConsumer, paths); | ||
} catch (RuntimeException e) { | ||
throw new IOException(e); | ||
} finally { | ||
executor.shutdown(); | ||
} | ||
} | ||
|
||
/** | ||
* Try to get non snapshot files. If any error occurred, just ignore it and return an empty | ||
* result. | ||
|
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.
I think if manifests size is very big, use randomlyOnlyExecute may oom?
Do you think so?
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.
I think there is indeed a risk of OOM.
But this can be avoided by reducing the parallelism (if it's equal to 1, then it's similar to serial execution before), and here it's just providing an ability to read in parallel.
If parallelism 1 is still OOM, then distributed mode is considered.
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.
What about change randomlyOnlyExecute to sequentialBatchedExecute?
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.
Yeah, I repleaced it with sequentialBatchedExecute since It has better control over memory
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.
In the end, you still need to put it in a set, so as long as there is no inflation during the reading process, there should be no risk of oom here.
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.
I test
sequentialBatchedExecute
randomlyOnlyExecute
andrandomlyExecute
in 16 parallelism on table with 2PB of data , None of them happened OOM.Besides
sequentialBatchedExecute
procedure cost 30 min ,randomlyOnlyExecute
andrandomlyExecute
cost about 20 minSo I prefer to use randomlyOnlyExecute because this method is more concise and no other variables are introduced, it can also reduce the gc process of temporary data files variable. The disadvantage of this method is that there will be waiting lock overhead while inserting useFiles, but this has no impact on the overall time consumption, because the overall time consumption mainly comes from IO.
What do you think? @JingsongLi @wwj6591812