-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathBackbeatProducer.js
More file actions
329 lines (296 loc) · 10.8 KB
/
BackbeatProducer.js
File metadata and controls
329 lines (296 loc) · 10.8 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
const { EventEmitter } = require('events');
const { Producer, CODES } = require('node-rdkafka');
const joi = require('joi');
const { errors, jsutil } = require('arsenal');
const Logger = require('werelogs').Logger;
const { withTopicPrefix } = require('./util/topic');
const KafkaBacklogMetrics = require('./KafkaBacklogMetrics');
const { observeKafkaStats } = require('./util/probe');
const DEFAULT_POLL_INTERVAL = 2000;
const {
KAFKA_PRODUCER_MESSAGE_MAX_BYTES,
KAFKA_PRODUCER_DEFAULT_COMPRESSION_TYPE,
KAFKA_PRODUCER_DEFAULT_REQUIRED_ACKS,
} = require('./config.joi');
const FLUSH_TIMEOUT = 5000;
class BackbeatProducer extends EventEmitter {
constructor(config) {
super();
const validConfig = joi.attempt(
config,
this.getConfigJoi(),
'invalid config params'
);
this._config = validConfig;
this.setFromConfig(validConfig);
this._ready = false;
this._log = new Logger(this.getClientId());
this._producer = new Producer(this.producerConfig, this.topicConfig);
this._producer.on('event', event => this._log.info('rdkafka.event', { event }));
this._producer.on('event.log', log => this._log.info('rdkafka.log', { log }));
this._producer.on('warning', warning => this._log.warn('rdkafka.warning', { warning }));
this._producer.on('event.throttle', throttle => this._log.info('rdkafka.throttle', { throttle }));
this._producer.on('event.stats', observeKafkaStats);
this.connect();
this.setListeners();
return this;
}
getConfigJoi() {
return joi.object(
{
kafka: joi.object({
hosts: joi.string().required(),
}).required(),
topic: joi.string(),
pollIntervalMs: joi.number().default(DEFAULT_POLL_INTERVAL),
maxRequestSize: joi.number().default(KAFKA_PRODUCER_MESSAGE_MAX_BYTES),
compressionType: joi.string().default(KAFKA_PRODUCER_DEFAULT_COMPRESSION_TYPE),
requiredAcks: joi.number().default(KAFKA_PRODUCER_DEFAULT_REQUIRED_ACKS),
}
);
}
getClientId() {
return 'BackbeatProducer';
}
getRequireAcks() {
return this._requiredAcks;
}
getAckTimeout() {
return 5000;
}
get producerConfig() {
const producerParams = {
'metadata.broker.list': this._kafkaHosts,
'message.max.bytes': this._maxRequestSize,
'dr_cb': true,
'compression.type': this._compressionType,
'statistics.interval.ms': 1000,
};
if (process.env.RDKAFKA_DEBUG_LOGS) {
producerParams.debug = process.env.RDKAFKA_DEBUG_LOGS;
}
return producerParams;
}
get topicConfig() {
return {
'request.required.acks': this.getRequireAcks(),
'request.timeout.ms': this.getAckTimeout(),
};
}
getKafkaProducer() {
return this._producer;
}
/**
* get metadata from kafka topics
* @param {object} params - call params
* @param {string} params.topic - topic name
* @param {number} params.timeout - timeout for the request
* @param {function} cb - callback: cb(err, response)
* @return {undefined}
*/
getMetadata(params, cb) {
this._producer.getMetadata(params, cb);
}
isReady() {
return this._ready;
}
setFromConfig(joiResult) {
const {
kafka,
topic,
pollIntervalMs,
maxRequestSize,
compressionType,
requiredAcks,
} = joiResult;
this._kafkaHosts = kafka.hosts;
this._topic = topic && withTopicPrefix(topic);
this._pollIntervalMs = pollIntervalMs;
this._maxRequestSize = maxRequestSize;
this._compressionType = compressionType;
this._requiredAcks = requiredAcks;
}
connect() {
this._producer.connect({ timeout: 30000 }, () => {
const opts = {
topic: withTopicPrefix('backbeat-sanitycheck'),
timeout: 10000,
};
this._producer.getMetadata(opts, err => {
if (err) {
this.emit('error', err);
}
});
});
}
setListeners() {
this._producer.on('ready', this.onReady.bind(this));
this._producer.on('event.error', this.onEventError.bind(this));
}
onDeliveryReport(error, report) {
const sendCtx = report.opaque;
const cbOnce = sendCtx.cbOnce;
sendCtx.receivedReports.push(report);
--sendCtx.pendingReportsCount;
KafkaBacklogMetrics.onDeliveryReportReceived(error);
if (error) {
this._log.error('error in delivery report retrieval', {
error: error.message,
method: 'BackbeatProducer._onDeliveryReport',
});
this.emit('error', error);
return cbOnce(error);
}
const { topic, partition, offset, timestamp } = report;
const key = report.key && report.key.toString();
this._log.debug('delivery report received',
{ topic, partition, offset, timestamp, key });
KafkaBacklogMetrics.onMessagePublished(
topic, partition, timestamp / 1000);
if (sendCtx.pendingReportsCount === 0) {
// all delivery reports received (if errors occurred, the
// callback will have been called earlier so this will be
// a no-op)
cbOnce(null, sendCtx.receivedReports);
}
return undefined;
}
onReady() {
this._ready = true;
this.emit('ready');
this._producer.setPollInterval(this._pollIntervalMs);
this._producer.on('delivery-report', this.onDeliveryReport.bind(this));
}
onEventError(error) {
// This is a bit hacky: the "broker transport failure"
// error occurs when the kafka broker reaps the idle
// connections every few minutes, and librdkafka handles
// reconnection automatically anyway, so we ignore those
// harmless errors
const config = this._config;
if (error.code === CODES.ERRORS.ERR__ALL_BROKERS_DOWN ||
error.code === CODES.ERRORS.ERR__TRANSPORT) {
this._log.error('error with producer', {
config,
error: error.message,
method: `${this.getClientId()}.constructor`,
});
this.emit('error', error);
} else {
this._log.error('rdkafka.error', { error });
}
}
/**
* sends entries/messages to the topic configured in producer
* @param {Object[]} entries - array of entries objects with properties
* key and message ([{ key: 'foo', message: 'hello world'}, ...])
* @param {callback} deliveryReportsCb - callback called when Kafka
* returns a delivery report for this message:
* deliveryReportCb(err, deliveryReports).
* NOTE: it can take a couple seconds for a delivery report to be
* received, hence it may not be a good idea to actively wait for
* this callback in the critical path.
* @return {this} current instance
*/
send(entries, deliveryReportsCb) {
if (!this._topic) {
process.nextTick(() => {
this._log.error('no topic configured to send messages to', {
method: 'BackbeatProducer.send',
});
deliveryReportsCb(errors.InternalError);
});
return this;
}
return this._sendToTopic(this._topic, entries, deliveryReportsCb);
}
/**
* sends entries/messages to the given topic
* @param {string} topic - topic to send messages to
* @param {Object[]} entries - array of entries objects with properties
* key and message ([{ key: 'foo', message: 'hello world'}, ...])
* @param {callback} deliveryReportsCb - callback called when Kafka
* returns a delivery report for this message:
* deliveryReportCb(err, deliveryReports).
* NOTE: it can take a couple seconds for a delivery report to be
* received, hence it may not be a good idea to actively wait for
* this callback in the critical path.
* @return {this} current instance
*/
sendToTopic(topic, entries, deliveryReportsCb) {
return this._sendToTopic(withTopicPrefix(topic), entries, deliveryReportsCb);
}
_sendToTopic(topic, entries, cb) {
this._log.debug('publishing entries', {
method: `${this.getClientId()}._sendToTopic`,
topic,
entryCount: entries.length,
});
if (!this._ready) {
process.nextTick(() => {
this._log.error('producer is not ready yet', {
method: `${this.getClientId()}._sendToTopic`,
ready: this._ready,
});
cb(errors.InternalError);
});
return this;
}
if (entries.length === 0) {
process.nextTick(cb);
return this;
}
const sendCtx = {
cbOnce: jsutil.once(cb),
pendingReportsCount: entries.length,
receivedReports: []
};
try {
entries.forEach(item => {
let partition = null;
if (item.partition !== undefined && item.key === 'canary') {
partition = item.partition;
}
this._producer.produce(
topic,
partition, // partition
Buffer.from(item.message), // value
item.key, // key (for keyed partitioning)
Date.now(), // timestamp
sendCtx, // opaque
item.headers || undefined // Kafka message headers
);
});
} catch (err) {
this._log.error('error publishing entries', {
method: `${this.getClientId()}._sendToTopic`,
topic,
error: err.message,
});
process.nextTick(
() => sendCtx.cbOnce(errors.InternalError.
customizeDescription(err.message)));
}
return this;
}
/**
* close client connection
* @param {callback} cb - cb()
* @return {object} this - current class instance
*/
close(cb) {
this._producer.flush(FLUSH_TIMEOUT, err => {
this._ready = false;
if (err) {
this._log.error('error flushing entries', {
error: err,
method: `${this.getClientId()}.close`,
});
}
this._producer.disconnect();
return cb(err);
});
return this;
}
}
module.exports = BackbeatProducer;