-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
leader election framework for performance metric collection
- Loading branch information
Amit Roushan
committed
Jun 3, 2021
1 parent
c46cdef
commit b6df07d
Showing
9 changed files
with
280 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Copyright 2020 The SODA Authors. | ||
# Copyright 2010 United States Government as represented by the | ||
# Administrator of the National Aeronautics and Space Administration. | ||
# Copyright 2011 Justin Santa Barbara | ||
# All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
"""Leader election interface defined""" | ||
|
||
import six | ||
import abc | ||
|
||
|
||
class LeaderCallback: | ||
|
||
def __init__(self): | ||
self.on_started_leading = None | ||
"""on_started_leading is called when elected as leader""" | ||
|
||
self.on_stopped_leading = None | ||
"""on_stopped_leading is called when Leader give up its leadership""" | ||
|
||
@staticmethod | ||
def register(on_started_leading, on_stop_leading): | ||
callback = LeaderCallback() | ||
callback.on_started_leading = on_started_leading | ||
callback.on_stopped_leading = on_stop_leading | ||
return callback | ||
|
||
|
||
# @six.add_metaclass(abc.ABCMeta) | ||
# class LeaderElectionLock: | ||
# | ||
# @abc.abstractmethod | ||
# def get(self): | ||
# """Get returns the LeaderElectionRecord""" | ||
# pass | ||
# | ||
# @abc.abstractmethod | ||
# def create(self, record): | ||
# """Create attempts to create a LeaderElectionRecord""" | ||
# pass | ||
# | ||
# @abc.abstractmethod | ||
# def update(self, record): | ||
# """Update will update and existing LeaderElectionRecord""" | ||
# pass | ||
# | ||
# @abc.abstractmethod | ||
# def identity(self): | ||
# """Identity will return the locks Identity""" | ||
# pass | ||
# | ||
# @abc.abstractmethod | ||
# def describe(self): | ||
# """Describe is used to convert details on current resource lock | ||
# into a string""" | ||
# pass | ||
|
||
|
||
@six.add_metaclass(abc.ABCMeta) | ||
class LeaderElector: | ||
|
||
def __init__(self, callbacks, election_key): | ||
self.callbacks = callbacks | ||
self.election_key = election_key | ||
|
||
@abc.abstractmethod | ||
def run(self): | ||
"""kick start leader election. | ||
invoke callback.on_started_leading callback once elected as leader | ||
invoke callback.on_stopped_leading callback once lose leadership | ||
run returns once leader losses its leadership | ||
""" | ||
pass | ||
|
||
def cleanup(self): | ||
"""Cleanup leader election residue | ||
""" | ||
pass |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright 2020 The SODA Authors. | ||
# Copyright 2010 United States Government as represented by the | ||
# Administrator of the National Aeronautics and Space Administration. | ||
# Copyright 2011 Justin Santa Barbara | ||
# All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
"""Leader elector is leased based leader election""" | ||
|
||
from delfin.leader_election.interface import LeaderElector | ||
from delfin.coordination import LeaderElectionCoordinator | ||
from delfin.coordination import LOCK_COORDINATOR | ||
from oslo_log import log | ||
|
||
import time | ||
|
||
LOG = log.getLogger(__name__) | ||
|
||
class Elector(LeaderElector): | ||
|
||
def __init__(self, callbacks, election_key): | ||
election_key = election_key.encode('ascii') | ||
super(Elector, self).__init__(callbacks, election_key) | ||
self.coordinator = None | ||
|
||
def run(self): | ||
if not self.coordinator: | ||
LOCK_COORDINATOR.start() | ||
self.coordinator = LeaderElectionCoordinator(self.election_key, | ||
LOCK_COORDINATOR. | ||
coordinator) | ||
self.coordinator.ensure_group() | ||
self.coordinator.join_group() | ||
self.coordinator.register_leader_callback(self.callbacks. | ||
on_started_leading) | ||
while True: | ||
LOG.info("sending heartbeat started") | ||
timeout = self.coordinator.start_heartbeat() | ||
LOG.info("started leader watch") | ||
self.coordinator.start_leader_watch() | ||
LOG.info("Return from leader watch") | ||
time.sleep(timeout/2) | ||
|
||
def cleanup(self): | ||
self.coordinator.cleanup() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters