|
| 1 | +#ifndef SWSS_RETRY_CACHE_H |
| 2 | +#define SWSS_RETRY_CACHE_H |
| 3 | + |
| 4 | +#include <unordered_set> |
| 5 | +#include <unordered_map> |
| 6 | + |
| 7 | +using namespace swss; |
| 8 | + |
| 9 | +enum ConstraintType |
| 10 | +{ |
| 11 | + RETRY_CST_DUMMY, |
| 12 | + RETRY_CST_NHG, // nhg doesn't exist |
| 13 | + RETRY_CST_NHG_REF, // nhg refcnt nonzero |
| 14 | + RETRY_CST_PIC, // context doesn't exist |
| 15 | + RETRY_CST_PIC_REF, // context refcnt nonzero |
| 16 | + RETRY_CST_ECMP // ecmp resources exhausted |
| 17 | +}; |
| 18 | + |
| 19 | +using ConstraintData = std::string; |
| 20 | +using Constraint = std::pair<ConstraintType, ConstraintData>; |
| 21 | + |
| 22 | +const Constraint DUMMY_CONSTRAINT{RETRY_CST_DUMMY, ""}; |
| 23 | + |
| 24 | +template<typename T = ConstraintData> |
| 25 | +inline Constraint make_constraint(ConstraintType type, T&& data = T()) { |
| 26 | + return {type, std::forward<T>(data)}; |
| 27 | +} |
| 28 | + |
| 29 | +typedef swss::KeyOpFieldsValuesTuple Task; |
| 30 | +typedef std::pair<Constraint, Task> FailedTask; |
| 31 | +typedef std::unordered_map<std::string, FailedTask> RetryMap; |
| 32 | + |
| 33 | +namespace std { |
| 34 | + template<> |
| 35 | + struct hash<::Constraint> { |
| 36 | + std::size_t operator()(const ::Constraint& c) const { |
| 37 | + return hash<::ConstraintType>{}(c.first) ^ |
| 38 | + (hash<::ConstraintData>{}(c.second) << 2); |
| 39 | + } |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +using RetryKeysMap = std::unordered_map<Constraint, std::unordered_set<std::string>>; |
| 44 | + |
| 45 | +class RetryCache |
| 46 | +{ |
| 47 | +public: |
| 48 | + |
| 49 | + // cache the data about the failed tasks for a ConsumerBase instance |
| 50 | + RetryMap m_toRetry; |
| 51 | + RetryKeysMap m_retryKeys; // maps a constraint to a set of failedKeys |
| 52 | + |
| 53 | + bool contains(const std::string &key) { |
| 54 | + return m_toRetry.find(key) != m_toRetry.end(); |
| 55 | + } |
| 56 | + |
| 57 | + /** Insert a failed task with its constraint to m_toRetry and m_retryKeys |
| 58 | + * @param task the task that has failed |
| 59 | + * @param cst constraint needs to be resolved for the task to succeed |
| 60 | + */ |
| 61 | + void insert_failed_task(Task &&task, Constraint &&cst) { |
| 62 | + const auto& key = kfvKey(task); |
| 63 | + m_retryKeys[cst].insert(key); |
| 64 | + m_toRetry.emplace( |
| 65 | + std::piecewise_construct, |
| 66 | + std::forward_as_tuple(key), |
| 67 | + std::forward_as_tuple(std::move(cst), std::move(task)) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** Erase a task from m_toRetry and m_retryKeys |
| 72 | + * @param key key of swss::KeyOpFieldsValuesTuple task |
| 73 | + * @return the task that has failed but is ready for retry |
| 74 | + */ |
| 75 | + std::shared_ptr<Task> erase_failed_task(const std::string &key) { |
| 76 | + |
| 77 | + auto it = m_toRetry.find(key); |
| 78 | + if (it == m_toRetry.end()) |
| 79 | + return std::make_shared<Task>(); |
| 80 | + |
| 81 | + Constraint& cst = it->second.first; |
| 82 | + auto task = std::make_shared<Task>(std::move(it->second.second)); |
| 83 | + |
| 84 | + m_retryKeys[cst].erase(key); |
| 85 | + m_toRetry.erase(key); |
| 86 | + |
| 87 | + if (m_retryKeys[cst].empty()) |
| 88 | + m_retryKeys.erase(cst); |
| 89 | + |
| 90 | + return task; |
| 91 | + } |
| 92 | + |
| 93 | + /** Find cached failed tasks that can be resolved by the constraint, remove them from the retry cache. |
| 94 | + * @param cst the retry constraint |
| 95 | + * @return the resolved failed tasks |
| 96 | + */ |
| 97 | + std::shared_ptr<std::deque<KeyOpFieldsValuesTuple>> resolve(const Constraint &cst) { |
| 98 | + |
| 99 | + auto tasks = std::make_shared<std::deque<KeyOpFieldsValuesTuple>>(); |
| 100 | + |
| 101 | + if (m_retryKeys.find(cst) == m_retryKeys.end()) |
| 102 | + return tasks; |
| 103 | + |
| 104 | + // get a set of keys that correspond to tasks constrained by the cst |
| 105 | + std::unordered_set<std::string>& keys = m_retryKeys[cst]; |
| 106 | + |
| 107 | + for (auto it = keys.begin(); it != keys.end();) |
| 108 | + { |
| 109 | + auto failed_task_it = m_toRetry.find(*it); |
| 110 | + if (failed_task_it != m_toRetry.end()) |
| 111 | + { |
| 112 | + tasks->push_back(std::move(failed_task_it->second.second)); |
| 113 | + m_toRetry.erase(failed_task_it); |
| 114 | + } |
| 115 | + it = keys.erase(it); |
| 116 | + } |
| 117 | + |
| 118 | + m_retryKeys.erase(cst); |
| 119 | + |
| 120 | + return tasks; |
| 121 | + } |
| 122 | +}; |
| 123 | + |
| 124 | +typedef std::unordered_map<std::string, std::shared_ptr<RetryCache>> RetryCacheMap; |
| 125 | + |
| 126 | +#endif /* SWSS_RETRY_CACHE_H */ |
0 commit comments