-
Notifications
You must be signed in to change notification settings - Fork 405
/
lb_agent.go
394 lines (326 loc) · 10.3 KB
/
lb_agent.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
package agent
import (
"bytes"
"context"
"errors"
"io"
"io/ioutil"
"net/http"
"time"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
"github.com/fnproject/fn/api/common"
"github.com/fnproject/fn/api/models"
pool "github.com/fnproject/fn/api/runnerpool"
"github.com/fnproject/fn/fnext"
)
type lbAgent struct {
cfg Config
callListeners []fnext.CallListener
rp pool.RunnerPool
placer pool.Placer
callOverrider CallOverrider
shutWg *common.WaitGroup
callOpts []CallOpt
}
type DetachedResponseWriter struct {
Headers http.Header
status int
acked chan struct{}
}
func (w *DetachedResponseWriter) Header() http.Header {
return w.Headers
}
func (w *DetachedResponseWriter) Write(data []byte) (int, error) {
return len(data), nil
}
func (w *DetachedResponseWriter) WriteHeader(statusCode int) {
w.status = statusCode
w.acked <- struct{}{}
}
func (w *DetachedResponseWriter) Status() int {
return w.status
}
func NewDetachedResponseWriter(h http.Header, statusCode int) *DetachedResponseWriter {
return &DetachedResponseWriter{
Headers: h,
status: statusCode,
acked: make(chan struct{}, 1),
}
}
var _ http.ResponseWriter = new(DetachedResponseWriter) // keep the compiler happy
type LBAgentOption func(*lbAgent) error
// WithLBAgentConfig sets the agent config to the provided Config
func WithLBAgentConfig(cfg *Config) LBAgentOption {
return func(a *lbAgent) error {
a.cfg = *cfg
return nil
}
}
// WithLBCallOverrider is for LB agents to register a CallOverrider to modify a Call and extensions
func WithLBCallOverrider(fn CallOverrider) LBAgentOption {
return func(a *lbAgent) error {
if a.callOverrider != nil {
return errors.New("lb-agent call overriders already exists")
}
a.callOverrider = fn
return nil
}
}
// WithLBCallOptions adds additional call options to each call created from GetCall, these
// options will be executed after any other options supplied to GetCall
func WithLBCallOptions(opts ...CallOpt) LBAgentOption {
return func(a *lbAgent) error {
a.callOpts = append(a.callOpts, opts...)
return nil
}
}
// NewLBAgent creates an Agent that knows how to load-balance function calls
// across a group of runner nodes.
func NewLBAgent(rp pool.RunnerPool, p pool.Placer, options ...LBAgentOption) (Agent, error) {
// Yes, LBAgent and Agent both use a Config.
cfg, err := NewConfig()
if err != nil {
logrus.WithError(err).Fatalf("error in lb-agent config cfg=%+v", cfg)
}
a := &lbAgent{
cfg: *cfg,
rp: rp,
placer: p,
shutWg: common.NewWaitGroup(),
}
// Allow overriding config
for _, option := range options {
err = option(a)
if err != nil {
logrus.WithError(err).Fatalf("error in lb-agent options")
}
}
logrus.Infof("lb-agent starting cfg=%+v", a.cfg)
return a, nil
}
// implements Agent
func (a *lbAgent) AddCallListener(listener fnext.CallListener) {
a.callListeners = append(a.callListeners, listener)
}
// implements callTrigger
func (a *lbAgent) fireBeforeCall(ctx context.Context, call *models.Call) error {
return fireBeforeCallFun(a.callListeners, ctx, call)
}
// implements callTrigger
func (a *lbAgent) fireAfterCall(ctx context.Context, call *models.Call) error {
return fireAfterCallFun(a.callListeners, ctx, call)
}
// implements Agent
func (a *lbAgent) GetCall(opts ...CallOpt) (Call, error) {
var c call
// add additional agent options after any call specific options
opts = append(opts, a.callOpts...)
for _, o := range opts {
err := o(&c)
if err != nil {
return nil, err
}
}
// TODO typed errors to test
if c.req == nil || c.Call == nil {
return nil, errors.New("no model or request provided for call")
}
// If overrider is present, let's allow it to modify models.Call
// and call extensions
if a.callOverrider != nil {
ext, err := a.callOverrider(c.req, c.Call, c.extensions)
if err != nil {
return nil, err
}
c.extensions = ext
}
setupCtx(&c)
c.ct = a
c.stderr = common.NoopReadWriteCloser{}
c.slotHashId = getSlotQueueKey(&c)
return &c, nil
}
// implements Agent
func (a *lbAgent) Close() error {
// start closing the front gate first
ch := a.shutWg.CloseGroupNB()
// finally shutdown the runner pool
err := a.rp.Shutdown(context.Background())
if err != nil {
logrus.WithError(err).Warn("Runner pool shutdown error")
}
// gate-on front-gate, should be completed if delegated agent & runner pool is gone.
<-ch
return err
}
// implements Agent
func (a *lbAgent) Submit(callI Call) error {
call := callI.(*call)
ctx, span := trace.StartSpan(call.req.Context(), "agent_submit")
defer span.End()
statsCalls(ctx)
if !a.shutWg.AddSession(1) {
statsTooBusy(ctx)
return models.ErrCallTimeoutServerBusy
}
defer a.shutWg.DoneSession()
statsEnqueue(ctx)
// pre-read and buffer request body if already not done based
// on GetBody presence.
buf, err := a.setRequestBody(ctx, call)
if buf != nil {
defer bufPool.Put(buf)
}
if err != nil {
return a.handleCallEnd(ctx, call, err, false)
}
err = call.Start(ctx)
if err != nil {
return a.handleCallEnd(ctx, call, err, false)
}
statsDequeue(ctx)
statsStartRun(ctx)
if call.Type == models.TypeDetached {
return a.placeDetachCall(ctx, call)
}
return a.placeCall(ctx, call)
}
func (a *lbAgent) placeDetachCall(ctx context.Context, call *call) error {
errPlace := make(chan error, 1)
rw := call.respWriter.(*DetachedResponseWriter)
go a.spawnPlaceCall(ctx, call, errPlace)
select {
case err := <-errPlace:
return err
case <-rw.acked:
return nil
}
}
func (a *lbAgent) placeCall(ctx context.Context, call *call) error {
err := a.placer.PlaceCall(ctx, a.rp, call)
return a.handleCallEnd(ctx, call, err, true)
}
func (a *lbAgent) spawnPlaceCall(ctx context.Context, call *call, errCh chan error) {
var cancel func()
ctx = common.BackgroundContext(ctx)
cfg := a.placer.GetPlacerConfig()
// PlacerTimeout for Detached + call.Timeout (inside container) + headroom for docker-pull, gRPC network retrasmit etc.)
newCtxTimeout := cfg.DetachedPlacerTimeout + time.Duration(call.Timeout)*time.Second + a.cfg.DetachedHeadRoom
ctx, cancel = context.WithTimeout(ctx, newCtxTimeout)
defer cancel()
err := a.placer.PlaceCall(ctx, a.rp, call)
errCh <- a.handleCallEnd(ctx, call, err, true)
}
// setRequestGetBody sets GetBody function on the given http.Request if it is missing. GetBody allows
// reading from the request body without mutating the state of the request.
func (a *lbAgent) setRequestBody(ctx context.Context, call *call) (*bytes.Buffer, error) {
r := call.req
if r.Body == nil || r.GetBody != nil {
return nil, nil
}
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
// WARNING: we need to handle IO in a separate go-routine below
// to be able to detect a ctx timeout. When we timeout, we
// let gin/http-server to unblock the go-routine below.
errApp := make(chan error, 1)
go func() {
_, err := buf.ReadFrom(r.Body)
if err != nil && err != io.EOF {
errApp <- err
return
}
r.Body = ioutil.NopCloser(bytes.NewReader(buf.Bytes()))
// GetBody does not mutate the state of the request body
r.GetBody = func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(buf.Bytes())), nil
}
close(errApp)
}()
select {
case err := <-errApp:
return buf, err
case <-ctx.Done():
return buf, ctx.Err()
}
}
// implements Agent
func (a *lbAgent) Enqueue(context.Context, *models.Call) error {
logrus.Error("Enqueue not implemented")
return errors.New("Enqueue not implemented")
}
func (a *lbAgent) handleCallEnd(ctx context.Context, call *call, err error, isForwarded bool) error {
if isForwarded {
call.End(ctx, err)
statsStopRun(ctx)
if err == nil {
statsComplete(ctx)
recordCallLatency(ctx, call, completedMetricName)
} else if err == context.DeadlineExceeded {
// We are here because we were unable to service this request for the given
// reservation. In detached case, the reservation is calculated based on estimated
// total time to run a request. (See: spawnPlaceCall) Otherwise, there's no set
// deadline in the request context. This is also a bit more robust going forward
// if we start enforcing a maximum overall deadline for clients. For detached case, the
// error is unlikely to be delivered to the client since this is essentially an async
// operation.
statsTimedout(ctx)
recordCallLatency(ctx, call, timedoutMetricName)
// We have failed: http 500 Internal Server Error
return models.ErrServiceReservationFailure
}
} else {
statsDequeue(ctx)
if err == context.DeadlineExceeded {
statsTooBusy(ctx)
recordCallLatency(ctx, call, serverBusyMetricName)
return models.ErrCallTimeoutServerBusy
}
}
if err == models.ErrCallTimeoutServerBusy {
statsTooBusy(ctx)
recordCallLatency(ctx, call, serverBusyMetricName)
return models.ErrCallTimeoutServerBusy
} else if err == context.Canceled {
statsCanceled(ctx)
recordCallLatency(ctx, call, canceledMetricName)
} else if err != nil {
statsErrors(ctx)
recordCallLatency(ctx, call, errorsMetricName)
}
return err
}
func recordCallLatency(ctx context.Context, call *call, status string) {
start := time.Time(call.StartedAt)
creat := time.Time(call.CreatedAt)
// IMPORTANT: Why do we prefer 'StartedAt'? This is because we would like to
// exclude client transmission of the request body to the LB. We are trying to
// measure how long it took us to execute a user function and obtain its response.
// Notice how we cache client body *before* we call call.Start() where StartedAt
// is set. If call.Start() is not called yet, then we use call.CreatedAt.
var callLatency time.Duration
if !start.IsZero() {
callLatency = time.Now().Sub(start)
} else if !creat.IsZero() {
callLatency = time.Now().Sub(creat)
} else {
common.Logger(ctx).Error("cannot determine call start time")
return
}
// We want to exclude time spent in user-code. Today, this is container
// request processing latency as observed by runner agent.
execLatency := call.GetUserExecutionTime()
// some sanity check before. If sanity checks flags something, then
// this is likely that runners are sending malicious/suspicious data.
if execLatency != nil {
if *execLatency >= callLatency {
common.Logger(ctx).Errorf("invalid latency callLatency=%v execLatency=%v", callLatency, execLatency)
return
}
callLatency -= *execLatency
}
statsCallLatency(ctx, callLatency, status)
}
var _ Agent = &lbAgent{}
var _ callTrigger = &lbAgent{}