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