Skip to content

Commit

Permalink
[SNMP Counters]: Update interface list periodically (sonic-net#24)
Browse files Browse the repository at this point in the history
* Extract init_counters_db from init_sync_d_interface_tables

* Init DB, switch to counters DB when we need this

* Introduce reinit_data method inside of MIBUpdater. This method would be called every 12 updates periods

* Fix trivial error

* Fix issue with omitted db_conn parameter for sync_d synchronization
  • Loading branch information
pavel-shirshov authored May 18, 2017
1 parent 74e57b8 commit e8e1e70
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 33 deletions.
18 changes: 18 additions & 0 deletions src/ax_interface/mib.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"""
DEFAULT_UPDATE_FREQUENCY = 5

"""
Interval between reinit runs (in seconds).
"""
DEFAULT_REINIT_RATE = 60

class MIBUpdater:
"""
Expand All @@ -20,16 +24,30 @@ class MIBUpdater:
def __init__(self):
self.run_event = asyncio.Event()
self.frequency = DEFAULT_UPDATE_FREQUENCY
self.update_counter = 0
self.reinit_rate = DEFAULT_REINIT_RATE // DEFAULT_UPDATE_FREQUENCY

async def start(self):
# Run the update while we are allowed
while self.run_event.is_set():
# reinit internal structures
if self.update_counter > self.reinit_rate:
self.reinit_data()
self.update_counter = 0
else:
self.update_counter += 1
# run the background update task
self.update_data()
# wait based on our update frequency before executing again.
# randomize to avoid concurrent update storms.
await asyncio.sleep(self.frequency + random.randint(-2, 2))

def reinit_data(self):
"""
Reinit task. Children may override this method.
"""
return

def update_data(self):
"""
Background task. Children must override this method.
Expand Down
19 changes: 15 additions & 4 deletions src/sonic_ax_impl/mibs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,25 @@ def config(**kwargs):
global redis_kwargs
redis_kwargs = {k:v for (k,v) in kwargs.items() if k in ['unix_socket_path', 'host', 'port']}

def init_sync_d_interface_tables():
def init_db():
"""
DRY helper method. Connects to and initializes interface maps for SyncD-connected MIB(s).
:return: tuple(db_conn, if_name_map, if_id_map, oid_map, if_alias_map)
Connects to DB
:return: db_conn
"""
# SyncD database connector. THIS MUST BE INITIALIZED ON A PER-THREAD BASIS.
# Redis PubSub objects (such as those within swsssdk) are NOT thread-safe.
db_conn = SonicV2Connector(**redis_kwargs)

return db_conn


def init_sync_d_interface_tables(db_conn):
"""
Initializes interface maps for SyncD-connected MIB(s).
:return: tuple(if_name_map, if_id_map, oid_map, if_alias_map)
"""

# Make sure we're connected to COUNTERS_DB
db_conn.connect(COUNTERS_DB)

# { if_name (SONiC) -> sai_id }
Expand Down Expand Up @@ -133,7 +144,7 @@ def init_sync_d_interface_tables():
logger.warning("No alias map found--port names will use SONiC names.")
if_alias_map = dict(zip(if_name_map.keys(), if_name_map.keys()))

return db_conn, if_name_map, if_alias_map, if_id_map, oid_sai_map, oid_name_map
return if_name_map, if_alias_map, if_id_map, oid_sai_map, oid_name_map


def init_sync_d_lag_tables(db_conn):
Expand Down
28 changes: 17 additions & 11 deletions src/sonic_ax_impl/mibs/ieee802_1ab.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,33 @@ class LLDPUpdater(MIBUpdater):
def __init__(self):
super().__init__()

self.db_conn, \
self.if_name_map, \
self.if_alias_map, \
self.if_id_map, \
self.oid_sai_map, \
self.oid_name_map = mibs.init_sync_d_interface_tables()

# establish connection to application database.
self.db_conn.connect(mibs.APPL_DB)
self.db_conn = mibs.init_db()
self.reinit_data()

# cache of interface counters
# { sai_id -> { 'counter': 'value' } }
self.lldp_counters = {}
self.sai_ids = []
# call our update method once to "seed" data before the "Agent" starts accepting requests.
self.update_data()

def reinit_data(self):
"""
Subclass update interface information
"""
self.if_name_map, \
self.if_alias_map, \
self.if_id_map, \
self.oid_sai_map, \
self.oid_name_map = mibs.init_sync_d_interface_tables(self.db_conn)

def update_data(self):
"""
Subclass update data routine. Updates available sai_ids and LLDP counters.
Subclass update data routine. Updates available LLDP counters.
"""

# establish connection to application database.
self.db_conn.connect(mibs.APPL_DB)

self.lldp_counters = {}
for if_name in self.if_name_map:
lldp_kvs = self.db_conn.get_all(mibs.APPL_DB, mibs.lldp_entry_table(if_name))
Expand Down
18 changes: 12 additions & 6 deletions src/sonic_ax_impl/mibs/ietf/rfc1213.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,8 @@ class IpMib(metaclass=MIBMeta, prefix='.1.3.6.1.2.1.4'):
class InterfacesUpdater(MIBUpdater):
def __init__(self):
super().__init__()
self.db_conn, \
self.if_name_map, \
self.if_alias_map, \
self.if_id_map, \
self.oid_sai_map, \
self.oid_name_map = mibs.init_sync_d_interface_tables()
self.db_conn = mibs.init_db()
self.reinit_data()

self.lag_name_if_name_map = {}
self.if_name_lag_name_map = {}
Expand All @@ -110,6 +106,16 @@ def __init__(self):
# call our update method once to "seed" data before the "Agent" starts accepting requests.
self.update_data()

def reinit_data(self):
"""
Subclass update interface information
"""
self.if_name_map, \
self.if_alias_map, \
self.if_id_map, \
self.oid_sai_map, \
self.oid_name_map = mibs.init_sync_d_interface_tables(self.db_conn)

def update_data(self):
"""
Update redis (caches config)
Expand Down
18 changes: 12 additions & 6 deletions src/sonic_ax_impl/mibs/ietf/rfc2863.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,8 @@ class InterfaceMIBUpdater(MIBUpdater):
def __init__(self):
super().__init__()

self.db_conn, \
self.if_name_map, \
self.if_alias_map, \
self.if_id_map, \
self.oid_sai_map, \
self.oid_name_map = mibs.init_sync_d_interface_tables()
self.db_conn = mibs.init_db()
self.reinit_data()

self.lag_name_if_name_map = {}
self.if_name_lag_name_map = {}
Expand All @@ -63,6 +59,16 @@ def __init__(self):
self.if_range = []
self.update_data()

def reinit_data(self):
"""
Subclass update interface information
"""
self.if_name_map, \
self.if_alias_map, \
self.if_id_map, \
self.oid_sai_map, \
self.oid_name_map = mibs.init_sync_d_interface_tables(self.db_conn)

def update_data(self):
"""
Update redis (caches config)
Expand Down
2 changes: 1 addition & 1 deletion src/sonic_ax_impl/mibs/ietf/rfc4292.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RouteUpdater(MIBUpdater):
def __init__(self):
super().__init__()
self.tos = 0 # ipCidrRouteTos
self.db_conn, _, _, _, _, _ = mibs.init_sync_d_interface_tables()
self.db_conn = mibs.init_db()
self.update_data()

def update_data(self):
Expand Down
15 changes: 10 additions & 5 deletions src/sonic_ax_impl/mibs/ietf/rfc4363.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ def fdb_vlanmac(fdb):
class FdbUpdater(MIBUpdater):
def __init__(self):
super().__init__()
self.db_conn, \
self.db_conn = mibs.init_db()
self.reinit_data()
# call our update method once to "seed" data before the "Agent" starts accepting requests.
self.update_data()

def reinit_data(self):
"""
Subclass update interface information
"""
self.if_name_map, \
self.if_alias_map, \
self.if_id_map, \
self.oid_sai_map, \
self.oid_name_map = mibs.init_sync_d_interface_tables()
# call our update method once to "seed" data before the "Agent" starts accepting requests.
self.update_data()
self.oid_name_map = mibs.init_sync_d_interface_tables(self.db_conn)

def update_data(self):
"""
Expand Down Expand Up @@ -50,7 +56,6 @@ def update_data(self):
self.vlanmac_ifindex_list.append(vlanmac)
self.vlanmac_ifindex_list.sort()


def fdb_ifindex(self, sub_id):
return self.vlanmac_ifindex_map.get(sub_id, None)

Expand Down

0 comments on commit e8e1e70

Please sign in to comment.