selectedhan opened a new issue, #13769:
URL: https://github.com/apache/seatunnel/issues/13769

   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/seatunnel/issues?q=is%3Aissue+label%3A%22bug%22)
 and found no similar issues. Related but different: #9953 covers 
`getWorkingDirectory()` and filter ordering during listing, and #11306 / #11377 
cover the submit-time listing. This one is about the per-file `open()` on the 
read path.
   
   ### What happened
   
   `SFTPFileSystem.open()` decides whether the path is a directory by calling 
the private `getFileStatus(ChannelSftp, Path)`. That method does not stat the 
file. It lists the **entire parent directory** with `ChannelSftp.ls()` and 
scans the entries for the file name.
   
   The source calls `open()` once for every file it reads, so reading N files 
from one directory lists that directory N times, even though the directory was 
already listed once during discovery. The cost per file therefore grows with 
the size of the directory.
   
   The same code exists in Hadoop's own `SFTPFileSystem` (`getFileStatus` lists 
the parent and compares names), and it has been in this connector since it was 
added in #3006, so this is not a recent regression.
   
   I measured the cost of one `open()` as the number of entries in the parent 
directory grows (SFTP server in the same cluster as the client, mean over 100 
files, baseline and patched builds run alternately, 2 runs each):
   
   | Entries per directory | `open()` + read, current | `open()` + read, with 
`lstat` | Time reduced |
   |---|---|---|---|
   | 100 | 11.5 - 13.0 ms | 6.3 - 7.0 ms | ~46% |
   | 1,000 | 28.4 - 28.9 ms | 7.5 - 7.7 ms | ~73% |
   | 5,000 | 87.7 - 102.2 ms | 7.4 - 9.4 ms | ~91% |
   
   "Time reduced" is computed from the mean of the two runs.
   
   Per-operation cost, same server, 1,000 entries: `lstat` ~ 1.0 ms, `realpath` 
~ 1.0 ms, `get` + read ~ 5 ms, parent `ls` ~ 20 ms.
   
   The server was in the same network as the client, so round-trip latency was 
low. A remote server adds latency to every round trip that `ls` makes (one per 
batch of entries), so I expect the difference to be larger there, but I have 
not measured it.
   
   ### Proposal
   
   In `SFTPFileSystem.open()` only, instead of listing the parent with the 
existing `ChannelSftp.ls()`, ask the server about the single path with 
`ChannelSftp.lstat()`:
   
   - Quote the path first (`quote()`), because JSch treats `*` and `?` in 
`lstat` as globs and fails with "not unique" (the same problem #8500 fixed for 
`get`).
   - Keep the symbolic-link handling by passing the attributes to the existing 
status builder, which resolves links with `realpath`.
   - Keep reporting any SFTP failure as `FileNotFoundException`, as today.
   - Leave every other caller of `getFileStatus` unchanged.
   
   I compared the current and the patched build on the same server with 22 
cases (special characters, relative/absolute/chained symlinks, a link to a 
directory, a broken link, missing files, directories with mode 000/311/644, a 
file with mode 000):
   
   - 21 cases behave identically.
   - One case differs: a directory that has the execute bit but not the read 
bit (`--x`). Today `open()` fails with `FileNotFoundException` because the 
listing is denied, while with `lstat` the file can be opened. I will call this 
out in the PR so a reviewer can decide whether it is acceptable. (On POSIX, 
execute permission on a directory is enough to access a file whose name you 
already know, even without read permission to list the directory. So the old 
failure looks like an over-strict side effect of listing the parent, which this 
change no longer requires. I am happy to keep the old behavior if reviewers 
prefer.)
   
   I have not changed the error mapping (for example reporting a permission 
failure as "permission denied" instead of "not found"). That would change 
behavior and I would rather discuss it separately.
   
   ### SeaTunnel Version
   
   `dev` (3.1.1-SNAPSHOT). The same `ls`-based `getFileStatus` and the same 
`open()` call exist in 3.0.0 and 3.1.0.
   
   ### Are you willing to submit PR?
   
   - [x] Yes. I have a patch with unit tests and will link the PR here.
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


-- 
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]

Reply via email to