PostgreSQL.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include "DB.hpp"
  27. #ifdef ZT_CONTROLLER_USE_LIBPQ
  28. #ifndef ZT_CONTROLLER_LIBPQ_HPP
  29. #define ZT_CONTROLLER_LIBPQ_HPP
  30. #define ZT_CENTRAL_CONTROLLER_COMMIT_THREADS 4
  31. extern "C" {
  32. typedef struct pg_conn PGconn;
  33. }
  34. namespace ZeroTier {
  35. struct MQConfig;
  36. /**
  37. * A controller database driver that talks to PostgreSQL
  38. *
  39. * This is for use with ZeroTier Central. Others are free to build and use it
  40. * but be aware taht we might change it at any time.
  41. */
  42. class PostgreSQL : public DB
  43. {
  44. public:
  45. PostgreSQL(const Identity &myId, const char *path, int listenPort, MQConfig *mqc = NULL);
  46. virtual ~PostgreSQL();
  47. virtual bool waitForReady();
  48. virtual bool isReady();
  49. virtual bool save(nlohmann::json &record,bool notifyListeners);
  50. virtual void eraseNetwork(const uint64_t networkId);
  51. virtual void eraseMember(const uint64_t networkId, const uint64_t memberId);
  52. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress &physicalAddress);
  53. protected:
  54. struct _PairHasher
  55. {
  56. inline std::size_t operator()(const std::pair<uint64_t,uint64_t> &p) const { return (std::size_t)(p.first ^ p.second); }
  57. };
  58. private:
  59. void initializeNetworks(PGconn *conn);
  60. void initializeMembers(PGconn *conn);
  61. void heartbeat();
  62. void membersDbWatcher();
  63. void _membersWatcher_Postgres(PGconn *conn);
  64. void _membersWatcher_RabbitMQ();
  65. void networksDbWatcher();
  66. void _networksWatcher_Postgres(PGconn *conn);
  67. void _networksWatcher_RabbitMQ();
  68. void commitThread();
  69. void onlineNotificationThread();
  70. enum OverrideMode {
  71. ALLOW_PGBOUNCER_OVERRIDE = 0,
  72. NO_OVERRIDE = 1
  73. };
  74. PGconn * getPgConn( OverrideMode m = ALLOW_PGBOUNCER_OVERRIDE );
  75. const Identity _myId;
  76. const Address _myAddress;
  77. std::string _myAddressStr;
  78. std::string _connString;
  79. BlockingQueue< std::pair<nlohmann::json,bool> > _commitQueue;
  80. std::thread _heartbeatThread;
  81. std::thread _membersDbWatcher;
  82. std::thread _networksDbWatcher;
  83. std::thread _commitThread[ZT_CENTRAL_CONTROLLER_COMMIT_THREADS];
  84. std::thread _onlineNotificationThread;
  85. std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > _lastOnline;
  86. mutable std::mutex _lastOnline_l;
  87. mutable std::mutex _readyLock;
  88. std::atomic<int> _ready, _connected, _run;
  89. mutable volatile bool _waitNoticePrinted;
  90. int _listenPort;
  91. MQConfig *_mqc;
  92. };
  93. } // namespace ZeroTier
  94. #endif // ZT_CONTROLLER_LIBPQ_HPP
  95. #endif // ZT_CONTROLLER_USE_LIBPQ