Chain of fixes needed to get --force-snapshot soak to a real live PASS (raft-ha-core-safety task 5.10a / ha-entity-snapshots task 7.1): 1. WAL segment rotation was gated on a hardcoded 64MB compile-time constant; trim_wal_segments() never deletes the active (currently-open) segment. At soak job counts a segment never rotated, so nothing was ever physically trimmable and ha_evaluate_repl_action always chose STREAM_WAL over SEND_SNAPSHOT for an empty-rejoining node. Fixed by threading a wal_segment_bytes override through persist_config_t (mirrors the --max-wal-records pattern: config.h default, persist.h setter, persist.c 3 usage sites + minimum-floor enforcement, 6-point CLI wiring through main.c/myconf.h/myapp.c/cluster.c) plus a new --wal-segment-bytes CLI flag. run-ha-soak.sh's --force-snapshot mode now sets WAL_SEGMENT_BYTES=4096 to force real rotation at soak scale. 2. This unmasked a genuine heap-use-after-free in INVOKE_MSG_HANDLER (include/netio.h): the macro calls ep->msg_handler(ep) synchronously (not via the vtask dispatcher, so the bug-1298 vtask ref/unref pairing doesn't cover it) then writes ep->fsm_in.pkt = NULL. A stale vote-reply handler chain (raft_on_vote_reply -> raft_cb_become_follower -> endpoint_unref) could drop the endpoint's last ref and free it inside the handler call, so the trailing write hit freed memory. This is the first-ever live exercise of a node receiving a snapshot transfer while mid-election. Fixed by holding a ref across the synchronous call, same pattern task 5.8 established for the async path. With both fixes: --force-snapshot soak now PASSES live (committed divergence=0, job_loss=0, confirmed real "HA: snapshot" install on the restarted node) — first genuine live proof of this scenario. 3-round --chaotic regression clean. Full 217-test controller suite green before/after every fix in the chain. Builds on the delaymgr starvation fix (b9ad018) and walcompare checkpoint-floor fix (f3c86a6) from earlier in this investigation.
312 lines
11 KiB
C
312 lines
11 KiB
C
#include <argp.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <tlog.h>
|
|
|
|
#include "myconf.h"
|
|
#include "utils.h"
|
|
#include "xce/cluster.h"
|
|
|
|
extern int myapp_run(myconf_t*);
|
|
|
|
/* forward declarations — implemented in startup_validation.c */
|
|
int validate_vcpus(int vcpus);
|
|
int validate_peer_count(int n_peers);
|
|
int validate_cert_files(const char *cert_path, const char *key_path,
|
|
char *err_buf, size_t err_len);
|
|
|
|
static char doc[] = "xce_controller";
|
|
static char args_doc[] = "";
|
|
const char* argp_program_version
|
|
= MK_VERSION("xce v0.0.1-", STR(VERSION_COMMIT_ID));
|
|
const char* argp_program_bug_address = BUG_EMAIL;
|
|
|
|
/* Long-option key (above ASCII to avoid collisions with short opts) */
|
|
#define OPT_PEERS 256
|
|
#define OPT_CONF_KEEP_N 257
|
|
#define OPT_CONF_STALE_TIMEOUT 258
|
|
#define OPT_VCPUS 259
|
|
#define OPT_CERTDIR 260
|
|
#define OPT_SYNC_TIMEOUT 261
|
|
#define OPT_WAL_SYNC 262
|
|
#define OPT_MAX_WAL_RECORDS 263
|
|
#define OPT_WAL_SEGMENT_BYTES 264
|
|
|
|
struct arguments {
|
|
char* myname;
|
|
char* logfile;
|
|
char* workdir;
|
|
int verbose;
|
|
char* peers_str;
|
|
int conf_keep_n;
|
|
long conf_stale_timeout;
|
|
int vcpus;
|
|
char* certdir;
|
|
uint32_t sync_timeout_ms;
|
|
uint32_t wal_sync_ms;
|
|
uint32_t max_wal_records;
|
|
uint32_t wal_segment_bytes;
|
|
};
|
|
|
|
static struct argp_option options[] = {
|
|
{ "workdir", 'w', "DIR", 0, "The work directory of xce controller" },
|
|
{ "logfile", 'l', "FILE", 0, "The full path of log file" },
|
|
{ "verbose", 'v', 0, 0, "Produce verbose logs" },
|
|
{ "myname", 'n', "STR", 0, "The name of controller" },
|
|
{ "peers", OPT_PEERS, "PEERS", 0,
|
|
"HA peer list: comma-separated host:port pairs ordered by priority "
|
|
"(e.g. ctrl01:9990,ctrl02:9990). Index 0 = highest priority (primary)." },
|
|
{ "conf-keep-n", OPT_CONF_KEEP_N, "N", 0,
|
|
"Versioned config files to retain (default: 2)" },
|
|
{ "conf-stale-timeout", OPT_CONF_STALE_TIMEOUT, "SECS", 0,
|
|
"Seconds before saved config is flagged stale (0=disabled, default: 86400)" },
|
|
{ "vcpus", OPT_VCPUS, "N", 0,
|
|
"Number of virtual CPUs for the task manager (must be >= 1)" },
|
|
{ "certdir", OPT_CERTDIR, "DIR", 0,
|
|
"Directory containing xce.crt and xce.key (default: <workdir>/certs)" },
|
|
{ "sync-timeout-ms", OPT_SYNC_TIMEOUT, "MS", 0,
|
|
"Synchronous majority-replication commit barrier timeout in ms (0 = default)" },
|
|
{ "wal-sync-ms", OPT_WAL_SYNC, "MS", 0,
|
|
"WAL flush (fsync) cadence in ms (0 = default)" },
|
|
{ "max-wal-records", OPT_MAX_WAL_RECORDS, "N", 0,
|
|
"WAL records since last checkpoint before an async checkpoint is "
|
|
"scheduled (0 = default; persist enforces a minimum of 100)" },
|
|
{ "wal-segment-bytes", OPT_WAL_SEGMENT_BYTES, "N", 0,
|
|
"Physical WAL segment file size in bytes; a segment is only eligible "
|
|
"for trim/delete after it rotates out (0 = default 64 MB; persist "
|
|
"enforces a minimum of 4096)" },
|
|
{ 0 }
|
|
};
|
|
|
|
|
|
static error_t parse_opt(int key, char* arg, struct argp_state* state)
|
|
{
|
|
struct arguments* arguments = state->input;
|
|
|
|
switch (key) {
|
|
case 'l':
|
|
arguments->logfile = arg;
|
|
break;
|
|
case 'v':
|
|
arguments->verbose++;
|
|
break;
|
|
case 'w':
|
|
arguments->workdir = arg;
|
|
break;
|
|
case 'n':
|
|
arguments->myname = arg;
|
|
break;
|
|
case OPT_PEERS:
|
|
arguments->peers_str = arg;
|
|
break;
|
|
case OPT_CONF_KEEP_N:
|
|
arguments->conf_keep_n = atoi(arg);
|
|
if (arguments->conf_keep_n < 1)
|
|
argp_error(state, "--conf-keep-n must be >= 1 (got: %s)", arg);
|
|
break;
|
|
case OPT_CONF_STALE_TIMEOUT:
|
|
arguments->conf_stale_timeout = atol(arg);
|
|
if (arguments->conf_stale_timeout < 0)
|
|
argp_error(state, "--conf-stale-timeout must be >= 0 (got: %s)", arg);
|
|
break;
|
|
case OPT_VCPUS:
|
|
arguments->vcpus = atoi(arg);
|
|
if (validate_vcpus(arguments->vcpus) != 0)
|
|
argp_error(state, "--vcpus must be >= 1 (got: %s)", arg);
|
|
break;
|
|
case OPT_CERTDIR:
|
|
arguments->certdir = arg;
|
|
break;
|
|
case OPT_SYNC_TIMEOUT: {
|
|
int v = atoi(arg);
|
|
if (v < 0)
|
|
argp_error(state, "--sync-timeout-ms must be >= 0 (got: %s)", arg);
|
|
arguments->sync_timeout_ms = (uint32_t)v;
|
|
break;
|
|
}
|
|
case OPT_WAL_SYNC: {
|
|
int v = atoi(arg);
|
|
if (v < 0)
|
|
argp_error(state, "--wal-sync-ms must be >= 0 (got: %s)", arg);
|
|
arguments->wal_sync_ms = (uint32_t)v;
|
|
break;
|
|
}
|
|
case OPT_MAX_WAL_RECORDS: {
|
|
int v = atoi(arg);
|
|
if (v < 0)
|
|
argp_error(state, "--max-wal-records must be >= 0 (got: %s)", arg);
|
|
arguments->max_wal_records = (uint32_t)v;
|
|
break;
|
|
}
|
|
case OPT_WAL_SEGMENT_BYTES: {
|
|
long v = atol(arg);
|
|
if (v < 0)
|
|
argp_error(state, "--wal-segment-bytes must be >= 0 (got: %s)", arg);
|
|
arguments->wal_segment_bytes = (uint32_t)v;
|
|
break;
|
|
}
|
|
default:
|
|
return ARGP_ERR_UNKNOWN;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static struct argp argp = { options, parse_opt, args_doc, doc };
|
|
|
|
/*
|
|
* Saved argv for self-restart (ha-convergence-recovery Req 1.3).
|
|
*
|
|
* main() captures the original, NULL-terminated argv pointer before argp_parse
|
|
* mutates the program state, so ha_self_restart can execv() the controller back
|
|
* into existence after a demotion/rejoin convergence truncates the divergent WAL
|
|
* suffix. argv supplied to main() is already NULL-terminated per C11 §5.1.2.2.1
|
|
* (argv[argc] == NULL) and lives for the whole program lifetime, so storing the
|
|
* pointer is sufficient — no copy needed.
|
|
*/
|
|
static char **g_saved_argv = NULL;
|
|
|
|
/*
|
|
* controller_saved_argv — accessor declared in ha.h, consumed by ha_self_restart.
|
|
* Returns the NULL-terminated argv captured by main(), or NULL if never saved
|
|
* (e.g. unit-test binaries that do not run main()).
|
|
*/
|
|
char **controller_saved_argv(void)
|
|
{
|
|
return g_saved_argv;
|
|
}
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
struct arguments arguments;
|
|
myconf_t myconf;
|
|
char* logfile = "/tmp/xce_controller.log";
|
|
char* loglevel = "info";
|
|
int logsize = 64 * 1024 * 1024;
|
|
int logcount = 10;
|
|
char key_file[XCE_MAXPATH_LEN + 1];
|
|
char cert_file[XCE_MAXPATH_LEN + 1];
|
|
char myname[XCE_MAXNAME_LEN + 1];
|
|
|
|
/* Capture the NULL-terminated argv for self-restart BEFORE argp_parse so a
|
|
* later demotion/rejoin convergence can execv() the same image (Req 1.3). */
|
|
g_saved_argv = argv;
|
|
|
|
memset(&arguments, 0x0, sizeof(arguments));
|
|
arguments.conf_keep_n = 2;
|
|
arguments.conf_stale_timeout = 86400;
|
|
arguments.vcpus = 0; /* 0 = not set; use cluster config default */
|
|
arguments.sync_timeout_ms = 0; /* 0 = not set; use persist config default */
|
|
arguments.wal_sync_ms = 0; /* 0 = not set; use persist config default */
|
|
arguments.max_wal_records = 0; /* 0 = not set; use persist config default */
|
|
arguments.wal_segment_bytes = 0; /* 0 = not set; use persist config default */
|
|
|
|
argp_parse(&argp, argc, argv, 0, 0, &arguments);
|
|
|
|
if (arguments.logfile != NULL) {
|
|
logfile = arguments.logfile;
|
|
}
|
|
|
|
if (arguments.verbose != 0) {
|
|
loglevel = verbose_to_loglevel(arguments.verbose);
|
|
}
|
|
|
|
mylog_init(logfile, loglevel, logsize, logcount);
|
|
|
|
if (arguments.workdir == NULL) {
|
|
INFO("no cluster work directory specified. The default work directory "
|
|
"`" DEFAULT_WORKDIR "' will be used.");
|
|
arguments.workdir = DEFAULT_WORKDIR;
|
|
}
|
|
|
|
if (arguments.myname == NULL) {
|
|
if (get_myname(myname, sizeof(myname) - 1) < 0) {
|
|
ERROR("get_myname");
|
|
return -1;
|
|
}
|
|
arguments.myname = myname;
|
|
}
|
|
|
|
INFO("My name is %s", arguments.myname);
|
|
|
|
const char *certdir = arguments.certdir;
|
|
char certdir_buf[XCE_MAXPATH_LEN + 1];
|
|
if (!certdir) {
|
|
snprintf(certdir_buf, sizeof(certdir_buf),
|
|
"%s/certs", arguments.workdir);
|
|
certdir = certdir_buf;
|
|
}
|
|
snprintf(key_file, XCE_MAXPATH_LEN, "%s/xce.key", certdir);
|
|
snprintf(cert_file, XCE_MAXPATH_LEN, "%s/xce.crt", certdir);
|
|
|
|
myconf.cluster_config = NULL;
|
|
myconf.conffile = NULL; /* set by load_primary_config() or fetch_config_from_primary() */
|
|
myconf.certfile = cert_file;
|
|
myconf.keyfile = key_file;
|
|
myconf.workdir = arguments.workdir;
|
|
myconf.name = arguments.myname;
|
|
myconf.conf_keep_n = arguments.conf_keep_n;
|
|
myconf.conf_stale_timeout = (time_t)arguments.conf_stale_timeout;
|
|
myconf.vcpus = arguments.vcpus;
|
|
myconf.sync_timeout_ms = arguments.sync_timeout_ms;
|
|
myconf.wal_sync_ms = arguments.wal_sync_ms;
|
|
myconf.max_wal_records = arguments.max_wal_records;
|
|
myconf.wal_segment_bytes = arguments.wal_segment_bytes;
|
|
memset(&myconf.peers_cfg, 0, sizeof(myconf.peers_cfg));
|
|
|
|
if (arguments.peers_str) {
|
|
if (xce_parse_peers(arguments.peers_str, &myconf.peers_cfg) != 0) {
|
|
fprintf(stderr,
|
|
"xce_controller: --peers parse error: expected "
|
|
"host:port,... got: %s\n",
|
|
arguments.peers_str);
|
|
return 1;
|
|
}
|
|
|
|
/* Req 4.1: reject even-N peer counts (cannot form safe majority) */
|
|
if (validate_peer_count(myconf.peers_cfg.candidates_count) != 0) {
|
|
fprintf(stderr,
|
|
"xce_controller: --peers: even peer count (%d) is not "
|
|
"allowed; Raft quorum requires an odd number of nodes "
|
|
"(1, 3, 5, ...)\n",
|
|
myconf.peers_cfg.candidates_count);
|
|
return 1;
|
|
}
|
|
|
|
/* CA cert from certdir (same certificate as certfile) */
|
|
snprintf(myconf.peers_cfg.ca_cert_path,
|
|
sizeof(myconf.peers_cfg.ca_cert_path),
|
|
"%s/xce.crt", certdir);
|
|
strncpy(myconf.peers_cfg.identity, arguments.myname,
|
|
sizeof(myconf.peers_cfg.identity) - 1);
|
|
INFO("HA mode: %d controller candidates, identity=%s",
|
|
myconf.peers_cfg.candidates_count, myconf.peers_cfg.identity);
|
|
for (int i = 0; i < myconf.peers_cfg.candidates_count; i++) {
|
|
if (myconf.peers_cfg.candidates[i].hostname[0] == '\0' ||
|
|
myconf.peers_cfg.candidates[i].port == 0) {
|
|
WARN("--peers: candidate %d has empty host or port 0 — "
|
|
"check peer string", i);
|
|
}
|
|
}
|
|
} else {
|
|
INFO("No --peers flag; running as single-node primary");
|
|
}
|
|
|
|
/* Req 4.2: cert pre-flight — fail early with a clear path in the message */
|
|
{
|
|
char cert_err[XCE_MAXPATH_LEN + 64];
|
|
if (validate_cert_files(cert_file, key_file,
|
|
cert_err, sizeof(cert_err)) != 0) {
|
|
fprintf(stderr, "xce_controller: %s\n", cert_err);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
myapp_run(&myconf);
|
|
|
|
mylog_fini();
|
|
|
|
return 0;
|
|
}
|