-
Notifications
You must be signed in to change notification settings - Fork 342
Dbconnector namespace support #376
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
Changes from 5 commits
074ad7b
4324228
d287cad
eb26c99
5408b10
67981ce
80cf409
a98ba20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,11 +7,20 @@ | |
| #include <utility> | ||
|
|
||
| #include <hiredis/hiredis.h> | ||
| #define EMPTY_NAMESPACE std::string() | ||
|
|
||
| namespace swss { | ||
|
|
||
| class DBConnector; | ||
|
|
||
| class RedisInstInfo | ||
| { | ||
| public: | ||
| std::string unixSocketPath; | ||
| std::string hostname; | ||
| int port; | ||
| }; | ||
|
|
||
| class SonicDBInfo | ||
| { | ||
| public: | ||
|
|
@@ -24,25 +33,35 @@ class SonicDBConfig | |
| { | ||
| public: | ||
| static void initialize(const std::string &file = DEFAULT_SONIC_DB_CONFIG_FILE); | ||
| static std::string getDbInst(const std::string &dbName); | ||
| static int getDbId(const std::string &dbName); | ||
| static std::string getSeparator(const std::string &dbName); | ||
| static std::string getSeparator(int dbId); | ||
| static void initializeGlobalConfig(const std::string &file = DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE); | ||
| static void validateNamespace(const std::string &nameSpace); | ||
| static std::string getDbInst(const std::string &dbName, const std::string &nameSpace = EMPTY_NAMESPACE); | ||
| static int getDbId(const std::string &dbName, const std::string &nameSpace = EMPTY_NAMESPACE); | ||
| static std::string getSeparator(const std::string &dbName, const std::string &nameSpace = EMPTY_NAMESPACE); | ||
| static std::string getSeparator(int dbId, const std::string &nameSpace = EMPTY_NAMESPACE); | ||
| static std::string getSeparator(const DBConnector* db); | ||
| static std::string getDbSock(const std::string &dbName); | ||
| static std::string getDbHostname(const std::string &dbName); | ||
| static int getDbPort(const std::string &dbName); | ||
| static std::string getDbSock(const std::string &dbName, const std::string &nameSpace = EMPTY_NAMESPACE); | ||
| static std::string getDbHostname(const std::string &dbName, const std::string &nameSpace = EMPTY_NAMESPACE); | ||
| static int getDbPort(const std::string &dbName, const std::string &nameSpace = EMPTY_NAMESPACE); | ||
| static std::vector<std::string> getNamespaces(); | ||
| static bool isInit() { return m_init; }; | ||
| static bool isGlobalInit() { return m_global_init; }; | ||
|
|
||
| private: | ||
| static constexpr const char *DEFAULT_SONIC_DB_CONFIG_FILE = "/var/run/redis/sonic-db/database_config.json"; | ||
| // { instName, { unix_socket_path, {hostname, port} } } | ||
| static std::unordered_map<std::string, std::pair<std::string, std::pair<std::string, int>>> m_inst_info; | ||
| // { dbName, {instName, dbId} } | ||
| static std::unordered_map<std::string, SonicDBInfo> m_db_info; | ||
| // { dbIp, separator } | ||
| static std::unordered_map<int, std::string> m_db_separator; | ||
| static constexpr const char *DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE = "/var/run/redis/sonic-db/database_global.json"; | ||
| // { namespace { instName, { unix_socket_path, hostname, port } } } | ||
| static std::unordered_map<std::string, std::unordered_map<std::string, RedisInstInfo>> m_inst_info; | ||
| // { namespace, { dbName, {instName, dbId, separator} } } | ||
| static std::unordered_map<std::string, std::unordered_map<std::string, SonicDBInfo>> m_db_info; | ||
| // { namespace, { dbId, separator } } | ||
| static std::unordered_map<std::string, std::unordered_map<int, std::string>> m_db_separator; | ||
| static bool m_init; | ||
| static bool m_global_init; | ||
| static void parseDatabaseConfig(const std::string &file, | ||
| std::unordered_map<std::string, RedisInstInfo> &inst_entry, | ||
| std::unordered_map<std::string, SonicDBInfo> &db_entry, | ||
| std::unordered_map<int, std::string> &separator_entry); | ||
| }; | ||
|
|
||
| class DBConnector | ||
|
|
@@ -60,12 +79,14 @@ class DBConnector | |
| DBConnector(int dbId, const std::string &hostname, int port, unsigned int timeout); | ||
| DBConnector(int dbId, const std::string &unixPath, unsigned int timeout); | ||
| DBConnector(const std::string &dbName, unsigned int timeout, bool isTcpConn = false); | ||
| DBConnector(const std::string &dbName, unsigned int timeout, bool isTcpConn, const std::string &nameSpace); | ||
|
||
|
|
||
| ~DBConnector(); | ||
|
|
||
| redisContext *getContext() const; | ||
| int getDbId() const; | ||
| std::string getDbName() const; | ||
| std::string getNamespace() const; | ||
|
|
||
| static void select(DBConnector *db); | ||
|
|
||
|
|
@@ -84,6 +105,7 @@ class DBConnector | |
| redisContext *m_conn; | ||
| int m_dbId; | ||
| std::string m_dbName; | ||
| std::string m_namespace; | ||
| }; | ||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,8 @@ class RedisSelect : public Selectable | |
| bool hasCachedData() override; | ||
| bool initializedWithData() override; | ||
| void updateAfterRead() override; | ||
| int getDbConnectorId() override; | ||
| std::string getDbNamespace() override; | ||
|
Contributor
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. Why they are added? How are they used?
Contributor
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. @qiluo-msft getDbConnectorId() is not being used as of now but it is useful if we want to check on DBId of select return object.
Collaborator
Author
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. another similar use case is in platform ledd daemon |
||
|
|
||
| /* Create a new redisContext, SELECT DB and SUBSCRIBE */ | ||
| void subscribe(DBConnector* db, const std::string &channelName); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,16 @@ class Selectable | |
| return m_priority; | ||
| } | ||
|
|
||
| virtual int getDbConnectorId() | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| virtual std::string getDbNamespace() | ||
| { | ||
| return std::string(); | ||
| } | ||
|
Contributor
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. In concept, Selectable has nothing todo with Redis. We need to remove these virtual functions.
Contributor
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. @qiluo-msft will check and update. |
||
|
|
||
| private: | ||
|
|
||
| friend class Select; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| { | ||
| "INSTANCES": { | ||
| "redis":{ | ||
| "hostname" : "127.0.0.1", | ||
| "port": 6379, | ||
| "unix_socket_path": "/var/run/redis0/redis.sock" | ||
| } | ||
| }, | ||
| "DATABASES" : { | ||
| "APPL_DB" : { | ||
| "id" : 0, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "ASIC_DB" : { | ||
| "id" : 1, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "COUNTERS_DB" : { | ||
| "id" : 2, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "LOGLEVEL_DB" : { | ||
| "id" : 3, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "CONFIG_DB" : { | ||
| "id" : 4, | ||
| "separator": "|", | ||
| "instance" : "redis" | ||
| }, | ||
| "PFC_WD_DB" : { | ||
| "id" : 5, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "FLEX_COUNTER_DB" : { | ||
| "id" : 5, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "STATE_DB" : { | ||
| "id" : 6, | ||
| "separator": "|", | ||
| "instance" : "redis" | ||
| }, | ||
| "SNMP_OVERLAY_DB" : { | ||
| "id" : 7, | ||
| "separator": "|", | ||
| "instance" : "redis" | ||
| }, | ||
| "ASIC_DB2" : { | ||
| "id" : 10, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "COUNTERS_DB2" : { | ||
| "id" : 11, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "FLEX_COUNTER_DB2" : { | ||
| "id" : 12, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "STATE_DB2" : { | ||
| "id" : 13, | ||
| "separator": "|", | ||
| "instance" : "redis" | ||
| }, | ||
| "TEST_DB" : { | ||
| "id" : 15, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| } | ||
| }, | ||
| "VERSION" : "1.0" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| { | ||
| "INSTANCES": { | ||
| "redis":{ | ||
| "hostname" : "127.0.0.1", | ||
| "port": 6379, | ||
| "unix_socket_path": "/var/run/redis1/redis.sock" | ||
| } | ||
| }, | ||
| "DATABASES" : { | ||
| "APPL_DB" : { | ||
| "id" : 0, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "ASIC_DB" : { | ||
| "id" : 1, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "COUNTERS_DB" : { | ||
| "id" : 2, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "LOGLEVEL_DB" : { | ||
| "id" : 3, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "CONFIG_DB" : { | ||
| "id" : 4, | ||
| "separator": "|", | ||
| "instance" : "redis" | ||
| }, | ||
| "PFC_WD_DB" : { | ||
| "id" : 5, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "FLEX_COUNTER_DB" : { | ||
| "id" : 5, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "STATE_DB" : { | ||
| "id" : 6, | ||
| "separator": "|", | ||
| "instance" : "redis" | ||
| }, | ||
| "SNMP_OVERLAY_DB" : { | ||
| "id" : 7, | ||
| "separator": "|", | ||
| "instance" : "redis" | ||
| }, | ||
| "ASIC_DB2" : { | ||
| "id" : 10, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "COUNTERS_DB2" : { | ||
| "id" : 11, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "FLEX_COUNTER_DB2" : { | ||
| "id" : 12, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| }, | ||
| "STATE_DB2" : { | ||
| "id" : 13, | ||
| "separator": "|", | ||
| "instance" : "redis" | ||
| }, | ||
| "TEST_DB" : { | ||
| "id" : 15, | ||
| "separator": ":", | ||
| "instance" : "redis" | ||
| } | ||
| }, | ||
| "VERSION" : "1.0" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "INCLUDES" : [ | ||
| { | ||
| "include" : "database_config.json" | ||
| }, | ||
| { | ||
| "namespace" : "asic0", | ||
| "include" : "../redis_multi_db_ut_config/database_config0.json" | ||
| }, | ||
| { | ||
| "namespace" : "asic1", | ||
| "include" : "../redis_multi_db_ut_config/database_config1.json" | ||
| } | ||
| ], | ||
| "VERSION" : "1.0" | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.