diff options
Diffstat (limited to 'controller')
-rw-r--r-- | controller/DB.cpp | 136 | ||||
-rw-r--r-- | controller/DB.hpp | 1 | ||||
-rw-r--r-- | controller/EmbeddedNetworkController.cpp | 7 | ||||
-rw-r--r-- | controller/FileDB.cpp | 48 | ||||
-rw-r--r-- | controller/FileDB.hpp | 1 | ||||
-rw-r--r-- | controller/RethinkDB.cpp | 11 | ||||
-rw-r--r-- | controller/RethinkDB.hpp | 4 |
7 files changed, 43 insertions, 165 deletions
diff --git a/controller/DB.cpp b/controller/DB.cpp index b2e8878a..61eed0e9 100644 --- a/controller/DB.cpp +++ b/controller/DB.cpp @@ -324,109 +324,6 @@ void DB::_memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool pu } } - /* - if (old.is_object()) { - json &config = old["config"]; - if (config.is_object()) { - memberId = OSUtils::jsonIntHex(config["id"],0ULL); - networkId = OSUtils::jsonIntHex(config["nwid"],0ULL); - if ((memberId)&&(networkId)) { - { - std::lock_guard<std::mutex> l(_networks_l); - auto nw2 = _networks.find(networkId); - if (nw2 != _networks.end()) - nw = nw2->second; - } - if (nw) { - std::lock_guard<std::mutex> l(nw->lock); - if (OSUtils::jsonBool(config["activeBridge"],false)) - nw->activeBridgeMembers.erase(memberId); - wasAuth = OSUtils::jsonBool(config["authorized"],false); - if (wasAuth) - nw->authorizedMembers.erase(memberId); - json &ips = config["ipAssignments"]; - if (ips.is_array()) { - for(unsigned long i=0;i<ips.size();++i) { - json &ipj = ips[i]; - if (ipj.is_string()) { - const std::string ips = ipj; - InetAddress ipa(ips.c_str()); - ipa.setPort(0); - nw->allocatedIps.erase(ipa); - } - } - } - } - } - } - } - - if (member.is_object()) { - json &config = member["config"]; - if (config.is_object()) { - if (!nw) { - memberId = OSUtils::jsonIntHex(config["id"],0ULL); - networkId = OSUtils::jsonIntHex(config["nwid"],0ULL); - if ((!memberId)||(!networkId)) - return; - std::lock_guard<std::mutex> l(_networks_l); - std::shared_ptr<_Network> &nw2 = _networks[networkId]; - if (!nw2) - nw2.reset(new _Network); - nw = nw2; - } - - { - std::lock_guard<std::mutex> l(nw->lock); - - nw->members[memberId] = config; - - if (OSUtils::jsonBool(config["activeBridge"],false)) - nw->activeBridgeMembers.insert(memberId); - isAuth = OSUtils::jsonBool(config["authorized"],false); - if (isAuth) - nw->authorizedMembers.insert(memberId); - json &ips = config["ipAssignments"]; - if (ips.is_array()) { - for(unsigned long i=0;i<ips.size();++i) { - json &ipj = ips[i]; - if (ipj.is_string()) { - const std::string ips = ipj; - InetAddress ipa(ips.c_str()); - ipa.setPort(0); - nw->allocatedIps.insert(ipa); - } - } - } - - if (!isAuth) { - const int64_t ldt = (int64_t)OSUtils::jsonInt(config["lastDeauthorizedTime"],0ULL); - if (ldt > nw->mostRecentDeauthTime) - nw->mostRecentDeauthTime = ldt; - } - } - - if (push) - _controller->onNetworkMemberUpdate(networkId,memberId); - } - } else if (memberId) { - if (nw) { - std::lock_guard<std::mutex> l(nw->lock); - nw->members.erase(memberId); - } - if (networkId) { - std::lock_guard<std::mutex> l(_networks_l); - auto er = _networkByMember.equal_range(memberId); - for(auto i=er.first;i!=er.second;++i) { - if (i->second == networkId) { - _networkByMember.erase(i); - break; - } - } - } - } - */ - if ((push)&&((wasAuth)&&(!isAuth)&&(networkId)&&(memberId))) _controller->onNetworkMemberDeauthorize(networkId,memberId); } @@ -460,39 +357,6 @@ void DB::_networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool _networks.erase(id); } } - - /* - if (network.is_object()) { - json &config = network["config"]; - if (networkConfig.is_object()) { - const std::string ids = config["id"]; - const uint64_t id = Utils::hexStrToU64(ids.c_str()); - if (id) { - std::shared_ptr<_Network> nw; - { - std::lock_guard<std::mutex> l(_networks_l); - std::shared_ptr<_Network> &nw2 = _networks[id]; - if (!nw2) - nw2.reset(new _Network); - nw = nw2; - } - { - std::lock_guard<std::mutex> l2(nw->lock); - nw->config = config; - } - if (push) - _controller->onNetworkUpdate(id); - } - } - } else if (old.is_object()) { - const std::string ids = old["id"]; - const uint64_t id = Utils::hexStrToU64(ids.c_str()); - if (id) { - std::lock_guard<std::mutex> l(_networks_l); - _networks.erase(id); - } - } - */ } void DB::_fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info) diff --git a/controller/DB.hpp b/controller/DB.hpp index 4757bb40..4b2940cd 100644 --- a/controller/DB.hpp +++ b/controller/DB.hpp @@ -82,6 +82,7 @@ public: virtual ~DB(); virtual bool waitForReady() = 0; + virtual bool isReady() = 0; inline bool hasNetwork(const uint64_t networkId) const { diff --git a/controller/EmbeddedNetworkController.cpp b/controller/EmbeddedNetworkController.cpp index 9a07b285..6a4134c6 100644 --- a/controller/EmbeddedNetworkController.cpp +++ b/controller/EmbeddedNetworkController.cpp @@ -551,7 +551,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpGET( for(auto member=members.begin();member!=members.end();++member) { mid = (*member)["id"]; char tmp[128]; - OSUtils::ztsnprintf(tmp,sizeof(tmp),"%s\"%s\":%llu",(responseBody.length() > 1) ? ",\"" : "\"",mid.c_str(),(unsigned long long)OSUtils::jsonInt((*member)["revision"],0)); + OSUtils::ztsnprintf(tmp,sizeof(tmp),"%s\"%s\":%llu",(responseBody.length() > 1) ? "," : "",mid.c_str(),(unsigned long long)OSUtils::jsonInt((*member)["revision"],0)); responseBody.append(tmp); } } @@ -596,10 +596,11 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpGET( // Controller status char tmp[4096]; - OSUtils::ztsnprintf(tmp,sizeof(tmp),"{\n\t\"controller\": true,\n\t\"apiVersion\": %d,\n\t\"clock\": %llu\n}\n",ZT_NETCONF_CONTROLLER_API_VERSION,(unsigned long long)OSUtils::now()); + const bool dbOk = _db->isReady(); + OSUtils::ztsnprintf(tmp,sizeof(tmp),"{\n\t\"controller\": true,\n\t\"apiVersion\": %d,\n\t\"clock\": %llu,\n\t\"databaseReady\": %s\n}\n",ZT_NETCONF_CONTROLLER_API_VERSION,(unsigned long long)OSUtils::now(),dbOk ? "true" : "false"); responseBody = tmp; responseContentType = "application/json"; - return 200; + return dbOk ? 200 : 503; } diff --git a/controller/FileDB.cpp b/controller/FileDB.cpp index a7b59cbf..8cbd60ce 100644 --- a/controller/FileDB.cpp +++ b/controller/FileDB.cpp @@ -63,14 +63,10 @@ FileDB::FileDB(EmbeddedNetworkController *const nc,const Identity &myId,const ch } } -FileDB::~FileDB() -{ -} +FileDB::~FileDB() {} -bool FileDB::waitForReady() -{ - return true; -} +bool FileDB::waitForReady() { return true; } +bool FileDB::isReady() { return true; } void FileDB::save(nlohmann::json *orig,nlohmann::json &record) { @@ -91,13 +87,15 @@ void FileDB::save(nlohmann::json *orig,nlohmann::json &record) nlohmann::json old; get(nwid,old); - OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json.new",_networksPath.c_str(),nwid); - OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json",_networksPath.c_str(),nwid); - if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1))) - fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1); - OSUtils::rename(p1,p2); + if ((!old.is_object())||(old != record)) { + OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json.new",_networksPath.c_str(),nwid); + OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json",_networksPath.c_str(),nwid); + if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1))) + fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1); + OSUtils::rename(p1,p2); - _networkChanged(old,record,true); + _networkChanged(old,record,true); + } } } else if (objtype == "member") { const uint64_t id = OSUtils::jsonIntHex(record["id"],0ULL); @@ -106,17 +104,21 @@ void FileDB::save(nlohmann::json *orig,nlohmann::json &record) nlohmann::json network,old; get(nwid,network,id,old); - OSUtils::ztsnprintf(pb,sizeof(pb),"%s" ZT_PATH_SEPARATOR_S "%.16llx" ZT_PATH_SEPARATOR_S "member",_networksPath.c_str(),(unsigned long long)nwid); - OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.10llx.json.new",pb,(unsigned long long)id); - OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.10llx.json",pb,(unsigned long long)id); - if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1))) { - OSUtils::mkdir(pb); - if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1))) - fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1); - } - OSUtils::rename(p1,p2); + if ((!old.is_object())||(old != record)) { + OSUtils::ztsnprintf(pb,sizeof(pb),"%s" ZT_PATH_SEPARATOR_S "%.16llx" ZT_PATH_SEPARATOR_S "member",_networksPath.c_str(),(unsigned long long)nwid); + OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.10llx.json.new",pb,(unsigned long long)id); + if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1))) { + OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.16llx",_networksPath.c_str(),(unsigned long long)nwid); + OSUtils::mkdir(p2); + OSUtils::mkdir(pb); + if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1))) + fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1); + } + OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.10llx.json",pb,(unsigned long long)id); + OSUtils::rename(p1,p2); - _memberChanged(old,record,true); + _memberChanged(old,record,true); + } } } else if (objtype == "trace") { const std::string id = record["id"]; diff --git a/controller/FileDB.hpp b/controller/FileDB.hpp index 1e275a36..1a3c12e9 100644 --- a/controller/FileDB.hpp +++ b/controller/FileDB.hpp @@ -31,6 +31,7 @@ public: virtual ~FileDB(); virtual bool waitForReady(); + virtual bool isReady(); virtual void save(nlohmann::json *orig,nlohmann::json &record); virtual void eraseNetwork(const uint64_t networkId); virtual void eraseMember(const uint64_t networkId,const uint64_t memberId); diff --git a/controller/RethinkDB.cpp b/controller/RethinkDB.cpp index f6c8a59c..a46d033f 100644 --- a/controller/RethinkDB.cpp +++ b/controller/RethinkDB.cpp @@ -263,9 +263,13 @@ RethinkDB::RethinkDB(EmbeddedNetworkController *const nc,const Identity &myId,co std::unique_ptr<R::Connection> rdb; while (_run == 1) { try { - if (!rdb) + if (!rdb) { + _connected = 0; rdb = R::connect(this->_host,this->_port,this->_auth); + } + if (rdb) { + _connected = 1; R::Array batch; R::Object tmpobj; @@ -434,6 +438,11 @@ bool RethinkDB::waitForReady() return true; } +bool RethinkDB::isReady() +{ + return ((_ready)&&(_connected)); +} + void RethinkDB::save(nlohmann::json *orig,nlohmann::json &record) { if (!record.is_object()) // sanity check diff --git a/controller/RethinkDB.hpp b/controller/RethinkDB.hpp index b1049ac3..60f04c5b 100644 --- a/controller/RethinkDB.hpp +++ b/controller/RethinkDB.hpp @@ -41,6 +41,7 @@ public: virtual ~RethinkDB(); virtual bool waitForReady(); + virtual bool isReady(); virtual void save(nlohmann::json *orig,nlohmann::json &record); virtual void eraseNetwork(const uint64_t networkId); virtual void eraseMember(const uint64_t networkId,const uint64_t memberId); @@ -72,8 +73,7 @@ protected: std::thread _heartbeatThread; mutable std::mutex _readyLock; // locked until ready - std::atomic<int> _ready; - std::atomic<int> _run; + std::atomic<int> _ready,_connected,_run; mutable volatile bool _waitNoticePrinted; }; |