kafka-ts 0.0.6-beta.4 → 0.0.6-beta.5
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/dist/producer/producer.js +56 -47
- package/package.json +1 -1
|
@@ -15,6 +15,7 @@ const messages_to_topic_partition_leaders_1 = require("../distributors/messages-
|
|
|
15
15
|
const partitioner_1 = require("../distributors/partitioner");
|
|
16
16
|
const metadata_1 = require("../metadata");
|
|
17
17
|
const delay_1 = require("../utils/delay");
|
|
18
|
+
const error_1 = require("../utils/error");
|
|
18
19
|
const memo_1 = require("../utils/memo");
|
|
19
20
|
const tracer_1 = require("../utils/tracer");
|
|
20
21
|
const trace = (0, tracer_1.createTracer)('Producer');
|
|
@@ -43,55 +44,63 @@ class Producer {
|
|
|
43
44
|
const topics = Array.from(new Set(messages.map((message) => message.topic)));
|
|
44
45
|
await this.metadata.fetchMetadataIfNecessary({ topics, allowTopicAutoCreation });
|
|
45
46
|
const nodeTopicPartitionMessages = (0, messages_to_topic_partition_leaders_1.distributeMessagesToTopicPartitionLeaders)(messages.map((message) => ({ ...message, partition: this.partition(message) })), this.metadata.getTopicPartitionLeaderIds());
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
baseTimestamp
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
maxTimestamp
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
lastOffsetDelta: messages.length - 1,
|
|
67
|
-
baseTimestamp: baseTimestamp ?? 0n,
|
|
68
|
-
maxTimestamp: maxTimestamp ?? 0n,
|
|
69
|
-
producerId: this.producerId,
|
|
70
|
-
producerEpoch: 0,
|
|
71
|
-
baseSequence: this.getSequence(topic, partitionIndex),
|
|
72
|
-
records: messages.map((message, index) => ({
|
|
47
|
+
try {
|
|
48
|
+
await Promise.all(Object.entries(nodeTopicPartitionMessages).map(async ([nodeId, topicPartitionMessages]) => {
|
|
49
|
+
const topicData = Object.entries(topicPartitionMessages).map(([topic, partitionMessages]) => ({
|
|
50
|
+
name: topic,
|
|
51
|
+
partitionData: Object.entries(partitionMessages).map(([partition, messages]) => {
|
|
52
|
+
const partitionIndex = parseInt(partition);
|
|
53
|
+
let baseTimestamp;
|
|
54
|
+
let maxTimestamp;
|
|
55
|
+
messages.forEach(({ timestamp = defaultTimestamp }) => {
|
|
56
|
+
if (!baseTimestamp || timestamp < baseTimestamp) {
|
|
57
|
+
baseTimestamp = timestamp;
|
|
58
|
+
}
|
|
59
|
+
if (!maxTimestamp || timestamp > maxTimestamp) {
|
|
60
|
+
maxTimestamp = timestamp;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
return {
|
|
64
|
+
index: partitionIndex,
|
|
65
|
+
baseOffset: 0n,
|
|
66
|
+
partitionLeaderEpoch: -1,
|
|
73
67
|
attributes: 0,
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
68
|
+
lastOffsetDelta: messages.length - 1,
|
|
69
|
+
baseTimestamp: baseTimestamp ?? 0n,
|
|
70
|
+
maxTimestamp: maxTimestamp ?? 0n,
|
|
71
|
+
producerId: this.producerId,
|
|
72
|
+
producerEpoch: 0,
|
|
73
|
+
baseSequence: this.getSequence(topic, partitionIndex),
|
|
74
|
+
records: messages.map((message, index) => ({
|
|
75
|
+
attributes: 0,
|
|
76
|
+
timestampDelta: (message.timestamp ?? defaultTimestamp) - (baseTimestamp ?? 0n),
|
|
77
|
+
offsetDelta: index,
|
|
78
|
+
key: message.key ?? null,
|
|
79
|
+
value: message.value,
|
|
80
|
+
headers: Object.entries(message.headers ?? {}).map(([key, value]) => ({ key, value })),
|
|
81
|
+
})),
|
|
82
|
+
};
|
|
83
|
+
}),
|
|
84
|
+
}));
|
|
85
|
+
await this.cluster.sendRequestToNode(parseInt(nodeId))(api_1.API.PRODUCE, {
|
|
86
|
+
transactionalId: null,
|
|
87
|
+
acks,
|
|
88
|
+
timeoutMs: 5000,
|
|
89
|
+
topicData,
|
|
92
90
|
});
|
|
93
|
-
|
|
94
|
-
|
|
91
|
+
topicData.forEach(({ name, partitionData }) => {
|
|
92
|
+
partitionData.forEach(({ index, records }) => {
|
|
93
|
+
this.updateSequence(name, index, records.length);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
}));
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
if ((error instanceof error_1.KafkaTSApiError) && error.errorCode === api_1.API_ERROR.OUT_OF_ORDER_SEQUENCE_NUMBER) {
|
|
100
|
+
await this.initProducerId();
|
|
101
|
+
}
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
95
104
|
}
|
|
96
105
|
async close() {
|
|
97
106
|
await this.cluster.disconnect();
|