[SPARK-57530][CORE] Make SparkFileUtils.recursiveList null-safe and linear-time#56590
Draft
LuciferYang wants to merge 1 commit into
Draft
[SPARK-57530][CORE] Make SparkFileUtils.recursiveList null-safe and linear-time#56590LuciferYang wants to merge 1 commit into
LuciferYang wants to merge 1 commit into
Conversation
…inear-time `SparkFileUtils.recursiveList` dereferenced `File.listFiles` without a null check (it returns null on an IO error or if the directory is removed during the walk), so it could throw NullPointerException; it also used `Buffer.remove(0)` as a queue, which is O(n) and made the walk O(n^2) on wide trees. Rewrite the traversal to null-guard both `listFiles` calls (skipping and logging an unreadable directory instead of throwing) and to use a queue with O(1) dequeue, keeping it linear. Drop the now-clearly-dead `Option(...).getOrElse(Array.empty)` guard at the `RocksDBFileManager` call site, since `recursiveList` never returns null. Add `SparkFileUtilsSuite`.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What changes were proposed in this pull request?
SparkFileUtils.recursiveListwalks a directory tree by readingFile.listFilesand draining a buffer used as a work queue. Two problems:listFileswithout a null check.File.listFilesreturns null when a directory cannot be read (an IO error, or the directory being removed during the walk), so the walk could throwNullPointerException.Buffer.remove(0), which is O(n); on a wide tree the whole walk is O(n^2).This PR rewrites the traversal to:
listFilescalls, skipping (and logging) a directory that cannot be listed instead of throwing;Queuewith O(1) dequeue, so the walk is linear in the number of entries.It also drops the now-clearly-dead
Option(...).getOrElse(Array.empty)guard at theRocksDBFileManagercall site, sincerecursiveListnever returns null. For a readable tree the result (the set of files and directories, and the traversal order) is unchanged.Why are the changes needed?
recursiveListruns on real paths, such as the RocksDB state-store file manager andLocalSparkCluster. A directory that becomes unreadable mid-walk would crash the walk with an NPE instead of returning what it could, and the O(n^2)remove(0)is needless overhead on directories with many entries.Does this PR introduce any user-facing change?
No. For a readable directory tree the returned entries are identical; the only difference is that a directory which cannot be listed is now skipped with a warning instead of raising an internal
NullPointerException.How was this patch tested?
Added
SparkFileUtilsSuitewith three cases: a nested listing, a root whoselistFilesreturns null, and a subdirectory whoselistFilesreturns null mid-walk (with a spy confirming non-directories are not recursed into). The two null cases throw on the old code and pass with the fix.build/sbt 'common-utils/testOnly *SparkFileUtilsSuite'passes.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 4.8)