-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
flb_input_chunk.c
1775 lines (1494 loc) · 56.7 KB
/
flb_input_chunk.c
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2015-2022 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define FS_CHUNK_SIZE_DEBUG(op) {flb_trace("[%d] %s -> fs_chunks_size = %zu", \
__LINE__, op->name, op->fs_chunks_size);}
#define FS_CHUNK_SIZE_DEBUG_MOD(op, chunk, mod) {flb_trace( \
"[%d] %s -> fs_chunks_size = %zu mod=%zd chunk=%s", __LINE__, \
op->name, op->fs_chunks_size, mod, flb_input_chunk_get_name(chunk));}
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_input_chunk.h>
#include <fluent-bit/flb_input_plugin.h>
#include <fluent-bit/flb_storage.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_router.h>
#include <fluent-bit/flb_task.h>
#include <fluent-bit/flb_routes_mask.h>
#include <fluent-bit/flb_metrics.h>
#include <fluent-bit/stream_processor/flb_sp.h>
#include <chunkio/chunkio.h>
#define BLOCK_UNTIL_KEYPRESS() {char temp_keypress_buffer; read(0, &temp_keypress_buffer, 1);}
#define FLB_INPUT_CHUNK_RELEASE_SCOPE_LOCAL 0
#define FLB_INPUT_CHUNK_RELEASE_SCOPE_GLOBAL 1
#ifdef FLB_HAVE_IN_STORAGE_BACKLOG
extern ssize_t sb_get_releasable_output_queue_space(struct flb_output_instance *output_plugin,
size_t required_space);
extern int sb_release_output_queue_space(struct flb_output_instance *output_plugin,
size_t required_space);
#else
ssize_t sb_get_releasable_output_queue_space(struct flb_output_instance *output_plugin,
size_t required_space)
{
return 0;
}
int sb_release_output_queue_space(struct flb_output_instance *output_plugin,
size_t required_space)
{
return 0;
}
#endif
static int flb_input_chunk_safe_delete(struct flb_input_chunk *ic,
struct flb_input_chunk *old_ic,
uint64_t o_id);
static int flb_input_chunk_is_task_safe_delete(struct flb_task *task);
static ssize_t flb_input_chunk_get_real_size(struct flb_input_chunk *ic);
static ssize_t flb_input_chunk_get_releasable_space(
struct flb_input_chunk *new_input_chunk,
struct flb_input_instance *input_plugin,
struct flb_output_instance *output_plugin,
size_t required_space)
{
struct mk_list *input_chunk_iterator;
ssize_t releasable_space;
struct flb_input_chunk *old_input_chunk;
releasable_space = 0;
mk_list_foreach(input_chunk_iterator, &input_plugin->chunks) {
old_input_chunk = mk_list_entry(input_chunk_iterator, struct flb_input_chunk, _head);
if (!flb_routes_mask_get_bit(old_input_chunk->routes_mask, output_plugin->id)) {
continue;
}
if (flb_input_chunk_safe_delete(new_input_chunk, old_input_chunk,
output_plugin->id) == FLB_FALSE ||
flb_input_chunk_is_task_safe_delete(old_input_chunk->task) == FLB_FALSE) {
continue;
}
releasable_space += flb_input_chunk_get_real_size(old_input_chunk);
if (releasable_space >= required_space) {
break;
}
}
return releasable_space;
}
static int flb_input_chunk_release_space(
struct flb_input_chunk *new_input_chunk,
struct flb_input_instance *input_plugin,
struct flb_output_instance *output_plugin,
ssize_t required_space,
int release_scope)
{
struct mk_list *input_chunk_iterator_tmp;
struct mk_list *input_chunk_iterator;
int chunk_destroy_flag;
struct flb_input_chunk *old_input_chunk;
ssize_t released_space;
int chunk_released;
ssize_t chunk_size;
released_space = 0;
mk_list_foreach_safe(input_chunk_iterator, input_chunk_iterator_tmp,
&input_plugin->chunks) {
old_input_chunk = mk_list_entry(input_chunk_iterator,
struct flb_input_chunk, _head);
if (!flb_routes_mask_get_bit(old_input_chunk->routes_mask,
output_plugin->id)) {
continue;
}
if (flb_input_chunk_safe_delete(new_input_chunk,
old_input_chunk,
output_plugin->id) == FLB_FALSE ||
flb_input_chunk_is_task_safe_delete(old_input_chunk->task) == FLB_FALSE) {
continue;
}
chunk_size = flb_input_chunk_get_real_size(old_input_chunk);
chunk_released = FLB_FALSE;
chunk_destroy_flag = FLB_FALSE;
if (release_scope == FLB_INPUT_CHUNK_RELEASE_SCOPE_LOCAL) {
flb_routes_mask_clear_bit(old_input_chunk->routes_mask,
output_plugin->id);
FS_CHUNK_SIZE_DEBUG_MOD(output_plugin, old_input_chunk, chunk_size);
output_plugin->fs_chunks_size -= chunk_size;
chunk_destroy_flag = flb_routes_mask_is_empty(
old_input_chunk->routes_mask);
chunk_released = FLB_TRUE;
}
else if (release_scope == FLB_INPUT_CHUNK_RELEASE_SCOPE_GLOBAL) {
chunk_destroy_flag = FLB_TRUE;
}
if (chunk_destroy_flag) {
if (old_input_chunk->task != NULL) {
/*
* If the chunk is referenced by a task and task has no active route,
* we need to destroy the task as well.
*/
if (old_input_chunk->task->users == 0) {
flb_debug("[task] drop task_id %d with no active route from input plugin %s",
old_input_chunk->task->id, new_input_chunk->in->name);
flb_task_destroy(old_input_chunk->task, FLB_TRUE);
chunk_released = FLB_TRUE;
}
}
else {
flb_debug("[input chunk] drop chunk %s with no output route from input plugin %s",
flb_input_chunk_get_name(old_input_chunk), new_input_chunk->in->name);
flb_input_chunk_destroy(old_input_chunk, FLB_TRUE);
chunk_released = FLB_TRUE;
}
}
if (chunk_released) {
released_space += chunk_size;
}
if (released_space >= required_space) {
break;
}
}
if (released_space < required_space) {
return -2;
}
return 0;
}
static void generate_chunk_name(struct flb_input_instance *in,
char *out_buf, int buf_size)
{
struct flb_time tm;
(void) in;
flb_time_get(&tm);
snprintf(out_buf, buf_size - 1,
"%i-%lu.%4lu.flb",
getpid(),
tm.tm.tv_sec, tm.tm.tv_nsec);
}
ssize_t flb_input_chunk_get_size(struct flb_input_chunk *ic)
{
return cio_chunk_get_content_size(ic->chunk);
}
/*
* When chunk is set to DOWN from memory, data_size is set to 0 and
* cio_chunk_get_content_size(1) returns the data_size. fs_chunks_size
* is used to track the size of chunks in filesystem so we need to call
* cio_chunk_get_real_size to return the original size in the file system
*/
static ssize_t flb_input_chunk_get_real_size(struct flb_input_chunk *ic)
{
ssize_t meta_size;
ssize_t size;
size = cio_chunk_get_real_size(ic->chunk);
if (size != 0) {
return size;
}
// Real size is not synced to chunk yet
size = flb_input_chunk_get_size(ic);
if (size == 0) {
flb_debug("[input chunk] no data in the chunk %s",
flb_input_chunk_get_name(ic));
return -1;
}
meta_size = cio_meta_size(ic->chunk);
size += meta_size
/* See https://github.com/edsiper/chunkio#file-layout for more details */
+ 2 /* HEADER BYTES */
+ 4 /* CRC32 */
+ 16 /* PADDING */
+ 2; /* METADATA LENGTH BYTES */
return size;
}
int flb_input_chunk_write(void *data, const char *buf, size_t len)
{
int ret;
struct flb_input_chunk *ic;
ic = (struct flb_input_chunk *) data;
ret = cio_chunk_write(ic->chunk, buf, len);
return ret;
}
int flb_input_chunk_write_at(void *data, off_t offset,
const char *buf, size_t len)
{
int ret;
struct flb_input_chunk *ic;
ic = (struct flb_input_chunk *) data;
ret = cio_chunk_write_at(ic->chunk, offset, buf, len);
return ret;
}
/*
* For input_chunk referenced by an outgoing task, we need to check
* whether the chunk is in the middle of output flush callback
*/
static int flb_input_chunk_is_task_safe_delete(struct flb_task *task)
{
if (!task) {
return FLB_TRUE;
}
if (task->users != 0) {
return FLB_FALSE;
}
return FLB_TRUE;
}
static int flb_input_chunk_safe_delete(struct flb_input_chunk *ic,
struct flb_input_chunk *old_ic,
uint64_t o_id)
{
/* The chunk we want to drop should not be the incoming chunk */
if (ic == old_ic) {
return FLB_FALSE;
}
/*
* Even if chunks from same input plugin have same routes_mask when created,
* the routes_mask could be modified when new chunks is ingested. Therefore,
* we still need to do the validation on the routes_mask with o_id.
*/
if (flb_routes_mask_get_bit(old_ic->routes_mask, o_id) == 0) {
return FLB_FALSE;
}
return FLB_TRUE;
}
int flb_input_chunk_release_space_compound(
struct flb_input_chunk *new_input_chunk,
struct flb_output_instance *output_plugin,
size_t *local_release_requirement,
int release_local_space)
{
ssize_t segregated_backlog_releasable_space;
ssize_t active_backlog_releasable_space;
ssize_t active_plugin_releasable_space;
ssize_t required_space_remainder;
struct flb_input_instance *storage_backlog_instance;
int result;
storage_backlog_instance = output_plugin->config->storage_input_plugin;
*local_release_requirement = flb_input_chunk_get_real_size(new_input_chunk);
required_space_remainder = (ssize_t) *local_release_requirement;
segregated_backlog_releasable_space = 0;
active_backlog_releasable_space = 0;
active_plugin_releasable_space = 0;
active_backlog_releasable_space = flb_input_chunk_get_releasable_space(
new_input_chunk,
storage_backlog_instance,
output_plugin,
required_space_remainder);
required_space_remainder -= active_backlog_releasable_space;
if (required_space_remainder > 0) {
segregated_backlog_releasable_space = sb_get_releasable_output_queue_space(
output_plugin,
required_space_remainder);
required_space_remainder -= segregated_backlog_releasable_space;
}
if (required_space_remainder > 0) {
active_plugin_releasable_space = flb_input_chunk_get_releasable_space(
new_input_chunk,
new_input_chunk->in,
output_plugin,
required_space_remainder);
required_space_remainder -= active_plugin_releasable_space;
}
/* When we get here required_space_remainder could be negative but it's not a problem
* this happens when the weight of the removed chunk is higher than the remainder of
* the required space and it's not something that can nor should be prevented.
*/
if (required_space_remainder > 0) {
return -2;
}
required_space_remainder = (ssize_t) *local_release_requirement;
if (required_space_remainder > 0 && active_backlog_releasable_space > 0) {
result = flb_input_chunk_release_space(new_input_chunk,
storage_backlog_instance,
output_plugin,
active_backlog_releasable_space,
FLB_INPUT_CHUNK_RELEASE_SCOPE_GLOBAL);
if (result) {
return -3;
}
required_space_remainder -= active_backlog_releasable_space;
}
if (required_space_remainder > 0 && segregated_backlog_releasable_space > 0) {
result = sb_release_output_queue_space(
output_plugin,
segregated_backlog_releasable_space);
if (result) {
*local_release_requirement = (size_t) required_space_remainder;
return -4;
}
required_space_remainder -= segregated_backlog_releasable_space;
}
if (release_local_space) {
if (required_space_remainder > 0 && active_plugin_releasable_space > 0) {
result = flb_input_chunk_release_space(new_input_chunk,
new_input_chunk->in,
output_plugin,
active_plugin_releasable_space,
FLB_INPUT_CHUNK_RELEASE_SCOPE_LOCAL);
if (result) {
printf("FAILED\n");
return -5;
}
required_space_remainder -= active_plugin_releasable_space;
}
}
if (required_space_remainder < 0) {
required_space_remainder = 0;
}
*local_release_requirement = (size_t) required_space_remainder;
return 0;
}
/*
* Returns how many chunks needs to be dropped in order to get enough space to
* buffer the incoming data (with size chunk_size)
*/
int flb_intput_chunk_count_dropped_chunks(struct flb_input_chunk *ic,
struct flb_output_instance *o_ins,
size_t chunk_size)
{
int count = 0;
int enough_space = FLB_FALSE;
ssize_t bytes_remained;
struct mk_list *head;
struct flb_input_chunk *old_ic;
FS_CHUNK_SIZE_DEBUG(o_ins);
bytes_remained = o_ins->total_limit_size -
o_ins->fs_chunks_size -
o_ins->fs_backlog_chunks_size;
mk_list_foreach(head, &ic->in->chunks) {
old_ic = mk_list_entry(head, struct flb_input_chunk, _head);
if (flb_input_chunk_safe_delete(ic, old_ic, o_ins->id) == FLB_FALSE ||
flb_input_chunk_is_task_safe_delete(old_ic->task) == FLB_FALSE) {
continue;
}
bytes_remained += flb_input_chunk_get_real_size(old_ic);
count++;
if (bytes_remained >= (ssize_t) chunk_size) {
enough_space = FLB_TRUE;
break;
}
}
/*
* flb_intput_chunk_count_dropped_chunks(3) will only be called if the chunk will
* be flushing to the output instance passed in and the instance will reach its
* limit after appending the new data. This function will try to count how many
* chunks need to be dropped in order to place the incoing chunk.
*
* Return '0' means that we cannot find a slot to ingest the incoming data.
*/
if (enough_space == FLB_FALSE) {
return 0;
}
return count;
}
/*
* Find a slot in the output instance to append the new data with size chunk_size, it
* will drop the the oldest chunks when the limitation on local disk is reached.
*/
int flb_input_chunk_find_space_new_data(struct flb_input_chunk *ic,
size_t chunk_size, int overlimit)
{
int count;
int result;
ssize_t bytes;
ssize_t old_ic_bytes;
struct mk_list *tmp;
struct mk_list *head;
struct mk_list *head_chunk;
struct flb_output_instance *o_ins;
struct flb_input_chunk *old_ic;
size_t local_release_requirement;
/*
* For each output instances that will be over the limit after adding the new chunk,
* we have to determine how many chunks needs to be removed. We will adjust the
* routes_mask to only route to the output plugin that have enough space after
* deleting some chunks fome the queue.
*/
mk_list_foreach(head, &ic->in->config->outputs) {
count = 0;
o_ins = mk_list_entry(head, struct flb_output_instance, _head);
if ((o_ins->total_limit_size == -1) || ((1 << o_ins->id) & overlimit) == 0 ||
(flb_routes_mask_get_bit(ic->routes_mask, o_ins->id) == 0)) {
continue;
}
local_release_requirement = 0;
result = flb_input_chunk_release_space_compound(
ic, o_ins,
&local_release_requirement,
FLB_FALSE);
if (!result && local_release_requirement == 0) {
/* If this function returned 0 it means the space requirement was
* satisfied solely by releasing chunks in either storage_backlog
* state (segregated or in queue)
*/
continue;
}
/* flb_input_chunk_find_space_new_data_backlog may fail to meet the space
* requirements but it always sets local_release_requirement to the right amount
*/
count = flb_intput_chunk_count_dropped_chunks(ic, o_ins, local_release_requirement);
if (count == 0) {
/*
* The worst scenerio is that we cannot find a space by dropping some
* old chunks for the incoming chunk. We need to adjust the routes_mask
* of the incoming chunk to not flush to that output instance.
*/
flb_error("[input chunk] chunk %s would exceed total limit size in plugin %s",
flb_input_chunk_get_name(ic), o_ins->name);
flb_routes_mask_clear_bit(ic->routes_mask, o_ins->id);
if (flb_routes_mask_is_empty(ic->routes_mask)) {
bytes = flb_input_chunk_get_size(ic);
if (bytes != 0) {
/*
* Skip newly created chunk as newly created chunk
* hasn't updated the fs_chunks_size yet.
*/
bytes = flb_input_chunk_get_real_size(ic);
FS_CHUNK_SIZE_DEBUG_MOD(o_ins, ic, -bytes);
o_ins->fs_chunks_size -= bytes;
flb_debug("[input chunk] chunk %s has no output route, "
"remove %ld bytes from fs_chunks_size",
flb_input_chunk_get_name(ic), bytes);
}
}
continue;
}
/*
* Here we need to drop some chunks from the beginning of chunks list.
* Since chunks are stored in a double linked list (mk_list), we are
* able to iterate the list from the beginning and check if the current
* chunk is able to be removed.
*/
mk_list_foreach_safe(head_chunk, tmp, &ic->in->chunks) {
old_ic = mk_list_entry(head_chunk, struct flb_input_chunk, _head);
if (flb_input_chunk_safe_delete(ic, old_ic, o_ins->id) == FLB_FALSE ||
flb_input_chunk_is_task_safe_delete(old_ic->task) == FLB_FALSE) {
continue;
}
old_ic_bytes = flb_input_chunk_get_real_size(old_ic);
/* drop chunk by adjusting the routes_mask */
flb_routes_mask_clear_bit(old_ic->routes_mask, o_ins->id);
FS_CHUNK_SIZE_DEBUG_MOD(o_ins, old_ic, -old_ic_bytes);
o_ins->fs_chunks_size -= old_ic_bytes;
flb_debug("[input chunk] remove route of chunk %s with size %ld bytes to output plugin %s "
"to place the incoming data with size %ld bytes", flb_input_chunk_get_name(old_ic),
old_ic_bytes, o_ins->name, chunk_size);
if (flb_routes_mask_is_empty(old_ic->routes_mask)) {
if (old_ic->task != NULL) {
/*
* If the chunk is referenced by a task and task has no active route,
* we need to destroy the task as well.
*/
if (old_ic->task->users == 0) {
flb_debug("[task] drop task_id %d with no active route from input plugin %s",
old_ic->task->id, ic->in->name);
flb_task_destroy(old_ic->task, FLB_TRUE);
}
}
else {
flb_debug("[input chunk] drop chunk %s with no output route from input plugin %s",
flb_input_chunk_get_name(old_ic), ic->in->name);
flb_input_chunk_destroy(old_ic, FLB_TRUE);
}
}
count--;
if (count == 0) {
/* we have dropped enough chunks to place the incoming chunks */
break;
}
}
}
if (count != 0) {
flb_error("[input chunk] fail to drop enough chunks in order to place new data");
}
return 0;
}
/*
* Returns a non-zero result if any output instances will reach the limit
* after buffering the new data
*/
int flb_input_chunk_has_overlimit_routes(struct flb_input_chunk *ic,
size_t chunk_size)
{
int overlimit = 0;
struct mk_list *head;
struct flb_output_instance *o_ins;
mk_list_foreach(head, &ic->in->config->outputs) {
o_ins = mk_list_entry(head, struct flb_output_instance, _head);
if ((o_ins->total_limit_size == -1) ||
(flb_routes_mask_get_bit(ic->routes_mask, o_ins->id) == 0)) {
continue;
}
FS_CHUNK_SIZE_DEBUG(o_ins);
flb_debug("[input chunk] chunk %s required %ld bytes and %ld bytes left "
"in plugin %s", flb_input_chunk_get_name(ic), chunk_size,
o_ins->total_limit_size -
o_ins->fs_backlog_chunks_size -
o_ins->fs_chunks_size,
o_ins->name);
if ((o_ins->fs_chunks_size +
o_ins->fs_backlog_chunks_size +
chunk_size) > o_ins->total_limit_size) {
overlimit |= (1 << o_ins->id);
}
}
return overlimit;
}
/* Find a slot for the incoming data to buffer it in local file system
* returns 0 if none of the routes can be written to
*/
int flb_input_chunk_place_new_chunk(struct flb_input_chunk *ic, size_t chunk_size)
{
int overlimit;
overlimit = flb_input_chunk_has_overlimit_routes(ic, chunk_size);
if (overlimit != 0) {
flb_input_chunk_find_space_new_data(ic, chunk_size, overlimit);
}
return !flb_routes_mask_is_empty(ic->routes_mask);
}
/* Create an input chunk using a Chunk I/O */
struct flb_input_chunk *flb_input_chunk_map(struct flb_input_instance *in,
int event_type,
void *chunk)
{
int records = 0;
int tag_len;
int has_routes;
int ret;
uint64_t ts;
char *buf_data;
size_t buf_size;
size_t offset;
ssize_t bytes;
const char *tag_buf;
struct flb_input_chunk *ic;
/* Create context for the input instance */
ic = flb_calloc(1, sizeof(struct flb_input_chunk));
if (!ic) {
flb_errno();
return NULL;
}
ic->event_type = event_type;
ic->busy = FLB_FALSE;
ic->fs_counted = FLB_FALSE;
ic->fs_backlog = FLB_TRUE;
ic->chunk = chunk;
ic->in = in;
msgpack_packer_init(&ic->mp_pck, ic, flb_input_chunk_write);
ret = cio_chunk_get_content(ic->chunk, &buf_data, &buf_size);
if (ret != CIO_OK) {
flb_error("[input chunk] error retrieving content for metrics");
flb_free(ic);
return NULL;
}
if (ic->event_type == FLB_INPUT_LOGS) {
/* Validate records in the chunk */
ret = flb_mp_validate_log_chunk(buf_data, buf_size, &records, &offset);
if (ret == -1) {
/* If there are valid records, truncate the chunk size */
if (records <= 0) {
flb_plg_error(in,
"chunk validation failed, data might be corrupted. "
"No valid records found, the chunk will be discarded.");
flb_free(ic);
return NULL;
}
if (records > 0 && offset > 32) {
flb_plg_warn(in,
"chunk validation failed, data might be corrupted. "
"Found %d valid records, failed content starts "
"right after byte %lu. Recovering valid records.",
records, offset);
/* truncate the chunk to recover valid records */
cio_chunk_write_at(chunk, offset, NULL, 0);
}
else {
flb_plg_error(in,
"chunk validation failed, data might be corrupted. "
"Found %d valid records, failed content starts "
"right after byte %lu. Cannot recover chunk,",
records, offset);
flb_free(ic);
return NULL;
}
}
}
else if (ic->event_type == FLB_INPUT_METRICS) {
ret = flb_mp_validate_metric_chunk(buf_data, buf_size, &records, &offset);
if (ret == -1) {
if (records <= 0) {
flb_plg_error(in,
"metrics chunk validation failed, data might be corrupted. "
"No valid records found, the chunk will be discarded.");
flb_free(ic);
return NULL;
}
if (records > 0 && offset > 32) {
flb_plg_warn(in,
"metrics chunk validation failed, data might be corrupted. "
"Found %d valid records, failed content starts "
"right after byte %lu. Recovering valid records.",
records, offset);
/* truncate the chunk to recover valid records */
cio_chunk_write_at(chunk, offset, NULL, 0);
}
else {
flb_plg_error(in,
"metrics chunk validation failed, data might be corrupted. "
"Found %d valid records, failed content starts "
"right after byte %lu. Cannot recover chunk,",
records, offset);
flb_free(ic);
return NULL;
}
}
}
/* Skip chunks without content data */
if (records == 0) {
flb_plg_error(in,
"chunk validation failed, data might be corrupted. "
"No valid records found, the chunk will be discarded.");
flb_free(ic);
return NULL;
}
/*
* If the content is valid and the chunk has extra padding zeros, just
* perform an adjustment.
*/
bytes = cio_chunk_get_content_size(chunk);
if (bytes == -1) {
flb_free(ic);
return NULL;
}
if (offset < bytes) {
cio_chunk_write_at(chunk, offset, NULL, 0);
}
/* Update metrics */
#ifdef FLB_HAVE_METRICS
ic->total_records = records;
if (ic->total_records > 0) {
/* timestamp */
ts = cmt_time_now();
/* fluentbit_input_records_total */
cmt_counter_add(in->cmt_records, ts, ic->total_records,
1, (char *[]) {(char *) flb_input_name(in)});
/* fluentbit_input_bytes_total */
cmt_counter_add(in->cmt_bytes, ts, buf_size,
1, (char *[]) {(char *) flb_input_name(in)});
/* OLD metrics */
flb_metrics_sum(FLB_METRIC_N_RECORDS, ic->total_records, in->metrics);
flb_metrics_sum(FLB_METRIC_N_BYTES, buf_size, in->metrics);
}
#endif
/* Get the the tag reference (chunk metadata) */
ret = flb_input_chunk_get_tag(ic, &tag_buf, &tag_len);
if (ret == -1) {
flb_error("[input chunk] error retrieving tag of input chunk");
flb_free(ic);
return NULL;
}
bytes = flb_input_chunk_get_real_size(ic);
if (bytes < 0) {
flb_warn("[input chunk] could not retrieve chunk real size");
flb_free(ic);
return NULL;
}
has_routes = flb_routes_mask_set_by_tag(ic->routes_mask, tag_buf, tag_len, in);
if (has_routes == 0) {
flb_warn("[input chunk] no matching route for backoff log chunk %s",
flb_input_chunk_get_name(ic));
}
mk_list_add(&ic->_head, &in->chunks);
flb_input_chunk_update_output_instances(ic, bytes);
return ic;
}
static int input_chunk_write_header(struct cio_chunk *chunk, int event_type,
char *tag, int tag_len)
{
int ret;
int meta_size;
char *meta;
/*
* Prepare the Chunk metadata header
* ----------------------------------
* m[0] = FLB_INPUT_CHUNK_MAGIC_BYTE_0
* m[1] = FLB_INPUT_CHUNK_MAGIC_BYTE_1
* m[2] = type (FLB_INPUT_CHUNK_TYPE_LOG | FLB_INPUT_CHUNK_TYPE_METRIC)
* m[3] = 0 (unused for now)
*/
/* write metadata (tag) */
if (tag_len > (65535 - FLB_INPUT_CHUNK_META_HEADER)) {
/* truncate length */
tag_len = 65535 - FLB_INPUT_CHUNK_META_HEADER;
}
meta_size = FLB_INPUT_CHUNK_META_HEADER + tag_len;
/* Allocate buffer for metadata header */
meta = flb_calloc(1, meta_size);
if (!meta) {
flb_errno();
return -1;
}
/*
* Write chunk header in a temporary buffer
* ----------------------------------------
*/
/* magic bytes */
meta[0] = FLB_INPUT_CHUNK_MAGIC_BYTE_0;
meta[1] = FLB_INPUT_CHUNK_MAGIC_BYTE_1;
/* event type */
if (event_type == FLB_INPUT_LOGS) {
meta[2] = FLB_INPUT_CHUNK_TYPE_LOG;
}
else if (event_type == FLB_INPUT_METRICS) {
meta[2] = FLB_INPUT_CHUNK_TYPE_METRIC;
}
/* unused byte */
meta[3] = 0;
/* copy the tag after magic bytes */
memcpy(meta + FLB_INPUT_CHUNK_META_HEADER, tag, tag_len);
/* Write tag into metadata section */
ret = cio_meta_write(chunk, (char *) meta, meta_size);
if (ret == -1) {
flb_error("[input chunk] could not write metadata");
flb_free(meta);
return -1;
}
flb_free(meta);
return 0;
}
struct flb_input_chunk *flb_input_chunk_create(struct flb_input_instance *in,
const char *tag, int tag_len)
{
int ret;
int err;
int set_down = FLB_FALSE;
int has_routes;
char name[64];
struct cio_chunk *chunk;
struct flb_storage_input *storage;
struct flb_input_chunk *ic;
storage = in->storage;
/* chunk name */
generate_chunk_name(in, name, sizeof(name) - 1);
/* open/create target chunk file */
chunk = cio_chunk_open(storage->cio, storage->stream, name,
CIO_OPEN, FLB_INPUT_CHUNK_SIZE, &err);
if (!chunk) {
flb_error("[input chunk] could not create chunk file: %s:%s",
storage->stream->name, name);
return NULL;
}
/*
* If the returned chunk at open is 'down', just put it up, write the
* content and set it down again.
*/
ret = cio_chunk_is_up(chunk);
if (ret == CIO_FALSE) {
ret = cio_chunk_up_force(chunk);
if (ret == -1) {
cio_chunk_close(chunk, CIO_TRUE);
return NULL;
}
set_down = FLB_TRUE;
}
/* Write chunk header */
ret = input_chunk_write_header(chunk, in->event_type, (char *) tag, tag_len);
if (ret == -1) {
cio_chunk_close(chunk, CIO_TRUE);
return NULL;
}
/* Create context for the input instance */
ic = flb_calloc(1, sizeof(struct flb_input_chunk));
if (!ic) {
flb_errno();
cio_chunk_close(chunk, CIO_TRUE);
return NULL;
}
/*
* Check chunk content type to be created: depending of the value set by
* the input plugin, this can be FLB_INPUT_LOGS or FLB_INPUT_METRICS
*/
ic->event_type = in->event_type;
ic->busy = FLB_FALSE;
ic->fs_counted = FLB_FALSE;
ic->chunk = chunk;
ic->fs_backlog = FLB_FALSE;
ic->in = in;
ic->stream_off = 0;
ic->task = NULL;
#ifdef FLB_HAVE_METRICS
ic->total_records = 0;
#endif
/* Calculate the routes_mask for the input chunk */
has_routes = flb_routes_mask_set_by_tag(ic->routes_mask, tag, tag_len, in);
if (has_routes == 0) {
flb_trace("[input chunk] no matching route for input chunk '%s' with tag '%s'",
flb_input_chunk_get_name(ic), tag);
}
msgpack_packer_init(&ic->mp_pck, ic, flb_input_chunk_write);
mk_list_add(&ic->_head, &in->chunks);
if (set_down == FLB_TRUE) {
cio_chunk_down(chunk);