feat: add persist.h — WAL types, persist_config_t, slot_key_t (P0-B)
- Define wal_entry_hdr_t (40 bytes, _Static_assert enforced)
- Define wal_entry_type enum (WAL_JOB_SUBMIT=1 … WAL_CHECKPOINT=100)
- Define persist_config_t with persist_config_defaults() inline helper
- Define slot_key_t (index, key[XCE_MAXNAME_LEN], key_len)
- Forward-declare persist_t opaque struct and full persist_* API surface
- Bootstrap controller Unity test suite (controller_unit_test binary)
- Register test('controller test', …, suite: 'controller') in meson
- 4 new tests all PASS: wal_header_size, wal_entry_types,
persist_config_defaults, slot_key_t_fields
This commit is contained in:
parent
0107828a77
commit
fe69dcbd7a
4 changed files with 282 additions and 0 deletions
106
src/include/persist.h
Normal file
106
src/include/persist.h
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
#pragma once
|
||||
#include <pthread.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
/* Forward declaration */
|
||||
typedef struct cluster_s cluster_t;
|
||||
|
||||
/* ============================================================
|
||||
* WAL entry header — 40 bytes, 8-byte aligned (Part 2.2)
|
||||
* ============================================================ */
|
||||
typedef struct {
|
||||
uint64_t term; /* Election term (incremented on failover) */
|
||||
uint64_t lsn; /* Log sequence number within this term */
|
||||
uint64_t timestamp; /* Epoch microseconds */
|
||||
uint32_t type; /* wal_entry_type enum value */
|
||||
uint32_t payload_len; /* Bytes of payload following this header */
|
||||
uint32_t crc; /* CRC32(header[0..WAL_HDR_CRC_SIZE-1] + payload) */
|
||||
uint32_t prev_crc; /* CRC of previous record; WAL_CRC_NONE for first in segment */
|
||||
} wal_entry_hdr_t;
|
||||
_Static_assert(sizeof(wal_entry_hdr_t) == 40,
|
||||
"wal_entry_hdr_t must be 40 bytes for on-disk format stability");
|
||||
|
||||
/* ============================================================
|
||||
* WAL entry types (Part 2.3)
|
||||
* ============================================================ */
|
||||
typedef enum {
|
||||
WAL_JOB_SUBMIT = 1, /* SYNC replication */
|
||||
WAL_JOB_START = 2,
|
||||
WAL_JOB_FINISH = 3,
|
||||
WAL_JOB_FAIL = 4,
|
||||
WAL_JOB_STOP = 5,
|
||||
WAL_JOB_RESUME = 6,
|
||||
WAL_JOB_UPDATE = 7,
|
||||
WAL_JOB_RUSAGE = 8,
|
||||
WAL_JOB_CKPT = 9,
|
||||
WAL_JOB_PREASON = 10,
|
||||
WAL_QUEUE_CTRL = 20,
|
||||
WAL_WORKER_CTRL = 30,
|
||||
WAL_CHECKPOINT = 100, /* Local WAL marker only — never replicated */
|
||||
} wal_entry_type;
|
||||
|
||||
/* ============================================================
|
||||
* Persistence configuration (Part 2.6)
|
||||
* ============================================================ */
|
||||
typedef struct {
|
||||
uint32_t wal_sync_ms; /* fsync interval in ms (default 10) */
|
||||
uint32_t snap_interval_s; /* checkpoint interval in seconds (default 60) */
|
||||
uint32_t snap_wal_size_mb; /* checkpoint when WAL exceeds this MB (default 64) */
|
||||
uint32_t snap_keep_n; /* snapshots to retain (default 2) */
|
||||
uint32_t transient_job_timeout_s; /* seconds before TRANSIENT job auto-terminates (default 300) */
|
||||
uint32_t finished_retain_s; /* retain finished jobs N seconds (default 3600) */
|
||||
uint32_t worker_retain_s; /* retain unavailable workers N seconds (default 86400) */
|
||||
uint32_t sync_timeout_ms; /* sync replication ACK timeout ms (default 50) */
|
||||
} persist_config_t;
|
||||
|
||||
static inline persist_config_t persist_config_defaults(void)
|
||||
{
|
||||
persist_config_t c;
|
||||
c.wal_sync_ms = DEFAULT_WAL_SYNC_MS;
|
||||
c.snap_interval_s = DEFAULT_SNAP_INTERVAL_S;
|
||||
c.snap_wal_size_mb = DEFAULT_SNAP_WAL_SIZE_MB;
|
||||
c.snap_keep_n = DEFAULT_SNAP_KEEP_N;
|
||||
c.transient_job_timeout_s = DEFAULT_TRANSIENT_JOB_TIMEOUT_S;
|
||||
c.finished_retain_s = DEFAULT_FINISHED_RETAIN_S;
|
||||
c.worker_retain_s = DEFAULT_WORKER_RETAIN_S;
|
||||
c.sync_timeout_ms = DEFAULT_SYNC_TIMEOUT_MS;
|
||||
return c;
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Key→index mapping for checkpoint serialization (Part 3.8)
|
||||
* ============================================================ */
|
||||
typedef struct {
|
||||
int index; /* shadow slot index in the frozen zarray */
|
||||
char key[XCE_MAXNAME_LEN]; /* owned deep copy — not a borrowed kvtab pointer */
|
||||
size_t key_len; /* length including null terminator */
|
||||
} slot_key_t;
|
||||
|
||||
/* ============================================================
|
||||
* persist_t — opaque; defined in persist.c (Task 7)
|
||||
* ============================================================ */
|
||||
typedef struct persist_s persist_t;
|
||||
|
||||
/* Lifecycle */
|
||||
int persist_init(persist_t* p, persist_config_t* config, cluster_t* c);
|
||||
int persist_start(persist_t* p);
|
||||
void persist_stop(persist_t* p);
|
||||
void persist_fini(persist_t* p);
|
||||
int persist_recover(persist_t* p);
|
||||
|
||||
/* WAL write paths */
|
||||
int persist_wal_async(persist_t* p, uint32_t type,
|
||||
const void* payload, uint32_t len);
|
||||
int persist_wal_sync_append(persist_t* p, uint32_t type,
|
||||
const void* payload, uint32_t len,
|
||||
int* sync_rc_out);
|
||||
|
||||
/* Checkpoint */
|
||||
int persist_checkpoint(persist_t* p);
|
||||
|
||||
/* Logging helper — never logs payload content (security invariant) */
|
||||
void persist_log_entry_metadata(const wal_entry_hdr_t* hdr);
|
||||
Loading…
Add table
Add a link
Reference in a new issue