Skip to content

Commit 92976de

Browse files
committed
Revert "DBConnector classes to understand the namespace. (#364)"
This reverts commit 71a3651.
1 parent 71a3651 commit 92976de

11 files changed

+74
-554
lines changed

common/dbconnector.cpp

+59-191
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ using namespace std;
1616

1717
namespace swss {
1818

19-
void SonicDBConfig::parseDatabaseConfig(const string &file,
20-
std::unordered_map<std::string, RedisInstInfo> &inst_entry,
21-
std::unordered_map<std::string, SonicDBInfo> &db_entry,
22-
std::unordered_map<int, std::string> &separator_entry)
19+
void SonicDBConfig::initialize(const string &file)
2320
{
21+
22+
SWSS_LOG_ENTER();
23+
24+
if (m_init)
25+
{
26+
SWSS_LOG_ERROR("SonicDBConfig already initialized");
27+
throw runtime_error("SonicDBConfig already initialized");
28+
}
29+
2430
ifstream i(file);
2531
if (i.good())
2632
{
@@ -34,7 +40,7 @@ void SonicDBConfig::parseDatabaseConfig(const string &file,
3440
string socket = it.value().at("unix_socket_path");
3541
string hostname = it.value().at("hostname");
3642
int port = it.value().at("port");
37-
inst_entry[instName] = {socket, hostname, port};
43+
m_inst_info[instName] = {socket, {hostname, port}};
3844
}
3945

4046
for (auto it = j["DATABASES"].begin(); it!= j["DATABASES"].end(); it++)
@@ -43,12 +49,12 @@ void SonicDBConfig::parseDatabaseConfig(const string &file,
4349
string instName = it.value().at("instance");
4450
int dbId = it.value().at("id");
4551
string separator = it.value().at("separator");
46-
db_entry[dbName] = {instName, dbId, separator};
52+
m_db_info[dbName] = {instName, dbId, separator};
4753

48-
separator_entry.emplace(dbId, separator);
54+
m_db_separator.emplace(dbId, separator);
4955
}
56+
m_init = true;
5057
}
51-
5258
catch (domain_error& e)
5359
{
5460
SWSS_LOG_ERROR("key doesn't exist in json object, NULL value has no iterator >> %s\n", e.what());
@@ -67,152 +73,32 @@ void SonicDBConfig::parseDatabaseConfig(const string &file,
6773
}
6874
}
6975

70-
void SonicDBConfig::initializeGlobalConfig(const string &file)
71-
{
72-
std::string local_file, dir_name, ns_name;
73-
std::unordered_map<std::string, SonicDBInfo> db_entry;
74-
std::unordered_map<std::string, RedisInstInfo> inst_entry;
75-
std::unordered_map<int, std::string> separator_entry;
76-
77-
SWSS_LOG_ENTER();
78-
79-
if (m_global_init)
80-
{
81-
SWSS_LOG_ERROR("SonicDBConfig Global config is already initialized");
82-
return;
83-
}
84-
85-
86-
ifstream i(file);
87-
if (i.good())
88-
{
89-
local_file = dir_name = std::string();
90-
91-
// Get the directory name from the file path given as input.
92-
std::string::size_type pos = file.rfind("/");
93-
if( pos != std::string::npos)
94-
{
95-
dir_name = file.substr(0,pos+1);
96-
}
97-
98-
try
99-
{
100-
json j;
101-
i >> j;
102-
103-
for (auto& element : j["INCLUDES"])
104-
{
105-
local_file.append(dir_name);
106-
local_file.append(element["include"]);
107-
108-
if(element["namespace"].empty())
109-
{
110-
// If database_config.json is already initlized via SonicDBConfig::initialize
111-
// skip initializing it here again.
112-
if(m_init)
113-
{
114-
local_file.clear();
115-
continue;
116-
}
117-
ns_name = EMPTY_NAMESPACE;
118-
}
119-
else
120-
{
121-
ns_name = element["namespace"];
122-
}
123-
124-
parseDatabaseConfig(local_file, inst_entry, db_entry, separator_entry);
125-
m_inst_info[ns_name] = inst_entry;
126-
m_db_info[ns_name] = db_entry;
127-
m_db_separator[ns_name] = separator_entry;
128-
129-
inst_entry.clear();
130-
db_entry.clear();
131-
separator_entry.clear();
132-
local_file.clear();
133-
}
134-
}
135-
136-
catch (domain_error& e)
137-
{
138-
SWSS_LOG_ERROR("key doesn't exist in json object, NULL value has no iterator >> %s\n", e.what());
139-
throw runtime_error("key doesn't exist in json object, NULL value has no iterator >> " + string(e.what()));
140-
}
141-
catch (exception &e)
142-
{
143-
SWSS_LOG_ERROR("Sonic database config file syntax error >> %s\n", e.what());
144-
throw runtime_error("Sonic database config file syntax error >> " + string(e.what()));
145-
}
146-
}
147-
else
148-
{
149-
SWSS_LOG_ERROR("Sonic database global config file doesn't exist at %s\n", file.c_str());
150-
throw runtime_error("Sonic database global config file doesn't exist at " + file);
151-
}
152-
153-
// Set it as the global config file is already parsed and init done.
154-
m_global_init = true;
155-
156-
// Make regular init also done
157-
m_init = true;
158-
}
159-
160-
void SonicDBConfig::initialize(const string &file, const string &nameSpace)
161-
{
162-
std::unordered_map<std::string, SonicDBInfo> db_entry;
163-
std::unordered_map<std::string, RedisInstInfo> inst_entry;
164-
std::unordered_map<int, std::string> separator_entry;
165-
166-
SWSS_LOG_ENTER();
167-
168-
if (m_init)
169-
{
170-
SWSS_LOG_ERROR("SonicDBConfig already initialized");
171-
throw runtime_error("SonicDBConfig already initialized");
172-
}
173-
174-
// namespace string is empty, use the file given as input to parse.
175-
if(nameSpace.empty())
176-
{
177-
parseDatabaseConfig(file, inst_entry, db_entry, separator_entry);
178-
m_inst_info[EMPTY_NAMESPACE] = inst_entry;
179-
m_db_info[EMPTY_NAMESPACE] = db_entry;
180-
m_db_separator[EMPTY_NAMESPACE] = separator_entry;
181-
}
182-
else
183-
// namespace is not empty, use DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE.
184-
initializeGlobalConfig();
185-
186-
// Set it as the config file is already parsed and init done.
187-
m_init = true;
188-
}
189-
190-
string SonicDBConfig::getDbInst(const string &dbName, const string &nameSpace)
76+
string SonicDBConfig::getDbInst(const string &dbName)
19177
{
19278
if (!m_init)
193-
initialize(DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace);
194-
return m_db_info[nameSpace].at(dbName).instName;
79+
initialize();
80+
return m_db_info.at(dbName).instName;
19581
}
19682

197-
int SonicDBConfig::getDbId(const string &dbName, const string &nameSpace)
83+
int SonicDBConfig::getDbId(const string &dbName)
19884
{
19985
if (!m_init)
200-
initialize(DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace);
201-
return m_db_info[nameSpace].at(dbName).dbId;
86+
initialize();
87+
return m_db_info.at(dbName).dbId;
20288
}
20389

204-
string SonicDBConfig::getSeparator(const string &dbName, const string &nameSpace)
90+
string SonicDBConfig::getSeparator(const string &dbName)
20591
{
20692
if (!m_init)
207-
initialize(DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace);
208-
return m_db_info[nameSpace].at(dbName).separator;
93+
initialize();
94+
return m_db_info.at(dbName).separator;
20995
}
21096

211-
string SonicDBConfig::getSeparator(int dbId, const string &nameSpace)
97+
string SonicDBConfig::getSeparator(int dbId)
21298
{
21399
if (!m_init)
214-
initialize(DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace);
215-
return m_db_separator[nameSpace].at(dbId);
100+
initialize();
101+
return m_db_separator.at(dbId);
216102
}
217103

218104
string SonicDBConfig::getSeparator(const DBConnector* db)
@@ -223,61 +109,42 @@ string SonicDBConfig::getSeparator(const DBConnector* db)
223109
}
224110

225111
string dbName = db->getDbName();
226-
string nameSpace = db->getNamespace();
227112
if (dbName.empty())
228113
{
229-
return getSeparator(db->getDbId(), nameSpace);
114+
return getSeparator(db->getDbId());
230115
}
231116
else
232117
{
233-
return getSeparator(dbName, nameSpace);
118+
return getSeparator(dbName);
234119
}
235120
}
236121

237-
string SonicDBConfig::getDbSock(const string &dbName, const string &nameSpace)
122+
string SonicDBConfig::getDbSock(const string &dbName)
238123
{
239124
if (!m_init)
240-
initialize(DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace);
241-
return m_inst_info[nameSpace].at(getDbInst(dbName)).unixSocketPath;
125+
initialize();
126+
return m_inst_info.at(getDbInst(dbName)).first;
242127
}
243128

244-
string SonicDBConfig::getDbHostname(const string &dbName, const string &nameSpace)
129+
string SonicDBConfig::getDbHostname(const string &dbName)
245130
{
246131
if (!m_init)
247-
initialize(DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace);
248-
return m_inst_info[nameSpace].at(getDbInst(dbName)).hostname;
132+
initialize();
133+
return m_inst_info.at(getDbInst(dbName)).second.first;
249134
}
250135

251-
int SonicDBConfig::getDbPort(const string &dbName, const string &nameSpace)
136+
int SonicDBConfig::getDbPort(const string &dbName)
252137
{
253138
if (!m_init)
254-
initialize(DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace);
255-
return m_inst_info[nameSpace].at(getDbInst(dbName)).port;
256-
}
257-
258-
vector<string> SonicDBConfig::getNamespaces()
259-
{
260-
vector<string> list;
261-
262-
if (!m_global_init)
263-
initializeGlobalConfig();
264-
265-
// This API returns back non-empty namespaces.
266-
for (auto it = m_inst_info.cbegin(); it != m_inst_info.cend(); ++it) {
267-
if(!((it->first).empty()))
268-
list.push_back(it->first);
269-
}
270-
271-
return list;
139+
initialize();
140+
return m_inst_info.at(getDbInst(dbName)).second.second;
272141
}
273142

274143
constexpr const char *SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE;
275-
constexpr const char *SonicDBConfig::DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE;
276-
unordered_map<string, unordered_map<string, RedisInstInfo>> SonicDBConfig::m_inst_info;
277-
unordered_map<string, unordered_map<string, SonicDBInfo>> SonicDBConfig::m_db_info;
278-
unordered_map<string, unordered_map<int, string>> SonicDBConfig::m_db_separator;
144+
unordered_map<string, pair<string, pair<string, int>>> SonicDBConfig::m_inst_info;
145+
unordered_map<string, SonicDBInfo> SonicDBConfig::m_db_info;
146+
unordered_map<int, string> SonicDBConfig::m_db_separator;
279147
bool SonicDBConfig::m_init = false;
280-
bool SonicDBConfig::m_global_init = false;
281148

282149
constexpr const char *DBConnector::DEFAULT_UNIXSOCKET;
283150

@@ -297,8 +164,7 @@ DBConnector::~DBConnector()
297164

298165
DBConnector::DBConnector(int dbId, const string& hostname, int port,
299166
unsigned int timeout) :
300-
m_dbId(dbId),
301-
m_namespace(EMPTY_NAMESPACE)
167+
m_dbId(dbId)
302168
{
303169
struct timeval tv = {0, (suseconds_t)timeout * 1000};
304170

@@ -315,8 +181,7 @@ DBConnector::DBConnector(int dbId, const string& hostname, int port,
315181
}
316182

317183
DBConnector::DBConnector(int dbId, const string& unixPath, unsigned int timeout) :
318-
m_dbId(dbId),
319-
m_namespace(EMPTY_NAMESPACE)
184+
m_dbId(dbId)
320185
{
321186
struct timeval tv = {0, (suseconds_t)timeout * 1000};
322187

@@ -327,31 +192,30 @@ DBConnector::DBConnector(int dbId, const string& unixPath, unsigned int timeout)
327192

328193
if (m_conn->err)
329194
throw system_error(make_error_code(errc::address_not_available),
330-
"Unable to connect to redis (unix-socket)");
195+
"Unable to connect to redis (unixs-socket)");
331196

332197
select(this);
333198
}
334199

335-
DBConnector::DBConnector(const string& dbName, unsigned int timeout, bool isTcpConn, const string& nameSpace)
336-
: m_dbId(SonicDBConfig::getDbId(dbName, nameSpace))
200+
DBConnector::DBConnector(const string& dbName, unsigned int timeout, bool isTcpConn)
201+
: m_dbId(SonicDBConfig::getDbId(dbName))
337202
, m_dbName(dbName)
338-
, m_namespace(nameSpace)
339203
{
340204
struct timeval tv = {0, (suseconds_t)timeout * 1000};
341205

342206
if (timeout)
343207
{
344208
if (isTcpConn)
345-
m_conn = redisConnectWithTimeout(SonicDBConfig::getDbHostname(dbName, nameSpace).c_str(), SonicDBConfig::getDbPort(dbName, nameSpace), tv);
209+
m_conn = redisConnectWithTimeout(SonicDBConfig::getDbHostname(dbName).c_str(), SonicDBConfig::getDbPort(dbName), tv);
346210
else
347-
m_conn = redisConnectUnixWithTimeout(SonicDBConfig::getDbSock(dbName, nameSpace).c_str(), tv);
211+
m_conn = redisConnectUnixWithTimeout(SonicDBConfig::getDbSock(dbName).c_str(), tv);
348212
}
349213
else
350214
{
351215
if (isTcpConn)
352-
m_conn = redisConnect(SonicDBConfig::getDbHostname(dbName, nameSpace).c_str(), SonicDBConfig::getDbPort(dbName, nameSpace));
216+
m_conn = redisConnect(SonicDBConfig::getDbHostname(dbName).c_str(), SonicDBConfig::getDbPort(dbName));
353217
else
354-
m_conn = redisConnectUnix(SonicDBConfig::getDbSock(dbName, nameSpace).c_str());
218+
m_conn = redisConnectUnix(SonicDBConfig::getDbSock(dbName).c_str());
355219
}
356220

357221
if (m_conn->err)
@@ -376,15 +240,19 @@ string DBConnector::getDbName() const
376240
return m_dbName;
377241
}
378242

379-
string DBConnector::getNamespace() const
380-
{
381-
return m_namespace;
382-
}
383-
384243
DBConnector *DBConnector::newConnector(unsigned int timeout) const
385244
{
386245
DBConnector *ret;
387-
ret = new DBConnector(getDbName(), timeout, (getContext()->connection_type == REDIS_CONN_TCP), getNamespace());
246+
if (getContext()->connection_type == REDIS_CONN_TCP)
247+
ret = new DBConnector(getDbId(),
248+
getContext()->tcp.host,
249+
getContext()->tcp.port,
250+
timeout);
251+
else
252+
ret = new DBConnector(getDbId(),
253+
getContext()->unix_sock.path,
254+
timeout);
255+
ret->m_dbName = m_dbName;
388256
return ret;
389257
}
390258

0 commit comments

Comments
 (0)