-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathsandbox.go
174 lines (148 loc) · 7.19 KB
/
sandbox.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package rapid
import (
"bytes"
"context"
"fmt"
"io"
"sync"
"go.amzn.com/lambda/appctx"
"go.amzn.com/lambda/core"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/rapi"
"go.amzn.com/lambda/rapi/rendering"
supvmodel "go.amzn.com/lambda/supervisor/model"
"go.amzn.com/lambda/telemetry"
)
type Sandbox struct {
EnableTelemetryAPI bool
StandaloneMode bool
InteropServer interop.Server
Tracer telemetry.Tracer
LogsSubscriptionAPI telemetry.SubscriptionAPI
TelemetrySubscriptionAPI telemetry.SubscriptionAPI
LogsEgressAPI telemetry.StdLogsEgressAPI
RuntimeStdoutWriter io.Writer
RuntimeStderrWriter io.Writer
Handler string
EventsAPI interop.EventsAPI
InitCachingEnabled bool
Supervisor supvmodel.ProcessSupervisor
RuntimeFsRootPath string // path to the root of the domain within the root mnt namespace. Reqired to find extensions
RuntimeAPIHost string
RuntimeAPIPort int
}
// Start pings Supervisor, and starts the Runtime API server. It allows the caller to configure:
// - Supervisor implementation: performs container construction & process management
// - Telemetry API and Logs API implementation: handling /logs and /telemetry of Runtime API
// - Events API implementation: handles platform log events emitted by Rapid (e.g. RuntimeDone, InitStart)
// - Logs Egress implementation: handling stdout/stderr logs from extension & runtime processes (TODO: remove & unify with Supervisor)
// - Tracer implementation: handling trace segments generate by platform (TODO: remove & unify with Events API)
// - InteropServer implementation: legacy interface for sending internal protocol messages, today only RuntimeReady remains (TODO: move RuntimeReady outside Core)
// - Feature flags:
// - StandaloneMode: indicates if being called by Rapid Core's standalone HTTP frontend (TODO: remove after unifying error reporting)
// - InitCachingEnabled: indicates if handlers must run Init Caching specific logic
// - TelemetryAPIEnabled: indicates if /telemetry and /logs endpoint HTTP handlers must be mounted
//
// - Contexts & Data:
// - ctx is used to gracefully terminate Runtime API HTTP Server on exit
func Start(ctx context.Context, s *Sandbox) (interop.RapidContext, interop.InternalStateGetter, string) {
// Initialize internal state objects required by Rapid handlers
appCtx := appctx.NewApplicationContext()
initFlow := core.NewInitFlowSynchronization()
invokeFlow := core.NewInvokeFlowSynchronization()
registrationService := core.NewRegistrationService(initFlow, invokeFlow)
renderingService := rendering.NewRenderingService()
credentialsService := core.NewCredentialsService()
appctx.StoreInitType(appCtx, s.InitCachingEnabled)
server := rapi.NewServer(s.RuntimeAPIHost, s.RuntimeAPIPort, appCtx, registrationService, renderingService, s.EnableTelemetryAPI, s.LogsSubscriptionAPI, s.TelemetrySubscriptionAPI, credentialsService)
runtimeAPIAddr := fmt.Sprintf("%s:%d", server.Host(), server.Port())
// TODO: pass this directly down to HTTP servers and handlers, instead of using
// global state to share the interop server implementation
appctx.StoreInteropServer(appCtx, s.InteropServer)
execCtx := &rapidContext{
// Internally initialized configurations
server: server,
appCtx: appCtx,
initDone: false,
initFlow: initFlow,
invokeFlow: invokeFlow,
registrationService: registrationService,
renderingService: renderingService,
credentialsService: credentialsService,
handlerExecutionMutex: sync.Mutex{},
shutdownContext: newShutdownContext(),
// Externally specified configurations (i.e. via SandboxBuilder)
telemetryAPIEnabled: s.EnableTelemetryAPI,
logsSubscriptionAPI: s.LogsSubscriptionAPI,
telemetrySubscriptionAPI: s.TelemetrySubscriptionAPI,
logsEgressAPI: s.LogsEgressAPI,
interopServer: s.InteropServer,
xray: s.Tracer,
standaloneMode: s.StandaloneMode,
eventsAPI: s.EventsAPI,
initCachingEnabled: s.InitCachingEnabled,
supervisor: processSupervisor{
ProcessSupervisor: s.Supervisor,
RootPath: s.RuntimeFsRootPath,
},
RuntimeStartedTime: -1,
RuntimeOverheadStartedTime: -1,
InvokeResponseMetrics: nil,
}
go startRuntimeAPI(ctx, execCtx)
return execCtx, registrationService.GetInternalStateDescriptor(appCtx), runtimeAPIAddr
}
func (r *rapidContext) HandleInit(init *interop.Init, initSuccessResponseChan chan<- interop.InitSuccess, initFailureResponseChan chan<- interop.InitFailure) {
r.handlerExecutionMutex.Lock()
defer r.handlerExecutionMutex.Unlock()
handleInit(r, init, initSuccessResponseChan, initFailureResponseChan)
}
func (r *rapidContext) HandleInvoke(invoke *interop.Invoke, sbInfoFromInit interop.SandboxInfoFromInit, requestBuffer *bytes.Buffer, responseSender interop.InvokeResponseSender) (interop.InvokeSuccess, *interop.InvokeFailure) {
r.handlerExecutionMutex.Lock()
defer r.handlerExecutionMutex.Unlock()
// Clear the context used by the last invoke
r.appCtx.Delete(appctx.AppCtxInvokeErrorTraceDataKey)
return handleInvoke(r, invoke, sbInfoFromInit, requestBuffer, responseSender)
}
func (r *rapidContext) HandleReset(reset *interop.Reset) (interop.ResetSuccess, *interop.ResetFailure) {
// 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
r.registrationService.CancelFlows(errResetReceived)
// Wait until invoke error handling has returned before continuing execution
r.handlerExecutionMutex.Lock()
defer r.handlerExecutionMutex.Unlock()
// Clear the context used by the last invoke
r.appCtx.Delete(appctx.AppCtxInvokeErrorTraceDataKey)
return handleReset(r, reset, r.RuntimeStartedTime, r.InvokeResponseMetrics)
}
func (r *rapidContext) HandleShutdown(shutdown *interop.Shutdown) interop.ShutdownSuccess {
// Wait until invoke error handling has returned before continuing execution
r.handlerExecutionMutex.Lock()
defer r.handlerExecutionMutex.Unlock()
// Shutdown doesn't cancel flows, so it can block forever
return handleShutdown(r, shutdown, standaloneShutdownReason)
}
func (r *rapidContext) HandleRestore(restore *interop.Restore) (interop.RestoreResult, error) {
return handleRestore(r, restore)
}
func (r *rapidContext) Clear() {
reinitialize(r)
}
func (r *rapidContext) SetRuntimeStartedTime(runtimeStartedTime int64) {
r.RuntimeStartedTime = runtimeStartedTime
}
func (r *rapidContext) SetRuntimeOverheadStartedTime(runtimeOverheadStartedTime int64) {
r.RuntimeOverheadStartedTime = runtimeOverheadStartedTime
}
func (r *rapidContext) SetInvokeResponseMetrics(metrics *interop.InvokeResponseMetrics) {
r.InvokeResponseMetrics = metrics
}
func (r *rapidContext) SetLogStreamName(logStreamName string) {
r.logStreamName = logStreamName
}
func (r *rapidContext) SetEventsAPI(eventsAPI interop.EventsAPI) {
r.eventsAPI = eventsAPI
}