CV1.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* (c) ZeroTier, Inc.
  2. * See LICENSE.txt in nonfree/
  3. */
  4. #include "DB.hpp"
  5. #ifdef ZT_CONTROLLER_USE_LIBPQ
  6. #ifndef ZT_CONTROLLER_CV1_HPP
  7. #define ZT_CONTROLLER_CV1_HPP
  8. #define ZT_CENTRAL_CONTROLLER_COMMIT_THREADS 4
  9. #include "../../node/Metrics.hpp"
  10. #include "ConnectionPool.hpp"
  11. #include "PostgreSQL.hpp"
  12. #include <memory>
  13. #include <pqxx/pqxx>
  14. #include <sw/redis++/redis++.h>
  15. namespace rustybits {
  16. struct SmeeClient;
  17. }
  18. namespace ZeroTier {
  19. struct RedisConfig;
  20. /**
  21. * A controller database driver that talks to PostgreSQL
  22. *
  23. * This is for use with ZeroTier Central. Others are free to build and use it
  24. * but be aware that we might change it at any time.
  25. */
  26. class CV1 : public DB {
  27. friend class MemberNotificationReceiver<CV1>;
  28. friend class NetworkNotificationReceiver<CV1>;
  29. public:
  30. CV1(const Identity& myId, const char* path, int listenPort, RedisConfig* rc);
  31. virtual ~CV1();
  32. virtual bool waitForReady();
  33. virtual bool isReady();
  34. virtual bool save(nlohmann::json& record, bool notifyListeners);
  35. virtual void eraseNetwork(const uint64_t networkId);
  36. virtual void eraseMember(const uint64_t networkId, const uint64_t memberId);
  37. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress);
  38. virtual void nodeIsOnline(
  39. const uint64_t networkId,
  40. const uint64_t memberId,
  41. const InetAddress& physicalAddress,
  42. const char* osArch);
  43. virtual AuthInfo getSSOAuthInfo(const nlohmann::json& member, const std::string& redirectURL);
  44. virtual bool ready()
  45. {
  46. return _ready == 2;
  47. }
  48. virtual void _memberChanged(nlohmann::json& old, nlohmann::json& memberConfig, bool notifyListeners)
  49. {
  50. DB::_memberChanged(old, memberConfig, notifyListeners);
  51. }
  52. virtual void _networkChanged(nlohmann::json& old, nlohmann::json& networkConfig, bool notifyListeners)
  53. {
  54. DB::_networkChanged(old, networkConfig, notifyListeners);
  55. }
  56. protected:
  57. struct _PairHasher {
  58. inline std::size_t operator()(const std::pair<uint64_t, uint64_t>& p) const
  59. {
  60. return (std::size_t)(p.first ^ p.second);
  61. }
  62. };
  63. private:
  64. void initializeNetworks();
  65. void initializeMembers();
  66. void heartbeat();
  67. void membersDbWatcher();
  68. void _membersWatcher_Postgres();
  69. void networksDbWatcher();
  70. void _networksWatcher_Postgres();
  71. void _membersWatcher_Redis();
  72. void _networksWatcher_Redis();
  73. void commitThread();
  74. void onlineNotificationThread();
  75. void onlineNotification_Postgres();
  76. void onlineNotification_Redis();
  77. uint64_t _doRedisUpdate(
  78. sw::redis::Transaction& tx,
  79. std::string& controllerId,
  80. std::unordered_map<std::pair<uint64_t, uint64_t>, NodeOnlineRecord, _PairHasher>& lastOnline);
  81. void configureSmee();
  82. void notifyNewMember(const std::string& networkID, const std::string& memberID);
  83. enum OverrideMode { ALLOW_PGBOUNCER_OVERRIDE = 0, NO_OVERRIDE = 1 };
  84. std::shared_ptr<ConnectionPool<PostgresConnection> > _pool;
  85. const Identity _myId;
  86. const Address _myAddress;
  87. std::string _myAddressStr;
  88. std::string _connString;
  89. BlockingQueue<std::pair<nlohmann::json, bool> > _commitQueue;
  90. std::thread _heartbeatThread;
  91. std::thread _membersDbWatcher;
  92. std::thread _networksDbWatcher;
  93. std::thread _commitThread[ZT_CENTRAL_CONTROLLER_COMMIT_THREADS];
  94. std::thread _onlineNotificationThread;
  95. std::unordered_map<std::pair<uint64_t, uint64_t>, NodeOnlineRecord, _PairHasher> _lastOnline;
  96. mutable std::mutex _lastOnline_l;
  97. mutable std::mutex _readyLock;
  98. std::atomic<int> _ready, _connected, _run;
  99. mutable volatile bool _waitNoticePrinted;
  100. int _listenPort;
  101. uint8_t _ssoPsk[48];
  102. RedisConfig* _rc;
  103. std::shared_ptr<sw::redis::Redis> _redis;
  104. std::shared_ptr<sw::redis::RedisCluster> _cluster;
  105. bool _redisMemberStatus;
  106. rustybits::SmeeClient* _smee;
  107. };
  108. } // namespace ZeroTier
  109. #endif // ZT_CONTROLLER_CV1_HPP
  110. #endif // ZT_CONTROLLER_USE_LIBPQ