@mavenmachines/event-handler-lib 2.0.2-rc.2 → 2.0.3-rc.0
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 +15 -3
- package/dist/cjs/clients/kafka/eventHandlerIndex.d.ts +9 -0
- package/dist/cjs/clients/kafka/eventHandlerIndex.js +26 -0
- package/dist/cjs/clients/kafka/eventHandlerIndex.js.map +1 -0
- package/dist/cjs/clients/kafka/handlerResultSummary.d.ts +11 -0
- package/dist/cjs/clients/kafka/handlerResultSummary.js +50 -0
- package/dist/cjs/clients/kafka/handlerResultSummary.js.map +1 -0
- package/dist/cjs/clients/kafka/inflightEventTracker.d.ts +11 -0
- package/dist/cjs/clients/kafka/inflightEventTracker.js +37 -0
- package/dist/cjs/clients/kafka/inflightEventTracker.js.map +1 -0
- package/dist/cjs/clients/kafka/kafkaAssignmentGauges.d.ts +8 -0
- package/dist/cjs/clients/kafka/kafkaAssignmentGauges.js +38 -0
- package/dist/cjs/clients/kafka/kafkaAssignmentGauges.js.map +1 -0
- package/dist/cjs/clients/kafka/kafkaConsumer.d.ts +15 -32
- package/dist/cjs/clients/kafka/kafkaConsumer.js +94 -461
- package/dist/cjs/clients/kafka/kafkaConsumer.js.map +1 -1
- package/dist/cjs/clients/kafka/kafkaConsumerSettings.d.ts +5 -0
- package/dist/cjs/clients/kafka/kafkaConsumerSettings.js +19 -0
- package/dist/cjs/clients/kafka/kafkaConsumerSettings.js.map +1 -0
- package/dist/cjs/clients/kafka/kafkaEventConsumer.d.ts +4 -9
- package/dist/cjs/clients/kafka/kafkaEventConsumer.js +26 -164
- package/dist/cjs/clients/kafka/kafkaEventConsumer.js.map +1 -1
- package/dist/cjs/clients/kafka/kafkaEventDispatcher.d.ts +11 -0
- package/dist/cjs/clients/kafka/kafkaEventDispatcher.js +80 -0
- package/dist/cjs/clients/kafka/kafkaEventDispatcher.js.map +1 -0
- package/dist/cjs/clients/kafka/kafkaFetchStallMonitor.d.ts +21 -0
- package/dist/cjs/clients/kafka/kafkaFetchStallMonitor.js +80 -0
- package/dist/cjs/clients/kafka/kafkaFetchStallMonitor.js.map +1 -0
- package/dist/cjs/clients/kafka/kafkaGroupJoin.d.ts +7 -0
- package/dist/cjs/clients/kafka/kafkaGroupJoin.js +45 -0
- package/dist/cjs/clients/kafka/kafkaGroupJoin.js.map +1 -0
- package/dist/cjs/clients/kafka/kafkaOffsetMarks.d.ts +12 -0
- package/dist/cjs/clients/kafka/kafkaOffsetMarks.js +53 -0
- package/dist/cjs/clients/kafka/kafkaOffsetMarks.js.map +1 -0
- package/dist/cjs/clients/kafka/kafkaRecordHandler.d.ts +12 -0
- package/dist/cjs/clients/kafka/kafkaRecordHandler.js +66 -0
- package/dist/cjs/clients/kafka/kafkaRecordHandler.js.map +1 -0
- package/dist/cjs/clients/kafka/kafkaStreamTypes.d.ts +17 -0
- package/dist/cjs/clients/kafka/kafkaStreamTypes.js +3 -0
- package/dist/cjs/clients/kafka/kafkaStreamTypes.js.map +1 -0
- package/dist/cjs/clients/kafka/kafkaTimedCommit.d.ts +1 -0
- package/dist/cjs/clients/kafka/kafkaTimedCommit.js +22 -0
- package/dist/cjs/clients/kafka/kafkaTimedCommit.js.map +1 -0
- package/dist/cjs/clients/kafka/kafkaTopicManager.js +5 -0
- package/dist/cjs/clients/kafka/kafkaTopicManager.js.map +1 -1
- package/dist/cjs/clients/kafka/kafkaTopicReader.d.ts +31 -0
- package/dist/cjs/clients/kafka/kafkaTopicReader.js +223 -0
- package/dist/cjs/clients/kafka/kafkaTopicReader.js.map +1 -0
- package/dist/cjs/clients/kafka/kafkaTopicStreams.d.ts +34 -0
- package/dist/cjs/clients/kafka/kafkaTopicStreams.js +238 -0
- package/dist/cjs/clients/kafka/kafkaTopicStreams.js.map +1 -0
- package/dist/cjs/clients/kafka/subscribedEventTopics.d.ts +3 -0
- package/dist/cjs/clients/kafka/subscribedEventTopics.js +23 -0
- package/dist/cjs/clients/kafka/subscribedEventTopics.js.map +1 -0
- package/dist/cjs/clients/kafka/topicOperationQueue.d.ts +4 -0
- package/dist/cjs/clients/kafka/topicOperationQueue.js +14 -0
- package/dist/cjs/clients/kafka/topicOperationQueue.js.map +1 -0
- package/dist/cjs/helpers/dynamoDB.client.js +12 -1
- package/dist/cjs/helpers/dynamoDB.client.js.map +1 -1
- package/dist/cjs/testUtils/bootstrapDatabase.js +4 -0
- package/dist/cjs/testUtils/bootstrapDatabase.js.map +1 -1
- package/dist/cjs/testUtils/testDependencies.js +4 -0
- package/dist/cjs/testUtils/testDependencies.js.map +1 -1
- package/dist/esm/clients/kafka/eventHandlerIndex.d.ts +9 -0
- package/dist/esm/clients/kafka/eventHandlerIndex.js +22 -0
- package/dist/esm/clients/kafka/eventHandlerIndex.js.map +1 -0
- package/dist/esm/clients/kafka/handlerResultSummary.d.ts +11 -0
- package/dist/esm/clients/kafka/handlerResultSummary.js +47 -0
- package/dist/esm/clients/kafka/handlerResultSummary.js.map +1 -0
- package/dist/esm/clients/kafka/inflightEventTracker.d.ts +11 -0
- package/dist/esm/clients/kafka/inflightEventTracker.js +33 -0
- package/dist/esm/clients/kafka/inflightEventTracker.js.map +1 -0
- package/dist/esm/clients/kafka/kafkaAssignmentGauges.d.ts +8 -0
- package/dist/esm/clients/kafka/kafkaAssignmentGauges.js +34 -0
- package/dist/esm/clients/kafka/kafkaAssignmentGauges.js.map +1 -0
- package/dist/esm/clients/kafka/kafkaConsumer.d.ts +15 -32
- package/dist/esm/clients/kafka/kafkaConsumer.js +92 -460
- package/dist/esm/clients/kafka/kafkaConsumer.js.map +1 -1
- package/dist/esm/clients/kafka/kafkaConsumerSettings.d.ts +5 -0
- package/dist/esm/clients/kafka/kafkaConsumerSettings.js +15 -0
- package/dist/esm/clients/kafka/kafkaConsumerSettings.js.map +1 -0
- package/dist/esm/clients/kafka/kafkaEventConsumer.d.ts +4 -9
- package/dist/esm/clients/kafka/kafkaEventConsumer.js +26 -165
- package/dist/esm/clients/kafka/kafkaEventConsumer.js.map +1 -1
- package/dist/esm/clients/kafka/kafkaEventDispatcher.d.ts +11 -0
- package/dist/esm/clients/kafka/kafkaEventDispatcher.js +77 -0
- package/dist/esm/clients/kafka/kafkaEventDispatcher.js.map +1 -0
- package/dist/esm/clients/kafka/kafkaFetchStallMonitor.d.ts +21 -0
- package/dist/esm/clients/kafka/kafkaFetchStallMonitor.js +76 -0
- package/dist/esm/clients/kafka/kafkaFetchStallMonitor.js.map +1 -0
- package/dist/esm/clients/kafka/kafkaGroupJoin.d.ts +7 -0
- package/dist/esm/clients/kafka/kafkaGroupJoin.js +41 -0
- package/dist/esm/clients/kafka/kafkaGroupJoin.js.map +1 -0
- package/dist/esm/clients/kafka/kafkaOffsetMarks.d.ts +12 -0
- package/dist/esm/clients/kafka/kafkaOffsetMarks.js +49 -0
- package/dist/esm/clients/kafka/kafkaOffsetMarks.js.map +1 -0
- package/dist/esm/clients/kafka/kafkaRecordHandler.d.ts +12 -0
- package/dist/esm/clients/kafka/kafkaRecordHandler.js +62 -0
- package/dist/esm/clients/kafka/kafkaRecordHandler.js.map +1 -0
- package/dist/esm/clients/kafka/kafkaStreamTypes.d.ts +17 -0
- package/dist/esm/clients/kafka/kafkaStreamTypes.js +2 -0
- package/dist/esm/clients/kafka/kafkaStreamTypes.js.map +1 -0
- package/dist/esm/clients/kafka/kafkaTimedCommit.d.ts +1 -0
- package/dist/esm/clients/kafka/kafkaTimedCommit.js +19 -0
- package/dist/esm/clients/kafka/kafkaTimedCommit.js.map +1 -0
- package/dist/esm/clients/kafka/kafkaTopicManager.js +5 -0
- package/dist/esm/clients/kafka/kafkaTopicManager.js.map +1 -1
- package/dist/esm/clients/kafka/kafkaTopicReader.d.ts +31 -0
- package/dist/esm/clients/kafka/kafkaTopicReader.js +219 -0
- package/dist/esm/clients/kafka/kafkaTopicReader.js.map +1 -0
- package/dist/esm/clients/kafka/kafkaTopicStreams.d.ts +34 -0
- package/dist/esm/clients/kafka/kafkaTopicStreams.js +234 -0
- package/dist/esm/clients/kafka/kafkaTopicStreams.js.map +1 -0
- package/dist/esm/clients/kafka/subscribedEventTopics.d.ts +3 -0
- package/dist/esm/clients/kafka/subscribedEventTopics.js +19 -0
- package/dist/esm/clients/kafka/subscribedEventTopics.js.map +1 -0
- package/dist/esm/clients/kafka/topicOperationQueue.d.ts +4 -0
- package/dist/esm/clients/kafka/topicOperationQueue.js +10 -0
- package/dist/esm/clients/kafka/topicOperationQueue.js.map +1 -0
- package/dist/esm/helpers/dynamoDB.client.js +12 -1
- package/dist/esm/helpers/dynamoDB.client.js.map +1 -1
- package/dist/esm/testUtils/bootstrapDatabase.js +4 -0
- package/dist/esm/testUtils/bootstrapDatabase.js.map +1 -1
- package/dist/esm/testUtils/testDependencies.js +4 -0
- package/dist/esm/testUtils/testDependencies.js.map +1 -1
- package/package.json +2 -2
|
@@ -11,62 +11,67 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
11
11
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
12
|
};
|
|
13
13
|
import { inject, injectable, optional } from "inversify";
|
|
14
|
-
import { Duration } from "luxon";
|
|
15
14
|
import { injectedEventHandlerConfig } from "../../helpers/index.js";
|
|
16
|
-
import { AppCleanupHelper } from "../../helpers/app.helpers.js";
|
|
17
15
|
import { AssertionHelper } from "../../helpers/assertionHelpers.js";
|
|
18
|
-
import { DefaultMap } from "../../helpers/dataStructure.helpers.js";
|
|
19
16
|
import { TYPES } from "../../dependencies/dependencyTypes.js";
|
|
20
|
-
import { consumerCount, partitionCount } from "../../initializers/metrics.js";
|
|
21
17
|
import { KafkaClientFactory } from "./kafkaClientFactory.js";
|
|
22
|
-
import {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const DEFAULT_KAFKA_FETCH_STALL_MS = 30_000;
|
|
31
|
-
const DEFAULT_KAFKA_COMMIT_TIMEOUT_MS = 30_000;
|
|
32
|
-
const STREAM_RESTART_DELAY_MS = 1_000;
|
|
33
|
-
function readPositiveInt(value, fallback) {
|
|
34
|
-
const parsed = parseInt(value);
|
|
35
|
-
if (parsed === undefined || parsed <= 0) {
|
|
36
|
-
return fallback;
|
|
37
|
-
}
|
|
38
|
-
return parsed;
|
|
39
|
-
}
|
|
40
|
-
function sleep(ms) {
|
|
41
|
-
return new Promise((resolve) => {
|
|
42
|
-
setTimeout(resolve, ms);
|
|
43
|
-
});
|
|
44
|
-
}
|
|
18
|
+
import { KafkaAssignmentGauges } from "./kafkaAssignmentGauges.js";
|
|
19
|
+
import { KafkaFetchStallMonitor } from "./kafkaFetchStallMonitor.js";
|
|
20
|
+
import { KafkaGroupJoinWait } from "./kafkaGroupJoin.js";
|
|
21
|
+
import { KafkaOffsetMarks } from "./kafkaOffsetMarks.js";
|
|
22
|
+
import { KafkaRecordHandler } from "./kafkaRecordHandler.js";
|
|
23
|
+
import { KafkaTopicReader } from "./kafkaTopicReader.js";
|
|
24
|
+
import { KafkaTopicStreams } from "./kafkaTopicStreams.js";
|
|
25
|
+
export { KAFKA_TOPIC_BUFFER_SIZE } from "./kafkaConsumerSettings.js";
|
|
45
26
|
let KafkaConsumer = class KafkaConsumer {
|
|
46
27
|
assertions;
|
|
47
|
-
cleanup;
|
|
48
28
|
clientFactory;
|
|
49
29
|
groupId;
|
|
50
30
|
consumer;
|
|
51
|
-
streams = new Map();
|
|
52
|
-
streamLoops = new Map();
|
|
53
|
-
pendingSeeks = new DefaultMap(() => new Map());
|
|
54
|
-
topicOperations = new Map();
|
|
55
|
-
closing = new Map();
|
|
56
|
-
observedAssignmentTopics = new Set();
|
|
57
|
-
fetchStallTimers = new Map();
|
|
58
|
-
fetchListeners = new Map();
|
|
59
31
|
eachMessage;
|
|
60
32
|
closed = false;
|
|
61
|
-
cancelJoinWait;
|
|
62
33
|
logger;
|
|
63
|
-
|
|
64
|
-
|
|
34
|
+
offsetMarks = new KafkaOffsetMarks();
|
|
35
|
+
assignments = new KafkaAssignmentGauges();
|
|
36
|
+
groupJoin = new KafkaGroupJoinWait();
|
|
37
|
+
discardQueued = new Map();
|
|
38
|
+
records;
|
|
39
|
+
fetchStalls;
|
|
40
|
+
topics;
|
|
41
|
+
onRebalance = () => {
|
|
42
|
+
this.publishAssignments();
|
|
43
|
+
for (const discard of this.discardQueued.values()) {
|
|
44
|
+
discard(false);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
onJoin = () => {
|
|
48
|
+
this.publishAssignments();
|
|
49
|
+
for (const discard of this.discardQueued.values()) {
|
|
50
|
+
discard(true);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
constructor(loggerService, assertions, clientFactory, groupId) {
|
|
65
54
|
this.assertions = assertions;
|
|
66
|
-
this.cleanup = cleanup;
|
|
67
55
|
this.clientFactory = clientFactory;
|
|
68
56
|
this.groupId = groupId;
|
|
69
57
|
this.logger = loggerService.getLogger(this);
|
|
58
|
+
this.records = new KafkaRecordHandler(this.logger, this.offsetMarks, (topic, partition) => this.isPartitionAssigned(topic, partition), () => this.eachMessage);
|
|
59
|
+
this.fetchStalls = new KafkaFetchStallMonitor(this.logger, (topic) => this.topics.restart(topic));
|
|
60
|
+
this.topics = new KafkaTopicStreams({
|
|
61
|
+
logger: this.logger,
|
|
62
|
+
isClosed: () => this.closed,
|
|
63
|
+
client: () => this.consumer,
|
|
64
|
+
watchFetches: (topic, stream) => {
|
|
65
|
+
this.fetchStalls.watch(topic, stream, {
|
|
66
|
+
stillOwns: () => this.topics.owns(topic, stream),
|
|
67
|
+
assignedPartitionCount: () => this.assignedPartitionCount(topic),
|
|
68
|
+
});
|
|
69
|
+
},
|
|
70
|
+
clearFetchWatch: (topic, stream) => {
|
|
71
|
+
this.fetchStalls.clear(topic, stream);
|
|
72
|
+
},
|
|
73
|
+
read: (topic, stream) => this.readTopic(topic, stream),
|
|
74
|
+
});
|
|
70
75
|
}
|
|
71
76
|
async init() {
|
|
72
77
|
if (this.groupId) {
|
|
@@ -74,72 +79,28 @@ let KafkaConsumer = class KafkaConsumer {
|
|
|
74
79
|
sessionTimeout: 60_000,
|
|
75
80
|
});
|
|
76
81
|
if (this.consumer) {
|
|
77
|
-
this.consumer.on("consumer:group:rebalance", this.
|
|
78
|
-
this.consumer.on("consumer:group:join", this.
|
|
82
|
+
this.consumer.on("consumer:group:rebalance", this.onRebalance);
|
|
83
|
+
this.consumer.on("consumer:group:join", this.onJoin);
|
|
79
84
|
this.logger.info("Kafka consumer ready", { groupId: this.groupId });
|
|
80
|
-
this.cleanup.register(this, () => this.disconnect());
|
|
81
85
|
}
|
|
82
86
|
}
|
|
83
87
|
return this;
|
|
84
88
|
}
|
|
85
|
-
updateAssignmentMetrics() {
|
|
86
|
-
const assignments = this.consumer?.assignments;
|
|
87
|
-
if (!assignments) {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
const byTopic = new DefaultMap(() => 0);
|
|
91
|
-
for (const assignment of assignments) {
|
|
92
|
-
byTopic.set(assignment.topic, byTopic.get(assignment.topic) + assignment.partitions.length);
|
|
93
|
-
}
|
|
94
|
-
const tracked = new Set([
|
|
95
|
-
...this.observedAssignmentTopics,
|
|
96
|
-
...this.streams.keys(),
|
|
97
|
-
]);
|
|
98
|
-
for (const topic of tracked) {
|
|
99
|
-
if (!byTopic.has(topic)) {
|
|
100
|
-
partitionCount.set({ topic, consumer: "0" }, 0);
|
|
101
|
-
consumerCount.set({ topic }, 0);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
this.observedAssignmentTopics.clear();
|
|
105
|
-
for (const [topic, count] of byTopic) {
|
|
106
|
-
partitionCount.set({ topic, consumer: "0" }, count);
|
|
107
|
-
consumerCount.set({ topic }, this.streams.has(topic) ? 1 : 0);
|
|
108
|
-
this.observedAssignmentTopics.add(topic);
|
|
109
|
-
}
|
|
110
|
-
for (const topic of tracked) {
|
|
111
|
-
if (!byTopic.has(topic)) {
|
|
112
|
-
this.observedAssignmentTopics.add(topic);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
89
|
async disconnect() {
|
|
117
90
|
this.closed = true;
|
|
118
|
-
this.
|
|
119
|
-
this.
|
|
120
|
-
|
|
121
|
-
try {
|
|
122
|
-
await stream.close();
|
|
123
|
-
}
|
|
124
|
-
catch (err) {
|
|
125
|
-
this.logger.warn(`Error closing stream for ${topic}`, { err });
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
await Promise.all(streamCloses);
|
|
129
|
-
this.streams.clear();
|
|
130
|
-
await Promise.all(this.streamLoops.values()).catch(() => undefined);
|
|
131
|
-
this.streamLoops.clear();
|
|
91
|
+
this.groupJoin.cancelWait();
|
|
92
|
+
this.fetchStalls.clearAll((topic) => this.topics.get(topic));
|
|
93
|
+
await this.topics.closeAll();
|
|
132
94
|
if (this.consumer) {
|
|
133
|
-
this.consumer.removeListener("consumer:group:rebalance", this.
|
|
134
|
-
this.consumer.removeListener("consumer:group:join", this.
|
|
95
|
+
this.consumer.removeListener("consumer:group:rebalance", this.onRebalance);
|
|
96
|
+
this.consumer.removeListener("consumer:group:join", this.onJoin);
|
|
135
97
|
await this.consumer.close(true);
|
|
136
98
|
this.consumer = undefined;
|
|
137
99
|
}
|
|
138
100
|
}
|
|
139
101
|
seek(topic, partition, offset) {
|
|
140
102
|
this.logger.warn(`Seek requested`, { topic, partition, offset });
|
|
141
|
-
this.
|
|
142
|
-
void this.restartTopicStream(topic).catch((err) => {
|
|
103
|
+
void this.topics.requestSeek(topic, partition, offset).catch((err) => {
|
|
143
104
|
this.logger.error(`Failed to restart stream after seek`, {
|
|
144
105
|
topic,
|
|
145
106
|
err,
|
|
@@ -174,14 +135,7 @@ let KafkaConsumer = class KafkaConsumer {
|
|
|
174
135
|
return true;
|
|
175
136
|
}
|
|
176
137
|
this.assertions.assert(this.eachMessage !== undefined, "Must call run before subscribing");
|
|
177
|
-
const startedTopics =
|
|
178
|
-
for (const topic of topics) {
|
|
179
|
-
if (this.streams.has(topic)) {
|
|
180
|
-
continue;
|
|
181
|
-
}
|
|
182
|
-
await this.startTopicStream(topic);
|
|
183
|
-
startedTopics.push(topic);
|
|
184
|
-
}
|
|
138
|
+
const startedTopics = await this.topics.openMissing(topics);
|
|
185
139
|
if (startedTopics.length) {
|
|
186
140
|
await this.waitForAssignment(startedTopics);
|
|
187
141
|
}
|
|
@@ -198,175 +152,52 @@ let KafkaConsumer = class KafkaConsumer {
|
|
|
198
152
|
return;
|
|
199
153
|
}
|
|
200
154
|
this.logger.info("Waiting for kafka consumer group join", { topics });
|
|
201
|
-
await this.
|
|
155
|
+
await this.groupJoin.wait(this.consumer, this.logger);
|
|
202
156
|
this.logger.info("Kafka consumer group join settled", {
|
|
203
157
|
topics,
|
|
204
158
|
assignments: this.consumer.assignments ?? [],
|
|
205
159
|
});
|
|
206
|
-
this.
|
|
160
|
+
this.publishAssignments();
|
|
207
161
|
}
|
|
208
162
|
getSubscribedTopics() {
|
|
209
|
-
return
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
consumer.removeListener("consumer:group:join", onJoin);
|
|
228
|
-
if (this.cancelJoinWait === finish) {
|
|
229
|
-
this.cancelJoinWait = undefined;
|
|
163
|
+
return this.topics.names();
|
|
164
|
+
}
|
|
165
|
+
publishAssignments() {
|
|
166
|
+
this.assignments.publish(this.consumer?.assignments, this.topics.names());
|
|
167
|
+
}
|
|
168
|
+
readTopic(topic, stream) {
|
|
169
|
+
const reader = new KafkaTopicReader(topic, stream, {
|
|
170
|
+
offsetMarks: this.offsetMarks,
|
|
171
|
+
ownsStream: () => this.topics.owns(topic, stream),
|
|
172
|
+
assignmentsKnown: () => !!this.consumer?.assignments,
|
|
173
|
+
isPartitionAssigned: (partition) => this.isPartitionAssigned(topic, partition),
|
|
174
|
+
handleRecord: (record) => this.handleRecord(topic, record),
|
|
175
|
+
registerDiscard: (discard) => {
|
|
176
|
+
this.discardQueued.set(topic, discard);
|
|
177
|
+
},
|
|
178
|
+
unregisterDiscard: (discard) => {
|
|
179
|
+
if (this.discardQueued.get(topic) === discard) {
|
|
180
|
+
this.discardQueued.delete(topic);
|
|
230
181
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
}, ASSIGNMENT_TIMEOUT.toMillis());
|
|
238
|
-
this.cancelJoinWait = finish;
|
|
239
|
-
consumer.on("consumer:group:join", onJoin);
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
enqueueTopicOperation(topic, operation) {
|
|
243
|
-
const previous = this.topicOperations.get(topic) ?? Promise.resolve();
|
|
244
|
-
const run = previous.then(operation, operation);
|
|
245
|
-
this.topicOperations.set(topic, run.then(() => undefined, () => undefined));
|
|
246
|
-
return run;
|
|
247
|
-
}
|
|
248
|
-
async restartTopicStream(topic) {
|
|
249
|
-
await this.enqueueTopicOperation(topic, async () => {
|
|
250
|
-
if (!this.consumer || this.closed) {
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
await this.stopTopicStream(topic);
|
|
254
|
-
if (this.closed) {
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
try {
|
|
258
|
-
await this.startTopicStream(topic);
|
|
259
|
-
}
|
|
260
|
-
catch (err) {
|
|
261
|
-
this.logger.error(`Failed to restart stream for ${topic}`, { err });
|
|
262
|
-
await this.retryTopicStream(topic);
|
|
263
|
-
}
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
async stopTopicStream(topic) {
|
|
267
|
-
const stream = this.streams.get(topic);
|
|
268
|
-
this.clearFetchWatch(topic, stream);
|
|
269
|
-
if (!stream) {
|
|
270
|
-
const pendingClose = this.closing.get(topic);
|
|
271
|
-
if (pendingClose) {
|
|
272
|
-
await pendingClose;
|
|
273
|
-
}
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
this.streams.delete(topic);
|
|
277
|
-
const closed = this.waitForStreamClose(stream);
|
|
278
|
-
this.closing.set(topic, closed);
|
|
279
|
-
try {
|
|
280
|
-
await stream.close();
|
|
281
|
-
}
|
|
282
|
-
catch (err) {
|
|
283
|
-
this.logger.warn(`Error stopping stream for ${topic}`, { err });
|
|
284
|
-
}
|
|
285
|
-
await closed;
|
|
286
|
-
if (this.closing.get(topic) === closed) {
|
|
287
|
-
this.closing.delete(topic);
|
|
288
|
-
}
|
|
289
|
-
const loop = this.streamLoops.get(topic);
|
|
290
|
-
this.streamLoops.delete(topic);
|
|
291
|
-
if (loop) {
|
|
292
|
-
await loop.catch(() => undefined);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
waitForStreamClose(stream) {
|
|
296
|
-
if (stream.closed) {
|
|
297
|
-
return Promise.resolve();
|
|
298
|
-
}
|
|
299
|
-
return new Promise((resolve) => {
|
|
300
|
-
stream.once("close", () => resolve());
|
|
301
|
-
});
|
|
302
|
-
}
|
|
303
|
-
async startTopicStream(topic) {
|
|
304
|
-
if (!this.consumer || this.closed) {
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
if (this.streams.has(topic)) {
|
|
308
|
-
return;
|
|
309
|
-
}
|
|
310
|
-
const seeks = this.pendingSeeks.get(topic);
|
|
311
|
-
const hasSeeks = seeks.size > 0;
|
|
312
|
-
if (hasSeeks) {
|
|
313
|
-
const metadata = await this.consumer.metadata({ topics: [topic] });
|
|
314
|
-
const topicMeta = metadata.topics.get(topic);
|
|
315
|
-
const offsets = Array.from(seeks.entries()).map(([partition, offset]) => ({
|
|
316
|
-
topic,
|
|
317
|
-
partition,
|
|
318
|
-
offset,
|
|
319
|
-
leaderEpoch: topicMeta?.partitions[partition]?.leaderEpoch ?? 0,
|
|
320
|
-
}));
|
|
321
|
-
await this.consumer.commit({ offsets });
|
|
322
|
-
seeks.clear();
|
|
323
|
-
}
|
|
324
|
-
this.logger.info(`Starting message stream for topic ${topic}`);
|
|
325
|
-
const stream = await this.consumer.consume({
|
|
326
|
-
topics: [topic],
|
|
327
|
-
autocommit: false,
|
|
328
|
-
mode: "committed",
|
|
329
|
-
fallbackMode: "latest",
|
|
330
|
-
highWaterMark: KAFKA_TOPIC_BUFFER_SIZE,
|
|
331
|
-
onDeserializationError: (context) => {
|
|
332
|
-
this.logger.error(`Failed to deserialize message on ${context.topic}`, {
|
|
333
|
-
err: context.error,
|
|
334
|
-
partition: context.partition,
|
|
335
|
-
offset: context.offset.toString(),
|
|
336
|
-
});
|
|
337
|
-
return "skip";
|
|
182
|
+
},
|
|
183
|
+
setBackpressured: (paused) => {
|
|
184
|
+
this.fetchStalls.notePaused(topic, paused);
|
|
185
|
+
},
|
|
186
|
+
onEnded: (streamError) => {
|
|
187
|
+
this.topics.abandon(topic, stream, streamError);
|
|
338
188
|
},
|
|
339
189
|
});
|
|
340
|
-
|
|
341
|
-
this.streams.set(topic, stream);
|
|
342
|
-
this.watchFetches(topic, stream);
|
|
343
|
-
consumerCount.set({ topic }, 1);
|
|
344
|
-
const loop = this.processStream(topic, stream).finally(() => {
|
|
345
|
-
if (this.streamLoops.get(topic) === loop) {
|
|
346
|
-
this.streamLoops.delete(topic);
|
|
347
|
-
}
|
|
348
|
-
});
|
|
349
|
-
this.streamLoops.set(topic, loop);
|
|
190
|
+
return reader.read();
|
|
350
191
|
}
|
|
351
|
-
|
|
352
|
-
return
|
|
192
|
+
handleRecord(topic, record) {
|
|
193
|
+
return this.records.handle(topic, record);
|
|
353
194
|
}
|
|
354
|
-
|
|
355
|
-
const
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
timer = setTimeout(() => {
|
|
361
|
-
reject(new Error(`Timed out committing Kafka offset after ${timeoutMs}ms`));
|
|
362
|
-
}, timeoutMs);
|
|
363
|
-
timer.unref();
|
|
364
|
-
});
|
|
365
|
-
return Promise.race([pending, timeout]).finally(() => {
|
|
366
|
-
if (timer) {
|
|
367
|
-
clearTimeout(timer);
|
|
368
|
-
}
|
|
369
|
-
});
|
|
195
|
+
isPartitionAssigned(topic, partition) {
|
|
196
|
+
const assignments = this.consumer?.assignments;
|
|
197
|
+
if (!assignments) {
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
return assignments.some((assignment) => assignment.topic === topic && assignment.partitions.includes(partition));
|
|
370
201
|
}
|
|
371
202
|
assignedPartitionCount(topic) {
|
|
372
203
|
const assignments = this.consumer?.assignments;
|
|
@@ -381,214 +212,15 @@ let KafkaConsumer = class KafkaConsumer {
|
|
|
381
212
|
}
|
|
382
213
|
return count;
|
|
383
214
|
}
|
|
384
|
-
watchFetches(topic, stream) {
|
|
385
|
-
const onFetch = () => {
|
|
386
|
-
if (this.ownsStream(topic, stream)) {
|
|
387
|
-
this.armFetchStall(topic, stream);
|
|
388
|
-
}
|
|
389
|
-
};
|
|
390
|
-
this.fetchListeners.set(topic, onFetch);
|
|
391
|
-
stream.on("fetch", onFetch);
|
|
392
|
-
this.armFetchStall(topic, stream);
|
|
393
|
-
}
|
|
394
|
-
armFetchStall(topic, stream) {
|
|
395
|
-
const previous = this.fetchStallTimers.get(topic);
|
|
396
|
-
if (previous) {
|
|
397
|
-
clearTimeout(previous);
|
|
398
|
-
}
|
|
399
|
-
const timeoutMs = readPositiveInt(process.env.KAFKA_FETCH_STALL_MS, DEFAULT_KAFKA_FETCH_STALL_MS);
|
|
400
|
-
const timer = setTimeout(() => this.onFetchStall(topic, stream), timeoutMs);
|
|
401
|
-
timer.unref();
|
|
402
|
-
this.fetchStallTimers.set(topic, timer);
|
|
403
|
-
}
|
|
404
|
-
onFetchStall(topic, stream) {
|
|
405
|
-
if (!this.ownsStream(topic, stream)) {
|
|
406
|
-
return;
|
|
407
|
-
}
|
|
408
|
-
if (this.assignedPartitionCount(topic) === 0 || stream.readableLength > 0) {
|
|
409
|
-
this.armFetchStall(topic, stream);
|
|
410
|
-
return;
|
|
411
|
-
}
|
|
412
|
-
this.logger.error(`Stream for ${topic} stopped fetching; restarting`);
|
|
413
|
-
void this.restartTopicStream(topic).catch((err) => {
|
|
414
|
-
this.logger.error(`Failed to restart stream for ${topic}`, { err });
|
|
415
|
-
});
|
|
416
|
-
}
|
|
417
|
-
clearFetchWatch(topic, stream) {
|
|
418
|
-
const timer = this.fetchStallTimers.get(topic);
|
|
419
|
-
if (timer) {
|
|
420
|
-
clearTimeout(timer);
|
|
421
|
-
this.fetchStallTimers.delete(topic);
|
|
422
|
-
}
|
|
423
|
-
const onFetch = this.fetchListeners.get(topic);
|
|
424
|
-
if (onFetch) {
|
|
425
|
-
this.fetchListeners.delete(topic);
|
|
426
|
-
stream?.removeListener("fetch", onFetch);
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
clearAllFetchWatches() {
|
|
430
|
-
const topics = new Set([
|
|
431
|
-
...this.fetchStallTimers.keys(),
|
|
432
|
-
...this.fetchListeners.keys(),
|
|
433
|
-
]);
|
|
434
|
-
for (const topic of topics) {
|
|
435
|
-
this.clearFetchWatch(topic, this.streams.get(topic));
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
async recoverTopic(topic, stream) {
|
|
439
|
-
await this.enqueueTopicOperation(topic, async () => {
|
|
440
|
-
try {
|
|
441
|
-
await stream.close();
|
|
442
|
-
}
|
|
443
|
-
catch (err) {
|
|
444
|
-
this.logger.warn(`Error closing failed stream for ${topic}`, { err });
|
|
445
|
-
}
|
|
446
|
-
await this.waitForStreamClose(stream);
|
|
447
|
-
if (this.closing.has(topic)) {
|
|
448
|
-
this.closing.delete(topic);
|
|
449
|
-
}
|
|
450
|
-
await this.retryTopicStream(topic);
|
|
451
|
-
});
|
|
452
|
-
}
|
|
453
|
-
async retryTopicStream(topic) {
|
|
454
|
-
while (this.consumer && !this.closed && !this.streams.has(topic)) {
|
|
455
|
-
await sleep(STREAM_RESTART_DELAY_MS);
|
|
456
|
-
if (!this.consumer || this.closed || this.streams.has(topic)) {
|
|
457
|
-
return;
|
|
458
|
-
}
|
|
459
|
-
try {
|
|
460
|
-
await this.startTopicStream(topic);
|
|
461
|
-
}
|
|
462
|
-
catch (err) {
|
|
463
|
-
this.logger.error(`Failed to restart stream for ${topic}`, { err });
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
async processStream(topic, stream) {
|
|
468
|
-
const partitionQueues = new DefaultMap(() => Promise.resolve());
|
|
469
|
-
const inFlight = new DefaultMap(() => 0);
|
|
470
|
-
const releasers = new DefaultMap(() => []);
|
|
471
|
-
let streamError;
|
|
472
|
-
const release = (partition) => {
|
|
473
|
-
const waiters = releasers.get(partition).splice(0);
|
|
474
|
-
for (const waiter of waiters) {
|
|
475
|
-
waiter();
|
|
476
|
-
}
|
|
477
|
-
};
|
|
478
|
-
const waitForCapacity = (partition) => {
|
|
479
|
-
if (inFlight.get(partition) < KAFKA_PARTITION_BUFFER_SIZE ||
|
|
480
|
-
!this.ownsStream(topic, stream)) {
|
|
481
|
-
return Promise.resolve();
|
|
482
|
-
}
|
|
483
|
-
return new Promise((resolve) => {
|
|
484
|
-
const waiters = releasers.get(partition);
|
|
485
|
-
waiters.push(resolve);
|
|
486
|
-
if (inFlight.get(partition) < KAFKA_PARTITION_BUFFER_SIZE ||
|
|
487
|
-
!this.ownsStream(topic, stream)) {
|
|
488
|
-
const index = waiters.indexOf(resolve);
|
|
489
|
-
if (index >= 0) {
|
|
490
|
-
waiters.splice(index, 1);
|
|
491
|
-
}
|
|
492
|
-
resolve();
|
|
493
|
-
}
|
|
494
|
-
});
|
|
495
|
-
};
|
|
496
|
-
try {
|
|
497
|
-
for await (const message of stream) {
|
|
498
|
-
if (!this.ownsStream(topic, stream)) {
|
|
499
|
-
break;
|
|
500
|
-
}
|
|
501
|
-
const partition = message.partition;
|
|
502
|
-
while (inFlight.get(partition) >= KAFKA_PARTITION_BUFFER_SIZE &&
|
|
503
|
-
this.ownsStream(topic, stream)) {
|
|
504
|
-
await waitForCapacity(partition);
|
|
505
|
-
}
|
|
506
|
-
if (!this.ownsStream(topic, stream)) {
|
|
507
|
-
break;
|
|
508
|
-
}
|
|
509
|
-
inFlight.set(partition, inFlight.get(partition) + 1);
|
|
510
|
-
const previous = partitionQueues.get(partition);
|
|
511
|
-
const next = previous.then(async () => {
|
|
512
|
-
let commitOffset = true;
|
|
513
|
-
try {
|
|
514
|
-
const result = await this.eachMessage({
|
|
515
|
-
topic: message.topic,
|
|
516
|
-
partition: message.partition,
|
|
517
|
-
message: {
|
|
518
|
-
key: message.key ?? null,
|
|
519
|
-
value: message.value ?? null,
|
|
520
|
-
offset: message.offset.toString(),
|
|
521
|
-
},
|
|
522
|
-
});
|
|
523
|
-
if (result?.commit === false) {
|
|
524
|
-
commitOffset = false;
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
catch (err) {
|
|
528
|
-
this.logger.error(`Error handling message on ${topic}`, {
|
|
529
|
-
err,
|
|
530
|
-
partition: message.partition,
|
|
531
|
-
offset: message.offset.toString(),
|
|
532
|
-
});
|
|
533
|
-
}
|
|
534
|
-
finally {
|
|
535
|
-
if (commitOffset) {
|
|
536
|
-
try {
|
|
537
|
-
await this.commitRecord(() => message.commit());
|
|
538
|
-
}
|
|
539
|
-
catch (commitErr) {
|
|
540
|
-
this.logger.error(`Failed to commit offset on ${topic}`, {
|
|
541
|
-
err: commitErr,
|
|
542
|
-
partition: message.partition,
|
|
543
|
-
offset: message.offset.toString(),
|
|
544
|
-
});
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
inFlight.set(partition, inFlight.get(partition) - 1);
|
|
548
|
-
release(partition);
|
|
549
|
-
}
|
|
550
|
-
});
|
|
551
|
-
partitionQueues.set(partition, next);
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
catch (err) {
|
|
555
|
-
streamError = err;
|
|
556
|
-
}
|
|
557
|
-
finally {
|
|
558
|
-
await Promise.all(partitionQueues.values()).catch(() => undefined);
|
|
559
|
-
if (this.ownsStream(topic, stream)) {
|
|
560
|
-
this.streams.delete(topic);
|
|
561
|
-
const closed = this.waitForStreamClose(stream);
|
|
562
|
-
this.closing.set(topic, closed);
|
|
563
|
-
if (streamError) {
|
|
564
|
-
this.logger.error(`Stream for ${topic} ended; restarting`, {
|
|
565
|
-
err: streamError,
|
|
566
|
-
});
|
|
567
|
-
}
|
|
568
|
-
else {
|
|
569
|
-
this.logger.warn(`Stream for ${topic} ended; restarting`);
|
|
570
|
-
}
|
|
571
|
-
setImmediate(() => {
|
|
572
|
-
void this.recoverTopic(topic, stream).catch((err) => {
|
|
573
|
-
this.logger.error(`Failed to restart stream for ${topic}`, {
|
|
574
|
-
err,
|
|
575
|
-
});
|
|
576
|
-
});
|
|
577
|
-
});
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
215
|
};
|
|
582
216
|
KafkaConsumer = __decorate([
|
|
583
217
|
injectable(),
|
|
584
218
|
__param(0, inject(TYPES.LoggerService)),
|
|
585
219
|
__param(1, inject(AssertionHelper)),
|
|
586
|
-
__param(2, inject(
|
|
587
|
-
__param(3,
|
|
588
|
-
__param(
|
|
589
|
-
__param(4, optional()),
|
|
220
|
+
__param(2, inject(KafkaClientFactory)),
|
|
221
|
+
__param(3, injectedEventHandlerConfig("kafka.groupId")),
|
|
222
|
+
__param(3, optional()),
|
|
590
223
|
__metadata("design:paramtypes", [Object, AssertionHelper,
|
|
591
|
-
AppCleanupHelper,
|
|
592
224
|
KafkaClientFactory, Object])
|
|
593
225
|
], KafkaConsumer);
|
|
594
226
|
export { KafkaConsumer };
|