Repository navigation
Allow limiting the number of outstanding prefetch requests - #2058
Conversation
|
Looks like this is specific to SFTP? |
|
Yes, the prefetch mechanism is sftp specific, but I could imagine that the underlying issue (sending too many requests) might also affect some ssh servers. But in my case, Serv-U only supports SFTP, but no interactive SSH access. |
| if max_outstanding_requests is not None: | ||
| while True: | ||
| with self._prefetch_lock: | ||
| if len(self._prefetch_extents) < max_outstanding_requests: |
There was a problem hiding this comment.
Checking my understanding of the implementation here -- does each call to self.sftp._async_request() cause self._prefetch_extents to increment by one, and then once each chunk is received, ._prefetch_extents is decremented?
And, thus, this new code causes this queuing within self._prefetch_thread() to idle whenever we hit our max-requests cap?
There was a problem hiding this comment.
The new async request number returned by _async_request is added to _prefetch_extents by the code below (hidden by the diff view on github by default:
with self._prefetch_lock:
self._prefetch_extents[num] = (offset, length)
The request number is then removed again in _async_response.
So yes, the background thread just idles here if the limit has been reached.
bskinn
left a comment
There was a problem hiding this comment.
Might be a piece of implementation missing
|
Will need bitprophet's input on that parameter name, I think -- paramiko tends toward more concise names. |
|
@kschoelhorn Are there any tests that could be added for this, that would be less onerous than mocking a server and monitoring the length of the request queue? |
|
Per #1587 (comment) and the SO post linked therein, this feature might mitigate or solve #1587? The workaround posted in this Gist, linked from #1587 (comment), suggests it likely would help. The implementation in this PR adds a parameter to the public API, versus the private class/member implementation in the Gist, so this PR is likely preferred. |
I'll think about some tests. I think we should be able to mock |
|
Is this still a possible PR pending tests? I've run into a similar situation as mentioned in the issues/Gist linked, and this works beautifully. I'm happy to rebase/write some tests if @kschoelhorn is MIA and/or wants to pass it off. |
Yes, it definitely is -- it's not at the top of the priority list for paramiko at the moment, though, so it hasn't seen any specific action. Up to you and @kschoelhorn to negotiate the test work. Whoever would like to contribute them is welcome! |
|
Not sure if it was the "right way" to do it, but I created a PR to @kschoelhorn's branch, rebased on main, with lint fixes and an added test. LMK if I should do it another way (create a new PR directly to paramiko with changes, reference/credit this one?). |
|
@bwinston-sdp: Thank you very much for the test, I'll check and merge to this PR later! |
8547abd to
2fcb575
Compare
|
I added @bwinston-sdp's test and modified it slightly to make it more stable (as the background prefetch thread and the test are not synchronized and I saw sporadic failures). |
|
Thanks @kschoelhorn!! I figured the test might be flaky because of the background thread, but wasn't sure exactly how to ensure stability (I've not done much multi-threaded coding). One other thing I thought of -- I know the |
Both use the same prefetch code internally, so I think one test is enough. |
| :param bool prefetch: | ||
| controls whether prefetching is performed (default: True) | ||
| :param int max_outstanding_prefetch_requests: | ||
| Limit the number of outstanding requests instead of sending all |
There was a problem hiding this comment.
Is this parameter defining the maximum outstanding requests, or the max concurrent requests? It seems more the latter to me.
Separately, this parameter is an int -- its description should explicitly say how that value is used. It's implicit that the value is the max number of concurrent requests, but it should be made explicit.
There was a problem hiding this comment.
I meant outstanding in the sense of "not yet done": the requests were sent, but we have not yet received an answer. But concurrent is also fine! 🙂
There was a problem hiding this comment.
@kschoelhorn Yeah, that was what prompted my suggestion: to my mind, 'outstanding' could be ambiguous, meaning either:
- The number of requests currently in-flight (the intended meaning)
- The total number of requests yet to be completed, including both in-flight and not-yet-sent requests (not the intended meaning)
Meaning (2) doesn't make a whole lot of sense, practically, but if that's the sense of the word that someone reaches first, then the name could be confusing. Thus, my recommendation for 'concurrent'.
|
Also--please add a CHANGELOG entry to |
|
I updated these and pushed to the PR on @kschoelhorn's branch. |
|
👍 - Linking directly to kschoelhorn#1, for reference. |
Currently prefetch will send all requests for all chunks for the file in one shot. With the default chunk size of 32k, this can result in many thousand outstanding requests for large files. Some servers like Serv-U 15.2.3.742 seem to be dropping requests after a certain number, which results in the file download hanging indefinitely (or until the server closes the connection). Fix this issue by letting the user specify a limit for the number of concurrent requests. This is similar to openssh's sftp, which limits the number of concurrent requests to 64 by default.
097c176 to
022e82f
Compare
The prefetch requests are added in a background process, so the might not be ready when the test wants to count them. Fix it by introducing a wait_for untility function that waits for a assert to pass for a specified timeout, to give the background thread the chance to start and add the prefetch requests. Also lock the prefetch lock before trying to access the extents to prevent a data race.
022e82f to
6006598
Compare
|
Thanks again to @bwinston-sdp! I merged your changes, rebased to latest master and fixed a few lints. @bskinn: I guess you can remove the "Needs changelog/docs" flag now. 🙂 |
Done! |
|
Thanks for this! Looks good, my only complaint is the copy-pasta of the docstring param blocks, but I'll just fix that myself as I merge. |
|
Sorry if this seems really silly, but all this testing and (proposed) change seems like exactly what I need to fix my current issue in some SFTP code. Did this change get implemented? Is there documentation for how to specify the "max_outstanding_prefetch_requests" variable? I am trying to transfer 1GB file through SFTP and the host is closing the connection with the "EOFError" |
|
@TallGibbs this feature is included in 3.3, with an additional parameter in the |
Oh awesome! Looks like I need to update then....I'm currently using 2.8.1 Thank you for verifying |
|
@bwinston-sdp I just updated to 3.4.0 and modified my code to include get(file, local_path, max_concurrent_prefetch_requests=32) and it worked like a charm! Super simple implementation (WAY easier than all the solutions on S.O.). This was a file just under 1 GB, for anyone else encountering these issues. Thanks to everyone involved for solving this SFTP problem |
Currently prefetch will send all requests for all chunks for the file in one shot. With the default chunk size of 32k, this can result in many thousand outstanding requests for large files. Some servers like Serv-U 15.2.3.742 seem to be dropping requests after a certain number, which results in the file download hanging indefinitely (or until the server closes the connection).
Fix this issue by letting the user specify a limit for the number of outstanding requests. This is similar to openssh's sftp, which limits the number of outstanding requests to 64 by default.
Note that this is just a quick initial implementation to fix the problem I had while using paramiko with Serv-U. This could also be a setting of the SFTPClient, that limits any
_async_requests, not just those from prefetching, though it seems the only other user islistdir_iter, which already has theread_aheadsparameter, which also limits the number of requests.See also #1779 where this was previously proposed and #151, which mixes different issues, but also contains descriptions of get calls hanging, that are fixed by disabling prefetching and are probably the same issue.