PostgreSQL.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "opentelemetry/trace/provider.h"
  10. #include <memory>
  11. #include <nlohmann/json.hpp>
  12. #include <pqxx/pqxx>
  13. namespace ZeroTier {
  14. extern "C" {
  15. typedef struct pg_conn PGconn;
  16. }
  17. class PostgresConnection : public Connection {
  18. public:
  19. virtual ~PostgresConnection()
  20. {
  21. }
  22. std::shared_ptr<pqxx::connection> c;
  23. int a;
  24. };
  25. class PostgresConnFactory : public ConnectionFactory {
  26. public:
  27. PostgresConnFactory(std::string& connString) : m_connString(connString)
  28. {
  29. }
  30. virtual std::shared_ptr<Connection> create()
  31. {
  32. Metrics::conn_counter++;
  33. auto c = std::shared_ptr<PostgresConnection>(new PostgresConnection());
  34. c->c = std::make_shared<pqxx::connection>(m_connString);
  35. return std::static_pointer_cast<Connection>(c);
  36. }
  37. private:
  38. std::string m_connString;
  39. };
  40. template <typename T> class MemberNotificationReceiver : public pqxx::notification_receiver {
  41. public:
  42. MemberNotificationReceiver(T* p, pqxx::connection& c, const std::string& channel) : pqxx::notification_receiver(c, channel), _psql(p)
  43. {
  44. fprintf(stderr, "initialize MemberNotificationReceiver\n");
  45. }
  46. virtual ~MemberNotificationReceiver()
  47. {
  48. fprintf(stderr, "MemberNotificationReceiver destroyed\n");
  49. }
  50. virtual void operator()(const std::string& payload, int backendPid)
  51. {
  52. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  53. auto tracer = provider->GetTracer("db_member_notification");
  54. auto span = tracer->StartSpan("db_member_notification::operator()");
  55. auto scope = tracer->WithActiveSpan(span);
  56. span->SetAttribute("payload", payload);
  57. span->SetAttribute("psqlReady", _psql->isReady());
  58. fprintf(stderr, "Member Notification received: %s\n", payload.c_str());
  59. Metrics::pgsql_mem_notification++;
  60. nlohmann::json tmp(nlohmann::json::parse(payload));
  61. nlohmann::json& ov = tmp["old_val"];
  62. nlohmann::json& nv = tmp["new_val"];
  63. nlohmann::json oldConfig, newConfig;
  64. if (ov.is_object())
  65. oldConfig = ov;
  66. if (nv.is_object())
  67. newConfig = nv;
  68. if (oldConfig.is_object() && newConfig.is_object()) {
  69. _psql->save(newConfig, _psql->isReady());
  70. fprintf(stderr, "payload sent\n");
  71. }
  72. else if (newConfig.is_object() && ! oldConfig.is_object()) {
  73. // new member
  74. Metrics::member_count++;
  75. _psql->save(newConfig, _psql->isReady());
  76. fprintf(stderr, "new member payload sent\n");
  77. }
  78. else if (! newConfig.is_object() && oldConfig.is_object()) {
  79. // member delete
  80. uint64_t networkId = OSUtils::jsonIntHex(oldConfig["nwid"], 0ULL);
  81. uint64_t memberId = OSUtils::jsonIntHex(oldConfig["id"], 0ULL);
  82. if (memberId && networkId) {
  83. _psql->eraseMember(networkId, memberId);
  84. fprintf(stderr, "member delete payload sent\n");
  85. }
  86. }
  87. }
  88. private:
  89. T* _psql;
  90. };
  91. template <typename T> class NetworkNotificationReceiver : public pqxx::notification_receiver {
  92. public:
  93. NetworkNotificationReceiver(T* p, pqxx::connection& c, const std::string& channel) : pqxx::notification_receiver(c, channel), _psql(p)
  94. {
  95. fprintf(stderr, "initialize NetworkrNotificationReceiver\n");
  96. }
  97. virtual ~NetworkNotificationReceiver()
  98. {
  99. fprintf(stderr, "NetworkNotificationReceiver destroyed\n");
  100. };
  101. virtual void operator()(const std::string& payload, int packend_pid)
  102. {
  103. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  104. auto tracer = provider->GetTracer("db_network_notification");
  105. auto span = tracer->StartSpan("db_network_notification::operator()");
  106. auto scope = tracer->WithActiveSpan(span);
  107. span->SetAttribute("payload", payload);
  108. span->SetAttribute("psqlReady", _psql->isReady());
  109. fprintf(stderr, "Network Notification received: %s\n", payload.c_str());
  110. Metrics::pgsql_net_notification++;
  111. nlohmann::json tmp(nlohmann::json::parse(payload));
  112. nlohmann::json& ov = tmp["old_val"];
  113. nlohmann::json& nv = tmp["new_val"];
  114. nlohmann::json oldConfig, newConfig;
  115. if (ov.is_object())
  116. oldConfig = ov;
  117. if (nv.is_object())
  118. newConfig = nv;
  119. if (oldConfig.is_object() && newConfig.is_object()) {
  120. std::string nwid = oldConfig["id"];
  121. span->SetAttribute("action", "network_change");
  122. span->SetAttribute("network_id", nwid);
  123. _psql->save(newConfig, _psql->isReady());
  124. fprintf(stderr, "payload sent\n");
  125. }
  126. else if (newConfig.is_object() && ! oldConfig.is_object()) {
  127. std::string nwid = newConfig["id"];
  128. span->SetAttribute("network_id", nwid);
  129. span->SetAttribute("action", "new_network");
  130. // new network
  131. _psql->save(newConfig, _psql->isReady());
  132. fprintf(stderr, "new network payload sent\n");
  133. }
  134. else if (! newConfig.is_object() && oldConfig.is_object()) {
  135. // network delete
  136. span->SetAttribute("action", "delete_network");
  137. std::string nwid = oldConfig["id"];
  138. span->SetAttribute("network_id", nwid);
  139. uint64_t networkId = Utils::hexStrToU64(nwid.c_str());
  140. span->SetAttribute("network_id_int", networkId);
  141. if (networkId) {
  142. _psql->eraseNetwork(networkId);
  143. fprintf(stderr, "network delete payload sent\n");
  144. }
  145. }
  146. }
  147. private:
  148. T* _psql;
  149. };
  150. struct NodeOnlineRecord {
  151. uint64_t lastSeen;
  152. InetAddress physicalAddress;
  153. std::string osArch;
  154. };
  155. } // namespace ZeroTier
  156. #endif // ZT_CONTROLLER_POSTGRESQL_HPP
  157. #endif // ZT_CONTROLLER_USE_LIBPQ