Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion common/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dist_swsscommon_DATA = $(EXTRA_CONF_DIST)
bin_PROGRAMS = swssloglevel

if DEBUG
DBGFLAGS = -ggdb -DDEBUG
DBGFLAGS = -ggdb -DDEBUG -g -rdynamic
else
DBGFLAGS = -g -DNDEBUG
endif
Expand All @@ -32,7 +32,9 @@ libswsscommon_la_SOURCES = \
redisreply.cpp \
configdb.cpp \
dbconnector.cpp \
dbdecorator.cpp \
dbinterface.cpp \
defaultvalueprovider.cpp \
sonicv2connector.cpp \
table.cpp \
json.cpp \
Expand Down
36 changes: 34 additions & 2 deletions common/configdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,41 @@ using namespace std;
using namespace swss;

ConfigDBConnector_Native::ConfigDBConnector_Native(bool use_unix_socket_path, const char *netns)
: ConfigDBConnector_Native(false, use_unix_socket_path, netns)
{
}

ConfigDBConnector_Native::ConfigDBConnector_Native(bool get_default_value, bool use_unix_socket_path, const char *netns)
: SonicV2Connector_Native(use_unix_socket_path, netns)
, m_table_name_separator("|")
, m_key_separator("|")
, m_get_default_value(get_default_value)
{
// TODO: [Hua] remove this because only for demo#ifdef DEBUG
#ifdef DEBUG
if (DefaultValueProvider::FeatureEnabledByEnvironmentVariable())
{
m_get_default_value = true;
}
#endif
}

void ConfigDBConnector_Native::db_connect(string db_name, bool wait_for_init, bool retry_on)
{
m_db_name = db_name;
m_key_separator = m_table_name_separator = get_db_separator(db_name);
SonicV2Connector_Native::connect(m_db_name, retry_on);

// attach decorator to client
auto& client = get_redis_client(m_db_name);
if (m_get_default_value)
{
auto decorator = ConfigDBReadDecorator::Create(m_table_name_separator);
client.setDBDecorator(decorator);
}

if (wait_for_init)
{
auto& client = get_redis_client(m_db_name);
auto pubsub = client.pubsub();
auto initialized = client.get(INIT_INDICATOR);
if (!initialized || initialized->empty())
Expand Down Expand Up @@ -280,7 +300,12 @@ std::string ConfigDBConnector_Native::getDbName() const
}

ConfigDBPipeConnector_Native::ConfigDBPipeConnector_Native(bool use_unix_socket_path, const char *netns)
: ConfigDBConnector_Native(use_unix_socket_path, netns)
: ConfigDBConnector_Native(false, use_unix_socket_path, netns)
{
}

ConfigDBPipeConnector_Native::ConfigDBPipeConnector_Native(bool get_default_value, bool use_unix_socket_path, const char *netns)
: ConfigDBConnector_Native(get_default_value, use_unix_socket_path, netns)
{
}

Expand Down Expand Up @@ -490,6 +515,13 @@ int ConfigDBPipeConnector_Native::_get_config(DBConnector& client, RedisTransact
string value = r.getChild(i+1)->str;
dataentry.emplace(field, value);
}

// Because run Redis command with pipe not use DBConnector, so need decorate result here.
auto& db_decorator = client.getDBDecorator(ReadDecorator);
if (db_decorator != nullptr)
{
db_decorator->decorate(key, dataentry);
}
}
return cur;
}
Expand Down
16 changes: 15 additions & 1 deletion common/configdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ class ConfigDBConnector_Native : public SonicV2Connector_Native
public:
static constexpr const char *INIT_INDICATOR = "CONFIG_DB_INITIALIZED";

#ifndef SWIG
[[deprecated("Please use ConfigDBConnector_Native(bool use_unix_socket_path = false, const char *netns = "", bool get_default_value = false) instead.")]]
#endif
ConfigDBConnector_Native(bool use_unix_socket_path = false, const char *netns = "");
ConfigDBConnector_Native(bool get_default_value, bool use_unix_socket_path = false, const char *netns = "");

void db_connect(std::string db_name, bool wait_for_init = false, bool retry_on = false);
void connect(bool wait_for_init = true, bool retry_on = false);
Expand All @@ -35,6 +39,7 @@ class ConfigDBConnector_Native : public SonicV2Connector_Native
std::string m_key_separator = "|";

std::string m_db_name;
bool m_get_default_value;
};

#ifdef SWIG
Expand All @@ -48,7 +53,12 @@ class ConfigDBConnector_Native : public SonicV2Connector_Native
raise ValueError('decode_responses must be True if specified, False is not supported')
if namespace is None:
namespace = ''
super(ConfigDBConnector, self).__init__(use_unix_socket_path = use_unix_socket_path, namespace = namespace)

get_default_value = False
if 'get_default_value' in kwargs and kwargs.pop('get_default_value') == True:
get_default_value = True

super(ConfigDBConnector, self).__init__(get_default_value = get_default_value, use_unix_socket_path = use_unix_socket_path, namespace = namespace)

# Trick: to achieve static/instance method "overload", we must use initize the function in ctor
# ref: https://stackoverflow.com/a/28766809/2514803
Expand Down Expand Up @@ -242,7 +252,11 @@ class ConfigDBConnector_Native : public SonicV2Connector_Native
class ConfigDBPipeConnector_Native: public ConfigDBConnector_Native
{
public:
#ifndef SWIG
[[deprecated("Please use ConfigDBPipeConnector_Native(bool get_default_value, bool use_unix_socket_path = false, const char *netns = "") instead.")]]
#endif
ConfigDBPipeConnector_Native(bool use_unix_socket_path = false, const char *netns = "");
ConfigDBPipeConnector_Native(bool get_default_value, bool use_unix_socket_path = false, const char *netns = "");

void set_entry(std::string table, std::string key, const std::map<std::string, std::string>& data) override;
void mod_config(const std::map<std::string, std::map<std::string, std::map<std::string, std::string>>>& data) override;
Expand Down
100 changes: 99 additions & 1 deletion common/dbconnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "logger.h"

#include "common/dbconnector.h"
#include "common/dbdecorator.h"
#include "common/redisreply.h"
#include "common/redisapi.h"
#include "common/pubsub.h"
Expand Down Expand Up @@ -511,6 +512,7 @@ DBConnector::DBConnector(const DBConnector &other)
: RedisContext(other)
, m_dbId(other.m_dbId)
, m_namespace(other.m_namespace)
, m_db_decorators(other.m_db_decorators)
{
select(this);
}
Expand Down Expand Up @@ -607,6 +609,7 @@ DBConnector *DBConnector::newConnector(unsigned int timeout) const

ret->m_dbName = m_dbName;
ret->setNamespace(getNamespace());
ret->m_db_decorators = m_db_decorators;

return ret;
}
Expand Down Expand Up @@ -780,7 +783,15 @@ shared_ptr<string> DBConnector::hget(const string &key, const string &field)

if (reply->type == REDIS_REPLY_NIL)
{
return shared_ptr<string>(NULL);
auto dbdecortor = this->getDBDecorator(ReadDecorator);
if (dbdecortor)
{
return dbdecortor->decorate(key, field);
}
else
{
return shared_ptr<string>(NULL);
}
}

if (reply->type == REDIS_REPLY_STRING)
Expand Down Expand Up @@ -915,3 +926,90 @@ void DBConnector::del(const std::vector<std::string>& keys)

RedisReply r(this, command, REDIS_REPLY_NIL);
}


const std::shared_ptr<DBDecorator> DBConnector::setDBDecorator(std::shared_ptr<DBDecorator> &db_decorator)
{
auto type = db_decorator->type();
auto existed = getDBDecorator(type);
m_db_decorators[type] = db_decorator;
return existed;
}

const std::shared_ptr<DBDecorator> DBConnector::getDBDecorator(swss::DBDecoratorType type) const
{
auto existed = m_db_decorators.find(type);
std::shared_ptr<DBDecorator> existedDecorator = nullptr;
if (existed != m_db_decorators.end()) {
existedDecorator = existed->second;
}

return existedDecorator;
}

const DecoratorMapping &DBConnector::getDBDecorators() const
{
return m_db_decorators;
}

// TODO: Need discussion connector design, remove following code after discussion.
/*
CfgDBConnector::CfgDBConnector(const DBConnector &other, bool getDefaultValue)
: DBConnector(other)
, m_get_default_value(getDefaultValue)
{
if (getDefaultValue)
{
m_db_decorator = make_shared<ConfigDBDecorator>();
}
}

CfgDBConnector::CfgDBConnector(int dbId, const RedisContext &ctx, bool getDefaultValue)
: DBConnector(dbId, ctx)
, m_get_default_value(getDefaultValue)
{
if (getDefaultValue)
{
m_db_decorator = make_shared<ConfigDBDecorator>();
}
}

CfgDBConnector::CfgDBConnector(int dbId, const std::string &hostname, int port, unsigned int timeout, bool getDefaultValue)
: DBConnector(dbId, hostname, port, timeout)
, m_get_default_value(getDefaultValue)
{
if (getDefaultValue)
{
m_db_decorator = make_shared<ConfigDBDecorator>();
}
}

CfgDBConnector::CfgDBConnector(const std::string &dbName, unsigned int timeout, bool getDefaultValue, bool isTcpConn)
: DBConnector(dbName, timeout, isTcpConn)
, m_get_default_value(getDefaultValue)
{
if (getDefaultValue)
{
m_db_decorator = make_shared<ConfigDBDecorator>();
}
}

CfgDBConnector::CfgDBConnector(const std::string &dbName, unsigned int timeout, bool isTcpConn, const std::string &netns, bool getDefaultValue)
: DBConnector(dbName, timeout, isTcpConn, netns)
, m_get_default_value(getDefaultValue)
{
if (getDefaultValue)
{
m_db_decorator = make_shared<ConfigDBDecorator>();
}
}

CfgDBConnector::~CfgDBConnector()
{
}

std::shared_ptr<DBDecorator> CfgDBConnector::getDBDecortor() const
{
return m_db_decorator;
}
*/
41 changes: 41 additions & 0 deletions common/dbconnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <mutex>

#include <hiredis/hiredis.h>
#include "dbdecorator.h"
#include "rediscommand.h"
#include "redisreply.h"
#define EMPTY_NAMESPACE std::string()
Expand All @@ -18,6 +19,8 @@ namespace swss {
class DBConnector;
class PubSub;

typedef std::map<swss::DBDecoratorType, std::shared_ptr<DBDecorator> > DecoratorMapping;

class RedisInstInfo
{
public:
Expand Down Expand Up @@ -243,6 +246,13 @@ class DBConnector : public RedisContext

bool flushdb();

// every DBDecoratorType can ony have 1 decorator, set new one will return old one.
const std::shared_ptr<swss::DBDecorator> setDBDecorator(std::shared_ptr<swss::DBDecorator> &db_decorator);

const std::shared_ptr<swss::DBDecorator> getDBDecorator(swss::DBDecoratorType type) const;

const DecoratorMapping &getDBDecorators() const;

private:
void setNamespace(const std::string &netns);

Expand All @@ -251,6 +261,8 @@ class DBConnector : public RedisContext
std::string m_namespace;

std::string m_shaRedisMulti;

DecoratorMapping m_db_decorators;
};

template <typename ReturnType>
Expand All @@ -276,6 +288,12 @@ void DBConnector::hgetall(const std::string &key, OutputIterator result)
*result = std::make_pair(ctx->element[i]->str, ctx->element[i+1]->str);
++result;
}

auto dbdecortor = this->getDBDecorator(ReadDecorator);
if (dbdecortor)
{
dbdecortor->decorate(key, ctx, result);
}
}
#endif

Expand All @@ -287,5 +305,28 @@ void DBConnector::hmset(const std::string &key, InputIterator start, InputIterat
RedisReply r(this, shmset, REDIS_REPLY_STATUS);
}

/*
// TODO: need more discussion about the API design, use may both want get/not get default vaule when running time
class CfgDBConnector : public DBConnector
{
explicit CfgDBConnector(const DBConnector &other, bool getDefaultValue);
CfgDBConnector(int dbId, const RedisContext &ctx, bool getDefaultValue);
CfgDBConnector(int dbId, const std::string &hostname, int port, unsigned int timeout, bool getDefaultValue);
CfgDBConnector(int dbId, const std::string &unixPath, unsigned int timeout, bool getDefaultValue);
CfgDBConnector(const std::string &dbName, unsigned int timeout, bool getDefaultValue, bool isTcpConn = false);
CfgDBConnector(const std::string &dbName, unsigned int timeout, bool isTcpConn, const std::string &netns, bool getDefaultValue);
CfgDBConnector& operator=(const DBConnector&) = delete;
CfgDBConnector& operator=(const CfgDBConnector&) = delete;

~CfgDBConnector();

std::shared_ptr<DBDecorator> getDBDecortor() const override;

private:
bool m_get_default_value;
std::shared_ptr<DBDecorator> m_db_decorator;
};
*/

}
#endif
Loading