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:
parent
a05c9bd344
commit
7e4528a0b9
5 changed files with 54 additions and 111 deletions
|
|
@ -250,41 +250,6 @@ static void master_rusages_callback(xce_job_rusages_t* rusages)
|
|||
free_xce_job_rusages(rusages);
|
||||
}
|
||||
|
||||
/* parse_peers — parse "--peers ctrl-1=10.0.1.1:7000,ctrl-2=10.0.1.2:7001"
|
||||
* into cfg->candidates[]. Returns 0 on success, -1 on parse error.
|
||||
* If peers_str is NULL or empty, returns -1 (caller falls back to single-host
|
||||
* path). */
|
||||
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) {
|
||||
/* Format: name=host:port OR host:port */
|
||||
char* eq = strchr(tok, '=');
|
||||
char* addr = eq ? eq + 1 : tok;
|
||||
char* colon = strrchr(addr, ':');
|
||||
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);
|
||||
cfg->candidates_count++;
|
||||
tok = strtok_r(NULL, ",", &saveptr);
|
||||
}
|
||||
return cfg->candidates_count > 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
static int check_xce_server_dir(char* myfile)
|
||||
{
|
||||
|
|
@ -497,7 +462,7 @@ int main(int argc, char* argv[])
|
|||
|
||||
/* P2.5-5: populate and validate xce_cluster_config_t when --peers given */
|
||||
if (peers_str) {
|
||||
if (parse_peers(peers_str, &cluster_cfg) != 0) {
|
||||
if (xce_parse_peers(peers_str, &cluster_cfg) != 0) {
|
||||
fprintf(stderr,
|
||||
"xcecd: --peers parse error: expected "
|
||||
"\"[name=]host:port,...\" got \"%s\"\n",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue