feat(controller-peers-env-fallback): flush the controller's log before every early exit (task 2.2)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Rongsong Shen 2026-10-03 06:04:19 +00:00
parent 7a90af089c
commit 25e2807355
Signed by untrusted user: rshen
GPG key ID: 80533536B0EB651C
3 changed files with 47 additions and 1 deletions

View file

@ -392,6 +392,10 @@ int main(int argc, char* argv[])
mylog_init(logfile, loglevel, logsize, logcount);
/* tlog writes the log file from a background thread and has no atexit
* flush, so every early return below calls mylog_fini() (tlog_exit())
* first; otherwise lines logged just before the return can be lost. */
if (arguments.workdir == NULL) {
INFO("no cluster work directory specified. The default work directory "
"`" DEFAULT_WORKDIR "' will be used.");
@ -401,6 +405,7 @@ int main(int argc, char* argv[])
if (arguments.myname == NULL) {
if (get_myname(myname, sizeof(myname) - 1) < 0) {
ERROR("get_myname");
mylog_fini();
return -1;
}
arguments.myname = myname;
@ -449,6 +454,7 @@ int main(int argc, char* argv[])
* cluster_run/myapp_run can ever call it. */
if (mylock_init(&myconf.lock) != 0) {
ERROR("Failed to initialize myconf lock");
mylog_fini();
return -1;
}
@ -458,6 +464,7 @@ int main(int argc, char* argv[])
"xce_controller: --peers parse error: expected "
"host:port,... got: %s\n",
arguments.peers_str);
mylog_fini();
return 1;
}
@ -468,6 +475,7 @@ int main(int argc, char* argv[])
"allowed; Raft quorum requires an odd number of nodes "
"(1, 3, 5, ...)\n",
myconf.peers_cfg.candidates_count);
mylog_fini();
return 1;
}
@ -492,6 +500,7 @@ int main(int argc, char* argv[])
if (validate_cert_files(cert_file, key_file,
cert_err, sizeof(cert_err)) != 0) {
fprintf(stderr, "xce_controller: %s\n", cert_err);
mylog_fini();
return 1;
}
}

View file

@ -300,6 +300,41 @@ on_exit() {
# them with add_case, in the order they should run.
# ════════════════════════════════════════════════════════════════════════════
# Task 2.2 (Req 1.3): the controller logs through tlog's background writer,
# so a line logged just before an early return is only in the log file if the
# return flushes the log first. With an empty --certdir and no --peers (and
# no XCE_CONTROLLER_PEERS) the controller stops at the certificate pre-flight
# right after its "No --peers flag" line. The loss is a race, so repeat the
# run and fail if any single run is missing the line.
FLUSH_RUNS=20
case_controller_log_flushed_on_early_exit() {
local i d missing=0
for i in $(seq 1 "$FLUSH_RUNS"); do
d="$(new_scratch controller-flush)"
mkdir -p "$d/work" "$d/certs"
run_to_completion "$d" 10 -u XCE_CONTROLLER_PEERS \
"$CONTROLLER_BIN" --myname ctrl-flush --workdir "$d/work" \
--certdir "$d/certs" --logfile "$d/controller.log"
if [ "$RUN_RC" -eq 0 ] || [ "$RUN_RC" -ge 124 ]; then
red " run $i: expected the certificate pre-flight to stop the controller, exit status $RUN_RC"
_show_tail "$d/stderr"
return 1
fi
if ! grep -Eq -- 'No --peers flag; running as single-node primary' \
"$d/controller.log" 2>/dev/null; then
missing=$((missing + 1))
red " run $i: last startup line missing from $d/controller.log"
fi
done
if [ "$missing" -ne 0 ]; then
red " $missing/$FLUSH_RUNS run(s) lost the last startup line"
return 1
fi
log " all $FLUSH_RUNS runs had the last startup line in the log file"
}
add_case case_controller_log_flushed_on_early_exit \
"controller: log flushed before the certificate pre-flight exit ($FLUSH_RUNS runs)"
# ════════════════════════════════════════════════════════════════════════════
# main