feat: implement restore_job/worker_from_capnp for WAL recovery

WAL hooks now write real Cap'n Proto payloads instead of stub uint32_t:
- WAL_JOB_SUBMIT: full xce_job_t serialized via encode_Job/write_Job
- WAL_WORKER_CTRL: [uint32_t detail][xce_worker_t capnp] via encode_Worker
- Other job events (START/FINISH/FAIL/STOP etc): uint64_t jobid only

restore_job(): deserializes Cap'n Proto xce_job_t, guards against duplicate
replay with key_find check, then calls create_job()

worker_from_capnp(): skips 4-byte detail prefix, deserializes xce_worker_t,
guards against duplicate replay, then calls restore_worker()

join.c: pass worker_t* (looked up by idx via vget) to WORKER_JOIN hook
so persist hook can serialize worker data without touching workermgr RTTI
This commit is contained in:
Rongsong Shen 2026-05-08 07:49:27 +00:00
parent b80ca3deaa
commit 9b6a91a89e
4 changed files with 239 additions and 42 deletions

View file

@ -1,9 +1,11 @@
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "cluster.h"
#include "controller.capnp.h"
#include "persist.h"
#include "wobj.h"
#include "worker.h"
@ -652,16 +654,38 @@ int do_hostcontrol_op(cluster_t* c, endpoint_t* ep, xce_request_t* request)
return rc;
}
/* P0 stub — bridges Cap'n Proto deserialization to restore_worker().
* Full implementation in Task 15 (cluster wiring): deserializes the
* Cap'n Proto Worker object, deep-copies pointer fields, and calls
* restore_worker(cls, host). */
/* Reconstruct a worker from a WAL_WORKER_CTRL Cap'n Proto payload.
* Payload layout: [uint32_t detail][Cap'n Proto xce_worker_t].
* Returns -EEXIST if the worker is already registered (idempotent replay). */
int worker_from_capnp(cluster_t* c, const void* capnp_payload, size_t len)
{
(void)c;
(void)capnp_payload;
(void)len;
return 0;
if (!capnp_payload || len <= sizeof(uint32_t))
return -EINVAL;
/* Skip the 4-byte detail discriminator written by persist_worker_hook_fun */
const uint8_t* data = (const uint8_t*)capnp_payload + sizeof(uint32_t);
size_t data_len = len - sizeof(uint32_t);
struct capn c_ctx;
capn_init_mem(&c_ctx, data, data_len, 0);
Worker_ptr wp;
wp.p = capn_getp(capn_root(&c_ctx), 0, 1);
struct Worker cw;
xce_worker_t host;
memset(&host, 0, sizeof(host));
read_Worker(&cw, wp);
decode_Worker(&host, &cw);
capn_free(&c_ctx);
/* Idempotency guard: skip if already present */
worker_t* existing = NULL;
lock(c->workermgr, RTTI_LOCK_READ);
int found = key_find(c->workermgr, (void*)host.name, (void**)&existing);
unlock(c->workermgr);
if (found > 0 && existing != NULL)
return -EEXIST;
return restore_worker(c, host);
}
/* worker_should_retain lives in persist.c —