def _do_verify_cooperated_brokers(self): other_brokers = list(self.cooperated_brokers.values()) if self.broker_idurl in other_brokers: other_brokers.remove(self.broker_idurl) if not other_brokers: self.automat('failed') return other_broker_idurl = other_brokers[0] service_params = { 'action': 'broker-verify', 'customer_id': self.customer_id, 'broker_id': self.broker_id, 'position': self.known_position, 'streams': self.known_streams, 'known_brokers': self.cooperated_brokers, } if _Debug: lg.args(_DebugLevel, target=other_broker_idurl, service_params=service_params) result = p2p_service_seeker.connect_known_node( remote_idurl=other_broker_idurl, service_name='service_message_broker', service_params=service_params, request_service_timeout=config.conf().getInt('services/message-broker/broker-negotiate-ack-timeout'), force_handshake=False, attempts=1, ) result.addCallback(self._on_broker_verify_result) if _Debug: result.addErrback(lg.errback, debug=_Debug, debug_level=_DebugLevel, method='queue_keeper._do_verify_cooperated_brokers') result.addErrback(self._on_broker_verify_failed)
def doRequestCurBroker(self, event, *args, **kwargs): """ Action method. """ target_pos = self.desired_position known_brokers = {} known_brokers.update(self.cooperated_brokers or {}) if event in [ 'record-busy', ]: # there is no cooperation done yet but current record in DHT on that position belongs to another broker target_pos = self.desired_position broker_idurl = id_url.field(self.dht_brokers[target_pos]) known_brokers[self.desired_position] = self.my_broker_idurl elif event in [ 'prev-record-busy', ]: # there is no cooperation done yet but found another broker on the previous position in DHT target_pos = self.desired_position - 1 broker_idurl = id_url.field(self.dht_brokers[target_pos]) known_brokers[self.desired_position] = self.my_broker_idurl elif event in [ 'my-record-busy', 'my-record-empty', 'my-record-own', ]: # me and two other brokers already made a cooperation, connecting again with already known previous broker target_pos = self.my_position - 1 broker_idurl = id_url.field(self.cooperated_brokers[target_pos]) known_brokers[self.my_position] = self.my_broker_idurl if _Debug: lg.args(_DebugLevel, e=event, my=self.my_position, desired=self.desired_position, target=target_pos, broker=broker_idurl, known=known_brokers) result = p2p_service_seeker.connect_known_node( remote_idurl=broker_idurl, service_name='service_message_broker', service_params=lambda idurl: self._do_prepare_service_request_params( idurl, target_pos, known_brokers, event), request_service_timeout=self.broker_negotiate_ack_timeout * (target_pos + 1), force_handshake=True, attempts=1, ) result.addCallback(self._on_cur_broker_connected, target_pos, self.my_position, self.desired_position, event) if _Debug: result.addErrback(lg.errback, debug=_Debug, debug_level=_DebugLevel, method='broker_negotiator.doRequestCurBroker') result.addErrback(self._on_cur_broker_connect_failed, target_pos, event)
def _do_request_service_one_broker(self, broker_idurl, broker_pos): if _Debug: lg.args(_DebugLevel, broker_pos=broker_pos, broker_idurl=broker_idurl, connecting_brokers=self.connecting_brokers) result = p2p_service_seeker.connect_known_node( remote_idurl=broker_idurl, service_name='service_message_broker', service_params=lambda idurl: self. _do_prepare_service_request_params(idurl, broker_pos), ) result.addCallback(self._on_broker_connected, broker_pos) if _Debug: result.addErrback( lg.errback, debug=_Debug, debug_level=_DebugLevel, method='group_member._do_request_service_one_broker') result.addErrback(self._on_message_broker_connect_failed, broker_pos)
def doRotateRequestCurBroker(self, *args, **kwargs): """ Action method. """ target_pos = self.my_position - 1 broker_idurl = id_url.field(self.cooperated_brokers[target_pos]) known_brokers = {} known_brokers.update(self.cooperated_brokers or {}) known_brokers.update(self.requestor_known_brokers or {}) known_brokers[target_pos] = self.my_broker_idurl if _Debug: lg.args(_DebugLevel, my=self.my_position, target=target_pos, broker=broker_idurl, known=known_brokers) result = p2p_service_seeker.connect_known_node( remote_idurl=broker_idurl, service_name='service_message_broker', service_params=lambda idurl: self._do_prepare_service_request_params( idurl, target_pos, known_brokers, None), request_service_timeout=self.broker_negotiate_ack_timeout * (target_pos + 1), force_handshake=True, attempts=1, ) result.addCallback(self._on_rotate_broker_connected, target_pos, None) if _Debug: result.addErrback( lg.errback, debug=_Debug, debug_level=_DebugLevel, method='broker_negotiator.doRotateRequestCurBroker') result.addErrback(self._on_rotate_broker_connect_failed, target_pos, None)
def doHireNewBroker(self, event, *args, **kwargs): """ Action method. """ known_brokers = {} known_brokers.update(self.cooperated_brokers or {}) known_brokers[self.desired_position] = self.my_broker_idurl target_pos = self.desired_position - 1 exclude_brokers = list( id_url.to_bin_list(filter(None, self.dht_brokers.values()))) exclude_brokers.extend( list( id_url.to_bin_list( filter(None, self.requestor_known_brokers.values())))) preferred_brokers = [] preferred_brokers_raw = config.conf().getData( 'services/message-broker/preferred-brokers').strip() if preferred_brokers_raw: preferred_brokers_list = re.split('\n|,|;| ', preferred_brokers_raw) preferred_brokers.extend(preferred_brokers_list) preferred_brokers = id_url.to_bin_list(preferred_brokers) if preferred_brokers: preferred_brokers = [ x for x in preferred_brokers if x not in exclude_brokers ] if _Debug: lg.args(_DebugLevel, e=event, my=self.my_position, desired=self.desired_position, target=target_pos, exclude=exclude_brokers, preferred=preferred_brokers) if preferred_brokers: preferred_broker_idurl = id_url.field(preferred_brokers[0]) if preferred_broker_idurl and id_url.is_not_in( preferred_broker_idurl, exclude_brokers, as_field=False): result = p2p_service_seeker.connect_known_node( remote_idurl=preferred_broker_idurl, service_name='service_message_broker', service_params=lambda idurl: self ._do_prepare_service_request_params( idurl, target_pos, known_brokers, event), request_service_timeout=self.broker_negotiate_ack_timeout * (target_pos + 1), exclude_nodes=list(exclude_brokers), force_handshake=True, attempts=1, ) result.addCallback(self._on_new_broker_hired, target_pos, self.my_position, self.desired_position) if _Debug: result.addErrback( lg.errback, debug=_Debug, debug_level=_DebugLevel, method='broker_negotiator.doHirePrevBroker') result.addErrback(self._on_new_broker_lookup_failed, target_pos) return result = p2p_service_seeker.connect_random_node( lookup_method=lookup.random_message_broker, service_name='service_message_broker', service_params=lambda idurl: self._do_prepare_service_request_params( idurl, target_pos, known_brokers, event), request_service_timeout=self.broker_negotiate_ack_timeout * (target_pos + 1), exclude_nodes=list(exclude_brokers), attempts=1, force_handshake=True, ) result.addCallback(self._on_new_broker_hired, target_pos, self.my_position, self.desired_position) if _Debug: result.addErrback(lg.errback, debug=_Debug, debug_level=_DebugLevel, method='broker_negotiator.doHirePrevBroker') result.addErrback(self._on_new_broker_lookup_failed, target_pos)
def doConnectFirstBroker(self, *args, **kwargs): """ Action method. """ existing_brokers = args[0] if _Debug: lg.args(_DebugLevel, existing_brokers=existing_brokers) known_brokers = [ None, ] * groups.REQUIRED_BROKERS_COUNT top_broker_pos = None top_broker_idurl = None for broker_pos in range(groups.REQUIRED_BROKERS_COUNT): broker_at_position = None for existing_broker in existing_brokers: if existing_broker[ 'position'] == broker_pos and existing_broker[ 'broker_idurl']: broker_at_position = existing_broker break if not broker_at_position: continue try: broker_idurl = broker_at_position['broker_idurl'] except IndexError: broker_idurl = None if not broker_idurl: lg.warn('broker is empty for %r at position %d' % ( self.group_key_id, broker_pos, )) continue known_brokers[broker_pos] = broker_idurl if _Debug: lg.dbg( _DebugLevel, 'found broker %r at position %r for %r' % ( broker_idurl, broker_pos, self.group_key_id, )) if top_broker_pos is None: top_broker_pos = broker_pos top_broker_idurl = broker_idurl if broker_pos < top_broker_pos: top_broker_pos = broker_pos top_broker_idurl = broker_idurl if _Debug: lg.args(_DebugLevel, known_brokers=known_brokers) if top_broker_idurl is None: lg.info('did not found any existing brokers for %r' % self.group_key_id) self.automat('broker-failed') return if _Debug: lg.args(_DebugLevel, top_broker_pos=top_broker_pos, top_broker_idurl=top_broker_idurl) result = p2p_service_seeker.connect_known_node( remote_idurl=top_broker_idurl, service_name='service_message_broker', service_params=lambda idurl: self. _do_prepare_service_request_params(idurl, top_broker_pos), attempts=1, ) result.addCallback(self._on_broker_connected, top_broker_pos) if _Debug: result.addErrback(lg.errback, debug=_Debug, debug_level=_DebugLevel, method='message_producer.doConnectFirstBroker') result.addErrback(self._on_message_broker_connect_failed, top_broker_pos)