refactor: consolidate parse_peers into xce_parse_peers in libxce

Three identical static copies (controller, worker, scheduler) replaced
with a single xce_parse_peers() in src/lib/cluster.c.

The libxce version is the canonical implementation: stores the logical
name from 'name=host:port' syntax so daemons on the same dev host can
identify themselves by name rather than IP address.
This commit is contained in:
Rongsong Shen 2026-05-08 07:06:57 +00:00
parent a05c9bd344
commit 7e4528a0b9
5 changed files with 54 additions and 111 deletions

View file

@ -38,44 +38,6 @@ static struct argp_option options[] = {
{ 0 }
};
/* parse_peers — split "host:port,host:port,..." into cfg->candidates[].
* Accepts optional "name=host:port" prefix (name is ignored for controller).
* Returns 0 on success, -1 on empty input or parse error. */
static int parse_peers(const char* peers_str, xce_cluster_config_t* cfg)
{
cfg->candidates_count = 0;
if (!peers_str || peers_str[0] == '\0')
return -1;
char buf[2048];
strncpy(buf, peers_str, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';
char* saveptr;
char* tok = strtok_r(buf, ",", &saveptr);
while (tok && cfg->candidates_count < MAX_HA_CANDIDATES) {
char* eq = strchr(tok, '=');
char* addr = eq ? eq + 1 : tok;
char* colon = strrchr(addr, ':');
if (!colon)
return -1;
*colon = '\0';
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);
}
return cfg->candidates_count > 0 ? 0 : -1;
}
static error_t parse_opt(int key, char* arg, struct argp_state* state)
{
@ -164,7 +126,7 @@ int main(int argc, char* argv[])
memset(&myconf.peers_cfg, 0, sizeof(myconf.peers_cfg));
if (arguments.peers_str) {
if (parse_peers(arguments.peers_str, &myconf.peers_cfg) != 0) {
if (xce_parse_peers(arguments.peers_str, &myconf.peers_cfg) != 0) {
fprintf(stderr,
"xce_controller: --peers parse error: expected "
"host:port,... got: %s\n",