PostgreSQL.hpp 5.0 KB

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