CentralDB.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #ifdef ZT_CONTROLLER_USE_LIBPQ
  2. #ifndef ZT_CONTROLLER_CENTRAL_DB_HPP
  3. #define ZT_CONTROLLER_CENTRAL_DB_HPP
  4. #define ZT_CENTRAL_CONTROLLER_COMMIT_THREADS 4
  5. #include "../../node/Metrics.hpp"
  6. #include "ConnectionPool.hpp"
  7. #include "DB.hpp"
  8. #include "NotificationListener.hpp"
  9. #include "PostgreSQL.hpp"
  10. #include "StatusWriter.hpp"
  11. #include <memory>
  12. #include <pqxx/pqxx>
  13. #include <sw/redis++/redis++.h>
  14. namespace rustybits {
  15. struct SmeeClient;
  16. }
  17. namespace ZeroTier {
  18. struct RedisConfig;
  19. struct ControllerConfig;
  20. struct ControllerChangeNotifier;
  21. class CentralDB : public DB {
  22. public:
  23. enum ListenerMode {
  24. LISTENER_MODE_PGSQL = 0,
  25. LISTENER_MODE_REDIS = 1,
  26. LISTENER_MODE_PUBSUB = 2,
  27. };
  28. enum StatusWriterMode {
  29. STATUS_WRITER_MODE_PGSQL = 0,
  30. STATUS_WRITER_MODE_REDIS = 1,
  31. STATUS_WRITER_MODE_BIGTABLE = 2,
  32. };
  33. CentralDB(
  34. const Identity& myId,
  35. const char* connString,
  36. int listenPort,
  37. CentralDB::ListenerMode mode,
  38. CentralDB::StatusWriterMode statusMode,
  39. const ControllerConfig* cc);
  40. virtual ~CentralDB();
  41. virtual bool waitForReady();
  42. virtual bool isReady();
  43. virtual bool save(nlohmann::json& record, bool notifyListeners);
  44. virtual void eraseNetwork(const uint64_t networkId);
  45. virtual void eraseMember(const uint64_t networkId, const uint64_t memberId);
  46. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress);
  47. virtual void nodeIsOnline(
  48. const uint64_t networkId,
  49. const uint64_t memberId,
  50. const InetAddress& physicalAddress,
  51. const char* osArch);
  52. virtual AuthInfo getSSOAuthInfo(const nlohmann::json& member, const std::string& redirectURL);
  53. virtual bool ready()
  54. {
  55. return _ready == 2;
  56. }
  57. virtual void _memberChanged(nlohmann::json& old, nlohmann::json& memberConfig, bool notifyListeners)
  58. {
  59. DB::_memberChanged(old, memberConfig, notifyListeners);
  60. }
  61. virtual void _networkChanged(nlohmann::json& old, nlohmann::json& networkConfig, bool notifyListeners)
  62. {
  63. DB::_networkChanged(old, networkConfig, notifyListeners);
  64. }
  65. protected:
  66. struct _PairHasher {
  67. inline std::size_t operator()(const std::pair<uint64_t, uint64_t>& p) const
  68. {
  69. return (std::size_t)(p.first ^ p.second);
  70. }
  71. };
  72. private:
  73. void initializeNetworks();
  74. void initializeMembers();
  75. void heartbeat();
  76. void commitThread();
  77. void onlineNotificationThread();
  78. void configureSmee();
  79. void notifyNewMember(const std::string& networkID, const std::string& memberID);
  80. nlohmann::json _getNetworkMember(pqxx::work& tx, const std::string networkID, const std::string memberID);
  81. nlohmann::json _getNetwork(pqxx::work& tx, const std::string networkID);
  82. private:
  83. enum OverrideMode { ALLOW_PGBOUNCER_OVERRIDE = 0, NO_OVERRIDE = 1 };
  84. ListenerMode _listenerMode;
  85. StatusWriterMode _statusWriterMode;
  86. const ControllerConfig* _cc;
  87. std::shared_ptr<ConnectionPool<PostgresConnection> > _pool;
  88. const Identity _myId;
  89. const Address _myAddress;
  90. std::string _myAddressStr;
  91. std::string _connString;
  92. BlockingQueue<std::pair<nlohmann::json, bool> > _commitQueue;
  93. std::thread _heartbeatThread;
  94. std::shared_ptr<NotificationListener> _membersDbWatcher;
  95. std::shared_ptr<NotificationListener> _networksDbWatcher;
  96. std::shared_ptr<StatusWriter> _statusWriter;
  97. std::shared_ptr<ControllerChangeNotifier> _changeNotifier;
  98. std::thread _commitThread[ZT_CENTRAL_CONTROLLER_COMMIT_THREADS];
  99. std::thread _onlineNotificationThread;
  100. std::unordered_map<std::pair<uint64_t, uint64_t>, NodeOnlineRecord, _PairHasher> _lastOnline;
  101. mutable std::mutex _lastOnline_l;
  102. mutable std::mutex _readyLock;
  103. std::atomic<int> _ready, _connected, _run;
  104. mutable volatile bool _waitNoticePrinted;
  105. int _listenPort;
  106. uint8_t _ssoPsk[48];
  107. std::shared_ptr<sw::redis::Redis> _redis;
  108. std::shared_ptr<sw::redis::RedisCluster> _cluster;
  109. bool _redisMemberStatus;
  110. rustybits::SmeeClient* _smee;
  111. };
  112. } // namespace ZeroTier
  113. #endif // ZT_CONTROLLER_CENTRAL_DB_HPP
  114. #endif // ZT_CONTROLLER_USE_LIBPQ