forked from lei.chen/myxce
feat: heartbeat watchdog pthread + ha_check_heartbeat + start/stop API (P1-3)
- Add ha_start_watchdog() / ha_stop_watchdog() — dedicated pthread with nanosleep(heartbeat_interval_ms) loop; stop via atomic watchdog_stop + pthread_join (no pthread_cancel, safe for OpenSSL) - Add ha_check_heartbeat_nocluster() — reads role with atomic_load_explicit(memory_order_acquire), guards on primary_host[0]=='\0', computes elapsed_ms from CLOCK_MONOTONIC, sets failover_requested on timeout - Update ha_fini() to call ha_stop_watchdog() before mutex destroy - 3 new tests (TDD RED→GREEN): watchdog starts/stops, no-op when no primary, stale heartbeat no crash; all 97 controller tests green
This commit is contained in:
parent
a289edb777
commit
2fae4c3df5
3 changed files with 165 additions and 1 deletions
|
|
@ -1,6 +1,7 @@
|
|||
#include <stdatomic.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <unity/unity.h>
|
||||
|
||||
|
|
@ -84,6 +85,72 @@ static void test_ha_promote_sets_primary_role(void)
|
|||
printf("test_ha_promote_sets_primary_role: PASS\n");
|
||||
}
|
||||
|
||||
/* ── P1-3 watchdog tests ────────────────────────────────────── */
|
||||
|
||||
/* ha_start_watchdog() must be callable after ha_init() */
|
||||
static void test_watchdog_starts_and_stops(void)
|
||||
{
|
||||
ha_state_t ha;
|
||||
ha_config_t cfg = {
|
||||
.heartbeat_interval_ms = 3000,
|
||||
.heartbeat_timeout_ms = 10000,
|
||||
.election_timeout_ms = 2000,
|
||||
.probe_connect_timeout_ms = 500,
|
||||
.max_promotion_retries = 5,
|
||||
};
|
||||
TEST_ASSERT_EQUAL_INT(0, ha_init(&ha, &cfg, 0));
|
||||
TEST_ASSERT_EQUAL_INT(0, ha_start_watchdog(&ha));
|
||||
/* brief sleep to let the thread wake at least once */
|
||||
struct timespec ts = { 0, 50 * 1000000L }; /* 50ms */
|
||||
nanosleep(&ts, NULL);
|
||||
ha_stop_watchdog(&ha); /* sets stop flag + pthread_join */
|
||||
ha_fini(&ha);
|
||||
printf("test_watchdog_starts_and_stops: PASS\n");
|
||||
}
|
||||
|
||||
/* ha_check_heartbeat_nocluster(): when in CTRL_STANDBY with no primary_ep,
|
||||
* must not promote */
|
||||
static void test_heartbeat_no_action_when_no_primary(void)
|
||||
{
|
||||
ha_state_t ha;
|
||||
ha_config_t cfg = {
|
||||
.heartbeat_interval_ms = 3000,
|
||||
.heartbeat_timeout_ms = 10000,
|
||||
.election_timeout_ms = 2000,
|
||||
.probe_connect_timeout_ms = 500,
|
||||
.max_promotion_retries = 5,
|
||||
};
|
||||
ha_init(&ha, &cfg, 1 /* not highest priority */);
|
||||
/* No primary_host set — watchdog must not promote */
|
||||
ha_check_heartbeat_nocluster(&ha); /* test-only accessor */
|
||||
TEST_ASSERT_EQUAL_INT(CTRL_STANDBY,
|
||||
(int)atomic_load_explicit(&ha.role, memory_order_acquire));
|
||||
ha_fini(&ha);
|
||||
printf("test_heartbeat_no_action_when_no_primary: PASS\n");
|
||||
}
|
||||
|
||||
/* ha_check_heartbeat_nocluster(): when last_heartbeat is stale (timeout exceeded)
|
||||
* and primary_host is empty (no known primary), must not crash or change role */
|
||||
static void test_heartbeat_stale_no_crash_without_primary(void)
|
||||
{
|
||||
ha_state_t ha;
|
||||
ha_config_t cfg = {
|
||||
.heartbeat_interval_ms = 3000,
|
||||
.heartbeat_timeout_ms = 100, /* very short for test */
|
||||
.election_timeout_ms = 2000,
|
||||
.probe_connect_timeout_ms = 500,
|
||||
.max_promotion_retries = 5,
|
||||
};
|
||||
ha_init(&ha, &cfg, 0);
|
||||
/* Set last_heartbeat to a time far in the past to simulate timeout */
|
||||
clock_gettime(CLOCK_MONOTONIC, &ha.last_heartbeat);
|
||||
ha.last_heartbeat.tv_sec -= 10; /* 10 seconds ago */
|
||||
ha_check_heartbeat_nocluster(&ha);
|
||||
/* Should not crash. Role may or may not change — but no segfault. */
|
||||
ha_fini(&ha);
|
||||
printf("test_heartbeat_stale_no_crash_without_primary: PASS\n");
|
||||
}
|
||||
|
||||
void test_ha_role(void)
|
||||
{
|
||||
RUN_TEST(test_ha_init_default_role_is_standby);
|
||||
|
|
@ -91,4 +158,7 @@ void test_ha_role(void)
|
|||
RUN_TEST(test_ha_init_rejects_short_election_timeout);
|
||||
RUN_TEST(test_ha_init_rejects_timeout_less_than_interval);
|
||||
RUN_TEST(test_ha_promote_sets_primary_role);
|
||||
RUN_TEST(test_watchdog_starts_and_stops);
|
||||
RUN_TEST(test_heartbeat_no_action_when_no_primary);
|
||||
RUN_TEST(test_heartbeat_stale_no_crash_without_primary);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue