PostgreSQL.hpp 7.3 KB

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