PostgreSQL.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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: 2023-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. extern "C" {
  19. typedef struct pg_conn PGconn;
  20. }
  21. namespace ZeroTier {
  22. struct MQConfig;
  23. /**
  24. * A controller database driver that talks to PostgreSQL
  25. *
  26. * This is for use with ZeroTier Central. Others are free to build and use it
  27. * but be aware taht we might change it at any time.
  28. */
  29. class PostgreSQL : public DB
  30. {
  31. public:
  32. PostgreSQL(const Identity &myId, const char *path, int listenPort, MQConfig *mqc = NULL);
  33. virtual ~PostgreSQL();
  34. virtual bool waitForReady();
  35. virtual bool isReady();
  36. virtual bool save(nlohmann::json &record,bool notifyListeners);
  37. virtual void eraseNetwork(const uint64_t networkId);
  38. virtual void eraseMember(const uint64_t networkId, const uint64_t memberId);
  39. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress &physicalAddress);
  40. protected:
  41. struct _PairHasher
  42. {
  43. inline std::size_t operator()(const std::pair<uint64_t,uint64_t> &p) const { return (std::size_t)(p.first ^ p.second); }
  44. };
  45. private:
  46. void initializeNetworks(PGconn *conn);
  47. void initializeMembers(PGconn *conn);
  48. void heartbeat();
  49. void membersDbWatcher();
  50. void _membersWatcher_Postgres(PGconn *conn);
  51. void _membersWatcher_RabbitMQ();
  52. void networksDbWatcher();
  53. void _networksWatcher_Postgres(PGconn *conn);
  54. void _networksWatcher_RabbitMQ();
  55. void _membersWatcher_Reids();
  56. void _networksWatcher_Redis();
  57. void commitThread();
  58. void onlineNotificationThread();
  59. enum OverrideMode {
  60. ALLOW_PGBOUNCER_OVERRIDE = 0,
  61. NO_OVERRIDE = 1
  62. };
  63. PGconn * getPgConn( OverrideMode m = ALLOW_PGBOUNCER_OVERRIDE );
  64. const Identity _myId;
  65. const Address _myAddress;
  66. std::string _myAddressStr;
  67. std::string _connString;
  68. BlockingQueue< std::pair<nlohmann::json,bool> > _commitQueue;
  69. std::thread _heartbeatThread;
  70. std::thread _membersDbWatcher;
  71. std::thread _networksDbWatcher;
  72. std::thread _commitThread[ZT_CENTRAL_CONTROLLER_COMMIT_THREADS];
  73. std::thread _onlineNotificationThread;
  74. std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > _lastOnline;
  75. mutable std::mutex _lastOnline_l;
  76. mutable std::mutex _readyLock;
  77. std::atomic<int> _ready, _connected, _run;
  78. mutable volatile bool _waitNoticePrinted;
  79. int _listenPort;
  80. MQConfig *_mqc;
  81. };
  82. } // namespace ZeroTier
  83. #endif // ZT_CONTROLLER_LIBPQ_HPP
  84. #endif // ZT_CONTROLLER_USE_LIBPQ