PostgreSQL.hpp 2.7 KB

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