PostgreSQL.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "DB.hpp"
  14. #ifdef ZT_CONTROLLER_USE_LIBPQ
  15. #ifndef ZT_CONTROLLER_LIBPQ_HPP
  16. #define ZT_CONTROLLER_LIBPQ_HPP
  17. #define ZT_CENTRAL_CONTROLLER_COMMIT_THREADS 4
  18. #include "ConnectionPool.hpp"
  19. #include <pqxx/pqxx>
  20. #include <memory>
  21. #include <redis++/redis++.h>
  22. #include "../node/Metrics.hpp"
  23. extern "C" {
  24. typedef struct pg_conn PGconn;
  25. }
  26. namespace smeeclient {
  27. struct SmeeClient;
  28. }
  29. namespace ZeroTier {
  30. struct RedisConfig;
  31. class PostgresConnection : public Connection {
  32. public:
  33. virtual ~PostgresConnection() {
  34. }
  35. std::shared_ptr<pqxx::connection> c;
  36. int a;
  37. };
  38. class PostgresConnFactory : public ConnectionFactory {
  39. public:
  40. PostgresConnFactory(std::string &connString)
  41. : m_connString(connString)
  42. {
  43. }
  44. virtual std::shared_ptr<Connection> create() {
  45. Metrics::conn_counter++;
  46. auto c = std::shared_ptr<PostgresConnection>(new PostgresConnection());
  47. c->c = std::make_shared<pqxx::connection>(m_connString);
  48. return std::static_pointer_cast<Connection>(c);
  49. }
  50. private:
  51. std::string m_connString;
  52. };
  53. class PostgreSQL;
  54. class MemberNotificationReceiver : public pqxx::notification_receiver {
  55. public:
  56. MemberNotificationReceiver(PostgreSQL *p, pqxx::connection &c, const std::string &channel);
  57. virtual ~MemberNotificationReceiver() {
  58. fprintf(stderr, "MemberNotificationReceiver destroyed\n");
  59. }
  60. virtual void operator() (const std::string &payload, int backendPid);
  61. private:
  62. PostgreSQL *_psql;
  63. };
  64. class NetworkNotificationReceiver : public pqxx::notification_receiver {
  65. public:
  66. NetworkNotificationReceiver(PostgreSQL *p, pqxx::connection &c, const std::string &channel);
  67. virtual ~NetworkNotificationReceiver() {
  68. fprintf(stderr, "NetworkNotificationReceiver destroyed\n");
  69. };
  70. virtual void operator() (const std::string &payload, int packend_pid);
  71. private:
  72. PostgreSQL *_psql;
  73. };
  74. /**
  75. * A controller database driver that talks to PostgreSQL
  76. *
  77. * This is for use with ZeroTier Central. Others are free to build and use it
  78. * but be aware that we might change it at any time.
  79. */
  80. class PostgreSQL : public DB
  81. {
  82. friend class MemberNotificationReceiver;
  83. friend class NetworkNotificationReceiver;
  84. public:
  85. PostgreSQL(const Identity &myId, const char *path, int listenPort, RedisConfig *rc);
  86. virtual ~PostgreSQL();
  87. virtual bool waitForReady();
  88. virtual bool isReady();
  89. virtual bool save(nlohmann::json &record,bool notifyListeners);
  90. virtual void eraseNetwork(const uint64_t networkId);
  91. virtual void eraseMember(const uint64_t networkId, const uint64_t memberId);
  92. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress &physicalAddress);
  93. virtual AuthInfo getSSOAuthInfo(const nlohmann::json &member, const std::string &redirectURL);
  94. protected:
  95. struct _PairHasher
  96. {
  97. inline std::size_t operator()(const std::pair<uint64_t,uint64_t> &p) const { return (std::size_t)(p.first ^ p.second); }
  98. };
  99. virtual void _memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners) {
  100. DB::_memberChanged(old, memberConfig, notifyListeners);
  101. }
  102. virtual void _networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners) {
  103. DB::_networkChanged(old, networkConfig, notifyListeners);
  104. }
  105. private:
  106. void initializeNetworks();
  107. void initializeMembers();
  108. void heartbeat();
  109. void membersDbWatcher();
  110. void _membersWatcher_Postgres();
  111. void networksDbWatcher();
  112. void _networksWatcher_Postgres();
  113. void _membersWatcher_Redis();
  114. void _networksWatcher_Redis();
  115. void commitThread();
  116. void onlineNotificationThread();
  117. void onlineNotification_Postgres();
  118. void onlineNotification_Redis();
  119. uint64_t _doRedisUpdate(sw::redis::Transaction &tx, std::string &controllerId,
  120. std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > &lastOnline);
  121. void configureSmee();
  122. void notifyNewMember(const std::string &networkID, const std::string &memberID);
  123. enum OverrideMode {
  124. ALLOW_PGBOUNCER_OVERRIDE = 0,
  125. NO_OVERRIDE = 1
  126. };
  127. std::shared_ptr<ConnectionPool<PostgresConnection> > _pool;
  128. const Identity _myId;
  129. const Address _myAddress;
  130. std::string _myAddressStr;
  131. std::string _connString;
  132. BlockingQueue< std::pair<nlohmann::json,bool> > _commitQueue;
  133. std::thread _heartbeatThread;
  134. std::thread _membersDbWatcher;
  135. std::thread _networksDbWatcher;
  136. std::thread _commitThread[ZT_CENTRAL_CONTROLLER_COMMIT_THREADS];
  137. std::thread _onlineNotificationThread;
  138. std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > _lastOnline;
  139. mutable std::mutex _lastOnline_l;
  140. mutable std::mutex _readyLock;
  141. std::atomic<int> _ready, _connected, _run;
  142. mutable volatile bool _waitNoticePrinted;
  143. int _listenPort;
  144. uint8_t _ssoPsk[48];
  145. RedisConfig *_rc;
  146. std::shared_ptr<sw::redis::Redis> _redis;
  147. std::shared_ptr<sw::redis::RedisCluster> _cluster;
  148. bool _redisMemberStatus;
  149. smeeclient::SmeeClient *_smee;
  150. };
  151. } // namespace ZeroTier
  152. #endif // ZT_CONTROLLER_LIBPQ_HPP
  153. #endif // ZT_CONTROLLER_USE_LIBPQ