Dynamic trunk add/remove via Redis pub/sub
Reflector::onRedisConfigChanged now handles 'trunk:<section>' events by reading the trunk:<section>: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.
This commit is contained in:
1 parent
9c6f27e12b
commit
1d639dd0b0
2 files changed
+111
-2
No files matched your search
@@ -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<std::string> Reflector::collectAllTrunkPrefixes(void) const
|
||||
{
|
||||
std::vector<std::string> 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<std::string, RedisStore::TrunkPeerConfig>{};
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,6 +319,7 @@ class Reflector : public sigc::trackable
|
||||
Json::Value m_status;
|
||||
|
||||
std::vector<TrunkLink*> m_trunk_links;
|
||||
std::set<std::string> m_redis_trunk_sections; // sections loaded from Redis
|
||||
std::set<uint32_t> 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<std::string> collectAllTrunkPrefixes(void) const;
|
||||
void refreshStatus(void);
|
||||
void initTrunkLinks(void);
|
||||
void initTrunkServer(void);
|
||||
|
||||
Reference in new issue
Block a user