PostgreSQL.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c)2025 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: 2026-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. #ifdef ZT_CONTROLLER_USE_LIBPQ
  14. #ifndef ZT_CONTROLLER_POSTGRESQL_HPP
  15. #define ZT_CONTROLLER_POSTGRESQL_HPP
  16. #include "ConnectionPool.hpp"
  17. #include "DB.hpp"
  18. #include "NotificationListener.hpp"
  19. #include "opentelemetry/trace/provider.h"
  20. #include <memory>
  21. #include <nlohmann/json.hpp>
  22. #include <pqxx/pqxx>
  23. namespace ZeroTier {
  24. extern "C" {
  25. typedef struct pg_conn PGconn;
  26. }
  27. class PostgresConnection : public Connection {
  28. public:
  29. virtual ~PostgresConnection()
  30. {
  31. }
  32. std::shared_ptr<pqxx::connection> c;
  33. int a;
  34. };
  35. class PostgresConnFactory : public ConnectionFactory {
  36. public:
  37. PostgresConnFactory(std::string& connString) : m_connString(connString)
  38. {
  39. }
  40. virtual std::shared_ptr<Connection> create()
  41. {
  42. Metrics::conn_counter++;
  43. auto c = std::shared_ptr<PostgresConnection>(new PostgresConnection());
  44. c->c = std::make_shared<pqxx::connection>(m_connString);
  45. return std::static_pointer_cast<Connection>(c);
  46. }
  47. private:
  48. std::string m_connString;
  49. };
  50. template <typename T> class MemberNotificationReceiver : public pqxx::notification_receiver {
  51. public:
  52. MemberNotificationReceiver(T* p, pqxx::connection& c, const std::string& channel) : pqxx::notification_receiver(c, channel), _psql(p)
  53. {
  54. fprintf(stderr, "initialize MemberNotificationReceiver\n");
  55. }
  56. virtual ~MemberNotificationReceiver()
  57. {
  58. fprintf(stderr, "MemberNotificationReceiver destroyed\n");
  59. }
  60. virtual void operator()(const std::string& payload, int backendPid)
  61. {
  62. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  63. auto tracer = provider->GetTracer("db_member_notification");
  64. auto span = tracer->StartSpan("db_member_notification::operator()");
  65. auto scope = tracer->WithActiveSpan(span);
  66. span->SetAttribute("payload", payload);
  67. span->SetAttribute("psqlReady", _psql->isReady());
  68. fprintf(stderr, "Member Notification received: %s\n", payload.c_str());
  69. Metrics::pgsql_mem_notification++;
  70. nlohmann::json tmp(nlohmann::json::parse(payload));
  71. nlohmann::json& ov = tmp["old_val"];
  72. nlohmann::json& nv = tmp["new_val"];
  73. nlohmann::json oldConfig, newConfig;
  74. if (ov.is_object())
  75. oldConfig = ov;
  76. if (nv.is_object())
  77. newConfig = nv;
  78. if (oldConfig.is_object() && newConfig.is_object()) {
  79. _psql->save(newConfig, _psql->isReady());
  80. fprintf(stderr, "payload sent\n");
  81. }
  82. else if (newConfig.is_object() && ! oldConfig.is_object()) {
  83. // new member
  84. Metrics::member_count++;
  85. _psql->save(newConfig, _psql->isReady());
  86. fprintf(stderr, "new member payload sent\n");
  87. }
  88. else if (! newConfig.is_object() && oldConfig.is_object()) {
  89. // member delete
  90. uint64_t networkId = OSUtils::jsonIntHex(oldConfig["nwid"], 0ULL);
  91. uint64_t memberId = OSUtils::jsonIntHex(oldConfig["id"], 0ULL);
  92. if (memberId && networkId) {
  93. _psql->eraseMember(networkId, memberId);
  94. fprintf(stderr, "member delete payload sent\n");
  95. }
  96. }
  97. }
  98. private:
  99. T* _psql;
  100. };
  101. template <typename T> class NetworkNotificationReceiver : public pqxx::notification_receiver {
  102. public:
  103. NetworkNotificationReceiver(T* p, pqxx::connection& c, const std::string& channel) : pqxx::notification_receiver(c, channel), _psql(p)
  104. {
  105. fprintf(stderr, "initialize NetworkrNotificationReceiver\n");
  106. }
  107. virtual ~NetworkNotificationReceiver()
  108. {
  109. fprintf(stderr, "NetworkNotificationReceiver destroyed\n");
  110. };
  111. virtual void operator()(const std::string& payload, int packend_pid)
  112. {
  113. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  114. auto tracer = provider->GetTracer("db_network_notification");
  115. auto span = tracer->StartSpan("db_network_notification::operator()");
  116. auto scope = tracer->WithActiveSpan(span);
  117. span->SetAttribute("payload", payload);
  118. span->SetAttribute("psqlReady", _psql->isReady());
  119. fprintf(stderr, "Network Notification received: %s\n", payload.c_str());
  120. Metrics::pgsql_net_notification++;
  121. nlohmann::json tmp(nlohmann::json::parse(payload));
  122. nlohmann::json& ov = tmp["old_val"];
  123. nlohmann::json& nv = tmp["new_val"];
  124. nlohmann::json oldConfig, newConfig;
  125. if (ov.is_object())
  126. oldConfig = ov;
  127. if (nv.is_object())
  128. newConfig = nv;
  129. if (oldConfig.is_object() && newConfig.is_object()) {
  130. std::string nwid = oldConfig["id"];
  131. span->SetAttribute("action", "network_change");
  132. span->SetAttribute("network_id", nwid);
  133. _psql->save(newConfig, _psql->isReady());
  134. fprintf(stderr, "payload sent\n");
  135. }
  136. else if (newConfig.is_object() && ! oldConfig.is_object()) {
  137. std::string nwid = newConfig["id"];
  138. span->SetAttribute("network_id", nwid);
  139. span->SetAttribute("action", "new_network");
  140. // new network
  141. _psql->save(newConfig, _psql->isReady());
  142. fprintf(stderr, "new network payload sent\n");
  143. }
  144. else if (! newConfig.is_object() && oldConfig.is_object()) {
  145. // network delete
  146. span->SetAttribute("action", "delete_network");
  147. std::string nwid = oldConfig["id"];
  148. span->SetAttribute("network_id", nwid);
  149. uint64_t networkId = Utils::hexStrToU64(nwid.c_str());
  150. span->SetAttribute("network_id_int", networkId);
  151. if (networkId) {
  152. _psql->eraseNetwork(networkId);
  153. fprintf(stderr, "network delete payload sent\n");
  154. }
  155. }
  156. }
  157. private:
  158. T* _psql;
  159. };
  160. struct NodeOnlineRecord {
  161. uint64_t lastSeen;
  162. InetAddress physicalAddress;
  163. std::string osArch;
  164. };
  165. /**
  166. * internal class for listening to PostgreSQL notification channels.
  167. */
  168. template <typename T> class _notificationReceiver : public pqxx::notification_receiver {
  169. public:
  170. _notificationReceiver(T* p, pqxx::connection& c, const std::string& channel) : pqxx::notification_receiver(c, channel), _listener(p)
  171. {
  172. fprintf(stderr, "initialize PostgresMemberNotificationListener::_notificationReceiver\n");
  173. }
  174. virtual void operator()(const std::string& payload, int backendPid)
  175. {
  176. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  177. auto tracer = provider->GetTracer("notification_receiver");
  178. auto span = tracer->StartSpan("notification_receiver::operator()");
  179. auto scope = tracer->WithActiveSpan(span);
  180. _listener->onNotification(payload);
  181. }
  182. private:
  183. T* _listener;
  184. };
  185. class PostgresMemberListener : public NotificationListener {
  186. public:
  187. PostgresMemberListener(DB* db, std::shared_ptr<ConnectionPool<PostgresConnection> > pool, const std::string& channel, uint64_t timeout);
  188. virtual ~PostgresMemberListener();
  189. virtual void listen();
  190. virtual void onNotification(const std::string& payload) override;
  191. private:
  192. bool _run = false;
  193. DB* _db;
  194. std::shared_ptr<ConnectionPool<PostgresConnection> > _pool;
  195. std::shared_ptr<PostgresConnection> _conn;
  196. uint64_t _notification_timeout;
  197. std::thread _listenerThread;
  198. _notificationReceiver<PostgresMemberListener>* _receiver;
  199. };
  200. class PostgresNetworkListener : public NotificationListener {
  201. public:
  202. PostgresNetworkListener(DB* db, std::shared_ptr<ConnectionPool<PostgresConnection> > pool, const std::string& channel, uint64_t timeout);
  203. virtual ~PostgresNetworkListener();
  204. virtual void listen();
  205. virtual void onNotification(const std::string& payload) override;
  206. private:
  207. bool _run = false;
  208. DB* _db;
  209. std::shared_ptr<ConnectionPool<PostgresConnection> > _pool;
  210. std::shared_ptr<PostgresConnection> _conn;
  211. uint64_t _notification_timeout;
  212. std::thread _listenerThread;
  213. _notificationReceiver<PostgresNetworkListener>* _receiver;
  214. };
  215. } // namespace ZeroTier
  216. #endif // ZT_CONTROLLER_POSTGRESQL_HPP
  217. #endif // ZT_CONTROLLER_USE_LIBPQ