forked from quantanotes/starfleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
396 lines (334 loc) · 8.4 KB
/
worker.go
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/rs/zerolog/log"
)
const (
workerDefaultHeartbeat = 1
workerDefaultTimeout = 20
workerDefaultMaxRetries = 10
)
type WorkerConfig struct {
Host string `json:"host"`
Capacity int `json:"capacity"`
Heartbeat int `json:"heartbeat,omitempty"`
Timeout int `json:"timeout,omitempty"`
CheckAlive bool `json:"checkAlive,omitempty"`
MaxRetries int `json:"maxRetries,omitempty"`
Restart bool `json:"restart,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
GenerateEndpoint string `json:"generateEndpoint,omitempty"`
OpenAI bool `json:"openai,omitempty"`
}
func (c *WorkerConfig) defaults() {
if c.Heartbeat <= 0 {
c.Heartbeat = workerDefaultHeartbeat
}
if c.Timeout <= 0 {
c.Timeout = workerDefaultTimeout
}
if c.GenerateEndpoint == "" {
c.GenerateEndpoint = "/generate"
}
if c.Headers == nil {
c.Headers = make(map[string]string)
}
}
type WorkerStats struct {
Host string
Alive bool
Capacity int
Queued int
Released int
Running int
Requests int
Finished int
Successes int
Fails int
AvgRequestTime int
}
type Worker struct {
Alive bool
Jobs chan *Job
host string
queue Queue
capacity int
running int32
requests int32
finished int32
fails int32
successes int32
failCount int32
maxRetries int
restart bool
avgReqTime int64
totalReqTime int64
heartbeat time.Duration
isHeartbeating bool
hbMu sync.Mutex
timeout time.Duration
checkAlive bool
headers map[string]string
generateEndpoint string
openai bool
config WorkerConfig
}
func NewWorker(config WorkerConfig) *Worker {
config.defaults()
return &Worker{
Alive: true,
Jobs: make(chan *Job, config.Capacity*2),
host: config.Host,
queue: NewQueue(config.Capacity),
capacity: config.Capacity,
running: 0,
requests: 0,
finished: 0,
fails: 0,
successes: 0,
failCount: 0,
restart: config.Restart,
maxRetries: config.MaxRetries,
avgReqTime: 0,
totalReqTime: 0,
heartbeat: time.Duration(config.Heartbeat) * time.Second,
isHeartbeating: false,
hbMu: sync.Mutex{},
timeout: time.Duration(config.Timeout) * time.Second,
checkAlive: config.CheckAlive,
headers: config.Headers,
generateEndpoint: config.GenerateEndpoint,
openai: config.OpenAI,
config: config,
}
}
func (w *Worker) Work() {
if w.checkAlive {
go w.doHearbeat()
}
for job := range w.Jobs {
if !w.Alive {
job.Err <- fmt.Errorf("LLM became unresponsive")
job.Finish()
continue
}
go w.generate(job)
}
}
func (w *Worker) Revive() {
if w.config.CheckAlive {
w.hbMu.Lock()
defer w.hbMu.Unlock()
w.checkAlive = true
w.Alive = true
go w.doHearbeat()
} else {
w.Alive = true
}
}
func (w *Worker) Load() float64 {
return float64(len(w.Jobs)+w.queue.Stats().Size+int(w.running)) / float64(w.capacity)
}
func (w *Worker) Stats() WorkerStats {
return WorkerStats{
Host: w.host,
Capacity: w.capacity,
Alive: w.Alive,
Queued: w.queue.Stats().Size,
Released: w.queue.Stats().Released,
Running: int(w.running),
Requests: int(w.requests),
Finished: int(w.finished),
Successes: int(w.successes),
Fails: int(w.fails),
AvgRequestTime: int(w.avgReqTime),
}
}
func (w *Worker) doHearbeat() {
w.hbMu.Lock()
w.isHeartbeating = true
w.hbMu.Unlock()
defer func() {
w.hbMu.Lock()
w.isHeartbeating = false
w.hbMu.Unlock()
}()
for range time.Tick(w.heartbeat) {
w.hbMu.Lock()
if !w.checkAlive {
w.hbMu.Unlock()
return
}
alive := w.ping()
if w.Alive && !alive {
log.Error().Str("host", w.host).Msg("Worker has died")
}
if !w.Alive && alive {
log.Error().Str("host", w.host).Msg("Worker has been revived")
}
w.Alive = alive
w.hbMu.Unlock()
}
}
func (w *Worker) ping() bool {
client := http.Client{Timeout: 5 * time.Second}
resp, err := client.Get(w.host)
if err != nil {
return false
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK
}
func (w *Worker) generate(job *Job) {
atomic.AddInt32(&w.requests, 1)
w.queue.Wait(job.Ctx, job.Id)
atomic.AddInt32(&w.running, 1)
failed := false
early := false
reqTime := time.Now().UnixMilli()
defer func() {
log.Info().Str("request id", job.Id).Str("worker host", w.host).Msg("Finishing generate request with worker")
job.Finish()
atomic.AddInt32(&w.running, -1)
atomic.AddInt32(&w.finished, 1)
if failed {
w.countFail()
} else if !early {
w.countSuccess()
}
if atomic.LoadInt32(&w.failCount) >= int32(w.maxRetries) {
log.Warn().Str("worker host", w.host).Msgf("Worker has failed after %v retries", atomic.LoadInt32(&w.failCount))
w.hbMu.Lock()
w.checkAlive = false
w.Alive = false
w.hbMu.Unlock()
if w.restart {
go w.doRestart()
}
}
if !w.Alive {
atomic.StoreInt32(&w.failCount, 0)
}
w.calcAvgReqTime(reqTime)
}()
select {
case <-job.ReqCtx.Done():
early = true
return
default:
}
res, err := w.prompt(job.Ctx, job.Payload)
if err != nil {
log.Error().Err(err).Str("request id", job.Id).Str("worker host", w.host).Msg("Error prompting LLM")
//lint:ignore ST1005 frontend error
job.Err <- fmt.Errorf("Error prompting LLM")
failed = true
return
}
defer res.Body.Close()
log.Info().Str("request id", job.Id).Str("worker host", w.host).Msg("Initiated generate request with worker")
for {
data := make([]byte, 1024)
size, err := res.Body.Read(data)
if err == io.EOF {
return
} else if err != nil {
log.Error().Err(err).Str("request id", job.Id).Str("worker host", w.host).Msg("Error reading tokens from LLM")
//lint:ignore ST1005 frontend error
job.Err <- fmt.Errorf("Error reading tokens from LLM")
failed = true
return
}
token := string(data[:size])
if w.openai {
if token, err = w.openaiFilter(token); err == io.EOF {
return
} else if err != nil {
log.Error().Err(err).Str("request id", job.Id).Str("worker host", w.host).Msg("Error reading tokens from LLM")
//lint:ignore ST1005 frontend error
job.Err <- fmt.Errorf("Error reading tokens from LLM")
failed = true
return
}
}
select {
case job.Output <- token:
if token == "" {
return
}
case <-job.ReqCtx.Done():
early = true
return
case <-time.After(w.timeout):
job.Err <- fmt.Errorf("LLM timed out after %v", w.timeout)
failed = true
return
}
}
}
func (w *Worker) prompt(ctx context.Context, payload []byte) (*http.Response, error) {
path, err := url.JoinPath(w.host, w.generateEndpoint)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", path, bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Accept", "application/json")
req.Header.Set("Connection", "keep-alive")
for h, v := range w.headers {
req.Header.Set(h, v)
}
client := http.Client{}
return client.Do(req.WithContext(ctx))
}
func (w *Worker) doRestart() {
}
func (w *Worker) countSuccess() {
atomic.AddInt32(&w.successes, 1)
atomic.StoreInt32(&w.failCount, 0)
}
func (w *Worker) countFail() {
atomic.AddInt32(&w.fails, 1)
atomic.AddInt32(&w.failCount, 1)
}
func (w *Worker) calcAvgReqTime(reqTime int64) {
reqTime = time.Now().UnixMilli() - reqTime
atomic.AddInt64(&w.totalReqTime, reqTime)
totalReqTime := atomic.LoadInt64(&w.totalReqTime)
numRequests := atomic.LoadInt32(&w.finished)
w.avgReqTime = totalReqTime / int64(numRequests)
}
type openaiResponse struct {
Choices []struct {
Delta struct {
Content string `json:"content"`
} `json:"delta"`
} `json:"choices"`
}
func (w *Worker) openaiFilter(data string) (string, error) {
data = strings.TrimPrefix(data, "data: ")
if string(data) == "[DONE]" {
return "", io.EOF
}
var jsn openaiResponse
if err := json.Unmarshal([]byte(data), &jsn); err != nil {
return "", err
}
if len(jsn.Choices) > 0 {
return jsn.Choices[0].Delta.Content, nil
}
return "", nil
}