-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathtransaction_manager_spec.rb
619 lines (545 loc) · 16.5 KB
/
transaction_manager_spec.rb
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
require 'spec_helper'
describe ::Kafka::TransactionManager do
let!(:logger) { LOGGER }
let!(:cluster) { double(:cluster) }
let!(:transaction_coordinator) { double(:broker) }
let!(:manager) do
described_class.new(logger: logger, cluster: cluster)
end
before do
allow(cluster).to receive(:get_transaction_coordinator).and_return(
transaction_coordinator
)
allow(transaction_coordinator).to receive(:init_producer_id).and_return(
Kafka::Protocol::InitProducerIDResponse.new(
error_code: 0,
producer_id: 1234,
producer_epoch: 1
)
)
end
describe '#init_producer_id' do
let!(:manager) do
described_class.new(logger: logger, cluster: cluster, transactional_id: 'IDID', transactional_timeout: 600)
end
context 'producer_id not exist' do
it 'fetches producer information from transaction coordinator' do
expect(transaction_coordinator).to receive(:init_producer_id).with(
transactional_id: 'IDID',
transactional_timeout: 600
)
manager.init_producer_id
expect(manager.producer_id).to eql(1234)
expect(manager.producer_epoch).to eql(1)
end
end
context 'producer_id already exists' do
before do
manager.init_producer_id
allow(transaction_coordinator).to receive(:init_producer_id).and_return(
Kafka::Protocol::InitProducerIDResponse.new(
error_code: 0,
producer_id: 456,
producer_epoch: 2
)
)
end
context 'without force option' do
it 'does not updates producer info' do
expect(transaction_coordinator).not_to receive(:init_producer_id)
manager.init_producer_id
expect(manager.producer_id).to eql(1234)
expect(manager.producer_epoch).to eql(1)
end
end
context 'with force option' do
it 'reloads the producer information' do
expect(transaction_coordinator).to receive(:init_producer_id).with(
transactional_id: 'IDID',
transactional_timeout: 600
)
manager.init_producer_id(true)
expect(manager.producer_id).to eql(456)
expect(manager.producer_epoch).to eql(2)
end
end
end
end
describe '#next_sequence_for' do
context 'topic not exist' do
it 'returns 0' do
expect(manager.next_sequence_for('hello', 10)).to eql(0)
end
end
context 'partition not exist' do
before do
manager.update_sequence_for('hello', 0, 5)
end
it 'returns 0' do
expect(manager.next_sequence_for('hello', 10)).to eql(0)
end
end
context 'topic and partition exist' do
before do
manager.update_sequence_for('hello', 10, 8)
end
it 'returns current sequence' do
expect(manager.next_sequence_for('hello', 10)).to eql(8)
end
end
end
describe '#add_partitions_to_transaction' do
let!(:manager) do
described_class.new(
logger: logger, cluster: cluster,
transactional: true, transactional_id: '1234'
)
end
context 'no partitions in the transaction yet' do
before do
manager.init_transactions
allow(transaction_coordinator).to receive(:add_partitions_to_txn).and_return(
success_add_partitions_to_txn_response(
'hello' => [1],
'world' => [2]
)
)
end
it 'calls the partition coordinator to add all partitions' do
expect(transaction_coordinator).to receive(:add_partitions_to_txn).with(
transactional_id: '1234',
producer_id: 1234,
producer_epoch: 1,
topics: {
'hello' => [1],
'world' => [2]
}
)
manager.add_partitions_to_transaction(
'hello' => [1],
'world' => [2]
)
end
end
context 'some partitions are in the transaction' do
before do
manager.init_transactions
allow(transaction_coordinator).to receive(:add_partitions_to_txn).and_return(
success_add_partitions_to_txn_response(
'hello' => [1],
'world' => [2]
)
)
manager.add_partitions_to_transaction(
'hello' => [1],
'world' => [2]
)
allow(transaction_coordinator).to receive(:add_partitions_to_txn).and_return(
success_add_partitions_to_txn_response(
'hello' => [3],
'bye' => [4]
)
)
end
it 'calls the partition coordinator to add new partitions' do
expect(transaction_coordinator).to receive(:add_partitions_to_txn).with(
transactional_id: '1234',
producer_id: 1234,
producer_epoch: 1,
topics: {
'hello' => [3],
'bye' => [4]
}
)
manager.add_partitions_to_transaction(
'hello' => [1, 3],
'world' => [2],
'bye' => [4]
)
end
end
context 'there are not new partitions in the transaction' do
before do
manager.init_transactions
allow(transaction_coordinator).to receive(:add_partitions_to_txn).and_return(
success_add_partitions_to_txn_response(
'hello' => [1, 2],
'world' => [2]
)
)
manager.add_partitions_to_transaction(
'hello' => [1, 2],
'world' => [2]
)
end
it 'does not calls the partition coordinator' do
expect(transaction_coordinator).not_to receive(:add_partitions_to_txn)
manager.add_partitions_to_transaction(
'hello' => [1],
'world' => [2]
)
end
end
context 'manager is not transactional' do
let!(:manager) { described_class.new(logger: logger, cluster: cluster) }
it 'raises exception' do
expect do
manager.add_partitions_to_transaction(
'hello' => [1, 2, 3]
)
end.to raise_error(Kafka::InvalidTxnStateError, /please turn on transactional mode/i)
end
end
end
describe '#init_transactions' do
let!(:manager) do
described_class.new(
logger: logger, cluster: cluster,
transactional: true, transactional_id: 'IDID', transactional_timeout: 600
)
end
it 'init producer information' do
manager.init_transactions
expect(manager.producer_id).to eql(1234)
expect(manager.producer_epoch).to eql(1)
end
context 'current status is not initialize' do
before do
manager.init_transactions
allow(transaction_coordinator).to receive(:init_producer_id).and_return(
Kafka::Protocol::InitProducerIDResponse.new(
error_code: 0,
producer_id: 456,
producer_epoch: 2
)
)
end
it 'does nothing' do
manager.init_transactions
expect(manager.producer_id).to eql(1234)
expect(manager.producer_epoch).to eql(1)
end
end
context 'manager is not in transactional mode' do
let!(:manager) do
described_class.new(logger: logger, cluster: cluster)
end
it 'raises exception' do
expect do
manager.init_transactions
end.to raise_error(Kafka::InvalidTxnStateError, /please turn on transactional mode/i)
end
it 'changes state to error' do
begin
manager.init_transactions
rescue; end
expect(manager.error?).to eql(true)
end
end
end
describe '#begin_transaction' do
let!(:manager) do
described_class.new(
logger: logger, cluster: cluster,
transactional: true, transactional_id: 'IDID', transactional_timeout: 600
)
end
it 'changes state to init_transaction' do
manager.init_transactions
manager.begin_transaction
expect(manager.in_transaction?).to eql(true)
end
context 'transaction already started' do
before do
manager.init_transactions
manager.begin_transaction
end
it 'raises exception' do
expect do
manager.begin_transaction
end.to raise_error(Kafka::InvalidTxnStateError, /transaction has already started/i)
end
it 'changes state to error' do
begin
manager.begin_transaction
rescue; end
expect(manager.error?).to eql(true)
end
end
context 'transaction is not initialized' do
it 'raises exception' do
expect do
manager.begin_transaction
end.to raise_error(Kafka::InvalidTxnStateError, /transaction is not ready/i)
end
it 'changes state to error' do
begin
manager.begin_transaction
rescue; end
expect(manager.error?).to eql(true)
end
end
context 'manager is not in transactional mode' do
let!(:manager) do
described_class.new(logger: logger, cluster: cluster)
end
it 'raises exception' do
expect do
manager.begin_transaction
end.to raise_error(Kafka::InvalidTxnStateError, /please turn on transactional mode/i)
end
it 'changes state to error' do
begin
manager.begin_transaction
rescue; end
expect(manager.error?).to eql(true)
end
end
end
describe '#abort_transaction' do
let!(:manager) do
described_class.new(
logger: logger, cluster: cluster,
transactional: true, transactional_id: 'IDID', transactional_timeout: 600
)
end
before do
allow(transaction_coordinator).to receive(:end_txn).and_return(
Kafka::Protocol::EndTxnResposne.new(
error_code: 0
)
)
end
context 'transaction is ready to abort' do
before do
manager.init_transactions
manager.begin_transaction
end
it 'calls transaction coordinator to end TXN' do
expect(transaction_coordinator).to receive(:end_txn).with(
transactional_id: 'IDID',
producer_id: 1234,
producer_epoch: 1,
transaction_result: false
)
manager.abort_transaction
expect(manager.in_transaction?).to eql(false)
end
end
context 'coordinator returns error' do
before do
manager.init_transactions
manager.begin_transaction
allow(transaction_coordinator).to receive(:end_txn).and_return(
Kafka::Protocol::EndTxnResposne.new(
error_code: 47
)
)
end
it 'raises exception' do
expect do
manager.abort_transaction
end.to raise_error(Kafka::InvalidProducerEpochError)
end
it 'changes state to error' do
begin
manager.abort_transaction
rescue; end
expect(manager.error?).to eql(true)
end
end
context 'manager is not in a transaction' do
before do
manager.init_transactions
end
it 'does not raise an exception' do
expect { manager.abort_transaction }.not_to raise_error
end
it 'leaves transaction manager in ready state' do
begin
manager.abort_transaction
rescue; end
expect(manager.ready?).to eql(true)
end
end
context 'manager is not in transactional mode' do
let!(:manager) do
described_class.new(logger: logger, cluster: cluster)
end
it 'raises exception' do
expect do
manager.abort_transaction
end.to raise_error(Kafka::InvalidTxnStateError, /please turn on transactional mode/i)
end
it 'changes state to error' do
begin
manager.abort_transaction
rescue; end
expect(manager.error?).to eql(true)
end
end
end
describe '#commit_transaction' do
let!(:manager) do
described_class.new(
logger: logger, cluster: cluster,
transactional: true, transactional_id: 'IDID', transactional_timeout: 600
)
end
before do
allow(transaction_coordinator).to receive(:end_txn).and_return(
Kafka::Protocol::EndTxnResposne.new(
error_code: 0
)
)
end
context 'transaction is ready to commit' do
before do
manager.init_transactions
manager.begin_transaction
end
it 'calls transaction coordinator to end TXN' do
expect(transaction_coordinator).to receive(:end_txn).with(
transactional_id: 'IDID',
producer_id: 1234,
producer_epoch: 1,
transaction_result: true
)
manager.commit_transaction
expect(manager.in_transaction?).to eql(false)
end
end
context 'coordinator returns error' do
before do
manager.init_transactions
manager.begin_transaction
allow(transaction_coordinator).to receive(:end_txn).and_return(
Kafka::Protocol::EndTxnResposne.new(
error_code: 47
)
)
end
it 'raises exception' do
expect do
manager.commit_transaction
end.to raise_error(Kafka::InvalidProducerEpochError)
end
it 'changes state to error' do
begin
manager.commit_transaction
rescue; end
expect(manager.error?).to eql(true)
end
end
context 'manager is not in a transaction' do
before do
manager.init_transactions
end
it 'raises exception' do
expect do
manager.commit_transaction
end.to raise_error(Kafka::InvalidTxnStateError, /transaction is not valid to commit/i)
end
it 'changes state to error' do
begin
manager.commit_transaction
rescue; end
expect(manager.error?).to eql(true)
end
end
context 'manager is not in transactional mode' do
let!(:manager) do
described_class.new(logger: logger, cluster: cluster)
end
it 'raises exception' do
expect do
manager.commit_transaction
end.to raise_error(Kafka::InvalidTxnStateError, /please turn on transactional mode/i)
end
it 'changes state to error' do
begin
manager.commit_transaction
rescue; end
expect(manager.error?).to eql(true)
end
end
end
describe '#close' do
let!(:manager) do
described_class.new(
logger: logger, cluster: cluster,
transactional: true, transactional_id: 'IDID', transactional_timeout: 600
)
end
before do
allow(transaction_coordinator).to receive(:end_txn).and_return(
Kafka::Protocol::EndTxnResposne.new(
error_code: 0
)
)
end
context 'currently in a transaction' do
before do
manager.init_transactions
manager.begin_transaction
end
it 'aborts transaction' do
expect(transaction_coordinator).to receive(:end_txn).with(
transactional_id: 'IDID',
producer_id: 1234,
producer_epoch: 1,
transaction_result: false
)
manager.close
expect(manager.in_transaction?).to eql(false)
end
end
end
describe '#send_offsets_to_txn' do
let!(:manager) do
described_class.new(
logger: logger,
cluster: cluster,
transactional: true,
transactional_id: 'IDID',
transactional_timeout: 600
)
end
context 'currently in transaction' do
before do
manager.init_transactions
manager.begin_transaction
allow(transaction_coordinator).to receive(:add_offsets_to_txn).and_return(
Kafka::Protocol::AddOffsetsToTxnResponse.new(
error_code: 0
)
)
allow(transaction_coordinator).to receive(:txn_offset_commit).and_return(
Kafka::Protocol::TxnOffsetCommitResponse.new(
error_code: 0
)
)
end
it 'notifies transaction coordinator' do
manager.send_offsets_to_txn(offsets: [1, 2], group_id: 1)
expect(transaction_coordinator).to have_received(:add_offsets_to_txn)
expect(transaction_coordinator).to have_received(:txn_offset_commit)
end
end
end
end
def success_add_partitions_to_txn_response(topics)
Kafka::Protocol::AddPartitionsToTxnResponse.new(
errors: topics.map do |topic, partitions|
Kafka::Protocol::AddPartitionsToTxnResponse::TopicPartitionsError.new(
topic: topic,
partitions: partitions.map do |partition|
Kafka::Protocol::AddPartitionsToTxnResponse::PartitionError.new(
partition: partition,
error_code: 0
)
end
)
end
)
end