Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion kubernetes/base/leaderelection/electionconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@

class Config:
# Validate config, exit if an error is detected
def __init__(self, lock, lease_duration, renew_deadline, retry_period, onstarted_leading, onstopped_leading):
def __init__(
self,
lock,
lease_duration,
renew_deadline,
retry_period,
onstarted_leading,
onstopped_leading,
# release the lock when stopped when release_on_stop set true
release_on_stop=False):
self.jitter_factor = 1.2

if lock is None:
Expand All @@ -44,6 +53,7 @@ def __init__(self, lock, lease_duration, renew_deadline, retry_period, onstarted
self.lease_duration = lease_duration
self.renew_deadline = renew_deadline
self.retry_period = retry_period
self.release_on_stop = release_on_stop

if onstarted_leading is None:
sys.exit("callback onstarted_leading cannot be None")
Expand Down
14 changes: 11 additions & 3 deletions kubernetes/base/leaderelection/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,17 @@ def example_func():
# In that case, a default callback function will be used

# Create config
config = electionconfig.Config(ConfigMapLock(lock_name, lock_namespace, candidate_id), lease_duration=17,
renew_deadline=15, retry_period=5, onstarted_leading=example_func,
onstopped_leading=None)
config = electionconfig.Config(
ConfigMapLock(
lock_name,
lock_namespace,
candidate_id),
lease_duration=17,
renew_deadline=15,
retry_period=5,
onstarted_leading=example_func,
onstopped_leading=None,
release_on_stop=True)

# Enter leader election
leaderelection.LeaderElection(config).run()
Expand Down
33 changes: 27 additions & 6 deletions kubernetes/base/leaderelection/leaderelection.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ def run(self):
logging.info("{} successfully acquired lease".format(self.election_config.lock.identity))

# Start leading and call OnStartedLeading()
threading.daemon = True
threading.Thread(target=self.election_config.onstarted_leading).start()
try:
threading.daemon = True
threading.Thread(target=self.election_config.onstarted_leading).start()

self.renew_loop()
self.renew_loop()
finally:
if self.election_config.release_on_stop:
self.release()
# Failed to update lease, run OnStoppedLeading callback
self.election_config.onstopped_leading()

# Failed to update lease, run OnStoppedLeading callback
self.election_config.onstopped_leading()

def acquire(self):
# Follower
Expand Down Expand Up @@ -103,6 +107,22 @@ def renew_loop(self):

# failed to renew, return
return

def release(self):
lock_status, old_election_record = self.election_config.lock.get(self.election_config.lock.name,
self.election_config.lock.namespace)
if self.observed_record and self.observed_record.holder_identity != old_election_record.holder_identity:
return

if lock_status:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

let's check if the elector is the leader. We can short-circuit the release method if not

logging.info("release lock: {}".format(old_election_record.holder_identity))
now_timestamp = time.time()
now = datetime.datetime.fromtimestamp(now_timestamp)
old_election_record.lease_duration = 1
old_election_record.holder_identity = None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the lock may have been successfully acquired by another client. Doing so may force that client to lose the leadership. Why do we want to do this?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

this happens when lock has been acquired and exception occurs that lock cannot be normally release, if not release the lock here, the next leader election must wait for timeout

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please also set renew time and acquire time

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

old_election_record.renew_time = str(now)
old_election_record.acquire_time = str(now)
self.update_lock(old_election_record)

def try_acquire_or_renew(self):
now_timestamp = time.time()
Expand Down Expand Up @@ -187,5 +207,6 @@ def update_lock(self, leader_election_record):

self.observed_record = leader_election_record
self.observed_time_milliseconds = int(time.time() * 1000)
logging.info("leader {} has successfully acquired lease".format(leader_election_record.holder_identity))
if leader_election_record.holder_identity:
logging.info("leader {} has successfully acquired lease".format(leader_election_record.holder_identity))
return True