CentralDB.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <memory>
  11. #include <pqxx/pqxx>
  12. #include <sw/redis++/redis++.h>
  13. namespace rustybits {
  14. struct SmeeClient;
  15. }
  16. namespace ZeroTier {
  17. struct RedisConfig;
  18. struct PubSubConfig;
  19. struct PostgresNotifyConfig;
  20. struct ControllerConfig {
  21. bool ssoEnabled;
  22. RedisConfig* redisConfig;
  23. PubSubConfig* pubSubConfig;
  24. PostgresNotifyConfig* postgresNotifyConfig;
  25. };
  26. class CentralDB : public DB {
  27. public:
  28. enum ListenerMode {
  29. LISTENER_MODE_PGSQL = 0,
  30. LISTENER_MODE_REDIS = 1,
  31. LISTENER_MODE_PUBSUB = 2,
  32. };
  33. CentralDB(const Identity& myId, const char* path, int listenPort, CentralDB::ListenerMode mode, ControllerConfig* cc);
  34. virtual ~CentralDB();
  35. virtual bool waitForReady();
  36. virtual bool isReady();
  37. virtual bool save(nlohmann::json& record, bool notifyListeners);
  38. virtual void eraseNetwork(const uint64_t networkId);
  39. virtual void eraseMember(const uint64_t networkId, const uint64_t memberId);
  40. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress);
  41. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress, const char* osArch);
  42. virtual AuthInfo getSSOAuthInfo(const nlohmann::json& member, const std::string& redirectURL);
  43. virtual bool ready()
  44. {
  45. return _ready == 2;
  46. }
  47. virtual void _memberChanged(nlohmann::json& old, nlohmann::json& memberConfig, bool notifyListeners)
  48. {
  49. DB::_memberChanged(old, memberConfig, notifyListeners);
  50. }
  51. virtual void _networkChanged(nlohmann::json& old, nlohmann::json& networkConfig, bool notifyListeners)
  52. {
  53. DB::_networkChanged(old, networkConfig, notifyListeners);
  54. }
  55. protected:
  56. struct _PairHasher {
  57. inline std::size_t operator()(const std::pair<uint64_t, uint64_t>& p) const
  58. {
  59. return (std::size_t)(p.first ^ p.second);
  60. }
  61. };
  62. private:
  63. void initializeNetworks();
  64. void initializeMembers();
  65. void heartbeat();
  66. void commitThread();
  67. void onlineNotificationThread();
  68. void onlineNotification_Postgres();
  69. void onlineNotification_Redis();
  70. uint64_t _doRedisUpdate(sw::redis::Transaction& tx, std::string& controllerId, std::unordered_map<std::pair<uint64_t, uint64_t>, NodeOnlineRecord, _PairHasher>& lastOnline);
  71. void configureSmee();
  72. void notifyNewMember(const std::string& networkID, const std::string& memberID);
  73. enum OverrideMode { ALLOW_PGBOUNCER_OVERRIDE = 0, NO_OVERRIDE = 1 };
  74. ListenerMode _listenerMode;
  75. ControllerConfig* _controllerConfig;
  76. std::shared_ptr<ConnectionPool<PostgresConnection> > _pool;
  77. const Identity _myId;
  78. const Address _myAddress;
  79. std::string _myAddressStr;
  80. std::string _connString;
  81. BlockingQueue<std::pair<nlohmann::json, bool> > _commitQueue;
  82. std::thread _heartbeatThread;
  83. std::shared_ptr<NotificationListener> _membersDbWatcher;
  84. std::shared_ptr<NotificationListener> _networksDbWatcher;
  85. std::thread _commitThread[ZT_CENTRAL_CONTROLLER_COMMIT_THREADS];
  86. std::thread _onlineNotificationThread;
  87. std::unordered_map<std::pair<uint64_t, uint64_t>, NodeOnlineRecord, _PairHasher> _lastOnline;
  88. mutable std::mutex _lastOnline_l;
  89. mutable std::mutex _readyLock;
  90. std::atomic<int> _ready, _connected, _run;
  91. mutable volatile bool _waitNoticePrinted;
  92. int _listenPort;
  93. uint8_t _ssoPsk[48];
  94. RedisConfig* _rc;
  95. std::shared_ptr<sw::redis::Redis> _redis;
  96. std::shared_ptr<sw::redis::RedisCluster> _cluster;
  97. bool _redisMemberStatus;
  98. rustybits::SmeeClient* _smee;
  99. };
  100. } // namespace ZeroTier
  101. #endif // ZT_CONTROLLER_CENTRAL_DB_HPP
  102. #endif // ZT_CONTROLLER_USE_LIBPQ