support passing args to request handle for elimination of global var

This commit is contained in:
Rongsong Shen 2025-12-11 17:07:51 +08:00
parent 79124c6e5c
commit 486b38d679
8 changed files with 120 additions and 19 deletions

View file

@ -83,7 +83,7 @@ typedef struct {
static run_job_t run_jobs[MAX_RUN_JOBS]; static run_job_t run_jobs[MAX_RUN_JOBS];
static run_task_t run_tasks[MAX_RUN_TASKS]; static run_task_t run_tasks[MAX_RUN_TASKS];
static int myreq_handler(xce_request_t*); static int myreq_handler(xce_request_t*, void*);
extern int cmd_help(int, char*[]); extern int cmd_help(int, char*[]);
extern int cmd_up(int, char*[]); extern int cmd_up(int, char*[]);
@ -265,7 +265,7 @@ int enter_ctx(command_ctx_t* ctx)
config.key = get_setting("secret"); config.key = get_setting("secret");
xcecli_ctx_init(&(ctx->ctx), &config); xcecli_ctx_init(&(ctx->ctx), &config);
xcecli_set_handler(ctx->ctx, &myreq_handler); xcecli_set_handler(ctx->ctx, &myreq_handler, NULL);
xcecli_start(ctx->ctx); xcecli_start(ctx->ctx);
return 0; return 0;
@ -2100,7 +2100,7 @@ static int handle_runtasks(xce_request_t* request)
return 0; return 0;
} }
static int myreq_handler(xce_request_t* request) static int myreq_handler(xce_request_t* request, void* args)
{ {
int rc = 0; int rc = 0;

View file

@ -9,7 +9,10 @@
#include "xce/types.h" #include "xce/types.h"
#include "xceapi.h" #include "xceapi.h"
typedef int (*xce_request_handler_t)(xce_request_t*); #if 0
typedef int (*xce_request_handler_t)(xce_request_t*, void*);
#endif
typedef void* xcecli_ctx_t; typedef void* xcecli_ctx_t;
#define N_MAX_MASTERS (3) #define N_MAX_MASTERS (3)
@ -26,7 +29,7 @@ typedef struct {
extern int xcecli_ctx_init(xcecli_ctx_t*, xce_client_config_t*); extern int xcecli_ctx_init(xcecli_ctx_t*, xce_client_config_t*);
extern void xcecli_ctx_fini(xcecli_ctx_t); extern void xcecli_ctx_fini(xcecli_ctx_t);
extern int xcecli_set_handler(xcecli_ctx_t, xce_request_handler_t); extern int xcecli_set_handler(xcecli_ctx_t, xce_request_handler_t, void*);
extern int xcecli_start(xcecli_ctx_t); extern int xcecli_start(xcecli_ctx_t);
extern int xcecli_stop(xcecli_ctx_t); extern int xcecli_stop(xcecli_ctx_t);
extern int xcecli_wakeup(xcecli_ctx_t, int); extern int xcecli_wakeup(xcecli_ctx_t, int);

View file

@ -15,7 +15,7 @@ typedef enum {
XCE_CTX_ERR XCE_CTX_ERR
} xce_ctx_state_t; } xce_ctx_state_t;
typedef int (*xce_request_handler_t)(xce_request_t*); typedef int (*xce_request_handler_t)(xce_request_t*, void*);
typedef struct { typedef struct {
xce_ctx_state_t state; xce_ctx_state_t state;
@ -24,6 +24,7 @@ typedef struct {
vtaskmgr_t vtaskmgr; vtaskmgr_t vtaskmgr;
endpoint_t* ep; endpoint_t* ep;
xce_request_handler_t handler; xce_request_handler_t handler;
void* handler_args;
cbuf_t wbuf; cbuf_t wbuf;
dataset_t* dataset; dataset_t* dataset;
@ -39,7 +40,7 @@ typedef struct {
extern int xce_ctx_init(xce_ctx_t*, char*, char*); extern int xce_ctx_init(xce_ctx_t*, char*, char*);
extern void xce_ctx_fini(xce_ctx_t*); extern void xce_ctx_fini(xce_ctx_t*);
extern int xce_ctx_set_handler(xce_ctx_t*, xce_request_handler_t); extern int xce_ctx_set_handler(xce_ctx_t*, xce_request_handler_t, void*);
extern int xce_ctx_start(xce_ctx_t*); extern int xce_ctx_start(xce_ctx_t*);
extern int xce_ctx_stop(xce_ctx_t*); extern int xce_ctx_stop(xce_ctx_t*);
extern int xce_wakeup(xce_ctx_t*, int); extern int xce_wakeup(xce_ctx_t*, int);

View file

@ -76,7 +76,8 @@ void xcecli_ctx_fini(xcecli_ctx_t c)
free(ctx); free(ctx);
} }
int xcecli_set_handler(xcecli_ctx_t c, xce_request_handler_t handler) int xcecli_set_handler(xcecli_ctx_t c, xce_request_handler_t handler,
void* args)
{ {
api_ctx_t* ctx = (api_ctx_t*)c; api_ctx_t* ctx = (api_ctx_t*)c;
@ -84,7 +85,7 @@ int xcecli_set_handler(xcecli_ctx_t c, xce_request_handler_t handler)
return -EINVAL; return -EINVAL;
} }
return xce_ctx_set_handler(&(ctx->ctx), handler); return xce_ctx_set_handler(&(ctx->ctx), handler, args);
} }
int xcecli_wakeup(xcecli_ctx_t c, int status) int xcecli_wakeup(xcecli_ctx_t c, int status)

View file

@ -107,7 +107,7 @@ static int vtask_entry(vtask_t* task)
} }
if (callnext) { if (callnext) {
rc = (ctx->handler)(&request); rc = (ctx->handler)(&request, ctx->handler_args);
} }
} }
@ -198,13 +198,16 @@ void xce_ctx_fini(xce_ctx_t* ctx)
return; return;
} }
int xce_ctx_set_handler(xce_ctx_t* ctx, xce_request_handler_t handler) int xce_ctx_set_handler(xce_ctx_t* ctx, xce_request_handler_t handler,
void* args)
{ {
if ((ctx == NULL) || (handler == NULL)) { if ((ctx == NULL) || (handler == NULL)) {
return -EINVAL; return -EINVAL;
} }
ctx->handler = handler; ctx->handler = handler;
ctx->handler_args = args;
return 0; return 0;
} }

View file

@ -1,3 +1,4 @@
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -6,5 +7,97 @@
#include "xce/types.h" #include "xce/types.h"
#include "scheduler.h" #include "scheduler.h"
#include "utils.h"
int scheduler_req_handler(xce_request_t* request) { return 0; } static int sched_handle_config(xce_conf4sched_t* config)
{
TRACE("%s...", __func__);
TRACE("my cluster name %s", config->clustername);
return 0;
}
static int sched_handle_jobpush(xce_jobs_data_t* jobs)
{
TRACE("%s...", __func__);
return 0;
}
static int sched_handle_workerpush(xce_workers_data_t* workers)
{
TRACE("%s...", __func__);
return 0;
}
static int sched_on_jobs(int njobs, xce_job_t **jobs) {
return 0;
}
static int sched_on_workers(int nworkers, xce_worker_t **workers) {
return 0;
}
static int sched_handle_dataset(dataset_t* dataset)
{
int rc = 0;
TRACE("%s table = %d", __func__, dataset->table);
switch (dataset->table) {
case Data_data_schedjobs:
rc = sched_on_jobs(dataset->nrows, (xce_job_t **)dataset->rows);
break;
case Data_data_schedworkers:
rc = sched_on_workers(dataset->nrows, (xce_worker_t **)dataset->rows);
break;
case Data_data_resources:
break;
case Data_data_resvals:
break;
}
return rc;
}
int scheduler_req_handler(xce_request_t* request)
{
int rc = 0;
switch (request->opcode) {
case OPCode_push:
switch (request->body_which) {
case Request_body_configpushmsg:
/* configuration arriving */
rc = sched_handle_config(
request->body.configpushmsg->data.conf4sched);
break;
case Request_body_pushmsg:
/* data arriving */
switch (request->body.pushmsg->table) {
case Data_data_schedjobs:
/* job arriving */
rc = sched_handle_jobpush(request->body.pushmsg->data.jobs);
break;
case Data_data_schedworkers:
/* workers arriving */
rc = sched_handle_workerpush(
request->body.pushmsg->data.workers);
break;
}
break;
case Request_body_datamsg:
rc = sched_handle_dataset(request->body.datamsg);
break;
default:
rc = -EINVAL;
break;
}
break;
default:
rc = -EINVAL;
break;
}
return rc;
}

View file

@ -11,7 +11,7 @@
#include "utils.h" #include "utils.h"
extern int scheduler_req_handler(xce_request_t*); extern int scheduler_req_handler(xce_request_t*, void*);
static char doc[] = "xce_scheduler"; static char doc[] = "xce_scheduler";
static char args_doc[] = ""; static char args_doc[] = "";
@ -70,7 +70,7 @@ static int scheduler_init(xcecli_ctx_t* ctx, xce_client_config_t* config)
return rc; return rc;
} }
xcecli_set_handler(*ctx, scheduler_req_handler); xcecli_set_handler(*ctx, scheduler_req_handler, NULL);
rc = xcecli_start(*ctx); rc = xcecli_start(*ctx);
if (rc < 0) { if (rc < 0) {
return rc; return rc;

View file

@ -29,7 +29,7 @@ xcecli_ctx_t xcecli_ctx = { 0 };
xce_client_config_t xce_client_cfg = { 0 }; xce_client_config_t xce_client_cfg = { 0 };
controller_connector_ctx_t* cc_ctx = NULL; controller_connector_ctx_t* cc_ctx = NULL;
static int myreq_handler(xce_request_t*); static int myreq_handler(xce_request_t*, void*);
/* make compiler happy */ /* make compiler happy */
typedef struct { typedef struct {
@ -240,7 +240,7 @@ int xcecd_join_cluster(hostattrs_t* attrs)
int role = HostRole_worker; int role = HostRole_worker;
xcecli_ctx_init(&xcecli_ctx, &xce_client_cfg); xcecli_ctx_init(&xcecli_ctx, &xce_client_cfg);
xcecli_set_handler(xcecli_ctx, &myreq_handler); xcecli_set_handler(xcecli_ctx, &myreq_handler, NULL);
xcecli_start(xcecli_ctx); xcecli_start(xcecli_ctx);
rc = xcecli_join(xcecli_ctx, role, attrs); rc = xcecli_join(xcecli_ctx, role, attrs);
@ -1218,7 +1218,7 @@ static int handle_jobcontrol(xce_request_t* request)
* @param ctx Pointer to the I/O context structure. * @param ctx Pointer to the I/O context structure.
* @return int 0 on success, -1 on failure. * @return int 0 on success, -1 on failure.
*/ */
static int myreq_handler(xce_request_t* request) static int myreq_handler(xce_request_t* request, void* args)
{ {
int ret = 0; int ret = 0;