Repository navigation
fix leaderelection: release lock when error occour and graceful shotd… #1747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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: | ||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i fix it just like client-go did: https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/client-go/tools/leaderelection/leaderelection.go#L294
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please also set renew time and acquire time
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
|
@@ -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 | ||
There was a problem hiding this comment.
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