network/member deletes work now
Some checks failed
/ build_macos (push) Has been cancelled
/ build_windows (push) Has been cancelled
/ build_ubuntu (push) Has been cancelled

This commit is contained in:
Grant Limberg 2025-07-16 16:35:28 -07:00
parent 14c0ccc94c
commit 307c4ed4b6
No known key found for this signature in database
GPG key ID: 8F2F97D3BE8D7735
2 changed files with 72 additions and 63 deletions

View file

@ -112,60 +112,12 @@ bool CV2::isReady()
void CV2::_memberChanged(nlohmann::json& old, nlohmann::json& memberConfig, bool notifyListeners)
{
// auto provider = opentelemetry::trace::Provider::GetTracerProvider();
// auto tracer = provider->GetTracer("cv2");
// auto span = tracer->StartSpan("cv2::_memberChanged");
// auto scope = tracer->WithActiveSpan(span);
// if (memberConfig.is_object()) {
// // member config change
// const std::string ids = memberConfig["id"];
// const uint64_t networkId = OSUtils::jsonIntHex(memberConfig["nwid"], 0ULL);
// const uint64_t memberId = Utils::hexStrToU64(ids.c_str());
// if ((networkId) && (memberId)) {
// save(memberConfig, notifyListeners);
// }
// }
// else if (old.is_object()) {
// // member delete
// const std::string ids = old["id"];
// const uint64_t networkId = OSUtils::jsonIntHex(old["nwid"], 0ULL);
// const uint64_t memberId = Utils::hexStrToU64(ids.c_str());
// if ((networkId) && (memberId)) {
// eraseMember(networkId, memberId);
// }
// }
// fprintf(stderr, "CV2::_memberChanged\n");
DB::_memberChanged(old, memberConfig, notifyListeners);
}
void CV2::_networkChanged(nlohmann::json& old, nlohmann::json& networkConfig, bool notifyListeners)
{
// auto provider = opentelemetry::trace::Provider::GetTracerProvider();
// auto tracer = provider->GetTracer("cv2");
// auto span = tracer->StartSpan("cv2::_networkChanged");
// auto scope = tracer->WithActiveSpan(span);
// if (networkConfig.is_object()) {
// // network config change
// const std::string ids = networkConfig["id"];
// const uint64_t networkId = Utils::hexStrToU64(ids.c_str());
// if (networkId) {
// save(networkConfig, notifyListeners);
// }
// }
// else if (old.is_object()) {
// // network delete
// const std::string ids = networkConfig["id"];
// const uint64_t networkId = Utils::hexStrToU64(ids.c_str());
// if (networkId) {
// eraseNetwork(networkId);
// }
// }
// fprintf(stderr, "CV2::_networkChanged\n");
DB::_networkChanged(old, networkConfig, false);
DB::_networkChanged(old, networkConfig, notifyListeners);
}
bool CV2::save(nlohmann::json& record, bool notifyListeners)
@ -241,19 +193,18 @@ void CV2::eraseNetwork(const uint64_t networkId)
auto span = tracer->StartSpan("cv2::eraseNetwork");
auto scope = tracer->WithActiveSpan(span);
char networkIdStr[17];
span->SetAttribute("network_id", Utils::hex(networkId, networkIdStr));
std::string nwid = Utils::hex(networkId, networkIdStr);
span->SetAttribute("network_id", nwid);
fprintf(stderr, "PostgreSQL::eraseNetwork\n");
char tmp2[24];
fprintf(stderr, "CV2::eraseNetwork\n");
waitForReady();
Utils::hex(networkId, tmp2);
std::pair<nlohmann::json, bool> tmp;
tmp.first["id"] = tmp2;
tmp.first["id"] = nwid;
tmp.first["objtype"] = "_delete_network";
tmp.second = true;
_commitQueue.post(tmp);
nlohmann::json nullJson;
_networkChanged(tmp.first, nullJson, true);
// nlohmann::json nullJson;
//_networkChanged(tmp.first, nullJson, isReady());
}
void CV2::eraseMember(const uint64_t networkId, const uint64_t memberId)
@ -278,8 +229,8 @@ void CV2::eraseMember(const uint64_t networkId, const uint64_t memberId)
tmp.first["objtype"] = "_delete_member";
tmp.second = true;
_commitQueue.post(tmp);
nlohmann::json nullJson;
_memberChanged(tmp.first, nullJson, true);
// nlohmann::json nullJson;
//_memberChanged(tmp.first, nullJson, isReady());
}
void CV2::nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress, const char* osArch)
@ -1106,11 +1057,17 @@ void CV2::commitThread()
try {
pqxx::work w(*c->c);
std::string networkId = config["id"];
fprintf(stderr, "Deleting network %s\n", networkId.c_str());
w.exec_params0("DELETE FROM network_memberships_ctl WHERE network_id = $1", networkId);
w.exec_params0("DELETE FROM networks_ctl WHERE id = $1", networkId);
w.commit();
uint64_t nwidInt = OSUtils::jsonIntHex(config["nwid"], 0ULL);
json oldConfig;
get(nwidInt, oldConfig);
json empty;
_networkChanged(oldConfig, empty, qitem.second);
}
catch (std::exception& e) {
fprintf(stderr, "%s ERROR: Error deleting network: %s\n", _myAddressStr.c_str(), e.what());
@ -1133,6 +1090,16 @@ void CV2::commitThread()
pqxx::result res = w.exec_params0("DELETE FROM network_memberships_ctl WHERE device_id = $1 AND network_id = $2", memberId, networkId);
w.commit();
uint64_t nwidInt = OSUtils::jsonIntHex(config["nwid"], 0ULL);
uint64_t memberidInt = OSUtils::jsonIntHex(config["id"], 0ULL);
nlohmann::json networkConfig;
nlohmann::json oldConfig;
get(nwidInt, networkConfig, memberidInt, oldConfig);
json empty;
_memberChanged(oldConfig, empty, qitem.second);
}
catch (std::exception& e) {
fprintf(stderr, "%s ERROR: Error deleting member: %s\n", _myAddressStr.c_str(), e.what());

View file

@ -89,10 +89,26 @@ template <typename T> class MemberNotificationReceiver : public pqxx::notificati
oldConfig = ov;
if (nv.is_object())
newConfig = nv;
if (oldConfig.is_object() || newConfig.is_object()) {
_psql->_memberChanged(oldConfig, newConfig, _psql->isReady());
if (oldConfig.is_object() && newConfig.is_object()) {
_psql->save(newConfig, _psql->isReady());
fprintf(stderr, "payload sent\n");
}
else if (newConfig.is_object() && ! oldConfig.is_object()) {
// new member
Metrics::member_count++;
_psql->save(newConfig, _psql->isReady());
fprintf(stderr, "new member payload sent\n");
}
else if (! newConfig.is_object() && oldConfig.is_object()) {
// member delete
uint64_t networkId = OSUtils::jsonIntHex(oldConfig["nwid"], 0ULL);
uint64_t memberId = OSUtils::jsonIntHex(oldConfig["id"], 0ULL);
if (memberId && networkId) {
_psql->eraseMember(networkId, memberId);
fprintf(stderr, "member delete payload sent\n");
}
}
}
private:
@ -123,17 +139,43 @@ template <typename T> class NetworkNotificationReceiver : public pqxx::notificat
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());
if (oldConfig.is_object() && newConfig.is_object()) {
std::string nwid = oldConfig["id"];
span->SetAttribute("action", "network_change");
span->SetAttribute("network_id", nwid);
_psql->save(newConfig, _psql->isReady());
fprintf(stderr, "payload sent\n");
}
else if (newConfig.is_object() && ! oldConfig.is_object()) {
std::string nwid = newConfig["id"];
span->SetAttribute("network_id", nwid);
span->SetAttribute("action", "new_network");
// new network
_psql->save(newConfig, _psql->isReady());
fprintf(stderr, "new network payload sent\n");
}
else if (! newConfig.is_object() && oldConfig.is_object()) {
// network delete
span->SetAttribute("action", "delete_network");
std::string nwid = oldConfig["id"];
span->SetAttribute("network_id", nwid);
uint64_t networkId = Utils::hexStrToU64(nwid.c_str());
span->SetAttribute("network_id_int", networkId);
if (networkId) {
_psql->eraseNetwork(networkId);
fprintf(stderr, "network delete payload sent\n");
}
}
}
private: