myxce/src/controller/join.c
Rongsong Shen 9b6a91a89e 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
2026-05-08 07:49:27 +00:00

247 lines
6.5 KiB
C

#include "config.h"
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "cluster.h"
#include "ha.h"
#include "xce/cluster_def.h"
#include "xce/errnox.h"
static int validate_host(cluster_t* c, const char* host, const char* key)
{
return 1;
}
static int handle_client_join(cluster_t* c, endpoint_t* ep,
xce_request_t* request)
{
xce_reply_t reply;
init_reply_by_req(&reply, request);
reply.status = 0;
reply.data_which = RequestReply_data_nodata;
xce_reply(ep, c->myhost, &reply);
return 0;
}
static int handle_worker_join(cluster_t* c, endpoint_t* ep,
xce_request_t* request)
{
int idx;
ep_private_t* priv = (ep_private_t*)ep->data;
xce_reply_t reply;
xce_peerinfo_t* from = request->from;
int rc = 0;
xce_joindata_t* joinmsg = NULL;
TRACE("%s worker %s joining...", __func__, from->name);
init_reply_by_req(&reply, request);
if (!validate_host(c, from->name, from->key)) {
ERROR("%s reject join of host %s", __func__, from->name);
rc = -EPERM;
reply.status = rc;
reply.data_which = RequestReply_data_nodata;
xce_reply(ep, c->myhost, &reply);
return rc;
}
joinmsg = request->body.joinmsg;
lock(c->workermgr, RTTI_LOCK_WRITE);
xce_run_hooks(&(c->hooksys), XCE_CONTROLLER_HOOK_WORKER, HOOK_BEFORE_EVENT,
HOOK_DETAIL_WORKER_JOIN, c, from->name, 0);
idx = add_worker(c, ep, from->name, joinmsg->data.attrs);
reply.status = idx;
reply.data_which = RequestReply_data_nodata;
/* Look up the newly added worker so the persist hook can serialize it */
worker_t* w = NULL;
if (idx >= 0)
vget(c->workermgr, idx, (void**)&w);
xce_run_hooks(&(c->hooksys), XCE_CONTROLLER_HOOK_WORKER, HOOK_AFTER_EVENT,
HOOK_DETAIL_WORKER_JOIN, c, w);
xce_reply(ep, c->myhost, &reply);
unlock(c->workermgr);
if (idx < 0) {
ERROR("worker %s join failed, rc %d", from->name, idx);
return idx;
}
priv->u.idx = idx;
on_worker_join(c, ep);
return 0;
}
static int handle_scheduler_join(cluster_t* c, endpoint_t* ep,
xce_request_t* request)
{
xce_reply_t reply;
ep_private_t* priv = (ep_private_t*)ep->data;
int rc = 0;
TRACE("%s scheduler join, ep %p", __func__, ep);
init_reply_by_req(&reply, request);
c->sched.ep = ep;
priv->u.idx = 0;
rc = on_scheduler_join(c);
reply.status = rc;
reply.data_which = RequestReply_data_nodata;
xce_reply(ep, c->myhost, &reply);
return rc;
}
static int handle_controller_join(cluster_t* c, endpoint_t* ep,
xce_request_t* request)
{
xce_reply_t reply;
int rc = -ENOSYS;
ERROR("we still have not support join of controller");
init_reply_by_req(&reply, request);
reply.status = rc;
reply.data_which = RequestReply_data_nodata;
xce_reply(ep, c->myhost, &reply);
return rc;
}
static int handle_apiserver_join(cluster_t* c, endpoint_t* ep,
xce_request_t* request)
{
xce_reply_t reply;
xce_peerinfo_t* from = request->from;
int rc = 0;
TRACE("%s api server join, ep %p", __func__, ep);
init_reply_by_req(&reply, request);
rc = add_apiserver(c, from->name, ep);
if (rc >= 0) {
ep_private_t* priv = (ep_private_t*)ep->data;
priv->u.idx = rc;
on_apiserver_join(c, ep);
}
reply.status = rc;
reply.data_which = RequestReply_data_nodata;
xce_reply(ep, c->myhost, &reply);
if (rc < 0) {
ERROR("api server %s join failed, rc %d", from->name, rc);
return rc;
}
return 0;
}
static int handle_join_(cluster_t* c, endpoint_t* ep, xce_request_t* request)
{
ep_private_t* priv;
int rc = -EINVAL;
int role;
xce_reply_t reply;
xce_peerinfo_t* from = request->from;
init_reply_by_req(&reply, request);
priv = (ep_private_t*)ep->data;
if (priv == NULL) {
rc = -E_XCE_INTERNAL;
ERROR("priv data of endpoint should not be NULL");
reply.status = rc;
reply.data_which = RequestReply_data_nodata;
xce_reply(ep, c->myhost, &reply);
return rc;
}
role = request->body.joinmsg->role;
priv->role = role;
TRACE("%s role %d join from %s", __func__, role, from->name);
/* ── HA guard (P1-6) ────────────────────────────────────────
* A standby must never accept joins as if it were the primary.
* If the primary address is known, tell the client where to go
* (E_XCE_REDIRECT). If no primary is known yet, tell the client
* to retry later (E_XCE_NOMASTER).
*/
if (ha_should_redirect(&c->ha)) {
WARN("%s standby redirecting role-%d join from %s to %s:%u",
__func__, role, from->name,
c->ha.primary_host, c->ha.primary_port);
rc = -E_XCE_REDIRECT;
reply.status = rc;
reply.data_which = RequestReply_data_nodata;
xce_reply(ep, c->myhost, &reply);
return rc;
}
if (ha_no_primary_known(&c->ha)) {
WARN("%s standby has no primary yet, rejecting role-%d join from %s",
__func__, role, from->name);
rc = -E_XCE_NOMASTER;
reply.status = rc;
reply.data_which = RequestReply_data_nodata;
xce_reply(ep, c->myhost, &reply);
return rc;
}
switch (role) {
case HostRole_client:
rc = handle_client_join(c, ep, request);
break;
case HostRole_worker:
rc = handle_worker_join(c, ep, request);
break;
case HostRole_scheduler:
rc = handle_scheduler_join(c, ep, request);
break;
case HostRole_controller:
rc = handle_controller_join(c, ep, request);
break;
case HostRole_apiserver:
rc = handle_apiserver_join(c, ep, request);
break;
default:
rc = -E_XCE_BAD_ROLE;
ERROR("unknown role %d from host %s", role, from->name);
reply.status = rc;
reply.data_which = RequestReply_data_nodata;
xce_reply(ep, c->myhost, &reply);
break;
}
return rc;
}
int handle_join(cluster_t* c, endpoint_t* ep, xce_request_t* request)
{
int rc = 0;
rc = handle_join_(c, ep, request);
return rc;
}