[minor] Remove get_global_worker(), RuntimeContext (#7638)

This commit is contained in:
Edward Oakes
2020-03-20 15:45:29 -05:00
committed by GitHub
parent 7ebc6783e4
commit 58dc70f90e
12 changed files with 33 additions and 69 deletions
-1
View File
@@ -96,7 +96,6 @@ import ray.projects # noqa: E402
# some functions in the worker.
import ray.actor # noqa: F401
from ray.actor import method # noqa: E402
from ray.runtime_context import _get_runtime_context # noqa: E402
from ray.cross_language import java_function, java_actor_class # noqa: E402
from ray import util # noqa: E402
+1 -1
View File
@@ -953,7 +953,7 @@ cdef class CoreWorker:
CObjectID c_outer_object_id = (outer_object_id.native() if
outer_object_id else
CObjectID.Nil())
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
worker.check_connected()
manager = worker.function_actor_manager
c_actor_id = self.core_worker.get().DeserializeAndRegisterActorHandle(
+6 -8
View File
@@ -463,7 +463,7 @@ class ActorClass:
if max_concurrency < 1:
raise ValueError("max_concurrency must be >= 1")
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
if worker.mode is None:
raise RuntimeError("Actors cannot be created before ray.init() "
"has been called.")
@@ -655,7 +655,7 @@ class ActorHandle:
def __del__(self):
# Mark that this actor handle has gone out of scope. Once all actor
# handles are out of scope, the actor will exit.
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
if worker.connected and hasattr(worker, "core_worker"):
worker.core_worker.remove_actor_handle_reference(
self._ray_actor_id)
@@ -682,7 +682,7 @@ class ActorHandle:
object_ids: A list of object IDs returned by the remote actor
method.
"""
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
args = args or []
kwargs = kwargs or {}
@@ -776,7 +776,7 @@ class ActorHandle:
Returns:
A dictionary of the information needed to reconstruct the object.
"""
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
worker.check_connected()
if hasattr(worker, "core_worker"):
@@ -809,7 +809,7 @@ class ActorHandle:
the actor handle.
"""
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
worker.check_connected()
if hasattr(worker, "core_worker"):
@@ -859,7 +859,7 @@ def modify_class(cls):
__ray_actor_class__ = cls # The original actor class
def __ray_terminate__(self):
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
if worker.mode != ray.LOCAL_MODE:
ray.actor.exit_actor()
@@ -937,8 +937,6 @@ def exit_actor():
raise TypeError("exit_actor called on a non-actor worker.")
ray.worker.global_worker.make_actor = make_actor
CheckpointContext = namedtuple(
"CheckpointContext",
[
+5
View File
@@ -11,6 +11,11 @@ class RayError(Exception):
pass
class RayConnectionError(RayError):
"""Raised when ray is not yet connected but needs to be."""
pass
class RayTaskError(RayError):
"""Indicates that a task threw an exception during execution.
+3 -3
View File
@@ -4,14 +4,14 @@ _local = {} # dict for local mode
def _internal_kv_initialized():
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
return hasattr(worker, "mode") and worker.mode is not None
def _internal_kv_get(key):
"""Fetch the value of a binary key."""
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
if worker.mode == ray.worker.LOCAL_MODE:
return _local.get(key)
@@ -27,7 +27,7 @@ def _internal_kv_put(key, value, overwrite=False):
already_exists (bool): whether the value already exists.
"""
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
if worker.mode == ray.worker.LOCAL_MODE:
exists = key in _local
if not exists or overwrite:
+2 -2
View File
@@ -7,7 +7,7 @@ __all__ = ["free", "global_gc"]
def global_gc():
"""Trigger gc.collect() on all workers in the cluster."""
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
worker.core_worker.global_gc()
@@ -54,7 +54,7 @@ def free(object_ids, local_only=False, delete_creating_tasks=False):
delete_creating_tasks (bool): Whether also delete the object creating
tasks.
"""
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
if isinstance(object_ids, ray.ObjectID):
object_ids = [object_ids]
+1 -1
View File
@@ -150,7 +150,7 @@ class RemoteFunction:
resources=None,
max_retries=None):
"""Submit the remote function for execution."""
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
worker.check_connected()
# If this function was not exported in this session and job, we need to
-30
View File
@@ -1,30 +0,0 @@
import ray.worker
class RuntimeContext:
"""A class used for getting runtime context."""
def __init__(self, worker=None):
self.worker = worker
@property
def current_driver_id(self):
"""Get current driver ID for this worker or driver.
Returns:
If called by a driver, this returns the driver ID. If called in
a task, return the driver ID of the associated driver.
"""
assert self.worker is not None
return self.worker.current_job_id
_runtime_context = None
def _get_runtime_context():
global _runtime_context
if _runtime_context is None:
_runtime_context = RuntimeContext(ray.worker.get_global_worker())
return _runtime_context
+3 -3
View File
@@ -164,7 +164,7 @@ class SerializationContext:
# TODO(swang): Remove this check. Otherwise, we will not be able to
# handle serialized plasma IDs correctly.
if obj.is_direct_call_type():
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
worker.check_connected()
obj, owner_id, owner_address = (
worker.core_worker.serialize_and_promote_object_id(obj))
@@ -184,7 +184,7 @@ class SerializationContext:
# somewhere, which causes an error.
context = ray.worker.global_worker.get_serialization_context()
if owner_id:
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
worker.check_connected()
# UniqueIDs are serialized as
# (class name, (unique bytes,)).
@@ -250,7 +250,7 @@ class SerializationContext:
# cloudpickle directly or captured in a remote function/actor),
# then pin the object for the lifetime of this worker by adding
# a local reference that won't ever be removed.
ray.worker.get_global_worker().core_worker.add_object_id_reference(
ray.worker.global_worker.core_worker.add_object_id_reference(
object_id)
def _deserialize_pickle5_data(self, data):
+2 -2
View File
@@ -299,12 +299,12 @@ def test_specific_job_id():
ray.init(num_cpus=1, job_id=dummy_driver_id)
# in driver
assert dummy_driver_id == ray._get_runtime_context().current_driver_id
assert dummy_driver_id == ray.worker.global_worker.current_job_id
# in worker
@ray.remote
def f():
return ray._get_runtime_context().current_driver_id
return ray.worker.global_worker.current_job_id
assert dummy_driver_id == ray.get(f.remote())
+8 -14
View File
@@ -42,6 +42,7 @@ from ray import import_thread
from ray import profiling
from ray.exceptions import (
RayConnectionError,
RayError,
RayTaskError,
ObjectStoreFullError,
@@ -108,7 +109,6 @@ class Worker:
self.mode = None
self.cached_functions_to_run = []
self.actor_init_error = None
self.make_actor = None
self.actors = {}
# Information used to maintain actor checkpoints.
self.actor_checkpoint_info = {}
@@ -496,10 +496,6 @@ _global_node = None
"""ray.node.Node: The global node object that is created by ray.init()."""
class RayConnectionError(Exception):
pass
def print_failed_task(task_status):
"""Print information about failed tasks.
@@ -886,6 +882,7 @@ def shutdown(exiting_interpreter=False):
atexit.register(shutdown, True)
# TODO(edoakes): this should only be set in the driver.
def sigterm_handler(signum, frame):
sys.exit(signal.SIGTERM)
@@ -1675,7 +1672,8 @@ def kill(actor):
raise ValueError("ray.kill() only supported for actors. "
"Got: {}.".format(type(actor)))
worker = ray.worker.get_global_worker()
worker = ray.worker.global_worker
worker.check_connected()
worker.core_worker.kill_actor(actor._ray_actor_id, False)
@@ -1690,10 +1688,6 @@ def _mode(worker=global_worker):
return worker.mode
def get_global_worker():
return global_worker
def make_decorator(num_return_vals=None,
num_cpus=None,
num_gpus=None,
@@ -1725,9 +1719,9 @@ def make_decorator(num_return_vals=None,
raise TypeError("The keyword 'max_calls' is not "
"allowed for actors.")
return worker.make_actor(function_or_class, num_cpus, num_gpus,
memory, object_store_memory, resources,
max_reconstructions)
return ray.actor.make_actor(function_or_class, num_cpus, num_gpus,
memory, object_store_memory, resources,
max_reconstructions)
raise TypeError("The @ray.remote decorator must be applied to "
"either a function or to a class.")
@@ -1815,7 +1809,7 @@ def remote(*args, **kwargs):
work and then shut down. If you want to kill them immediately, you can
also call ``ray.kill(actor)``.
"""
worker = get_global_worker()
worker = global_worker
if len(args) == 1 and len(kwargs) == 0 and callable(args[0]):
# This is the case where the decorator is just @ray.remote.