Skip to content

Commit fabb30f

Browse files
authored
Fix swsscommon psubscribe code break in frrcfgd (#13836)
Fix swsscommon psubscribe code break in frrcfgd #### Why I did it Fix frrcfgd psubscribe code break: #13109 The code issue caused by API change when migrate from swsssdk to swsscommon #### How I did it Fix frrcfgd code to use swsscommon psubscribe API. #### How to verify it Pass all UT. Manually check fixed code work correctly.
1 parent 165e33b commit fabb30f

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/sonic-frr-mgmt-framework/frrcfgd/frrcfgd.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,7 @@ class ExtConfigDBConnector(ConfigDBConnector):
14451445
def __init__(self, ns_attrs = None):
14461446
super(ExtConfigDBConnector, self).__init__()
14471447
self.nosort_attrs = ns_attrs if ns_attrs is not None else {}
1448+
self.__listen_thread_running = False
14481449
def raw_to_typed(self, raw_data, table = ''):
14491450
if len(raw_data) == 0:
14501451
raw_data = None
@@ -1469,12 +1470,28 @@ def sub_msg_handler(self, msg_item):
14691470
except Exception as e:
14701471
syslog.syslog(syslog.LOG_ERR, '[bgp cfgd] Failed handling config DB update with exception:' + str(e))
14711472
logging.exception(e)
1473+
1474+
def listen_thread(self, timeout):
1475+
self.__listen_thread_running = True
1476+
sub_key_space = "__keyspace@{}__:*".format(self.get_dbid(self.db_name))
1477+
self.pubsub.psubscribe(sub_key_space)
1478+
while self.__listen_thread_running:
1479+
msg = self.pubsub.get_message(timeout, True)
1480+
if msg:
1481+
self.sub_msg_handler(msg)
1482+
1483+
self.pubsub.punsubscribe(sub_key_space)
1484+
14721485
def listen(self):
14731486
"""Start listen Redis keyspace events and will trigger corresponding handlers when content of a table changes.
14741487
"""
14751488
self.pubsub = self.get_redis_client(self.db_name).pubsub()
1476-
self.pubsub.psubscribe(**{"__keyspace@{}__:*".format(self.get_dbid(self.db_name)): self.sub_msg_handler})
1477-
self.sub_thread = self.pubsub.run_in_thread(sleep_time = 0.01)
1489+
self.sub_thread = threading.Thread(target=self.listen_thread, args=(0.01,))
1490+
self.sub_thread.start()
1491+
1492+
def stop_listen(self):
1493+
self.__listen_thread_running = False
1494+
14781495
@staticmethod
14791496
def get_table_key(table, key):
14801497
return table + '&&' + key
@@ -3774,7 +3791,7 @@ def start(self):
37743791
self.subscribe_all()
37753792
self.config_db.listen()
37763793
def stop(self):
3777-
self.config_db.sub_thread.stop()
3794+
self.config_db.stop_listen()
37783795
if self.config_db.sub_thread.is_alive():
37793796
self.config_db.sub_thread.join()
37803797

src/sonic-frr-mgmt-framework/tests/test_config.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def test_contructor():
1414
for table, hdlr in daemon.table_handler_list:
1515
daemon.config_db.subscribe.assert_any_call(table, hdlr)
1616
daemon.config_db.pubsub.psubscribe.assert_called_once()
17+
assert(daemon.config_db.sub_thread.is_alive() == True)
1718
daemon.stop()
18-
daemon.config_db.sub_thread.stop.assert_called()
19-
daemon.config_db.sub_thread.is_alive.assert_called_once()
20-
daemon.config_db.sub_thread.join.assert_called_once()
19+
daemon.config_db.pubsub.punsubscribe.assert_called_once()
20+
assert(daemon.config_db.sub_thread.is_alive() == False)
2121

2222
class CmdMapTestInfo:
2323
data_buf = {}

0 commit comments

Comments
 (0)