PostgreSQL.hpp 5.3 KB

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