def setUp(self): """ Setup the prerequisit of tests """ print("Setup") ConfigManager.init("test_Cluster_stop_sigterm") self.confstore = ConfigManager.get_confstore() MessageBus.init()
def _get_producer(self): """ Get message bus producer """ message_type = Conf.get(const.HA_GLOBAL_INDEX, f"MONITOR{_DELIM}message_type") producer_id = Conf.get(const.HA_GLOBAL_INDEX, f"MONITOR{_DELIM}producer_id") MessageBus.init() Log.info( f"Getting producer {producer_id} for message type: {message_type}") return MessageBus.get_producer(producer_id, message_type)
def _get_consumer(self) -> MessageBusConsumer: """ Returns an object of MessageBusConsumer class which will listen on cluster_event message type and callback will be executed """ self._consumer_id = Conf.get(const.HA_GLOBAL_INDEX, f'FAULT_TOLERANCE{_DELIM}consumer_id') self._consumer_group = Conf.get(const.HA_GLOBAL_INDEX, f'FAULT_TOLERANCE{_DELIM}consumer_group') self._message_type = Conf.get(const.HA_GLOBAL_INDEX, f'FAULT_TOLERANCE{_DELIM}message_type') MessageBus.init() return MessageBus.get_consumer(consumer_id=self._consumer_id, \ consumer_group=self._consumer_group, \ message_type=self._message_type, \ callback=self.process_message)
def _get_consumer(self): """ Return instace of message bus consumer. """ consumer_id = Conf.get(HA_GLOBAL_INDEX, f"EVENT_MANAGER{_DELIM}consumer_id") consumer_group = Conf.get(HA_GLOBAL_INDEX, f"EVENT_MANAGER{_DELIM}consumer_group") message_type = Conf.get(HA_GLOBAL_INDEX, f"EVENT_MANAGER{_DELIM}message_type") MessageBus.init() return MessageBus.get_consumer(consumer_id=consumer_id, consumer_group=consumer_group, message_type=message_type, callback=self.process_event)
def __init__(self, consumer_id: int, message_type: str, consumer_group: str, event_filter: Filter, event_parser: Parser, subscriber: Subscriber): """ Initalize Watcher class to monitor message bus event. Args: consumer_id (int): Consumer ID for message bus. message_type (str): Message type for getting event. consumer_group (str): Consumer Group of message bus. event_filter (Filter): Filter unused event. event_parser (Parser): Parse event to HealthEvent subscriber (Subscriber): Pass event to Subscriber. """ Log.info(f"Initalizing watcher {message_type}-{str(consumer_id)}") self.consumer_id = consumer_id self.message_type = message_type self.consumer_group = consumer_group self.filter = event_filter self.parser = event_parser self.subscriber = subscriber self._validate() # TBD : Call MessageBus.init() from here if this watcher needs to be used in HA. Reference EOS-26999 self.consumer = MessageBus.get_consumer( consumer_id=str(self.consumer_id), consumer_group=self.consumer_group, message_type=self.message_type, callback=self.process_message)
def _register_for_resp(self): """ Register to wait for a response to the sent request. """ # Unique consumer_group for each actuator response self.consumer_group = self._uuid self.consumer_id = Conf.get(const.HA_GLOBAL_INDEX, f"ACTUATOR_MANAGER{_DELIM}consumer_id") self.resp_message_type = Conf.get( const.HA_GLOBAL_INDEX, f"ACTUATOR_MANAGER{_DELIM}resp_message_type") self.consumer = MessageBus.get_consumer( consumer_id=str(self.consumer_id), consumer_group=self.consumer_group, message_type=self.resp_message_type, callback=self.process_resp, offset="latest", timeout=ACTUATOR_MSG_WAIT_TIME) # Start the thread to listen to response self.consumer.start() Log.debug( f"Waiting to get response on message_type {self.resp_message_type}" )
def _delete_message_type(self, component: str) -> None: """ Deletes the message type created earlier Args: component (str): Component name. """ message_type = EVENT_MANAGER_KEYS.MESSAGE_TYPE_VALUE.value.replace( "<component_id>", component) MessageBus.deregister(message_type) # Remove message type key from confstore message_type_key = EVENT_MANAGER_KEYS.MESSAGE_TYPE_KEY.value.replace( "<component_id>", component) if self._confstore.key_exists(message_type_key): self._confstore.delete(message_type_key) Log.info( f"Unsubscribed component {component} from message_type {message_type}" )
def send_start_cluster_shutdown_message(self): """ Sends the "start_cluster_shutdown" message for cluster stop monitor """ producer_id = "csm_producer" self.message_type = Conf.get( const.HA_GLOBAL_INDEX, f'CLUSTER_STOP_MON{const._DELIM}message_type') self.producer = MessageBus.get_producer(producer_id=producer_id, message_type=self.message_type) self.producer.publish({"start_cluster_shutdown": 1})
def __init__(self, default_log_enable, singleton_check: bool = False): """ Private Constructor. Make initialization work for Event Manager """ if singleton_check is False: raise Exception("Please use EventManager.get_instance() to fetch \ singleton instance of class") if EventManager.__instance is None: EventManager.__instance = self else: raise Exception( "EventManager is singleton class, use EventManager.get_instance()." ) if default_log_enable: ConfigManager.init(const.EVENT_MANAGER_LOG) self._confstore = ConfigManager.get_confstore() self._monitor_rule = MonitorRulesManager() self._default_action = HEALTH_MON_ACTIONS.PUBLISH_ACT.value MessageBus.init()
def setUp(self): """ Setup. """ self.message_type = "test_ha" MessageBus.deregister(self.message_type) # Consumer self.consumer_id = 1 self.consumer_group = "test_ha_group" # Producer producer_id = "ha_producer" self.producer = MessageBus.get_producer(producer_id=producer_id, message_type=self.message_type) self.status = None self.stop_thread = False self.count = 1 self.consumer = MessageBus.get_consumer( consumer_id=self.consumer_id, consumer_group=self.consumer_group, message_type=self.message_type, callback=self._callback)
def _create_message_type(self, component: str) -> str: """ Create and register message type for this component Args: component (str): Component name. Returns: str: Message type. """ # TODO: Separate this logic in another class message_type = EVENT_MANAGER_KEYS.MESSAGE_TYPE_VALUE.value.replace( "<component_id>", component) MessageBus.register(message_type) # Add message type key/value to confstore message_type_key = EVENT_MANAGER_KEYS.MESSAGE_TYPE_KEY.value.replace( "<component_id>", component) if not self._confstore.key_exists(message_type_key): self._confstore.set(message_type_key, message_type) Log.debug(f"Created {message_type} with {message_type_key}") return message_type
def _get_producer(self, component: str) -> object: """ Get Producer object. Args: component (str): Component """ producer_id = const.EVENT_MGR_PRODUCER_ID.replace( "<component_id>", component) message_type_key = EVENT_MANAGER_KEYS.MESSAGE_TYPE_KEY.value.replace( "<component_id>", component) message_type_key_val = self._confstore.get(message_type_key) _, message_type = message_type_key_val.popitem() return MessageBus.get_producer(producer_id, message_type)
def _send_req(self, req): """ Send the created request to "monitor" on message bus Args: req : created actuator request """ self.req_message_type = Conf.get( const.HA_GLOBAL_INDEX, f"ACTUATOR_MANAGER{_DELIM}req_message_type") self.producer_id = Conf.get(const.HA_GLOBAL_INDEX, f"ACTUATOR_MANAGER{_DELIM}producer_id") self.producer = MessageBus.get_producer(self.producer_id, self.req_message_type) Log.debug( f"Publishing request {req} on message_type {self.req_message_type}" ) self.producer.publish(req)
def cleanup_deregister_all_message_types(self): try: cluster_stop_message_type = Conf.get( const.HA_GLOBAL_INDEX, f'CLUSTER_STOP_MON{const._DELIM}message_type') MessageBus.deregister(cluster_stop_message_type) health_message_type = Conf.get( const.HA_GLOBAL_INDEX, f'EVENT_MANAGER{const._DELIM}message_type') MessageBus.deregister(health_message_type) fault_message_type = Conf.get( const.HA_GLOBAL_INDEX, f'FAULT_TOLERANCE{const._DELIM}message_type') MessageBus.deregister(fault_message_type) except Exception as ex: print(f"Exeception during derigister all messages: {ex}")
def tearDown(self): """ destroy. """ MessageBus.deregister(self.message_type)
def _get_producer(self): message_type = Conf.get(const.HA_GLOBAL_INDEX, f"EVENT_MANAGER{_DELIM}message_type") producer_id = Conf.get(const.HA_GLOBAL_INDEX, f"EVENT_MANAGER{_DELIM}producer_id") return MessageBus.get_producer(producer_id, message_type)
def publish(args: argparse.Namespace) -> None: """ publishes the message on the message bus. Args: args: parsed argument conf_store: ConftStoreSearch object """ try: with open(args.file, 'r') as fi: events_dict = json.load(fi) if _events_key in events_dict.keys(): ConfigManager.init(None) MessageBus.init() message_type = Conf.get( const.HA_GLOBAL_INDEX, f'FAULT_TOLERANCE{const._DELIM}message_type') message_producer = MessageBus.get_producer( "health_event_generator", message_type) cluster_id = Conf.get( const.HA_GLOBAL_INDEX, f'COMMON_CONFIG{const._DELIM}cluster_id') site_id = Conf.get(const.HA_GLOBAL_INDEX, f'COMMON_CONFIG{const._DELIM}site_id') rack_id = Conf.get(const.HA_GLOBAL_INDEX, f'COMMON_CONFIG{const._DELIM}rack_id') storageset_id = '1' # TODO: Read from config when available. for _, value in events_dict[_events_key].items(): resource_type = value[_resource_type_key] resource_type_list = Conf.get( const.HA_GLOBAL_INDEX, f"CLUSTER{const._DELIM}resource_type") if resource_type not in resource_type_list: raise Exception( f'Invalid resource_type: {resource_type}') resource_status = value[_resource_status_key] status_supported = False for status in list(HEALTH_STATUSES): if resource_status == status.value: status_supported = True break if status_supported is False: raise Exception( f'Invalid resource_status: {resource_status}') payload = { f'{HealthAttr.SOURCE}': value[_source_key], f'{HealthAttr.CLUSTER_ID}': cluster_id, f'{HealthAttr.SITE_ID}': site_id, f'{HealthAttr.RACK_ID}': rack_id, f'{HealthAttr.STORAGESET_ID}': storageset_id, f'{HealthAttr.NODE_ID}': value[_node_id_key], f'{HealthAttr.RESOURCE_TYPE}': resource_type, f'{HealthAttr.RESOURCE_ID}': value[_resource_id_key], f'{HealthAttr.RESOURCE_STATUS}': resource_status } health_event = HealthEvent(**payload) health_event.set_specific_info(value[_specific_info_key]) print(f"Publishing health event {health_event.json}") message_producer.publish(health_event.json) if _delay_key in events_dict.keys(): print( f"Sleeping for {events_dict[_delay_key]} seconds") time.sleep(events_dict[_delay_key]) except Exception as err: sys.stderr.write(f"Health event generator failed. Error: {err}\n") return errno.EINVAL
print("********Event Publisher********") event_manager = EventManager.get_instance() component = "csm" resource_type = "node:fru:disk" state = "failed" message_type = event_manager.subscribe( 'csm', [SubscribeEvent(resource_type, [state])]) print(f"Subscribed {component}, message type is {message_type}") health_event = HealthEvent("csm", "1", "failed", "fault", "1", "1", "_1", "1", "1", "1", "node", "16215009572", "1", None) action_event = RecoveryActionEvent(health_event) event_manager.publish(action_event.get_event()) print("Consuming the action event") message_consumer = MessageBus.get_consumer( consumer_id="1", consumer_group='test_publisher', message_type=message_type, callback=receive) message_consumer.start() while not MSG: time.sleep(2) print("waiting for msg") message_consumer.stop() unsubscribe = event_manager.unsubscribe( 'csm', [SubscribeEvent(resource_type, [state])]) print(f"Unsubscribed {component}") print("Event Publisher test completed successfully") except Exception as e: print(f"Failed to run event manager publiser test, Error: {e}")