-
-
Notifications
You must be signed in to change notification settings - Fork 533
/
Copy pathsendMessages.js
170 lines (146 loc) · 5.94 KB
/
sendMessages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const { KafkaJSMetadataNotLoaded } = require('../errors')
const { staleMetadata } = require('../protocol/error')
const groupMessagesPerPartition = require('./groupMessagesPerPartition')
const createTopicData = require('./createTopicData')
const responseSerializer = require('./responseSerializer')
const { keys } = Object
/**
* @param {Object} options
* @param {import("../../types").Logger} options.logger
* @param {import("../../types").Cluster} options.cluster
* @param {ReturnType<import("../../types").ICustomPartitioner>} options.partitioner
* @param {import("./eosManager").EosManager} options.eosManager
* @param {import("../retry").Retrier} options.retrier
*/
module.exports = ({ logger, cluster, partitioner, eosManager, retrier }) => {
return async ({ acks, timeout, compression, topicMessages }) => {
/** @type {Map<import("../../types").Broker, any[]>} */
const responsePerBroker = new Map()
/** @param {Map<import("../../types").Broker, any[]>} responsePerBroker */
const createProducerRequests = async responsePerBroker => {
const topicMetadata = new Map()
await cluster.refreshMetadataIfNecessary()
for (const { topic, messages } of topicMessages) {
const partitionMetadata = cluster.findTopicPartitionMetadata(topic)
if (partitionMetadata.length === 0) {
logger.debug('Producing to topic without metadata', {
topic,
targetTopics: Array.from(cluster.targetTopics),
})
throw new KafkaJSMetadataNotLoaded('Producing to topic without metadata')
}
const messagesPerPartition = groupMessagesPerPartition({
topic,
partitionMetadata,
messages,
partitioner,
})
const partitions = keys(messagesPerPartition)
const partitionsPerLeader = cluster.findLeaderForPartitions(topic, partitions)
const leaders = keys(partitionsPerLeader)
topicMetadata.set(topic, {
partitionsPerLeader,
messagesPerPartition,
})
for (const nodeId of leaders) {
const broker = await cluster.findBroker({ nodeId })
if (!responsePerBroker.has(broker)) {
responsePerBroker.set(broker, null)
}
}
}
const brokers = Array.from(responsePerBroker.keys())
const brokersWithoutResponse = brokers.filter(broker => !responsePerBroker.get(broker))
return brokersWithoutResponse.map(async broker => {
const entries = Array.from(topicMetadata.entries())
const topicDataForBroker = entries
.filter(([_, { partitionsPerLeader }]) => !!partitionsPerLeader[broker.nodeId])
.map(([topic, { partitionsPerLeader, messagesPerPartition, sequencePerPartition }]) => ({
topic,
partitions: partitionsPerLeader[broker.nodeId],
messagesPerPartition,
}))
const topicData = createTopicData(topicDataForBroker)
await eosManager.acquireBrokerLock(broker)
try {
if (eosManager.isTransactional()) {
await eosManager.addPartitionsToTransaction(topicData)
}
topicData.forEach(({ topic, partitions }) => {
partitions.forEach(entry => {
entry['firstSequence'] = eosManager.getSequence(topic, entry.partition)
eosManager.updateSequence(topic, entry.partition, entry.messages.length)
})
})
let response
try {
response = await broker.produce({
transactionalId: eosManager.isTransactional()
? eosManager.getTransactionalId()
: undefined,
producerId: eosManager.getProducerId(),
producerEpoch: eosManager.getProducerEpoch(),
acks,
timeout,
compression,
topicData,
})
} catch (e) {
topicData.forEach(({ topic, partitions }) => {
partitions.forEach(entry => {
eosManager.updateSequence(topic, entry.partition, -entry.messages.length)
})
})
throw e
}
const expectResponse = acks !== 0
const formattedResponse = expectResponse ? responseSerializer(response) : []
responsePerBroker.set(broker, formattedResponse)
} catch (e) {
responsePerBroker.delete(broker)
throw e
} finally {
await eosManager.releaseBrokerLock(broker)
}
})
}
return retrier(async (bail, retryCount, retryTime) => {
const topics = topicMessages.map(({ topic }) => topic)
await cluster.addMultipleTargetTopics(topics)
try {
const requests = await createProducerRequests(responsePerBroker)
await Promise.all(requests)
return Array.from(responsePerBroker.values()).flat()
} catch (e) {
if (e.name === 'KafkaJSConnectionClosedError') {
cluster.removeBroker({ host: e.host, port: e.port })
}
if (!cluster.isConnected()) {
logger.debug(`Cluster has disconnected, reconnecting: ${e.message}`, {
retryCount,
retryTime,
})
await cluster.connect()
await cluster.refreshMetadata()
throw e
}
// This is necessary in case the metadata is stale and the number of partitions
// for this topic has increased in the meantime
if (
staleMetadata(e) ||
e.name === 'KafkaJSMetadataNotLoaded' ||
e.name === 'KafkaJSConnectionError' ||
e.name === 'KafkaJSConnectionClosedError' ||
(e.name === 'KafkaJSProtocolError' && e.retriable)
) {
logger.error(`Failed to send messages: ${e.message}`, { retryCount, retryTime })
await cluster.refreshMetadata()
throw e
}
logger.error(`${e.message}`, { retryCount, retryTime })
if (e.retriable) throw e
bail(e)
}
})
}
}