[Serve] deployment update behavior different between serve.run() and serve deploy config.yaml
What happened + What you expected to happen
I would like update the configuration of a deployment of an application (for example the num_replicas) without the need of restarting any untouched replicas. I understand from the docs that this is possible by deploying a new config file. However, I would like to do this programmatically through python. My goal is to have a serve application, with a FastAPI endpoint, that performs these kind of deployment config updates, without the need of managing config files. In the reproduction code I would like to get the same behavior as if i was deploying a config file:
- AppManager updates FakeService through AppManager.update_deployment()
- The FakeIngress deployment stays up and running
Actual behavior:
- FakeService is stopped and new replicas are deployed
- FakeIngress is restarted
Is there any solution for this?
Versions / Dependencies
ray-2.45.0
Reproduction script
# serve_app.py
from ray import serve
import logging
from fastapi import FastAPI
logger = logging.getLogger("ray.serve")
fastapi_app = FastAPI()
@serve.deployment()
class FakeService():
def __init__(self, service_id):
self.service_id = service_id
async def __call__(self):
logger.info(f"Hello world I am the service {self.service_id}")
@serve.deployment()
class FakeIngress():
def __init__(self, service_handle):
self.service_handle = service_handle
async def __call__(self):
logger.info(f"Hello world I am the ingress!")
service_handle = FakeService.options(name="Service_A").bind(service_id="Service_A")
ingress_handle = FakeIngress.options(name="Ingress").bind(service_handle=service_handle)
@serve.deployment()
@serve.ingress(fastapi_app)
class AppManager():
@fastapi_app.get('/update/{num_replicas}')
async def update_deployment(self, num_replicas):
"""We are trying to get the same behavior of [serve deploy serve_config.yaml],
where the serve_config.yaml contains the updated configuration of the deployment."""
service_handle = FakeService.options(name="Service_A", num_replicas=num_replicas).bind(service_id="Service_A")
ingress_handle = FakeIngress.options(name="Ingress").bind(service_handle=service_handle)
serve.run(ingress_handle, name='UpdatableService', route_prefix="/service")
app = AppManager.options(name='AppManager').bind()
# N.B.
# The AppManager also deploys the fake app for the first time.
# local cluster:
# serve run serve_app:app
# client.py
async def update(num_replicas):
async with aiohttp.ClientSession() as session:
async with session.get(f"{BASE_URL}/update/{num_replicas}") as response:
if response.status == 200:
data = await response.json()
print("Operation successfull:", json.dumps(data, indent=2))
else:
error = await response.text()
print(f"Operation failed (response status {response.status}): {error}")
if __name__ == "__main__":
import asyncio
while True:
n = input(f"\nInsert num replicas: \nnum_replicas: ")
asyncio.run(update(n))
Issue Severity
Medium: It is a significant difficulty but I can work around it.
You could construct the config and call the deploy applications API. https://github.com/ray-project/ray/blob/c77af9ff70680033cf305e1ef38d21d2f1fcee99/python/ray/serve/scripts.py#L349-L358
Thanks @abrarsheikh for the quick reply.
Is this equivalent to using the PUT REST API (docs)? In this case it would be necessary that the config file includes all the currently running applications with their configurations, not only the one we would like to udpate/deploy. I would like to avoid managing the whole ray cluster applications through the config file.
A bit more context of what I am trying to achieve might help explain myself.
Use case:
The ray cluster will hold an AppManager application and many Service applications. The AppManager is deployed at startup, while the Service applications are deployed at runtime by the AppManager with serve.run().
Each Service application is independent from other Service applications and its deployment or scaling update might happen frequently based on the team needs. Eventually we could have hundreds of different Service applications deployed, each with its own config.
I would like to be able to update the serve configuration of a Service application (the deployment_config of its deployments) without the need to know and manage the configuration of all the other Service applications.
When udpating a serve app through serve.run() shouldn't it check that the application is already existing with the same name and route_prefix and I am just trying to update a config param (such as num_replicas) and perform the same thing as if I passed a config file to the REST API?
It does recognize what application and delpoyments I am addressing with the update, because it stops them gradually and replaces their replicas but, instead of just updating the deployment that changed, it replaces the whole application. I feel like serve.run() and the deploy of a new config file should follow the same behavior here.
I think this is something we should make clear on the serve APIs doc strings.
serve.run: supports two modes of deploying a app
- imperative: which is the way you are doing it, basically deploying an app object
- declarative: which is using configs
One difference between imperative and declarative applications is that, for imperative applications, we are unable to determine the version of code being deployed; this version is used to determine whether the app should be fully redeployed or only the changing deployment configs need to be updated, now because we cannot determine the code version we alway resort to deploying the full app from scratch. In the declarative case, we are able to reliably determine the app version, and hence, we support applying only the changed configs.
Now the question is, can the code version be determined for the imperative case, possibly, but that would require some amount of change from what I can tell. I am also not sure about the motivation of the original author to not implement this.
Unfortunately, I cannot think of a way to overcome the issue you are running into w/o changing the architecture of the app you are building to use configs.
This clarifies the issue here, thanks.
Is it feasible to add this feature to the roadmap?
Something like serve.update() that accepts the same parameters as serve.run() or a config file but with just the target deployment. The important thing is to be able to address the single deployment and not affect other deployments.
Makes sense, I am going to leave this issue open here and move it to our backlog.
@AmirBachir if you have the bandwidth, PR is welcome :)
Hi @abrarsheikh, I'd like to pick up the docstring part you described here. Plan: update the serve.run docstring in python/ray/serve/api.py to explain that it is the imperative path and redeploys the whole application, unlike applying a config, which updates only the changed deployments in place; and add a short section to doc/source/serve/advanced-guides/inplace-updates.md showing a programmatic declarative update with ServeSubmissionClient(address).deploy_applications(...), the same call serve deploy makes in python/ray/serve/scripts.py. I'll make clear that the config passed must list every running application, because others get removed. I'll check that the docs build and the docstring renders in the API reference, with no behaviour change. Could you assign it to me?
@rahulsjha thanks, please do