Make sure user seeding does not affect actor ID generation. (#506)

* Make sure user seeding does not affect actor ID generation.

* Fix linting.

* Add test.
This commit is contained in:
Robert Nishihara
2017-05-03 16:29:55 -07:00
committed by Philipp Moritz
parent e50a23b820
commit 245c8ab888
4 changed files with 50 additions and 14 deletions
+1 -6
View File
@@ -5,7 +5,6 @@ from __future__ import print_function
import hashlib
import inspect
import json
import numpy as np
import random
import redis
import traceback
@@ -14,7 +13,7 @@ import ray.local_scheduler
import ray.pickling as pickling
import ray.signature as signature
import ray.worker
from ray.utils import binary_to_hex, hex_to_binary
from ray.utils import random_string, binary_to_hex, hex_to_binary
# This is a variable used by each actor to indicate the IDs of the GPUs that
# the worker is currently allowed to use.
@@ -30,10 +29,6 @@ def get_gpu_ids():
return gpu_ids
def random_string():
return np.random.bytes(20)
def random_actor_id():
return ray.local_scheduler.ObjectID(random_string())
+26
View File
@@ -3,11 +3,37 @@ from __future__ import division
from __future__ import print_function
import binascii
import numpy as np
import sys
import ray.local_scheduler
def random_string():
"""Generate a random string to use as an ID.
Note that users may seed numpy, which could cause this function to generate
duplicate IDs. Therefore, we need to seed numpy ourselves, but we can't
interfere with the state of the user's random number generator, so we extract
the state of the random number generator and reset it after we are done.
TODO(rkn): If we want to later guarantee that these are generated in a
deterministic manner, then we will need to make some changes here.
Returns:
A random byte string of length 20.
"""
# Get the state of the numpy random number generator.
numpy_state = np.random.get_state()
# Try to use true randomness.
np.random.seed(None)
# Generate the random ID.
random_id = np.random.bytes(20)
# Reset the state of the numpy random number generator.
np.random.set_state(numpy_state)
return random_id
def decode(byte_str):
"""Make this unicode in Python 3, otherwise leave it as bytes."""
if sys.version_info >= (3, 0):
+1 -8
View File
@@ -27,6 +27,7 @@ import ray.signature as signature
import ray.numbuf
import ray.local_scheduler
import ray.plasma
from ray.utils import random_string
SCRIPT_MODE = 0
WORKER_MODE = 1
@@ -60,14 +61,6 @@ PUT_RECONSTRUCTION_ERROR_TYPE = b"put_reconstruction"
TASK_STATUS_RUNNING = 8
def random_string():
return np.random.bytes(20)
def random_object_id():
return ray.local_scheduler.ObjectID(random_string())
class FunctionID(object):
def __init__(self, function_id):
self.function_id = function_id