This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Provisioner.js
300 lines (240 loc) · 13.1 KB
/
Provisioner.js
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
/* @flow */
/* eslint-disable max-len */
import ProvisionerConfigurableBase from './provisioning/ProvisionerConfigurableBase';
import RateLimitedDecrement from './utils/RateLimitedDecrement';
import Throughput from './utils/Throughput';
import ProvisionerLogging from './provisioning/ProvisionerLogging';
import { Region } from './configuration/Region';
import DefaultProvisioner from './configuration/DefaultProvisioner';
import { invariant } from './Global';
import type { TableProvisionedAndConsumedThroughput, ProvisionerConfig, AdjustmentContext } from './flow/FlowTypes';
export default class Provisioner extends ProvisionerConfigurableBase {
// Get the region
getDynamoDBRegion(): string {
return Region;
}
// Gets the list of tables which we want to autoscale
async getTableNamesAsync(): Promise<string[]> {
// Option 1 - All tables (Default)
return await this.db.listAllTableNamesAsync();
// Option 2 - Hardcoded list of tables
// return ['Table1', 'Table2', 'Table3'];
// Option 3 - DynamoDB / S3 configured list of tables
// return await ...;
}
// Gets the json settings which control how the specifed table will be autoscaled
// eslint-disable-next-line no-unused-vars
getTableConfig(data: TableProvisionedAndConsumedThroughput): ProvisionerConfig {
// Option 1 - Default settings for all tables
return DefaultProvisioner;
// Option 2 - Bespoke table specific settings
// return data.TableName === 'Table1' ? Climbing : Default;
// Option 3 - DynamoDB / S3 sourced table specific settings
// return await ...;
}
isReadCapacityIncrementRequired(data: TableProvisionedAndConsumedThroughput): boolean {
invariant(data != null, 'Parameter \'data\' is not set');
let config = this.getTableConfig(data);
let adjustmentContext = this.getReadCapacityIncrementAdjustmentContext(data, config);
return this.isCapacityAdjustmentRequired(data, adjustmentContext);
}
calculateIncrementedReadCapacityValue(data: TableProvisionedAndConsumedThroughput): number {
invariant(data != null, 'Parameter \'data\' is not set');
let config = this.getTableConfig(data);
let adjustmentContext = this.getReadCapacityIncrementAdjustmentContext(data, config);
return Throughput.getAdjustedCapacityUnits(adjustmentContext);
}
isReadCapacityDecrementRequired(data: TableProvisionedAndConsumedThroughput): boolean {
invariant(data != null, 'Parameter \'data\' is not set');
let config = this.getTableConfig(data);
let adjustmentContext = this.getReadCapacityDecrementAdjustmentContext(data, config);
return this.isCapacityAdjustmentRequired(data, adjustmentContext);
}
calculateDecrementedReadCapacityValue(data: TableProvisionedAndConsumedThroughput): number {
invariant(data != null, 'Parameter \'data\' is not set');
let config = this.getTableConfig(data);
let adjustmentContext = this.getReadCapacityDecrementAdjustmentContext(data, config);
return Throughput.getAdjustedCapacityUnits(adjustmentContext);
}
isWriteCapacityIncrementRequired(data: TableProvisionedAndConsumedThroughput): boolean {
invariant(data != null, 'Parameter \'data\' is not set');
let config = this.getTableConfig(data);
let adjustmentContext = this.getWriteCapacityIncrementAdjustmentContext(data, config);
return this.isCapacityAdjustmentRequired(data, adjustmentContext);
}
calculateIncrementedWriteCapacityValue(data: TableProvisionedAndConsumedThroughput): number {
invariant(data != null, 'Parameter \'data\' is not set');
let config = this.getTableConfig(data);
let adjustmentContext = this.getWriteCapacityIncrementAdjustmentContext(data, config);
return Throughput.getAdjustedCapacityUnits(adjustmentContext);
}
isWriteCapacityDecrementRequired(data: TableProvisionedAndConsumedThroughput): boolean {
invariant(data != null, 'Parameter \'data\' is not set');
let config = this.getTableConfig(data);
let adjustmentContext = this.getWriteCapacityDecrementAdjustmentContext(data, config);
return this.isCapacityAdjustmentRequired(data, adjustmentContext);
}
calculateDecrementedWriteCapacityValue(data: TableProvisionedAndConsumedThroughput): number {
invariant(data != null, 'Parameter \'data\' is not set');
let config = this.getTableConfig(data);
let adjustmentContext = this.getWriteCapacityDecrementAdjustmentContext(data, config);
return Throughput.getAdjustedCapacityUnits(adjustmentContext);
}
getReadCapacityIncrementAdjustmentContext(data: TableProvisionedAndConsumedThroughput, config: ProvisionerConfig): AdjustmentContext {
invariant(data != null, 'Argument \'data\' cannot be null');
invariant(config != null, 'Argument \'config\' cannot be null');
let context = {
TableName: data.TableName,
IndexName: data.IndexName,
CapacityType: 'read',
AdjustmentType: 'increment',
ProvisionedValue: data.ProvisionedThroughput.ReadCapacityUnits,
ConsumedValue: data.ConsumedThroughput.ReadCapacityUnits,
ThrottledEvents: data.ThrottledEvents.ThrottledReadEvents,
UtilisationPercent: (data.ConsumedThroughput.ReadCapacityUnits / data.ProvisionedThroughput.ReadCapacityUnits) * 100,
CapacityConfig: config.ReadCapacity,
};
if (config.ReadCapacity.Increment != null) {
// $FlowIgnore
context.CapacityAdjustmentConfig = config.ReadCapacity.Increment;
}
return context;
}
getReadCapacityDecrementAdjustmentContext(data: TableProvisionedAndConsumedThroughput, config: ProvisionerConfig): AdjustmentContext {
invariant(data != null, 'Argument \'data\' cannot be null');
invariant(config != null, 'Argument \'config\' cannot be null');
let context = {
TableName: data.TableName,
IndexName: data.IndexName,
CapacityType: 'read',
AdjustmentType: 'decrement',
ProvisionedValue: data.ProvisionedThroughput.ReadCapacityUnits,
ConsumedValue: data.ConsumedThroughput.ReadCapacityUnits,
ThrottledEvents: data.ThrottledEvents.ThrottledReadEvents,
UtilisationPercent: (data.ConsumedThroughput.ReadCapacityUnits / data.ProvisionedThroughput.ReadCapacityUnits) * 100,
CapacityConfig: config.ReadCapacity,
};
if (config.ReadCapacity.Decrement != null) {
// $FlowIgnore
context.CapacityAdjustmentConfig = config.ReadCapacity.Decrement;
}
return context;
}
getWriteCapacityIncrementAdjustmentContext(data: TableProvisionedAndConsumedThroughput, config: ProvisionerConfig): AdjustmentContext {
invariant(data != null, 'Argument \'data\' cannot be null');
invariant(config != null, 'Argument \'config\' cannot be null');
let context = {
TableName: data.TableName,
IndexName: data.IndexName,
CapacityType: 'write',
AdjustmentType: 'increment',
ProvisionedValue: data.ProvisionedThroughput.WriteCapacityUnits,
ConsumedValue: data.ConsumedThroughput.WriteCapacityUnits,
ThrottledEvents: data.ThrottledEvents.ThrottledWriteEvents,
UtilisationPercent: (data.ConsumedThroughput.WriteCapacityUnits / data.ProvisionedThroughput.WriteCapacityUnits) * 100,
CapacityConfig: config.WriteCapacity,
};
if (config.WriteCapacity.Increment != null) {
// $FlowIgnore
context.CapacityAdjustmentConfig = config.WriteCapacity.Increment;
}
return context;
}
getWriteCapacityDecrementAdjustmentContext(data: TableProvisionedAndConsumedThroughput, config: ProvisionerConfig): AdjustmentContext {
invariant(data != null, 'Argument \'data\' cannot be null');
invariant(config != null, 'Argument \'config\' cannot be null');
let context = {
TableName: data.TableName,
IndexName: data.IndexName,
CapacityType: 'write',
AdjustmentType: 'decrement',
ProvisionedValue: data.ProvisionedThroughput.WriteCapacityUnits,
ConsumedValue: data.ConsumedThroughput.WriteCapacityUnits,
ThrottledEvents: data.ThrottledEvents.ThrottledWriteEvents,
UtilisationPercent: (data.ConsumedThroughput.WriteCapacityUnits / data.ProvisionedThroughput.WriteCapacityUnits) * 100,
CapacityConfig: config.WriteCapacity,
};
if (config.WriteCapacity.Decrement != null) {
// $FlowIgnore
context.CapacityAdjustmentConfig = config.WriteCapacity.Decrement;
}
return context;
}
isCapacityAdjustmentRequired(data: TableProvisionedAndConsumedThroughput, adjustmentContext: AdjustmentContext): boolean {
// Determine if an adjustment is wanted
let isProvAboveMax = adjustmentContext.CapacityConfig.Max == null ? false : adjustmentContext.ProvisionedValue > adjustmentContext.CapacityConfig.Max;
let isProvBelowMax = adjustmentContext.CapacityConfig.Max == null ? true : adjustmentContext.ProvisionedValue < adjustmentContext.CapacityConfig.Max;
let isProvBelowMin = adjustmentContext.CapacityConfig.Min == null ? adjustmentContext.ProvisionedValue < 1 : adjustmentContext.ProvisionedValue < adjustmentContext.CapacityConfig.Min;
let isProvAboveMin = adjustmentContext.CapacityConfig.Min == null ? adjustmentContext.ProvisionedValue > 1 : adjustmentContext.ProvisionedValue > adjustmentContext.CapacityConfig.Min;
let isUtilAboveThreshold = this.isAboveThreshold(adjustmentContext);
let isUtilBelowThreshold = this.isBelowThreshold(adjustmentContext);
let isThrottledEventsAboveThreshold = this.isThrottledEventsAboveThreshold(adjustmentContext);
let isAdjustmentWanted = adjustmentContext.AdjustmentType === 'increment' ?
(isProvBelowMin || isUtilAboveThreshold || isUtilBelowThreshold || isThrottledEventsAboveThreshold) && isProvBelowMax :
(isProvAboveMax || isUtilAboveThreshold || isUtilBelowThreshold) && isProvAboveMin;
// Determine if an adjustment is allowed under the rate limiting rules
let isAfterLastDecreaseGracePeriod = adjustmentContext.CapacityAdjustmentConfig == null ||
this.isAfterLastAdjustmentGracePeriod(data.ProvisionedThroughput.LastDecreaseDateTime,
adjustmentContext.CapacityAdjustmentConfig.When.AfterLastDecrementMinutes);
let isAfterLastIncreaseGracePeriod = adjustmentContext.CapacityAdjustmentConfig == null ||
this.isAfterLastAdjustmentGracePeriod(data.ProvisionedThroughput.LastIncreaseDateTime,
adjustmentContext.CapacityAdjustmentConfig.When.AfterLastIncrementMinutes);
let isReadDecrementAllowed = adjustmentContext.AdjustmentType === 'decrement' ?
RateLimitedDecrement.isDecrementAllowed(data, adjustmentContext, d => this.calculateDecrementedReadCapacityValue(d)) :
true;
let isAdjustmentAllowed = isAfterLastDecreaseGracePeriod && isAfterLastIncreaseGracePeriod && isReadDecrementAllowed;
// Package up the configuration and the results so that we can produce
// some effective logs
let adjustmentData = {
isAboveMax: isProvAboveMax,
isBelowMin: isProvBelowMin,
isAboveThreshold: isUtilAboveThreshold,
isBelowThreshold: isUtilBelowThreshold,
isAboveThrottledEventThreshold: isThrottledEventsAboveThreshold,
isAfterLastDecreaseGracePeriod,
isAfterLastIncreaseGracePeriod,
isAdjustmentWanted,
isAdjustmentAllowed
};
// Log and return result
ProvisionerLogging.isAdjustmentRequiredLog(adjustmentContext, adjustmentData);
return isAdjustmentWanted && isAdjustmentAllowed;
}
isThrottledEventsAboveThreshold(context: AdjustmentContext): boolean {
invariant(context != null, 'Parameter \'context\' is not set');
if (context.CapacityAdjustmentConfig == null ||
context.CapacityAdjustmentConfig.When.ThrottledEventsPerMinuteIsAbove == null ||
context.AdjustmentType === 'decrement') {
return false;
}
return context.ThrottledEvents >
context.CapacityAdjustmentConfig.When.ThrottledEventsPerMinuteIsAbove;
}
isAboveThreshold(context: AdjustmentContext): boolean {
invariant(context != null, 'Parameter \'context\' is not set');
if (context.CapacityAdjustmentConfig == null ||
context.CapacityAdjustmentConfig.When.UtilisationIsAbovePercent == null) {
return false;
}
let utilisationPercent = (context.ConsumedValue / context.ProvisionedValue) * 100;
return utilisationPercent > context.CapacityAdjustmentConfig.When.UtilisationIsAbovePercent;
}
isBelowThreshold(context: AdjustmentContext): boolean {
invariant(context != null, 'Parameter \'context\' is not set');
if (context.CapacityAdjustmentConfig == null ||
context.CapacityAdjustmentConfig.When.UtilisationIsBelowPercent == null) {
return false;
}
let utilisationPercent = (context.ConsumedValue / context.ProvisionedValue) * 100;
return utilisationPercent < context.CapacityAdjustmentConfig.When.UtilisationIsBelowPercent;
}
isAfterLastAdjustmentGracePeriod(lastAdjustmentDateTime: string, afterLastAdjustmentMinutes?: number): boolean {
if (lastAdjustmentDateTime == null || afterLastAdjustmentMinutes == null) {
return true;
}
let lastDecreaseDateTime = new Date(Date.parse(lastAdjustmentDateTime));
let thresholdDateTime = new Date(Date.now());
thresholdDateTime.setMinutes(thresholdDateTime.getMinutes() - (afterLastAdjustmentMinutes));
return lastDecreaseDateTime < thresholdDateTime;
}
}