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

Add hashed memcached client support #173

Merged
Show file tree
Hide file tree
Changes from 5 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
38 changes: 31 additions & 7 deletions deltacat/io/memcached_object_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from collections import defaultdict
import time
from deltacat.io.object_store import IObjectStore
from typing import Any, List
from typing import Any, List, Optional
from deltacat import logs
import uuid
import socket
from pymemcache.client.base import Client
from pymemcache.client.retrying import RetryingClient
from pymemcache.exceptions import MemcacheUnexpectedCloseError
from pymemcache.client.rendezvous import RendezvousHash

logger = logs.configure_deltacat_logger(logging.getLogger(__name__))

Expand All @@ -19,25 +20,39 @@ class MemcachedObjectStore(IObjectStore):
An implementation of object store that uses Memcached.
"""

def __init__(self, port=11212) -> None:
def __init__(
self, storage_node_ips: Optional[List[str]] = None, port: Optional[int] = 11212
) -> None:
self.client_cache = {}
self.current_ip = None
self.SEPARATOR = "_"
self.port = port
self.storage_node_ips = storage_node_ips
self.hasher = None
super().__init__()

def initialize_hasher(self):
if not self.hasher and self.storage_node_ips:
self.hasher = RendezvousHash()
for n in self.storage_node_ips:
self.hasher.add_node(n)

def put_many(self, objects: List[object], *args, **kwargs) -> List[Any]:
input = {}
result = []
current_ip = self._get_current_ip()
if self.storage_node_ips:
create_ref_ip = self._get_storage_node_ip(current_ip)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call get_node with the key instead of current ip to ensure the uniform distribution?

else:
create_ref_ip = current_ip
for obj in objects:
serialized = cloudpickle.dumps(obj)
uid = uuid.uuid4()
ref = self._create_ref(uid, current_ip)
ref = self._create_ref(uid, create_ref_ip)
input[uid.__str__()] = serialized
result.append(ref)

client = self._get_client_by_ip(current_ip)
client = self._get_client_by_ip(create_ref_ip)
if client.set_many(input, noreply=False):
raise RuntimeError("Unable to write few keys to cache")

Expand All @@ -47,8 +62,12 @@ def put(self, obj: object, *args, **kwargs) -> Any:
serialized = cloudpickle.dumps(obj)
uid = uuid.uuid4()
current_ip = self._get_current_ip()
ref = self._create_ref(uid, current_ip)
client = self._get_client_by_ip(current_ip)
if self.storage_node_ips:
create_ref_ip = self._get_storage_node_ip(current_ip)
else:
create_ref_ip = current_ip
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this to a single function so that we don't have to have this if statement in every method?

ref = self._create_ref(uid, create_ref_ip)
client = self._get_client_by_ip(create_ref_ip)

if client.set(uid.__str__(), serialized):
return ref
Expand Down Expand Up @@ -99,6 +118,11 @@ def get(self, ref: Any, *args, **kwargs) -> object:
def _create_ref(self, uid, ip) -> str:
return f"{uid}{self.SEPARATOR}{ip}"

def _get_storage_node_ip(self, ip_address: str):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename argument to key.

self.initialize_hasher()
storage_node_ip = self.hasher.get_node(ip_address)
raghumdani marked this conversation as resolved.
Show resolved Hide resolved
return storage_node_ip

def _get_client_by_ip(self, ip_address: str):
if ip_address in self.client_cache:
return self.client_cache[ip_address]
Expand All @@ -108,7 +132,7 @@ def _get_client_by_ip(self, ip_address: str):
base_client,
attempts=3,
retry_delay=0.01,
retry_for=[MemcacheUnexpectedCloseError],
retry_for=[MemcacheUnexpectedCloseError, ConnectionResetError],
)

self.client_cache[ip_address] = client
Expand Down
4 changes: 3 additions & 1 deletion deltacat/tests/io/test_memcached_object_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class TestMemcachedObjectStore(unittest.TestCase):
def setUp(self):
from deltacat.io.memcached_object_store import MemcachedObjectStore

self.object_store = MemcachedObjectStore()
self.object_store = MemcachedObjectStore(
storage_node_ips=["172.1.1.1", "172.2.2.2", "172.3.3.3"]
)

@mock.patch("deltacat.io.memcached_object_store.Client")
@mock.patch("deltacat.io.memcached_object_store.RetryingClient")
Expand Down
10 changes: 8 additions & 2 deletions deltacat/utils/placement.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

@dataclass
class PlacementGroupConfig:
def __init__(self, opts, resource):
def __init__(self, opts, resource, node_ips):
self.opts = opts
self.resource = resource
self.node_ips = node_ips


class NodeGroupManager:
Expand Down Expand Up @@ -275,17 +276,22 @@ def _config(
# query available resources given list of node id
all_nodes_available_res = ray._private.state.state._available_resources_per_node()
pg_res = {"CPU": 0, "memory": 0, "object_store_memory": 0}
node_ips = []
for node_id in node_ids:
if node_id in all_nodes_available_res:
v = all_nodes_available_res[node_id]
node_detail = get_node(node_id)
pg_res["CPU"] += node_detail["resources_total"]["CPU"]
pg_res["memory"] += v["memory"]
pg_res["object_store_memory"] += v["object_store_memory"]
node_ips.append(node_detail["node_ip"])
cluster_resources["CPU"] = int(pg_res["CPU"])
cluster_resources["memory"] = float(pg_res["memory"])
cluster_resources["object_store_memory"] = float(pg_res["object_store_memory"])
pg_config = PlacementGroupConfig(opts, cluster_resources)
pg_config = PlacementGroupConfig(
opts=opts, cluster_resources=cluster_resources, node_ips=node_ips
)
logger.info(f"pg has resources:{cluster_resources}")
logger.debug(f"pg has node ips:{node_ips}")

return pg_config