-
Notifications
You must be signed in to change notification settings - Fork 405
/
pure_runner.go
1203 lines (1021 loc) · 32.4 KB
/
pure_runner.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
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package agent
import (
"bytes"
"context"
"crypto/tls"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
runner "github.com/fnproject/fn/api/agent/grpc"
"github.com/fnproject/fn/api/common"
"github.com/fnproject/fn/api/id"
"github.com/fnproject/fn/api/models"
"github.com/fnproject/fn/fnext"
"github.com/fnproject/fn/grpcutil"
"github.com/golang/protobuf/ptypes/empty"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
)
/*
Pure Runner (implements Agent) proxies gRPC requests to the actual Agent instance. This is
done using http.ResponseWriter interfaces where Agent pushes the function I/O through:
1) Function output to pure runner is received through callHandle http.ResponseWriter interface.
2) Function input from pure runner to Agent is processed through callHandle io.PipeWriter.
3) LB to runner input is handled via receiver (inQueue)
4) runner to LB output is handled via sender (outQueue)
The flow of events is as follows:
LB:
1) LB sends ClientMsg_TryCall to runner
2) LB sends ClientMsg_DataFrame messages with an EOF for last message set.
3) LB receives RunnerMsg_CallResultStart for http status and headers
4) LB receives RunnerMsg_DataFrame messages for http body with an EOF for last message set.
8) LB receives RunnerMsg_CallFinished as the final message.
LB can be interrupted with RunnerMsg_CallFinished anytime. If this is a NACK, presence of 503
means LB can retry the call.
Runner:
1) Runner upon receiving ClientMsg_TryCall calls agent.Submit()
2) Runner allocates its resources but can send a NACK: RunnerMsg_Finished if it cannot service the call in time.
3) agent.Submit starts reading data from callHandle io.PipeReader, this reads
data from LB via gRPC receiver (inQueue). The http reader detects headers/data
and sends RunnerMsg_CallResultStart and/or RunnerMsg_DataFrame messages to LB.
4) agent.Submit() completes, this means, the Function I/O is now completed. Runner sends RunnerMsg_Finished
*/
var (
ErrorExpectedTry = errors.New("Protocol failure: expected ClientMsg_Try")
ErrorExpectedData = errors.New("Protocol failure: expected ClientMsg_Data")
)
// callHandle represents the state of the call as handled by the pure runner, and additionally it implements the
// interface of http.ResponseWriter so that it can be used for streaming the output back.
type callHandle struct {
engagement runner.RunnerProtocol_EngageServer
ctx context.Context
c *call // the agent's version of call
// For implementing http.ResponseWriter:
headers http.Header
status int
headerOnce sync.Once
shutOnce sync.Once
pipeToFnCloseOnce sync.Once
outQueue chan *runner.RunnerMsg
doneQueue chan struct{}
errQueue chan error
inQueue chan *runner.ClientMsg
// Pipe to push data to the agent Function container
pipeToFnW *io.PipeWriter
pipeToFnR *io.PipeReader
}
func NewCallHandle(engagement runner.RunnerProtocol_EngageServer) *callHandle {
// set up a pipe to push data to agent Function container
pipeR, pipeW := io.Pipe()
state := &callHandle{
engagement: engagement,
ctx: engagement.Context(),
headers: make(http.Header),
status: 200,
outQueue: make(chan *runner.RunnerMsg),
doneQueue: make(chan struct{}),
errQueue: make(chan error, 1), // always allow one error (buffered)
inQueue: make(chan *runner.ClientMsg),
pipeToFnW: pipeW,
pipeToFnR: pipeR,
}
// spawn one receiver and one sender go-routine.
// See: https://grpc.io/docs/reference/go/generated-code.html, which reads:
// "Thread-safety: note that client-side RPC invocations and server-side RPC handlers
// are thread-safe and are meant to be run on concurrent goroutines. But also note that
// for individual streams, incoming and outgoing data is bi-directional but serial;
// so e.g. individual streams do not support concurrent reads or concurrent writes
// (but reads are safely concurrent with writes)."
state.spawnReceiver()
state.spawnSender()
return state
}
// closePipeToFn closes the pipe that feeds data to the function in agent.
func (ch *callHandle) closePipeToFn() {
ch.pipeToFnCloseOnce.Do(func() {
ch.pipeToFnW.Close()
})
}
// finalize initiates a graceful shutdown of the session. This is
// currently achieved by a sentinel nil enqueue to gRPC sender.
func (ch *callHandle) finalize() error {
// final sentinel nil msg for graceful shutdown
err := ch.enqueueMsg(nil)
if err != nil {
ch.shutdown(err)
}
return err
}
// shutdown initiates a shutdown and terminates the gRPC session with
// a given error.
func (ch *callHandle) shutdown(err error) {
ch.closePipeToFn()
ch.shutOnce.Do(func() {
common.Logger(ch.ctx).WithError(err).Debugf("Shutting down call handle")
// try to queue an error message if it's not already queued.
if err != nil {
select {
case ch.errQueue <- err:
default:
}
}
close(ch.doneQueue)
})
}
// waitError waits until the session is completed and results
// any queued error if there is any.
func (ch *callHandle) waitError() error {
select {
case <-ch.ctx.Done():
case <-ch.doneQueue:
}
var err error
// get queued error if there's any
select {
case err = <-ch.errQueue:
default:
err = ch.ctx.Err()
}
if err != nil {
logrus.WithError(err).Debugf("Wait Error")
}
return err
}
// enqueueMsg attempts to queue a message to the gRPC sender
func (ch *callHandle) enqueueMsg(msg *runner.RunnerMsg) error {
select {
case ch.outQueue <- msg:
return nil
case <-ch.ctx.Done():
case <-ch.doneQueue:
}
return io.EOF
}
// enqueueMsgStricy enqueues a message to the gRPC sender and if
// that fails then initiates an error case shutdown.
func (ch *callHandle) enqueueMsgStrict(msg *runner.RunnerMsg) error {
err := ch.enqueueMsg(msg)
if err != nil {
ch.shutdown(err)
}
return err
}
// enqueueCallResponse enqueues a Submit() response to the LB
// and initiates a graceful shutdown of the session.
func (ch *callHandle) enqueueCallResponse(err error) {
var createdAt string
var startedAt string
var completedAt string
var details string
var errCode int
var errStr string
var errUser bool
log := common.Logger(ch.ctx)
if err != nil {
errCode = models.GetAPIErrorCode(err)
errStr = err.Error()
errUser = models.IsFuncError(err)
}
schedulerDuration, executionDuration := GetCallLatencies(ch.c)
if ch.c != nil {
mcall := ch.c.Model()
// These timestamps are related. To avoid confusion
// and for robustness, nested if stmts below.
if !time.Time(mcall.CreatedAt).IsZero() {
createdAt = mcall.CreatedAt.String()
if !time.Time(mcall.StartedAt).IsZero() {
startedAt = mcall.StartedAt.String()
if !time.Time(mcall.CompletedAt).IsZero() {
completedAt = mcall.CompletedAt.String()
} else {
// IMPORTANT: We punch this in ourselves.
completedAt = common.DateTime(time.Now()).String()
}
}
}
details = mcall.ID
}
log.Debugf("Sending Call Finish details=%v", details)
errTmp := ch.enqueueMsgStrict(&runner.RunnerMsg{
Body: &runner.RunnerMsg_Finished{Finished: &runner.CallFinished{
Success: err == nil,
Details: details,
ErrorCode: int32(errCode),
ErrorStr: errStr,
CreatedAt: createdAt,
StartedAt: startedAt,
CompletedAt: completedAt,
SchedulerDuration: int64(schedulerDuration),
ExecutionDuration: int64(executionDuration),
ErrorUser: errUser,
}}})
if errTmp != nil {
log.WithError(errTmp).Infof("enqueueCallResponse Send Error details=%v err=%v:%v", details, errCode, errStr)
return
}
errTmp = ch.finalize()
if errTmp != nil {
log.WithError(errTmp).Infof("enqueueCallResponse Finalize Error details=%v err=%v:%v", details, errCode, errStr)
}
}
// spawnPipeToFn pumps data to Function via callHandle io.PipeWriter (pipeToFnW)
// which is fed using input channel.
func (ch *callHandle) spawnPipeToFn() chan *runner.DataFrame {
input := make(chan *runner.DataFrame)
go func() {
defer ch.closePipeToFn()
for {
select {
case <-ch.doneQueue:
return
case <-ch.ctx.Done():
return
case data := <-input:
if data == nil {
return
}
if len(data.Data) > 0 {
_, err := io.CopyN(ch.pipeToFnW, bytes.NewReader(data.Data), int64(len(data.Data)))
if err != nil {
ch.shutdown(err)
return
}
}
if data.Eof {
return
}
}
}
}()
return input
}
// spawnReceiver starts a gRPC receiver, which
// feeds received LB messages into inQueue
func (ch *callHandle) spawnReceiver() {
go func() {
defer close(ch.inQueue)
for {
msg, err := ch.engagement.Recv()
if err != nil {
// engagement is close/cancelled from client.
if err == io.EOF {
return
}
ch.shutdown(err)
return
}
select {
case ch.inQueue <- msg:
case <-ch.doneQueue:
return
case <-ch.ctx.Done():
return
}
}
}()
}
// spawnSender starts a gRPC sender, which
// pumps messages from outQueue to the LB.
func (ch *callHandle) spawnSender() {
go func() {
for {
select {
case msg := <-ch.outQueue:
if msg == nil {
ch.shutdown(nil)
return
}
err := ch.engagement.Send(msg)
if err != nil {
ch.shutdown(err)
return
}
case <-ch.doneQueue:
return
case <-ch.ctx.Done():
return
}
}
}()
}
// Header implements http.ResponseWriter, which
// is used by Agent to push headers to pure runner
func (ch *callHandle) Header() http.Header {
return ch.headers
}
// WriteHeader implements http.ResponseWriter, which
// is used by Agent to push http status to pure runner
func (ch *callHandle) WriteHeader(status int) {
ch.status = status
var err error
ch.headerOnce.Do(func() {
// WARNING: we do fetch Status and Headers without
// a lock below. This is a problem in agent in general, and needs
// to be fixed in all accessing go-routines such as protocol/http.go,
// protocol/json.go, agent.go, etc. In practice however, one go routine
// accesses them (which also compiles and writes headers), but this
// is fragile and needs to be fortified.
err = ch.enqueueMsgStrict(&runner.RunnerMsg{
Body: &runner.RunnerMsg_ResultStart{
ResultStart: &runner.CallResultStart{
Meta: &runner.CallResultStart_Http{
Http: &runner.HttpRespMeta{
Headers: ch.prepHeaders(),
StatusCode: int32(status),
},
},
},
},
})
})
if err != nil {
logrus.WithError(err).Info("Error in WriteHeader, unable to send RunnerMsg_ResultStart, shutting down callHandler")
}
}
// prepHeaders is a utility function to compile http headers
// into a flat array.
func (ch *callHandle) prepHeaders() []*runner.HttpHeader {
var headers []*runner.HttpHeader
for h, vals := range ch.headers {
for _, v := range vals {
headers = append(headers, &runner.HttpHeader{
Key: h,
Value: v,
})
}
}
return headers
}
// Write implements http.ResponseWriter, which
// is used by Agent to push http data to pure runner. The
// received data is pushed to LB via gRPC sender queue.
// Write also sends http headers/state to the LB.
func (ch *callHandle) Write(data []byte) (int, error) {
if ch.c.Model().Type == models.TypeDetached {
//If it is an detached call we just /dev/null the data coming back from the container
return len(data), nil
}
ch.WriteHeader(ch.status)
// if we have any error during the WriteHeader the doneQueue will be closed by the
// shutdown process. We check here if that happens, if so we return immediately
// as there is no point to proceed with the Write
select {
case <-ch.ctx.Done():
return 0, io.EOF
case <-ch.doneQueue:
return 0, io.EOF
default:
}
var err error
total := 0
// split up data into gRPC chunks
for {
chunkSize := len(data)
if chunkSize > MaxDataChunk {
chunkSize = MaxDataChunk
}
if chunkSize == 0 {
break
}
// we cannot retain 'data'
cpData := make([]byte, chunkSize)
copy(cpData, data[0:chunkSize])
data = data[chunkSize:]
err = ch.enqueueMsg(&runner.RunnerMsg{
Body: &runner.RunnerMsg_Data{
Data: &runner.DataFrame{
Data: cpData,
Eof: false,
},
},
})
if err != nil {
return total, err
}
total += chunkSize
}
return total, nil
}
// getTryMsg fetches/waits for a TryCall message from
// the LB using inQueue (gRPC receiver)
func (ch *callHandle) getTryMsg() *runner.TryCall {
var msg *runner.TryCall
select {
case <-ch.doneQueue:
case <-ch.ctx.Done():
// if ctx timed out while waiting, then this is a 503 (retriable)
err := status.Errorf(codes.Code(models.ErrCallTimeoutServerBusy.Code()), models.ErrCallTimeoutServerBusy.Error())
ch.shutdown(err)
return nil
case item := <-ch.inQueue:
if item != nil {
msg = item.GetTry()
}
}
if msg == nil {
ch.shutdown(ErrorExpectedTry)
}
return msg
}
// getDataMsg fetches/waits for a DataFrame message from
// the LB using inQueue (gRPC receiver)
func (ch *callHandle) getDataMsg() *runner.DataFrame {
var msg *runner.DataFrame
select {
case <-ch.doneQueue:
case <-ch.ctx.Done():
case item := <-ch.inQueue:
if item != nil {
msg = item.GetData()
}
}
if msg == nil {
ch.shutdown(ErrorExpectedData)
}
return msg
}
const (
// Here we give 5 seconds of timeout inside the container. We hardcode these numbers here to
// ensure we control idle timeout & timeout as well as how long should cache be valid.
// A cache duration of idleTimeout + 500 msecs allows us to reuse the cache, for about 1.5 secs,
// and during this time, since we allow no queries to go through, the hot container times out.
//
// For now, status tests a single case: a new hot container is spawned when cache is expired
// and when a query is allowed to run.
// TODO: we might want to mix this up and perhaps allow that hot container to handle
// more than one query to test both 'new hot container' and 'old hot container' cases.
StatusCallTimeout = int32(5)
StatusCallIdleTimeout = int32(1)
StatusCallCacheDuration = time.Duration(500)*time.Millisecond + time.Duration(StatusCallIdleTimeout)*time.Second
// Total context timeout (scheduler+execution.) We need to allocate plenty of time here.
// 60 seconds should be enough to provoke disk I/O errors, docker timeouts. etc.
StatusCtxTimeout = time.Duration(60 * time.Second)
)
// statusTracker maintains cache data/state/locks for Status Call invocations.
type statusTracker struct {
inflight int32
requestsReceived uint64
requestsHandled uint64
kdumpsOnDisk uint64
imageName string
// if file exists, then network in status checks is enabled.
barrierPath string
// lock protects expiry/cache/wait fields below. RunnerStatus ptr itself
// stored every time status image is executed. Cache fetches use a shallow
// copy of RunnerStatus to ensure consistency. Shallow copy is sufficient
// since we set/save contents of RunnerStatus once.
lock sync.Mutex
expiry time.Time
cache *runner.RunnerStatus
wait chan struct{}
}
// Log Streamer to manage log gRPC interface
type LogStreamer interface {
StreamLogs(runner.RunnerProtocol_StreamLogsServer) error
}
// pureRunner implements Agent and delegates execution of functions to an internal Agent; basically it wraps around it
// and provides the gRPC server that implements the LB <-> Runner protocol.
type pureRunner struct {
gRPCServer *grpc.Server
gRPCOptions []grpc.ServerOption
creds credentials.TransportCredentials
a Agent
logStreamer LogStreamer
status statusTracker
callHandleMap map[string]*callHandle
callHandleLock sync.Mutex
enableDetach bool
configFunc func(context.Context, *runner.ConfigMsg) (*runner.ConfigStatus, error)
customHealthCheckerFunc func(context.Context) (map[string]string, error)
}
// implements Agent
func (pr *pureRunner) GetCall(opts ...CallOpt) (Call, error) {
return pr.a.GetCall(opts...)
}
// implements Agent
func (pr *pureRunner) Submit(Call) error {
return errors.New("Submit cannot be called directly in a Pure Runner.")
}
// implements Agent
func (pr *pureRunner) Close() error {
// First stop accepting requests
pr.gRPCServer.GracefulStop()
// Then let the agent finish
err := pr.a.Close()
if err != nil {
return err
}
return nil
}
// implements Agent
func (pr *pureRunner) AddCallListener(cl fnext.CallListener) {
pr.a.AddCallListener(cl)
}
func (pr *pureRunner) saveCallHandle(ch *callHandle) {
pr.callHandleLock.Lock()
pr.callHandleMap[ch.c.Model().ID] = ch
pr.callHandleLock.Unlock()
}
func (pr *pureRunner) removeCallHandle(cID string) {
pr.callHandleLock.Lock()
delete(pr.callHandleMap, cID)
pr.callHandleLock.Unlock()
}
func (pr *pureRunner) spawnSubmit(state *callHandle) {
go func() {
err := pr.a.Submit(state.c)
state.enqueueCallResponse(err)
}()
}
func (pr *pureRunner) spawnDetachSubmit(state *callHandle) {
go func() {
pr.saveCallHandle(state)
err := pr.a.Submit(state.c)
state.enqueueCallResponse(err)
pr.removeCallHandle(state.c.Model().ID)
}()
}
// handleTryCall based on the TryCall message, tries to place the call on NBIO Agent
func (pr *pureRunner) handleTryCall(tc *runner.TryCall, state *callHandle) error {
var c models.Call
err := json.Unmarshal([]byte(tc.ModelsCallJson), &c)
if err != nil {
state.enqueueCallResponse(err)
return err
}
// Status image is reserved for internal Status checks.
// We need to make sure normal functions calls cannot call it.
if pr.status.imageName != "" && c.Image == pr.status.imageName {
err = models.ErrFnsInvalidImage
state.enqueueCallResponse(err)
return err
}
// IMPORTANT: We clear/initialize these dates as start/created/completed dates from
// unmarshalled Model from LB-agent represent unrelated time-line events.
// From this point, CreatedAt/StartedAt/CompletedAt are based on our local clock.
c.CreatedAt = common.DateTime(time.Now())
c.StartedAt = common.DateTime(time.Time{})
c.CompletedAt = common.DateTime(time.Time{})
agentCall, err := pr.a.GetCall(FromModelAndInput(&c, state.pipeToFnR),
WithLogger(common.NoopReadWriteCloser{}),
WithWriter(state),
WithContext(state.ctx),
WithExtensions(tc.GetExtensions()),
)
if err != nil {
state.enqueueCallResponse(err)
return err
}
state.c = agentCall.(*call)
if tc.SlotHashId != "" {
hashID, err := hex.DecodeString(tc.SlotHashId)
if err != nil {
state.enqueueCallResponse(err)
return err
}
state.c.slotHashId = string(hashID[:])
}
if state.c.Type == models.TypeDetached {
if !pr.enableDetach {
err = models.ErrDetachUnsupported
state.enqueueCallResponse(err)
return err
}
pr.spawnDetachSubmit(state)
return nil
}
pr.spawnSubmit(state)
return nil
}
// implements RunnerProtocolServer
// Handles a client engagement
func (pr *pureRunner) Engage(engagement runner.RunnerProtocol_EngageServer) error {
grpc.EnableTracing = false
ctx := engagement.Context()
log := common.Logger(ctx)
// Keep lightweight tabs on what this runner is doing: for draindown tests
atomic.AddInt32(&pr.status.inflight, 1)
atomic.AddUint64(&pr.status.requestsReceived, 1)
pv, ok := peer.FromContext(ctx)
log.Debug("Starting engagement")
if ok {
log.Debug("Peer is ", pv)
}
md, ok := metadata.FromIncomingContext(ctx)
if ok {
log.Debug("MD is ", md)
}
state := NewCallHandle(engagement)
tryMsg := state.getTryMsg()
if tryMsg != nil {
errTry := pr.handleTryCall(tryMsg, state)
if errTry == nil {
dataFeed := state.spawnPipeToFn()
DataLoop:
for {
dataMsg := state.getDataMsg()
if dataMsg == nil {
break
}
select {
case dataFeed <- dataMsg:
if dataMsg.Eof {
break DataLoop
}
case <-state.doneQueue:
break DataLoop
case <-state.ctx.Done():
break DataLoop
}
}
}
}
err := state.waitError()
// if we didn't respond with TooBusy, then this means the request
// was processed.
if err != models.ErrCallTimeoutServerBusy {
atomic.AddUint64(&pr.status.requestsHandled, 1)
}
atomic.AddInt32(&pr.status.inflight, -1)
return err
}
// Runs a status call using status image with baked in parameters.
func (pr *pureRunner) runStatusCall(ctx context.Context) *runner.RunnerStatus {
// IMPORTANT: apply an upper bound timeout
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, StatusCtxTimeout)
defer cancel()
result := &runner.RunnerStatus{}
log := common.Logger(ctx)
start := time.Now()
// construct call
var c models.Call
// Most of these arguments are baked in. We might want to make this
// more configurable.
c.ID = id.New().String()
c.Image = pr.status.imageName
c.Type = models.TypeSync
c.TmpFsSize = 0
// IMPORTANT: mem/cpu set to zero. This means status containers cannot be evicted.
c.Memory = 0
c.CPUs = models.MilliCPUs(0)
c.URL = "/"
c.Method = "GET"
c.CreatedAt = common.DateTime(start)
c.Config = make(models.Config)
c.Payload = "{}"
c.Timeout = StatusCallTimeout
c.IdleTimeout = StatusCallIdleTimeout
// TODO: reliably shutdown this container after executing one request.
log.Debugf("Running status call with id=%v image=%v", c.ID, c.Image)
recorder := httptest.NewRecorder()
player := ioutil.NopCloser(strings.NewReader(c.Payload))
// Fetch network status
if pr.status.barrierPath != "" {
_, err := os.Lstat(pr.status.barrierPath)
if err != nil {
result.IsNetworkDisabled = true
}
}
var err error
// handle custom healthcheck
if pr.customHealthCheckerFunc != nil {
result.CustomStatus, err = pr.customHealthCheckerFunc(ctx)
}
var agentCall Call
var mcall *call
if err == nil {
agentCall, err = pr.a.GetCall(FromModelAndInput(&c, player),
WithLogger(common.NoopReadWriteCloser{}),
WithWriter(recorder),
WithContext(ctx),
)
}
if err == nil {
mcall = agentCall.(*call)
// disable network if not ready
mcall.disableNet = result.IsNetworkDisabled
err = pr.a.Submit(mcall)
}
resp := recorder.Result()
if err != nil {
result.ErrorCode = int32(models.GetAPIErrorCode(err))
result.ErrorStr = err.Error()
result.Failed = true
} else if resp.StatusCode >= http.StatusBadRequest {
result.ErrorCode = int32(resp.StatusCode)
result.Failed = true
}
schedDur, execDur := GetCallLatencies(mcall)
result.SchedulerDuration = int64(schedDur)
result.ExecutionDuration = int64(execDur)
// These timestamps are related. To avoid confusion
// and for robustness, nested if stmts below.
if !time.Time(c.CreatedAt).IsZero() {
result.CreatedAt = c.CreatedAt.String()
if !time.Time(c.StartedAt).IsZero() {
result.StartedAt = c.StartedAt.String()
if !time.Time(c.CompletedAt).IsZero() {
result.CompletedAt = c.CompletedAt.String()
} else {
// IMPORTANT: We punch this in ourselves.
result.CompletedAt = common.DateTime(time.Now()).String()
}
}
}
// Status images should not output excessive data since we echo the
// data back to caller.
body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
result.Details = string(body)
result.Id = c.ID
log.Debugf("Finished status call id=%v result=%+v", c.ID, result)
return result
}
func (pr *pureRunner) spawnStatusCall(ctx context.Context) {
go func() {
var waitChan chan struct{}
// IMPORTANT: We have to strip client timeouts to make sure this completes
// in the background even if client cancels/times out.
cachePtr := pr.runStatusCall(common.BackgroundContext(ctx))
now := time.Now()
// Pointer store of 'cachePtr' is sufficient here as isWaiter/isCached above perform a shallow
// copy of 'cache'
pr.status.lock.Lock()
pr.status.cache = cachePtr
pr.status.expiry = now.Add(StatusCallCacheDuration)
waitChan = pr.status.wait // cannot be null
pr.status.wait = nil
pr.status.lock.Unlock()
// signal waiters
close(waitChan)
}()
}
func (pr *pureRunner) fetchStatusCall(ctx context.Context) (*runner.RunnerStatus, error) {
var cacheObj runner.RunnerStatus
// A shallow copy is sufficient here, as we do not modify nested data in
// RunnerStatus in any way.
pr.status.lock.Lock()
cacheObj = *pr.status.cache // cannot be null
// deepcopy of custom healthcheck status is needed
cacheObj.CustomStatus = make(map[string]string)
for k, v := range pr.status.cache.CustomStatus {
cacheObj.CustomStatus[k] = v
}
pr.status.cache.Cached = true
pr.status.lock.Unlock()
// The rest of the RunnerStatus fields are not cached and always populated
// with latest metrics.
cacheObj.Active = atomic.LoadInt32(&pr.status.inflight)
cacheObj.RequestsReceived = atomic.LoadUint64(&pr.status.requestsReceived)
cacheObj.RequestsHandled = atomic.LoadUint64(&pr.status.requestsHandled)
cacheObj.KdumpsOnDisk = atomic.LoadUint64(&pr.status.kdumpsOnDisk)
return &cacheObj, ctx.Err()
}
func (pr *pureRunner) checkStatusCall(ctx context.Context) (chan struct{}, bool) {
now := time.Now()
pr.status.lock.Lock()
defer pr.status.lock.Unlock()
// cached?
if pr.status.expiry.After(now) {
return nil, false
}
// already running?
if pr.status.wait != nil {
return pr.status.wait, false
}
// spawn a new call
pr.status.wait = make(chan struct{})
return pr.status.wait, true
}
// Handles a status call concurrency and caching.
func (pr *pureRunner) handleStatusCall(ctx context.Context) (*runner.RunnerStatus, error) {
waitChan, isSpawner := pr.checkStatusCall(ctx)
// from cache
if waitChan == nil {
return pr.fetchStatusCall(ctx)
}
if isSpawner {
pr.spawnStatusCall(ctx)
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-waitChan:
return pr.fetchStatusCall(ctx)
}
}
// implements RunnerProtocolServer
func (pr *pureRunner) Status(ctx context.Context, _ *empty.Empty) (*runner.RunnerStatus, error) {
// Status using image name is disabled. We return inflight request count only
if pr.status.imageName == "" {
return &runner.RunnerStatus{
Active: atomic.LoadInt32(&pr.status.inflight),
RequestsReceived: atomic.LoadUint64(&pr.status.requestsReceived),
RequestsHandled: atomic.LoadUint64(&pr.status.requestsHandled),
}, nil
}
status, err := pr.handleStatusCall(ctx)
if err != nil {
common.Logger(ctx).WithError(err).Errorf("Status call failed result=%+v", status)
}
cached := "error"
success := "error"
network := "error"
if err == nil && status != nil {
cached = strconv.FormatBool(status.Cached)
success = strconv.FormatBool(!status.Failed)
network = strconv.FormatBool(!status.IsNetworkDisabled)
}
statsStatusCall(ctx, cached, success, network)
return status, err
}