api for agent worker job count (#3068)

* api for agent worker job count

* cleanup

* temp deps

* temp deps

* deps
This commit is contained in:
Paul Wells
2024-10-05 05:13:52 -07:00
committed by GitHub
parent 2491ee7c7c
commit 3261560098
6 changed files with 31 additions and 30 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ require (
github.com/jxskiss/base62 v1.1.0
github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1
github.com/livekit/mediatransportutil v0.0.0-20240730083616-559fa5ece598
github.com/livekit/protocol v1.23.1-0.20241005050546-ec3b682c05d8
github.com/livekit/protocol v1.23.1-0.20241005120643-603b677f7905
github.com/livekit/psrpc v0.6.1-0.20240924010758-9f0a4268a3b9
github.com/mackerelio/go-osstat v0.2.5
github.com/magefile/mage v1.15.0
+2 -2
View File
@@ -165,8 +165,8 @@ github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1 h1:jm09419p0lqTkD
github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ=
github.com/livekit/mediatransportutil v0.0.0-20240730083616-559fa5ece598 h1:yLlkHk2feSLHstD9n4VKg7YEBR4rLODTI4WE8gNBEnQ=
github.com/livekit/mediatransportutil v0.0.0-20240730083616-559fa5ece598/go.mod h1:jwKUCmObuiEDH0iiuJHaGMXwRs3RjrB4G6qqgkr/5oE=
github.com/livekit/protocol v1.23.1-0.20241005050546-ec3b682c05d8 h1:7cJS9u4VfOcvTeERxwk4yRTOIkwC44LWLSELI+OxWlc=
github.com/livekit/protocol v1.23.1-0.20241005050546-ec3b682c05d8/go.mod h1:nxRzmQBKSYK64gqr7ABWwt78hvrgiO2wYuCojRYb7Gs=
github.com/livekit/protocol v1.23.1-0.20241005120643-603b677f7905 h1:lQQa2AZfwGPlksZML3G14yfUoxEKewTMmKQ7oRNxYAU=
github.com/livekit/protocol v1.23.1-0.20241005120643-603b677f7905/go.mod h1:nxRzmQBKSYK64gqr7ABWwt78hvrgiO2wYuCojRYb7Gs=
github.com/livekit/psrpc v0.6.1-0.20240924010758-9f0a4268a3b9 h1:33oBjGpVD9tYkDXQU42tnHl8eCX9G6PVUToBVuCUyOs=
github.com/livekit/psrpc v0.6.1-0.20240924010758-9f0a4268a3b9/go.mod h1:CQUBSPfYYAaevg1TNCc6/aYsa8DJH4jSRFdCeSZk5u0=
github.com/mackerelio/go-osstat v0.2.5 h1:+MqTbZUhoIt4m8qzkVoXUJg1EuifwlAJSk4Yl2GXh+o=
+1 -1
View File
@@ -384,7 +384,7 @@ func (w *AgentWorker) sendStatus() {
w.SendUpdateWorker(&livekit.UpdateWorkerStatus{
Status: &status,
Load: load,
JobCount: int32(jobCount),
JobCount: uint32(jobCount),
})
}
+20 -19
View File
@@ -48,9 +48,8 @@ type WorkerProtocolVersion int
const CurrentProtocol = 1
const (
registerTimeout = 10 * time.Second
assignJobTimeout = 10 * time.Second
pingFrequency = 10 * time.Second
RegisterTimeout = 10 * time.Second
AssignJobTimeout = 10 * time.Second
)
type SignalConn interface {
@@ -165,7 +164,7 @@ func NewWorkerRegisterer(conn SignalConn, serverInfo *livekit.ServerInfo, protoc
WorkerPingHandler: WorkerPingHandler{conn: conn},
serverInfo: serverInfo,
protocol: protocol,
deadline: time.Now().Add(registerTimeout),
deadline: time.Now().Add(RegisterTimeout),
}
}
@@ -301,6 +300,12 @@ func (w *Worker) RunningJobs() map[livekit.JobID]*livekit.Job {
return jobs
}
func (w *Worker) RunningJobCount() int {
w.mu.Lock()
defer w.mu.Unlock()
return len(w.runningJobs)
}
func (w *Worker) GetJobState(jobID livekit.JobID) (*livekit.JobState, error) {
w.mu.Lock()
defer w.mu.Unlock()
@@ -342,7 +347,7 @@ func (w *Worker) AssignJob(ctx context.Context, job *livekit.Job) (*livekit.JobS
Availability: &livekit.AvailabilityRequest{Job: job},
}})
timeout := time.NewTimer(assignJobTimeout)
timeout := time.NewTimer(AssignJobTimeout)
defer timeout.Stop()
// See handleAvailability for the response
@@ -385,10 +390,10 @@ func (w *Worker) AssignJob(ctx context.Context, job *livekit.Job) (*livekit.JobS
func (w *Worker) TerminateJob(jobID livekit.JobID, reason rpc.JobTerminateReason) (*livekit.JobState, error) {
w.mu.Lock()
job := w.runningJobs[jobID]
_, ok := w.runningJobs[jobID]
w.mu.Unlock()
if job == nil {
if !ok {
return nil, ErrJobNotFound
}
@@ -459,7 +464,6 @@ func (w *Worker) HandleAvailability(res *livekit.AvailabilityResponse) error {
func (w *Worker) HandleUpdateJob(update *livekit.UpdateJobStatus) error {
_, err := w.UpdateJobStatus(update)
if err != nil {
w.logger.Infow("received job update for unknown job", "jobID", update.JobId)
}
@@ -483,16 +487,14 @@ func (w *Worker) UpdateJobStatus(update *livekit.UpdateJobStatus) (*livekit.JobS
job.State.StartedAt = now.UnixNano()
}
if job.State.Status < livekit.JobStatus_JS_SUCCESS && JobStatusIsEnded(update.Status) {
job.State.EndedAt = now.UnixNano()
}
job.State.Status = update.Status
job.State.Error = update.Error
// TODO do not delete, leave inside the JobDefinition
if JobStatusIsEnded(job.State.Status) {
if JobStatusIsEnded(update.Status) {
job.State.EndedAt = now.UnixNano()
delete(w.runningJobs, jobID)
w.logger.Infow("job ended", "jobID", update.JobId, "status", update.Status, "error", update.Error)
}
return proto.Clone(job.State).(*livekit.JobState), nil
@@ -516,7 +518,7 @@ func (w *Worker) HandleSimulateJob(simulate *livekit.SimulateJobRequest) error {
go func() {
_, err := w.AssignJob(w.ctx, job)
if err != nil {
w.logger.Errorw("failed to simulate job, assignment failed", err, "jobId", job.Id)
w.logger.Errorw("unable to simulate job", err, "jobId", job.Id)
}
}()
@@ -524,13 +526,12 @@ func (w *Worker) HandleSimulateJob(simulate *livekit.SimulateJobRequest) error {
}
func (w *Worker) HandleUpdateWorker(update *livekit.UpdateWorkerStatus) error {
w.logger.Debugw("worker status update", "update", logger.Proto(update))
w.mu.Lock()
defer w.mu.Unlock()
if update.Status != nil {
w.status = update.GetStatus()
if status := update.Status; status != nil && w.status != *status {
w.status = *status
w.Logger().Debugw("worker status changed", "status", w.status)
}
w.load = update.GetLoad()
+3 -2
View File
@@ -38,6 +38,7 @@ import (
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/rpc"
"github.com/livekit/protocol/utils"
"github.com/livekit/psrpc"
)
@@ -341,7 +342,7 @@ func (h *AgentHandler) JobRequest(ctx context.Context, job *livekit.Job) (*rpc.J
for {
selected, err := h.selectWorkerWeightedByLoad(key, attempted)
if err != nil {
return nil, psrpc.NewError(psrpc.DeadlineExceeded, err)
return nil, psrpc.NewError(psrpc.ResourceExhausted, err)
}
attempted[selected] = struct{}{}
@@ -361,7 +362,7 @@ func (h *AgentHandler) JobRequest(ctx context.Context, job *livekit.Job) (*rpc.J
h.logger.Debugw("assigning job", values...)
state, err := selected.AssignJob(ctx, job)
if err != nil {
if errors.Is(err, agent.ErrWorkerNotAvailable) {
if utils.ErrorIsOneOf(err, agent.ErrWorkerNotAvailable, agent.ErrWorkerClosed) {
continue // Try another worker
}
return nil, err
+4 -5
View File
@@ -30,7 +30,6 @@ import (
"github.com/livekit/mediatransportutil"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/logger/zaputil"
"github.com/livekit/protocol/utils"
"github.com/livekit/livekit-server/pkg/sfu/buffer"
@@ -1654,7 +1653,7 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e
"extRefTS", extRefTS,
"extLastTS", extLastTS,
"diffSeconds", math.Abs(diffSeconds),
"refInfos", zaputil.ObjectSlice(f.refInfos[:]),
"refInfos", logger.ObjectSlice(f.refInfos[:]),
)
}
// TODO-REMOVE-AFTER-DATA-COLLECTION
@@ -1669,7 +1668,7 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e
"extRefTS", extRefTS,
"extLastTS", extLastTS,
"diffSeconds", math.Abs(diffSeconds),
"refInfos", zaputil.ObjectSlice(f.refInfos[:]),
"refInfos", logger.ObjectSlice(f.refInfos[:]),
)
}
@@ -1865,7 +1864,7 @@ func (f *Forwarder) getTranslationParamsCommon(extPkt *buffer.ExtPacket, layer i
"could not switch feed",
"error", err,
"layer", layer,
"refInfos", zaputil.ObjectSlice(f.refInfos[:]),
"refInfos", logger.ObjectSlice(f.refInfos[:]),
"currentLayer", f.vls.GetCurrent(),
"targetLayer", f.vls.GetCurrent(),
"maxLayer", f.vls.GetMax(),
@@ -1878,7 +1877,7 @@ func (f *Forwarder) getTranslationParamsCommon(extPkt *buffer.ExtPacket, layer i
"from", f.lastSSRC,
"to", extPkt.Packet.SSRC,
"layer", layer,
"refInfos", zaputil.ObjectSlice(f.refInfos[:]),
"refInfos", logger.ObjectSlice(f.refInfos[:]),
"currentLayer", f.vls.GetCurrent(),
"targetLayer", f.vls.GetCurrent(),
"maxLayer", f.vls.GetMax(),