|
|
|
@ -18,8 +18,10 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include "ConnectionPool.hpp" |
|
|
|
#include "ConnectionPool.hpp" |
|
|
|
#include "DB.hpp" |
|
|
|
#include "DB.hpp" |
|
|
|
|
|
|
|
#include "opentelemetry/trace/provider.h" |
|
|
|
|
|
|
|
|
|
|
|
#include <memory> |
|
|
|
#include <memory> |
|
|
|
|
|
|
|
#include <nlohmann/json.hpp> |
|
|
|
#include <pqxx/pqxx> |
|
|
|
#include <pqxx/pqxx> |
|
|
|
|
|
|
|
|
|
|
|
namespace ZeroTier { |
|
|
|
namespace ZeroTier { |
|
|
|
@ -56,32 +58,84 @@ class PostgresConnFactory : public ConnectionFactory { |
|
|
|
std::string m_connString; |
|
|
|
std::string m_connString; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
class MemberNotificationReceiver : public pqxx::notification_receiver { |
|
|
|
template <typename T> class MemberNotificationReceiver : public pqxx::notification_receiver { |
|
|
|
public: |
|
|
|
public: |
|
|
|
MemberNotificationReceiver(DB* p, pqxx::connection& c, const std::string& channel); |
|
|
|
MemberNotificationReceiver(T* p, pqxx::connection& c, const std::string& channel) : pqxx::notification_receiver(c, channel), _psql(p) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
fprintf(stderr, "initialize MemberNotificationReceiver\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
virtual ~MemberNotificationReceiver() |
|
|
|
virtual ~MemberNotificationReceiver() |
|
|
|
{ |
|
|
|
{ |
|
|
|
fprintf(stderr, "MemberNotificationReceiver destroyed\n"); |
|
|
|
fprintf(stderr, "MemberNotificationReceiver destroyed\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
virtual void operator()(const std::string& payload, int backendPid); |
|
|
|
virtual void operator()(const std::string& payload, int backendPid) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
auto provider = opentelemetry::trace::Provider::GetTracerProvider(); |
|
|
|
|
|
|
|
auto tracer = provider->GetTracer("db_member_notification"); |
|
|
|
|
|
|
|
auto span = tracer->StartSpan("db_member_notification::operator()"); |
|
|
|
|
|
|
|
auto scope = tracer->WithActiveSpan(span); |
|
|
|
|
|
|
|
span->SetAttribute("payload", payload); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, "Member Notification received: %s\n", payload.c_str()); |
|
|
|
|
|
|
|
Metrics::pgsql_mem_notification++; |
|
|
|
|
|
|
|
nlohmann::json tmp(nlohmann::json::parse(payload)); |
|
|
|
|
|
|
|
nlohmann::json& ov = tmp["old_val"]; |
|
|
|
|
|
|
|
nlohmann::json& nv = tmp["new_val"]; |
|
|
|
|
|
|
|
nlohmann::json oldConfig, newConfig; |
|
|
|
|
|
|
|
if (ov.is_object()) |
|
|
|
|
|
|
|
oldConfig = ov; |
|
|
|
|
|
|
|
if (nv.is_object()) |
|
|
|
|
|
|
|
newConfig = nv; |
|
|
|
|
|
|
|
if (oldConfig.is_object() || newConfig.is_object()) { |
|
|
|
|
|
|
|
_psql->_memberChanged(oldConfig, newConfig, _psql->isReady()); |
|
|
|
|
|
|
|
fprintf(stderr, "payload sent\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
private: |
|
|
|
DB* _psql; |
|
|
|
T* _psql; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
class NetworkNotificationReceiver : public pqxx::notification_receiver { |
|
|
|
template <typename T> class NetworkNotificationReceiver : public pqxx::notification_receiver { |
|
|
|
public: |
|
|
|
public: |
|
|
|
NetworkNotificationReceiver(DB* p, pqxx::connection& c, const std::string& channel); |
|
|
|
NetworkNotificationReceiver(T* p, pqxx::connection& c, const std::string& channel) : pqxx::notification_receiver(c, channel), _psql(p) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
fprintf(stderr, "initialize NetworkrNotificationReceiver\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
virtual ~NetworkNotificationReceiver() |
|
|
|
virtual ~NetworkNotificationReceiver() |
|
|
|
{ |
|
|
|
{ |
|
|
|
fprintf(stderr, "NetworkNotificationReceiver destroyed\n"); |
|
|
|
fprintf(stderr, "NetworkNotificationReceiver destroyed\n"); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
virtual void operator()(const std::string& payload, int packend_pid); |
|
|
|
virtual void operator()(const std::string& payload, int packend_pid) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
auto provider = opentelemetry::trace::Provider::GetTracerProvider(); |
|
|
|
|
|
|
|
auto tracer = provider->GetTracer("db_network_notification"); |
|
|
|
|
|
|
|
auto span = tracer->StartSpan("db_network_notification::operator()"); |
|
|
|
|
|
|
|
auto scope = tracer->WithActiveSpan(span); |
|
|
|
|
|
|
|
span->SetAttribute("payload", payload); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, "Network Notification received: %s\n", payload.c_str()); |
|
|
|
|
|
|
|
Metrics::pgsql_net_notification++; |
|
|
|
|
|
|
|
nlohmann::json tmp(nlohmann::json::parse(payload)); |
|
|
|
|
|
|
|
nlohmann::json& ov = tmp["old_val"]; |
|
|
|
|
|
|
|
nlohmann::json& nv = tmp["new_val"]; |
|
|
|
|
|
|
|
nlohmann::json oldConfig, newConfig; |
|
|
|
|
|
|
|
if (ov.is_object()) |
|
|
|
|
|
|
|
oldConfig = ov; |
|
|
|
|
|
|
|
if (nv.is_object()) |
|
|
|
|
|
|
|
newConfig = nv; |
|
|
|
|
|
|
|
if (oldConfig.is_object() || newConfig.is_object()) { |
|
|
|
|
|
|
|
_psql->_networkChanged(oldConfig, newConfig, _psql->isReady()); |
|
|
|
|
|
|
|
fprintf(stderr, "payload sent\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
private: |
|
|
|
DB* _psql; |
|
|
|
T* _psql; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
struct NodeOnlineRecord { |
|
|
|
struct NodeOnlineRecord { |
|
|
|
|