zuul/zuul/zk/election.py
James E. Blair 65cac91e6c Add ZK session-aware elections
This creates a session-aware election class which will set a flag
that indicates it has lost the underlying lock.  We can check this
flag when iterating to make sure that we don't continue to attempt
to operate when we have lost the lock underlying an election.

Some drivers had connection lost handling for the EventReceiverElection
at the driver level.  Those are updated to use the handling at the
election level for consistency as well as brevity.

Change-Id: I776f88d015acdfbf1487a85d8473cd174917e90f
2021-09-10 10:55:00 -07:00

38 lines
1.3 KiB
Python

# Copyright 2021 Acme Gating, LLC
#
# 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.
from kazoo.protocol.states import KazooState
from kazoo.recipe.election import Election
class SessionAwareElection(Election):
def __init__(self, client, path, identifier=None):
self._zuul_session_expired = False
super().__init__(client, path, identifier)
def run(self, func, *args, **kwargs):
self._zuul_session_expired = False
self.lock.client.add_listener(self._zuul_session_watcher)
try:
return super().run(func, *args, **kwargs)
finally:
self.lock.client.remove_listener(self._zuul_session_watcher)
def _zuul_session_watcher(self, state):
if state == KazooState.LOST:
self._zuul_session_expired = True
def is_still_valid(self):
return not self._zuul_session_expired