@crawlee/core 4.0.0-beta.11 → 4.0.0-beta.110
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -13
- package/autoscaling/autoscaled_pool.d.ts +67 -172
- package/autoscaling/autoscaled_pool.js +165 -320
- package/autoscaling/client_load_signal.d.ts +55 -0
- package/autoscaling/client_load_signal.js +73 -0
- package/autoscaling/concurrency_system.d.ts +268 -0
- package/autoscaling/concurrency_system.js +351 -0
- package/autoscaling/cpu_load_signal.d.ts +43 -0
- package/autoscaling/cpu_load_signal.js +46 -0
- package/autoscaling/event_loop_load_signal.d.ts +51 -0
- package/autoscaling/event_loop_load_signal.js +60 -0
- package/autoscaling/index.d.ts +6 -1
- package/autoscaling/index.js +6 -1
- package/autoscaling/load_signal.d.ts +99 -0
- package/autoscaling/load_signal.js +104 -0
- package/autoscaling/memory_load_signal.d.ts +47 -0
- package/autoscaling/memory_load_signal.js +105 -0
- package/autoscaling/snapshotter.d.ts +58 -163
- package/autoscaling/snapshotter.js +45 -263
- package/autoscaling/system_status.d.ts +62 -84
- package/autoscaling/system_status.js +92 -122
- package/autoscaling/weighted_avg.d.ts +5 -0
- package/autoscaling/weighted_avg.js +14 -0
- package/byte_utils.d.ts +17 -0
- package/byte_utils.js +42 -0
- package/configuration.d.ts +96 -223
- package/configuration.js +170 -222
- package/cookie_utils.d.ts +4 -3
- package/cookie_utils.js +22 -13
- package/crawlers/context_pipeline.d.ts +10 -1
- package/crawlers/context_pipeline.js +31 -8
- package/crawlers/crawler_commons.d.ts +90 -83
- package/crawlers/crawler_commons.js +1 -116
- package/crawlers/error_snapshotter.d.ts +1 -4
- package/crawlers/error_snapshotter.js +3 -6
- package/crawlers/error_tracker.d.ts +0 -1
- package/crawlers/error_tracker.js +0 -1
- package/crawlers/index.d.ts +1 -3
- package/crawlers/index.js +0 -3
- package/crawlers/internals/types.d.ts +0 -1
- package/crawlers/internals/types.js +0 -1
- package/crawlers/statistics.d.ts +91 -41
- package/crawlers/statistics.js +83 -72
- package/debug.d.ts +36 -0
- package/debug.js +70 -0
- package/enqueue_links/enqueue_links.d.ts +59 -68
- package/enqueue_links/enqueue_links.js +57 -62
- package/enqueue_links/index.d.ts +0 -1
- package/enqueue_links/index.js +0 -1
- package/enqueue_links/shared.d.ts +39 -26
- package/enqueue_links/shared.js +89 -67
- package/errors.d.ts +41 -4
- package/errors.js +50 -5
- package/events/event_manager.d.ts +34 -8
- package/events/event_manager.js +8 -10
- package/events/index.d.ts +0 -1
- package/events/index.js +0 -1
- package/events/local_event_manager.d.ts +15 -3
- package/events/local_event_manager.js +37 -11
- package/index.d.ts +6 -4
- package/index.js +5 -3
- package/iterables.d.ts +79 -0
- package/iterables.js +134 -0
- package/log.d.ts +82 -3
- package/log.js +106 -1
- package/memory-storage/consts.d.ts +4 -0
- package/memory-storage/consts.js +4 -0
- package/memory-storage/index.d.ts +1 -0
- package/memory-storage/index.js +1 -0
- package/memory-storage/memory-storage.d.ts +42 -0
- package/memory-storage/memory-storage.js +136 -0
- package/memory-storage/resource-clients/common/base-client.d.ts +4 -0
- package/memory-storage/resource-clients/common/base-client.js +6 -0
- package/memory-storage/resource-clients/dataset.d.ts +40 -0
- package/memory-storage/resource-clients/dataset.js +114 -0
- package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
- package/memory-storage/resource-clients/key-value-store.js +204 -0
- package/memory-storage/resource-clients/request-queue.d.ts +77 -0
- package/memory-storage/resource-clients/request-queue.js +422 -0
- package/memory-storage/utils.d.ts +16 -0
- package/memory-storage/utils.js +41 -0
- package/owned_or_injected.d.ts +58 -0
- package/owned_or_injected.js +98 -0
- package/package.json +13 -12
- package/proxy_configuration.d.ts +27 -152
- package/proxy_configuration.js +27 -179
- package/recoverable_state.d.ts +111 -0
- package/recoverable_state.js +143 -0
- package/request.d.ts +86 -17
- package/request.js +117 -41
- package/router.d.ts +193 -21
- package/router.js +188 -43
- package/serialization.d.ts +0 -1
- package/serialization.js +9 -11
- package/service_locator.d.ts +147 -0
- package/service_locator.js +244 -0
- package/session_pool/consts.d.ts +1 -2
- package/session_pool/consts.js +1 -2
- package/session_pool/errors.d.ts +0 -1
- package/session_pool/errors.js +0 -1
- package/session_pool/fingerprint.d.ts +9 -0
- package/session_pool/fingerprint.js +30 -0
- package/session_pool/index.d.ts +0 -2
- package/session_pool/index.js +0 -2
- package/session_pool/session.d.ts +37 -86
- package/session_pool/session.js +85 -138
- package/session_pool/session_pool.d.ts +77 -91
- package/session_pool/session_pool.js +159 -147
- package/storages/dataset.d.ts +114 -54
- package/storages/dataset.js +285 -144
- package/storages/index.d.ts +9 -8
- package/storages/index.js +7 -8
- package/storages/key_value_store.d.ts +185 -42
- package/storages/key_value_store.js +424 -151
- package/storages/key_value_store_codec.d.ts +32 -0
- package/storages/key_value_store_codec.js +113 -0
- package/storages/request_dedup_cache.d.ts +22 -0
- package/storages/request_dedup_cache.js +48 -0
- package/storages/request_list.d.ts +52 -116
- package/storages/request_list.js +158 -133
- package/storages/request_loader.d.ts +96 -0
- package/storages/request_loader.js +1 -0
- package/storages/request_manager.d.ts +33 -0
- package/storages/request_manager.js +1 -0
- package/storages/request_manager_tandem.d.ts +97 -0
- package/storages/request_manager_tandem.js +197 -0
- package/storages/request_queue.d.ts +290 -47
- package/storages/request_queue.js +824 -216
- package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +37 -88
- package/storages/{sitemap_request_list.js → sitemap_request_loader.js} +136 -143
- package/storages/storage_instance_manager.d.ts +87 -0
- package/storages/storage_instance_manager.js +256 -0
- package/storages/storage_stats.d.ts +48 -0
- package/storages/storage_stats.js +29 -0
- package/storages/transaction.d.ts +252 -0
- package/storages/transaction.js +251 -0
- package/storages/utils.d.ts +54 -9
- package/storages/utils.js +64 -13
- package/system-info/cpu-info.d.ts +67 -0
- package/system-info/cpu-info.js +216 -0
- package/system-info/memory-info.d.ts +31 -0
- package/system-info/memory-info.js +115 -0
- package/system-info/ps-tree.d.ts +17 -0
- package/system-info/ps-tree.js +144 -0
- package/system-info/runtime.d.ts +14 -0
- package/system-info/runtime.js +80 -0
- package/typedefs.d.ts +0 -6
- package/typedefs.js +0 -1
- package/validators.d.ts +8 -1
- package/validators.js +10 -3
- package/autoscaling/autoscaled_pool.d.ts.map +0 -1
- package/autoscaling/autoscaled_pool.js.map +0 -1
- package/autoscaling/index.d.ts.map +0 -1
- package/autoscaling/index.js.map +0 -1
- package/autoscaling/snapshotter.d.ts.map +0 -1
- package/autoscaling/snapshotter.js.map +0 -1
- package/autoscaling/system_status.d.ts.map +0 -1
- package/autoscaling/system_status.js.map +0 -1
- package/configuration.d.ts.map +0 -1
- package/configuration.js.map +0 -1
- package/cookie_utils.d.ts.map +0 -1
- package/cookie_utils.js.map +0 -1
- package/crawlers/context_pipeline.d.ts.map +0 -1
- package/crawlers/context_pipeline.js.map +0 -1
- package/crawlers/crawler_commons.d.ts.map +0 -1
- package/crawlers/crawler_commons.js.map +0 -1
- package/crawlers/crawler_utils.d.ts +0 -10
- package/crawlers/crawler_utils.d.ts.map +0 -1
- package/crawlers/crawler_utils.js +0 -12
- package/crawlers/crawler_utils.js.map +0 -1
- package/crawlers/error_snapshotter.d.ts.map +0 -1
- package/crawlers/error_snapshotter.js.map +0 -1
- package/crawlers/error_tracker.d.ts.map +0 -1
- package/crawlers/error_tracker.js.map +0 -1
- package/crawlers/index.d.ts.map +0 -1
- package/crawlers/index.js.map +0 -1
- package/crawlers/internals/types.d.ts.map +0 -1
- package/crawlers/internals/types.js.map +0 -1
- package/crawlers/statistics.d.ts.map +0 -1
- package/crawlers/statistics.js.map +0 -1
- package/enqueue_links/enqueue_links.d.ts.map +0 -1
- package/enqueue_links/enqueue_links.js.map +0 -1
- package/enqueue_links/index.d.ts.map +0 -1
- package/enqueue_links/index.js.map +0 -1
- package/enqueue_links/shared.d.ts.map +0 -1
- package/enqueue_links/shared.js.map +0 -1
- package/errors.d.ts.map +0 -1
- package/errors.js.map +0 -1
- package/events/event_manager.d.ts.map +0 -1
- package/events/event_manager.js.map +0 -1
- package/events/index.d.ts.map +0 -1
- package/events/index.js.map +0 -1
- package/events/local_event_manager.d.ts.map +0 -1
- package/events/local_event_manager.js.map +0 -1
- package/http_clients/base-http-client.d.ts +0 -134
- package/http_clients/base-http-client.d.ts.map +0 -1
- package/http_clients/base-http-client.js +0 -33
- package/http_clients/base-http-client.js.map +0 -1
- package/http_clients/form-data-like.d.ts +0 -67
- package/http_clients/form-data-like.d.ts.map +0 -1
- package/http_clients/form-data-like.js +0 -5
- package/http_clients/form-data-like.js.map +0 -1
- package/http_clients/got-scraping-http-client.d.ts +0 -15
- package/http_clients/got-scraping-http-client.d.ts.map +0 -1
- package/http_clients/got-scraping-http-client.js +0 -69
- package/http_clients/got-scraping-http-client.js.map +0 -1
- package/http_clients/index.d.ts +0 -3
- package/http_clients/index.d.ts.map +0 -1
- package/http_clients/index.js +0 -3
- package/http_clients/index.js.map +0 -1
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/log.d.ts.map +0 -1
- package/log.js.map +0 -1
- package/proxy_configuration.d.ts.map +0 -1
- package/proxy_configuration.js.map +0 -1
- package/request.d.ts.map +0 -1
- package/request.js.map +0 -1
- package/router.d.ts.map +0 -1
- package/router.js.map +0 -1
- package/serialization.d.ts.map +0 -1
- package/serialization.js.map +0 -1
- package/session_pool/consts.d.ts.map +0 -1
- package/session_pool/consts.js.map +0 -1
- package/session_pool/errors.d.ts.map +0 -1
- package/session_pool/errors.js.map +0 -1
- package/session_pool/events.d.ts +0 -3
- package/session_pool/events.d.ts.map +0 -1
- package/session_pool/events.js +0 -3
- package/session_pool/events.js.map +0 -1
- package/session_pool/index.d.ts.map +0 -1
- package/session_pool/index.js.map +0 -1
- package/session_pool/session.d.ts.map +0 -1
- package/session_pool/session.js.map +0 -1
- package/session_pool/session_pool.d.ts.map +0 -1
- package/session_pool/session_pool.js.map +0 -1
- package/storages/access_checking.d.ts +0 -13
- package/storages/access_checking.d.ts.map +0 -1
- package/storages/access_checking.js +0 -14
- package/storages/access_checking.js.map +0 -1
- package/storages/dataset.d.ts.map +0 -1
- package/storages/dataset.js.map +0 -1
- package/storages/index.d.ts.map +0 -1
- package/storages/index.js.map +0 -1
- package/storages/key_value_store.d.ts.map +0 -1
- package/storages/key_value_store.js.map +0 -1
- package/storages/request_list.d.ts.map +0 -1
- package/storages/request_list.js.map +0 -1
- package/storages/request_provider.d.ts +0 -308
- package/storages/request_provider.d.ts.map +0 -1
- package/storages/request_provider.js +0 -555
- package/storages/request_provider.js.map +0 -1
- package/storages/request_queue.d.ts.map +0 -1
- package/storages/request_queue.js.map +0 -1
- package/storages/request_queue_v2.d.ts +0 -87
- package/storages/request_queue_v2.d.ts.map +0 -1
- package/storages/request_queue_v2.js +0 -438
- package/storages/request_queue_v2.js.map +0 -1
- package/storages/sitemap_request_list.d.ts.map +0 -1
- package/storages/sitemap_request_list.js.map +0 -1
- package/storages/storage_manager.d.ts +0 -58
- package/storages/storage_manager.d.ts.map +0 -1
- package/storages/storage_manager.js +0 -105
- package/storages/storage_manager.js.map +0 -1
- package/storages/utils.d.ts.map +0 -1
- package/storages/utils.js.map +0 -1
- package/tsconfig.build.tsbuildinfo +0 -1
- package/typedefs.d.ts.map +0 -1
- package/typedefs.js.map +0 -1
- package/validators.d.ts.map +0 -1
- package/validators.js.map +0 -1
|
@@ -1,285 +1,67 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { log as defaultLog } from '../log.js';
|
|
6
|
-
const RESERVE_MEMORY_RATIO = 0.5;
|
|
7
|
-
const CLIENT_RATE_LIMIT_ERROR_RETRY_COUNT = 2;
|
|
8
|
-
const CRITICAL_OVERLOAD_RATE_LIMIT_MILLIS = 10000;
|
|
1
|
+
import { ClientLoadSignal } from './client_load_signal.js';
|
|
2
|
+
import { CpuLoadSignal } from './cpu_load_signal.js';
|
|
3
|
+
import { EventLoopLoadSignal } from './event_loop_load_signal.js';
|
|
4
|
+
import { MemoryLoadSignal } from './memory_load_signal.js';
|
|
9
5
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* The class is used by the {@link AutoscaledPool} class.
|
|
6
|
+
* Owns the four built-in {@link LoadSignal} instances — {@link MemoryLoadSignal},
|
|
7
|
+
* {@link EventLoopLoadSignal}, {@link CpuLoadSignal} and {@link ClientLoadSignal} — constructing the ones
|
|
8
|
+
* that were not switched off and driving their shared lifecycle.
|
|
14
9
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* CPU becomes overloaded locally when its current use exceeds the `maxUsedCpuRatio` option or
|
|
20
|
-
* when Apify platform marks it as overloaded.
|
|
21
|
-
*
|
|
22
|
-
* Memory becomes overloaded if its current use exceeds the `maxUsedMemoryRatio` option.
|
|
23
|
-
* It's computed using the total memory available to the container when running on
|
|
24
|
-
* the Apify platform and a quarter of total system memory when running locally.
|
|
25
|
-
* Max total memory when running locally may be overridden by using the `CRAWLEE_MEMORY_MBYTES`
|
|
26
|
-
* environment variable.
|
|
27
|
-
*
|
|
28
|
-
* Event loop becomes overloaded if it slows down by more than the `maxBlockedMillis` option.
|
|
29
|
-
*
|
|
30
|
-
* Client becomes overloaded when rate limit errors (429 - Too Many Requests),
|
|
31
|
-
* typically received from the request queue, exceed the set limit within the set interval.
|
|
32
|
-
* @category Scaling
|
|
10
|
+
* Configured indirectly through {@link ConcurrencySystemOptions.loadSignals|`loadSignals`}, whose per-signal bags
|
|
11
|
+
* are simply forwarded to the corresponding constructor.
|
|
12
|
+
* @internal
|
|
33
13
|
*/
|
|
34
14
|
export class Snapshotter {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
15
|
+
// Absent when switched off through the corresponding option (e.g. `client: false`).
|
|
16
|
+
#memorySignal;
|
|
17
|
+
#eventLoopSignal;
|
|
18
|
+
#cpuSignal;
|
|
19
|
+
#clientSignal;
|
|
20
|
+
/**
|
|
21
|
+
* Returns the enabled built-in signals, so `SystemStatus` can iterate them alongside any custom `LoadSignal`
|
|
22
|
+
* instances. Signals switched off through the options are simply absent — the system status reports them as
|
|
23
|
+
* not overloaded.
|
|
24
|
+
*/
|
|
25
|
+
getLoadSignals() {
|
|
26
|
+
const builtin = [
|
|
27
|
+
this.#memorySignal,
|
|
28
|
+
this.#eventLoopSignal,
|
|
29
|
+
this.#cpuSignal,
|
|
30
|
+
this.#clientSignal,
|
|
31
|
+
];
|
|
32
|
+
return builtin.filter((signal) => signal !== undefined);
|
|
33
|
+
}
|
|
53
34
|
/**
|
|
54
35
|
* @param [options] All `Snapshotter` configuration options.
|
|
55
36
|
*/
|
|
56
37
|
constructor(options = {}) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const { eventLoopSnapshotIntervalSecs = 0.5, clientSnapshotIntervalSecs = 1, snapshotHistorySecs = 30, maxBlockedMillis = 50, maxUsedMemoryRatio = 0.9, maxClientErrors = 3, log = defaultLog, config = Configuration.getGlobalConfig(), client = config.getStorageClient(), } = options;
|
|
69
|
-
this.log = log.child({ prefix: 'Snapshotter' });
|
|
70
|
-
this.client = client;
|
|
71
|
-
this.config = config;
|
|
72
|
-
this.events = this.config.getEventManager();
|
|
73
|
-
this.eventLoopSnapshotIntervalMillis = eventLoopSnapshotIntervalSecs * 1000;
|
|
74
|
-
this.clientSnapshotIntervalMillis = clientSnapshotIntervalSecs * 1000;
|
|
75
|
-
this.snapshotHistoryMillis = snapshotHistorySecs * 1000;
|
|
76
|
-
this.maxBlockedMillis = maxBlockedMillis;
|
|
77
|
-
this.maxUsedMemoryRatio = maxUsedMemoryRatio;
|
|
78
|
-
this.maxClientErrors = maxClientErrors;
|
|
79
|
-
// We need to pre-bind those functions to be able to successfully remove listeners.
|
|
80
|
-
this._snapshotCpu = this._snapshotCpu.bind(this);
|
|
81
|
-
this._snapshotMemory = this._snapshotMemory.bind(this);
|
|
38
|
+
const { memory = {}, eventLoop = {}, cpu = {}, client = {} } = options;
|
|
39
|
+
// Each signal resolves its own ambient dependencies when started, and is told the window it will be sampled
|
|
40
|
+
// over then too - so there is nothing to thread in here beyond the caller's tuning.
|
|
41
|
+
if (memory !== false)
|
|
42
|
+
this.#memorySignal = new MemoryLoadSignal(memory);
|
|
43
|
+
if (eventLoop !== false)
|
|
44
|
+
this.#eventLoopSignal = new EventLoopLoadSignal(eventLoop);
|
|
45
|
+
if (cpu !== false)
|
|
46
|
+
this.#cpuSignal = new CpuLoadSignal(cpu);
|
|
47
|
+
if (client !== false)
|
|
48
|
+
this.#clientSignal = new ClientLoadSignal(client);
|
|
82
49
|
}
|
|
83
50
|
/**
|
|
84
|
-
* Starts capturing snapshots at configured intervals.
|
|
51
|
+
* Starts capturing snapshots at configured intervals. The `context` carries the sample window the signals will
|
|
52
|
+
* be queried with, which is also how much history they retain.
|
|
85
53
|
*/
|
|
86
|
-
async start() {
|
|
87
|
-
|
|
88
|
-
if (memoryMbytes > 0) {
|
|
89
|
-
this.maxMemoryBytes = memoryMbytes * 1024 * 1024;
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
const containerized = this.config.get('containerized', await isContainerized());
|
|
93
|
-
const memInfo = await getMemoryInfo(containerized);
|
|
94
|
-
const totalBytes = memInfo.totalBytes;
|
|
95
|
-
this.maxMemoryBytes = Math.ceil(totalBytes * this.config.get('availableMemoryRatio'));
|
|
96
|
-
this.log.debug(`Setting max memory of this run to ${Math.round(this.maxMemoryBytes / 1024 / 1024)} MB. ` +
|
|
97
|
-
'Use the CRAWLEE_MEMORY_MBYTES or CRAWLEE_AVAILABLE_MEMORY_RATIO environment variable to override it.');
|
|
98
|
-
}
|
|
99
|
-
// Start snapshotting.
|
|
100
|
-
this.eventLoopInterval = betterSetInterval(this._snapshotEventLoop.bind(this), this.eventLoopSnapshotIntervalMillis);
|
|
101
|
-
this.clientInterval = betterSetInterval(this._snapshotClient.bind(this), this.clientSnapshotIntervalMillis);
|
|
102
|
-
this.events.on("systemInfo" /* EventType.SYSTEM_INFO */, this._snapshotCpu);
|
|
103
|
-
this.events.on("systemInfo" /* EventType.SYSTEM_INFO */, this._snapshotMemory);
|
|
54
|
+
async start(context) {
|
|
55
|
+
await Promise.all(this.getLoadSignals().map(async (signal) => signal.start(context)));
|
|
104
56
|
}
|
|
105
57
|
/**
|
|
106
58
|
* Stops all resource capturing.
|
|
107
59
|
*/
|
|
108
60
|
async stop() {
|
|
109
|
-
|
|
110
|
-
betterClearInterval(this.clientInterval);
|
|
111
|
-
this.events.off("systemInfo" /* EventType.SYSTEM_INFO */, this._snapshotCpu);
|
|
112
|
-
this.events.off("systemInfo" /* EventType.SYSTEM_INFO */, this._snapshotMemory);
|
|
61
|
+
await Promise.all(this.getLoadSignals().map(async (signal) => signal.stop()));
|
|
113
62
|
// Allow microtask queue to unwind before stop returns.
|
|
114
63
|
await new Promise((resolve) => {
|
|
115
64
|
setImmediate(resolve);
|
|
116
65
|
});
|
|
117
66
|
}
|
|
118
|
-
/**
|
|
119
|
-
* Returns a sample of latest memory snapshots, with the size of the sample defined
|
|
120
|
-
* by the sampleDurationMillis parameter. If omitted, it returns a full snapshot history.
|
|
121
|
-
*/
|
|
122
|
-
getMemorySample(sampleDurationMillis) {
|
|
123
|
-
return this._getSample(this.memorySnapshots, sampleDurationMillis);
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Returns a sample of latest event loop snapshots, with the size of the sample defined
|
|
127
|
-
* by the sampleDurationMillis parameter. If omitted, it returns a full snapshot history.
|
|
128
|
-
*/
|
|
129
|
-
getEventLoopSample(sampleDurationMillis) {
|
|
130
|
-
return this._getSample(this.eventLoopSnapshots, sampleDurationMillis);
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Returns a sample of latest CPU snapshots, with the size of the sample defined
|
|
134
|
-
* by the sampleDurationMillis parameter. If omitted, it returns a full snapshot history.
|
|
135
|
-
*/
|
|
136
|
-
getCpuSample(sampleDurationMillis) {
|
|
137
|
-
return this._getSample(this.cpuSnapshots, sampleDurationMillis);
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Returns a sample of latest Client snapshots, with the size of the sample defined
|
|
141
|
-
* by the sampleDurationMillis parameter. If omitted, it returns a full snapshot history.
|
|
142
|
-
*/
|
|
143
|
-
getClientSample(sampleDurationMillis) {
|
|
144
|
-
return this._getSample(this.clientSnapshots, sampleDurationMillis);
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Finds the latest snapshots by sampleDurationMillis in the provided array.
|
|
148
|
-
*/
|
|
149
|
-
_getSample(snapshots, sampleDurationMillis) {
|
|
150
|
-
if (!sampleDurationMillis)
|
|
151
|
-
return snapshots;
|
|
152
|
-
const sample = [];
|
|
153
|
-
let idx = snapshots.length;
|
|
154
|
-
if (!idx)
|
|
155
|
-
return sample;
|
|
156
|
-
const latestTime = snapshots[idx - 1].createdAt;
|
|
157
|
-
while (idx--) {
|
|
158
|
-
const snapshot = snapshots[idx];
|
|
159
|
-
if (+latestTime - +snapshot.createdAt <= sampleDurationMillis) {
|
|
160
|
-
sample.unshift(snapshot);
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
break;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return sample;
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Creates a snapshot of current memory usage
|
|
170
|
-
* using the Apify platform `systemInfo` event.
|
|
171
|
-
*/
|
|
172
|
-
_snapshotMemory(systemInfo) {
|
|
173
|
-
const createdAt = systemInfo.createdAt ? new Date(systemInfo.createdAt) : new Date();
|
|
174
|
-
this._pruneSnapshots(this.memorySnapshots, createdAt);
|
|
175
|
-
const { memCurrentBytes } = systemInfo;
|
|
176
|
-
const snapshot = {
|
|
177
|
-
createdAt,
|
|
178
|
-
isOverloaded: memCurrentBytes / this.maxMemoryBytes > this.maxUsedMemoryRatio,
|
|
179
|
-
usedBytes: memCurrentBytes,
|
|
180
|
-
};
|
|
181
|
-
this.memorySnapshots.push(snapshot);
|
|
182
|
-
this._memoryOverloadWarning(systemInfo);
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Checks for critical memory overload and logs it to the console.
|
|
186
|
-
*/
|
|
187
|
-
_memoryOverloadWarning(systemInfo) {
|
|
188
|
-
const { memCurrentBytes } = systemInfo;
|
|
189
|
-
const createdAt = systemInfo.createdAt ? new Date(systemInfo.createdAt) : new Date();
|
|
190
|
-
if (this.lastLoggedCriticalMemoryOverloadAt &&
|
|
191
|
-
+createdAt < +this.lastLoggedCriticalMemoryOverloadAt + CRITICAL_OVERLOAD_RATE_LIMIT_MILLIS)
|
|
192
|
-
return;
|
|
193
|
-
const maxDesiredMemoryBytes = this.maxUsedMemoryRatio * this.maxMemoryBytes;
|
|
194
|
-
const reserveMemory = this.maxMemoryBytes * (1 - this.maxUsedMemoryRatio) * RESERVE_MEMORY_RATIO;
|
|
195
|
-
const criticalOverloadBytes = maxDesiredMemoryBytes + reserveMemory;
|
|
196
|
-
const isCriticalOverload = memCurrentBytes > criticalOverloadBytes;
|
|
197
|
-
if (isCriticalOverload) {
|
|
198
|
-
const usedPercentage = Math.round((memCurrentBytes / this.maxMemoryBytes) * 100);
|
|
199
|
-
const toMb = (bytes) => Math.round(bytes / 1024 ** 2);
|
|
200
|
-
this.log.warning('Memory is critically overloaded. ' +
|
|
201
|
-
`Using ${toMb(memCurrentBytes)} MB of ${toMb(this.maxMemoryBytes)} MB (${usedPercentage}%). Consider increasing available memory.`);
|
|
202
|
-
this.lastLoggedCriticalMemoryOverloadAt = createdAt;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Creates a snapshot of current event loop delay.
|
|
207
|
-
*/
|
|
208
|
-
_snapshotEventLoop(intervalCallback) {
|
|
209
|
-
const now = new Date();
|
|
210
|
-
this._pruneSnapshots(this.eventLoopSnapshots, now);
|
|
211
|
-
const snapshot = {
|
|
212
|
-
createdAt: now,
|
|
213
|
-
isOverloaded: false,
|
|
214
|
-
exceededMillis: 0,
|
|
215
|
-
};
|
|
216
|
-
const previousSnapshot = this.eventLoopSnapshots[this.eventLoopSnapshots.length - 1];
|
|
217
|
-
if (previousSnapshot) {
|
|
218
|
-
const { createdAt } = previousSnapshot;
|
|
219
|
-
const delta = now.getTime() - +createdAt - this.eventLoopSnapshotIntervalMillis;
|
|
220
|
-
if (delta > this.maxBlockedMillis)
|
|
221
|
-
snapshot.isOverloaded = true;
|
|
222
|
-
snapshot.exceededMillis = Math.max(delta - this.maxBlockedMillis, 0);
|
|
223
|
-
}
|
|
224
|
-
this.eventLoopSnapshots.push(snapshot);
|
|
225
|
-
intervalCallback();
|
|
226
|
-
}
|
|
227
|
-
/**
|
|
228
|
-
* Creates a snapshot of current CPU usage using the Apify platform `systemInfo` event.
|
|
229
|
-
*/
|
|
230
|
-
_snapshotCpu(systemInfo) {
|
|
231
|
-
const { cpuCurrentUsage, isCpuOverloaded } = systemInfo;
|
|
232
|
-
const createdAt = systemInfo.createdAt ? new Date(systemInfo.createdAt) : new Date();
|
|
233
|
-
this._pruneSnapshots(this.cpuSnapshots, createdAt);
|
|
234
|
-
this.cpuSnapshots.push({
|
|
235
|
-
createdAt,
|
|
236
|
-
isOverloaded: isCpuOverloaded,
|
|
237
|
-
usedRatio: Math.ceil(cpuCurrentUsage / 100),
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Creates a snapshot of current API state by checking for
|
|
242
|
-
* rate limit errors. Only errors produced by a 2nd retry
|
|
243
|
-
* of the API call are considered for snapshotting since
|
|
244
|
-
* earlier errors may just be caused by a random spike in
|
|
245
|
-
* number of requests and do not necessarily signify API
|
|
246
|
-
* overloading.
|
|
247
|
-
*/
|
|
248
|
-
_snapshotClient(intervalCallback) {
|
|
249
|
-
const now = new Date();
|
|
250
|
-
this._pruneSnapshots(this.clientSnapshots, now);
|
|
251
|
-
const allErrorCounts = this.client.stats?.rateLimitErrors ?? []; // storage client might not support this
|
|
252
|
-
const currentErrCount = allErrorCounts[CLIENT_RATE_LIMIT_ERROR_RETRY_COUNT] || 0;
|
|
253
|
-
// Handle empty snapshots array
|
|
254
|
-
const snapshot = {
|
|
255
|
-
createdAt: now,
|
|
256
|
-
isOverloaded: false,
|
|
257
|
-
rateLimitErrorCount: currentErrCount,
|
|
258
|
-
};
|
|
259
|
-
const previousSnapshot = this.clientSnapshots[this.clientSnapshots.length - 1];
|
|
260
|
-
if (previousSnapshot) {
|
|
261
|
-
const { rateLimitErrorCount } = previousSnapshot;
|
|
262
|
-
const delta = currentErrCount - rateLimitErrorCount;
|
|
263
|
-
if (delta > this.maxClientErrors)
|
|
264
|
-
snapshot.isOverloaded = true;
|
|
265
|
-
}
|
|
266
|
-
this.clientSnapshots.push(snapshot);
|
|
267
|
-
intervalCallback();
|
|
268
|
-
}
|
|
269
|
-
/**
|
|
270
|
-
* Removes snapshots that are older than the snapshotHistorySecs option
|
|
271
|
-
* from the array (destructively - in place).
|
|
272
|
-
*/
|
|
273
|
-
_pruneSnapshots(snapshots, now) {
|
|
274
|
-
let oldCount = 0;
|
|
275
|
-
for (let i = 0; i < snapshots.length; i++) {
|
|
276
|
-
const { createdAt } = snapshots[i];
|
|
277
|
-
if (now.getTime() - new Date(createdAt).getTime() > this.snapshotHistoryMillis)
|
|
278
|
-
oldCount++;
|
|
279
|
-
else
|
|
280
|
-
break;
|
|
281
|
-
}
|
|
282
|
-
snapshots.splice(0, oldCount);
|
|
283
|
-
}
|
|
284
67
|
}
|
|
285
|
-
//# sourceMappingURL=snapshotter.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { Snapshotter } from './snapshotter.js';
|
|
1
|
+
import type { LoadSignal } from './load_signal.js';
|
|
2
|
+
import type { Snapshotter } from './snapshotter.js';
|
|
3
3
|
/**
|
|
4
4
|
* Represents the current status of the system.
|
|
5
5
|
*/
|
|
@@ -10,6 +10,7 @@ export interface SystemInfo {
|
|
|
10
10
|
eventLoopInfo: ClientInfo;
|
|
11
11
|
cpuInfo: ClientInfo;
|
|
12
12
|
clientInfo: ClientInfo;
|
|
13
|
+
memTotalBytes?: number;
|
|
13
14
|
memCurrentBytes?: number;
|
|
14
15
|
/**
|
|
15
16
|
* Platform only property
|
|
@@ -26,7 +27,28 @@ export interface SystemInfo {
|
|
|
26
27
|
* @internal
|
|
27
28
|
*/
|
|
28
29
|
createdAt?: Date;
|
|
30
|
+
/**
|
|
31
|
+
* Status of additional load signals beyond the built-in four.
|
|
32
|
+
* Keys are `LoadSignal.name` values, values are overload info.
|
|
33
|
+
*/
|
|
34
|
+
loadSignalInfo?: Record<string, ClientInfo>;
|
|
29
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* How far back the *current* system status looks by default — the window that gates task dispatch.
|
|
38
|
+
* @internal
|
|
39
|
+
*/
|
|
40
|
+
export declare const DEFAULT_CURRENT_HISTORY_SECS = 5;
|
|
41
|
+
/**
|
|
42
|
+
* How far back the *historical* system status looks by default — the window autoscaling decisions are based on, and
|
|
43
|
+
* therefore how much history the signals are asked to retain.
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
export declare const DEFAULT_SNAPSHOT_HISTORY_SECS = 30;
|
|
47
|
+
/**
|
|
48
|
+
* An implementation detail of the {@link ConcurrencySystem} — configure it through
|
|
49
|
+
* {@link ConcurrencySystemOptions} (`loadSignals`, `currentHistorySecs` and `snapshotHistorySecs`).
|
|
50
|
+
* @internal
|
|
51
|
+
*/
|
|
30
52
|
export interface SystemStatusOptions {
|
|
31
53
|
/**
|
|
32
54
|
* Defines max age of snapshots used in the {@link SystemStatus.getCurrentStatus} measurement.
|
|
@@ -34,35 +56,23 @@ export interface SystemStatusOptions {
|
|
|
34
56
|
*/
|
|
35
57
|
currentHistorySecs?: number;
|
|
36
58
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
|
|
41
|
-
maxMemoryOverloadedRatio?: number;
|
|
42
|
-
/**
|
|
43
|
-
* Sets the maximum ratio of overloaded snapshots in an event loop sample.
|
|
44
|
-
* If the sample exceeds this ratio, the system will be overloaded.
|
|
45
|
-
* @default 0.6
|
|
59
|
+
* Defines max age of snapshots used in the {@link SystemStatus.getHistoricalStatus} measurement — the window
|
|
60
|
+
* autoscaling decisions are based on. Applied uniformly to every signal, built-in or custom, so that a signal's
|
|
61
|
+
* private retention cannot silently widen the window.
|
|
62
|
+
* @default 30
|
|
46
63
|
*/
|
|
47
|
-
|
|
64
|
+
historySecs?: number;
|
|
48
65
|
/**
|
|
49
|
-
*
|
|
50
|
-
* If the sample exceeds this ratio, the system will be overloaded.
|
|
51
|
-
* @default 0.4
|
|
66
|
+
* The `Snapshotter` whose built-in signals are evaluated.
|
|
52
67
|
*/
|
|
53
|
-
|
|
68
|
+
snapshotter: Snapshotter;
|
|
54
69
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
70
|
+
* Additional load signals to include in the system status evaluation.
|
|
71
|
+
* These are evaluated alongside the built-in memory, CPU, event loop,
|
|
72
|
+
* and client signals. If any signal reports overload, the system is
|
|
73
|
+
* considered overloaded. Each signal carries its own overload ratio.
|
|
58
74
|
*/
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* The `Snapshotter` instance to be queried for `SystemStatus`.
|
|
62
|
-
*/
|
|
63
|
-
snapshotter?: Snapshotter;
|
|
64
|
-
/** @internal */
|
|
65
|
-
config?: Configuration;
|
|
75
|
+
loadSignals?: LoadSignal[];
|
|
66
76
|
}
|
|
67
77
|
export interface ClientInfo {
|
|
68
78
|
isOverloaded: boolean;
|
|
@@ -82,35 +92,33 @@ export interface FinalStatistics {
|
|
|
82
92
|
crawlerRuntimeMillis: number;
|
|
83
93
|
}
|
|
84
94
|
/**
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* The system status is calculated using a weighted average of overloaded
|
|
89
|
-
* messages in the snapshots, with the weights being the time intervals
|
|
90
|
-
* between the snapshots. Each resource is calculated separately
|
|
91
|
-
* and the system is overloaded whenever at least one resource is overloaded.
|
|
92
|
-
* The class is used by the {@link AutoscaledPool} class.
|
|
95
|
+
* Reads the overload verdict of every signal — the {@link Snapshotter}'s built-in four plus any custom ones — and
|
|
96
|
+
* combines them into a {@link SystemInfo}: each signal is a time-weighted average of its snapshots, and the system
|
|
97
|
+
* is overloaded whenever at least one of them is.
|
|
93
98
|
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* by the `currentHistorySecs` option and represents the max age
|
|
98
|
-
* of snapshots to be considered for the calculation.
|
|
99
|
+
* Evaluated over two windows, both requested explicitly from every signal so that a signal's private retention cannot
|
|
100
|
+
* widen what it contributes: a short `currentHistorySecs` one ({@link SystemStatus.getCurrentStatus}, gating task
|
|
101
|
+
* dispatch) and a longer `historySecs` one ({@link SystemStatus.getHistoricalStatus}, driving autoscaling).
|
|
99
102
|
*
|
|
100
|
-
* {@link
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
* in the {@link Snapshotter} instance.
|
|
104
|
-
* @category Scaling
|
|
103
|
+
* An implementation detail of the {@link ConcurrencySystem}, configured through
|
|
104
|
+
* {@link ConcurrencySystemOptions}.
|
|
105
|
+
* @internal
|
|
105
106
|
*/
|
|
106
107
|
export declare class SystemStatus {
|
|
107
|
-
private
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
108
|
+
#private;
|
|
109
|
+
constructor(options: SystemStatusOptions);
|
|
110
|
+
/**
|
|
111
|
+
* The widest window any signal will be queried with, and therefore exactly how much history the signals are asked
|
|
112
|
+
* to retain when they start. Derived here, where the windows are resolved, so nothing has to reapply their
|
|
113
|
+
* defaults.
|
|
114
|
+
*/
|
|
115
|
+
get maxSampleWindowMillis(): number;
|
|
116
|
+
/**
|
|
117
|
+
* Signal names are the keys of the reported {@link SystemInfo}, so a duplicate would leave a status object that
|
|
118
|
+
* contradicts actual behavior: both signals are still evaluated (any overloaded one holds concurrency down), but
|
|
119
|
+
* only the last is reported.
|
|
120
|
+
*/
|
|
121
|
+
private assertUniqueSignalNames;
|
|
114
122
|
/**
|
|
115
123
|
* Returns an {@link SystemInfo} object with the following structure:
|
|
116
124
|
*
|
|
@@ -140,42 +148,12 @@ export declare class SystemStatus {
|
|
|
140
148
|
* }
|
|
141
149
|
* ```
|
|
142
150
|
*
|
|
143
|
-
* Where the `isSystemIdle` property is set to `false` if the system
|
|
144
|
-
*
|
|
145
|
-
* (which is configurable in the {@link Snapshotter}) and `true` otherwise.
|
|
151
|
+
* Where the `isSystemIdle` property is set to `false` if the system has been overloaded within the last
|
|
152
|
+
* `historySecs` seconds and `true` otherwise.
|
|
146
153
|
*/
|
|
147
154
|
getHistoricalStatus(): SystemInfo;
|
|
148
155
|
/**
|
|
149
156
|
* Returns a system status object.
|
|
150
157
|
*/
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Returns an object with an isOverloaded property set to true
|
|
154
|
-
* if the memory has been overloaded in the last sampleDurationMillis.
|
|
155
|
-
*/
|
|
156
|
-
protected _isMemoryOverloaded(sampleDurationMillis?: number): ClientInfo;
|
|
157
|
-
/**
|
|
158
|
-
* Returns an object with an isOverloaded property set to true
|
|
159
|
-
* if the event loop has been overloaded in the last sampleDurationMillis.
|
|
160
|
-
*/
|
|
161
|
-
protected _isEventLoopOverloaded(sampleDurationMillis?: number): ClientInfo;
|
|
162
|
-
/**
|
|
163
|
-
* Returns an object with an isOverloaded property set to true
|
|
164
|
-
* if the CPU has been overloaded in the last sampleDurationMillis.
|
|
165
|
-
*/
|
|
166
|
-
protected _isCpuOverloaded(sampleDurationMillis?: number): ClientInfo;
|
|
167
|
-
/**
|
|
168
|
-
* Returns an object with an isOverloaded property set to true
|
|
169
|
-
* if the client has been overloaded in the last sampleDurationMillis.
|
|
170
|
-
*/
|
|
171
|
-
protected _isClientOverloaded(sampleDurationMillis?: number): ClientInfo;
|
|
172
|
-
/**
|
|
173
|
-
* Returns an object with sample information and an isOverloaded property
|
|
174
|
-
* set to true if at least the ratio of snapshots in the sample are overloaded.
|
|
175
|
-
*/
|
|
176
|
-
protected _isSampleOverloaded<T extends {
|
|
177
|
-
createdAt: Date;
|
|
178
|
-
isOverloaded: boolean;
|
|
179
|
-
}>(sample: T[], ratio: number): ClientInfo;
|
|
158
|
+
private isSystemIdle;
|
|
180
159
|
}
|
|
181
|
-
//# sourceMappingURL=system_status.d.ts.map
|