From 1d639dd0b056d8bb5ae3425bfd8dcd8014e43928 Mon Sep 17 00:00:00 2001 From: Audric Date: Wed, 15 Apr 2026 15:09:17 +0200 Subject: [PATCH] Dynamic trunk add/remove via Redis pub/sub Reflector::onRedisConfigChanged now handles 'trunk:
' events by reading the trunk:
:peer hash from Redis: - peer present + no local link -> create new TrunkLink - peer absent + local link -> destroy local TrunkLink (Redis-managed only) - both present -> reload filters (existing behavior) - neither -> log and ignore Tracks which sections were loaded from Redis (m_redis_trunk_sections) so conf-based test trunks (e.g. TRUNK_TEST) correctly fall back to reloadConfig() rather than being destroyed on a filter-change event. Dashboards can now add/remove trunk peers without restarting the reflector. --- src/svxlink/reflector/Reflector.cpp | 109 +++++++++++++++++++++++++++- src/svxlink/reflector/Reflector.h | 4 + 2 files changed, 111 insertions(+), 2 deletions(-) diff --git a/src/svxlink/reflector/Reflector.cpp b/src/svxlink/reflector/Reflector.cpp index e6c6271..3d0ccf5 100644 --- a/src/svxlink/reflector/Reflector.cpp +++ b/src/svxlink/reflector/Reflector.cpp @@ -2194,6 +2194,7 @@ void Reflector::initTrunkLinks(void) m_cfg->setValue(section, "REMOTE_PREFIX", p.remote_prefix); if (!p.peer_id.empty()) m_cfg->setValue(section, "PEER_ID", p.peer_id); + m_redis_trunk_sections.insert(section); } std::cout << "Redis: loaded " << peers.size() << " trunk peer(s)" << std::endl; @@ -2803,6 +2804,84 @@ void Reflector::reloadClusterTgs(void) } +std::vector Reflector::collectAllTrunkPrefixes(void) const +{ + std::vector all; + for (const auto* link : m_trunk_links) { + for (const std::string& p : link->remotePrefix()) all.push_back(p); + } + return all; +} + + +bool Reflector::addTrunkLink(const std::string& section) +{ + if (m_redis == nullptr) { + std::cerr << "*** addTrunkLink called without Redis — refusing" + << std::endl; + return false; + } + // Already present? + for (const auto* link : m_trunk_links) { + if (link->section() == section) return true; + } + + auto peers = m_redis->loadTrunkPeers(); + auto it = peers.find(section); + if (it == peers.end()) { + std::cerr << "*** addTrunkLink(" << section + << "): peer hash not found in Redis" << std::endl; + return false; + } + const auto& p = it->second; + + // Synthesize the config section (same shape as startup). + m_cfg->setValue(section, "HOST", p.host); + if (!p.port.empty()) m_cfg->setValue(section, "PORT", p.port); + m_cfg->setValue(section, "SECRET", p.secret); + m_cfg->setValue(section, "REMOTE_PREFIX", p.remote_prefix); + if (!p.peer_id.empty()) m_cfg->setValue(section, "PEER_ID", p.peer_id); + + auto* link = new TrunkLink(this, *m_cfg, section); + if (!link->initialize()) { + std::cerr << "*** addTrunkLink(" << section + << "): TrunkLink::initialize() failed" << std::endl; + delete link; + return false; + } + m_trunk_links.push_back(link); + m_redis_trunk_sections.insert(section); + + // Rebroadcast the full prefix set to every link (including the new one). + auto all_prefixes = collectAllTrunkPrefixes(); + for (auto* l : m_trunk_links) l->setAllPrefixes(all_prefixes); + + std::cout << "Added trunk link: " << section << " (" << p.host + << ":" << (p.port.empty() ? "5302" : p.port) << ")" << std::endl; + return true; +} + + +bool Reflector::removeTrunkLink(const std::string& section) +{ + auto it = std::find_if(m_trunk_links.begin(), m_trunk_links.end(), + [&](TrunkLink* l) { return l->section() == section; }); + if (it == m_trunk_links.end()) return false; + + TrunkLink* dead = *it; + m_trunk_links.erase(it); + m_redis_trunk_sections.erase(section); + delete dead; // destructor clears trunk talker state via TGHandler + + // Rebroadcast the remaining prefix set so peers drop the removed one. + auto all_prefixes = collectAllTrunkPrefixes(); + for (auto* l : m_trunk_links) l->setAllPrefixes(all_prefixes); + + std::cout << "Removed trunk link: " << section << std::endl; + return true; +} + + void Reflector::onRedisConfigChanged(std::string scope) { std::cout << "Redis config.changed: " << scope << std::endl; @@ -2816,8 +2895,34 @@ void Reflector::onRedisConfigChanged(std::string scope) } if (scope.rfind("trunk:", 0) == 0 || scope == "all") { std::string section = (scope == "all") ? "" : scope.substr(6); - for (auto* link : m_trunk_links) { - if (section.empty() || link->section() == section) link->reloadConfig(); + if (section.empty()) { + // Full sweep — reload filters on every existing link; don't try + // to add/remove here (startup already did that). + for (auto* link : m_trunk_links) link->reloadConfig(); + } else { + // Per-section: check Redis for peer presence vs local state. + auto peers = m_redis ? m_redis->loadTrunkPeers() + : std::map{}; + bool peer_in_redis = peers.find(section) != peers.end(); + TrunkLink* existing = nullptr; + for (auto* l : m_trunk_links) { + if (l->section() == section) { existing = l; break; } + } + bool redis_managed = m_redis_trunk_sections.count(section) > 0; + if (peer_in_redis && existing == nullptr) { + addTrunkLink(section); + } else if (!peer_in_redis && existing != nullptr && redis_managed) { + // Only remove links that were originally created from Redis; conf-based + // links (e.g. TRUNK_TEST in tests) use reloadConfig() instead. + removeTrunkLink(section); + } else if (existing != nullptr) { + existing->reloadConfig(); + } else { + // neither — log and ignore + std::cout << "Redis: trunk:" << section + << " event but peer not in Redis and no local link" + << std::endl; + } } } } diff --git a/src/svxlink/reflector/Reflector.h b/src/svxlink/reflector/Reflector.h index 2a67ab0..9d40ea2 100644 --- a/src/svxlink/reflector/Reflector.h +++ b/src/svxlink/reflector/Reflector.h @@ -319,6 +319,7 @@ class Reflector : public sigc::trackable Json::Value m_status; std::vector m_trunk_links; + std::set m_redis_trunk_sections; // sections loaded from Redis std::set m_cluster_tgs; bool m_trunk_debug = false; Async::Timer m_nodelist_timer; @@ -368,6 +369,9 @@ class Reflector : public sigc::trackable std::string new_cs); void onRedisConfigChanged(std::string scope); void reloadClusterTgs(void); + bool addTrunkLink(const std::string& section); + bool removeTrunkLink(const std::string& section); + std::vector collectAllTrunkPrefixes(void) const; void refreshStatus(void); void initTrunkLinks(void); void initTrunkServer(void);