Skip to content

Add API endpoints to ConfigDBConnector to support pre-loading data without blackout #587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 30, 2022
Merged
15 changes: 13 additions & 2 deletions common/configdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,16 @@ class ConfigDBConnector_Native : public SonicV2Connector_Native
return self.getDbName()

## Note: callback is difficult to implement by SWIG C++, so keep in python
def listen(self):
## Start listen Redis keyspace events and will trigger corresponding handlers when content of a table changes.
def listen(self, init=None):
## Start listen Redis keyspace event. Pass a callback function to `init` to handle initial table data.
self.pubsub = self.get_redis_client(self.db_name).pubsub()
self.pubsub.psubscribe("__keyspace@{}__:*".format(self.get_dbid(self.db_name)))

init_data = {}
if init:
init_data = {tbl: self.get_table(tbl) for tbl, cb in self.handlers.items()}
init(init_data)

while True:
item = self.pubsub.listen_message()
if item['type'] == 'pmessage':
Expand All @@ -84,6 +90,11 @@ class ConfigDBConnector_Native : public SonicV2Connector_Native
if table in self.handlers:
client = self.get_redis_client(self.db_name)
data = self.raw_to_typed(client.hgetall(key))
if table in init_data and row in init_data[table]:
if init_data[table][row] == data:
continue
else:
del init_data[table][row]
self.__fire(table, row, data)
except ValueError:
pass #Ignore non table-formated redis entries
Expand Down