Skip to content

Commit

Permalink
[core] fix workflow tests (ray-project#44994)
Browse files Browse the repository at this point in the history
Fix workflow tests. The web service it used to compute fibonacci is
completely dead now.

Test:
- CI
- python
python/ray/workflow/examples/comparisons/prefect/compute_fib_workflow.py

Signed-off-by: can <can@anyscale.com>
  • Loading branch information
can-anyscale authored Apr 26, 2024
1 parent 6588ed0 commit 6d6d0a1
Showing 1 changed file with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
import tempfile

import ray
from ray import workflow
import requests
from ray.actor import ActorHandle


@ray.remote
class FibonacciActor:
def __init__(self):
self.cache = {}

def fibonacci(n):
assert n > 0
a, b = 0, 1
for _ in range(n - 1):
a, b = b, a + b
return b
def compute(self, n):
if n not in self.cache:
assert n > 0
a, b = 0, 1
for _ in range(n - 1):
a, b = b, a + b
self.cache[n] = b
return self.cache[n]


@ray.remote
def compute_large_fib(M: int, n: int = 1, fib: int = 1):
try:
next_fib = requests.post(
"https://nemo.api.stdlib.com/fibonacci@0.0.1/", data={"nth": n}
).json()
assert isinstance(next_fib, int)
except AssertionError:
# TODO(suquark): The web service would fail sometimes. This is a workaround.
next_fib = fibonacci(n)
def compute_large_fib(fibonacci_actor: ActorHandle, M: int, n: int = 1, fib: int = 1):
next_fib = ray.get(fibonacci_actor.compute.remote(n))
if next_fib > M:
return fib
else:
return workflow.continuation(compute_large_fib.bind(M, n + 1, next_fib))
return workflow.continuation(
compute_large_fib.bind(fibonacci_actor, M, n + 1, next_fib)
)


if __name__ == "__main__":
assert workflow.run(compute_large_fib.bind(100)) == 89
ray.init(storage=f"file://{tempfile.TemporaryDirectory().name}")
assert workflow.run(compute_large_fib.bind(FibonacciActor.remote(), 100)) == 89

0 comments on commit 6d6d0a1

Please sign in to comment.