Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use requests.Session object to reuse connections in PolicyClient #33035

Merged

Conversation

MattiasDC
Copy link
Contributor

@MattiasDC MattiasDC commented Mar 4, 2023

Why are these changes needed?

Uses connections/resources more efficiently in PolicyClient. By using a requests.Session object, instead of using requests directly, connections are reused.

Related issue number

Closes #30136

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • [] I've included any doc changes needed for https://docs.ray.io/en/master/.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests

@stale
Copy link

stale bot commented Apr 6, 2023

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

  • If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@stale stale bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Apr 6, 2023
@MattiasDC
Copy link
Contributor Author

@sven1977 this has been marked as stale, could someone do a review?

@stale stale bot removed the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Apr 6, 2023
@stale
Copy link

stale bot commented May 8, 2023

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

  • If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@stale stale bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label May 8, 2023
@MattiasDC
Copy link
Contributor Author

@sven1977 this has been marked as stale, could someone do a review?

@ArturNiederfahrenhorst
Copy link
Contributor

@sven1977 @kouroshHakha According to https://requests.readthedocs.io/en/latest/user/advanced/#session-objects, sessions can bring significant speedups.

I've timed this a little and from a quick look I could not observe a speedup.
Nevertheless, since the code definitely works, I think we should merge this.

@stale stale bot removed the stale The issue is stale. It will be closed within 7 days unless there are further conversation label May 11, 2023
@kouroshHakha
Copy link
Contributor

I wonder if keeping the session open can cause any problems / leaks. Usually it has to be closed. Nevertheless the PR makes sense. @MattiasDC if you were to do session.close() where should that happen?

@MattiasDC
Copy link
Contributor Author

Hi @kouroshHakha,

I agree that it's cleaner to close the session. Typically a session is automatically closed when you use it as a context manager. There is an example in the requests doc.
Using it here as a context manager would imply that either the session object has to be passed as an argument to the constructor of the PolicyClient, and the context manager is created 'outside' of the PolicyClient, or we make PolicyClient a context manager itself.

Alternatively we simply call self.session.close() in the destructor of the PolicyClient. This seems to me the approach with the least impact on the user, while still adhering to closing sessions. If you're ok with this approach, I can update the PR.

@kouroshHakha
Copy link
Contributor

@MattiasDC yep that's what I thought about too. How about we make an optional arg for policyClient called session that allows users to pass in their pre constructed session from outside. In this case, the caller will be responsible for closing the session. However if the user does not provide the session we resort to the old method?

@MattiasDC MattiasDC force-pushed the 30136-policy-client-session branch from 9de94d6 to e3586db Compare May 11, 2023 15:31
@MattiasDC
Copy link
Contributor Author

@kouroshHakha I agree with your suggestion and pushed it

@MattiasDC MattiasDC force-pushed the 30136-policy-client-session branch from e3586db to 0df7eee Compare May 11, 2023 15:34
@ArturNiederfahrenhorst
Copy link
Contributor

Cool stuff! :)

  1. Since the tests are running on nightly wheels, they failed because we are migrating major stuff there (@MattiasDC please push again tomorrow, CI should run on updated wheels then)
  2. One last minor request: Can you document the reasoning in the code? Basically, include in the docstring that using a session can lead to speedups and that it must be closed by the creator of the session itself.

Thanks for this PR!

@MattiasDC MattiasDC force-pushed the 30136-policy-client-session branch from 0df7eee to 596eab0 Compare May 20, 2023 20:13
@MattiasDC
Copy link
Contributor Author

@ArturNiederfahrenhorst can you have a look at the ones still failing and let me know if they are caused by my PR?

@kouroshHakha
Copy link
Contributor

All RLlib tests are passing. Merging now ...

@kouroshHakha kouroshHakha merged commit 323d9d5 into ray-project:master May 21, 2023
2 checks passed
@MattiasDC MattiasDC deleted the 30136-policy-client-session branch May 22, 2023 08:00
scv119 pushed a commit to scv119/ray that referenced this pull request Jun 16, 2023
arvind-chandra pushed a commit to lmco/ray that referenced this pull request Aug 31, 2023
…urce more efficiently in PolicyClient (ray-project#33035)

Signed-off-by: mattias <[email protected]>
Signed-off-by: e428265 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RLlib] PolicyClient does not reuse http sessions
3 participants