RethinkDB.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifdef ZT_CONTROLLER_USE_RETHINKDB
  19. #include <unistd.h>
  20. #include "RethinkDB.hpp"
  21. #include "EmbeddedNetworkController.hpp"
  22. #include "../version.h"
  23. #include <chrono>
  24. #include <algorithm>
  25. #include <stdexcept>
  26. #include "../ext/librethinkdbxx/build/include/rethinkdb.h"
  27. namespace R = RethinkDB;
  28. using json = nlohmann::json;
  29. namespace ZeroTier {
  30. RethinkDB::RethinkDB(EmbeddedNetworkController *const nc,const Identity &myId,const char *path) :
  31. DB(nc,myId,path),
  32. _ready(2), // two tables need to be synchronized before we're ready, so this is ready when it reaches 0
  33. _run(1),
  34. _waitNoticePrinted(false)
  35. {
  36. // rethinkdb:host:port:db[:auth]
  37. std::vector<std::string> ps(OSUtils::split(path,":","",""));
  38. if ((ps.size() < 4)||(ps[0] != "rethinkdb"))
  39. throw std::runtime_error("invalid rethinkdb database url");
  40. _host = ps[1];
  41. _port = Utils::strToInt(ps[2].c_str());
  42. _db = ps[3];
  43. if (ps.size() > 4)
  44. _auth = ps[4];
  45. _readyLock.lock();
  46. _membersDbWatcher = std::thread([this]() {
  47. try {
  48. while (_run == 1) {
  49. try {
  50. std::unique_ptr<R::Connection> rdb(R::connect(this->_host,this->_port,this->_auth));
  51. if (rdb) {
  52. _membersDbWatcherConnection = (void *)rdb.get();
  53. auto cur = R::db(this->_db).table("Member",R::optargs("read_mode","outdated")).get_all(this->_myAddressStr,R::optargs("index","controllerId")).changes(R::optargs("squash",0.05,"include_initial",true,"include_types",true,"include_states",true)).run(*rdb);
  54. while (cur.has_next()) {
  55. if (_run != 1) break;
  56. json tmp(json::parse(cur.next().as_json()));
  57. if ((tmp["type"] == "state")&&(tmp["state"] == "ready")) {
  58. if (--this->_ready == 0) {
  59. if (_waitNoticePrinted)
  60. fprintf(stderr,"NOTICE: %.10llx controller RethinkDB data download complete." ZT_EOL_S,(unsigned long long)_myAddress.toInt());
  61. this->_readyLock.unlock();
  62. }
  63. } else {
  64. try {
  65. json &ov = tmp["old_val"];
  66. json &nv = tmp["new_val"];
  67. if (ov.is_object()||nv.is_object()) {
  68. //if (nv.is_object()) printf("MEMBER: %s" ZT_EOL_S,nv.dump().c_str());
  69. this->_memberChanged(ov,nv,(this->_ready <= 0));
  70. }
  71. } catch ( ... ) {} // ignore bad records
  72. }
  73. }
  74. }
  75. } catch (std::exception &e) {
  76. fprintf(stderr,"ERROR: %.10llx controller RethinkDB (member change stream): %s" ZT_EOL_S,(unsigned long long)_myAddress.toInt(),e.what());
  77. } catch (R::Error &e) {
  78. fprintf(stderr,"ERROR: %.10llx controller RethinkDB (member change stream): %s" ZT_EOL_S,(unsigned long long)_myAddress.toInt(),e.message.c_str());
  79. } catch ( ... ) {
  80. fprintf(stderr,"ERROR: %.10llx controller RethinkDB (member change stream): unknown exception" ZT_EOL_S,(unsigned long long)_myAddress.toInt());
  81. }
  82. std::this_thread::sleep_for(std::chrono::milliseconds(250));
  83. }
  84. } catch ( ... ) {}
  85. });
  86. _networksDbWatcher = std::thread([this]() {
  87. try {
  88. while (_run == 1) {
  89. try {
  90. std::unique_ptr<R::Connection> rdb(R::connect(this->_host,this->_port,this->_auth));
  91. if (rdb) {
  92. _networksDbWatcherConnection = (void *)rdb.get();
  93. auto cur = R::db(this->_db).table("Network",R::optargs("read_mode","outdated")).get_all(this->_myAddressStr,R::optargs("index","controllerId")).changes(R::optargs("squash",0.05,"include_initial",true,"include_types",true,"include_states",true)).run(*rdb);
  94. while (cur.has_next()) {
  95. if (_run != 1) break;
  96. json tmp(json::parse(cur.next().as_json()));
  97. if ((tmp["type"] == "state")&&(tmp["state"] == "ready")) {
  98. if (--this->_ready == 0) {
  99. if (_waitNoticePrinted)
  100. fprintf(stderr,"NOTICE: %.10llx controller RethinkDB data download complete." ZT_EOL_S,(unsigned long long)_myAddress.toInt());
  101. this->_readyLock.unlock();
  102. }
  103. } else {
  104. try {
  105. json &ov = tmp["old_val"];
  106. json &nv = tmp["new_val"];
  107. if (ov.is_object()||nv.is_object()) {
  108. //if (nv.is_object()) printf("NETWORK: %s" ZT_EOL_S,nv.dump().c_str());
  109. this->_networkChanged(ov,nv,(this->_ready <= 0));
  110. }
  111. } catch ( ... ) {} // ignore bad records
  112. }
  113. }
  114. }
  115. } catch (std::exception &e) {
  116. fprintf(stderr,"ERROR: %.10llx controller RethinkDB (network change stream): %s" ZT_EOL_S,(unsigned long long)_myAddress.toInt(),e.what());
  117. } catch (R::Error &e) {
  118. fprintf(stderr,"ERROR: %.10llx controller RethinkDB (network change stream): %s" ZT_EOL_S,(unsigned long long)_myAddress.toInt(),e.message.c_str());
  119. } catch ( ... ) {
  120. fprintf(stderr,"ERROR: %.10llx controller RethinkDB (network change stream): unknown exception" ZT_EOL_S,(unsigned long long)_myAddress.toInt());
  121. }
  122. std::this_thread::sleep_for(std::chrono::milliseconds(250));
  123. }
  124. } catch ( ... ) {}
  125. });
  126. for(int t=0;t<ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS;++t) {
  127. _commitThread[t] = std::thread([this]() {
  128. try {
  129. std::unique_ptr<R::Connection> rdb;
  130. nlohmann::json *config = (nlohmann::json *)0;
  131. while ((this->_commitQueue.get(config))&&(_run == 1)) {
  132. if (!config)
  133. continue;
  134. json record;
  135. const std::string objtype = (*config)["objtype"];
  136. const char *table;
  137. std::string deleteId;
  138. try {
  139. if (objtype == "member") {
  140. const std::string nwid = (*config)["nwid"];
  141. const std::string id = (*config)["id"];
  142. record["id"] = nwid + "-" + id;
  143. record["controllerId"] = this->_myAddressStr;
  144. record["networkId"] = nwid;
  145. record["nodeId"] = id;
  146. record["config"] = *config;
  147. table = "Member";
  148. } else if (objtype == "network") {
  149. const std::string id = (*config)["id"];
  150. record["id"] = id;
  151. record["controllerId"] = this->_myAddressStr;
  152. record["config"] = *config;
  153. table = "Network";
  154. } else if (objtype == "trace") {
  155. record = *config;
  156. table = "RemoteTrace";
  157. } else if (objtype == "_delete_network") {
  158. deleteId = (*config)["id"];
  159. table = "Network";
  160. } else if (objtype == "_delete_member") {
  161. deleteId = (*config)["nwid"];
  162. deleteId.push_back('-');
  163. const std::string tmp = (*config)["id"];
  164. deleteId.append(tmp);
  165. table = "Member";
  166. } else {
  167. delete config;
  168. continue;
  169. }
  170. delete config;
  171. } catch ( ... ) {
  172. delete config;
  173. continue;
  174. }
  175. while (_run == 1) {
  176. try {
  177. if (!rdb)
  178. rdb = R::connect(this->_host,this->_port,this->_auth);
  179. if (rdb) {
  180. if (deleteId.length() > 0) {
  181. //printf("DELETE: %s" ZT_EOL_S,deleteId.c_str());
  182. R::db(this->_db).table(table).get(deleteId).delete_().run(*rdb);
  183. } else {
  184. //printf("UPSERT: %s" ZT_EOL_S,record.dump().c_str());
  185. R::db(this->_db).table(table).insert(R::Datum::from_json(record.dump()),R::optargs("conflict","update","return_changes",false)).run(*rdb);
  186. }
  187. break;
  188. } else {
  189. fprintf(stderr,"ERROR: %.10llx controller RethinkDB (insert/update): connect failed (will retry)" ZT_EOL_S,(unsigned long long)_myAddress.toInt());
  190. }
  191. } catch (std::exception &e) {
  192. fprintf(stderr,"ERROR: %.10llx controller RethinkDB (insert/update): %s" ZT_EOL_S,(unsigned long long)_myAddress.toInt(),e.what());
  193. rdb.reset();
  194. } catch (R::Error &e) {
  195. fprintf(stderr,"ERROR: %.10llx controller RethinkDB (insert/update): %s" ZT_EOL_S,(unsigned long long)_myAddress.toInt(),e.message.c_str());
  196. rdb.reset();
  197. } catch ( ... ) {
  198. fprintf(stderr,"ERROR: %.10llx controller RethinkDB (insert/update): unknown exception" ZT_EOL_S,(unsigned long long)_myAddress.toInt());
  199. rdb.reset();
  200. }
  201. std::this_thread::sleep_for(std::chrono::milliseconds(250));
  202. }
  203. }
  204. } catch ( ... ) {}
  205. });
  206. }
  207. _onlineNotificationThread = std::thread([this]() {
  208. int64_t lastUpdatedNetworkStatus = 0;
  209. std::unordered_map< std::pair<uint64_t,uint64_t>,int64_t,_PairHasher > lastOnlineCumulative;
  210. try {
  211. std::unique_ptr<R::Connection> rdb;
  212. while (_run == 1) {
  213. try {
  214. if (!rdb)
  215. rdb = R::connect(this->_host,this->_port,this->_auth);
  216. if (rdb) {
  217. R::Array batch;
  218. R::Object tmpobj;
  219. std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > lastOnline;
  220. {
  221. std::lock_guard<std::mutex> l(_lastOnline_l);
  222. lastOnline.swap(_lastOnline);
  223. }
  224. for(auto i=lastOnline.begin();i!=lastOnline.end();++i) {
  225. lastOnlineCumulative[i->first] = i->second.first;
  226. char tmp[64],tmp2[64];
  227. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.16llx-%.10llx",i->first.first,i->first.second);
  228. tmpobj["id"] = tmp;
  229. tmpobj["ts"] = i->second.first;
  230. tmpobj["phy"] = i->second.second.toIpString(tmp2);
  231. batch.emplace_back(tmpobj);
  232. if (batch.size() >= 1024) {
  233. R::db(this->_db).table("MemberStatus",R::optargs("read_mode","outdated")).insert(batch,R::optargs("conflict","update")).run(*rdb);
  234. batch.clear();
  235. }
  236. }
  237. if (batch.size() > 0) {
  238. R::db(this->_db).table("MemberStatus",R::optargs("read_mode","outdated")).insert(batch,R::optargs("conflict","update")).run(*rdb);
  239. batch.clear();
  240. }
  241. tmpobj.clear();
  242. const int64_t now = OSUtils::now();
  243. if ((now - lastUpdatedNetworkStatus) > 10000) {
  244. lastUpdatedNetworkStatus = now;
  245. std::vector< std::pair< uint64_t,std::shared_ptr<_Network> > > networks;
  246. {
  247. std::lock_guard<std::mutex> l(_networks_l);
  248. networks.reserve(_networks.size() + 1);
  249. for(auto i=_networks.begin();i!=_networks.end();++i)
  250. networks.push_back(*i);
  251. }
  252. for(auto i=networks.begin();i!=networks.end();++i) {
  253. char tmp[64];
  254. Utils::hex(i->first,tmp);
  255. tmpobj["id"] = tmp;
  256. {
  257. std::lock_guard<std::mutex> l2(i->second->lock);
  258. tmpobj["authorizedMemberCount"] = i->second->authorizedMembers.size();
  259. tmpobj["totalMemberCount"] = i->second->members.size();
  260. unsigned long onlineMemberCount = 0;
  261. for(auto m=i->second->members.begin();m!=i->second->members.end();++m) {
  262. auto lo = lastOnlineCumulative.find(std::pair<uint64_t,uint64_t>(i->first,m->first));
  263. if (lo != lastOnlineCumulative.end()) {
  264. if ((now - lo->second) <= (ZT_NETWORK_AUTOCONF_DELAY * 2))
  265. ++onlineMemberCount;
  266. else lastOnlineCumulative.erase(lo);
  267. }
  268. }
  269. tmpobj["onlineMemberCount"] = onlineMemberCount;
  270. tmpobj["bridgeCount"] = i->second->activeBridgeMembers.size();
  271. tmpobj["ts"] = now;
  272. }
  273. batch.emplace_back(tmpobj);
  274. if (batch.size() >= 1024) {
  275. R::db(this->_db).table("NetworkStatus",R::optargs("read_mode","outdated")).insert(batch,R::optargs("conflict","update")).run(*rdb);
  276. batch.clear();
  277. }
  278. }
  279. if (batch.size() > 0) {
  280. R::db(this->_db).table("NetworkStatus",R::optargs("read_mode","outdated")).insert(batch,R::optargs("conflict","update")).run(*rdb);
  281. batch.clear();
  282. }
  283. }
  284. }
  285. } catch (std::exception &e) {
  286. fprintf(stderr,"ERROR: %.10llx controller RethinkDB (node status update): %s" ZT_EOL_S,(unsigned long long)_myAddress.toInt(),e.what());
  287. rdb.reset();
  288. } catch (R::Error &e) {
  289. fprintf(stderr,"ERROR: %.10llx controller RethinkDB (node status update): %s" ZT_EOL_S,(unsigned long long)_myAddress.toInt(),e.message.c_str());
  290. rdb.reset();
  291. } catch ( ... ) {
  292. fprintf(stderr,"ERROR: %.10llx controller RethinkDB (node status update): unknown exception" ZT_EOL_S,(unsigned long long)_myAddress.toInt());
  293. rdb.reset();
  294. }
  295. std::this_thread::sleep_for(std::chrono::milliseconds(250));
  296. }
  297. } catch ( ... ) {}
  298. });
  299. _heartbeatThread = std::thread([this]() {
  300. try {
  301. R::Object controllerRecord;
  302. std::unique_ptr<R::Connection> rdb;
  303. {
  304. char publicId[1024];
  305. //char secretId[1024];
  306. char hostname[1024];
  307. this->_myId.toString(false,publicId);
  308. //this->_myId.toString(true,secretId);
  309. if (gethostname(hostname,sizeof(hostname)) != 0) {
  310. hostname[0] = (char)0;
  311. } else {
  312. for(int i=0;i<sizeof(hostname);++i) {
  313. if ((hostname[i] == '.')||(hostname[i] == 0)) {
  314. hostname[i] = (char)0;
  315. break;
  316. }
  317. }
  318. }
  319. controllerRecord["id"] = this->_myAddressStr.c_str();
  320. controllerRecord["publicIdentity"] = publicId;
  321. //controllerRecord["secretIdentity"] = secretId;
  322. if (hostname[0])
  323. controllerRecord["clusterHost"] = hostname;
  324. controllerRecord["vMajor"] = ZEROTIER_ONE_VERSION_MAJOR;
  325. controllerRecord["vMinor"] = ZEROTIER_ONE_VERSION_MINOR;
  326. controllerRecord["vRev"] = ZEROTIER_ONE_VERSION_REVISION;
  327. controllerRecord["vBuild"] = ZEROTIER_ONE_VERSION_BUILD;
  328. }
  329. while (_run == 1) {
  330. try {
  331. if (!rdb)
  332. rdb = R::connect(this->_host,this->_port,this->_auth);
  333. if (rdb) {
  334. controllerRecord["lastAlive"] = OSUtils::now();
  335. //printf("HEARTBEAT: %s" ZT_EOL_S,tmp);
  336. R::db(this->_db).table("Controller",R::optargs("read_mode","outdated")).insert(controllerRecord,R::optargs("conflict","update")).run(*rdb);
  337. }
  338. } catch ( ... ) {
  339. rdb.reset();
  340. }
  341. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  342. }
  343. } catch ( ... ) {}
  344. });
  345. }
  346. RethinkDB::~RethinkDB()
  347. {
  348. _run = 0;
  349. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  350. _commitQueue.stop();
  351. for(int t=0;t<ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS;++t)
  352. _commitThread[t].join();
  353. if (_membersDbWatcherConnection)
  354. ((R::Connection *)_membersDbWatcherConnection)->close();
  355. if (_networksDbWatcherConnection)
  356. ((R::Connection *)_networksDbWatcherConnection)->close();
  357. _membersDbWatcher.join();
  358. _networksDbWatcher.join();
  359. _heartbeatThread.join();
  360. _onlineNotificationThread.join();
  361. }
  362. bool RethinkDB::waitForReady()
  363. {
  364. while (_ready > 0) {
  365. if (!_waitNoticePrinted) {
  366. _waitNoticePrinted = true;
  367. fprintf(stderr,"NOTICE: %.10llx controller RethinkDB waiting for initial data download..." ZT_EOL_S,(unsigned long long)_myAddress.toInt());
  368. }
  369. _readyLock.lock();
  370. _readyLock.unlock();
  371. }
  372. return true;
  373. }
  374. void RethinkDB::save(nlohmann::json *orig,nlohmann::json &record)
  375. {
  376. if (!record.is_object()) // sanity check
  377. return;
  378. waitForReady();
  379. if (orig) {
  380. if (*orig != record) {
  381. nlohmann::json *q = new nlohmann::json();
  382. try {
  383. record["revision"] = OSUtils::jsonInt(record["revision"],0ULL) + 1;
  384. for(auto kv=record.begin();kv!=record.end();++kv) {
  385. if ((kv.key() == "id")||(kv.key() == "nwid")||(kv.key() == "objtype")||((*q)[kv.key()] != kv.value()))
  386. (*q)[kv.key()] = kv.value();
  387. }
  388. } catch ( ... ) {
  389. delete q;
  390. throw;
  391. }
  392. }
  393. } else {
  394. record["revision"] = 1;
  395. _commitQueue.post(new nlohmann::json(record));
  396. }
  397. }
  398. void RethinkDB::eraseNetwork(const uint64_t networkId)
  399. {
  400. char tmp2[24];
  401. waitForReady();
  402. Utils::hex(networkId,tmp2);
  403. json *tmp = new json();
  404. (*tmp)["id"] = tmp2;
  405. (*tmp)["objtype"] = "_delete_network"; // pseudo-type, tells thread to delete network
  406. _commitQueue.post(tmp);
  407. }
  408. void RethinkDB::eraseMember(const uint64_t networkId,const uint64_t memberId)
  409. {
  410. char tmp2[24];
  411. json *tmp = new json();
  412. waitForReady();
  413. Utils::hex(networkId,tmp2);
  414. (*tmp)["nwid"] = tmp2;
  415. Utils::hex10(memberId,tmp2);
  416. (*tmp)["id"] = tmp2;
  417. (*tmp)["objtype"] = "_delete_member"; // pseudo-type, tells thread to delete network
  418. _commitQueue.post(tmp);
  419. }
  420. void RethinkDB::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress)
  421. {
  422. std::lock_guard<std::mutex> l(_lastOnline_l);
  423. std::pair<int64_t,InetAddress> &i = _lastOnline[std::pair<uint64_t,uint64_t>(networkId,memberId)];
  424. i.first = OSUtils::now();
  425. if (physicalAddress)
  426. i.second = physicalAddress;
  427. }
  428. } // namespace ZeroTier
  429. #endif // ZT_CONTROLLER_USE_RETHINKDB