Don't hold lock when incrementing load balancer position

This commit is contained in:
Tyler Hobbs
2013-06-03 16:56:27 -05:00
parent 2d046080a3
commit ed7d488d25

View File

@@ -1,6 +1,5 @@
from itertools import islice, cycle, groupby, repeat from itertools import islice, cycle, groupby, repeat
from random import randint from random import randint
from threading import RLock
from cassandra import ConsistencyLevel from cassandra import ConsistencyLevel
@@ -22,9 +21,6 @@ class LoadBalancingPolicy(object):
class RoundRobinPolicy(LoadBalancingPolicy): class RoundRobinPolicy(LoadBalancingPolicy):
def __init__(self):
self._lock = RLock()
def populate(self, cluster, hosts): def populate(self, cluster, hosts):
self._live_hosts = set(hosts) self._live_hosts = set(hosts)
if len(hosts) <= 1: if len(hosts) <= 1:
@@ -36,7 +32,8 @@ class RoundRobinPolicy(LoadBalancingPolicy):
return HostDistance.LOCAL return HostDistance.LOCAL
def make_query_plan(self, query=None): def make_query_plan(self, query=None):
with self._lock: # not thread-safe, but we don't care much about lost increments
# for the purposes of load balancing
pos = self._position pos = self._position
self._position += 1 self._position += 1
@@ -66,7 +63,6 @@ class DCAwareRoundRobinPolicy(LoadBalancingPolicy):
self.local_dc = local_dc self.local_dc = local_dc
self.used_hosts_per_remote_dc = used_hosts_per_remote_dc self.used_hosts_per_remote_dc = used_hosts_per_remote_dc
self._dc_live_hosts = {} self._dc_live_hosts = {}
self._lock = RLock()
def populate(self, cluster, hosts): def populate(self, cluster, hosts):
for dc, dc_hosts in groupby(hosts, lambda h: h.datacenter): for dc, dc_hosts in groupby(hosts, lambda h: h.datacenter):
@@ -96,7 +92,8 @@ class DCAwareRoundRobinPolicy(LoadBalancingPolicy):
return HostDistance.IGNORED return HostDistance.IGNORED
def make_query_plan(self, query=None): def make_query_plan(self, query=None):
with self._lock: # not thread-safe, but we don't care much about lost increments
# for the purposes of load balancing
pos = self._position pos = self._position
self._position += 1 self._position += 1