-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathhandlers.go
240 lines (206 loc) · 8.12 KB
/
handlers.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
"math"
"net/http"
"os"
"strconv"
"strings"
"time"
"go.amzn.com/lambda/core/statejson"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/rapidcore"
"go.amzn.com/lambda/rapidcore/env"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
type Sandbox interface {
Init(i *interop.Init, invokeTimeoutMs int64)
Invoke(responseWriter http.ResponseWriter, invoke *interop.Invoke) error
}
type InteropServer interface {
Init(i *interop.Init, invokeTimeoutMs int64) error
AwaitInitialized() error
FastInvoke(w http.ResponseWriter, i *interop.Invoke, direct bool) error
Reserve(id string, traceID, lambdaSegmentID string) (*rapidcore.ReserveResponse, error)
Reset(reason string, timeoutMs int64) (*statejson.ResetDescription, error)
AwaitRelease() (*statejson.InternalStateDescription, error)
Shutdown(shutdown *interop.Shutdown) *statejson.InternalStateDescription
InternalState() (*statejson.InternalStateDescription, error)
CurrentToken() *interop.Token
Restore(restore *interop.Restore) error
}
var initDone bool
func GetenvWithDefault(key string, defaultValue string) string {
envValue := os.Getenv(key)
if envValue == "" {
return defaultValue
}
return envValue
}
func GetFunctionName() string {
defaultValue := "function"
if GetenvWithDefault("AWS_LAMBDA_RIE_DYNAMIC_FUNCTION_URL", "FALSE") == "FALSE" {
defaultValue = "test_function"
}
return GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", defaultValue)
}
func printEndReports(invokeId string, initDuration string, memorySize string, invokeStart time.Time, timeoutDuration time.Duration) {
// Calcuation invoke duration
invokeDuration := math.Min(float64(time.Now().Sub(invokeStart).Nanoseconds()),
float64(timeoutDuration.Nanoseconds())) / float64(time.Millisecond)
fmt.Println("END RequestId: " + invokeId)
// We set the Max Memory Used and Memory Size to be the same (whatever it is set to) since there is
// not a clean way to get this information from rapidcore
fmt.Printf(
"REPORT RequestId: %s\t"+
initDuration+
"Duration: %.2f ms\t"+
"Billed Duration: %.f ms\t"+
"Memory Size: %s MB\t"+
"Max Memory Used: %s MB\t\n",
invokeId, invokeDuration, math.Ceil(invokeDuration), memorySize, memorySize)
}
func InvokeHandler(w http.ResponseWriter, r *http.Request, sandbox Sandbox, bs interop.Bootstrap) {
log.Debugf("invoke: -> %s %s %v", r.Method, r.URL, r.Header)
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Errorf("Failed to read invoke body: %s", err)
w.WriteHeader(500)
return
}
rawClientContext, err := base64.StdEncoding.DecodeString(r.Header.Get("X-Amz-Client-Context"))
if err != nil {
log.Errorf("Failed to decode X-Amz-Client-Context: %s", err)
w.WriteHeader(500)
return
}
initDuration := ""
inv := GetenvWithDefault("AWS_LAMBDA_FUNCTION_TIMEOUT", "300")
timeoutDuration, _ := time.ParseDuration(inv + "s")
// Default
timeout, err := strconv.ParseInt(inv, 10, 64)
if err != nil {
panic(err)
}
functionVersion := GetenvWithDefault("AWS_LAMBDA_FUNCTION_VERSION", "$LATEST")
memorySize := GetenvWithDefault("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "3008")
if !initDone {
initStart, initEnd := InitHandler(sandbox, functionVersion, timeout, bs)
// Calculate InitDuration
initTimeMS := math.Min(float64(initEnd.Sub(initStart).Nanoseconds()),
float64(timeoutDuration.Nanoseconds())) / float64(time.Millisecond)
initDuration = fmt.Sprintf("Init Duration: %.2f ms\t", initTimeMS)
// Set initDone so next invokes do not try to Init the function again
initDone = true
}
invokeStart := time.Now()
invokePayload := &interop.Invoke{
ID: uuid.New().String(),
InvokedFunctionArn: fmt.Sprintf("arn:aws:lambda:us-east-1:012345678912:function:%s", GetFunctionName()),
TraceID: r.Header.Get("X-Amzn-Trace-Id"),
LambdaSegmentID: r.Header.Get("X-Amzn-Segment-Id"),
Payload: bytes.NewReader(bodyBytes),
ClientContext: string(rawClientContext),
}
fmt.Println("START RequestId: " + invokePayload.ID + " Version: " + functionVersion)
// If we write to 'w' directly and waitUntilRelease fails, we won't be able to propagate error anymore
invokeResp := &ResponseWriterProxy{}
if err := sandbox.Invoke(invokeResp, invokePayload); err != nil {
switch err {
// Reserve errors:
case rapidcore.ErrAlreadyReserved:
log.Errorf("Failed to reserve: %s", err)
w.WriteHeader(http.StatusBadRequest)
return
case rapidcore.ErrInternalServerError:
w.WriteHeader(http.StatusInternalServerError)
return
case rapidcore.ErrInitDoneFailed:
w.WriteHeader(http.StatusBadGateway)
w.Write(invokeResp.Body)
return
case rapidcore.ErrReserveReservationDone:
// TODO use http.StatusBadGateway
w.WriteHeader(http.StatusGatewayTimeout)
return
// Invoke errors:
case rapidcore.ErrNotReserved:
case rapidcore.ErrAlreadyReplied:
case rapidcore.ErrAlreadyInvocating:
log.Errorf("Failed to set reply stream: %s", err)
w.WriteHeader(http.StatusBadRequest)
return
case rapidcore.ErrInvokeReservationDone:
// TODO use http.StatusBadGateway
w.WriteHeader(http.StatusGatewayTimeout)
return
case rapidcore.ErrInvokeResponseAlreadyWritten:
return
// AwaitRelease errors:
case rapidcore.ErrInvokeDoneFailed:
w.WriteHeader(http.StatusBadGateway)
w.Write(invokeResp.Body)
return
case rapidcore.ErrReleaseReservationDone:
// TODO return sandbox status when we implement async reset handling
// TODO use http.StatusOK
w.WriteHeader(http.StatusGatewayTimeout)
return
case rapidcore.ErrInvokeTimeout:
printEndReports(invokePayload.ID, initDuration, memorySize, invokeStart, timeoutDuration)
w.Write([]byte(fmt.Sprintf("Task timed out after %d.00 seconds", timeout)))
time.Sleep(100 * time.Millisecond)
//initDone = false
return
}
}
printEndReports(invokePayload.ID, initDuration, memorySize, invokeStart, timeoutDuration)
if invokeResp.StatusCode != 0 {
w.WriteHeader(invokeResp.StatusCode)
}
w.Write(invokeResp.Body)
}
func InitHandler(sandbox Sandbox, functionVersion string, timeout int64, bs interop.Bootstrap) (time.Time, time.Time) {
additionalFunctionEnvironmentVariables := map[string]string{}
// Add default Env Vars if they were not defined. This is a required otherwise 1p Python2.7, Python3.6, and
// possibly others pre runtime API runtimes will fail. This will be overwritten if they are defined on the system.
additionalFunctionEnvironmentVariables["AWS_LAMBDA_LOG_GROUP_NAME"] = "/aws/lambda/Functions"
additionalFunctionEnvironmentVariables["AWS_LAMBDA_LOG_STREAM_NAME"] = "$LATEST"
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_VERSION"] = "$LATEST"
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_MEMORY_SIZE"] = "3008"
additionalFunctionEnvironmentVariables["AWS_LAMBDA_FUNCTION_NAME"] = GetFunctionName()
// Forward Env Vars from the running system (container) to what the function can view. Without this, Env Vars will
// not be viewable when the function runs.
for _, env := range os.Environ() {
// Split the env into by the first "=". This will account for if the env var's value has a '=' in it
envVar := strings.SplitN(env, "=", 2)
additionalFunctionEnvironmentVariables[envVar[0]] = envVar[1]
}
initStart := time.Now()
// pass to rapid
sandbox.Init(&interop.Init{
Handler: GetenvWithDefault("AWS_LAMBDA_FUNCTION_HANDLER", os.Getenv("_HANDLER")),
AwsKey: os.Getenv("AWS_ACCESS_KEY_ID"),
AwsSecret: os.Getenv("AWS_SECRET_ACCESS_KEY"),
AwsSession: os.Getenv("AWS_SESSION_TOKEN"),
XRayDaemonAddress: "0.0.0.0:0", // TODO
FunctionName: GetFunctionName(),
FunctionVersion: functionVersion,
RuntimeInfo: interop.RuntimeInfo{
ImageJSON: "{}",
Arn: "",
Version: ""},
CustomerEnvironmentVariables: additionalFunctionEnvironmentVariables,
SandboxType: interop.SandboxClassic,
Bootstrap: bs,
EnvironmentVariables: env.NewEnvironment(),
}, timeout*1000)
initEnd := time.Now()
return initStart, initEnd
}