Skip to content
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

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

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, start=True):
alexrallen marked this conversation as resolved.
Show resolved Hide resolved
## Start listen Redis keyspace event. Use start=False if you need to load in initial data to prevent blackout.
alexrallen marked this conversation as resolved.
Show resolved Hide resolved
## Then call process(cache) where cache is a dict of {table_name: data} of the initialized data to prevent
alexrallen marked this conversation as resolved.
Show resolved Hide resolved
## duplicate calls to the callback if the data has not changed.
self.pubsub = self.get_redis_client(self.db_name).pubsub()
self.pubsub.psubscribe("__keyspace@{}__:*".format(self.get_dbid(self.db_name)))

if start: self.process()

def process(self, cache={}):
alexrallen marked this conversation as resolved.
Show resolved Hide resolved
alexrallen marked this conversation as resolved.
Show resolved Hide resolved
alexrallen marked this conversation as resolved.
Show resolved Hide resolved
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 cache and row in cache[table]:
if cache[table][row] == data:
continue
else:
del cache[table][row]
self.__fire(table, row, data)
except ValueError:
pass #Ignore non table-formated redis entries
Expand Down