2
0

PostgreSQL.hpp 5.1 KB

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