def __init__(self, etcd_client, name, election_path,
work_time,
recovery_time=5,
multi_worker_ok=False):
"""Participant in a leader election via etcd datastore.
etcd_client: the client handle for dealing with etcd
name: the category name - we elect one leader of this type
election_path: the location where we conduct elections in etcd
work_time: the typical time the leader spends doing work.
It remains elected for this long without conducting another
election.
recovery_time: the time, after we're certain the leader has
stopped doing work, that is the longest we want to wait before
someone else takes over if the leader has died (Note that
this means you can be without a working leader for work_time
+ recovery_time if the leader crashes just after winning the
election)
multi_worker_ok: True if you'd prefer to favour having at least
one elected leader over having no more than one elected leader.
Typically this will cause a second leader to start working even
if the original still believes it's elected, and is useful if
that's more likely to reduce pauses.
"""
self.etcd_client = etcd_client
self.name = name
# A unique value that identifies each worker thread
self.thread_id = str(uuid.uuid4())
# Sleeping threads wake up after this time and
# check if a master is alive and one of them will become the master
# if the current master key has expired
self.recovery_time = recovery_time
# Threads hold the lock for this lng because this is the most
# work they will do.
self.work_time = work_time
self.master_key = election_path + "/master_%s" % self.name
# We recommend you configure these log levels
# etcd_log = logging.getLogger('etcd.client')
# etcd_log.setLevel(logging.logging.WARNING)
# LOG.setLevel(logging.logging.INFO)
global elector_cleanup
elector_cleanup.append(self)
评论列表
文章目录