PostgreSQL.hpp 5.0 KB

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