Refactor code about ray.ObjectID. (#3674)

* Refactor code about ray.ObjectID.

* remove from_random and use nil_id instead of constructor

* remove id() in hash

* Lint and fix

* Change driver id to ObjectID

* Replace binary_to_hex(ObjectID.id()) to ObjectID.hex()
This commit is contained in:
Yuhong Guo
2019-01-13 01:47:29 -08:00
committed by Philipp Moritz
parent c4b058739b
commit d2cf8561f2
14 changed files with 191 additions and 169 deletions
+21 -10
View File
@@ -30,8 +30,6 @@ using ray::UniqueID;
using ray::FunctionID;
using ray::TaskID;
PyObject *CommonError;
/* Initialize pickle module. */
PyObject *pickle_module = NULL;
@@ -117,13 +115,18 @@ static int PyObjectID_init(PyObjectID *self, PyObject *args, PyObject *kwds) {
return -1;
}
if (size != sizeof(ObjectID)) {
PyErr_SetString(CommonError, "ObjectID: object id string needs to have length 20");
PyErr_SetString(PyExc_ValueError,
"ObjectID: object id string needs to have length 20");
return -1;
}
std::memcpy(self->object_id.mutable_data(), data, sizeof(self->object_id));
return 0;
}
static PyObject *PyObjectID_nil_id(PyObject *cls) {
return PyObjectID_make(ray::UniqueID());
}
/* Create a PyObjectID from C. */
PyObject *PyObjectID_make(ObjectID object_id) {
PyObjectID *result = PyObject_New(PyObjectID, &PyObjectIDType);
@@ -269,9 +272,14 @@ static PyObject *PyObjectID_repr(PyObjectID *self) {
return result;
}
static PyObject *PyObjectID___reduce__(PyObjectID *self) {
PyErr_SetString(CommonError, "ObjectID objects cannot be serialized.");
return NULL;
static PyObject *PyObjectID_getstate(PyObjectID *self) {
PyObject *field;
field = PyBytes_FromStringAndSize((char *)self->object_id.data(), sizeof(ObjectID));
return Py_BuildValue("(N)", field);
}
static PyObject *PyObjectID___reduce__(PyObjectID *self, PyObject *arg) {
return Py_BuildValue("(ON)", Py_TYPE(self), PyObjectID_getstate(self));
}
static PyMethodDef PyObjectID_methods[] = {
@@ -283,9 +291,10 @@ static PyMethodDef PyObjectID_methods[] = {
"Return the object ID as a string in hex."},
{"is_nil", (PyCFunction)PyObjectID_is_nil, METH_NOARGS,
"Return whether the ObjectID is nil"},
{"__reduce__", (PyCFunction)PyObjectID___reduce__, METH_NOARGS,
"Say how to pickle this ObjectID. This raises an exception to prevent"
"object IDs from being serialized."},
{"__reduce__", (PyCFunction)PyObjectID___reduce__, METH_VARARGS,
"Provide a way to pickle this ObjectID."},
{"nil_id", (PyCFunction)PyObjectID_nil_id, METH_NOARGS | METH_CLASS,
"Create an instance of ray.ObjectID from random string"},
{NULL} /* Sentinel */
};
@@ -293,9 +302,11 @@ static PyMemberDef PyObjectID_members[] = {
{NULL} /* Sentinel */
};
// This python class is introduced by python/ray/raylet/__init__.py.
// Therefore, tp_name should match the path. ray.ObjectID is also OK.
PyTypeObject PyObjectIDType = {
PyVarObject_HEAD_INIT(NULL, 0) /* ob_size */
"common.ObjectID", /* tp_name */
"ray.raylet.ObjectID", /* tp_name */
sizeof(PyObjectID), /* tp_basicsize */
0, /* tp_itemsize */
0, /* tp_dealloc */
+1 -1
View File
@@ -12,7 +12,7 @@
typedef char TaskSpec;
class TaskBuilder;
extern PyObject *CommonError;
extern PyObject *ray_common_error;
// clang-format off
typedef struct {
@@ -31,6 +31,8 @@ static int PyRayletClient_init(PyRayletClient *self, PyObject *args, PyObject *k
return 0;
}
PyObject *ray_common_error = nullptr;
static void PyRayletClient_dealloc(PyRayletClient *self) {
if (self->raylet_client != NULL) {
delete self->raylet_client;
@@ -112,7 +114,7 @@ static PyObject *PyRayletClient_FetchOrReconstruct(PyRayletClient *self, PyObjec
stream << "[RayletClient] FetchOrReconstruct failed: "
<< "raylet client may be closed, check raylet status. error message: "
<< status.ToString();
PyErr_SetString(CommonError, stream.str().c_str());
PyErr_SetString(ray_common_error, stream.str().c_str());
return NULL;
}
}
@@ -485,9 +487,9 @@ MOD_INIT(libraylet_library_python) {
PyModule_AddObject(m, "RayletClient", (PyObject *)&PyRayletClientType);
char common_error[] = "common.error";
CommonError = PyErr_NewException(common_error, NULL, NULL);
Py_INCREF(CommonError);
PyModule_AddObject(m, "common_error", CommonError);
ray_common_error = PyErr_NewException(common_error, NULL, NULL);
Py_INCREF(ray_common_error);
PyModule_AddObject(m, "RayCommonError", ray_common_error);
Py_INCREF(&PyRayConfigType);
PyModule_AddObject(m, "RayConfig", (PyObject *)&PyRayConfigType);