-
Notifications
You must be signed in to change notification settings - Fork 554
/
system_status.ts
282 lines (255 loc) · 9.55 KB
/
system_status.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
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
import { weightedAvg } from '@crawlee/utils';
import ow from 'ow';
import { Snapshotter } from './snapshotter';
import type { Configuration } from '../configuration';
/**
* Represents the current status of the system.
*/
export interface SystemInfo {
/** If true, system is being overloaded. */
isSystemIdle: boolean;
memInfo: ClientInfo;
eventLoopInfo: ClientInfo;
cpuInfo: ClientInfo;
clientInfo: ClientInfo;
memCurrentBytes?: number;
/**
* Platform only property
* @internal
*/
cpuCurrentUsage?: number;
/**
* Platform only property
* @internal
*/
isCpuOverloaded?: boolean;
/**
* Platform only property
* @internal
*/
createdAt?: Date;
}
export interface SystemStatusOptions {
/**
* Defines max age of snapshots used in the {@apilink SystemStatus.getCurrentStatus} measurement.
* @default 5
*/
currentHistorySecs?: number;
/**
* Sets the maximum ratio of overloaded snapshots in a memory sample.
* If the sample exceeds this ratio, the system will be overloaded.
* @default 0.2
*/
maxMemoryOverloadedRatio?: number;
/**
* Sets the maximum ratio of overloaded snapshots in an event loop sample.
* If the sample exceeds this ratio, the system will be overloaded.
* @default 0.6
*/
maxEventLoopOverloadedRatio?: number;
/**
* Sets the maximum ratio of overloaded snapshots in a CPU sample.
* If the sample exceeds this ratio, the system will be overloaded.
* @default 0.4
*/
maxCpuOverloadedRatio?: number;
/**
* Sets the maximum ratio of overloaded snapshots in a Client sample.
* If the sample exceeds this ratio, the system will be overloaded.
* @default 0.3
*/
maxClientOverloadedRatio?: number;
/**
* The `Snapshotter` instance to be queried for `SystemStatus`.
*/
snapshotter?: Snapshotter;
/** @internal */
config?: Configuration;
}
export interface ClientInfo {
isOverloaded: boolean;
limitRatio: number;
actualRatio: number;
}
export interface FinalStatistics {
requestsFinished: number;
requestsFailed: number;
retryHistogram: number[];
requestAvgFailedDurationMillis: number;
requestAvgFinishedDurationMillis: number;
requestsFinishedPerMinute: number;
requestsFailedPerMinute: number;
requestTotalDurationMillis: number;
requestsTotal: number;
crawlerRuntimeMillis: number;
}
/**
* Provides a simple interface to reading system status from a {@apilink Snapshotter} instance.
* It only exposes two functions {@apilink SystemStatus.getCurrentStatus}
* and {@apilink SystemStatus.getHistoricalStatus}.
* The system status is calculated using a weighted average of overloaded
* messages in the snapshots, with the weights being the time intervals
* between the snapshots. Each resource is calculated separately
* and the system is overloaded whenever at least one resource is overloaded.
* The class is used by the {@apilink AutoscaledPool} class.
*
* {@apilink SystemStatus.getCurrentStatus}
* returns a boolean that represents the current status of the system.
* The length of the current timeframe in seconds is configurable
* by the `currentHistorySecs` option and represents the max age
* of snapshots to be considered for the calculation.
*
* {@apilink SystemStatus.getHistoricalStatus}
* returns a boolean that represents the long-term status
* of the system. It considers the full snapshot history available
* in the {@apilink Snapshotter} instance.
* @category Scaling
*/
export class SystemStatus {
private readonly currentHistorySecs: number;
private readonly maxMemoryOverloadedRatio: number;
private readonly maxEventLoopOverloadedRatio: number;
private readonly maxCpuOverloadedRatio: number;
private readonly maxClientOverloadedRatio: number;
private readonly snapshotter: Snapshotter;
constructor(options: SystemStatusOptions = {}) {
ow(options, ow.object.exactShape({
currentHistorySecs: ow.optional.number,
maxMemoryOverloadedRatio: ow.optional.number,
maxEventLoopOverloadedRatio: ow.optional.number,
maxCpuOverloadedRatio: ow.optional.number,
maxClientOverloadedRatio: ow.optional.number,
snapshotter: ow.optional.object,
config: ow.optional.object,
}));
const {
currentHistorySecs = 5,
maxMemoryOverloadedRatio = 0.2,
maxEventLoopOverloadedRatio = 0.6,
maxCpuOverloadedRatio = 0.4,
maxClientOverloadedRatio = 0.3,
snapshotter,
config,
} = options;
this.currentHistorySecs = currentHistorySecs * 1000;
this.maxMemoryOverloadedRatio = maxMemoryOverloadedRatio;
this.maxEventLoopOverloadedRatio = maxEventLoopOverloadedRatio;
this.maxCpuOverloadedRatio = maxCpuOverloadedRatio;
this.maxClientOverloadedRatio = maxClientOverloadedRatio;
this.snapshotter = snapshotter || new Snapshotter({ config });
}
/**
* Returns an {@apilink SystemInfo} object with the following structure:
*
* ```javascript
* {
* isSystemIdle: Boolean,
* memInfo: Object,
* eventLoopInfo: Object,
* cpuInfo: Object
* }
* ```
*
* Where the `isSystemIdle` property is set to `false` if the system
* has been overloaded in the last `options.currentHistorySecs` seconds,
* and `true` otherwise.
*/
getCurrentStatus(): SystemInfo {
return this._isSystemIdle(this.currentHistorySecs);
}
/**
* Returns an {@apilink SystemInfo} object with the following structure:
*
* ```javascript
* {
* isSystemIdle: Boolean,
* memInfo: Object,
* eventLoopInfo: Object,
* cpuInfo: Object
* }
* ```
*
* Where the `isSystemIdle` property is set to `false` if the system
* has been overloaded in the full history of the {@apilink Snapshotter}
* (which is configurable in the {@apilink Snapshotter}) and `true` otherwise.
*/
getHistoricalStatus(): SystemInfo {
return this._isSystemIdle();
}
/**
* Returns a system status object.
*/
protected _isSystemIdle(sampleDurationMillis?: number): SystemInfo {
const memInfo = this._isMemoryOverloaded(sampleDurationMillis);
const eventLoopInfo = this._isEventLoopOverloaded(sampleDurationMillis);
const cpuInfo = this._isCpuOverloaded(sampleDurationMillis);
const clientInfo = this._isClientOverloaded(sampleDurationMillis);
return {
isSystemIdle: !memInfo.isOverloaded && !eventLoopInfo.isOverloaded && !cpuInfo.isOverloaded && !clientInfo.isOverloaded,
memInfo,
eventLoopInfo,
cpuInfo,
clientInfo,
};
}
/**
* Returns an object with an isOverloaded property set to true
* if the memory has been overloaded in the last sampleDurationMillis.
*/
protected _isMemoryOverloaded(sampleDurationMillis?: number) {
const sample = this.snapshotter.getMemorySample(sampleDurationMillis);
return this._isSampleOverloaded(sample, this.maxMemoryOverloadedRatio);
}
/**
* Returns an object with an isOverloaded property set to true
* if the event loop has been overloaded in the last sampleDurationMillis.
*/
protected _isEventLoopOverloaded(sampleDurationMillis?: number) {
const sample = this.snapshotter.getEventLoopSample(sampleDurationMillis);
return this._isSampleOverloaded(sample, this.maxEventLoopOverloadedRatio);
}
/**
* Returns an object with an isOverloaded property set to true
* if the CPU has been overloaded in the last sampleDurationMillis.
*/
protected _isCpuOverloaded(sampleDurationMillis?: number) {
const sample = this.snapshotter.getCpuSample(sampleDurationMillis);
return this._isSampleOverloaded(sample, this.maxCpuOverloadedRatio);
}
/**
* Returns an object with an isOverloaded property set to true
* if the client has been overloaded in the last sampleDurationMillis.
*/
protected _isClientOverloaded(sampleDurationMillis?: number): ClientInfo {
const sample = this.snapshotter.getClientSample(sampleDurationMillis);
return this._isSampleOverloaded(sample, this.maxClientOverloadedRatio);
}
/**
* Returns an object with sample information and an isOverloaded property
* set to true if at least the ratio of snapshots in the sample are overloaded.
*/
protected _isSampleOverloaded<T extends { createdAt: Date; isOverloaded: boolean }>(sample: T[], ratio: number): ClientInfo {
if (sample.length === 0) {
return {
isOverloaded: false,
limitRatio: ratio,
actualRatio: 0,
};
}
const weights: number[] = [];
const values: number[] = [];
for (let i = 1; i < sample.length; i++) {
const previous = sample[i - 1];
const current = sample[i];
const weight = +current.createdAt - +previous.createdAt;
weights.push(weight || 1); // Prevent errors from 0ms long intervals (sync) between snapshots.
values.push(+current.isOverloaded);
}
const wAvg = sample.length === 1 ? +sample[0].isOverloaded : weightedAvg(values, weights);
return {
isOverloaded: wAvg > ratio,
limitRatio: ratio,
actualRatio: Math.round(wAvg * 1000) / 1000,
};
}
}