redis init optimization

This commit is contained in:
Grant Limberg 2020-05-26 18:54:27 -07:00
parent 7bde004c7c
commit 2f0f0e4f53

View File

@ -271,7 +271,7 @@ void PostgreSQL::initializeNetworks(PGconn *conn)
exit(1); exit(1);
} }
std::string setKey = "networks:{" + std::string(_myAddressStr.c_str()) + "}"; std::string setKey = "networks:{" + _myAddressStr + "}";
if (_rc != NULL) { if (_rc != NULL) {
try { try {
@ -286,6 +286,8 @@ void PostgreSQL::initializeNetworks(PGconn *conn)
} }
} }
std::unordered_set<std::string> networkSet;
const char *params[1] = { const char *params[1] = {
_myAddressStr.c_str() _myAddressStr.c_str()
}; };
@ -319,13 +321,7 @@ void PostgreSQL::initializeNetworks(PGconn *conn)
}; };
std::string nwid = PQgetvalue(res, i, 0); std::string nwid = PQgetvalue(res, i, 0);
if (_rc != NULL) { networkSet.insert(nwid);
if (_rc->clusterMode) {
_cluster->sadd(setKey, nwid);
} else {
_redis->sadd(setKey, nwid);
}
}
config["id"] = nwid; config["id"] = nwid;
config["nwid"] = nwid; config["nwid"] = nwid;
@ -441,6 +437,16 @@ void PostgreSQL::initializeNetworks(PGconn *conn)
PQclear(res); PQclear(res);
if (_rc && _rc->clusterMode) {
auto tx = _cluster->transaction(_myAddressStr, true);
tx.sadd(setKey, networkSet.begin(), networkSet.end());
tx.exec();
} else if (_rc && !_rc->clusterMode) {
auto tx = _redis->transaction(true);
tx.sadd(setKey, networkSet.begin(), networkSet.end());
tx.exec();
}
if (++this->_ready == 2) { if (++this->_ready == 2) {
if (_waitNoticePrinted) { if (_waitNoticePrinted) {
fprintf(stderr,"[%s] NOTICE: %.10llx controller PostgreSQL data download complete." ZT_EOL_S,_timestr(),(unsigned long long)_myAddress.toInt()); fprintf(stderr,"[%s] NOTICE: %.10llx controller PostgreSQL data download complete." ZT_EOL_S,_timestr(),(unsigned long long)_myAddress.toInt());
@ -463,24 +469,32 @@ void PostgreSQL::initializeMembers(PGconn *conn)
fprintf(stderr, "Bad Database Connection: %s", PQerrorMessage(conn)); fprintf(stderr, "Bad Database Connection: %s", PQerrorMessage(conn));
exit(1); exit(1);
} }
std::string setKeyBase = "network-nodes-all:{" + std::string(_myAddressStr.c_str()) + "}:"; std::string setKeyBase = "network-nodes-all:{" + _myAddressStr + "}:";
if (_rc != NULL) { if (_rc != NULL) {
std::lock_guard<std::mutex> l(_networks_l); std::lock_guard<std::mutex> l(_networks_l);
std::unordered_set<std::string> deletes;
for ( auto it : _networks) { for ( auto it : _networks) {
uint64_t nwid_i = it.first; uint64_t nwid_i = it.first;
char nwidTmp[64] = {0}; char nwidTmp[64] = {0};
OSUtils::ztsnprintf(nwidTmp, sizeof(nwidTmp), "%.16llx", nwid_i); OSUtils::ztsnprintf(nwidTmp, sizeof(nwidTmp), "%.16llx", nwid_i);
std::string nwid(nwidTmp); std::string nwid(nwidTmp);
std::string key = setKeyBase + nwid; std::string key = setKeyBase + nwid;
if (_rc->clusterMode) { deletes.insert(key);
try {
_cluster->del(key);
} catch (...) {}
} else {
try {
_redis->del(key);
} catch (...) {}
} }
if (_rc->clusterMode) {
auto tx = _cluster->transaction(_myAddressStr, true);
for (std::string k : deletes) {
tx.del(k);
}
tx.exec();
} else {
auto tx = _redis->transaction(true);
for (std::string k : deletes) {
tx.del(k);
}
tx.exec();
} }
} }
@ -488,6 +502,8 @@ void PostgreSQL::initializeMembers(PGconn *conn)
_myAddressStr.c_str() _myAddressStr.c_str()
}; };
std::unordered_map<std::string, std::string> networkMembers;
fprintf(stderr, "Initializing Members...\n"); fprintf(stderr, "Initializing Members...\n");
PGresult *res = PQexecParams(conn, PGresult *res = PQexecParams(conn,
"SELECT m.id, m.network_id, m.active_bridge, m.authorized, m.capabilities, EXTRACT(EPOCH FROM m.creation_time AT TIME ZONE 'UTC')*1000, m.identity, " "SELECT m.id, m.network_id, m.active_bridge, m.authorized, m.capabilities, EXTRACT(EPOCH FROM m.creation_time AT TIME ZONE 'UTC')*1000, m.identity, "
@ -520,13 +536,7 @@ void PostgreSQL::initializeMembers(PGconn *conn)
std::string memberId(PQgetvalue(res, i, 0)); std::string memberId(PQgetvalue(res, i, 0));
std::string networkId(PQgetvalue(res, i, 1)); std::string networkId(PQgetvalue(res, i, 1));
if (_rc != NULL) { networkMembers.insert(std::pair<std::string, std::string>(setKeyBase+networkId, memberId));
if (_rc->clusterMode) {
_cluster->sadd(setKeyBase + networkId, memberId);
} else {
_redis->sadd(setKeyBase + networkId, memberId);
}
}
std::string ctime = PQgetvalue(res, i, 5); std::string ctime = PQgetvalue(res, i, 5);
config["id"] = memberId; config["id"] = memberId;
@ -628,19 +638,26 @@ void PostgreSQL::initializeMembers(PGconn *conn)
config["ipAssignments"].push_back(ipaddr); config["ipAssignments"].push_back(ipaddr);
} }
if (_rc != NULL) {
if (_rc->clusterMode) {
_cluster->sadd(setKeyBase + networkId, memberId);
} else {
_redis->sadd(setKeyBase + networkId, memberId);
}
}
_memberChanged(empty, config, false); _memberChanged(empty, config, false);
} }
PQclear(res); PQclear(res);
if (_rc != NULL) {
if (_rc->clusterMode) {
auto tx = _cluster->transaction(_myAddressStr, true);
for (auto it : networkMembers) {
tx.sadd(it.first, it.second);
}
tx.exec();
} else {
auto tx = _redis->transaction(true);
for (auto it : networkMembers) {
tx.sadd(it.first, it.second);
}
tx.exec();
}
}
if (++this->_ready == 2) { if (++this->_ready == 2) {
if (_waitNoticePrinted) { if (_waitNoticePrinted) {
fprintf(stderr,"[%s] NOTICE: %.10llx controller PostgreSQL data download complete." ZT_EOL_S,_timestr(),(unsigned long long)_myAddress.toInt()); fprintf(stderr,"[%s] NOTICE: %.10llx controller PostgreSQL data download complete." ZT_EOL_S,_timestr(),(unsigned long long)_myAddress.toInt());