Skip to content

Commit

Permalink
v5 SQL: Remove unused load balancer methods
Browse files Browse the repository at this point in the history
These methods were specially added for 4.2 SQL, but they are unused
in v5. Hence, we are removing the unused methods and reverting the
changes we made around the load balancer implementations and tests.

Note that, it is perfectly safe to remove these methods in Python.
If there were users that implemented these methods, they can use
their code without changing anything.
  • Loading branch information
mdumandag committed Aug 24, 2021
1 parent d2dee7f commit 834678b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 122 deletions.
53 changes: 6 additions & 47 deletions hazelcast/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,61 +276,18 @@ def next(self):
"""
raise NotImplementedError("next")

def next_data_member(self):
"""Returns the next data member to route to.
Returns:
hazelcast.core.MemberInfo: The next data member or
``None`` if no data member is available.
"""
return None

def can_get_next_data_member(self):
"""Returns whether this instance supports getting data members
through a call to :func:`next_data_member`.
Returns:
bool: ``True`` if this instance supports getting data members.
"""
return False


class _Members(object):
__slots__ = ("members", "data_members")

def __init__(self, members, data_members):
self.members = members
self.data_members = data_members


class _AbstractLoadBalancer(LoadBalancer):
def __init__(self):
self._cluster_service = None
self._members = _Members([], [])
self._members = []

def init(self, cluster_service):
self._cluster_service = cluster_service
cluster_service.add_listener(self._listener, self._listener, True)

def next(self):
members = self._members.members
return self._next(members)

def next_data_member(self):
members = self._members.data_members
return self._next(members)

def can_get_next_data_member(self):
return True

def _listener(self, _):
members = self._cluster_service.get_members()
data_members = [member for member in members if not member.lite_member]

self._members = _Members(members, data_members)

def _next(self, members):
raise NotImplementedError("_next")
self._members = self._cluster_service.get_members()


class RoundRobinLB(_AbstractLoadBalancer):
Expand All @@ -345,7 +302,8 @@ def __init__(self):
super(RoundRobinLB, self).__init__()
self._idx = 0

def _next(self, members):
def next(self):
members = self._members
if not members:
return None

Expand All @@ -358,7 +316,8 @@ def _next(self, members):
class RandomLB(_AbstractLoadBalancer):
"""A load balancer that selects a random member to route to."""

def _next(self, members):
def next(self):
members = self._members
if not members:
return None
idx = random.randrange(0, len(members))
Expand Down
85 changes: 10 additions & 75 deletions tests/unit/load_balancer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,9 @@
from hazelcast.util import RandomLB, RoundRobinLB


class _MockMember(object):
def __init__(self, id, lite_member):
self.id = id
self.lite_member = lite_member


class _MockClusterService(object):
def __init__(self, data_member_count, lite_member_count):
id = 0
data_members = []
lite_members = []

for _ in range(data_member_count):
data_members.append(_MockMember(id, False))
id += 1

for _ in range(lite_member_count):
lite_members.append(_MockMember(id, True))
id += 1

self._members = data_members + lite_members
def __init__(self, members):
self._members = members

def add_listener(self, listener, *_):
for m in self._members:
Expand All @@ -35,74 +17,27 @@ def get_members(self):

class LoadBalancersTest(unittest.TestCase):
def test_random_lb_with_no_members(self):
cluster = _MockClusterService(0, 0)
cluster = _MockClusterService([])
lb = RandomLB()
lb.init(cluster)
self.assertIsNone(lb.next())
self.assertIsNone(lb.next_data_member())

def test_round_robin_lb_with_no_members(self):
cluster = _MockClusterService(0, 0)
cluster = _MockClusterService([])
lb = RoundRobinLB()
lb.init(cluster)
self.assertIsNone(lb.next())
self.assertIsNone(lb.next_data_member())

def test_random_lb_with_data_members(self):
cluster = _MockClusterService(3, 0)
def test_random_lb_with_members(self):
cluster = _MockClusterService([0, 1, 2])
lb = RandomLB()
lb.init(cluster)
self.assertTrue(lb.can_get_next_data_member())
for _ in range(10):
self.assertTrue(0 <= lb.next().id <= 2)
self.assertTrue(0 <= lb.next_data_member().id <= 2)
self.assertTrue(0 <= lb.next() <= 2)

def test_round_robin_lb_with_data_members(self):
cluster = _MockClusterService(5, 0)
def test_round_robin_lb_with_members(self):
cluster = _MockClusterService([0, 1, 2])
lb = RoundRobinLB()
lb.init(cluster)
self.assertTrue(lb.can_get_next_data_member())

for i in range(10):
self.assertEqual(i % 5, lb.next().id)

for i in range(10):
self.assertEqual(i % 5, lb.next_data_member().id)

def test_random_lb_with_lite_members(self):
cluster = _MockClusterService(0, 3)
lb = RandomLB()
lb.init(cluster)

for _ in range(10):
self.assertTrue(0 <= lb.next().id <= 2)
self.assertIsNone(lb.next_data_member())

def test_round_robin_lb_with_lite_members(self):
cluster = _MockClusterService(0, 3)
lb = RoundRobinLB()
lb.init(cluster)

for i in range(10):
self.assertEqual(i % 3, lb.next().id)
self.assertIsNone(lb.next_data_member())

def test_random_lb_with_mixed_members(self):
cluster = _MockClusterService(3, 3)
lb = RandomLB()
lb.init(cluster)

for _ in range(20):
self.assertTrue(0 <= lb.next().id < 6)
self.assertTrue(0 <= lb.next_data_member().id < 3)

def test_round_robin_lb_with_mixed_members(self):
cluster = _MockClusterService(3, 3)
lb = RoundRobinLB()
lb.init(cluster)

for i in range(24):
self.assertEqual(i % 6, lb.next().id)

for i in range(24):
self.assertEqual(i % 3, lb.next_data_member().id)
self.assertEqual(i % 3, lb.next())

0 comments on commit 834678b

Please sign in to comment.