forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.go
More file actions
575 lines (483 loc) · 18.3 KB
/
Copy pathstart.go
File metadata and controls
575 lines (483 loc) · 18.3 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// Package rapid implements synchronous even dispatch loop.
package rapid
import (
"context"
"errors"
"io"
"os"
"time"
"go.amzn.com/lambda/agents"
"go.amzn.com/lambda/appctx"
"go.amzn.com/lambda/core"
"go.amzn.com/lambda/extensions"
"go.amzn.com/lambda/fatalerror"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/logging"
"go.amzn.com/lambda/metering"
"go.amzn.com/lambda/rapi"
"go.amzn.com/lambda/rapi/rendering"
"go.amzn.com/lambda/runtimecmd"
"go.amzn.com/lambda/telemetry"
log "github.com/sirupsen/logrus"
)
const (
RuntimeAPIHost = "127.0.0.1"
RuntimeAPIPort = 9001
defaultAgentLocation = "/opt/extensions"
runtimeDeadlineShare = 0.3
disableExtensionsFile = "/opt/disable-extensions-jwigqn8j"
)
const (
standaloneShutdownReason = "spindown"
)
var errResetReceived = errors.New("errResetReceived")
type rapidContext struct {
bootstrap Bootstrap
interopServer interop.Server
server *rapi.Server
appCtx appctx.ApplicationContext
preLoadTimeNs int64
postLoadTimeNs int64
startRequest *interop.Start
initDone bool
initFlow core.InitFlowSynchronization
invokeFlow core.InvokeFlowSynchronization
registrationService core.RegistrationService
renderingService *rendering.EventRenderingService
telemetryAPIEnabled bool
telemetryService telemetry.LogsAPIService
xray telemetry.Tracer
exitPidChan chan int
resetChan chan *interop.Reset
environment EnvironmentVariables
standaloneMode bool
debugTailLogger *logging.TailLogWriter
platformLogger logging.PlatformLogger
extensionLogWriter io.Writer
runtimeLogWriter io.Writer
}
func (c *rapidContext) HasActiveExtensions() bool {
return extensions.AreEnabled() && c.registrationService.CountAgents() > 0
}
func logAgentsInitStatus(execCtx *rapidContext) {
for _, agent := range execCtx.registrationService.AgentsInfo() {
execCtx.platformLogger.LogExtensionInitEvent(agent.Name, agent.State, agent.ErrorType, agent.Subscriptions)
}
}
func agentLaunchError(agent *core.ExternalAgent, appCtx appctx.ApplicationContext, launchError error) {
if err := agent.LaunchError(launchError); err != nil {
log.Warnf("LaunchError transition fail for %s from %s: %s", agent, agent.GetState().Name(), err)
}
appctx.StoreFirstFatalError(appCtx, fatalerror.AgentLaunchError)
}
func doInitExtensions(execCtx *rapidContext, watchdog *core.Watchdog) error {
agentPaths := agents.ListExternalAgentPaths(defaultAgentLocation)
initFlow := execCtx.registrationService.InitFlow()
// we don't bring it into the loop below because we don't want unnecessary broadcasts on agent gate
if err := initFlow.SetExternalAgentsRegisterCount(uint16(len(agentPaths))); err != nil {
return err
}
for _, agentPath := range agentPaths {
env := execCtx.environment.AgentExecEnv()
agentLogSinks := execCtx.extensionLogWriter
agentProc := agents.NewExternalAgentProcess(agentPath, env, agentLogSinks)
agent, err := execCtx.registrationService.CreateExternalAgent(agentProc.Name())
if err != nil {
return err
}
if execCtx.registrationService.CountAgents() > core.MaxAgentsAllowed {
agentLaunchError(agent, execCtx.appCtx, core.ErrTooManyExtensions)
return core.ErrTooManyExtensions
}
if err := agentProc.Start(); err != nil {
agentLaunchError(agent, execCtx.appCtx, err)
return err
}
agent.Pid = watchdog.GoWait(&agentProc, fatalerror.AgentCrash)
}
if err := initFlow.AwaitExternalAgentsRegistered(); err != nil {
return err
}
return nil
}
func doInit(ctx context.Context, execCtx *rapidContext, watchdog *core.Watchdog) error {
execCtx.xray.RecordInitStartTime()
defer execCtx.xray.RecordInitEndTime()
if extensions.AreEnabled() {
defer func() {
logAgentsInitStatus(execCtx)
}()
if err := doInitExtensions(execCtx, watchdog); err != nil {
return err
}
}
initFlow := execCtx.registrationService.InitFlow()
// Runtime state machine
runtime := core.NewRuntime(initFlow, execCtx.invokeFlow)
// Registration service keeps track of parties registered in the system and events they are registered for.
// Runtime's use case is generalized, because runtime doesn't register itself, we preregister it in the system;
// runtime is implicitly subscribed for certain lifecycle events.
log.Debug("Preregister runtime")
registrationService := execCtx.registrationService
if err := registrationService.PreregisterRuntime(runtime); err != nil {
return err
}
bootstrap := execCtx.bootstrap
bootstrapCmd, err := bootstrap.Cmd()
if err != nil {
if fatalError, formattedLog, hasError := bootstrap.CachedFatalError(err); hasError {
appctx.StoreFirstFatalError(execCtx.appCtx, fatalError)
execCtx.platformLogger.Printf("%s", formattedLog)
} else {
appctx.StoreFirstFatalError(execCtx.appCtx, fatalerror.InvalidEntrypoint)
}
return err
}
bootstrapEnv := bootstrap.Env(execCtx.environment)
bootstrapCwd, err := bootstrap.Cwd()
if err != nil {
if fatalError, formattedLog, hasError := bootstrap.CachedFatalError(err); hasError {
appctx.StoreFirstFatalError(execCtx.appCtx, fatalError)
execCtx.platformLogger.Printf("%s", formattedLog)
} else {
appctx.StoreFirstFatalError(execCtx.appCtx, fatalerror.InvalidWorkingDir)
}
return err
}
bootstrapExtraFiles := bootstrap.ExtraFiles()
runtimeCmd := runtimecmd.NewCustomRuntimeCmd(ctx, bootstrapCmd, bootstrapCwd, bootstrapEnv, execCtx.runtimeLogWriter, bootstrapExtraFiles)
log.Debug("Start runtime")
err = runtimeCmd.Start()
if err != nil {
if fatalError, formattedLog, hasError := bootstrap.CachedFatalError(err); hasError {
appctx.StoreFirstFatalError(execCtx.appCtx, fatalError)
execCtx.platformLogger.Printf("%s", formattedLog)
} else {
appctx.StoreFirstFatalError(execCtx.appCtx, fatalerror.InvalidEntrypoint)
}
return err
}
registrationService.GetRuntime().Pid = watchdog.GoWait(runtimeCmd, fatalerror.RuntimeExit)
if err := initFlow.AwaitRuntimeReady(); err != nil {
return err
}
// Registration phase finished for agents - no more agents can be registered with the system
registrationService.TurnOff()
if extensions.AreEnabled() {
// Initialize and activate the gate with the number of agent we wait to return ready
if err := initFlow.SetAgentsReadyCount(registrationService.GetRegisteredAgentsSize()); err != nil {
return err
}
if err := initFlow.AwaitAgentsReady(); err != nil {
return err
}
}
// Logs API subscription phase finished for agents - no more agents can be subscribed to the Logs API
if execCtx.telemetryAPIEnabled {
execCtx.telemetryService.TurnOff()
}
execCtx.initDone = true
return nil
}
func doInvoke(ctx context.Context, execCtx *rapidContext, watchdog *core.Watchdog, invokeRequest *interop.Invoke, mx *rendering.InvokeRendererMetrics) error {
appCtx := execCtx.appCtx
appctx.StoreErrorResponse(appCtx, nil)
if invokeRequest.NeedDebugLogs {
execCtx.debugTailLogger.Enable()
} else {
execCtx.debugTailLogger.Disable()
}
xray := execCtx.xray
xray.Configure(invokeRequest)
return xray.CaptureInvokeSegment(ctx, xray.WithErrorCause(ctx, appCtx, func(ctx context.Context) error {
if !execCtx.initDone {
// do inline init
if err := xray.CaptureInitSubsegment(ctx, func(ctx context.Context) error {
return doInit(ctx, execCtx, watchdog)
}); err != nil {
return err
}
} else if execCtx.startRequest.SandboxType != interop.SandboxPreWarmed {
xray.SendInitSubsegmentWithRecordedTimesOnce(ctx)
}
invokeFlow := execCtx.invokeFlow
log.Debug("Initialize invoke flow barriers")
err := invokeFlow.InitializeBarriers()
if err != nil {
return err
}
registrationService := execCtx.registrationService
runtime := registrationService.GetRuntime()
var intAgents []*core.InternalAgent
var extAgents []*core.ExternalAgent
if extensions.AreEnabled() {
intAgents = registrationService.GetSubscribedInternalAgents(core.InvokeEvent)
extAgents = registrationService.GetSubscribedExternalAgents(core.InvokeEvent)
if err := invokeFlow.SetAgentsReadyCount(uint16(len(intAgents) + len(extAgents))); err != nil {
return err
}
}
// Invoke
if err := xray.CaptureInvokeSubsegment(ctx, xray.WithError(ctx, appCtx, func(ctx context.Context) error {
log.Debug("Set renderer for invoke")
renderer := rendering.NewInvokeRenderer(ctx, invokeRequest, xray.TracingHeaderParser())
defer func() {
*mx = renderer.GetMetrics()
}()
execCtx.renderingService.SetRenderer(renderer)
if extensions.AreEnabled() {
log.Debug("Release agents conditions")
for _, agent := range extAgents {
agent.Release()
}
for _, agent := range intAgents {
agent.Release()
}
}
log.Debug("Release runtime condition")
runtime.Release()
log.Debug("Await runtime response")
return invokeFlow.AwaitRuntimeResponse()
})); err != nil {
return err
}
// Runtime overhead
if err := xray.CaptureOverheadSubsegment(ctx, func(ctx context.Context) error {
log.Debug("Await runtime ready")
return invokeFlow.AwaitRuntimeReady()
}); err != nil {
return err
}
// Extensions overhead
if execCtx.HasActiveExtensions() {
execCtx.interopServer.SendRuntimeReady()
log.Debug("Await agents ready")
if err := invokeFlow.AwaitAgentsReady(); err != nil {
log.Warnf("AwaitAgentsReady() = %s", err)
return err
}
}
return nil
}))
}
func extensionsDisabledByLayer() bool {
_, err := os.Stat(disableExtensionsFile)
log.Infof("extensionsDisabledByLayer(%s) -> %s", disableExtensionsFile, err)
return err == nil
}
// acceptStartRequest is a second initialization phase, performed after receiving START
// initialized entities: _HANDLER, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN
func (c *rapidContext) acceptStartRequest(startRequest *interop.Start) {
c.startRequest = startRequest
c.environment.StoreEnvironmentVariablesFromInit(
startRequest.CustomerEnvironmentVariables,
startRequest.Handler,
startRequest.AwsKey,
startRequest.AwsSecret,
startRequest.AwsSession,
startRequest.FunctionName,
startRequest.FunctionVersion)
c.registrationService.SetFunctionMetadata(core.FunctionMetadata{
FunctionName: startRequest.FunctionName,
FunctionVersion: startRequest.FunctionVersion,
Handler: startRequest.Handler,
})
if extensionsDisabledByLayer() {
extensions.Disable()
}
}
func handleStart(ctx context.Context, execCtx *rapidContext, watchdog *core.Watchdog, startRequest *interop.Start) {
execCtx.acceptStartRequest(startRequest)
interopServer, appCtx := execCtx.interopServer, execCtx.appCtx
if err := interopServer.SendRunning(&interop.Running{
PreLoadTimeNs: execCtx.preLoadTimeNs,
PostLoadTimeNs: execCtx.postLoadTimeNs,
WaitStartTimeNs: execCtx.postLoadTimeNs,
WaitEndTimeNs: metering.Monotime(),
ExtensionsEnabled: extensions.AreEnabled(),
}); err != nil {
log.Panic(err)
}
if !startRequest.SuppressInit {
if err := doInit(ctx, execCtx, watchdog); err != nil {
log.WithError(err).WithField("InvokeID", startRequest.InvokeID).Error("Init failed")
doneFailMsg := generateDoneFail(execCtx, startRequest.CorrelationID, nil)
handleInitError(doneFailMsg, execCtx, startRequest.InvokeID, interopServer, err)
return
}
}
doneMsg := &interop.Done{
CorrelationID: startRequest.CorrelationID,
Meta: interop.DoneMetadata{
RuntimeRelease: appctx.GetRuntimeRelease(appCtx),
NumActiveExtensions: execCtx.registrationService.CountAgents(),
},
}
if execCtx.telemetryAPIEnabled {
doneMsg.Meta.LogsAPIMetrics = execCtx.telemetryService.FlushMetrics()
}
if err := interopServer.SendDone(doneMsg); err != nil {
log.Panic(err)
}
}
func generateDoneFail(execCtx *rapidContext, correlationID string, invokeMx *rendering.InvokeRendererMetrics) *interop.DoneFail {
errorType, found := appctx.LoadFirstFatalError(execCtx.appCtx)
if !found {
errorType = fatalerror.Unknown
}
doneFailMsg := &interop.DoneFail{
ErrorType: errorType,
CorrelationID: correlationID, // required for standalone mode
Meta: interop.DoneMetadata{
RuntimeRelease: appctx.GetRuntimeRelease(execCtx.appCtx),
NumActiveExtensions: execCtx.registrationService.CountAgents(),
},
}
if invokeMx != nil {
doneFailMsg.Meta.InvokeRequestReadTimeNs = invokeMx.ReadTime.Nanoseconds()
doneFailMsg.Meta.InvokeRequestSizeBytes = int64(invokeMx.SizeBytes)
}
if execCtx.telemetryAPIEnabled {
doneFailMsg.Meta.LogsAPIMetrics = execCtx.telemetryService.FlushMetrics()
}
return doneFailMsg
}
func handleInvoke(ctx context.Context, execCtx *rapidContext, watchdog *core.Watchdog, invokeRequest *interop.Invoke) {
interopServer, appCtx := execCtx.interopServer, execCtx.appCtx
invokeMx := rendering.InvokeRendererMetrics{}
if err := doInvoke(ctx, execCtx, watchdog, invokeRequest, &invokeMx); err != nil {
log.WithError(err).WithField("InvokeID", invokeRequest.ID).Error("Invoke failed")
doneFailMsg := generateDoneFail(execCtx, invokeRequest.CorrelationID, &invokeMx)
handleInvokeError(doneFailMsg, execCtx, invokeRequest.ID, interopServer, err)
return
}
if err := execCtx.interopServer.CommitResponse(); err != nil {
log.Panic(err)
}
var invokeCompletionTimeNs int64
if responseTimeNs := execCtx.registrationService.GetRuntime().GetRuntimeDescription().State.ResponseTimeNs; responseTimeNs != 0 {
invokeCompletionTimeNs = time.Now().UnixNano() - responseTimeNs
}
doneMsg := &interop.Done{
CorrelationID: invokeRequest.CorrelationID,
Meta: interop.DoneMetadata{
RuntimeRelease: appctx.GetRuntimeRelease(appCtx),
NumActiveExtensions: execCtx.registrationService.CountAgents(),
InvokeRequestReadTimeNs: invokeMx.ReadTime.Nanoseconds(),
InvokeRequestSizeBytes: int64(invokeMx.SizeBytes),
InvokeCompletionTimeNs: invokeCompletionTimeNs,
},
}
if execCtx.telemetryAPIEnabled {
doneMsg.Meta.LogsAPIMetrics = execCtx.telemetryService.FlushMetrics()
}
if err := interopServer.SendDone(doneMsg); err != nil {
log.Panic(err)
}
}
func reinitialize(execCtx *rapidContext, watchdog *core.Watchdog) {
execCtx.interopServer.Clear()
execCtx.appCtx.Delete(appctx.AppCtxInvokeErrorResponseKey)
execCtx.appCtx.Delete(appctx.AppCtxRuntimeReleaseKey)
execCtx.appCtx.Delete(appctx.AppCtxFirstFatalErrorKey)
execCtx.renderingService.SetRenderer(nil)
execCtx.initDone = false
execCtx.registrationService.Clear()
execCtx.initFlow.Clear()
execCtx.invokeFlow.Clear()
if execCtx.telemetryAPIEnabled {
execCtx.telemetryService.Clear()
}
watchdog.Clear()
}
func blockForever() {
select {}
}
// handle notification of reset
func handleReset(execCtx *rapidContext, watchdog *core.Watchdog, reset *interop.Reset) {
log.Warnf("Reset initiated: %s", reset.Reason)
profiler := metering.ExtensionsResetDurationProfiler{}
gracefulShutdown(execCtx, watchdog, &profiler, reset.DeadlineNs, execCtx.standaloneMode, reset.Reason)
extensionsResetMs, resetTimeout := profiler.CalculateExtensionsResetMs()
meta := interop.DoneMetadata{
ExtensionsResetMs: extensionsResetMs,
}
if !execCtx.standaloneMode {
// GIRP interopServer implementation sends GIRP RSTFAIL/RSTDONE
if resetTimeout {
// TODO: DoneFail must contain a reset timeout ErrorType for rapid local to distinguish errors
doneFail := &interop.DoneFail{CorrelationID: reset.CorrelationID, Meta: meta}
if err := execCtx.interopServer.SendDoneFail(doneFail); err != nil {
log.Panicf("Failed to SendDoneFail: %s", err)
}
} else {
done := &interop.Done{CorrelationID: reset.CorrelationID, Meta: meta}
if err := execCtx.interopServer.SendDone(done); err != nil {
log.Panicf("Failed to SendDone: %s", err)
}
}
os.Exit(0)
}
reinitialize(execCtx, watchdog)
fatalErrorType, _ := appctx.LoadFirstFatalError(execCtx.appCtx)
if resetTimeout {
doneFail := &interop.DoneFail{CorrelationID: reset.CorrelationID, ErrorType: fatalErrorType, Meta: meta}
if err := execCtx.interopServer.SendDoneFail(doneFail); err != nil {
log.Panicf("Failed to SendDoneFail: %s", err)
}
} else {
done := &interop.Done{CorrelationID: reset.CorrelationID, ErrorType: fatalErrorType, Meta: meta}
if err := execCtx.interopServer.SendDone(done); err != nil {
log.Panicf("Failed to SendDone: %s", err)
}
}
}
// handle notification of shutdown
func handleShutdown(execCtx *rapidContext, watchdog *core.Watchdog, shutdown *interop.Shutdown, reason string) {
log.Warnf("Shutdown initiated")
gracefulShutdown(execCtx, watchdog, &metering.ExtensionsResetDurationProfiler{}, shutdown.DeadlineNs, true, reason)
fatalErrorType, _ := appctx.LoadFirstFatalError(execCtx.appCtx)
if err := execCtx.interopServer.SendDone(&interop.Done{CorrelationID: shutdown.CorrelationID, ErrorType: fatalErrorType}); err != nil {
log.Panicf("Failed to SendDone: %s", err)
}
// Shutdown induces a terminal state and no further messages will be processed
blockForever()
}
func start(signalCtx context.Context, execCtx *rapidContext) {
watchdog := core.NewWatchdog(execCtx.registrationService.InitFlow(), execCtx.invokeFlow, execCtx.exitPidChan, execCtx.appCtx)
interopServer := execCtx.interopServer
// Start Runtime API Server
err := execCtx.server.Listen()
if err != nil {
log.WithError(err).Panic("Runtime API Server failed to listen")
}
go func() { execCtx.server.Serve(signalCtx) }()
// Note, most of initialization code should run before blocking to receive START,
// code before START runs in parallel with code downloads.
go func() {
for {
reset := <-interopServer.ResetChan()
// In the event of a Reset during init/invoke, CancelFlows cancels execution
// flows and return with the errResetReceived err - this error is special-cased
// and not handled by the init/invoke (unexpected) error handling functions
watchdog.CancelFlows(errResetReceived)
execCtx.resetChan <- reset
}
}()
for {
select {
case start := <-interopServer.StartChan():
handleStart(signalCtx, execCtx, watchdog, start)
case invoke := <-interopServer.InvokeChan():
handleInvoke(signalCtx, execCtx, watchdog, invoke)
case err := <-interopServer.TransportErrorChan():
log.Panicf("Transport error emitted by interop server: %s", err)
case reset := <-execCtx.resetChan:
handleReset(execCtx, watchdog, reset)
case shutdown := <-interopServer.ShutdownChan(): // only in standalone mode
handleShutdown(execCtx, watchdog, shutdown, standaloneShutdownReason)
}
}
}