LuciferYang opened a new pull request, #56590: URL: https://github.com/apache/spark/pull/56590
### What changes were proposed in this pull request? `SparkFileUtils.recursiveList` walks a directory tree by reading `File.listFiles` and draining a buffer used as a work queue. Two problems: - It calls `listFiles` without a null check. `File.listFiles` returns null when a directory cannot be read (an IO error, or the directory being removed during the walk), so the walk could throw `NullPointerException`. - It takes the next directory to visit with `Buffer.remove(0)`, which is O(n); on a wide tree the whole walk is O(n^2). This PR rewrites the traversal to: - null-guard both `listFiles` calls, skipping (and logging) a directory that cannot be listed instead of throwing; - use a `Queue` with 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 the `RocksDBFileManager` call site, since `recursiveList` never 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? `recursiveList` runs on real paths, such as the RocksDB state-store file manager and `LocalSparkCluster`. 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 `SparkFileUtilsSuite` with three cases: a nested listing, a root whose `listFiles` returns null, and a subdirectory whose `listFiles` returns 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) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
