myxce/src/controller/join.c
Rongsong Shen 045d9e3287 feat: add HOOK_DETAIL_JOB_STOP/RESUME; fix xce_reply ordering after AFTER_EVENT hooks (P0-4a)
- cluster_def.h: add struct tag `cluster_s` to cluster_t to resolve
  conflicting typedef with persist.h forward declaration
- cluster_def.h: add HOOK_DETAIL_JOB_STOP and HOOK_DETAIL_JOB_RESUME enum
  values after HOOK_DETAIL_JOB_RUSAGE_UPDATE
- job.c on_job_stop(): replace mark_job_update() call with direct
  xce_run_hooks(BEFORE/AFTER, HOOK_DETAIL_JOB_STOP) so WAL writer
  records stop events with the correct detail value
- job.c on_job_resume(): same pattern with HOOK_DETAIL_JOB_RESUME
- job.c do_clr_preason(): fix typo — second hook call was HOOK_BEFORE_EVENT
  instead of HOOK_AFTER_EVENT (double-before, zero-after bug)
- join.c handle_worker_join(): move xce_reply() to after HOOK_AFTER_EVENT
  so client ACK is not sent before WAL write completes (durability fix)
- testsuite/test_hooks.c: new TDD tests for JOB_STOP/RESUME enum values
- testsuite/controller_test.c + meson.build: register test_hooks suite

Reply ordering audit results:
  job.c handle_jobsubmit: reply before AFTER_EVENT — intentional (jobid
    returned to client before scheduler notification; WAL hook belongs in
    BEFORE_EVENT for this path, handled in Task 13)
  job.c do_set_preason: reply before set_preason() loop — intentional
    fire-and-forget (no AFTER_EVENT hook at top level)
  job.c do_clr_preason: reply before loop — same fire-and-forget pattern
  job.c do_jobcontrol_op: no AFTER_EVENT hook, reply only — OK
  join.c handle_worker_join: FIXED (reply moved after AFTER_EVENT)
  worker.c on_worker_update/dead: reply after all hooks — OK
  cluster.c: AFTER_EVENT hooks are internal (no reply) — OK
2026-05-07 12:08:52 +00:00

216 lines
5.2 KiB
C

#include "config.h"
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "cluster.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;
xce_run_hooks(&(c->hooksys), XCE_CONTROLLER_HOOK_WORKER, HOOK_AFTER_EVENT,
HOOK_DETAIL_WORKER_JOIN, c, from->name, idx);
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);
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;
}