-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.go
More file actions
208 lines (173 loc) · 4.91 KB
/
Copy pathqueue.go
File metadata and controls
208 lines (173 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"context"
"log"
"sort"
"sync"
"time"
"github.com/google/uuid"
)
// Job states
const (
JobStateRunning = "RUNNING"
JobStateSucceeded = "SUCCEEDED"
JobStateFailed = "FAILED"
)
// Job represents a batch inference job.
type Job struct {
ID string
PriorityName string
State string
InferenceReq InferenceRequest
Results []map[string]string
Message string
EnqueuedAt time.Time
StartedAt time.Time
CompletedAt time.Time
}
// JobQueue manages job submission, execution, and persistence.
// Inference requests are sent directly to vLLM via Ray Serve —
// Ray handles load balancing and vLLM handles continuous batching.
type JobQueue struct {
mu sync.Mutex
ray *RayClient
store *JobStore // nil if Redis unavailable
jobs map[string]*Job // only active (running) jobs
ctx context.Context
}
func NewJobQueue(ray *RayClient, store *JobStore) *JobQueue {
return &JobQueue{
ray: ray,
store: store,
jobs: make(map[string]*Job),
}
}
// SetContext sets the context for Redis operations.
func (q *JobQueue) SetContext(ctx context.Context) {
q.ctx = ctx
}
// Submit creates a job and fires it to vLLM immediately.
// Ray Serve distributes requests across vLLM replicas.
func (q *JobQueue) Submit(req InferenceRequest, priority string) *Job {
job := &Job{
ID: uuid.New().String(),
PriorityName: priority,
State: JobStateRunning,
InferenceReq: req,
EnqueuedAt: time.Now(),
StartedAt: time.Now(),
}
q.mu.Lock()
q.jobs[job.ID] = job
q.mu.Unlock()
q.persistJob(job)
jobsSubmitted.Inc()
jobsActive.Inc()
jobsSubmittedByPriority.WithLabelValues(priority).Inc()
batchSize.Observe(float64(len(req.Prompts)))
log.Printf("submitted job %s (model=%s, prompts=%d, priority=%s)",
job.ID, req.Model, len(req.Prompts), priority)
go q.executeJob(job)
return job
}
// GetJob returns the job state. Checks in-memory for active jobs, Redis for completed jobs.
func (q *JobQueue) GetJob(id string) *Job {
q.mu.Lock()
job := q.jobs[id]
q.mu.Unlock()
if job != nil {
return job
}
if q.store != nil {
return q.store.Load(q.ctx, id)
}
return nil
}
// ListJobs returns recent jobs, prioritizing Redis data for completed jobs.
func (q *JobQueue) ListJobs(limit int) []*Job {
var result []*Job
seen := make(map[string]bool)
if q.store != nil {
redisJobs := q.store.ListRecent(q.ctx, limit)
for _, job := range redisJobs {
result = append(result, job)
seen[job.ID] = true
}
}
q.mu.Lock()
for _, job := range q.jobs {
if !seen[job.ID] {
result = append(result, job)
seen[job.ID] = true
}
}
q.mu.Unlock()
sort.Slice(result, func(i, j int) bool {
return result[i].EnqueuedAt.After(result[j].EnqueuedAt)
})
if len(result) > limit {
result = result[:limit]
}
return result
}
// QueueInfo returns current active job count.
func (q *JobQueue) QueueInfo() (active int) {
q.mu.Lock()
defer q.mu.Unlock()
return len(q.jobs)
}
// executeJob sends the inference request to vLLM and updates the job state.
func (q *JobQueue) executeJob(job *Job) {
start := time.Now()
resp, err := q.ray.Complete(q.ctx, CompletionRequest{
Model: job.InferenceReq.Model,
Prompt: job.InferenceReq.Prompts,
MaxTokens: job.InferenceReq.MaxTokens,
})
q.mu.Lock()
defer q.mu.Unlock()
duration := time.Since(start).Seconds()
job.CompletedAt = time.Now()
model := job.InferenceReq.Model
inferenceDuration.WithLabelValues(model).Observe(duration)
if err != nil {
job.State = JobStateFailed
job.Message = err.Error()
jobsByStatus.WithLabelValues("FAILED").Inc()
log.Printf("job %s FAILED after %.1fs: %v", job.ID, duration, err)
} else {
job.State = JobStateSucceeded
job.Message = "completed"
results := make([]map[string]string, len(job.InferenceReq.Prompts))
for _, choice := range resp.Choices {
if choice.Index < 0 || choice.Index >= len(results) {
log.Printf("job %s: vLLM returned out-of-range choice index %d (prompts=%d), skipping", job.ID, choice.Index, len(results))
continue
}
prompt := job.InferenceReq.Prompts[choice.Index]
results[choice.Index] = map[string]string{
"prompt": prompt,
"output": choice.Text,
}
}
job.Results = results
tokensTotal.WithLabelValues("prompt", model).Add(float64(resp.Usage.PromptTokens))
tokensTotal.WithLabelValues("completion", model).Add(float64(resp.Usage.CompletionTokens))
tokensPerRequest.Observe(float64(resp.Usage.TotalTokens))
jobsByStatus.WithLabelValues("SUCCEEDED").Inc()
log.Printf("job %s SUCCEEDED in %.1fs (%d prompts, %d tokens)", job.ID, duration, len(resp.Choices), resp.Usage.TotalTokens)
}
q.persistJob(job)
if job.State == JobStateSucceeded || job.State == JobStateFailed {
delete(q.jobs, job.ID)
}
jobDuration.Observe(duration)
jobsActive.Dec()
}
// persistJob saves job state to Redis (non-blocking, best-effort).
func (q *JobQueue) persistJob(job *Job) {
if q.store == nil {
return
}
q.store.Save(q.ctx, job)
}