-
Notifications
You must be signed in to change notification settings - Fork 6.8k
import custom class safely in controller by using runtime env #56855
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request correctly defers the loading of the custom request router to the replica, which resolves the issue of ModuleNotFoundError
in the controller. The change from serializing/deserializing the class to normalizing it to a path and importing it on demand is a good approach. I've added a couple of comments to update the docstrings which are now outdated due to these changes. Also, it seems the _serialized_request_router_cls
attribute in RequestRouterConfig
is now obsolete and could be removed in a follow-up to improve code clarity.
I think this still wont work given runtime-env for proxy is not from application |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the intention to explicitly only support this for non-ingress deployments then? (unless they make it importable from the image)
We would encourage users to use
|
Signed-off-by: abrar <[email protected]>
773063f
to
e97f7ba
Compare
Signed-off-by: abrar <[email protected]>
Signed-off-by: abrar <[email protected]>
Signed-off-by: abrar <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @abrarsheikh!
Is the intention to explicitly only support this for non-ingress deployments then? (unless they make it importable from the image)
We would encourage users to use
- Custom routers shipped with serve, there wont be that many of these to be honest.
- If they build their own, then we make these nuances clear to them in the documentation.
@abrarsheikh would it be possible to add the docs update to this PR as well?
I was planning to do that in a follow up PR, this once already quite long |
Signed-off-by: abrar <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks @abrarsheikh!
Signed-off-by: abrar <[email protected]>
Signed-off-by: abrar <[email protected]>
Signed-off-by: abrar <[email protected]>
Signed-off-by: abrar <[email protected]>
Signed-off-by: abrar <[email protected]>
…brar-request_router
Signed-off-by: abrar <[email protected]>
Signed-off-by: abrar <[email protected]>
…brar-request_router
…brar-request_router
Signed-off-by: abrar <[email protected]>
@abrarsheikh I'm not very familiar with |
Gotchas and limitations -- based on my limited understanding of cloudpickle and limited by serve specific requirements :D , I am sure there are things that could go wrong.
When users provide a policy, we can fully support it as long as it’s simple, self-contained Python code relying only on the standard library. Once the policy becomes more complex (e.g., depends on other custom modules or packages), they’ll need to ensure those modules are bundled into the Docker image or environment. This PR improves the out-of-the-box experience for most users and newcomers by lowering friction, but it doesn’t completely eliminate environment-dependency issues for complex setups. |
Signed-off-by: abrar <[email protected]>
Signed-off-by: abrar <[email protected]>
currently the
RequestRouterConfig
is initialized from the controller. WhenRequestRouterConfig
is initialized, we try to import the Class and serialize it. This is unsafe because the controller does not have the application runtime-env. See the following stack trace for evidence of howRequestRouterConfig
is initialized in controllerThis PR import the custom class from
build_serve_application
so that the operation is performed in context of runtime envThe same idea applies to the deployment level autoscaling function and application-level autoscaling function.
Another issue happens because
cloudpickle
sees theUniformRequestRouter
class as a top-level symbol in an importable module (custom_request_router
).When
cloudpickle.dumps()
runs, it recognizes that the object can be re-imported viaimport custom_request_router; custom_request_router.UniformRequestRouter
,so instead of embedding the actual code, it just stores that import reference.
That’s why the pickle bytes only contain
b'...custom_request_router...UniformRequestRouter...'
— no source or bytecode.but we want
cloudpickle
to embed the class definition instead of just referencing it, call:This tells
cloudpickle
to serialize everything from that module by value (with code included) rather than by reference.We cannot rely on the reference because custom request router class can be used in serve proxy running outside of runtime env, hence having the entire bytecode make
cloudpickle.loads
possible