PostgreSQL.hpp 2.8 KB

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