def reset(config_path: str): """Remove/Delete all the data/logs that was created by user/testing.""" from cortx.utils.message_bus.error import MessageBusError try: # use gconf to deregister IEM from cortx.utils.message_bus import MessageBusAdmin, MessageBus from cortx.utils.message_bus import MessageProducer Conf.load(GCONF_INDEX, config_path, skip_reload=True) message_bus_backend = Conf.get('config', MSG_BUS_BACKEND_KEY) message_server_endpoints = Conf.get('config',\ f'{EXTERNAL_KEY}>{message_bus_backend}>endpoints') MessageBus.init(message_server_endpoints) mb = MessageBusAdmin(admin_id='reset') message_types_list = mb.list_message_types() if message_types_list: for message_type in message_types_list: producer = MessageProducer(producer_id=message_type,\ message_type=message_type, method='sync') producer.delete() except MessageBusError as e: raise SetupError(e.rc, "Can not reset Message Bus. %s", e) except Exception as e: raise SetupError( errors.ERR_OP_FAILED, "Internal error, can not \ reset Message Bus. %s", e) return 0
def test_delete_messages(self): """ Test Delete """ producer = MessageProducer(producer_id="p1", \ message_type=TestMessage._msg_type, method="sync") producer.delete() unread_count = producer.get_unread_count \ (consumer_group=TestMessage._consumer_group) self.assertEqual(unread_count, 0) self.assertFalse(unread_count > 0)
def test_018_multiple_admins(self): """Test multiple instances of admin interface.""" message_types_list = TestMessageBus._admin.list_message_types() message_types_list.remove(TestMessageBus._message_type) message_types_list.remove('__consumer_offsets') if message_types_list: for message_type in message_types_list: producer = MessageProducer(producer_id=message_type, \ message_type=message_type, method='sync') producer.delete()
def test_send(self): """ Test Send Message. """ messages = [] producer = MessageProducer(TestMessage.message_bus, producer_id='sel', \ message_type='Sel', method='async') self.assertIsNotNone(producer, "Producer not found") for i in range(0, 10): messages.append("This is message" + str(i)) self.assertIsInstance(messages, list) producer.send(messages) producer.send(messages) producer.delete()
class S3CortxMsgBus: def __init__(self): """Init.""" self._producer = None self._consumer = None def setup_producer(self, prod_id, msg_type, method): """Setup producer.""" try: self._producer = MessageProducer(producer_id=prod_id, message_type=msg_type, method=method) except Exception as exception: msg = ("msg_bus setup producer except:%s %s") % ( exception, traceback.format_exc()) return False, msg return True, None def send(self, messages): """Send the constructed message.""" try: self._producer.send(messages) except Exception as exception: msg = ("msg_bus send except:%s %s") % ( exception, traceback.format_exc()) return False, msg return True, None def purge(self): """Purge/Delete all the messages.""" self._producer.delete() def setup_consumer(self, cons_id, group, msg_type, auto_ack, offset): """Setup the consumer.""" try: self._consumer = MessageConsumer(consumer_id=cons_id, \ consumer_group=group, message_types=[msg_type], auto_ack=auto_ack, offset=offset) except Exception as exception: msg = ("msg_bus setup_consumer except:%s %s") % ( exception, traceback.format_exc()) return False, msg return True, None def receive(self, daemon_mode): """Receive the incoming message.""" try: if daemon_mode: #timeout=0 makes it as blocking indefinitely message = self._consumer.receive(timeout=0) else: #timeout = 0.5 sec, by default, which is non-blocking message = self._consumer.receive() except Exception as exception: msg = ("msg_bus receive except:%s %s") % ( exception, traceback.format_exc()) return False, msg return True, message def ack(self): """Ack the received message.""" try: self._consumer.ack() except Exception as exception: msg = ("msg_bus ack except:%s %s") % ( exception, traceback.format_exc()) return False, msg return True, None def count(self, consumer_group): """Get the count of unread messages.""" unread_count = 0 try: unread_count = self._producer.get_unread_count(consumer_group) except: return 0 return unread_count @staticmethod def create_topic(admin_id: str, message_types: list, partitions: int): """create topic.""" mbadmin = MessageBusAdmin(admin_id = admin_id) try: mbadmin.register_message_type(message_types = message_types, partitions = partitions) except Exception as e: if "TOPIC_ALREADY_EXISTS" not in str(e): raise(e) @staticmethod def add_concurrency(admin_id: str, message_type: str, concurrency_count: int): """Increase partition count for given topic.""" mbadmin = MessageBusAdmin(admin_id = admin_id) mbadmin.add_concurrency(message_type = message_type, concurrency_count = concurrency_count) @staticmethod def delete_topic(admin_id: str, message_types: list): """Delete given topic""" mbadmin = MessageBusAdmin(admin_id = admin_id) mbadmin.deregister_message_type(message_types = message_types) @staticmethod def list_topics(admin_id: str): """list all available topics""" mbadmin = MessageBusAdmin(admin_id = admin_id) return mbadmin.list_message_types() @staticmethod def is_topic_exist(admin_id: str, topic_name: str): """retuns true if topic exist else false""" mbadmin = MessageBusAdmin(admin_id = admin_id) if topic_name in mbadmin.list_message_types(): return True return False
class S3CortxMsgBus: def __init__(self): """Init.""" self._message_bus = None self._producer = None self._consumer = None def connect(self): """Connect to Message Bus.""" try: self._message_bus = MessageBus() except Exception as exception: msg = ("msg_bus init except:%s %s") % (exception, traceback.format_exc()) return False, msg return True, None def setup_producer(self, prod_id, msg_type, method): """Setup producer.""" if not self._message_bus: raise Exception("Non Existent Message Bus") try: self._producer = MessageProducer(self._message_bus, \ producer_id=prod_id, message_type=msg_type, method=method) except Exception as exception: msg = ("msg_bus setup producer except:%s %s") % ( exception, traceback.format_exc()) return False, msg return True, None def send(self, messages): """Send the constructed message.""" try: self._producer.send(messages) except Exception as exception: msg = ("msg_bus send except:%s %s") % (exception, traceback.format_exc()) return False, msg return True, None def purge(self): """Purge/Delete all the messages.""" if not self._message_bus: raise Exception("Non Existent Message Bus, Cannot Purge") self._producer.delete() def setup_consumer(self, cons_id, group, msg_type, auto_ack, offset): """Setup the consumer.""" if not self._message_bus: raise Exception("Non Existent Message Bus") try: self._consumer = MessageConsumer(self._message_bus, consumer_id=cons_id, \ consumer_group=group, message_type=[msg_type], auto_ack=auto_ack, offset=offset) except Exception as exception: msg = ("msg_bus setup_consumer except:%s %s") % ( exception, traceback.format_exc()) return False, msg return True, None def receive(self): """Receive the incoming message.""" try: message = self._consumer.receive() except Exception as exception: msg = ("msg_bus receive except:%s %s") % (exception, traceback.format_exc()) return False, msg return True, message def ack(self): """Ack the received message.""" try: self._consumer.ack() except Exception as exception: msg = ("msg_bus ack except:%s %s") % (exception, traceback.format_exc()) return False, msg return True, None