Load trunk peer definitions from Redis at startup

When [REDIS] is configured, Reflector::initialize synthesizes [TRUNK_*]
sections from 'trunk:<section>:peer' hashes before the trunk discovery
loop. .conf [TRUNK_*] sections are ignored (with a warning) when Redis
is configured. The importer --import-conf-to-redis now also writes peer
definitions into 'trunk:<section>:peer' so existing configs can migrate.
This commit is contained in:
Audric committed 2026-04-15 14:41:18 +02:00
1 parent 5cab611ccd
commit 9c6f27e12b
4 files changed
+129

No files matched your search

+59
View File
@@ -194,6 +194,65 @@ std::map<uint32_t, uint32_t> RedisStore::loadTrunkTgMap(const std::string& secti
return out; return out;
} }
std::map<std::string, RedisStore::TrunkPeerConfig>
RedisStore::loadTrunkPeers(void)
{
std::map<std::string, TrunkPeerConfig> out;
if (!m_sync) return out;
std::string pattern = keyFor("trunk:*:peer");
redisReply* r = (redisReply*)redisCommand(m_sync, "KEYS %s", pattern.c_str());
if (!r || r->type != REDIS_REPLY_ARRAY) {
if (r) freeReplyObject(r);
return out;
}
std::vector<std::string> matched_keys;
for (size_t i = 0; i < r->elements; ++i) {
if (r->element[i]->type == REDIS_REPLY_STRING) {
matched_keys.emplace_back(r->element[i]->str, r->element[i]->len);
}
}
freeReplyObject(r);
// Extract section name: strip "<keyprefix>:trunk:" prefix and ":peer" suffix.
std::string left = keyFor("trunk:");
const std::string right = ":peer";
for (const std::string& key : matched_keys) {
if (key.size() < left.size() + right.size()) continue;
if (key.substr(0, left.size()) != left) continue;
if (key.substr(key.size() - right.size()) != right) continue;
std::string section = key.substr(left.size(),
key.size() - left.size() - right.size());
if (section.empty()) continue;
redisReply* h = (redisReply*)redisCommand(m_sync, "HGETALL %s", key.c_str());
if (!h || h->type != REDIS_REPLY_ARRAY) {
if (h) freeReplyObject(h);
continue;
}
TrunkPeerConfig cfg;
for (size_t i = 0; i + 1 < h->elements; i += 2) {
std::string field(h->element[i]->str, h->element[i]->len);
std::string value(h->element[i+1]->str, h->element[i+1]->len);
if (field == "host") cfg.host = value;
else if (field == "port") cfg.port = value;
else if (field == "secret") cfg.secret = value;
else if (field == "remote_prefix") cfg.remote_prefix = value;
else if (field == "peer_id") cfg.peer_id = value;
}
freeReplyObject(h);
// Require host + secret + remote_prefix
if (cfg.host.empty() || cfg.secret.empty() || cfg.remote_prefix.empty()) {
std::cerr << "*** WARN: RedisStore: trunk peer " << section
<< " missing required fields (host/secret/remote_prefix); "
<< "skipping" << std::endl;
continue;
}
out[section] = std::move(cfg);
}
return out;
}
void RedisStore::publishConfigChanged(const std::string& scope) { void RedisStore::publishConfigChanged(const std::string& scope) {
if (!m_sync) return; if (!m_sync) return;
std::string ch = channelName(); std::string ch = channelName();
+10
View File
@@ -32,6 +32,15 @@ class RedisStore : public sigc::trackable
std::string tls_client_key; std::string tls_client_key;
}; };
struct TrunkPeerConfig
{
std::string host;
std::string port;
std::string secret;
std::string remote_prefix;
std::string peer_id;
};
explicit RedisStore(const Config& cfg); explicit RedisStore(const Config& cfg);
~RedisStore(); ~RedisStore();
@@ -56,6 +65,7 @@ class RedisStore : public sigc::trackable
std::string loadTrunkFilter(const std::string& section, std::string loadTrunkFilter(const std::string& section,
const std::string& field); // "blacklist"|"allow" const std::string& field); // "blacklist"|"allow"
std::map<uint32_t, uint32_t> loadTrunkTgMap(const std::string& section); std::map<uint32_t, uint32_t> loadTrunkTgMap(const std::string& section);
std::map<std::string, TrunkPeerConfig> loadTrunkPeers(void);
// Publish a config.changed scope token (sync). // Publish a config.changed scope token (sync).
void publishConfigChanged(const std::string& scope); void publishConfigChanged(const std::string& scope);
+31
View File
@@ -2168,6 +2168,37 @@ void Reflector::initTrunkLinks(void)
} }
} }
// If Redis is configured, load trunk peer definitions from Redis and
// synthesize them into m_cfg so the existing trunk initialization
// loop picks them up. Warn if the .conf ALSO has [TRUNK_*] sections
// (they are ignored per the Redis-overrides rule).
if (m_redis != nullptr) {
std::list<std::string> conf_sections = m_cfg->listSections();
bool conf_has_trunk = false;
for (const std::string& s : conf_sections) {
if (s.size() >= 6 && s.substr(0, 6) == "TRUNK_") { conf_has_trunk = true; break; }
}
if (conf_has_trunk) {
std::cerr << "WARN: [TRUNK_*] sections in svxreflector.conf are "
"ignored because [REDIS] is configured. Run "
"--import-conf-to-redis to migrate." << std::endl;
}
auto peers = m_redis->loadTrunkPeers();
for (const auto& kv : peers) {
const std::string& section = kv.first;
const auto& p = kv.second;
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);
}
std::cout << "Redis: loaded " << peers.size() << " trunk peer(s)"
<< std::endl;
}
// First pass: collect all remote prefixes so we can do longest-prefix-match // First pass: collect all remote prefixes so we can do longest-prefix-match
std::vector<std::string> trunk_sections; std::vector<std::string> trunk_sections;
for (const auto& section : m_cfg->listSections()) for (const auto& section : m_cfg->listSections())
+29
View File
@@ -262,6 +262,35 @@ static bool runImportConfToRedis(Async::Config& cfg, bool dry_run)
++trunks_n; ++trunks_n;
std::string trunk_label = sec.substr(6); // "TRUNK_AB" → "AB" std::string trunk_label = sec.substr(6); // "TRUNK_AB" → "AB"
// Import the peer definition hash.
std::string host, port_s_peer, secret, remote_prefix, peer_id;
cfg.getValue(sec, "HOST", host);
cfg.getValue(sec, "PORT", port_s_peer);
cfg.getValue(sec, "SECRET", secret);
cfg.getValue(sec, "REMOTE_PREFIX", remote_prefix);
cfg.getValue(sec, "PEER_ID", peer_id);
if (!host.empty() && !secret.empty() && !remote_prefix.empty())
{
std::string pk = kf("trunk:" + trunk_label + ":peer");
auto setField = [&](const char* field, const std::string& value) {
if (value.empty()) return;
if (dry_run) {
std::cout << "[dry-run] HSET " << pk << " " << field
<< " " << (std::string(field) == "secret" ? "<REDACTED>" : value)
<< std::endl;
} else {
redisReply* r = (redisReply*)redisCommand(ctx, "HSET %s %s %s",
pk.c_str(), field, value.c_str());
if (r) freeReplyObject(r);
}
};
setField("host", host);
setField("port", port_s_peer);
setField("secret", secret);
setField("remote_prefix", remote_prefix);
setField("peer_id", peer_id);
}
auto importSet = [&](const std::string& field, const std::string& subkey) { auto importSet = [&](const std::string& field, const std::string& subkey) {
std::string s; std::string s;
if (!cfg.getValue(sec, field, s) || s.empty()) return; if (!cfg.getValue(sec, field, s) || s.empty()) return;