Skip to content

Commit 19ea849

Browse files
authored
route_check.py should not print too many outputs to stdout (#3071)
Otherwise it fills up the pipe and print() will block. When this happens, `route_check.py` will timeout and eventually killed by `monit` after several minutes. #### What I did Add an option in `print_message()` to skip printing. #### How I did it Add an option `write_to_stdout=True`, in `mitigate_installed_not_offloaded_frr_routes()` call `print_message()` with `write_to_stdout=False`. #### How to verify it Manually run route_check.py, verify that the messages were shown in syslog but not in stdout. #### Previous command output (if the output of a command-line utility has changed) #### New command output (if the output of a command-line utility has changed)
1 parent 21b1285 commit 19ea849

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

scripts/route_check.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,12 @@ def set_level(lvl, log_to_syslog):
114114
report_level = syslog.LOG_DEBUG
115115

116116

117-
def print_message(lvl, *args):
117+
def print_message(lvl, *args, write_to_stdout=True):
118118
"""
119119
print and log the message for given level.
120120
:param lvl: Log level for this message as ERR/INFO/DEBUG
121121
:param args: message as list of strings or convertible to string
122+
:param write_to_stdout: print the message to stdout if set to true
122123
:return None
123124
"""
124125
msg = ""
@@ -129,7 +130,8 @@ def print_message(lvl, *args):
129130
break
130131
msg += str(arg)[0:rem_len]
131132

132-
print(msg)
133+
if write_to_stdout:
134+
print(msg)
133135
if write_to_syslog:
134136
syslog.syslog(lvl, msg)
135137

@@ -575,7 +577,7 @@ def mitigate_installed_not_offloaded_frr_routes(missed_frr_rt, rt_appl):
575577
fvs = swsscommon.FieldValuePairs([('err_str', 'SWSS_RC_SUCCESS'), ('protocol', entry['protocol'])])
576578
response_producer.send('SWSS_RC_SUCCESS', entry['prefix'], fvs)
577579

578-
print_message(syslog.LOG_ERR, f'Mitigated route {entry["prefix"]}')
580+
print_message(syslog.LOG_ERR, f'Mitigated route {entry["prefix"]}', write_to_stdout=False)
579581

580582

581583
def get_soc_ips(config_db):

tests/route_check_test.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
from io import StringIO
23
import json
34
import os
45
import logging
@@ -7,7 +8,7 @@
78
import time
89
from sonic_py_common import device_info
910
from unittest.mock import MagicMock, patch
10-
from tests.route_check_test_data import APPL_DB, ARGS, ASIC_DB, CONFIG_DB, DEFAULT_CONFIG_DB, DESCR, OP_DEL, OP_SET, PRE, RESULT, RET, TEST_DATA, UPD, FRR_ROUTES
11+
from tests.route_check_test_data import APPL_DB, ARGS, ASIC_DB, CONFIG_DB, DEFAULT_CONFIG_DB, APPL_STATE_DB, DESCR, OP_DEL, OP_SET, PRE, RESULT, RET, TEST_DATA, UPD, FRR_ROUTES
1112

1213
import pytest
1314

@@ -89,7 +90,7 @@ def hget(self, key, field):
8990
return True, ret
9091

9192

92-
db_conns = {"APPL_DB": APPL_DB, "ASIC_DB": ASIC_DB}
93+
db_conns = {"APPL_DB": APPL_DB, "ASIC_DB": ASIC_DB, "APPL_STATE_DB": APPL_STATE_DB }
9394
def conn_side_effect(arg, _):
9495
return db_conns[arg]
9596

@@ -321,3 +322,11 @@ def test_logging(self):
321322
assert len(msg) == 5
322323
msg = route_check.print_message(syslog.LOG_ERR, "a", "b", "c", "d", "e", "f")
323324
assert len(msg) == 5
325+
326+
def test_mitigate_routes(self, mock_dbs):
327+
missed_frr_rt = [ { 'prefix': '192.168.0.1', 'protocol': 'bgp' } ]
328+
rt_appl = [ '192.168.0.1' ]
329+
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
330+
route_check.mitigate_installed_not_offloaded_frr_routes(missed_frr_rt, rt_appl)
331+
# Verify that the stdout are suppressed in this function
332+
assert not mock_stdout.getvalue()

tests/route_check_test_data.py

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
APPL_DB = 0
55
ASIC_DB = 1
66
CONFIG_DB = 4
7+
APPL_STATE_DB = 14
78
PRE = "pre-value"
89
UPD = "update"
910
FRR_ROUTES = "frr-routes"

0 commit comments

Comments
 (0)