PostgreSQL.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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)
  53. : pqxx::notification_receiver(c, channel)
  54. , _psql(p)
  55. {
  56. fprintf(stderr, "initialize MemberNotificationReceiver\n");
  57. }
  58. virtual ~MemberNotificationReceiver()
  59. {
  60. fprintf(stderr, "MemberNotificationReceiver destroyed\n");
  61. }
  62. virtual void operator()(const std::string& payload, int backendPid)
  63. {
  64. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  65. auto tracer = provider->GetTracer("db_member_notification");
  66. auto span = tracer->StartSpan("db_member_notification::operator()");
  67. auto scope = tracer->WithActiveSpan(span);
  68. span->SetAttribute("payload", payload);
  69. span->SetAttribute("psqlReady", _psql->isReady());
  70. fprintf(stderr, "Member Notification received: %s\n", payload.c_str());
  71. Metrics::pgsql_mem_notification++;
  72. nlohmann::json tmp(nlohmann::json::parse(payload));
  73. nlohmann::json& ov = tmp["old_val"];
  74. nlohmann::json& nv = tmp["new_val"];
  75. nlohmann::json oldConfig, newConfig;
  76. if (ov.is_object())
  77. oldConfig = ov;
  78. if (nv.is_object())
  79. newConfig = nv;
  80. if (oldConfig.is_object() && newConfig.is_object()) {
  81. _psql->save(newConfig, _psql->isReady());
  82. fprintf(stderr, "payload sent\n");
  83. }
  84. else if (newConfig.is_object() && ! oldConfig.is_object()) {
  85. // new member
  86. Metrics::member_count++;
  87. _psql->save(newConfig, _psql->isReady());
  88. fprintf(stderr, "new member payload sent\n");
  89. }
  90. else if (! newConfig.is_object() && oldConfig.is_object()) {
  91. // member delete
  92. uint64_t networkId = OSUtils::jsonIntHex(oldConfig["nwid"], 0ULL);
  93. uint64_t memberId = OSUtils::jsonIntHex(oldConfig["id"], 0ULL);
  94. if (memberId && networkId) {
  95. _psql->eraseMember(networkId, memberId);
  96. fprintf(stderr, "member delete payload sent\n");
  97. }
  98. }
  99. }
  100. private:
  101. T* _psql;
  102. };
  103. template <typename T> class NetworkNotificationReceiver : public pqxx::notification_receiver {
  104. public:
  105. NetworkNotificationReceiver(T* p, pqxx::connection& c, const std::string& channel)
  106. : pqxx::notification_receiver(c, channel)
  107. , _psql(p)
  108. {
  109. fprintf(stderr, "initialize NetworkrNotificationReceiver\n");
  110. }
  111. virtual ~NetworkNotificationReceiver()
  112. {
  113. fprintf(stderr, "NetworkNotificationReceiver destroyed\n");
  114. };
  115. virtual void operator()(const std::string& payload, int packend_pid)
  116. {
  117. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  118. auto tracer = provider->GetTracer("db_network_notification");
  119. auto span = tracer->StartSpan("db_network_notification::operator()");
  120. auto scope = tracer->WithActiveSpan(span);
  121. span->SetAttribute("payload", payload);
  122. span->SetAttribute("psqlReady", _psql->isReady());
  123. fprintf(stderr, "Network Notification received: %s\n", payload.c_str());
  124. Metrics::pgsql_net_notification++;
  125. nlohmann::json tmp(nlohmann::json::parse(payload));
  126. nlohmann::json& ov = tmp["old_val"];
  127. nlohmann::json& nv = tmp["new_val"];
  128. nlohmann::json oldConfig, newConfig;
  129. if (ov.is_object())
  130. oldConfig = ov;
  131. if (nv.is_object())
  132. newConfig = nv;
  133. if (oldConfig.is_object() && newConfig.is_object()) {
  134. std::string nwid = oldConfig["id"];
  135. span->SetAttribute("action", "network_change");
  136. span->SetAttribute("network_id", nwid);
  137. _psql->save(newConfig, _psql->isReady());
  138. fprintf(stderr, "payload sent\n");
  139. }
  140. else if (newConfig.is_object() && ! oldConfig.is_object()) {
  141. std::string nwid = newConfig["id"];
  142. span->SetAttribute("network_id", nwid);
  143. span->SetAttribute("action", "new_network");
  144. // new network
  145. _psql->save(newConfig, _psql->isReady());
  146. fprintf(stderr, "new network payload sent\n");
  147. }
  148. else if (! newConfig.is_object() && oldConfig.is_object()) {
  149. // network delete
  150. span->SetAttribute("action", "delete_network");
  151. std::string nwid = oldConfig["id"];
  152. span->SetAttribute("network_id", nwid);
  153. uint64_t networkId = Utils::hexStrToU64(nwid.c_str());
  154. span->SetAttribute("network_id_int", networkId);
  155. if (networkId) {
  156. _psql->eraseNetwork(networkId);
  157. fprintf(stderr, "network delete payload sent\n");
  158. }
  159. }
  160. }
  161. private:
  162. T* _psql;
  163. };
  164. struct NodeOnlineRecord {
  165. uint64_t lastSeen;
  166. InetAddress physicalAddress;
  167. std::string osArch;
  168. std::string version;
  169. };
  170. /**
  171. * internal class for listening to PostgreSQL notification channels.
  172. */
  173. template <typename T> class _notificationReceiver : public pqxx::notification_receiver {
  174. public:
  175. _notificationReceiver(T* p, pqxx::connection& c, const std::string& channel)
  176. : pqxx::notification_receiver(c, channel)
  177. , _listener(p)
  178. {
  179. fprintf(stderr, "initialize PostgresMemberNotificationListener::_notificationReceiver\n");
  180. }
  181. virtual void operator()(const std::string& payload, int backendPid)
  182. {
  183. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  184. auto tracer = provider->GetTracer("notification_receiver");
  185. auto span = tracer->StartSpan("notification_receiver::operator()");
  186. auto scope = tracer->WithActiveSpan(span);
  187. _listener->onNotification(payload);
  188. }
  189. private:
  190. T* _listener;
  191. };
  192. class PostgresMemberListener : public NotificationListener {
  193. public:
  194. PostgresMemberListener(
  195. DB* db,
  196. std::shared_ptr<ConnectionPool<PostgresConnection> > pool,
  197. const std::string& channel,
  198. uint64_t timeout);
  199. virtual ~PostgresMemberListener();
  200. virtual void listen();
  201. virtual void onNotification(const std::string& payload) override;
  202. private:
  203. bool _run = false;
  204. DB* _db;
  205. std::shared_ptr<ConnectionPool<PostgresConnection> > _pool;
  206. std::shared_ptr<PostgresConnection> _conn;
  207. uint64_t _notification_timeout;
  208. std::thread _listenerThread;
  209. _notificationReceiver<PostgresMemberListener>* _receiver;
  210. };
  211. class PostgresNetworkListener : public NotificationListener {
  212. public:
  213. PostgresNetworkListener(
  214. DB* db,
  215. std::shared_ptr<ConnectionPool<PostgresConnection> > pool,
  216. const std::string& channel,
  217. uint64_t timeout);
  218. virtual ~PostgresNetworkListener();
  219. virtual void listen();
  220. virtual void onNotification(const std::string& payload) override;
  221. private:
  222. bool _run = false;
  223. DB* _db;
  224. std::shared_ptr<ConnectionPool<PostgresConnection> > _pool;
  225. std::shared_ptr<PostgresConnection> _conn;
  226. uint64_t _notification_timeout;
  227. std::thread _listenerThread;
  228. _notificationReceiver<PostgresNetworkListener>* _receiver;
  229. };
  230. } // namespace ZeroTier
  231. #endif // ZT_CONTROLLER_POSTGRESQL_HPP
  232. #endif // ZT_CONTROLLER_USE_LIBPQ