Skip to content

Commit

Permalink
[flow counter] Fix issue: should not compare str with int (sonic-net#…
Browse files Browse the repository at this point in the history
…2001)

*When clearing stats, current flow counter stats is serialized to a file for further compare. All stats value is saved as string value. When doing diff, the loaded stats value shall be convert into int and do the compare. The PR is to fix this issue.
  • Loading branch information
Junchao-Mellanox authored Jan 11, 2022
1 parent a8785a8 commit 626cdc4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions scripts/flow_counters_stat
Original file line number Diff line number Diff line change
Expand Up @@ -230,21 +230,21 @@ class FlowCounterStats(object):
# counter OID is changed.
old_values[-1] = values[-1]
for i in diff_column_positions:
old_values[i] = 0
old_values[i] = '0'
values[i] = ns_diff(values[i], old_values[i])
need_update_cache = True
continue

has_negative_diff = False
for i in diff_column_positions:
# If any diff has negative value, set all counter values to 0 and update cache
if values[i] < old_values[i]:
if int(values[i]) < int(old_values[i]):
has_negative_diff = True
break

if has_negative_diff:
for i in diff_column_positions:
old_values[i] = 0
old_values[i] = '0'
values[i] = ns_diff(values[i], old_values[i])
need_update_cache = True
continue
Expand Down
18 changes: 9 additions & 9 deletions tests/flow_counter_stats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,25 @@ def test_diff(self):
stats._collect = mock.MagicMock()
old_data = {
'': {
'bgp': [100, 200, 50.0, 1],
'bgpv6': [100, 200, 50.0, 2],
'lldp': [100, 200, 50.0, 3],
'bgp': ['100', '200', '50.0', '1'],
'bgpv6': ['100', '200', '50.0', '2'],
'lldp': ['100', '200', '50.0', '3'],
}
}
stats._save(old_data)
stats.data = {
'': {
'bgp': [100, 200, 50.0, 4],
'bgpv6': [100, 100, 50.0, 2],
'lldp': [200, 300, 50.0, 3],
'bgp': ['100', '200', '50.0', '4'],
'bgpv6': ['100', '100', '50.0', '2'],
'lldp': ['200', '300', '50.0', '3'],
}
}

stats._collect_and_diff()
cached_data = stats._load()
assert cached_data['']['bgp'] == [0, 0, 50.0, 4]
assert cached_data['']['bgpv6'] == [0, 0, 50.0, 2]
assert cached_data['']['lldp'] == [100, 200, 50.0, 3]
assert cached_data['']['bgp'] == ['0', '0', '50.0', '4']
assert cached_data['']['bgpv6'] == ['0', '0', '50.0', '2']
assert cached_data['']['lldp'] == ['100', '200', '50.0', '3']


class TestTrapStatsMultiAsic:
Expand Down

0 comments on commit 626cdc4

Please sign in to comment.