def _send_db_change_event(self, table, key, action, value, topic): if self.use_pubsub: if not self.enable_selective_topo_dist: topic = SEND_ALL_TOPIC update = DbUpdate(table, key, action, value, topic=topic) self.publisher.send_event(update) eventlet.sleep(0)
def test_pub_sub_event_number_different_port(self): if not self.do_test: return ns = Namespace() ns.events_num = 0 ns.events_action = None def _db_change_callback(table, key, action, value, topic): if 'log' == key: ns.events_num += 1 ns.events_action = action publisher = get_publisher() subscriber = get_subscriber(_db_change_callback) eventlet.sleep(2) local_events_num = ns.events_num action = "log" update = DbUpdate('info', 'log', action, "test ev no diff ports value") publisher.send_event(update) eventlet.sleep(1) self.assertEqual(local_events_num + 1, ns.events_num) self.assertEqual(ns.events_action, action) local_events_num = ns.events_num for i in six.moves.range(100): publisher.send_event(update) eventlet.sleep(0.01) eventlet.sleep(1) self.assertEqual(local_events_num + 100, ns.events_num) subscriber.stop()
def test_pub_sub_add_topic(self): if not self.do_test: return self.events_num_t = 0 self.events_action_t = None def _db_change_callback_topic(table, key, action, value, topic): if 'log' == key: self.events_num_t += 1 self.events_action_t = action publisher = get_publisher() subscriber = get_subscriber(_db_change_callback_topic) eventlet.sleep(2) topic = "topic" subscriber.register_topic(topic) eventlet.sleep(0.5) local_events_num = self.events_num_t action = "log" update = DbUpdate( 'info', 'log', action, "test_pub_sub_add_topic value" ) publisher.send_event(update, topic) eventlet.sleep(1) self.assertEqual(self.events_action_t, action) self.assertEqual(local_events_num + 1, self.events_num_t) no_topic_action = 'log' other_topic = "Other-topic" self.events_action_t = None update = DbUpdate('info', None, no_topic_action, "No topic value") publisher.send_event(update, other_topic) eventlet.sleep(1) self.assertEqual(self.events_action_t, None) self.assertNotEqual(local_events_num + 2, self.events_num_t) subscriber.unregister_topic(topic) publisher.send_event(update, topic) self.assertEqual(self.events_action_t, None) subscriber.stop()
def test_subscribe_success(self): pubsub = mock.Mock() self.RedisSubscriberAgent.pub_sub = pubsub update = DbUpdate("router", "key", "action", "value", topic='teststring') event_json = jsonutils.dumps(update.to_dict()) data = pub_sub_api.pack_message(event_json) self.RedisSubscriberAgent.pub_sub.listen.return_value = \ [{'type': 'message', 'data': data}] self.RedisSubscriberAgent.pub_sub.subscribe.return_value = 1 self.RedisSubscriberAgent.pub_sub.unsubscribe.return_value = 1 result = self.RedisSubscriberAgent.register_topic('subscribe') self.assertIsNone(result) result = self.RedisSubscriberAgent.unregister_topic('subscribe') self.RedisSubscriberAgent.db_changes_callback = mock.Mock() self.RedisSubscriberAgent.db_changes_callback.return_value = 1 self.assertIsNone(result)
def test_publish_success(self): client = mock.Mock() self.RedisPublisherAgent.client = client client.publish.return_value = 1 update = DbUpdate("router", "key", "action", "value", topic='teststring') result = self.RedisPublisherAgent.send_event(update, 'teststring') self.assertIsNone(result)
def test_pub_sub_register_addr(self): if not self.do_test: return ns = Namespace() ns.events_num = 0 ns.events_action = None def _db_change_callback(table, key, action, value, topic): if 'log' == key: ns.events_num += 1 ns.events_action = action publisher = get_publisher() subscriber = get_subscriber(_db_change_callback) eventlet.sleep(2) action = "log" update = DbUpdate( 'info', 'log', action, "value" ) update.action = action update.topic = SEND_ALL_TOPIC publisher.send_event(update) eventlet.sleep(1) self.assertEqual(ns.events_action, action) publisher2 = get_server_publisher() uri = '%s://%s:%s' % ( cfg.CONF.df.publisher_transport, '127.0.0.1', cfg.CONF.df.publisher_port) subscriber.register_listen_address(uri) eventlet.sleep(2) update.action = action update.topic = SEND_ALL_TOPIC ns.events_action = None publisher2.send_event(update) eventlet.sleep(1) self.assertEqual(ns.events_action, action)
def setUp(self): super(TestMultiprocPubSub, self).setUp() self.do_test = cfg.CONF.df.enable_df_pub_sub self.key = 'key-{}'.format(random.random()) self.event = DbUpdate( 'info', None, "log", "TestMultiprocPubSub value", topic=SEND_ALL_TOPIC, ) self.subscriber = None
def test_pub_sub_register_addr(self): if not self.do_test: return ns = Namespace() ns.events_num = 0 ns.events_action = None def _db_change_callback(table, key, action, value, topic): if 'log' == key: ns.events_num += 1 ns.events_action = action publisher = get_publisher() subscriber = get_subscriber(_db_change_callback) eventlet.sleep(2) action = "log" update = DbUpdate('info', 'log', action, "value") update.action = action update.topic = SEND_ALL_TOPIC publisher.send_event(update) eventlet.sleep(1) self.assertEqual(ns.events_action, action) publisher2 = get_server_publisher() uri = '%s://%s:%s' % (cfg.CONF.df.publisher_transport, '127.0.0.1', cfg.CONF.df.publisher_port) subscriber.register_listen_address(uri) eventlet.sleep(2) update.action = action update.topic = SEND_ALL_TOPIC ns.events_action = None publisher2.send_event(update) eventlet.sleep(1) self.assertEqual(ns.events_action, action)
def run(self): while True: # fetch cluster topology info every 10 sec eventlet.sleep(10) try: nodes = self.get_cluster_topology_by_all_nodes() if len(nodes) > 0: if self.publisher is not None: nodes_json = jsonutils.dumps(nodes) update = DbUpdate('ha', 'nodes', 'set', nodes_json, topic='redis') self.publisher.send_event(update) # process new nodes got self.redis_failover_callback(nodes) except Exception as e: LOG.exception( _LE("exception happened " "when receive messages from plugin, " "%(e)s") % {'e': e})
def db_change_callback(self, table, key, action, value, topic=None): update = DbUpdate(table, key, action, value, topic=topic) LOG.info(_LI("Pushing Update to Queue: %s"), update) self._queue.put(update) eventlet.sleep(0)