submit job from xcecli

This commit is contained in:
Rongsong Shen 2026-04-14 15:55:52 +08:00
parent c1c9f9e736
commit d9b5c361c2
5 changed files with 148 additions and 281 deletions

View file

@ -9,6 +9,7 @@ package backend
#include "client.h"
#include "netio.h"
#include "helper.h"
#include "idgen.h"
#include "controller.capnp.h"
// backend_ctx_t wraps xcecli_ctx_t for use as the C handler's arg.
@ -54,6 +55,7 @@ import (
"apiserver/models"
logger "apiserver/utils"
"fmt"
"sync"
"unsafe"
"go.uber.org/zap"
@ -62,6 +64,10 @@ import (
// gCtx holds the C client context for the lifetime of the process.
var gCtx C.backend_ctx_t
// gIDCtx is the C idgen context used to generate job IDs.
var gIDCtx C.idgen_ctx_t
var idCtxOnce sync.Once
// gHandler receives push notifications from the backend.
// Must be set via StartNotificationListener before RegisterToBackend.
var gHandler NotificationHandler
@ -113,6 +119,30 @@ func StartNotificationListener(handler NotificationHandler) error {
return nil
}
// initIDGen initialises the C idgen context. Called once via sync.Once.
func initIDGen() {
C.idgen_ctx_init(&gIDCtx)
}
// GenerateJobID calls the C idgen() to produce a unique job ID.
// clusterID and apiserverID identify the originating instance;
// arraySize=1 for normal jobs, >1 for array jobs.
func GenerateJobID(clusterID, apiserverID int, arraySize int) (uint64, error) {
idCtxOnce.Do(initIDGen)
var jobID C.uint64_t
rc := C.idgen(&gIDCtx, C.size_t(clusterID), C.size_t(apiserverID),
C.size_t(arraySize), &jobID)
if rc < 0 {
return 0, fmt.Errorf("idgen failed: %d", int(rc))
}
return uint64(jobID), nil
}
// Cleanup releases C resources. Call at process shutdown.
func Cleanup() {
C.idgen_ctx_fini(&gIDCtx)
}
// go_push_handler is called by the C library on every incoming request.
// It must be exported so the C trampoline can call it from a C thread.
//
@ -204,12 +234,15 @@ func handleConfigPush(cfgdata *C.xce_configdata_t) C.int {
}
// SubmitJob sends a job submission to the backend via xcecli_jobsubmit.
// arraySize=1 for normal jobs, >1 for array jobs.
// The store is updated only when the backend pushes the resulting OnJobCreated
// notification — not immediately here.
func SubmitJob(job *models.JobInfo) error {
func SubmitJob(job *models.JobInfo, arraySize int) error {
js, sd, si := modelJobToSubmit(job)
defer freeJobSubmit(js, sd, si)
js.array_size = C.uint64_t(arraySize)
var outJobID C.uint64_t
rc := C.xcecli_jobsubmit(gCtx.ctx, js, &outJobID)
if rc < 0 {
@ -222,28 +255,6 @@ func SubmitJob(job *models.JobInfo) error {
return nil
}
// SubmitArrayJobs submits an array job via xcecli_jobsubmit with array_size > 1.
func SubmitArrayJobs(arrayjob *models.ArrayJobInfo, jobs []models.JobInfo) error {
if len(jobs) == 0 {
return fmt.Errorf("no jobs in array")
}
// Use the first job as the template; set array_size to the total count.
js, sd, si := modelJobToSubmit(&jobs[0])
defer freeJobSubmit(js, sd, si)
js.array_size = C.uint64_t(len(jobs))
var outJobID C.uint64_t
rc := C.xcecli_jobsubmit(gCtx.ctx, js, &outJobID)
if rc < 0 {
return fmt.Errorf("xcecli_jobsubmit (array) failed: %d", int(rc))
}
if outJobID != 0 {
arrayjob.JobID = int64(outJobID)
}
return nil
}
// ControlJob sends a job control action to the backend via xcecli_jobctrl.
// action must be one of the models.JOB_* constants; content is an optional
// signal name (e.g. "TERM") for kill operations.

View file

@ -15,11 +15,13 @@ func RegisterToBackend(config *BackendConfig) error { return errNoCGO }
// StartNotificationListener stub — real implementation in clib.go (requires CGO).
func StartNotificationListener(handler NotificationHandler) error { return errNoCGO }
// SubmitJob stub — real implementation in clib.go (requires CGO).
func SubmitJob(job *models.JobInfo) error { return errNoCGO }
// GenerateJobID stub — real implementation in clib.go (requires CGO).
func GenerateJobID(clusterID, apiserverID int, arraySize int) (uint64, error) {
return 0, errNoCGO
}
// SubmitArrayJobs stub — real implementation in clib.go (requires CGO).
func SubmitArrayJobs(arrayjob *models.ArrayJobInfo, jobs []models.JobInfo) error { return errNoCGO }
// SubmitJob stub — real implementation in clib.go (requires CGO).
func SubmitJob(job *models.JobInfo, arraySize int) error { return errNoCGO }
// ControlJob stub — real implementation in clib.go (requires CGO).
func ControlJob(jobid uint64, action int, content string) error { return errNoCGO }