Skip to content

Commit 776731b

Browse files
authored
Merge pull request #1 from gtfierro/update-btrdb-custom-serialization
Make BTrDB connections serializable in Ray
2 parents 13e30ff + ab180fd commit 776731b

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

btrdb/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
from btrdb.exceptions import ConnectionError
2121
from btrdb.version import get_version
2222
from btrdb.utils.credentials import credentials_by_profile, credentials
23+
from btrdb.utils.ray import register_serializer
2324
from btrdb.stream import MINIMUM_TIME, MAXIMUM_TIME
25+
from warnings import warn
2426

2527
##########################################################################
2628
## Module Variables
@@ -40,7 +42,7 @@
4042
def _connect(endpoints=None, apikey=None):
4143
return BTrDB(Endpoint(Connection(endpoints, apikey=apikey).channel))
4244

43-
def connect(conn_str=None, apikey=None, profile=None):
45+
def connect(conn_str=None, apikey=None, profile=None, shareable=False):
4446
"""
4547
Connect to a BTrDB server.
4648
@@ -57,6 +59,12 @@ def connect(conn_str=None, apikey=None, profile=None):
5759
The name of a profile containing the required connection information as
5860
found in the user's predictive grid credentials file
5961
`~/.predictivegrid/credentials.yaml`.
62+
shareable: bool, default=False
63+
Whether or not the connection can be "shared" in a distributed setting such
64+
as Ray workers. If set to True, the connection can be serialized and sent
65+
to other workers so that data can be retrieved in parallel; **however**, this
66+
is less secure because it is possible for other users of the Ray cluster to
67+
use your API key to fetch data.
6068
6169
Returns
6270
-------
@@ -68,6 +76,11 @@ def connect(conn_str=None, apikey=None, profile=None):
6876
if conn_str and profile:
6977
raise ValueError("Received both conn_str and profile arguments.")
7078

79+
# check shareable flag and register custom serializer if necessary
80+
if shareable:
81+
warn("a shareable connection is potentially insecure; other users of the same cluster may be able to access your API key")
82+
register_serializer(conn_str=conn_str, apikey=apikey, profile=profile)
83+
7184
# use specific profile if requested
7285
if profile:
7386
return _connect(**credentials_by_profile(profile))

btrdb/utils/ray.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import ray
44

5+
import semver
6+
57
import btrdb
68
from btrdb.conn import BTrDB
79

@@ -22,8 +24,18 @@ def register_serializer(conn_str=None, apikey=None, profile=None):
2224
found in the user's predictive grid credentials file
2325
`~/.predictivegrid/credentials.yaml`.
2426
"""
25-
ray.register_custom_serializer(
26-
BTrDB, serializer=btrdb_serializer, deserializer=partial(btrdb_deserializer, conn_str=conn_str, apikey=apikey, profile=profile))
27+
assert ray.is_initialized(), "Need to call ray.init() before registering custom serializer"
28+
# TODO: check the version using the 'semver' package?
29+
ver = semver.VersionInfo.parse(ray.__version__)
30+
if ver.major == 0:
31+
ray.register_custom_serializer(
32+
BTrDB, serializer=btrdb_serializer, deserializer=partial(btrdb_deserializer, conn_str=conn_str, apikey=apikey, profile=profile))
33+
elif ver.major == 1 and ver.minor in range(2, 4):
34+
# TODO: check different versions of ray?
35+
ray.util.register_serializer(
36+
BTrDB, serializer=btrdb_serializer, deserializer=partial(btrdb_deserializer, conn_str=conn_str, apikey=apikey, profile=profile))
37+
else:
38+
raise Exception("Ray version %s does not have custom serialization. Please upgrade to >= 1.2.0" % ray.__version__)
2739

2840
def btrdb_serializer(_):
2941
"""

0 commit comments

Comments
 (0)