-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbackbeatConsumer.js
More file actions
400 lines (353 loc) · 15 KB
/
backbeatConsumer.js
File metadata and controls
400 lines (353 loc) · 15 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
const assert = require('assert');
const sinon = require('sinon');
const BackbeatConsumer = require('../../lib/BackbeatConsumer');
const { CODES } = require('node-rdkafka');
const { kafka } = require('../config.json');
const { BreakerState } = require('breakbeat').CircuitBreaker;
class BackbeatConsumerMock extends BackbeatConsumer {
_init() {}
}
describe('backbeatConsumer', () => {
afterEach(() => {
process.env.KAFKA_TOPIC_PREFIX = '';
});
it('should use default topic name without prefix', () => {
const backbeatConsumer = new BackbeatConsumer({
kafka,
groupId: 'unittest-group',
topic: 'my-test-topic',
});
assert.strictEqual(backbeatConsumer._topic, 'my-test-topic');
});
it('should use default topic name with prefix', () => {
process.env.KAFKA_TOPIC_PREFIX = 'testing.';
const backbeatConsumer = new BackbeatConsumer({
kafka,
groupId: 'unittest-group',
topic: 'my-test-topic',
});
assert.strictEqual(backbeatConsumer._topic, 'testing.my-test-topic');
});
describe('pause/resume topic partitions on circuit breaker', () => {
let consumer;
beforeEach(() => {
consumer = new BackbeatConsumerMock({
kafka,
groupId: 'unittest-group',
topic: 'my-test-topic',
});
const mockConsumer = {
pause: sinon.stub().returns(),
resume: sinon.stub().returns(),
isConnected: () => false,
};
consumer._consumer = mockConsumer;
});
afterEach(() => {
sinon.restore();
});
describe('_pauseAssignments', () => {
it('should not pause when consumer not connected', () => {
consumer._consumer.isConnected = () => false;
consumer._consumer.subscription = () => ['example-topic'];
consumer._pauseAssignments();
assert(consumer._consumer.pause.notCalled);
});
it('should not call pause when no paritions assigned', () => {
consumer._consumer.isConnected = () => true;
consumer._consumer.subscription = () => ['example-topic'];
consumer._consumer.assignments = () => [];
consumer._pauseAssignments();
assert(consumer._consumer.pause.notCalled);
});
it('should pause all assignments', () => {
consumer._consumer.isConnected = () => true;
consumer._consumer.subscription = () => ['example-topic'];
const assignments = [
{ topic: 'my-test-topic', partition: 0 },
{ topic: 'my-test-topic', partition: 1 },
{ topic: 'my-test-topic', partition: 2 },
];
consumer._consumer.assignments = () => assignments;
consumer._pauseAssignments();
assert(consumer._consumer.pause.calledWithMatch([
{ topic: 'my-test-topic', partition: 0 },
{ topic: 'my-test-topic', partition: 1 },
{ topic: 'my-test-topic', partition: 2 },
]));
});
});
describe('_resumePausedPartitions', () => {
it('should not resume when consumer not connected', () => {
consumer._consumer.isConnected = () => false;
consumer._consumer.subscription = () => ['example-topic'];
consumer._resumePausedPartitions();
assert(consumer._consumer.resume.notCalled);
});
it('should not call resume when no paritions are paused', () => {
consumer._consumer.isConnected = () => true;
consumer._consumer.subscription = () => ['example-topic'];
consumer._consumer.assignments = () => [];
consumer._resumePausedPartitions();
assert(consumer._consumer.resume.notCalled);
});
it('should resume all paused partitions', () => {
consumer._consumer.isConnected = () => true;
consumer._consumer.subscription = () => ['example-topic'];
consumer._consumer.assignments = () => [
{ topic: 'my-test-topic', partition: 0 },
{ topic: 'my-test-topic', partition: 1 },
{ topic: 'my-test-topic', partition: 2 },
];
consumer._resumePausedPartitions();
assert(consumer._consumer.resume.calledWithMatch([
{ topic: 'my-test-topic', partition: 0 },
{ topic: 'my-test-topic', partition: 1 },
{ topic: 'my-test-topic', partition: 2 },
]));
});
});
describe('_onCircuitBreakerStateChanged', () => {
it('should resume consumption when circuit breaker state is nominal', () => {
const stub = sinon.stub(consumer, '_resumePausedPartitions');
consumer._onCircuitBreakerStateChanged(BreakerState.Nominal);
assert(stub.calledOnce);
});
it('should pause consumption when circuit breaker state got tripped', () => {
const stub = sinon.stub(consumer, '_pauseAssignments');
consumer._onCircuitBreakerStateChanged(BreakerState.Tripped);
assert(stub.calledOnce);
});
it('should keep old state when circuit breaker state is stabilizing', () => {
const resumeStub = sinon.stub(consumer, '_resumePausedPartitions');
const pauseStub = sinon.stub(consumer, '_pauseAssignments');
consumer._onCircuitBreakerStateChanged(BreakerState.Stabilizing);
assert(resumeStub.notCalled);
assert(pauseStub.notCalled);
});
it('should do nothing when state is unknown', () => {
const resumeStub = sinon.stub(consumer, '_resumePausedPartitions');
const pauseStub = sinon.stub(consumer, '_pauseAssignments');
consumer._onCircuitBreakerStateChanged(-1);
assert(resumeStub.notCalled);
assert(pauseStub.notCalled);
});
});
});
describe('sequentialy consume from topic', () => {
let consumer;
beforeEach(() => {
consumer = new BackbeatConsumerMock({
kafka,
groupId: 'unittest-group',
topic: 'my-test-topic',
});
});
afterEach(() => {
sinon.restore();
clearTimeout(consumer._tryConsumedTimeout);
});
it('should not consume new messages when a call to consume is already in progress', () => {
consumer._concurrency = 2;
consumer._consumer = {
consume: sinon.stub(),
};
consumer._processingQueue = {
running: sinon.stub()
.returns(0)
.onFirstCall().returns(1),
length: () => 0,
};
// consume() is called here but hangs as we never return
consumer._tryConsume();
// this call should return without calling consume()
consumer._tryConsume();
assert(consumer._consumer.consume.calledOnce);
assert.strictEqual(consumer._tasksCompletedSinceLastConsume, true);
});
it('should immediatly try to consume if a task completed since the last consume', done => {
consumer._concurrency = 2;
// setting to true to simulate a task completed
consumer._tasksCompletedSinceLastConsume = true;
consumer._consumer = {
consume: sinon.stub().yields(null, [{}, {}]),
};
consumer._processingQueue = {
running: sinon.stub()
.returns(0)
.onFirstCall().returns(1),
length: () => 0,
};
const tryConsumeSpy = sinon.spy(consumer, '_tryConsume');
consumer._tryConsume();
consumer._consumer.consume.onSecondCall().callsFake(() => {
assert(tryConsumeSpy.calledTwice);
assert.strictEqual(consumer._tasksCompletedSinceLastConsume, false);
done();
});
});
it('should only consume once if no tasks completed since last consume', done => {
consumer._concurrency = 2;
consumer._tasksCompletedSinceLastConsume = false;
consumer._consumer = {
consume: sinon.stub().yields(null, [{}, {}]),
};
consumer._processingQueue = {
running: sinon.stub()
.returns(0)
.onFirstCall().returns(1),
length: () => 0,
};
const tryConsumeSpy = sinon.spy(consumer, '_tryConsume');
consumer._tryConsume();
// consume is called asynchronously, waiting 500ms to make
// sure it didn't get called at the end of the first one.
setTimeout(() => {
assert(tryConsumeSpy.calledOnce);
assert.strictEqual(consumer._tasksCompletedSinceLastConsume, false);
done();
}, 500);
});
});
describe('onEntryCommittable', () => {
let consumer;
let mockConsumer;
const entry = {
topic: 'my-test-topic',
partition: 2,
offset: 280,
key: null,
timestamp: Date.now(),
};
beforeEach(() => {
consumer = new BackbeatConsumerMock({
kafka,
groupId: 'unittest-group',
topic: 'my-test-topic',
});
mockConsumer = {
offsetsStore: sinon.stub(),
subscription: sinon.stub().returns(['my-test-topic']),
isConnected: sinon.stub().returns(true),
};
consumer._consumer = mockConsumer;
// pre-register the offset as consumed so onOffsetProcessed returns a value
consumer._offsetLedger.onOffsetConsumed(entry.topic, entry.partition, entry.offset);
});
afterEach(() => {
sinon.restore();
});
it('should call offsetsStore when consumer is active and connected', () => {
consumer.onEntryCommittable(entry);
assert(mockConsumer.offsetsStore.calledOnce);
});
it('should not call offsetsStore when consumer is paused (unsubscribed)', () => {
mockConsumer.subscription.returns([]);
consumer.onEntryCommittable(entry);
assert(mockConsumer.offsetsStore.notCalled);
});
it('should not call offsetsStore when consumer is not connected', () => {
mockConsumer.isConnected.returns(false);
consumer.onEntryCommittable(entry);
assert(mockConsumer.offsetsStore.notCalled);
});
it('should not throw and always log at error level when offsetsStore throws', () => {
const errState = new Error('Local: Erroneous state');
errState.code = CODES.ERRORS.ERR__STATE;
mockConsumer.offsetsStore.throws(errState);
const errorSpy = sinon.spy(consumer._log, 'error');
assert.doesNotThrow(() => consumer.onEntryCommittable(entry));
assert(errorSpy.calledOnce);
});
it('should not throw and log at error level when offsetsStore throws an unexpected error', () => {
const unexpectedErr = new Error('unexpected kafka error');
unexpectedErr.code = -1;
mockConsumer.offsetsStore.throws(unexpectedErr);
const errorSpy = sinon.spy(consumer._log, 'error');
assert.doesNotThrow(() => consumer.onEntryCommittable(entry));
assert(errorSpy.calledOnce);
});
});
describe('_getAvailableSlotsInPipeline', () => {
let consumer;
beforeEach(() => {
consumer = new BackbeatConsumerMock({
kafka,
groupId: 'unittest-group',
topic: 'my-test-topic',
});
});
[
{
// should take into account pending requests
state: {
maxQueued : 10,
concurrency : 10,
processingQueue: {
length: () => 0,
running: () => 0,
},
nConsumePendingRequests: 5,
},
expectedSlots: 5,
},{
// should not exceed max running tasks
state: {
maxQueued : 10,
concurrency : 10,
processingQueue: {
length: () => 0,
running: () => 9,
},
nConsumePendingRequests: 0,
},
expectedSlots: 1,
},{
// should not exceed max queued
state: {
maxQueued : 10,
concurrency : 10,
processingQueue: {
length: () => 9,
running: () => 1,
},
nConsumePendingRequests: 0,
},
expectedSlots: 1,
},{
// should return 0 when exceeding max queued tasks
state: {
maxQueued : 10,
concurrency : 10,
processingQueue: {
length: () => 12,
running: () => 1,
},
nConsumePendingRequests: 0,
},
expectedSlots: 0,
},{
// should return 0 when exceeding max running tasks
state: {
maxQueued : 10,
concurrency : 10,
processingQueue: {
length: () => 3,
running: () => 13,
},
nConsumePendingRequests: 0,
},
expectedSlots: 0,
},
].forEach((params, i) => {
it(`should return available slots in pipeline (scenario ${i})`, () => {
consumer._maxQueued = params.state.maxQueued;
consumer._concurrency = params.state.concurrency;
consumer._processingQueue = params.state.processingQueue;
consumer._nConsumePendingRequests = params.state.nConsumePendingRequests;
const availableSlots = consumer._getAvailableSlotsInPipeline();
assert.strictEqual(availableSlots, params.expectedSlots);
});
});
});
});