From b08e588e730ea7f0eadcbe80e5230f44bd6967aa Mon Sep 17 00:00:00 2001 From: Yevhen Fastiuk Date: Mon, 12 Jun 2023 23:22:45 +0300 Subject: [PATCH 1/3] Report ready status to Sysmonitor daemon Signed-off-by: Yevhen Fastiuk --- sonic-xcvrd/xcvrd/xcvrd.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/sonic-xcvrd/xcvrd/xcvrd.py b/sonic-xcvrd/xcvrd/xcvrd.py index 2593eba1e..06666a331 100644 --- a/sonic-xcvrd/xcvrd/xcvrd.py +++ b/sonic-xcvrd/xcvrd/xcvrd.py @@ -244,6 +244,37 @@ def strip_unit_and_beautify(value, unit): return str(value) +def notify_system_ready(fail_status=False, fail_reason='-'): + key = 'FEATURE|pmon' + statusvalue = {} + + try: + state_db = swsscommon.SonicV2Connector() + state_db.connect(state_db.STATE_DB) + except Exception: + helper_logger.log_error('Failed to connect STATE_DB to report ' + f'{SYSLOG_IDENTIFIER} ready status') + return + + if fail_status: + statusvalue['up_status'] = 'false' + statusvalue['fail_reason'] = fail_reason + else: + statusvalue['up_status'] = 'true' + + if getattr(notify_system_ready, 'reported', False): + helper_logger.log_debug( + f'{SYSLOG_IDENTIFIER} ready status already reported. Tried to ' + f'report status: {statusvalue}') + return + + state_db.delete(state_db.STATE_DB, key) + state_db.hmset(state_db.STATE_DB, key, statusvalue) + helper_logger.log_info(f'Report {SYSLOG_IDENTIFIER} ready status: ' + f'{statusvalue}') + notify_system_ready.reported = True + + def _wrapper_get_presence(physical_port): if platform_chassis is not None: try: @@ -2375,6 +2406,11 @@ def retry_eeprom_reading(self): # Update retry EEPROM set self.retry_eeprom_set -= retry_success_set + # If we were here and now retry_eeprom_set is empty, then we can state + # that all SFPs accessible + if not self.retry_eeprom_set: + notify_system_ready() + # # Daemon ======================================================================= From c95de0e6c2efecf3fb2b9b0a574ab8ef5c43c315 Mon Sep 17 00:00:00 2001 From: Yevhen Fastiuk Date: Mon, 20 May 2024 17:32:07 +0300 Subject: [PATCH 2/3] Add notify system ready unit tests Signed-off-by: Yevhen Fastiuk --- sonic-xcvrd/tests/test_xcvrd.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/sonic-xcvrd/tests/test_xcvrd.py b/sonic-xcvrd/tests/test_xcvrd.py index f28052642..5a37e40fe 100644 --- a/sonic-xcvrd/tests/test_xcvrd.py +++ b/sonic-xcvrd/tests/test_xcvrd.py @@ -318,6 +318,23 @@ def test_is_cmis_api(self, mock_class, expected_return_value): mock_xcvr_api.__class__ = mock_class assert is_cmis_api(mock_xcvr_api) == expected_return_value + @patch('swsscommon.swsscommon.SonicV2Connector') + def test_notify_system_ready(self, mock_dbconn): + mock_db = MagicMock() + mock_db.connect = MagicMock() + mock_db.delete = MagicMock() + mock_db.hmset = MagicMock() + mock_dbconn.return_value = mock_db + + # Case 1: Report ready status + notify_system_ready() + mock_db.hmset.assert_called_once() + + # Case 2: Should not report status again + mock_db.hmset.reset_mock() + notify_system_ready() + mock_db.hmset.assert_not_called() + @patch('xcvrd.xcvrd._wrapper_get_sfp_type') @patch('xcvrd.xcvrd_utilities.port_event_helper.PortMapping.logical_port_name_to_physical_port_list', MagicMock(return_value=[0])) @patch('xcvrd.xcvrd._wrapper_get_presence', MagicMock(return_value=True)) From 33c94587be23d6866b7b00416fdd1ef596572a77 Mon Sep 17 00:00:00 2001 From: Yevhen Fastiuk Date: Thu, 15 Aug 2024 02:28:13 +0300 Subject: [PATCH 3/3] Update xcvrd ready events Signed-off-by: Yevhen Fastiuk --- sonic-xcvrd/xcvrd/xcvrd.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sonic-xcvrd/xcvrd/xcvrd.py b/sonic-xcvrd/xcvrd/xcvrd.py index 945b12c6c..85eaf51e4 100644 --- a/sonic-xcvrd/xcvrd/xcvrd.py +++ b/sonic-xcvrd/xcvrd/xcvrd.py @@ -2415,11 +2415,6 @@ def retry_eeprom_reading(self): # Update retry EEPROM set self.retry_eeprom_set -= retry_success_set - # If we were here and now retry_eeprom_set is empty, then we can state - # that all SFPs accessible - if not self.retry_eeprom_set: - notify_system_ready() - # # Daemon ======================================================================= @@ -2596,6 +2591,7 @@ def run(self): for thread in self.threads: self.log_notice("Started thread {}".format(thread.getName())) + notify_system_ready() self.stop_event.wait() self.log_info("Stop daemon main loop") @@ -2611,6 +2607,8 @@ def run(self): generate_sigkill = True if generate_sigkill is True: + # Notify system not ready + notify_system_ready(False, "Exception occured in xcvrd daemon") self.log_error("Exiting main loop as child thread raised exception!") os.kill(os.getpid(), signal.SIGKILL) @@ -2639,6 +2637,8 @@ def run(self): self.log_info("Shutting down...") if self.sfp_error_event.is_set(): + # Notify system not ready + notify_system_ready(False, "SFP system error") sys.exit(SFP_SYSTEM_ERROR)