feat: support name=host:port format in controller --peers

- xce_candidate_t gains name[] field for logical peer identity
- parse_peers stores name prefix before '=' (empty when omitted)
- cluster_init matches own identity by name first, hostname as fallback
  enabling multiple controllers on the same dev host via
  --peers ctrl-1=10.0.1.1:7000,ctrl-2=10.0.1.2:7001
This commit is contained in:
Rongsong Shen 2026-05-08 06:51:54 +00:00
parent 59597e105e
commit a05c9bd344
3 changed files with 23 additions and 8 deletions

View file

@ -650,9 +650,13 @@ int cluster_init(cluster_t* c)
if (pcfg->candidates_count > 0) {
my_priority = pcfg->candidates_count - 1; /* default: last = lowest */
for (int i = 0; i < pcfg->candidates_count; i++) {
if (strncmp(pcfg->candidates[i].hostname,
c->myconf->name,
XCE_MAXHOSTNAME_LEN - 1) == 0) {
/* Match by logical name first (supports multiple controllers
* on the same dev host via name=host:port syntax). Fall back
* to hostname match when no name was specified. */
const char* key = pcfg->candidates[i].name[0]
? pcfg->candidates[i].name
: pcfg->candidates[i].hostname;
if (strncmp(key, c->myconf->name, XCE_MAXNAME_LEN - 1) == 0) {
my_priority = i;
break;
}

View file

@ -60,10 +60,17 @@ static int parse_peers(const char* peers_str, xce_cluster_config_t* cfg)
if (!colon)
return -1;
*colon = '\0';
strncpy(cfg->candidates[cfg->candidates_count].hostname, addr,
XCE_MAXHOSTNAME_LEN - 1);
cfg->candidates[cfg->candidates_count].hostname[XCE_MAXHOSTNAME_LEN - 1] = '\0';
cfg->candidates[cfg->candidates_count].port = atoi(colon + 1);
xce_candidate_t* cand = &cfg->candidates[cfg->candidates_count];
if (eq) {
*eq = '\0';
strncpy(cand->name, tok, XCE_MAXNAME_LEN - 1);
cand->name[XCE_MAXNAME_LEN - 1] = '\0';
} else {
cand->name[0] = '\0';
}
strncpy(cand->hostname, addr, XCE_MAXHOSTNAME_LEN - 1);
cand->hostname[XCE_MAXHOSTNAME_LEN - 1] = '\0';
cand->port = atoi(colon + 1);
cfg->candidates_count++;
tok = strtok_r(NULL, ",", &saveptr);
}