Skip to content

Commit

Permalink
[syncd]: Refactor mutexes (sonic-net#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik authored and Shuotian Cheng committed Sep 14, 2017
1 parent 257b139 commit 4d7deba
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 60 deletions.
30 changes: 19 additions & 11 deletions syncd/syncd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@
#include "swss/tokenize.h"
#include <limits.h>

std::mutex g_db_mutex;
/**
* @brief Global mutex for thread synchronization
*
* Purpose of this mutex is to synchronize multiple threads like main thread,
* counters and notifications as well as all operations which require multiple
* Redis DB access.
*
* For example: query DB for next VID id number, and then put map RID and VID
* to Redis. From syncd point of view this entire operation should be atomic
* and no other thread should access DB or make assumption on previous
* information until entire operation will finish.
*/
std::mutex g_mutex;

swss::RedisClient *g_redisClient = NULL;

Expand Down Expand Up @@ -148,7 +160,6 @@ void remove_rid_and_vid_from_local(
sai_object_id_t translate_rid_to_vid(
_In_ sai_object_id_t rid)
{
std::lock_guard<std::mutex> lock(g_db_mutex);

SWSS_LOG_ENTER();

Expand Down Expand Up @@ -277,7 +288,6 @@ void translate_rid_to_vid_list(
sai_object_id_t translate_vid_to_rid(
_In_ sai_object_id_t vid)
{
std::lock_guard<std::mutex> lock(g_db_mutex);

SWSS_LOG_ENTER();

Expand Down Expand Up @@ -401,7 +411,6 @@ void snoop_get_attr(

SWSS_LOG_DEBUG("%s", key.c_str());

std::lock_guard<std::mutex> lock(g_db_mutex);

g_redisClient->hset(key, attr_id, attr_value);
}
Expand Down Expand Up @@ -589,7 +598,6 @@ void internal_syncd_get_send(
// object type and object id, only get status is required
// get response will not put any data to table only queue is used

std::lock_guard<std::mutex> lock(g_db_mutex);

getResponse->set(key, entry, "getresponse");

Expand Down Expand Up @@ -707,7 +715,6 @@ sai_status_t handle_generic(
std::string str_vid = sai_serialize_object_id(object_id);
std::string str_rid = sai_serialize_object_id(real_object_id);

std::lock_guard<std::mutex> lock(g_db_mutex);

g_redisClient->hset(VIDTORID, str_vid, str_rid);
g_redisClient->hset(RIDTOVID, str_rid, str_vid);
Expand Down Expand Up @@ -741,7 +748,6 @@ sai_status_t handle_generic(
std::string str_vid = sai_serialize_object_id(object_id);
std::string str_rid = sai_serialize_object_id(rid);

std::lock_guard<std::mutex> lock(g_db_mutex);

g_redisClient->hdel(VIDTORID, str_vid);
g_redisClient->hdel(RIDTOVID, str_rid);
Expand Down Expand Up @@ -984,7 +990,6 @@ void sendResponse(sai_status_t status)

SWSS_LOG_NOTICE("sending response: %s", str_status.c_str());

std::lock_guard<std::mutex> lock(g_db_mutex);

getResponse->set(str_status, entry, "notify");
}
Expand All @@ -1001,7 +1006,6 @@ void clearTempView()

// TODO optimize with lua script (this takes ~0.2s now)

std::lock_guard<std::mutex> lock(g_db_mutex);

for (const auto &key: g_redisClient->keys(pattern))
{
Expand Down Expand Up @@ -1396,6 +1400,8 @@ sai_status_t processBulkEvent(

sai_status_t processEvent(swss::ConsumerTable &consumer)
{
std::lock_guard<std::mutex> lock(g_mutex);

SWSS_LOG_ENTER();

swss::KeyOpFieldsValuesTuple kco;
Expand Down Expand Up @@ -1799,8 +1805,6 @@ bool handleRestartQuery(swss::NotificationConsumer &restartQuery)

bool isVeryFirstRun()
{
std::lock_guard<std::mutex> lock(g_db_mutex);

SWSS_LOG_ENTER();

// if lane map is not defined in redis db then
Expand Down Expand Up @@ -1977,6 +1981,8 @@ int main(int argc, char **argv)
startCountersThread(options.countersThreadIntervalInSeconds);
}

startNotificationsProcessingThread();

SWSS_LOG_NOTICE("syncd listening for events");

swss::Select s;
Expand Down Expand Up @@ -2039,5 +2045,7 @@ int main(int argc, char **argv)

stop_cli();

stopNotificationsProcessingThread();

return EXIT_SUCCESS;
}
5 changes: 4 additions & 1 deletion syncd/syncd.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extern "C" {

extern void exit_and_notify(int status) __attribute__ ((__noreturn__));

extern std::mutex g_db_mutex;
extern std::mutex g_mutex;
extern std::set<sai_object_id_t> g_defaultPriorityGroupsRids;
extern std::set<sai_object_id_t> g_defaultSchedulerGroupsRids;
extern std::set<sai_object_id_t> g_defaultQueuesRids;
Expand Down Expand Up @@ -167,4 +167,7 @@ void stop_cli();
sai_status_t applyViewTransition();
sai_status_t syncdApplyView();

void startNotificationsProcessingThread();
void stopNotificationsProcessingThread();

#endif // __SYNCD_H__
6 changes: 3 additions & 3 deletions syncd/syncd_counters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ void collectCounters(swss::Table &countersTable,
const std::vector<sai_port_stat_counter_t> &supportedCounters)
{
// collect counters should be under mutex
// sice configuration can change and we
// since configuration can change and we
// don't want that during counters collection

std::lock_guard<std::mutex> lock(g_mutex);

SWSS_LOG_ENTER();

uint32_t countersSize = (uint32_t)supportedCounters.size();
Expand Down Expand Up @@ -50,8 +52,6 @@ void collectCounters(swss::Table &countersTable,
values.push_back(fvt);
}

std::lock_guard<std::mutex> lock(g_db_mutex);

countersTable.set(strPortId, values, "");
}
}
Expand Down
Loading

0 comments on commit 4d7deba

Please sign in to comment.