Skip to content

Commit

Permalink
[eeprom_tlv_info] Optimize EEPROM data process by using visitor patte…
Browse files Browse the repository at this point in the history
…rn (#193)

Remove EEPROM cache file and use DB instead

1. Use visitor pattern accessing EEPROM data
2. Provide utility functions to access redis data base
  • Loading branch information
Junchao-Mellanox committed Jun 11, 2021
1 parent 295b68c commit fc2e9e2
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 104 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ sonic_platform_common.egg-info/
# Unit test / coverage reports
.coverage
htmlcov/
coverage.xml
test-results.xml
17 changes: 17 additions & 0 deletions sonic_platform_base/sonic_eeprom/eeprom_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def __init__(self, path, format, start, status, readonly):
self.s = start
self.u = status
self.r = readonly
# Warning: the following members are deprecated, the parsed EEPROM data is stored in the
# Redis STATE_DB, cached data should be fetched from STATE_DB.EEPROM_INFO.
self.cache_name = None
self.cache_update_needed = False
self.lock_file = None
Expand All @@ -47,6 +49,9 @@ def check_status(self):
return 'ok'

def set_cache_name(self, name):
# Warning: this method is deprecated, the parsed EEPROM data is stored in the
# Redis STATE_DB, cached data should be fetched from STATE_DB.EEPROM_INFO.

# before accessing the eeprom we acquire an exclusive lock on the eeprom file.
# this will prevent a race condition where multiple instances of this app
# could try to update the cache at the same time
Expand Down Expand Up @@ -214,6 +219,9 @@ def open_eeprom(self):
using_eeprom = True
eeprom_file = self.p
try:
# Warning: cache file is deprecated, the parsed EEPROM data is stored in the
# Redis STATE_DB, cached data should be fetched from STATE_DB.EEPROM_INFO. This
# code need to be adjusted once cache file is completely removing from the system.
if os.path.isfile(self.cache_name):
eeprom_file = self.cache_name
using_eeprom = False
Expand All @@ -240,6 +248,9 @@ def read_eeprom_bytes(self, byteCount, offset=0):
# expect, the file may be corrupt. Delete it and try again, this
# time reading from the actual EEPROM.
if len(o) != byteCount and not self.cache_update_needed:
# Warning: cache file is deprecated, the parsed EEPROM data is stored in the
# Redis STATE_DB, cached data should be fetched from STATE_DB.EEPROM_INFO. This
# code needs to be adjusted once cache file is completely removed from the system.
os.remove(self.cache_name)
self.cache_update_needed = True
F.close()
Expand Down Expand Up @@ -277,6 +288,9 @@ def write_eeprom(self, e):
self.write_cache(e)

def write_cache(self, e):
# Warning: this method is deprecated, the parsed EEPROM data is stored in the
# Redis STATE_DB, cached data should be fetched from STATE_DB.EEPROM_INFO.

if self.cache_name:
F = None
try:
Expand All @@ -290,6 +304,9 @@ def write_cache(self, e):
F.close()

def update_cache(self, e):
# Warning: this method is deprecated, the parsed EEPROM data is stored in the
# Redis STATE_DB, cached data should be fetched from STATE_DB.EEPROM_INFO.

if self.cache_update_needed:
self.write_cache(e)
fcntl.flock(self.lock_file, fcntl.LOCK_UN)
Expand Down
Loading

0 comments on commit fc2e9e2

Please sign in to comment.