Skip to content

Commit fd0562a

Browse files
[FC] Fix the cache to handle change in stats for the same object type (#3654)
* [FC] Fix the cache to handle the change in stats * Add UT for coverage Signed-off-by: Vivek Reddy <[email protected]> --------- Signed-off-by: Vivek Reddy <[email protected]> Co-authored-by: Sudharsan Dhamal Gopalarathnam <[email protected]>
1 parent db7d939 commit fd0562a

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

orchagent/flex_counter/flex_counter_manager.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,11 @@ struct CachedObjects
188188
counter_keys.pop_back();
189189

190190
startFlexCounterPolling(pending_switch_id, counter_keys, counter_ids, counter_type_it->second);
191-
191+
192+
/* Clear the cached stats and objects after flush */
192193
pending_sai_objects.clear();
194+
pending_counter_stats.clear();
195+
pending_switch_id = SAI_NULL_OBJECT_ID;
193196
}
194197

195198
void cache(sai_object_id_t object_id)
@@ -243,8 +246,12 @@ class FlexCounterCachedManager : public FlexCounterManager
243246
}
244247
else
245248
{
249+
// Either counter_type, counter_ids or switch_id has changed in the new entry
250+
// Flush the old entries and save the new one
251+
// TODO: Improve the logic to read all the entries and group them before flushing
252+
// to reduce number of sairedis calls
246253
flush(group_name, cached_objects);
247-
cached_objects.cache(object_id);
254+
cached_objects.try_cache(object_id, counter_type, counter_stats, effective_switch_id);
248255
}
249256
}
250257

tests/mock_tests/flexcounter_ut.cpp

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ namespace flexcounter_test
10021002
);
10031003

10041004
using namespace mock_orch_test;
1005-
class EniStatFlexCounterTest : public MockOrchTest
1005+
class StandaloneFCTest : public MockOrchTest
10061006
{
10071007
virtual void PostSetUp() {
10081008
_hook_sai_switch_api();
@@ -1013,7 +1013,7 @@ namespace flexcounter_test
10131013
}
10141014
};
10151015

1016-
TEST_F(EniStatFlexCounterTest, TestStatusUpdate)
1016+
TEST_F(StandaloneFCTest, TestEniStatusUpdate)
10171017
{
10181018
/* Add a mock ENI */
10191019
EniEntry tmp_entry;
@@ -1029,4 +1029,72 @@ namespace flexcounter_test
10291029
m_DashOrch->handleFCStatusUpdate(false);
10301030
ASSERT_FALSE(checkFlexCounter(ENI_STAT_COUNTER_FLEX_COUNTER_GROUP, tmp_entry.eni_id, ENI_COUNTER_ID_LIST));
10311031
}
1032+
1033+
TEST_F(StandaloneFCTest, TestCaching)
1034+
{
1035+
mockFlexCounterOperationCallCount = 0;
1036+
1037+
/* Disable traditional FC since caching is only used for FC config through SAIREDIS channel */
1038+
gTraditionalFlexCounter = false;
1039+
FlexCounterTaggedCachedManager<void> port_stat_manager(PORT_STAT_COUNTER_FLEX_COUNTER_GROUP, StatsMode::READ, 1000, false);
1040+
1041+
// Create two port OIDs
1042+
sai_object_id_t port1_oid = 0x100000000000d;
1043+
sai_object_id_t port2_oid = 0x100000000000e;
1044+
sai_object_id_t port3_oid = 0x100000000000f;
1045+
sai_object_id_t port4_oid = 0x1000000000010;
1046+
// Different counter stats for each port
1047+
std::unordered_set<string> type1_stats = {
1048+
"SAI_PORT_STAT_IF_IN_OCTETS",
1049+
"SAI_PORT_STAT_IF_IN_ERRORS"
1050+
};
1051+
std::unordered_set<string> type2_stats = {
1052+
"SAI_PORT_STAT_IF_OUT_OCTETS",
1053+
"SAI_PORT_STAT_IF_OUT_ERRORS"
1054+
};
1055+
1056+
// Set counter IDs for both ports
1057+
port_stat_manager.setCounterIdList(port1_oid, CounterType::PORT, type1_stats);
1058+
port_stat_manager.setCounterIdList(port2_oid, CounterType::PORT, type1_stats);
1059+
port_stat_manager.setCounterIdList(port3_oid, CounterType::PORT, type2_stats);
1060+
port_stat_manager.setCounterIdList(port4_oid, CounterType::PORT, type1_stats);
1061+
1062+
// Flush the counters
1063+
port_stat_manager.flush();
1064+
1065+
/* SAIREDIS channel should have been called thrice, once for port1&port2,port3,port4 */
1066+
ASSERT_EQ(mockFlexCounterOperationCallCount, 3);
1067+
1068+
ASSERT_TRUE(checkFlexCounter(PORT_STAT_COUNTER_FLEX_COUNTER_GROUP, port1_oid,
1069+
{
1070+
{PORT_COUNTER_ID_LIST,
1071+
"SAI_PORT_STAT_IF_IN_OCTETS,"
1072+
"SAI_PORT_STAT_IF_IN_ERRORS"
1073+
}
1074+
}));
1075+
1076+
ASSERT_TRUE(checkFlexCounter(PORT_STAT_COUNTER_FLEX_COUNTER_GROUP, port2_oid,
1077+
{
1078+
{PORT_COUNTER_ID_LIST,
1079+
"SAI_PORT_STAT_IF_IN_OCTETS,"
1080+
"SAI_PORT_STAT_IF_IN_ERRORS"
1081+
}
1082+
}));
1083+
1084+
ASSERT_TRUE(checkFlexCounter(PORT_STAT_COUNTER_FLEX_COUNTER_GROUP, port3_oid,
1085+
{
1086+
{PORT_COUNTER_ID_LIST,
1087+
"SAI_PORT_STAT_IF_OUT_OCTETS,"
1088+
"SAI_PORT_STAT_IF_OUT_ERRORS"
1089+
}
1090+
}));
1091+
1092+
ASSERT_TRUE(checkFlexCounter(PORT_STAT_COUNTER_FLEX_COUNTER_GROUP, port4_oid,
1093+
{
1094+
{PORT_COUNTER_ID_LIST,
1095+
"SAI_PORT_STAT_IF_IN_OCTETS,"
1096+
"SAI_PORT_STAT_IF_IN_ERRORS"
1097+
}
1098+
}));
1099+
}
10321100
}

0 commit comments

Comments
 (0)