-
Notifications
You must be signed in to change notification settings - Fork 273
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
Support async DB update for both ZMQ producer&consumer table. #821
Merged
liuh-80
merged 6 commits into
sonic-net:master
from
liuh-80:dev/liuh/zmq-producer-writedb
Oct 12, 2023
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
47c1625
Suppory async DB update for both ZMQ producer/consumer table.
liuh-80 6db3886
Fix UT
liuh-80 12fda22
Improve UT and code
liuh-80 46c0ba9
Improve code and UT
liuh-80 86d7b95
Fix PR comments
liuh-80 0281a96
Fix PR comments
liuh-80 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 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 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,114 @@ | ||
#include <string> | ||
#include <deque> | ||
#include <limits> | ||
#include <hiredis/hiredis.h> | ||
#include <pthread.h> | ||
#include "asyncdbupdater.h" | ||
#include "dbconnector.h" | ||
#include "redisselect.h" | ||
#include "redisapi.h" | ||
#include "table.h" | ||
|
||
using namespace std; | ||
|
||
namespace swss { | ||
|
||
AsyncDBUpdater::AsyncDBUpdater(DBConnector *db, const std::string &tableName) | ||
: m_db(db) | ||
, m_tableName(tableName) | ||
{ | ||
m_runThread = true; | ||
m_dbUpdateThread = std::make_shared<std::thread>(&AsyncDBUpdater::dbUpdateThread, this); | ||
|
||
SWSS_LOG_DEBUG("AsyncDBUpdater ctor tableName: %s", tableName.c_str()); | ||
} | ||
|
||
AsyncDBUpdater::~AsyncDBUpdater() | ||
{ | ||
m_runThread = false; | ||
|
||
// notify db update thread exit | ||
m_dbUpdateDataNotifyCv.notify_all(); | ||
m_dbUpdateThread->join(); | ||
} | ||
|
||
void AsyncDBUpdater::update(std::shared_ptr<KeyOpFieldsValuesTuple> pkco) | ||
{ | ||
{ | ||
std::lock_guard<std::mutex> lock(m_dbUpdateDataQueueMutex); | ||
m_dbUpdateDataQueue.push(pkco); | ||
} | ||
|
||
m_dbUpdateDataNotifyCv.notify_all(); | ||
} | ||
|
||
void AsyncDBUpdater::dbUpdateThread() | ||
{ | ||
SWSS_LOG_ENTER(); | ||
SWSS_LOG_NOTICE("dbUpdateThread begin"); | ||
|
||
// Different schedule policy has different min priority | ||
pthread_attr_t attr; | ||
int policy; | ||
pthread_attr_getschedpolicy(&attr, &policy); | ||
int min_priority = sched_get_priority_min(policy); | ||
// Use min priority will block poll thread | ||
pthread_setschedprio(pthread_self(), min_priority + 1); | ||
|
||
// Follow same logic in ConsumerStateTable: every received data will write to 'table'. | ||
DBConnector db(m_db->getDbName(), 0, true); | ||
Table table(&db, m_tableName); | ||
std::mutex cvMutex; | ||
std::unique_lock<std::mutex> cvLock(cvMutex); | ||
|
||
while (m_runThread) | ||
{ | ||
size_t count; | ||
count = queueSize(); | ||
if (count == 0) | ||
{ | ||
// when queue is empty, wait notification, when data come, continue to check queue size again | ||
m_dbUpdateDataNotifyCv.wait(cvLock); | ||
continue; | ||
} | ||
|
||
for (size_t ie = 0; ie < count; ie++) | ||
{ | ||
auto& kco = *(m_dbUpdateDataQueue.front()); | ||
|
||
if (kfvOp(kco) == SET_COMMAND) | ||
{ | ||
auto& values = kfvFieldsValues(kco); | ||
|
||
// Delete entry before Table::set(), because Table::set() does not remove the no longer existed fields from entry. | ||
table.del(kfvKey(kco)); | ||
table.set(kfvKey(kco), values); | ||
} | ||
else if (kfvOp(kco) == DEL_COMMAND) | ||
{ | ||
table.del(kfvKey(kco)); | ||
} | ||
else | ||
{ | ||
SWSS_LOG_ERROR("db: %s, table: %s receive unknown operation: %s", m_db->getDbName().c_str(), m_tableName.c_str(), kfvOp(kco).c_str()); | ||
} | ||
|
||
{ | ||
std::lock_guard<std::mutex> lock(m_dbUpdateDataQueueMutex); | ||
m_dbUpdateDataQueue.pop(); | ||
} | ||
} | ||
} | ||
|
||
SWSS_LOG_DEBUG("AsyncDBUpdater dbUpdateThread end: %s", m_tableName.c_str()); | ||
} | ||
|
||
size_t AsyncDBUpdater::queueSize() | ||
{ | ||
// size() is not thread safe | ||
std::lock_guard<std::mutex> lock(m_dbUpdateDataQueueMutex); | ||
|
||
return m_dbUpdateDataQueue.size(); | ||
} | ||
|
||
} |
This file contains 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,43 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <deque> | ||
#include <condition_variable> | ||
#include "dbconnector.h" | ||
#include "table.h" | ||
|
||
#define MQ_RESPONSE_MAX_COUNT (4*1024*1024) | ||
#define MQ_SIZE 100 | ||
#define MQ_MAX_RETRY 10 | ||
#define MQ_POLL_TIMEOUT (1000) | ||
|
||
namespace swss { | ||
|
||
class AsyncDBUpdater | ||
{ | ||
public: | ||
AsyncDBUpdater(DBConnector *db, const std::string &tableName); | ||
~AsyncDBUpdater(); | ||
|
||
void update(std::shared_ptr<KeyOpFieldsValuesTuple> pkco); | ||
|
||
size_t queueSize(); | ||
private: | ||
void dbUpdateThread(); | ||
|
||
volatile bool m_runThread; | ||
|
||
std::shared_ptr<std::thread> m_dbUpdateThread; | ||
|
||
std::mutex m_dbUpdateDataQueueMutex; | ||
|
||
std::condition_variable m_dbUpdateDataNotifyCv; | ||
|
||
std::queue<std::shared_ptr<KeyOpFieldsValuesTuple>> m_dbUpdateDataQueue; | ||
|
||
DBConnector *m_db; | ||
|
||
std::string m_tableName; | ||
}; | ||
|
||
} |
This file contains 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 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
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.
This MACRO has been defined in asyncdbupdater.h, why redefine it?
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.
Fixed, remove these dupe macro