def become_leader(config, redis_client):
""" tscached can be deployed on multiple servers. Only one of them should exert shadow load.
We use RedLock (http://redis.io/topics/distlock) to achieve this. If we cannot acquire the
shadow lock, fail fast. If our server (or this program) crashes, the leader key will expire and
another server will take over eventually.
RedLock is (debatably) imperfect, but that's okay with us: our worst case is that some work gets
done twice - because we are using Redis as a cache and *not* as a datastore. We're using one of
the standard Python clientlibs: https://github.com/glasslion/redlock
This implementation assumes a single-master Redis cluster.
:param config: dict representing the top-level tscached config
:param redis_client: redis.StrictRedis
:return: redlock.RedLock or False
"""
hostname = socket.gethostname()
leader_expiration = config['shadow'].get('leader_expiration', 3600) * 1000 # ms expected
deets = [redis_client] # no need to reinitialize a redis connection.
try:
lock = redlock.RedLock(SHADOW_LOCK_KEY, ttl=leader_expiration, connection_details=deets)
if lock.acquire():
# mostly for debugging purposes
redis_client.set(SHADOW_SERVER_KEY, hostname, px=leader_expiration)
logging.info('Lock acquired; now held by %s' % hostname)
return lock
else:
other_host = redis_client.get(SHADOW_SERVER_KEY)
logging.info('Could not acquire lock; lock is held by %s' % other_host)
return False
except redis.exceptions.RedisError as e:
logging.error('RedisError in acquire_leader: ' + e.message)
return False
except redlock.RedLockError as e:
logging.error('RedLockError in acquire_leader: ' + e.message)
return False
评论列表
文章目录