feat: JOB_STAT_TRANSIENT, job_should_retain, worker_should_retain, dead_since (P0-5a, P0-8, P0-13)

Add terminal-state constants and GC retention predicates for the
checkpoint xzarray_thaw() GC pass:

- types.h: add JOB_STAT_TRANSIENT (0x400), XCE_WORKER_HIDDEN (0x00200000),
  QUEUE_STAT_HIDDEN (0x0100)
- persist.c: implement job_is_terminal(), job_should_retain(),
  worker_should_retain() — placed here so the unit test binary
  (which links only persist.c) can access them without RTTI deps
- persist.h: declare the three GC predicates; include xce/types.h
- worker.c: set dead_since = time(NULL) in mark_worker_unavail()
  when dead_since is not already set
- cluster.c: add TODO P0-8b marker on zarray_del endpoint cleanup
- meson.build: register test_gc_retention.c; raise test timeout 60→120s
- testsuite/test_gc_retention.c: 19 tests covering job_is_terminal,
  job_should_retain (TRANSIENT TTL, terminal TTL, non-terminal always
  retained), and worker_should_retain (HIDDEN/UNAVAIL/healthy/zero
  dead_since); all pass (63 total, 0 failures)
This commit is contained in:
Rongsong Shen 2026-05-07 12:52:07 +00:00
parent 6f3b98c6b5
commit 3030012f51
9 changed files with 330 additions and 2 deletions

View file

@ -12,6 +12,7 @@ extern void test_persist_checkpoint(void);
extern void test_persist_recovery(void);
extern void test_hooks(void);
extern void test_persist_hooks(void);
extern void test_gc_retention(void);
void setUp(void) { return; }
@ -93,6 +94,7 @@ int main(int argc, char* argv[])
TEST(test_persist_recovery);
TEST(test_hooks);
TEST(test_persist_hooks);
TEST(test_gc_retention);
return UNITY_END();
}

View file

@ -0,0 +1,244 @@
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unity/unity.h>
#include "config.h"
#include "persist.h"
#include "xce/types.h"
/* ------------------------------------------------------------------ */
/* Helpers to build minimal job_t / xce_worker_t fixtures */
/* ------------------------------------------------------------------ */
static job_t make_job(unsigned int state, time_t finish_time,
uint64_t transient_since)
{
job_t j;
memset(&j, 0, sizeof(j));
j.job.state = state;
j.job.finish_time = finish_time;
j.job.transient_since = (uint64_t)transient_since;
return j;
}
static xce_worker_t make_worker(uint32_t status, time_t dead_since)
{
xce_worker_t w;
memset(&w, 0, sizeof(w));
w.status = status;
w.dead_since = dead_since;
return w;
}
/* ------------------------------------------------------------------ */
/* Forward declarations for functions under test */
/* ------------------------------------------------------------------ */
extern bool job_is_terminal(const job_t* j);
extern bool job_should_retain(const job_t* j, const persist_config_t* cfg);
extern bool worker_should_retain(const xce_worker_t* w,
const persist_config_t* cfg);
/* ------------------------------------------------------------------ */
/* job_is_terminal tests */
/* ------------------------------------------------------------------ */
static void test_running_job_not_terminal(void)
{
job_t j = make_job(JOB_STAT_RUN, 0, 0);
TEST_ASSERT_FALSE(job_is_terminal(&j));
}
static void test_pending_job_not_terminal(void)
{
job_t j = make_job(JOB_STAT_PEND, 0, 0);
TEST_ASSERT_FALSE(job_is_terminal(&j));
}
static void test_exit_job_is_terminal(void)
{
job_t j = make_job(JOB_STAT_EXIT, time(NULL), 0);
TEST_ASSERT_TRUE(job_is_terminal(&j));
}
static void test_done_job_is_terminal(void)
{
job_t j = make_job(JOB_STAT_DONE, time(NULL), 0);
TEST_ASSERT_TRUE(job_is_terminal(&j));
}
static void test_perr_job_is_terminal(void)
{
job_t j = make_job(JOB_STAT_PERR, time(NULL), 0);
TEST_ASSERT_TRUE(job_is_terminal(&j));
}
static void test_transient_job_not_terminal(void)
{
/* JOB_STAT_TRANSIENT is NOT terminal — it's a special non-terminal state */
job_t j = make_job(JOB_STAT_TRANSIENT, 0, (uint64_t)time(NULL));
TEST_ASSERT_FALSE(job_is_terminal(&j));
}
/* ------------------------------------------------------------------ */
/* job_should_retain: TRANSIENT timeout tests */
/* ------------------------------------------------------------------ */
static void test_transient_job_retained_within_timeout(void)
{
/* transient_since = now - 100s, timeout = 300s → retain */
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
cfg.transient_job_timeout_s = 300;
job_t j = make_job(JOB_STAT_TRANSIENT, 0, now - 100);
TEST_ASSERT_TRUE(job_should_retain(&j, &cfg));
}
static void test_transient_job_expired(void)
{
/* transient_since = now - 400s, timeout = 300s → do NOT retain */
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
cfg.transient_job_timeout_s = 300;
job_t j = make_job(JOB_STAT_TRANSIENT, 0, now - 400);
TEST_ASSERT_FALSE(job_should_retain(&j, &cfg));
}
static void test_transient_job_zero_since_always_retained(void)
{
/* transient_since == 0 means unset → never expire (operator must decide) */
persist_config_t cfg = persist_config_defaults();
cfg.transient_job_timeout_s = 300;
job_t j = make_job(JOB_STAT_TRANSIENT, 0, 0);
TEST_ASSERT_TRUE(job_should_retain(&j, &cfg));
}
/* ------------------------------------------------------------------ */
/* job_should_retain: terminal job TTL tests */
/* ------------------------------------------------------------------ */
static void test_finished_job_retained_within_ttl(void)
{
/* finish_time = now - 1800s, retain = 3600s → retain */
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
cfg.finished_retain_s = 3600;
job_t j = make_job(JOB_STAT_EXIT, now - 1800, 0);
TEST_ASSERT_TRUE(job_should_retain(&j, &cfg));
}
static void test_finished_job_expired(void)
{
/* finish_time = now - 7200s, retain = 3600s → do NOT retain */
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
cfg.finished_retain_s = 3600;
job_t j = make_job(JOB_STAT_EXIT, now - 7200, 0);
TEST_ASSERT_FALSE(job_should_retain(&j, &cfg));
}
static void test_done_job_retained_within_ttl(void)
{
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
cfg.finished_retain_s = 3600;
job_t j = make_job(JOB_STAT_DONE, now - 1800, 0);
TEST_ASSERT_TRUE(job_should_retain(&j, &cfg));
}
static void test_running_job_always_retained(void)
{
persist_config_t cfg = persist_config_defaults();
job_t j = make_job(JOB_STAT_RUN, 0, 0);
TEST_ASSERT_TRUE(job_should_retain(&j, &cfg));
}
static void test_pending_job_always_retained(void)
{
persist_config_t cfg = persist_config_defaults();
job_t j = make_job(JOB_STAT_PEND, 0, 0);
TEST_ASSERT_TRUE(job_should_retain(&j, &cfg));
}
/* ------------------------------------------------------------------ */
/* worker_should_retain tests */
/* ------------------------------------------------------------------ */
static void test_worker_unavail_retained_within_ttl(void)
{
/* dead_since = now - 43200s (12 h), retain = 86400s (24 h) → retain */
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
cfg.worker_retain_s = 86400;
xce_worker_t w = make_worker(XCE_WORKER_UNAVAIL, now - 43200);
TEST_ASSERT_TRUE(worker_should_retain(&w, &cfg));
}
static void test_worker_unavail_expired(void)
{
/* dead_since = now - 100000s, retain = 86400s → do NOT retain */
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
cfg.worker_retain_s = 86400;
xce_worker_t w = make_worker(XCE_WORKER_UNAVAIL, now - 100000);
TEST_ASSERT_FALSE(worker_should_retain(&w, &cfg));
}
static void test_worker_hidden_never_retained(void)
{
/* XCE_WORKER_HIDDEN → cleanup immediately regardless of dead_since */
time_t now = time(NULL);
persist_config_t cfg = persist_config_defaults();
xce_worker_t w = make_worker(XCE_WORKER_HIDDEN, now - 10);
TEST_ASSERT_FALSE(worker_should_retain(&w, &cfg));
}
static void test_worker_healthy_always_retained(void)
{
persist_config_t cfg = persist_config_defaults();
xce_worker_t w = make_worker(0, 0);
TEST_ASSERT_TRUE(worker_should_retain(&w, &cfg));
}
static void test_worker_unavail_zero_dead_since_always_retained(void)
{
/* dead_since == 0 → not yet set, do not GC */
persist_config_t cfg = persist_config_defaults();
cfg.worker_retain_s = 86400;
xce_worker_t w = make_worker(XCE_WORKER_UNAVAIL, 0);
TEST_ASSERT_TRUE(worker_should_retain(&w, &cfg));
}
/* ------------------------------------------------------------------ */
/* Test suite entry point */
/* ------------------------------------------------------------------ */
void test_gc_retention(void)
{
RUN_TEST(test_running_job_not_terminal);
RUN_TEST(test_pending_job_not_terminal);
RUN_TEST(test_exit_job_is_terminal);
RUN_TEST(test_done_job_is_terminal);
RUN_TEST(test_perr_job_is_terminal);
RUN_TEST(test_transient_job_not_terminal);
RUN_TEST(test_transient_job_retained_within_timeout);
RUN_TEST(test_transient_job_expired);
RUN_TEST(test_transient_job_zero_since_always_retained);
RUN_TEST(test_finished_job_retained_within_ttl);
RUN_TEST(test_finished_job_expired);
RUN_TEST(test_done_job_retained_within_ttl);
RUN_TEST(test_running_job_always_retained);
RUN_TEST(test_pending_job_always_retained);
RUN_TEST(test_worker_unavail_retained_within_ttl);
RUN_TEST(test_worker_unavail_expired);
RUN_TEST(test_worker_hidden_never_retained);
RUN_TEST(test_worker_healthy_always_retained);
RUN_TEST(test_worker_unavail_zero_dead_since_always_retained);
}