DBMirrorSet.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "DBMirrorSet.hpp"
  14. namespace ZeroTier {
  15. DBMirrorSet::DBMirrorSet(DB::ChangeListener *listener)
  16. : _listener(listener)
  17. , _running(true)
  18. , _syncCheckerThread()
  19. , _dbs()
  20. , _dbs_l()
  21. {
  22. _syncCheckerThread = std::thread([this]() {
  23. for(;;) {
  24. for(int i=0;i<120;++i) { // 1 minute delay between checks
  25. if (!_running)
  26. return;
  27. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  28. }
  29. std::vector< std::shared_ptr<DB> > dbs;
  30. {
  31. std::unique_lock<std::shared_mutex> l(_dbs_l);
  32. if (_dbs.size() <= 1)
  33. continue; // no need to do this if there's only one DB, so skip the iteration
  34. dbs = _dbs;
  35. }
  36. for(auto db=dbs.begin();db!=dbs.end();++db) {
  37. (*db)->each([&dbs,&db](uint64_t networkId,const nlohmann::json &network,uint64_t memberId,const nlohmann::json &member) {
  38. try {
  39. if (network.is_object()) {
  40. if (memberId == 0) {
  41. for(auto db2=dbs.begin();db2!=dbs.end();++db2) {
  42. if (db->get() != db2->get()) {
  43. nlohmann::json nw2;
  44. if ((!(*db2)->get(networkId,nw2))||((nw2.is_object())&&(OSUtils::jsonInt(nw2["revision"],0) < OSUtils::jsonInt(network["revision"],0)))) {
  45. nw2 = network;
  46. (*db2)->save(nw2,false);
  47. }
  48. }
  49. }
  50. } else if (member.is_object()) {
  51. for(auto db2=dbs.begin();db2!=dbs.end();++db2) {
  52. if (db->get() != db2->get()) {
  53. nlohmann::json nw2,m2;
  54. if ((!(*db2)->get(networkId,nw2,memberId,m2))||((m2.is_object())&&(OSUtils::jsonInt(m2["revision"],0) < OSUtils::jsonInt(member["revision"],0)))) {
  55. m2 = member;
  56. (*db2)->save(m2,false);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. } catch ( ... ) {} // skip entries that generate JSON errors
  63. });
  64. }
  65. }
  66. });
  67. }
  68. DBMirrorSet::~DBMirrorSet()
  69. {
  70. _running = false;
  71. _syncCheckerThread.join();
  72. }
  73. bool DBMirrorSet::hasNetwork(const uint64_t networkId) const
  74. {
  75. std::shared_lock<std::shared_mutex> l(_dbs_l);
  76. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  77. if ((*d)->hasNetwork(networkId))
  78. return true;
  79. }
  80. return false;
  81. }
  82. bool DBMirrorSet::get(const uint64_t networkId,nlohmann::json &network)
  83. {
  84. std::shared_lock<std::shared_mutex> l(_dbs_l);
  85. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  86. if ((*d)->get(networkId,network)) {
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. bool DBMirrorSet::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member)
  93. {
  94. std::shared_lock<std::shared_mutex> l(_dbs_l);
  95. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  96. if ((*d)->get(networkId,network,memberId,member))
  97. return true;
  98. }
  99. return false;
  100. }
  101. bool DBMirrorSet::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,DB::NetworkSummaryInfo &info)
  102. {
  103. std::shared_lock<std::shared_mutex> l(_dbs_l);
  104. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  105. if ((*d)->get(networkId,network,memberId,member,info))
  106. return true;
  107. }
  108. return false;
  109. }
  110. bool DBMirrorSet::get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members)
  111. {
  112. std::shared_lock<std::shared_mutex> l(_dbs_l);
  113. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  114. if ((*d)->get(networkId,network,members))
  115. return true;
  116. }
  117. return false;
  118. }
  119. AuthInfo DBMirrorSet::getSSOAuthInfo(const nlohmann::json &member, const std::string &redirectURL)
  120. {
  121. std::shared_lock<std::shared_mutex> l(_dbs_l);
  122. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  123. AuthInfo info = (*d)->getSSOAuthInfo(member, redirectURL);
  124. if (info.enabled) {
  125. return info;
  126. }
  127. }
  128. return AuthInfo();
  129. }
  130. void DBMirrorSet::networks(std::set<uint64_t> &networks)
  131. {
  132. std::shared_lock<std::shared_mutex> l(_dbs_l);
  133. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  134. (*d)->networks(networks);
  135. }
  136. }
  137. bool DBMirrorSet::waitForReady()
  138. {
  139. bool r = false;
  140. std::shared_lock<std::shared_mutex> l(_dbs_l);
  141. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  142. r |= (*d)->waitForReady();
  143. }
  144. return r;
  145. }
  146. bool DBMirrorSet::isReady()
  147. {
  148. std::shared_lock<std::shared_mutex> l(_dbs_l);
  149. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  150. if (!(*d)->isReady())
  151. return false;
  152. }
  153. return true;
  154. }
  155. bool DBMirrorSet::save(nlohmann::json &record,bool notifyListeners)
  156. {
  157. std::vector< std::shared_ptr<DB> > dbs;
  158. {
  159. std::unique_lock<std::shared_mutex> l(_dbs_l);
  160. dbs = _dbs;
  161. }
  162. if (notifyListeners) {
  163. for(auto d=dbs.begin();d!=dbs.end();++d) {
  164. if ((*d)->save(record,true))
  165. return true;
  166. }
  167. return false;
  168. } else {
  169. bool modified = false;
  170. for(auto d=dbs.begin();d!=dbs.end();++d) {
  171. modified |= (*d)->save(record,false);
  172. }
  173. return modified;
  174. }
  175. }
  176. void DBMirrorSet::eraseNetwork(const uint64_t networkId)
  177. {
  178. std::unique_lock<std::shared_mutex> l(_dbs_l);
  179. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  180. (*d)->eraseNetwork(networkId);
  181. }
  182. }
  183. void DBMirrorSet::eraseMember(const uint64_t networkId,const uint64_t memberId)
  184. {
  185. std::unique_lock<std::shared_mutex> l(_dbs_l);
  186. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  187. (*d)->eraseMember(networkId,memberId);
  188. }
  189. }
  190. void DBMirrorSet::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress)
  191. {
  192. std::shared_lock<std::shared_mutex> l(_dbs_l);
  193. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  194. (*d)->nodeIsOnline(networkId,memberId,physicalAddress);
  195. }
  196. }
  197. void DBMirrorSet::onNetworkUpdate(const void *db,uint64_t networkId,const nlohmann::json &network)
  198. {
  199. nlohmann::json record(network);
  200. std::unique_lock<std::shared_mutex> l(_dbs_l);
  201. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  202. if (d->get() != db) {
  203. (*d)->save(record,false);
  204. }
  205. }
  206. _listener->onNetworkUpdate(this,networkId,network);
  207. }
  208. void DBMirrorSet::onNetworkMemberUpdate(const void *db,uint64_t networkId,uint64_t memberId,const nlohmann::json &member)
  209. {
  210. nlohmann::json record(member);
  211. std::unique_lock<std::shared_mutex> l(_dbs_l);
  212. for(auto d=_dbs.begin();d!=_dbs.end();++d) {
  213. if (d->get() != db) {
  214. (*d)->save(record,false);
  215. }
  216. }
  217. _listener->onNetworkMemberUpdate(this,networkId,memberId,member);
  218. }
  219. void DBMirrorSet::onNetworkMemberDeauthorize(const void *db,uint64_t networkId,uint64_t memberId)
  220. {
  221. _listener->onNetworkMemberDeauthorize(this,networkId,memberId);
  222. }
  223. } // namespace ZeroTier