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 send_entrypoint_request_reply(self, message: EntrypointRequest): reply_context = message.reply_context req_id = message.req_id remote_rpc_endpoint = message.remote_rpc_endpoint process_fid = message.process_fid LOG.debug('Processing entrypoint request from remote endpoint' " '{}', process fid {}".format(remote_rpc_endpoint, str(process_fid))) sess = principal_rm = confds = None try: prov = ConsulUtil() sess = prov.get_leader_session_no_wait() principal_rm = prov.get_session_node(sess) confds = prov.get_confd_list() except Exception: LOG.exception('Failed to get the data from Consul.' ' Replying with EAGAIN error code.') self._ffi.entrypoint_reply(reply_context, req_id.to_c(), EAGAIN, 0, make_array(FidStruct, []), make_array(c.c_char_p, []), 0, self.rm_fid.to_c(), None) LOG.debug('Reply sent') return rc_quorum = int(len(confds) / 2 + 1) rm_eps = None for svc in confds: if svc.node == principal_rm: rm_eps = svc.address break if not rm_eps: raise RuntimeError('No RM node found in Consul') confd_fids = [x.fid.to_c() for x in confds] confd_eps = [make_c_str(x.address) for x in confds] LOG.debug('Passing the entrypoint reply to hax.c layer') self._ffi.entrypoint_reply(reply_context, req_id.to_c(), 0, len(confds), make_array(FidStruct, confd_fids), make_array(c.c_char_p, confd_eps), rc_quorum, self.rm_fid.to_c(), make_c_str(rm_eps)) LOG.debug('Entrypoint request has been replied to')
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)