-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
test_buffer.rb
1493 lines (1197 loc) · 54.3 KB
/
test_buffer.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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require_relative '../helper'
require 'fluent/plugin/buffer'
require 'fluent/plugin/buffer/memory_chunk'
require 'fluent/plugin/compressable'
require 'fluent/plugin/buffer/chunk'
require 'fluent/event'
require 'flexmock/test_unit'
require 'fluent/log'
require 'fluent/plugin_id'
require 'time'
module FluentPluginBufferTest
class DummyOutputPlugin < Fluent::Plugin::Base
include Fluent::PluginId
include Fluent::PluginLoggerMixin
end
class DummyMemoryChunkError < StandardError; end
class DummyMemoryChunk < Fluent::Plugin::Buffer::MemoryChunk
attr_reader :append_count, :rollbacked, :closed, :purged, :chunk
attr_accessor :failing
def initialize(metadata, compress: :text)
super
@append_count = 0
@rollbacked = false
@closed = false
@purged = false
@failing = false
end
def concat(data, size)
@append_count += 1
raise DummyMemoryChunkError if @failing
super
end
def rollback
super
@rollbacked = true
end
def close
super
@closed = true
end
def purge
super
@purged = true
end
end
class DummyPlugin < Fluent::Plugin::Buffer
def create_metadata(timekey=nil, tag=nil, variables=nil)
Fluent::Plugin::Buffer::Metadata.new(timekey, tag, variables)
end
def create_chunk(metadata, data)
c = FluentPluginBufferTest::DummyMemoryChunk.new(metadata)
c.append(data)
c.commit
c
end
def create_chunk_es(metadata, es)
c = FluentPluginBufferTest::DummyMemoryChunk.new(metadata)
c.concat(es.to_msgpack_stream, es.size)
c.commit
c
end
def resume
dm0 = create_metadata(Time.parse('2016-04-11 16:00:00 +0000').to_i, nil, nil)
dm1 = create_metadata(Time.parse('2016-04-11 16:10:00 +0000').to_i, nil, nil)
dm2 = create_metadata(Time.parse('2016-04-11 16:20:00 +0000').to_i, nil, nil)
dm3 = create_metadata(Time.parse('2016-04-11 16:30:00 +0000').to_i, nil, nil)
staged = {
dm2 => create_chunk(dm2, ["b" * 100]).staged!,
dm3 => create_chunk(dm3, ["c" * 100]).staged!,
}
queued = [
create_chunk(dm0, ["0" * 100]).enqueued!,
create_chunk(dm1, ["a" * 100]).enqueued!,
create_chunk(dm1, ["a" * 3]).enqueued!,
]
return staged, queued
end
def generate_chunk(metadata)
DummyMemoryChunk.new(metadata, compress: @compress)
end
end
end
class BufferTest < Test::Unit::TestCase
def create_buffer(hash)
buffer_conf = config_element('buffer', '', hash, [])
owner = FluentPluginBufferTest::DummyOutputPlugin.new
owner.configure(config_element('ROOT', '', {}, [ buffer_conf ]))
p = FluentPluginBufferTest::DummyPlugin.new
p.owner = owner
p.configure(buffer_conf)
p
end
def create_metadata(timekey=nil, tag=nil, variables=nil)
Fluent::Plugin::Buffer::Metadata.new(timekey, tag, variables)
end
def create_chunk(metadata, data)
c = FluentPluginBufferTest::DummyMemoryChunk.new(metadata)
c.append(data)
c.commit
c
end
def create_chunk_es(metadata, es)
c = FluentPluginBufferTest::DummyMemoryChunk.new(metadata)
c.concat(es.to_msgpack_stream, es.size)
c.commit
c
end
setup do
Fluent::Test.setup
end
sub_test_case 'using base buffer class' do
setup do
buffer_conf = config_element('buffer', '', {}, [])
owner = FluentPluginBufferTest::DummyOutputPlugin.new
owner.configure(config_element('ROOT', '', {}, [ buffer_conf ]))
p = Fluent::Plugin::Buffer.new
p.owner = owner
p.configure(buffer_conf)
@p = p
end
test 'default persistency is false' do
assert !@p.persistent?
end
test 'chunk bytes limit is 8MB, and total bytes limit is 512MB' do
assert_equal 8*1024*1024, @p.chunk_limit_size
assert_equal 512*1024*1024, @p.total_limit_size
end
test 'chunk records limit is ignored in default' do
assert_nil @p.chunk_limit_records
end
test '#storable? checks total size of staged and enqueued(includes dequeued chunks) against total_limit_size' do
assert_equal 512*1024*1024, @p.total_limit_size
assert_equal 0, @p.stage_size
assert_equal 0, @p.queue_size
assert @p.storable?
@p.stage_size = 256 * 1024 * 1024
@p.queue_size = 256 * 1024 * 1024 - 1
assert @p.storable?
@p.queue_size = 256 * 1024 * 1024
assert !@p.storable?
end
test '#resume must be implemented by subclass' do
assert_raise NotImplementedError do
@p.resume
end
end
test '#generate_chunk must be implemented by subclass' do
assert_raise NotImplementedError do
@p.generate_chunk(Object.new)
end
end
end
sub_test_case 'with default configuration and dummy implementation' do
setup do
@p = create_buffer({'queued_chunks_limit_size' => 100})
@dm0 = create_metadata(Time.parse('2016-04-11 16:00:00 +0000').to_i, nil, nil)
@dm1 = create_metadata(Time.parse('2016-04-11 16:10:00 +0000').to_i, nil, nil)
@dm2 = create_metadata(Time.parse('2016-04-11 16:20:00 +0000').to_i, nil, nil)
@dm3 = create_metadata(Time.parse('2016-04-11 16:30:00 +0000').to_i, nil, nil)
@p.start
end
test '#start resumes buffer states and update queued numbers per metadata' do
plugin = create_buffer({})
assert_equal({}, plugin.stage)
assert_equal([], plugin.queue)
assert_equal({}, plugin.dequeued)
assert_equal({}, plugin.queued_num)
assert_equal 0, plugin.stage_size
assert_equal 0, plugin.queue_size
assert_equal [], plugin.timekeys
# @p is started plugin
assert_equal [@dm2,@dm3], @p.stage.keys
assert_equal "b" * 100, @p.stage[@dm2].read
assert_equal "c" * 100, @p.stage[@dm3].read
assert_equal 200, @p.stage_size
assert_equal 3, @p.queue.size
assert_equal "0" * 100, @p.queue[0].read
assert_equal "a" * 100, @p.queue[1].read
assert_equal "a" * 3, @p.queue[2].read
assert_equal 203, @p.queue_size
# staged, queued
assert_equal 1, @p.queued_num[@dm0]
assert_equal 2, @p.queued_num[@dm1]
end
test '#close closes all chunks in dequeued, enqueued and staged' do
dmx = create_metadata(Time.parse('2016-04-11 15:50:00 +0000').to_i, nil, nil)
cx = create_chunk(dmx, ["x" * 1024])
@p.dequeued[cx.unique_id] = cx
staged_chunks = @p.stage.values.dup
queued_chunks = @p.queue.dup
@p.close
assert cx.closed
assert{ staged_chunks.all?{|c| c.closed } }
assert{ queued_chunks.all?{|c| c.closed } }
end
test '#terminate initializes all internal states' do
dmx = create_metadata(Time.parse('2016-04-11 15:50:00 +0000').to_i, nil, nil)
cx = create_chunk(dmx, ["x" * 1024])
@p.dequeued[cx.unique_id] = cx
@p.close
@p.terminate
assert_nil @p.stage
assert_nil @p.queue
assert_nil @p.dequeued
assert_nil @p.queued_num
assert_nil @p.stage_length_metrics
assert_nil @p.stage_size_metrics
assert_nil @p.queue_length_metrics
assert_nil @p.queue_size_metrics
assert_nil @p.available_buffer_space_ratios_metrics
assert_nil @p.total_queued_size_metrics
assert_nil @p.newest_timekey_metrics
assert_nil @p.oldest_timekey_metrics
assert_equal [], @p.timekeys
end
test '#queued_records returns total number of size in all chunks in queue' do
assert_equal 3, @p.queue.size
r0 = @p.queue[0].size
assert_equal 1, r0
r1 = @p.queue[1].size
assert_equal 1, r1
r2 = @p.queue[2].size
assert_equal 1, r2
assert_equal (r0+r1+r2), @p.queued_records
end
test '#queued? returns queue has any chunks or not without arguments' do
assert @p.queued?
@p.queue.reject!{|_c| true }
assert !@p.queued?
end
test '#queued? returns queue has chunks for specified metadata with an argument' do
assert @p.queued?(@dm0)
assert @p.queued?(@dm1)
assert !@p.queued?(@dm2)
end
test '#enqueue_chunk enqueues a chunk on stage with specified metadata' do
assert_equal 2, @p.stage.size
assert_equal [@dm2,@dm3], @p.stage.keys
assert_equal 3, @p.queue.size
assert_nil @p.queued_num[@dm2]
assert_equal 200, @p.stage_size
assert_equal 203, @p.queue_size
@p.enqueue_chunk(@dm2)
assert_equal [@dm3], @p.stage.keys
assert_equal @dm2, @p.queue.last.metadata
assert_equal 1, @p.queued_num[@dm2]
assert_equal 100, @p.stage_size
assert_equal 303, @p.queue_size
end
test '#enqueue_chunk ignores empty chunks' do
assert_equal 3, @p.queue.size
m = @p.metadata(timekey: Time.parse('2016-04-11 16:40:00 +0000').to_i)
c = create_chunk(m, [''])
@p.stage[m] = c
assert @p.stage[m].empty?
assert !c.closed
@p.enqueue_chunk(m)
assert_nil @p.stage[m]
assert_equal 3, @p.queue.size
assert_nil @p.queued_num[m]
assert c.closed
end
test '#enqueue_chunk calls #enqueued! if chunk responds to it' do
assert_equal 3, @p.queue.size
m = @p.metadata(timekey: Time.parse('2016-04-11 16:40:00 +0000').to_i)
c = create_chunk(m, ['c' * 256])
callback_called = false
(class << c; self; end).module_eval do
define_method(:enqueued!){ callback_called = true }
end
@p.stage[m] = c
@p.enqueue_chunk(m)
assert_equal c, @p.queue.last
assert callback_called
end
test '#enqueue_all enqueues chunks on stage which given block returns true with' do
m1 = @p.metadata(timekey: Time.parse('2016-04-11 16:40:00 +0000').to_i)
c1 = create_chunk(m1, ['c' * 256])
@p.stage[m1] = c1
m2 = @p.metadata(timekey: Time.parse('2016-04-11 16:50:00 +0000').to_i)
c2 = create_chunk(m2, ['c' * 256])
@p.stage[m2] = c2
assert_equal [@dm2,@dm3,m1,m2], @p.stage.keys
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
@p.enqueue_all{ |m, c| m.timekey < Time.parse('2016-04-11 16:41:00 +0000').to_i }
assert_equal [m2], @p.stage.keys
assert_equal [@dm0,@dm1,@dm1,@dm2,@dm3,m1], @p.queue.map(&:metadata)
end
test '#enqueue_all enqueues all chunks on stage without block' do
m1 = @p.metadata(timekey: Time.parse('2016-04-11 16:40:00 +0000').to_i)
c1 = create_chunk(m1, ['c' * 256])
@p.stage[m1] = c1
m2 = @p.metadata(timekey: Time.parse('2016-04-11 16:50:00 +0000').to_i)
c2 = create_chunk(m2, ['c' * 256])
@p.stage[m2] = c2
assert_equal [@dm2,@dm3,m1,m2], @p.stage.keys
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
@p.enqueue_all
assert_equal [], @p.stage.keys
assert_equal [@dm0,@dm1,@dm1,@dm2,@dm3,m1,m2], @p.queue.map(&:metadata)
end
test '#dequeue_chunk dequeues a chunk from queue if a chunk exists' do
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal({}, @p.dequeued)
m1 = @p.dequeue_chunk
assert_equal @dm0, m1.metadata
assert_equal @dm0, @p.dequeued[m1.unique_id].metadata
m2 = @p.dequeue_chunk
assert_equal @dm1, m2.metadata
assert_equal @dm1, @p.dequeued[m2.unique_id].metadata
m3 = @p.dequeue_chunk
assert_equal @dm1, m3.metadata
assert_equal @dm1, @p.dequeued[m3.unique_id].metadata
m4 = @p.dequeue_chunk
assert_nil m4
end
test '#takeback_chunk resumes a chunk from dequeued to queued at the head of queue, and returns true' do
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal({}, @p.dequeued)
m1 = @p.dequeue_chunk
assert_equal @dm0, m1.metadata
assert_equal @dm0, @p.dequeued[m1.unique_id].metadata
assert_equal [@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal({m1.unique_id => m1}, @p.dequeued)
assert @p.takeback_chunk(m1.unique_id)
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal({}, @p.dequeued)
end
test '#purge_chunk removes a chunk specified by argument id from dequeued chunks' do
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal({}, @p.dequeued)
m0 = @p.dequeue_chunk
m1 = @p.dequeue_chunk
assert @p.takeback_chunk(m0.unique_id)
assert_equal [@dm0,@dm1], @p.queue.map(&:metadata)
assert_equal({m1.unique_id => m1}, @p.dequeued)
assert !m1.purged
@p.purge_chunk(m1.unique_id)
assert m1.purged
assert_equal [@dm0,@dm1], @p.queue.map(&:metadata)
assert_equal({}, @p.dequeued)
end
test '#purge_chunk removes an argument metadata if no chunks exist on stage or in queue' do
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal({}, @p.dequeued)
m0 = @p.dequeue_chunk
assert_equal [@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal({m0.unique_id => m0}, @p.dequeued)
assert !m0.purged
@p.purge_chunk(m0.unique_id)
assert m0.purged
assert_equal [@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal({}, @p.dequeued)
end
test '#takeback_chunk returns false if specified chunk_id is already purged' do
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal({}, @p.dequeued)
m0 = @p.dequeue_chunk
assert_equal [@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal({m0.unique_id => m0}, @p.dequeued)
assert !m0.purged
@p.purge_chunk(m0.unique_id)
assert m0.purged
assert_equal [@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal({}, @p.dequeued)
assert !@p.takeback_chunk(m0.unique_id)
assert_equal [@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal({}, @p.dequeued)
end
test '#clear_queue! removes all chunks in queue, but leaves staged chunks' do
qchunks = @p.queue.dup
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal 2, @p.stage.size
assert_equal({}, @p.dequeued)
@p.clear_queue!
assert_equal [], @p.queue
assert_equal 0, @p.queue_size
assert_equal 2, @p.stage.size
assert_equal({}, @p.dequeued)
assert{ qchunks.all?{ |c| c.purged } }
end
test '#write returns immediately if argument data is empty array' do
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
m = @p.metadata(timekey: Time.parse('2016-04-11 16:40:00 +0000').to_i)
@p.write({m => []})
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
end
test '#write returns immediately if argument data is empty event stream' do
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
m = @p.metadata(timekey: Time.parse('2016-04-11 16:40:00 +0000').to_i)
@p.write({m => Fluent::ArrayEventStream.new([])})
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
end
test '#write raises BufferOverflowError if buffer is not storable' do
@p.stage_size = 256 * 1024 * 1024
@p.queue_size = 256 * 1024 * 1024
m = @p.metadata(timekey: Time.parse('2016-04-11 16:40:00 +0000').to_i)
assert_raise Fluent::Plugin::Buffer::BufferOverflowError do
@p.write({m => ["x" * 256]})
end
end
test '#write stores data into an existing chunk with metadata specified' do
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
dm3data = @p.stage[@dm3].read.dup
prev_stage_size = @p.stage_size
assert_equal 1, @p.stage[@dm3].append_count
@p.write({@dm3 => ["x" * 256, "y" * 256, "z" * 256]})
assert_equal 2, @p.stage[@dm3].append_count
assert_equal (dm3data + ("x" * 256) + ("y" * 256) + ("z" * 256)), @p.stage[@dm3].read
assert_equal (prev_stage_size + 768), @p.stage_size
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
end
test '#write creates new chunk and store data into it if there are no chunks for specified metadata' do
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
timekey = Time.parse('2016-04-11 16:40:00 +0000').to_i
assert !@p.timekeys.include?(timekey)
prev_stage_size = @p.stage_size
m = @p.metadata(timekey: timekey)
@p.write({m => ["x" * 256, "y" * 256, "z" * 256]})
assert_equal 1, @p.stage[m].append_count
assert_equal ("x" * 256 + "y" * 256 + "z" * 256), @p.stage[m].read
assert_equal (prev_stage_size + 768), @p.stage_size
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3,m], @p.stage.keys
@p.update_timekeys
assert @p.timekeys.include?(timekey)
end
test '#write tries to enqueue and store data into a new chunk if existing chunk is full' do
assert_equal 8 * 1024 * 1024, @p.chunk_limit_size
assert_equal 0.95, @p.chunk_full_threshold
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
m = @p.metadata(timekey: Time.parse('2016-04-11 16:40:00 +0000').to_i)
row = "x" * 1024 * 1024
small_row = "x" * 1024 * 512
@p.write({m => [row] * 7 + [small_row]})
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3,m], @p.stage.keys
assert_equal 1, @p.stage[m].append_count
@p.write({m => [row]})
assert_equal [@dm0,@dm1,@dm1,m], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3,m], @p.stage.keys
assert_equal 1, @p.stage[m].append_count
assert_equal 1024*1024, @p.stage[m].bytesize
assert_equal 3, @p.queue.last.append_count # 1 -> write (2) -> write_step_by_step (3)
assert @p.queue.last.rollbacked
end
test '#write rollbacks if commit raises errors' do
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
m = @p.metadata(timekey: Time.parse('2016-04-11 16:40:00 +0000').to_i)
row = "x" * 1024
@p.write({m => [row] * 8})
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3,m], @p.stage.keys
target_chunk = @p.stage[m]
assert_equal 1, target_chunk.append_count
assert !target_chunk.rollbacked
(class << target_chunk; self; end).module_eval do
define_method(:commit){ raise "yay" }
end
assert_raise RuntimeError.new("yay") do
@p.write({m => [row]})
end
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3,m], @p.stage.keys
assert_equal 2, target_chunk.append_count
assert target_chunk.rollbacked
assert_equal row * 8, target_chunk.read
end
test '#write w/ format raises BufferOverflowError if buffer is not storable' do
@p.stage_size = 256 * 1024 * 1024
@p.queue_size = 256 * 1024 * 1024
m = @p.metadata(timekey: Time.parse('2016-04-11 16:40:00 +0000').to_i)
es = Fluent::ArrayEventStream.new([ [event_time('2016-04-11 16:40:01 +0000'), {"message" => "xxxxxxxxxxxxxx"} ] ])
assert_raise Fluent::Plugin::Buffer::BufferOverflowError do
@p.write({m => es}, format: ->(e){e.to_msgpack_stream})
end
end
test '#write w/ format stores data into an existing chunk with metadata specified' do
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
dm3data = @p.stage[@dm3].read.dup
prev_stage_size = @p.stage_size
assert_equal 1, @p.stage[@dm3].append_count
es = Fluent::ArrayEventStream.new(
[
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 128}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "y" * 128}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "z" * 128}],
]
)
@p.write({@dm3 => es}, format: ->(e){e.to_msgpack_stream})
assert_equal 2, @p.stage[@dm3].append_count
assert_equal (dm3data + es.to_msgpack_stream), @p.stage[@dm3].read
assert_equal (prev_stage_size + es.to_msgpack_stream.bytesize), @p.stage_size
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
end
test '#write w/ format creates new chunk and store data into it if there are not chunks for specified metadata' do
assert_equal 8 * 1024 * 1024, @p.chunk_limit_size
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
timekey = Time.parse('2016-04-11 16:40:00 +0000').to_i
assert !@p.timekeys.include?(timekey)
m = @p.metadata(timekey: timekey)
es = Fluent::ArrayEventStream.new(
[
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:03 +0000'), {"message" => "z" * 1024 * 512}],
]
)
@p.write({m => es}, format: ->(e){e.to_msgpack_stream})
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3,m], @p.stage.keys
assert_equal 1, @p.stage[m].append_count
@p.update_timekeys
assert @p.timekeys.include?(timekey)
end
test '#write w/ format tries to enqueue and store data into a new chunk if existing chunk does not have enough space' do
assert_equal 8 * 1024 * 1024, @p.chunk_limit_size
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
m = @p.metadata(timekey: Time.parse('2016-04-11 16:40:00 +0000').to_i)
es = Fluent::ArrayEventStream.new(
[
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:03 +0000'), {"message" => "z" * 1024 * 512}],
]
)
@p.write({m => es}, format: ->(e){e.to_msgpack_stream})
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3,m], @p.stage.keys
assert_equal 1, @p.stage[m].append_count
es2 = Fluent::OneEventStream.new(event_time('2016-04-11 16:40:03 +0000'), {"message" => "z" * 1024 * 1024})
@p.write({m => es2}, format: ->(e){e.to_msgpack_stream})
assert_equal [@dm0,@dm1,@dm1,m], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3,m], @p.stage.keys
assert_equal 1, @p.stage[m].append_count
assert_equal es2.to_msgpack_stream.bytesize, @p.stage[m].bytesize
assert_equal 2, @p.queue.last.append_count # 1 -> write (2) -> rollback&enqueue
assert @p.queue.last.rollbacked
end
test '#write w/ format enqueues chunk if it is already full after adding data' do
assert_equal 8 * 1024 * 1024, @p.chunk_limit_size
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
m = @p.metadata(timekey: Time.parse('2016-04-11 16:40:00 +0000').to_i)
es = Fluent::ArrayEventStream.new(
[
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * (1024 * 1024 - 25)}], # 1024 * 1024 bytes as msgpack stream
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * (1024 * 1024 - 25)}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * (1024 * 1024 - 25)}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * (1024 * 1024 - 25)}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * (1024 * 1024 - 25)}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * (1024 * 1024 - 25)}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * (1024 * 1024 - 25)}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * (1024 * 1024 - 25)}],
]
)
@p.write({m => es}, format: ->(e){e.to_msgpack_stream})
assert_equal [@dm0,@dm1,@dm1,m], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
assert_equal 1, @p.queue.last.append_count
end
test '#write w/ format rollbacks if commit raises errors' do
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
m = @p.metadata(timekey: Time.parse('2016-04-11 16:40:00 +0000').to_i)
es = Fluent::ArrayEventStream.new(
[
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:01 +0000'), {"message" => "x" * 1024 * 1024}],
[event_time('2016-04-11 16:40:03 +0000'), {"message" => "z" * 1024 * 512}],
]
)
@p.write({m => es}, format: ->(e){e.to_msgpack_stream})
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3,m], @p.stage.keys
target_chunk = @p.stage[m]
assert_equal 1, target_chunk.append_count
assert !target_chunk.rollbacked
(class << target_chunk; self; end).module_eval do
define_method(:commit){ raise "yay" }
end
es2 = Fluent::ArrayEventStream.new(
[
[event_time('2016-04-11 16:40:04 +0000'), {"message" => "z" * 1024 * 128}],
]
)
assert_raise RuntimeError.new("yay") do
@p.write({m => es2}, format: ->(e){e.to_msgpack_stream})
end
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3,m], @p.stage.keys
assert_equal 2, target_chunk.append_count
assert target_chunk.rollbacked
assert_equal es.to_msgpack_stream, target_chunk.read
end
test '#write writes many metadata and data pairs at once' do
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
row = "x" * 1024
@p.write({ @dm0 => [row, row, row], @dm1 => [row, row] })
assert_equal [@dm2,@dm3,@dm0,@dm1], @p.stage.keys
end
test '#write does not commit on any chunks if any append operation on chunk fails' do
assert_equal [@dm0,@dm1,@dm1], @p.queue.map(&:metadata)
assert_equal [@dm2,@dm3], @p.stage.keys
row = "x" * 1024
@p.write({ @dm0 => [row, row, row], @dm1 => [row, row] })
assert_equal [@dm2,@dm3,@dm0,@dm1], @p.stage.keys
dm2_size = @p.stage[@dm2].size
assert !@p.stage[@dm2].rollbacked
dm3_size = @p.stage[@dm3].size
assert !@p.stage[@dm3].rollbacked
assert{ @p.stage[@dm0].size == 3 }
assert !@p.stage[@dm0].rollbacked
assert{ @p.stage[@dm1].size == 2 }
assert !@p.stage[@dm1].rollbacked
meta_list = [@dm0, @dm1, @dm2, @dm3].sort
@p.stage[meta_list.last].failing = true
assert_raise(FluentPluginBufferTest::DummyMemoryChunkError) do
@p.write({ @dm2 => [row], @dm3 => [row], @dm0 => [row, row, row], @dm1 => [row, row] })
end
assert{ @p.stage[@dm2].size == dm2_size }
assert @p.stage[@dm2].rollbacked
assert{ @p.stage[@dm3].size == dm3_size }
assert @p.stage[@dm3].rollbacked
assert{ @p.stage[@dm0].size == 3 }
assert @p.stage[@dm0].rollbacked
assert{ @p.stage[@dm1].size == 2 }
assert @p.stage[@dm1].rollbacked
end
test '#compress returns :text' do
assert_equal :text, @p.compress
end
# https://github.com/fluent/fluentd/issues/3089
test "closed chunk should not be committed" do
assert_equal 8 * 1024 * 1024, @p.chunk_limit_size
assert_equal 0.95, @p.chunk_full_threshold
purge_count = 0
stub.proxy(@p).generate_chunk(anything) do |chunk|
stub.proxy(chunk).purge do |result|
purge_count += 1
result
end
stub.proxy(chunk).commit do |result|
assert_false(chunk.closed?)
result
end
stub.proxy(chunk).rollback do |result|
assert_false(chunk.closed?)
result
end
chunk
end
m = @p.metadata(timekey: Time.parse('2016-04-11 16:40:00 +0000').to_i)
small_row = "x" * 1024 * 400
big_row = "x" * 1024 * 1024 * 8 # just `chunk_size_limit`, it does't cause BufferOverFlowError.
# Write 42 events in 1 event stream, last one is for triggering `ShouldRetry`
@p.write({m => [small_row] * 40 + [big_row] + ["x"]})
# Above event strem will be splitted twice by `Buffer#write_step_by_step`
#
# 1. `write_once`: 42 [events] * 1 [stream]
# 2. `write_step_by_step`: 4 [events]* 10 [streams] + 2 [events] * 1 [stream]
# 3. `write_step_by_step` (by `ShouldRetry`): 1 [event] * 42 [streams]
#
# The problematic data is built in the 2nd stage.
# In the 2nd stage, 5 streams are packed in a chunk.
# ((1024 * 400) [bytes] * 4 [events] * 5 [streams] = 8192000 [bytes] < `chunk_limit_size` (8MB)).
# So 3 chunks are used to store all data.
# The 1st chunk is already staged by `write_once`.
# The 2nd & 3rd chunks are newly created as unstaged.
# The 3rd chunk is purged before `ShouldRetry`, it's no problem:
# https://github.com/fluent/fluentd/blob/7e9eba736ff40ad985341be800ddc46558be75f2/lib/fluent/plugin/buffer.rb#L850
# The 2nd chunk is purged in `rescue ShouldRetry`:
# https://github.com/fluent/fluentd/blob/7e9eba736ff40ad985341be800ddc46558be75f2/lib/fluent/plugin/buffer.rb#L862
# It causes the issue described in https://github.com/fluent/fluentd/issues/3089#issuecomment-1811839198
assert_equal 2, purge_count
end
# https://github.com/fluent/fluentd/issues/4446
test "#write_step_by_step keeps chunks kept in locked in entire #write process" do
assert_equal 8 * 1024 * 1024, @p.chunk_limit_size
assert_equal 0.95, @p.chunk_full_threshold
mon_enter_counts_by_chunk = {}
mon_exit_counts_by_chunk = {}
stub.proxy(@p).generate_chunk(anything) do |chunk|
stub(chunk).mon_enter do
enter_count = 1 + mon_enter_counts_by_chunk.fetch(chunk, 0)
exit_count = mon_exit_counts_by_chunk.fetch(chunk, 0)
mon_enter_counts_by_chunk[chunk] = enter_count
# Assert that chunk is passed to &block of write_step_by_step before exiting the lock.
# (i.e. The lock count must be 2 greater than the exit count).
# Since ShouldRetry occurs once, the staged chunk takes the lock 3 times when calling the block.
if chunk.staged?
lock_in_block = enter_count == 3
assert_equal(enter_count - 2, exit_count) if lock_in_block
else
lock_in_block = enter_count == 2
assert_equal(enter_count - 2, exit_count) if lock_in_block
end
end
stub(chunk).mon_exit do
exit_count = 1 + mon_exit_counts_by_chunk.fetch(chunk, 0)
mon_exit_counts_by_chunk[chunk] = exit_count
end
chunk
end
m = @p.metadata(timekey: Time.parse('2016-04-11 16:40:00 +0000').to_i)
small_row = "x" * 1024 * 400
big_row = "x" * 1024 * 1024 * 8 # just `chunk_size_limit`, it does't cause BufferOverFlowError.
# Write 42 events in 1 event stream, last one is for triggering `ShouldRetry`
@p.write({m => [small_row] * 40 + [big_row] + ["x"]})
# Above event strem will be splitted twice by `Buffer#write_step_by_step`
#
# 1. `write_once`: 42 [events] * 1 [stream]
# 2. `write_step_by_step`: 4 [events]* 10 [streams] + 2 [events] * 1 [stream]
# 3. `write_step_by_step` (by `ShouldRetry`): 1 [event] * 42 [streams]
#
# Example of staged chunk lock behavior:
#
# 1. mon_enter in write_step_by_step
# 2. ShouldRetry occurs
# 3. mon_exit in write_step_by_step
# 4. mon_enter again in write_step_by_step (retry)
# 5. passed to &block of write_step_by_step
# 6. mon_enter in the block (write)
# 7. mon_exit in write_step_by_step
# 8. mon_exit in write
assert_equal(mon_enter_counts_by_chunk.values, mon_exit_counts_by_chunk.values)
end
end
sub_test_case 'standard format with configuration for test with lower chunk limit size' do
setup do
@p = create_buffer({"chunk_limit_size" => 1_280_000})
@format = ->(e){e.to_msgpack_stream}
@dm0 = dm0 = create_metadata(Time.parse('2016-04-11 16:00:00 +0000').to_i, nil, nil)
# 1 record is 128bytes in msgpack stream
@es0 = es0 = Fluent::ArrayEventStream.new([ [event_time('2016-04-11 16:00:01 +0000'), {"message" => "x" * (128 - 22)}] ] * 5000)
(class << @p; self; end).module_eval do
define_method(:resume) {
staged = {
dm0 => create_chunk_es(dm0, es0).staged!,
}
queued = []
return staged, queued
}
end
@p.start
end
test '#write appends event stream into staged chunk' do
assert_equal [@dm0], @p.stage.keys
assert_equal [], @p.queue.map(&:metadata)
assert_equal 1_280_000, @p.chunk_limit_size
es = Fluent::ArrayEventStream.new([ [event_time('2016-04-11 16:00:02 +0000'), {"message" => "x" * (128 - 22)}] ] * 1000)
@p.write({@dm0 => es}, format: @format)
assert_equal [@dm0], @p.stage.keys
assert_equal [], @p.queue.map(&:metadata)
assert_equal (@es0.to_msgpack_stream + es.to_msgpack_stream), @p.stage[@dm0].read
end
test '#write writes event stream into a new chunk with enqueueing existing chunk if event stream is larger than available space of existing chunk' do
assert_equal [@dm0], @p.stage.keys