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

Named actor #2120

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion python/ray/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
# some functions in the worker.
import ray.actor # noqa: F401
from ray.actor import method # noqa: E402
from ray.actor import actors

# Ray version string. TODO(rkn): This is also defined separately in setup.py.
# Fix this.
Expand All @@ -68,7 +69,7 @@
"remote", "log_event", "log_span", "flush_log", "actor", "method",
"get_gpu_ids", "get_webui_url", "register_custom_serializer",
"SCRIPT_MODE", "WORKER_MODE", "PYTHON_MODE", "SILENT_MODE", "global_state",
"ObjectID", "_config", "__version__"
"ObjectID", "_config", "__version__", "actors"
]

import ctypes # noqa: E402
Expand Down
41 changes: 41 additions & 0 deletions python/ray/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,46 @@

DEFAULT_ACTOR_METHOD_NUM_RETURN_VALS = 1

class ActorMap:
"""
v1.0
A map supporting named actor fetching.
It shall only support get_item and set_item operation for now.

Not sure if we shall provide an repr method.
"""

def __init__(self):
self.worker = ray.worker.get_global_worker()

def __getitem__(self, name):
"""
Attempt to get an actor handle with the name.
The actor handle shall exist in the Redis database
"""
actor_key = self._calculate_key_(name)
pickled_state = self.worker.redis_client.hmget(actor_key, name)
assert len(pickled_state) == 1, \
"Error: Multiple actors under this name."
assert pickled_state[0] is not None, \
"Error: actor with name {} doesn't exist".format(name)
handle = pickle.loads(pickled_state[0])
return handle

def __setitem__(self, name, actor_handle):
"""
Put an actor handle under a name as the key into the Redis database
"""
actor_key = self._calculate_key_(name)
pickled_state = pickle.dumps(actor_handle)
assert type(actor_handle) == ActorHandle, \
"Error: you could only store named-actors."
self.worker.redis_client.hmset(actor_key, {name: pickled_state})

def _calculate_key_(self, name):
return b"Actor:" + str.encode(name)

actors = ActorMap()

def compute_actor_handle_id(actor_handle_id, num_forks):
"""Deterministically compute an actor handle ID.
Expand Down Expand Up @@ -899,6 +939,7 @@ def _deserialization_helper(self, state, ray_forking):

# This is the driver ID of the driver that owns the actor, not
# necessarily the driver that owns this actor handle.

actor_driver_id = ray.ObjectID(state["actor_driver_id"])

self.__init__(
Expand Down
2 changes: 1 addition & 1 deletion python/ray/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@
# specified.
DEFAULT_ACTOR_METHOD_CPUS_SIMPLE_CASE = 1
DEFAULT_ACTOR_CREATION_CPUS_SIMPLE_CASE = 0

# Default resource requirements for actors when some resource requirements are
# specified.
DEFAULT_ACTOR_METHOD_CPUS_SPECIFIED_CASE = 0
DEFAULT_ACTOR_CREATION_CPUS_SPECIFIED_CASE = 1


class RayTaskError(Exception):
"""An object used internally to represent a task that threw an exception.

Expand Down