Skip to content

Commit 2654f4a

Browse files
authored
Fix snmp agent Initialize config DB multiple times issue (#245)
<!-- Please make sure you've read and understood our contributing guidelines; https://github.com/Azure/SONiC/blob/gh-pages/CONTRIBUTING.md ** Make sure all your commits include a signature generated with `git commit -s` ** If this is a bug fix, make sure your description includes "fixes #xxxx", or "closes #xxxx" Please provide the following information: --> **- What I did** Fix following code issue: 1. When initialize SonicDBConfig on multi ASIC device, not check if the global config already initialized issue. 2. Initialize SonicDBConfig multiple times with dupe code. **- How I did it** Code change to check isGlobalInit before load global config. Move dupe code to Namespace.init_sonic_db_config() and initialize config only once. Add new mock method for SonicDBConfig.isGlobalInit **- How to verify it** Pass all UT and E2E test. **- Description for the changelog** <!-- Write a short (one line) summary that describes the changes in this pull request for inclusion in the changelog: -->
1 parent 6bd51c4 commit 2654f4a

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

src/sonic_ax_impl/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import sys
1010

1111
import ax_interface
12-
from sonic_ax_impl.mibs import ieee802_1ab
12+
from sonic_ax_impl.mibs import ieee802_1ab, Namespace
1313
from . import logger
1414
from .mibs.ietf import rfc1213, rfc2737, rfc2863, rfc3433, rfc4292, rfc4363
1515
from .mibs.vendor import dell, cisco
@@ -58,6 +58,8 @@ def main(update_frequency=None):
5858
global event_loop
5959

6060
try:
61+
Namespace.init_sonic_db_config()
62+
6163
# initialize handler and set update frequency (or use the default)
6264
agent = ax_interface.Agent(SonicMIB, update_frequency or DEFAULT_UPDATE_FREQUENCY, event_loop)
6365

src/sonic_ax_impl/mibs/__init__.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,11 @@ def init_db():
218218
Connects to DB
219219
:return: db_conn
220220
"""
221-
if not SonicDBConfig.isInit():
222-
if multi_asic.is_multi_asic():
223-
# Load the global config file database_global.json once.
224-
SonicDBConfig.load_sonic_global_db_config()
225-
else:
226-
SonicDBConfig.load_sonic_db_config()
221+
Namespace.init_sonic_db_config()
222+
227223
# SyncD database connector. THIS MUST BE INITIALIZED ON A PER-THREAD BASIS.
228224
# Redis PubSub objects (such as those within swsssdk) are NOT thread-safe.
229225
db_conn = SonicV2Connector(**redis_kwargs)
230-
231226
return db_conn
232227

233228
def init_mgmt_interface_tables(db_conn):
@@ -536,14 +531,32 @@ def get_oidvalue(self, oid):
536531
return self.oid_map[oid]
537532

538533
class Namespace:
534+
535+
"""
536+
Sonic database initialized flag.
537+
"""
538+
db_config_loaded = False
539+
540+
@staticmethod
541+
def init_sonic_db_config():
542+
"""
543+
Initialize SonicDBConfig
544+
"""
545+
if Namespace.db_config_loaded:
546+
return
547+
548+
if multi_asic.is_multi_asic():
549+
# Load the global config file database_global.json once.
550+
SonicDBConfig.load_sonic_global_db_config()
551+
else:
552+
SonicDBConfig.load_sonic_db_config()
553+
554+
Namespace.db_config_loaded = True
555+
539556
@staticmethod
540557
def init_namespace_dbs():
541558
db_conn = []
542-
if not SonicDBConfig.isInit():
543-
if multi_asic.is_multi_asic():
544-
SonicDBConfig.load_sonic_global_db_config()
545-
else:
546-
SonicDBConfig.load_sonic_db_config()
559+
Namespace.init_sonic_db_config()
547560
host_namespace_idx = 0
548561
for idx, namespace in enumerate(SonicDBConfig.get_ns_list()):
549562
if namespace == multi_asic.DEFAULT_NAMESPACE:

tests/mock_tables/dbconnector.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ def clean_up_config():
2525
SonicDBConfig._sonic_db_global_config_init = False
2626
SonicDBConfig._sonic_db_config_init = False
2727

28+
def mock_SonicDBConfig_isGlobalInit():
29+
return SonicDBConfig._sonic_db_global_config_init
30+
2831

2932
# TODO Convert this to fixture as all Test classes require it.
3033
def load_namespace_config():
@@ -140,6 +143,7 @@ def keys(self, pattern='*'):
140143
SonicV2Connector.connect = connect_SonicV2Connector
141144
swsscommon.SonicV2Connector = SonicV2Connector
142145
swsscommon.SonicDBConfig = SonicDBConfig
146+
swsscommon.SonicDBConfig.isGlobalInit = mock_SonicDBConfig_isGlobalInit
143147

144148
# pytest case collecting will import some module before monkey patch, so reload
145149
from importlib import reload

0 commit comments

Comments
 (0)