From fd3480371872654c048029a87d5aae37d023791e Mon Sep 17 00:00:00 2001 From: Judy Joseph Date: Tue, 27 Sep 2022 09:07:31 -0700 Subject: [PATCH] Connect to DBs in namespace once in _init_ of FeatureHandler. --- scripts/hostcfgd | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/scripts/hostcfgd b/scripts/hostcfgd index e701deb00b9e..7215dfbcd3b4 100755 --- a/scripts/hostcfgd +++ b/scripts/hostcfgd @@ -197,10 +197,25 @@ class FeatureHandler(object): self._config_db = config_db self._feature_state_table = feature_state_table self._device_config = device_config - self._device_macsec_support = device_info.get_macsec_support_metadata() self._cached_config = {} self.is_multi_npu = device_info.is_multi_npu() self._device_running_config = device_info.get_device_runtime_metadata() + self._device_macsec_support = device_info.get_macsec_support_metadata() + self.ns_cfg_db = {} + self.ns_feature_state_tbl = {} + + # Initlaize Global config that loads all database*.json + if self.is_multi_npu: + SonicDBConfig.initializeGlobalConfig() + namespaces = device_info.get_namespaces() + for ns in namespaces: + #Connect to ConfigDB in each namespace + self.ns_cfg_db[ns] = ConfigDBConnector(namespace=ns) + self.ns_cfg_db[ns].connect(wait_for_init=True, retry_on=True) + + #Connect to stateDB in each namespace + db_conn = DBConnector(STATE_DB, 0, False, ns); + self.ns_feature_state_tbl[ns] = Table(db_conn, 'FEATURE') def handler(self, feature_name, op, feature_cfg): if not feature_cfg: @@ -463,21 +478,15 @@ class FeatureHandler(object): self._config_db.mod_entry('FEATURE', feature.name, {'state': feature.state}) # resync the feature state in CONFIG_DB in namespaces in multi-asic platform - namespaces = device_info.get_namespaces() - for namespace in namespaces: - db = ConfigDBConnector(namespace=namespace) - db.connect() + for ns, db in self.ns_cfg_db.items(): db.mod_entry('FEATURE', feature.name, {'state': feature.state}) def set_feature_state(self, feature, state): self._feature_state_table.set(feature.name, [('state', state)]) # Update the feature state in STATE_DB in namespaces in multi-asic platform - namespaces = device_info.get_namespaces() - for namespace in namespaces: - db_conn = DBConnector(STATE_DB, 0, False, namespace); - feature_state_tbl = Table(db_conn, 'FEATURE') - feature_state_tbl.set(feature.name, [('state', state)]) + for ns, tbl in self.ns_feature_state_tbl.items(): + tbl.set(feature.name, [('state', state)]) class Iptables(object): def __init__(self): @@ -1485,10 +1494,6 @@ class HostConfigDaemon: self.is_multi_npu = device_info.is_multi_npu() - # Initlaize Global config that loads all database*.json - if self.is_multi_npu: - SonicDBConfig.initializeGlobalConfig() - # Initialize AAACfg self.aaacfg = AaaCfg()