-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathlambda.ts
190 lines (169 loc) · 6.51 KB
/
lambda.ts
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
import { ActionRequestMessage, RetryableScalingError, scaleUp as scaleUpR } from './scale-runners/scale-up';
import { Context, SQSEvent, SQSRecord, ScheduledEvent } from 'aws-lambda';
import { Config } from './scale-runners/config';
import { ScaleUpMetrics, ScaleUpChronMetrics, sendMetricsAtTimeout, sendMetricsTimeoutVars } from './scale-runners/metrics';
import { getDelayWithJitterRetryCount, stochaticRunOvershoot } from './scale-runners/utils';
import { scaleDown as scaleDownR } from './scale-runners/scale-down';
import { scaleUpChron as scaleUpChronR } from './scale-runners/scale-up-chron';
import { sqsSendMessages, sqsDeleteMessageBatch } from './scale-runners/sqs';
async function sendRetryEvents(evtFailed: Array<[SQSRecord, boolean, number]>, metrics: ScaleUpMetrics) {
console.error(`Detected ${evtFailed.length} errors when processing messages, will retry relevant messages.`);
metrics.exception();
const messagesToSend: Array<ActionRequestMessage> = [];
console.dir(evtFailed);
for (const [evt, retryable, incRetry] of evtFailed) {
const body: ActionRequestMessage = JSON.parse(evt.body);
const retryCount = body?.retryCount ?? 0;
if (
retryCount < Config.Instance.maxRetryScaleUpRecord &&
(Config.Instance.retryScaleUpRecordQueueUrl?.length ?? 0) > 0
) {
if (incRetry > 0) {
if (retryable) {
metrics.scaleUpFailureRetryable(retryCount);
} else {
metrics.scaleUpFailureNonRetryable(retryCount);
}
} else {
metrics.stochasticOvershoot();
}
body.retryCount = retryCount + incRetry;
body.delaySeconds = Math.min(
900,
getDelayWithJitterRetryCount(
retryCount,
Math.max(Config.Instance.retryScaleUpRecordDelayS, 20),
Config.Instance.retryScaleUpRecordJitterPct,
),
);
messagesToSend.push(body);
console.warn(`Queued message ${body}`);
} else {
console.error(`Permanently abandoning message: ${evt.body}`);
}
}
if (messagesToSend.length) {
sqsSendMessages(metrics, messagesToSend, Config.Instance.retryScaleUpRecordQueueUrl as string);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function scaleUp(event: SQSEvent, context: Context, callback: any) {
// we mantain open connections to redis, so the event pool is only cleaned when the SIGTERM is sent
context.callbackWaitsForEmptyEventLoop = false;
let success = false;
const metrics = new ScaleUpMetrics();
const sndMetricsTimout: sendMetricsTimeoutVars = {
metrics: metrics,
};
sndMetricsTimout.setTimeout = setTimeout(
sendMetricsAtTimeout(sndMetricsTimout),
(Config.Instance.lambdaTimeout - 10) * 1000,
);
const evtFailed: Array<[SQSRecord, boolean, number]> = [];
try {
recordsIterProcess: for (let i = 0; i < event.Records.length; i += 1) {
const evt = event.Records[i];
try {
const body = JSON.parse(evt.body) as ActionRequestMessage;
if (
!stochaticRunOvershoot(body?.retryCount ?? 0, 900, Math.max(Config.Instance.retryScaleUpRecordDelayS, 20))
) {
console.warn(`Skipping message due to stochatic run overshoot: ${evt.body}`);
evtFailed.push([evt, true, 0]);
} else {
await scaleUpR(evt.eventSource, body, metrics);
metrics.scaleUpSuccess();
}
} catch (e) {
if (e instanceof RetryableScalingError) {
console.error(`Retryable error thrown: "${e.message}"`);
evtFailed.push([evt, true, 1]);
} else {
console.error(`Non-retryable error during request: "${(e as Error).message}"`);
console.error(`All remaning '${event.Records.length - i}' messages will be scheduled to retry`);
for (let ii = i; ii < event.Records.length; ii += 1) {
evtFailed.push([event.Records[ii], false, 1]);
}
break recordsIterProcess;
}
}
}
if (evtFailed.length > 0) {
await sendRetryEvents(evtFailed, metrics);
}
success = evtFailed.every((i) => {
return i[1];
});
} catch (e) {
console.error(e);
} finally {
if (!success) {
// In this case the framework does nothing and exits in a dirty state, this makes the message currently being
// processed to stay in-flight and jam the processing of the other messages due the FIFO nature of the SQS queue
// so a manual cleanup is required
if (evtFailed.length > 0) {
try {
await sqsDeleteMessageBatch(metrics, event.Records);
metrics.scaleUpDeleteMessageSuccess(evtFailed.length);
} catch (e) {
console.error(`FAILED TO DELETE PROCESSED MESSAGES: ${e}`);
metrics.scaleUpDeleteMessageFailure(evtFailed.length);
}
}
}
try {
clearTimeout(sndMetricsTimout.setTimeout);
sndMetricsTimout.metrics = undefined;
sndMetricsTimout.setTimeout = undefined;
await metrics.sendMetrics();
} catch (e) {
console.error(`Error sending metrics: ${e}`);
}
}
if (success) {
callback(null);
} else {
callback('Failed handling SQS event');
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function scaleDown(event: ScheduledEvent, context: Context, callback: any) {
// we mantain open connections to redis, so the event pool is only cleaned when the SIGTERM is sent
context.callbackWaitsForEmptyEventLoop = false;
try {
await scaleDownR();
return callback(null);
} catch (e) {
console.error(e);
return callback('Failed');
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function scaleUpChron(event: ScheduledEvent, context: Context, callback: any) {
// we mantain open connections to redis, so the event pool is only cleaned when the SIGTERM is sent
context.callbackWaitsForEmptyEventLoop = false;
const metrics = new ScaleUpChronMetrics();
const sndMetricsTimout: sendMetricsTimeoutVars = {
metrics: metrics,
};
sndMetricsTimout.setTimeout = setTimeout(
sendMetricsAtTimeout(sndMetricsTimout),
(Config.Instance.lambdaTimeout - 10) * 1000,
);
try {
await scaleUpChronR(metrics);
return callback(null);
} catch (e) {
console.error(e);
return callback('Failed');
} finally {
try {
clearTimeout(sndMetricsTimout.setTimeout);
sndMetricsTimout.metrics = undefined;
sndMetricsTimout.setTimeout = undefined;
await metrics.sendMetrics();
} catch (e) {
console.error(`Error sending metrics: ${e}`);
}
}
}