forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.go
More file actions
684 lines (556 loc) · 17.1 KB
/
server.go
File metadata and controls
684 lines (556 loc) · 17.1 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package rapidcore
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"net/http"
"sync"
"time"
"go.amzn.com/lambda/core/directinvoke"
"go.amzn.com/lambda/core/statejson"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/metering"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
const (
autoresetReasonTimeout = "Timeout"
autoresetReasonReserveFail = "ReserveFail"
autoresetReasonReleaseFail = "ReleaseFail"
standaloneVersionID = "1"
resetDefaultTimeoutMs = 2000
)
type rapidPhase int
const (
phaseIdle rapidPhase = iota
phaseInitializing
phaseInvoking
)
type runtimeState int
const (
runtimeNotStarted = iota
runtimeInitStarted
runtimeInitError
runtimeInitComplete
runtimeInitFailed
runtimeInvokeResponseSent
runtimeInvokeError
runtimeReady
runtimeInvokeComplete
)
type DoneWithState struct {
*interop.Done
State statejson.InternalStateDescription
}
func (s *DoneWithState) String() string {
return fmt.Sprintf("%v %v", *s.Done, string(s.State.AsJSON()))
}
type InvokeContext struct {
Token interop.Token
ReplySent bool
ReplyStream http.ResponseWriter
Direct bool
}
type Server struct {
InternalStateGetter interop.InternalStateGetter
invokeChanOut chan *interop.Invoke
startChanOut chan *interop.Start
resetChanOut chan *interop.Reset
shutdownChanOut chan *interop.Shutdown
errorChanOut chan error
sendRunningChan chan *interop.Running
sendResponseChan chan struct{}
doneChan chan *interop.Done
InitDoneChan chan DoneWithState
InvokeDoneChan chan DoneWithState
ResetDoneChan chan *interop.Done
ShutdownDoneChan chan *interop.Done
mutex sync.Mutex
invokeCtx *InvokeContext
invokeTimeout time.Duration
reservationContext context.Context
reservationCancel func()
rapidPhase rapidPhase
runtimeState runtimeState
}
func (s *Server) StartAcceptingDirectInvokes() error {
return nil
}
func (s *Server) setRapidPhase(phase rapidPhase) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.rapidPhase = phase
}
func (s *Server) getRapidPhase() rapidPhase {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.rapidPhase
}
func (s *Server) setRuntimeState(state runtimeState) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.runtimeState = state
}
func (s *Server) getRuntimeState() runtimeState {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.runtimeState
}
func (s *Server) SetInvokeTimeout(timeout time.Duration) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.invokeTimeout = timeout
}
func (s *Server) GetInvokeTimeout() time.Duration {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.invokeTimeout
}
func (s *Server) GetInvokeContext() *InvokeContext {
s.mutex.Lock()
defer s.mutex.Unlock()
ctx := *s.invokeCtx
return &ctx
}
func (s *Server) setNewInvokeContext(invokeID string, traceID, lambdaSegmentID string) (*ReserveResponse, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.invokeCtx != nil {
return nil, ErrAlreadyReserved
}
s.invokeCtx = &InvokeContext{
Token: interop.Token{
ReservationToken: uuid.New().String(),
InvokeID: invokeID,
VersionID: standaloneVersionID,
FunctionTimeout: s.invokeTimeout,
TraceID: traceID,
LambdaSegmentID: lambdaSegmentID,
InvackDeadlineNs: math.MaxInt64, // no INVACK in standalone
},
}
resp := &ReserveResponse{
Token: s.invokeCtx.Token,
}
s.reservationContext, s.reservationCancel = context.WithCancel(context.Background())
return resp, nil
}
// Reserve allocates invoke context
func (s *Server) Reserve(id string, traceID, lambdaSegmentID string) (*ReserveResponse, error) {
invokeID := uuid.New().String()
if len(id) > 0 {
invokeID = id
}
resp, err := s.setNewInvokeContext(invokeID, traceID, lambdaSegmentID)
if err != nil {
return nil, err
}
resp.InternalState, err = s.waitInit()
return resp, err
}
func (s *Server) waitInit() (*statejson.InternalStateDescription, error) {
for {
select {
case doneWithState, chanOpen := <-s.InitDoneChan:
if !chanOpen {
// init only happens once
return nil, ErrInitAlreadyDone
}
close(s.InitDoneChan) // this was first call to reserve
if s.getRuntimeState() == runtimeInitFailed {
return &doneWithState.State, ErrInitError
}
if len(doneWithState.ErrorType) > 0 {
log.Errorf("INIT DONE failed: %s", doneWithState.ErrorType)
return &doneWithState.State, ErrInitDoneFailed
}
return &doneWithState.State, nil
case <-s.reservationContext.Done():
return nil, ErrReserveReservationDone
}
}
}
func (s *Server) setReplyStream(w http.ResponseWriter, direct bool) (string, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.invokeCtx == nil {
return "", ErrNotReserved
}
if s.invokeCtx.ReplySent {
return "", ErrAlreadyReplied
}
if s.invokeCtx.ReplyStream != nil {
return "", ErrAlreadyInvocating
}
s.invokeCtx.ReplyStream = w
s.invokeCtx.Direct = direct
return s.invokeCtx.Token.InvokeID, nil
}
// Release closes the invocation, making server ready for reserve again
func (s *Server) Release() error {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.invokeCtx == nil {
return ErrNotReserved
}
if s.reservationCancel != nil {
s.reservationCancel()
}
s.invokeCtx = nil
return nil
}
// GetCurrentInvokeID
func (s *Server) GetCurrentInvokeID() string {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.invokeCtx == nil {
return ""
}
return s.invokeCtx.Token.InvokeID
}
// SetInternalStateGetter is used to set callback which returnes internal state for /test/internalState request
func (s *Server) SetInternalStateGetter(cb interop.InternalStateGetter) {
s.InternalStateGetter = cb
}
// StartChan returns Start emitter
func (s *Server) StartChan() <-chan *interop.Start {
return s.startChanOut
}
// InvokeChan returns Invoke emitter
func (s *Server) InvokeChan() <-chan *interop.Invoke {
return s.invokeChanOut
}
// ResetChan returns Reset emitter
func (s *Server) ResetChan() <-chan *interop.Reset {
return s.resetChanOut
}
// ShutdownChan returns Shutdown emitter
func (s *Server) ShutdownChan() <-chan *interop.Shutdown {
return s.shutdownChanOut
}
// InvalidMessageChan emits errors if there was something we could not parse
func (s *Server) TransportErrorChan() <-chan error {
return s.errorChanOut
}
func (s *Server) sendResponseUnsafe(invokeID string, contentType string, status int, payload io.Reader) error {
if s.invokeCtx == nil || invokeID != s.invokeCtx.Token.InvokeID {
return interop.ErrInvalidInvokeID
}
if s.invokeCtx.ReplySent {
return interop.ErrResponseSent
}
if s.invokeCtx.ReplyStream == nil {
return fmt.Errorf("ReplyStream not available")
}
// TODO: earlier, status was set to 500 if runtime called /invocation/error. However, the integration
// tests do not differentiate between /invocation/error and /invocation/response, but they check the error type:
// To identify user-errors, we should also allowlist custom errortypes and propagate them via headers.
// s.invokeCtx.ReplyStream.WriteHeader(status)
var reportedErr error
if s.invokeCtx.Direct {
if err := directinvoke.SendDirectInvokeResponse(map[string]string{"Content-Type": contentType}, payload, s.invokeCtx.ReplyStream); err != nil {
// TODO: Do we need to drain the reader in case of a large payload and connection reuse?
log.Errorf("Failed to write response to %s: %s", invokeID, err)
flusher, ok := s.invokeCtx.ReplyStream.(http.Flusher)
if !ok {
log.Error("Failed to flush response")
}
flusher.Flush()
reportedErr = err
}
} else {
data, err := ioutil.ReadAll(payload)
if err != nil {
return fmt.Errorf("Failed to read response on %s: %s", invokeID, err)
}
if len(data) > interop.MaxPayloadSize {
return &interop.ErrorResponseTooLarge{
ResponseSize: len(data),
MaxResponseSize: interop.MaxPayloadSize,
}
}
s.invokeCtx.ReplyStream.Header().Add("Content-Type", contentType)
if _, err := s.invokeCtx.ReplyStream.Write(data); err != nil {
return fmt.Errorf("Failed to write response to %s: %s", invokeID, err)
}
}
s.sendResponseChan <- struct{}{}
s.invokeCtx.ReplySent = true
s.invokeCtx.Direct = false
return reportedErr
}
func (s *Server) SendResponse(invokeID string, contentType string, reader io.Reader) error {
s.setRuntimeState(runtimeInvokeResponseSent)
s.mutex.Lock()
defer s.mutex.Unlock()
return s.sendResponseUnsafe(invokeID, contentType, http.StatusOK, reader)
}
func (s *Server) CommitResponse() error { return nil }
func (s *Server) SendRunning(run *interop.Running) error {
s.setRuntimeState(runtimeInitStarted)
s.sendRunningChan <- run
return nil
}
func (s *Server) SendErrorResponse(invokeID string, resp *interop.ErrorResponse) error {
switch s.getRapidPhase() {
case phaseInitializing:
s.setRuntimeState(runtimeInitError)
return nil
case phaseInvoking:
// This branch can also occur during a suppressed init error, which is reported as invoke error
s.setRuntimeState(runtimeInvokeError)
s.mutex.Lock()
defer s.mutex.Unlock()
return s.sendResponseUnsafe(invokeID, resp.ContentType, http.StatusInternalServerError, bytes.NewReader(resp.Payload))
default:
panic("received unexpected error response outside invoke or init phases")
}
}
func (s *Server) SendDone(done *interop.Done) error {
s.doneChan <- done
return nil
}
func (s *Server) SendDoneFail(doneFail *interop.DoneFail) error {
s.doneChan <- &interop.Done{
ErrorType: doneFail.ErrorType,
CorrelationID: doneFail.CorrelationID, // filipovi: correlationID is required to dispatch message into correct channel
Meta: doneFail.Meta,
}
return nil
}
func (s *Server) Reset(reason string, timeoutMs int64) (*statejson.ResetDescription, error) {
// pass reset to rapid
s.resetChanOut <- &interop.Reset{
Reason: reason,
DeadlineNs: deadlineNsFromTimeoutMs(timeoutMs),
CorrelationID: "resetCorrelationID",
}
// TODO do not block on reset, instead consume ResetDoneChan in waitForRelease handler,
// this will get us more aligned on async reset notification handling.
done := <-s.ResetDoneChan
s.Release()
if done.ErrorType != "" {
return nil, errors.New(string(done.ErrorType))
}
return &statejson.ResetDescription{ExtensionsResetMs: done.Meta.ExtensionsResetMs}, nil
}
func NewServer(ctx context.Context) *Server {
s := &Server{
startChanOut: make(chan *interop.Start),
invokeChanOut: make(chan *interop.Invoke),
errorChanOut: make(chan error),
resetChanOut: make(chan *interop.Reset),
shutdownChanOut: make(chan *interop.Shutdown),
sendRunningChan: make(chan *interop.Running),
sendResponseChan: make(chan struct{}),
doneChan: make(chan *interop.Done),
// These two channels are buffered, because they are depleted asynchronously (by reserve and waitUntilRelease) and we don't want to block in SendDone until they are called
InitDoneChan: make(chan DoneWithState, 1),
InvokeDoneChan: make(chan DoneWithState, 1),
ResetDoneChan: make(chan *interop.Done),
ShutdownDoneChan: make(chan *interop.Done),
}
go s.dispatchDone()
return s
}
func (s *Server) setInitDoneRuntimeState(done *interop.Done) {
if len(done.ErrorType) > 0 {
s.setRuntimeState(runtimeInitFailed) // donefail
} else {
s.setRuntimeState(runtimeInitComplete) // done
}
}
// Note, the dispatch loop below has potential to block, when
// channel is not drained. E.g. if test assumes sandbox init
// completion before dispatching reset, then reset will block
// until init channel is drained.
func (s *Server) dispatchDone() {
for {
done := <-s.doneChan
log.Debug("Dispatching DONE:", done.CorrelationID)
internalState := s.InternalStateGetter()
s.setRapidPhase(phaseIdle)
if done.CorrelationID == "initCorrelationID" {
s.setInitDoneRuntimeState(done)
s.InitDoneChan <- DoneWithState{Done: done, State: internalState}
} else if done.CorrelationID == "invokeCorrelationID" {
s.setRuntimeState(runtimeInvokeComplete)
s.InvokeDoneChan <- DoneWithState{Done: done, State: internalState}
} else if done.CorrelationID == "resetCorrelationID" {
s.setRuntimeState(runtimeNotStarted)
s.ResetDoneChan <- done
} else if done.CorrelationID == "shutdownCorrelationID" {
s.setRuntimeState(runtimeNotStarted)
s.ShutdownDoneChan <- done
} else {
panic("Received DONE without correlation ID")
}
}
}
func drainChannel(c chan DoneWithState) {
for {
select {
case dws := <-c:
log.Warnf("Discard DONE response: %s", dws.String())
break
default:
return
}
}
}
func (s *Server) Clear() {
// we do not drain InitDoneChannel, because Init is only done once during rapid lifetime
drainChannel(s.InvokeDoneChan)
s.Release()
}
func (s *Server) IsResponseSent() bool {
panic("unexpected call to unimplemented method in rapidcore: IsResponseSent()")
}
func (s *Server) SendRuntimeReady() error {
// only called when extensions are enabled
s.setRuntimeState(runtimeReady)
return nil
}
func deadlineNsFromTimeoutMs(timeoutMs int64) int64 {
mono := metering.Monotime()
return mono + timeoutMs*1000*1000
}
func (s *Server) Init(i *interop.Start, invokeTimeoutMs int64) {
s.SetInvokeTimeout(time.Duration(invokeTimeoutMs) * time.Millisecond)
s.startChanOut <- i
s.setRapidPhase(phaseInitializing)
<-s.sendRunningChan
log.Debug("Received RUNNING")
}
func (s *Server) FastInvoke(w http.ResponseWriter, i *interop.Invoke, direct bool) error {
invokeID, err := s.setReplyStream(w, direct)
if err != nil {
return err
}
s.setRapidPhase(phaseInvoking)
i.ID = invokeID
select {
case s.invokeChanOut <- i:
break
case <-s.sendResponseChan:
// we didn't pass invoke to rapid yet, but rapid already has written some response
// It can happend if runtime/agent crashed even before we passed invoke to it
return ErrInvokeResponseAlreadyWritten
}
select {
case <-s.sendResponseChan:
break
case <-s.reservationContext.Done():
return ErrInvokeReservationDone
}
return nil
}
func (s *Server) CurrentToken() *interop.Token {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.invokeCtx == nil {
return nil
}
tok := s.invokeCtx.Token
return &tok
}
// Invoke is used by the Runtime Interface Emulator (Rapid Local)
// https://github.com/aws/aws-lambda-runtime-interface-emulator
func (s *Server) Invoke(responseWriter http.ResponseWriter, invoke *interop.Invoke) error {
resetCtx, resetCancel := context.WithCancel(context.Background())
defer resetCancel()
timeoutChan := make(chan error)
go func() {
select {
case <-time.After(s.GetInvokeTimeout()):
timeoutChan <- ErrInvokeTimeout
s.Reset(autoresetReasonTimeout, resetDefaultTimeoutMs)
case <-resetCtx.Done():
log.Debugf("execute finished, autoreset cancelled")
}
}()
reserveResp, err := s.Reserve(invoke.ID, "", "")
if err != nil {
switch err {
case ErrInitError:
// Simulate 'Suppressed Init' scenario
s.Reset(autoresetReasonReserveFail, resetDefaultTimeoutMs)
reserveResp, err = s.Reserve("", "", "")
if err == ErrInitAlreadyDone {
break
}
return err
case ErrInitDoneFailed, ErrTerminated:
s.Reset(autoresetReasonReserveFail, resetDefaultTimeoutMs)
return err
case ErrInitAlreadyDone:
// This is a valid response (e.g. for 2nd and 3rd invokes)
// TODO: switch on ReserveResponse status instead of err,
// since these are valid values
if s.InternalStateGetter == nil {
responseWriter.Write([]byte("error: internal state callback not set"))
return ErrInternalServerError
}
default:
return err
}
}
invoke.DeadlineNs = fmt.Sprintf("%d", metering.Monotime()+reserveResp.Token.FunctionTimeout.Nanoseconds())
invokeChan := make(chan error)
go func() {
if err := s.FastInvoke(responseWriter, invoke, false); err != nil {
invokeChan <- err
}
}()
releaseChan := make(chan error)
go func() {
_, err := s.AwaitRelease()
releaseChan <- err
}()
// TODO: verify the order of channel receives. When timeouts happen, Reset()
// is called first, which also does Release() => this may signal a type
// Err<*>ReservationDone error to the non-timeout channels. This is currently
// handled by the http handler, which returns GatewayTimeout for reservation errors
// too. However, Timeouts should ideally be only represented by ErrInvokeTimeout.
select {
case err = <-invokeChan:
case err = <-timeoutChan:
case err = <-releaseChan:
if err != nil {
s.Reset(autoresetReasonReleaseFail, resetDefaultTimeoutMs)
}
}
return err
}
func (s *Server) AwaitRelease() (*statejson.InternalStateDescription, error) {
select {
case doneWithState := <-s.InvokeDoneChan:
if len(doneWithState.ErrorType) > 0 {
log.Errorf("Invoke DONE failed: %s", doneWithState.ErrorType)
return nil, ErrInvokeDoneFailed
}
s.Release()
return &doneWithState.State, nil
case <-s.reservationContext.Done():
return nil, ErrReleaseReservationDone
}
}
func (s *Server) Shutdown(shutdown *interop.Shutdown) *statejson.InternalStateDescription {
s.shutdownChanOut <- shutdown
<-s.ShutdownDoneChan
state := s.InternalStateGetter()
return &state
}
func (s *Server) InternalState() (*statejson.InternalStateDescription, error) {
if s.InternalStateGetter == nil {
return nil, errors.New("InternalStateGetterNotSet")
}
state := s.InternalStateGetter()
return &state, nil
}