Skip to content

Allow limiting the number of outstanding prefetch requests - #2058

Merged
bitprophet merged 4 commits into
paramiko:mainfrom
kschoelhorn:limit-outstanding-prefetch-requests
Jun 9, 2023
Merged

bitprophet merged 4 commits into
paramiko:mainfrom
kschoelhorn:limit-outstanding-prefetch-requests

Conversation

@kschoelhorn

Copy link
Copy Markdown
Contributor

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 is listdir_iter, which already has the read_aheads parameter, 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.

@bskinn

bskinn commented May 17, 2022

Copy link
Copy Markdown
Contributor

Looks like this is specific to SFTP?

@kschoelhorn

Copy link
Copy Markdown
Contributor Author

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.

@bskinn bskinn added the SFTP label May 25, 2022
Comment thread paramiko/sftp_file.py Outdated
if max_outstanding_requests is not None:
while True:
with self._prefetch_lock:
if len(self._prefetch_extents) < max_outstanding_requests:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 bskinn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a piece of implementation missing

Comment thread paramiko/sftp_file.py Resolved
@bskinn

bskinn commented May 25, 2022

Copy link
Copy Markdown
Contributor

Will need bitprophet's input on that parameter name, I think -- paramiko tends toward more concise names.

@bskinn

bskinn commented May 25, 2022

Copy link
Copy Markdown
Contributor

@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?

@bskinn

bskinn commented May 25, 2022

Copy link
Copy Markdown
Contributor

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.

@kschoelhorn

Copy link
Copy Markdown
Contributor Author

@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?

I'll think about some tests. I think we should be able to mock SFTPClient and then simulate a large file transfer.

@bitprophet bitprophet added this to the p2 milestone Jun 3, 2022
@bwinston-sdp

Copy link
Copy Markdown
Contributor

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.

@bskinn

bskinn commented Apr 19, 2023

Copy link
Copy Markdown
Contributor

Is this still a possible PR pending tests?

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!

@bwinston-sdp

Copy link
Copy Markdown
Contributor

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?).

@kschoelhorn

Copy link
Copy Markdown
Contributor Author

@bwinston-sdp: Thank you very much for the test, I'll check and merge to this PR later!

@kschoelhorn
kschoelhorn force-pushed the limit-outstanding-prefetch-requests branch 2 times, most recently from 8547abd to 2fcb575 Compare April 20, 2023 13:06
@kschoelhorn

Copy link
Copy Markdown
Contributor Author

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).

@kschoelhorn
kschoelhorn marked this pull request as ready for review April 20, 2023 13:11
@bwinston-sdp

Copy link
Copy Markdown
Contributor

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 max_outstanding_requests gets passed through readv as well, not sure if we need another test for that.

@kschoelhorn

Copy link
Copy Markdown
Contributor Author

One other thing I thought of -- I know the max_outstanding_requests gets passed through readv as well, not sure if we need another test for that.

Both use the same prefetch code internally, so I think one test is enough.

Comment thread paramiko/sftp_client.py Outdated
: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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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! 🙂

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kschoelhorn Yeah, that was what prompted my suggestion: to my mind, 'outstanding' could be ambiguous, meaning either:

  1. The number of requests currently in-flight (the intended meaning)
  2. 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'.

@bskinn

bskinn commented May 14, 2023

Copy link
Copy Markdown
Contributor

Also--please add a CHANGELOG entry to /sites/www/changelog.rst -- thanks!

@bwinston-sdp

Copy link
Copy Markdown
Contributor

I updated these and pushed to the PR on @kschoelhorn's branch.

@bskinn

bskinn commented May 23, 2023

Copy link
Copy Markdown
Contributor

👍 - 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.
@kschoelhorn
kschoelhorn force-pushed the limit-outstanding-prefetch-requests branch 2 times, most recently from 097c176 to 022e82f Compare June 2, 2023 10:20
bwinston-sdp and others added 3 commits June 2, 2023 12:30
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.
@kschoelhorn
kschoelhorn force-pushed the limit-outstanding-prefetch-requests branch from 022e82f to 6006598 Compare June 2, 2023 10:30
@kschoelhorn

Copy link
Copy Markdown
Contributor Author

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. 🙂

@bskinn

bskinn commented Jun 2, 2023

Copy link
Copy Markdown
Contributor

I guess you can remove the "Needs changelog/docs" flag now. 🙂

Done!

@bitprophet

Copy link
Copy Markdown
Member

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.

bitprophet added a commit that referenced this pull request Jun 9, 2023
@bitprophet
bitprophet merged commit 6006598 into paramiko:main Jun 9, 2023
@TallGibbs

Copy link
Copy Markdown

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"

@bwinston-sdp

Copy link
Copy Markdown
Contributor

@TallGibbs

Copy link
Copy Markdown

@TallGibbs this feature is included in 3.3, with an additional parameter in the get() function: https://docs.paramiko.org/en/latest/api/sftp.html?highlight=prefetch#paramiko.sftp_client.SFTPClient.get

Oh awesome! Looks like I need to update then....I'm currently using 2.8.1

Thank you for verifying

@TallGibbs

Copy link
Copy Markdown

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants