def _remove_stale_session(util: ConsulUtil) -> None: '''Destroys a stale RC leader session if it exists or does nothing otherwise. An RC leader session may survive 'hctl shutdown'. In such a case 'leader' key will contain a garbage value 'elect2805' but the session will be alive and thus RC leader will not be re-elected. ''' if not util.kv.kv_get_raw('leader'): # No leader key means that there can be no stale RC leader session for # sure. We're starting for the first time against a fresh Consul KV. return sess = util.get_leader_session_no_wait() # We might face situation where we have stale session such that we have # 'session' present but 'value' is not present for leader key. # In such situation we need to destroy the session so that RC re-election # can be triggered automatically if util.is_leader_value_present_for_session(): node = util.get_leader_node() if re.match(r'^elect[\d]+$', node): LOG.debug( 'Stale leader session found: RC leader session %s is ' 'found while the leader node seems to be ' 'stub: %s', sess, node) util.destroy_session(sess) LOG.debug('Stale session %s destroyed ' 'to enable RC re-election', sess) else: util.destroy_session(sess) LOG.debug('Stale session %s destroyed to enable RC re-election', sess)
def _remove_stale_session(util: ConsulUtil) -> None: '''Destroys a stale RC leader session if it exists or does nothing otherwise. An RC leader session may survive 'hctl shutdown'. In such a case 'leader' key will contain a garbage value 'elect2805' but the session will be alive and thus RC leader will not be re-elected. ''' if not util.kv.kv_get_raw('leader'): # No leader key means that there can be no stale RC leader session for # sure. We're starting for the first time against a fresh Consul KV. return sess = util.get_leader_session_no_wait() node = util.get_leader_node() if re.match(r'^elect[\d]+$', node): LOG.debug( 'Stale leader session found: RC leader session %s is ' 'found while the leader node seems to be stub: %s', sess, node) util.destroy_session(sess) LOG.debug('Stale session %s destroyed to enable RC re-election', sess)