Skip to content

Commit

Permalink
[serve] Remove serve.link(), rename serve.split() -> serve.set_traffi…
Browse files Browse the repository at this point in the history
  • Loading branch information
edoakes authored Apr 21, 2020
1 parent 6799fbb commit 505f3a8
Show file tree
Hide file tree
Showing 22 changed files with 41 additions and 57 deletions.
2 changes: 1 addition & 1 deletion doc/source/rayserve/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ It's trivial to also split traffic, simply specify the endpoint and the backends
serve.create_endpoint("endpoint_identifier_split", "/split", methods=["GET", "POST"])
# splitting traffic 70/30
serve.split("endpoint_identifier_split", {"my_endpoint_backend": 0.7, "my_endpoint_backend_class": 0.3})
serve.set_traffic("endpoint_identifier_split", {"my_endpoint_backend": 0.7, "my_endpoint_backend_class": 0.3})
Batching
Expand Down
4 changes: 2 additions & 2 deletions python/ray/serve/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from ray.serve.backend_config import BackendConfig
from ray.serve.policy import RoutePolicy
from ray.serve.api import (init, create_backend, create_endpoint, link, split,
from ray.serve.api import (init, create_backend, create_endpoint, set_traffic,
get_handle, stat, set_backend_config,
get_backend_config, accept_batch) # noqa: E402

__all__ = [
"init", "create_backend", "create_endpoint", "link", "split", "get_handle",
"init", "create_backend", "create_endpoint", "set_traffic", "get_handle",
"stat", "set_backend_config", "get_backend_config", "BackendConfig",
"RoutePolicy", "accept_batch"
]
24 changes: 4 additions & 20 deletions python/ray/serve/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,28 +246,12 @@ def create_backend(func_or_class,


@_ensure_connected
def link(endpoint_name, backend_tag):
"""Associate a service endpoint with backend tag.
Example:
>>> serve.link("service-name", "backend:v1")
Note:
This is equivalent to
>>> serve.split("service-name", {"backend:v1": 1.0})
"""
split(endpoint_name, {backend_tag: 1.0})


@_ensure_connected
def split(endpoint_name, traffic_policy_dictionary):
def set_traffic(endpoint_name, traffic_policy_dictionary):
"""Associate a service endpoint with traffic policy.
Example:
>>> serve.split("service-name", {
>>> serve.set_traffic("service-name", {
"backend:v1": 0.5,
"backend:v2": 0.5
})
Expand All @@ -278,8 +262,8 @@ def split(endpoint_name, traffic_policy_dictionary):
to their traffic weights. The weights must sum to 1.
"""
ray.get(
master_actor.split_traffic.remote(endpoint_name,
traffic_policy_dictionary))
master_actor.set_traffic.remote(endpoint_name,
traffic_policy_dictionary))


@_ensure_connected
Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/examples/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def noop(_):

serve.create_endpoint("noop", "/noop")
serve.create_backend(noop, "noop")
serve.split("noop", {"noop": 1.0})
serve.set_traffic("noop", {"noop": 1.0})

url = "{}/noop".format(DEFAULT_HTTP_ADDRESS)
while requests.get(url).status_code == 404:
Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/examples/doc/quickstart_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __call__(self, flask_request):

serve.create_endpoint("counter", "/counter")
serve.create_backend(Counter, "counter")
serve.split("counter", {"counter": 1.0})
serve.set_traffic("counter", {"counter": 1.0})

requests.get("http://127.0.0.1:8000/counter").json()
# > {"current_counter": self.count}
2 changes: 1 addition & 1 deletion python/ray/serve/examples/doc/quickstart_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def echo(flask_request):

serve.create_endpoint("hello", "/hello")
serve.create_backend(echo, "hello")
serve.split("hello", {"hello": 1.0})
serve.set_traffic("hello", {"hello": 1.0})

requests.get("http://127.0.0.1:8000/hello").text
# > "hello serve!"
2 changes: 1 addition & 1 deletion python/ray/serve/examples/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def echo(flask_request):

serve.create_endpoint("my_endpoint", "/echo")
serve.create_backend(echo, "echo:v1")
serve.link("my_endpoint", "echo:v1")
serve.set_traffic("my_endpoint", {"echo:v1": 1.0})

while True:
resp = requests.get("http://127.0.0.1:8000/echo").json()
Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/examples/echo_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __call__(self, flask_request, base_number=None):
serve.init(blocking=True)
serve.create_endpoint("magic_counter", "/counter")
serve.create_backend(MagicCounter, "counter:v1", 42) # increment=42
serve.link("magic_counter", "counter:v1")
serve.set_traffic("magic_counter", {"counter:v1": 1.0})

print("Sending ten queries via HTTP")
for i in range(10):
Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/examples/echo_actor_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __call__(self, flask_request_list, base_number=None):
b_config = BackendConfig(max_batch_size=5)
serve.create_backend(
MagicCounter, "counter:v1", 42, backend_config=b_config) # increment=42
serve.link("magic_counter", "counter:v1")
serve.set_traffic("magic_counter", {"counter:v1": 1.0})

print("Sending ten queries via HTTP")
for i in range(10):
Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/examples/echo_batching.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __call__(self, flask_request, base_number=None):
MagicCounter, "counter:v1", 42, backend_config=b_config) # increment=42
print("Backend Config for backend: 'counter:v1'")
print(b_config)
serve.link("magic_counter", "counter:v1")
serve.set_traffic("magic_counter", {"counter:v1": 1.0})

handle = serve.get_handle("magic_counter")
future_list = []
Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/examples/echo_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def echo(_):

serve.create_endpoint("my_endpoint", "/echo")
serve.create_backend(echo, "echo:v1")
serve.link("my_endpoint", "echo:v1")
serve.set_traffic("my_endpoint", {"echo:v1": 1.0})

for _ in range(2):
resp = requests.get("http://127.0.0.1:8000/echo").json()
Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/examples/echo_fixed_packing.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def echo_v2(_):
serve.create_backend(echo_v2, "echo:v2")

# link and split the service to two backends
serve.split("my_endpoint", {"echo:v1": 0.5, "echo:v2": 0.5})
serve.set_traffic("my_endpoint", {"echo:v1": 0.5, "echo:v2": 0.5})

while True:
resp = requests.get("http://127.0.0.1:8000/echo").json()
Expand Down
4 changes: 2 additions & 2 deletions python/ray/serve/examples/echo_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def echo_v1(flask_request, response="hello from python!"):

# We can link an endpoint to a backend, the means all the traffic
# goes to my_endpoint will now goes to echo:v1 backend.
serve.link("my_endpoint", "echo:v1")
serve.set_traffic("my_endpoint", {"echo:v1": 1.0})

print(requests.get("http://127.0.0.1:8000/echo", timeout=0.5).text)
# The service will be reachable from http
Expand All @@ -51,7 +51,7 @@ def echo_v2(flask_request):
backend_config_v2 = serve.get_backend_config("echo:v2")

# The two backend will now split the traffic 50%-50%.
serve.split("my_endpoint", {"echo:v1": 0.5, "echo:v2": 0.5})
serve.set_traffic("my_endpoint", {"echo:v1": 0.5, "echo:v2": 0.5})

# Observe requests are now split between two backends.
for _ in range(10):
Expand Down
8 changes: 4 additions & 4 deletions python/ray/serve/examples/echo_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def echo_v1(_, response="hello from python!"):

serve.create_endpoint("echo_v1", "/echo_v1")
serve.create_backend(echo_v1, "echo_v1")
serve.split("echo_v1", {"echo_v1": 1.0})
serve.set_traffic("echo_v1", {"echo_v1": 1.0})


def echo_v2(_, relay=""):
Expand All @@ -27,7 +27,7 @@ def echo_v2(_, relay=""):

serve.create_endpoint("echo_v2", "/echo_v2")
serve.create_backend(echo_v2, "echo_v2")
serve.split("echo_v2", {"echo_v2": 1.0})
serve.set_traffic("echo_v2", {"echo_v2": 1.0})


def echo_v3(_, relay=""):
Expand All @@ -36,7 +36,7 @@ def echo_v3(_, relay=""):

serve.create_endpoint("echo_v3", "/echo_v3")
serve.create_backend(echo_v3, "echo_v3")
serve.split("echo_v3", {"echo_v3": 1.0})
serve.set_traffic("echo_v3", {"echo_v3": 1.0})


def echo_v4(_, relay1="", relay2=""):
Expand All @@ -45,7 +45,7 @@ def echo_v4(_, relay1="", relay2=""):

serve.create_endpoint("echo_v4", "/echo_v4")
serve.create_backend(echo_v4, "echo_v4")
serve.split("echo_v4", {"echo_v4": 1.0})
serve.set_traffic("echo_v4", {"echo_v4": 1.0})
"""
The pipeline created is as follows -
"my_endpoint1"
Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/examples/echo_round_robin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def echo_v2(_):
serve.create_backend(echo_v2, "echo:v2")

# link and split the service to two backends
serve.split("my_endpoint", {"echo:v1": 0.5, "echo:v2": 0.5})
serve.set_traffic("my_endpoint", {"echo:v1": 0.5, "echo:v2": 0.5})

while True:
resp = requests.get("http://127.0.0.1:8000/echo").json()
Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/examples/echo_slo_reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def echo_v1(flask_request, response="hello from python!"):


serve.create_backend(echo_v1, "echo:v1")
serve.link("my_endpoint", "echo:v1")
serve.set_traffic("my_endpoint", {"echo:v1": 1.0})

# wait for routing table to get populated
time.sleep(2)
Expand Down
4 changes: 2 additions & 2 deletions python/ray/serve/examples/echo_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def echo_v2(_):

serve.create_endpoint("my_endpoint", "/echo")
serve.create_backend(echo_v1, "echo:v1")
serve.link("my_endpoint", "echo:v1")
serve.set_traffic("my_endpoint", {"echo:v1": 1.0})

for _ in range(3):
resp = requests.get("http://127.0.0.1:8000/echo").json()
Expand All @@ -32,7 +32,7 @@ def echo_v2(_):
time.sleep(2)

serve.create_backend(echo_v2, "echo:v2")
serve.split("my_endpoint", {"echo:v1": 0.5, "echo:v2": 0.5})
serve.set_traffic("my_endpoint", {"echo:v1": 0.5, "echo:v2": 0.5})
while True:
resp = requests.get("http://127.0.0.1:8000/echo").json()
print(pformat_color_json(resp))
Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def get_all_endpoints(self):
return expand(
self.route_table.list_service(include_headless=True).values())

async def split_traffic(self, endpoint_name, traffic_policy_dictionary):
async def set_traffic(self, endpoint_name, traffic_policy_dictionary):
assert endpoint_name in expand(
self.route_table.list_service(include_headless=True).values())

Expand Down
10 changes: 5 additions & 5 deletions python/ray/serve/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def function(flask_request):
return {"method": flask_request.method}

serve.create_backend(function, "echo:v1")
serve.link("endpoint", "echo:v1")
serve.set_traffic("endpoint", {"echo:v1": 1.0})

resp = requests.get("http://127.0.0.1:8000/api").json()["method"]
assert resp == "GET"
Expand All @@ -47,7 +47,7 @@ def func(_, i=1):
return 1

serve.create_backend(func, "backend:1")
serve.link("noroute-endpoint", "backend:1")
serve.set_traffic("noroute-endpoint", {"backend:1": 1.0})
service_handle = serve.get_handle("noroute-endpoint")
result = ray.get(service_handle.remote(i=1))
assert result == 1
Expand All @@ -71,7 +71,7 @@ def __call__(self, _):

b_config = BackendConfig(num_replicas=2)
serve.create_backend(Counter, "counter:v1", backend_config=b_config)
serve.link("counter", "counter:v1")
serve.set_traffic("counter", {"counter:v1": 1.0})

counter_result = []
for _ in range(10):
Expand Down Expand Up @@ -116,7 +116,7 @@ def __call__(self, flask_request, temp=None):
b_config = BackendConfig(max_batch_size=5)
serve.create_backend(
BatchingExample, "counter:v11", backend_config=b_config)
serve.link("counter1", "counter:v11")
serve.set_traffic("counter1", {"counter:v11": 1.0})

future_list = []
handle = serve.get_handle("counter1")
Expand Down Expand Up @@ -146,7 +146,7 @@ def __call__(self, flask_request, temp=None):
b_config = BackendConfig(max_batch_size=5)
serve.create_backend(
NoListReturned, "exception:v1", backend_config=b_config)
serve.link("exception-test", "exception:v1")
serve.set_traffic("exception-test", {"exception:v1": 1.0})

handle = serve.get_handle("exception-test")
with pytest.raises(ray.exceptions.RayTaskError):
Expand Down
12 changes: 6 additions & 6 deletions python/ray/serve/tests/test_failure.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def function():
return "hello1"

serve.create_backend(function, "proxy_failure:v1")
serve.link("proxy_failure", "proxy_failure:v1")
serve.set_traffic("proxy_failure", {"proxy_failure:v1": 1.0})

assert request_with_retries("/proxy_failure", timeout=0.1).text == "hello1"

Expand All @@ -47,7 +47,7 @@ def function():
return "hello2"

serve.create_backend(function, "proxy_failure:v2")
serve.link("proxy_failure", "proxy_failure:v2")
serve.set_traffic("proxy_failure", {"proxy_failure:v2": 1.0})

for _ in range(10):
response = request_with_retries("/proxy_failure", timeout=30)
Expand All @@ -67,7 +67,7 @@ def function():
return "hello1"

serve.create_backend(function, "router_failure:v1")
serve.link("router_failure", "router_failure:v1")
serve.set_traffic("router_failure", {"router_failure:v1": 1.0})

assert request_with_retries("/router_failure", timeout=5).text == "hello1"

Expand All @@ -81,7 +81,7 @@ def function():
return "hello2"

serve.create_backend(function, "router_failure:v2")
serve.link("router_failure", "router_failure:v2")
serve.set_traffic("router_failure", {"router_failure:v2": 1.0})

for _ in range(10):
response = request_with_retries("/router_failure", timeout=30)
Expand All @@ -106,7 +106,7 @@ def __call__(self):
return os.getpid()

serve.create_backend(Worker1, "worker_failure:v1")
serve.link("worker_failure", "worker_failure:v1")
serve.set_traffic("worker_failure", {"worker_failure:v1": 1.0})

# Get the PID of the worker.
old_pid = request_with_retries("/worker_failure", timeout=0.1).text
Expand Down Expand Up @@ -164,7 +164,7 @@ def __call__(self):
backend_config = serve.get_backend_config("replica_failure")
backend_config.num_replicas = 2
serve.set_backend_config("replica_failure", backend_config)
serve.link("replica_failure", "replica_failure")
serve.set_traffic("replica_failure", {"replica_failure": 1.0})

# Wait until both replicas have been started.
responses = set()
Expand Down
4 changes: 2 additions & 2 deletions python/ray/serve/tests/test_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def __call__(self):

serve.create_endpoint("endpoint1", "/endpoint1", methods=["GET", "POST"])
serve.create_backend(Endpoint1, "endpoint1:v0")
serve.link("endpoint1", "endpoint1:v0")
serve.set_traffic("endpoint1", {"endpoint1:v0": 1.0})

serve.create_endpoint("endpoint2", "/endpoint2", methods=["GET", "POST"])
serve.create_backend(Endpoint2, "endpoint2:v0")
serve.link("endpoint2", "endpoint2:v0")
serve.set_traffic("endpoint2", {"endpoint2:v0": 1.0})

assert requests.get("http://127.0.0.1:8000/endpoint2").text == "hello"
2 changes: 1 addition & 1 deletion python/ray/serve/tests/test_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def driver(flask_request):
serve.create_endpoint("driver", "/driver")
serve.create_backend(driver, "driver")
serve.split("driver", {{"driver": 1.0}})
serve.set_traffic("driver", {{"driver": 1.0}})
""".format(ray.worker._global_node._redis_address)

with tempfile.NamedTemporaryFile(mode="w", delete=False) as f:
Expand Down

0 comments on commit 505f3a8

Please sign in to comment.