diff options
author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2017-03-21 06:15:49 -0700 |
---|---|---|
committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2017-03-21 06:15:49 -0700 |
commit | c62141fd9870eabf6f987da8e2a82ff5a999ddcf (patch) | |
tree | c9c50d6552d4c4895e0db2714939fd8ee9f613a0 | |
parent | ae303ee90276f7cf85079c66d35716f80ea2321f (diff) | |
download | infinitytier-c62141fd9870eabf6f987da8e2a82ff5a999ddcf.tar.gz infinitytier-c62141fd9870eabf6f987da8e2a82ff5a999ddcf.zip |
Make controller do a simple write-through cache without revalidating. Means you must restart if files change on disk, but will decrease I/O considerably.
-rw-r--r-- | controller/EmbeddedNetworkController.cpp | 31 | ||||
-rw-r--r-- | controller/EmbeddedNetworkController.hpp | 14 | ||||
-rw-r--r-- | controller/JSONDB.cpp | 63 | ||||
-rw-r--r-- | controller/JSONDB.hpp | 17 |
4 files changed, 41 insertions, 84 deletions
diff --git a/controller/EmbeddedNetworkController.cpp b/controller/EmbeddedNetworkController.cpp index 8fbc1cf8..0e704288 100644 --- a/controller/EmbeddedNetworkController.cpp +++ b/controller/EmbeddedNetworkController.cpp @@ -61,9 +61,6 @@ using json = nlohmann::json; // Nodes are considered active if they've queried in less than this long #define ZT_NETCONF_NODE_ACTIVE_THRESHOLD (ZT_NETWORK_AUTOCONF_DELAY * 2) -// Timeout for disk read cache (ms) -#define ZT_NETCONF_DB_CACHE_TTL 60000 - namespace ZeroTier { static json _renderRule(ZT_VirtualNetworkRule &rule) @@ -503,7 +500,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpGET( json network; { Mutex::Lock _l(_db_m); - network = _db.get("network",nwids,ZT_NETCONF_DB_CACHE_TTL); + network = _db.get("network",nwids); } if (!network.size()) return 404; @@ -518,7 +515,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpGET( json member; { Mutex::Lock _l(_db_m); - member = _db.get("network",nwids,"member",Address(address).toString(),ZT_NETCONF_DB_CACHE_TTL); + member = _db.get("network",nwids,"member",Address(address).toString()); } if (!member.size()) return 404; @@ -533,7 +530,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpGET( Mutex::Lock _l(_db_m); responseBody = "{"; - _db.filter((std::string("network/") + nwids + "/member/"),ZT_NETCONF_DB_CACHE_TTL,[&responseBody](const std::string &n,const json &member) { + _db.filter((std::string("network/") + nwids + "/member/"),[&responseBody](const std::string &n,const json &member) { if ((member.is_object())&&(member.size() > 0)) { responseBody.append((responseBody.length() == 1) ? "\"" : ",\""); responseBody.append(OSUtils::jsonString(member["id"],"0")); @@ -566,7 +563,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpGET( std::set<std::string> networkIds; { Mutex::Lock _l(_db_m); - _db.filter("network/",120000,[&networkIds](const std::string &n,const json &obj) { + _db.filter("network/",[&networkIds](const std::string &n,const json &obj) { if (n.length() == (16 + 8)) networkIds.insert(n.substr(8)); return true; // do not delete @@ -641,7 +638,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpPOST( json member; { Mutex::Lock _l(_db_m); - member = _db.get("network",nwids,"member",Address(address).toString(),ZT_NETCONF_DB_CACHE_TTL); + member = _db.get("network",nwids,"member",Address(address).toString()); } json origMember(member); // for detecting changes _initMember(member); @@ -824,7 +821,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpPOST( uint64_t tryNwid = nwidPrefix | (nwidPostfix & 0xffffffULL); if ((tryNwid & 0xffffffULL) == 0ULL) tryNwid |= 1ULL; Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)tryNwid); - if (_db.get("network",nwids,ZT_NETCONF_DB_CACHE_TTL).size() <= 0) { + if (_db.get("network",nwids).size() <= 0) { nwid = tryNwid; break; } @@ -833,7 +830,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpPOST( return 503; } - network = _db.get("network",nwids,ZT_NETCONF_DB_CACHE_TTL); + network = _db.get("network",nwids); } json origNetwork(network); // for detecting changes _initNetwork(network); @@ -1053,7 +1050,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpPOST( } // Send an update to all members of the network - _db.filter((std::string("network/") + nwids + "/member/"),120000,[this,&now,&nwid](const std::string &n,const json &obj) { + _db.filter((std::string("network/") + nwids + "/member/"),[this,&now,&nwid](const std::string &n,const json &obj) { _pushMemberUpdate(now,nwid,obj); return true; // do not delete }); @@ -1095,7 +1092,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpDELETE( json network; { Mutex::Lock _l(_db_m); - network = _db.get("network",nwids,ZT_NETCONF_DB_CACHE_TTL); + network = _db.get("network",nwids); } if (!network.size()) return 404; @@ -1106,7 +1103,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpDELETE( Mutex::Lock _l(_db_m); - json member = _db.get("network",nwids,"member",Address(address).toString(),ZT_NETCONF_DB_CACHE_TTL); + json member = _db.get("network",nwids,"member",Address(address).toString()); _db.erase("network",nwids,"member",Address(address).toString()); if (!member.size()) @@ -1119,7 +1116,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpDELETE( Mutex::Lock _l(_db_m); std::string pfx("network/"); pfx.append(nwids); - _db.filter(pfx,120000,[](const std::string &n,const json &obj) { + _db.filter(pfx,[](const std::string &n,const json &obj) { return false; // delete }); @@ -1246,8 +1243,8 @@ void EmbeddedNetworkController::_request( json member; { Mutex::Lock _l(_db_m); - network = _db.get("network",nwids,ZT_NETCONF_DB_CACHE_TTL); - member = _db.get("network",nwids,"member",identity.address().toString(),ZT_NETCONF_DB_CACHE_TTL); + network = _db.get("network",nwids); + member = _db.get("network",nwids,"member",identity.address().toString()); } if (!network.size()) { @@ -1772,7 +1769,7 @@ void EmbeddedNetworkController::_getNetworkMemberInfo(uint64_t now,uint64_t nwid { Mutex::Lock _l(_db_m); - _db.filter(pfx,120000,[&nmi,&now](const std::string &n,const json &member) { + _db.filter(pfx,[&nmi,&now](const std::string &n,const json &member) { try { if (OSUtils::jsonBool(member["authorized"],false)) { ++nmi.authorizedMemberCount; diff --git a/controller/EmbeddedNetworkController.hpp b/controller/EmbeddedNetworkController.hpp index a7277ace..5d2454ed 100644 --- a/controller/EmbeddedNetworkController.hpp +++ b/controller/EmbeddedNetworkController.hpp @@ -99,12 +99,7 @@ public: private: static void _circuitTestCallback(ZT_Node *node,ZT_CircuitTest *test,const ZT_CircuitTestReport *report); - void _request( - uint64_t nwid, - const InetAddress &fromAddr, - uint64_t requestPacketId, - const Identity &identity, - const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData); + void _request(uint64_t nwid,const InetAddress &fromAddr,uint64_t requestPacketId,const Identity &identity,const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData); struct _RQEntry { @@ -134,12 +129,9 @@ private: }; std::map<uint64_t,_NetworkMemberInfo> _nmiCache; Mutex _nmiCache_m; + void _getNetworkMemberInfo(uint64_t now,uint64_t nwid,_NetworkMemberInfo &nmi); - inline void _clearNetworkMemberInfoCache(const uint64_t nwid) - { - Mutex::Lock _l(_nmiCache_m); - _nmiCache.erase(nwid); - } + inline void _clearNetworkMemberInfoCache(const uint64_t nwid) { Mutex::Lock _l(_nmiCache_m); _nmiCache.erase(nwid); } void _pushMemberUpdate(uint64_t now,uint64_t nwid,const nlohmann::json &member); diff --git a/controller/JSONDB.cpp b/controller/JSONDB.cpp index 298ac721..9c28ddf1 100644 --- a/controller/JSONDB.cpp +++ b/controller/JSONDB.cpp @@ -53,8 +53,6 @@ bool JSONDB::put(const std::string &n,const nlohmann::json &obj) _E &e = _db[n]; e.obj = obj; - e.lastModifiedOnDisk = OSUtils::getLastModified(path.c_str()); - e.lastCheck = OSUtils::now(); return true; } @@ -64,53 +62,26 @@ const nlohmann::json &JSONDB::get(const std::string &n,unsigned long maxSinceChe if (!_isValidObjectName(n)) return _EMPTY_JSON; - const uint64_t now = OSUtils::now(); - std::string buf; std::map<std::string,_E>::iterator e(_db.find(n)); - - if (e != _db.end()) { - if ((now - e->second.lastCheck) <= (uint64_t)maxSinceCheck) - return e->second.obj; - - const std::string path(_genPath(n,false)); - if (!path.length()) // sanity check - return _EMPTY_JSON; - - // We are somewhat tolerant to momentary disk failures here. This may - // occur over e.g. EC2's elastic filesystem (NFS). - const uint64_t lm = OSUtils::getLastModified(path.c_str()); - if (e->second.lastModifiedOnDisk != lm) { - if (OSUtils::readFile(path.c_str(),buf)) { - try { - e->second.obj = OSUtils::jsonParse(buf); - e->second.lastModifiedOnDisk = lm; // don't update these if there is a parse error -- try again and again ASAP - e->second.lastCheck = now; - } catch ( ... ) {} // parse errors result in "holding pattern" behavior - } - } - + if (e != _db.end()) return e->second.obj; - } else { - const std::string path(_genPath(n,false)); - if (!path.length()) - return _EMPTY_JSON; - - if (!OSUtils::readFile(path.c_str(),buf)) - return _EMPTY_JSON; - - const uint64_t lm = OSUtils::getLastModified(path.c_str()); - _E &e2 = _db[n]; - try { - e2.obj = OSUtils::jsonParse(buf); - } catch ( ... ) { - e2.obj = _EMPTY_JSON; - buf = "{}"; - } - e2.lastModifiedOnDisk = lm; - e2.lastCheck = now; - return e2.obj; + const std::string path(_genPath(n,false)); + if (!path.length()) + return _EMPTY_JSON; + std::string buf; + if (!OSUtils::readFile(path.c_str(),buf)) + return _EMPTY_JSON; + + _E &e2 = _db[n]; + try { + e2.obj = OSUtils::jsonParse(buf); + } catch ( ... ) { + e2.obj = _EMPTY_JSON; + buf = "{}"; } + + return e2.obj; } void JSONDB::erase(const std::string &n) @@ -131,7 +102,7 @@ void JSONDB::_reload(const std::string &p,const std::string &b) std::vector<std::string> dl(OSUtils::listDirectory(p.c_str())); for(std::vector<std::string>::const_iterator di(dl.begin());di!=dl.end();++di) { if ((di->length() > 5)&&(di->substr(di->length() - 5) == ".json")) { - this->get(b + di->substr(0,di->length() - 5),0); + this->get(b + di->substr(0,di->length() - 5)); } else { this->_reload((p + ZT_PATH_SEPARATOR + *di),(b + *di + ZT_PATH_SEPARATOR)); } diff --git a/controller/JSONDB.hpp b/controller/JSONDB.hpp index d2549173..00eeb8d8 100644 --- a/controller/JSONDB.hpp +++ b/controller/JSONDB.hpp @@ -63,12 +63,12 @@ public: inline bool put(const std::string &n1,const std::string &n2,const std::string &n3,const std::string &n4,const nlohmann::json &obj) { return this->put((n1 + "/" + n2 + "/" + n3 + "/" + n4),obj); } inline bool put(const std::string &n1,const std::string &n2,const std::string &n3,const std::string &n4,const std::string &n5,const nlohmann::json &obj) { return this->put((n1 + "/" + n2 + "/" + n3 + "/" + n4 + "/" + n5),obj); } - const nlohmann::json &get(const std::string &n,unsigned long maxSinceCheck = 0); + const nlohmann::json &get(const std::string &n); - inline const nlohmann::json &get(const std::string &n1,const std::string &n2,unsigned long maxSinceCheck = 0) { return this->get((n1 + "/" + n2),maxSinceCheck); } - inline const nlohmann::json &get(const std::string &n1,const std::string &n2,const std::string &n3,unsigned long maxSinceCheck = 0) { return this->get((n1 + "/" + n2 + "/" + n3),maxSinceCheck); } - inline const nlohmann::json &get(const std::string &n1,const std::string &n2,const std::string &n3,const std::string &n4,unsigned long maxSinceCheck = 0) { return this->get((n1 + "/" + n2 + "/" + n3 + "/" + n4),maxSinceCheck); } - inline const nlohmann::json &get(const std::string &n1,const std::string &n2,const std::string &n3,const std::string &n4,const std::string &n5,unsigned long maxSinceCheck = 0) { return this->get((n1 + "/" + n2 + "/" + n3 + "/" + n4 + "/" + n5),maxSinceCheck); } + inline const nlohmann::json &get(const std::string &n1,const std::string &n2) { return this->get((n1 + "/" + n2)); } + inline const nlohmann::json &get(const std::string &n1,const std::string &n2,const std::string &n3) { return this->get((n1 + "/" + n2 + "/" + n3)); } + inline const nlohmann::json &get(const std::string &n1,const std::string &n2,const std::string &n3,const std::string &n4) { return this->get((n1 + "/" + n2 + "/" + n3 + "/" + n4)); } + inline const nlohmann::json &get(const std::string &n1,const std::string &n2,const std::string &n3,const std::string &n4,const std::string &n5) { return this->get((n1 + "/" + n2 + "/" + n3 + "/" + n4 + "/" + n5)); } void erase(const std::string &n); @@ -78,11 +78,11 @@ public: inline void erase(const std::string &n1,const std::string &n2,const std::string &n3,const std::string &n4,const std::string &n5) { this->erase(n1 + "/" + n2 + "/" + n3 + "/" + n4 + "/" + n5); } template<typename F> - inline void filter(const std::string &prefix,unsigned long maxSinceCheck,F func) + inline void filter(const std::string &prefix,F func) { for(std::map<std::string,_E>::iterator i(_db.lower_bound(prefix));i!=_db.end();) { if ((i->first.length() >= prefix.length())&&(!memcmp(i->first.data(),prefix.data(),prefix.length()))) { - if (!func(i->first,get(i->first,maxSinceCheck))) { + if (!func(i->first,get(i->first))) { std::map<std::string,_E>::iterator i2(i); ++i2; this->erase(i->first); i = i2; @@ -102,9 +102,6 @@ private: struct _E { nlohmann::json obj; - uint64_t lastModifiedOnDisk; - uint64_t lastCheck; - inline bool operator==(const _E &e) const { return (obj == e.obj); } inline bool operator!=(const _E &e) const { return (obj != e.obj); } }; |