-
Notifications
You must be signed in to change notification settings - Fork 729
[orchagent]: Add ZmqRouteServer for concurrent route updates #4564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
venkit-nexthop
wants to merge
2
commits into
sonic-net:master
Choose a base branch
from
venkit-nexthop:orchagent-zmq-route-server
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,111 @@ | ||||||
| #include "zmqrouteorch.h" | ||||||
|
|
||||||
| using namespace swss; | ||||||
| using namespace std; | ||||||
|
|
||||||
| extern int gBatchSize; | ||||||
| extern size_t gMaxBulkSize; | ||||||
|
|
||||||
| ZmqRouteConsumer::ZmqRouteConsumer(ZmqRouteConsumerStateTable *select, Orch *orch, const std::string &name) | ||||||
| : ConsumerBase(select, orch, name) | ||||||
| { | ||||||
| // mqPollThread delivers bursts of tuples through this callback. Stage them | ||||||
| // in the plain m_ingress map under m_toSyncMutex rather than merging into | ||||||
| // m_toSync here; the merge into m_toSync happens on the orch main thread in | ||||||
| // execute(). The eventfd is fired once the staged batch reaches | ||||||
| // gMaxBulkSize (so the main loop has a real batch to drain); otherwise | ||||||
| // mqPollThread fires it once per burst after the burst quiesces. | ||||||
| select->setIngressCallback( | ||||||
| [this, select](const std::vector<std::shared_ptr<KeyOpFieldsValuesTuple>> &kcos) { | ||||||
| std::lock_guard<std::mutex> lk(m_toSyncMutex); | ||||||
| for (const auto &kco : kcos) | ||||||
| { | ||||||
| // Plain last-writer-wins staging by key. The SyncMap merge into | ||||||
| // m_toSync is applied later by execute()'s addToSync(). | ||||||
| m_ingress[kfvKey(*kco)] = *kco; | ||||||
| } | ||||||
| if (m_ingress.size() >= gMaxBulkSize) | ||||||
| { | ||||||
| select->notifyPending(); | ||||||
| } | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| void ZmqRouteConsumer::execute() | ||||||
| { | ||||||
| SWSS_LOG_ENTER(); | ||||||
|
|
||||||
| { | ||||||
| // Drain the staged tuples into m_toSync under the lock, mirroring | ||||||
| // ZmqConsumer::execute()'s pops() + addToSync(entries). The lock is | ||||||
| // held only while moving tuples out of m_ingress. | ||||||
| std::lock_guard<std::mutex> lk(m_toSyncMutex); | ||||||
| std::deque<KeyOpFieldsValuesTuple> entries; | ||||||
| for (auto &kv : m_ingress) | ||||||
| { | ||||||
| entries.push_back(std::move(kv.second)); | ||||||
| } | ||||||
| m_ingress.clear(); | ||||||
| addToSync(entries); | ||||||
| } | ||||||
|
|
||||||
| // m_toSync is mutated only by this (main) thread, so drain() — which reads | ||||||
| // m_toSync and hands it to doTask — does not need to hold m_toSyncMutex. | ||||||
| drain(); | ||||||
| } | ||||||
|
|
||||||
| void ZmqRouteConsumer::drain() | ||||||
| { | ||||||
| if (!m_toSync.empty()) | ||||||
| (static_cast<ZmqRouteOrch*>(m_orch))->doTask(*this); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| ZmqRouteOrch::ZmqRouteOrch(DBConnector *db, const vector<string> &tableNames, ZmqRouteServer *zmqServer) | ||||||
| : Orch() | ||||||
| { | ||||||
| for (auto it : tableNames) | ||||||
| { | ||||||
| addConsumer(db, it, default_orch_pri, zmqServer); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| ZmqRouteOrch::ZmqRouteOrch(DBConnector *db, const vector<table_name_with_pri_t> &tableNames_with_pri, ZmqRouteServer *zmqServer) | ||||||
| { | ||||||
| for (const auto& it : tableNames_with_pri) | ||||||
| { | ||||||
| addConsumer(db, it.first, it.second, zmqServer); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| void ZmqRouteOrch::addConsumer(DBConnector *db, string tableName, int pri, ZmqRouteServer *zmqServer) | ||||||
| { | ||||||
| if (db->getDbId() == APPL_DB || db->getDbId() == DPU_APPL_DB) | ||||||
| { | ||||||
| if (zmqServer != nullptr) | ||||||
| { | ||||||
| SWSS_LOG_DEBUG("ZmqRouteConsumer initialize for: %s", tableName.c_str()); | ||||||
| addExecutor( | ||||||
| new ZmqRouteConsumer( | ||||||
| new ZmqRouteConsumerStateTable( | ||||||
| db, tableName, *zmqServer, pri, /* dbPersistence= */false), | ||||||
| this, tableName)); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| SWSS_LOG_DEBUG("Consumer initialize for: %s", tableName.c_str()); | ||||||
| addExecutor(new Consumer(new ConsumerStateTable(db, tableName, gBatchSize, pri), this, tableName)); | ||||||
| } | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| SWSS_LOG_WARN("ZmqRouteOrch does not support create consumer for db: %d, table: %s", db->getDbId(), tableName.c_str()); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| void ZmqRouteOrch::doTask(Consumer &consumer) | ||||||
| { | ||||||
| // When ZMQ disabled, forward data from Consumer | ||||||
| doTask((ConsumerBase &)consumer); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #pragma once | ||
|
|
||
| #include <deque> | ||
| #include <memory> | ||
| #include <mutex> | ||
| #include <unordered_map> | ||
| #include <vector> | ||
| #include <string> | ||
| #include <orch.h> | ||
| #include "zmqrouteserver.h" | ||
| #include "zmqrouteconsumerstatetable.h" | ||
|
|
||
| extern int gZmqExecuteTimeQuantaMsecs; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be good to add a comment of TODO on this will be used as we are not referencing this now |
||
|
|
||
| class ZmqRouteConsumer : public ConsumerBase { | ||
| public: | ||
| ZmqRouteConsumer(ZmqRouteConsumerStateTable *select, Orch *orch, const std::string &name); | ||
|
|
||
| swss::TableBase *getConsumerTable() const override | ||
| { | ||
| // ZmqRouteConsumerStateTable is a subclass of TableBase | ||
| return static_cast<ZmqRouteConsumerStateTable *>(getSelectable()); | ||
| } | ||
|
|
||
| void execute() override; | ||
| void drain() override; | ||
|
|
||
| private: | ||
| // Staging buffer for tuples delivered by the ZmqRouteServer mqPollThread | ||
| // ingress callback. The callback writes here under m_toSyncMutex (rather | ||
| // than merging into m_toSync directly); execute() drains it into m_toSync | ||
| // under the same lock. This keeps m_toSync single-threaded (touched only | ||
| // by the orch main thread), so the base ConsumerBase paths need no locking. | ||
| std::mutex m_toSyncMutex; | ||
| std::unordered_map<std::string, swss::KeyOpFieldsValuesTuple> m_ingress; | ||
| }; | ||
|
|
||
| class ZmqRouteOrch : public Orch | ||
| { | ||
| public: | ||
| ZmqRouteOrch(swss::DBConnector *db, const std::vector<std::string> &tableNames, ZmqRouteServer *zmqServer); | ||
| ZmqRouteOrch(swss::DBConnector *db, const std::vector<table_name_with_pri_t> &tableNames_with_pri, ZmqRouteServer *zmqServer); | ||
|
|
||
| virtual void doTask(ConsumerBase &consumer) { }; | ||
| void doTask(Consumer &consumer) override; | ||
|
|
||
| private: | ||
| void addConsumer(swss::DBConnector *db, std::string tableName, int pri, ZmqRouteServer *zmqServer); | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deadlock possibility in case of resync where we call consumer.addToSync(x) from inside doTask(ConsumerBase&)