-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
flb_engine_dispatch.c
214 lines (189 loc) · 6.48 KB
/
flb_engine_dispatch.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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2019 The Fluent Bit Authors
* Copyright (C) 2015-2018 Treasure Data Inc.
*
* 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
*
* https://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.
*/
#include <stdlib.h>
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_input_chunk.h>
#include <fluent-bit/flb_output.h>
#include <fluent-bit/flb_router.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_thread.h>
#include <fluent-bit/flb_engine.h>
#include <fluent-bit/flb_task.h>
/* It creates a new output thread using a 'Retry' context */
int flb_engine_dispatch_retry(struct flb_task_retry *retry,
struct flb_config *config)
{
int ret;
size_t buf_size;
struct flb_thread *th;
struct flb_task *task;
struct flb_input_instance *i_ins;
task = retry->parent;
i_ins = task->i_ins;
/* Set file up/down based on restrictions */
ret = flb_input_chunk_set_up(task->ic);
if (ret == -1) {
/*
* The re-try is not possible. The chunk is not in memory and trying to bringing it
* up was not possible.
*
* A common cause for this is that the Chunk I/O system is not draining fast
* enough like errors on delivering data. So if we cannot put the chunk in memory
* it cannot be retried.
*/
ret = flb_task_retry_reschedule(retry, config);
if (ret == -1) {
return -1;
}
/* Just return because it has been re-scheduled */
return 0;
}
/* There is a match, get the buffer */
task->buf = flb_input_chunk_flush(task->ic, &buf_size);
task->size = buf_size;
if (!task->buf) {
/* Could not retrieve chunk content */
flb_error("[engine_dispatch] could not retrieve chunk content, removing retry");
flb_task_retry_destroy(retry);
return -1;
}
th = flb_output_thread(task,
i_ins,
retry->o_ins,
config,
task->buf, task->size,
task->tag, task->tag_len);
if (!th) {
return -1;
}
flb_task_add_thread(th, task);
flb_thread_resume(th);
return 0;
}
static int tasks_start(struct flb_input_instance *in,
struct flb_config *config)
{
struct mk_list *tmp;
struct mk_list *head;
struct mk_list *r_head;
struct flb_task *task;
struct flb_thread *th;
struct flb_task_route *route;
/* At this point the input instance should have some tasks linked */
mk_list_foreach_safe(head, tmp, &in->tasks) {
task = mk_list_entry(head, struct flb_task, _head);
/* Only process recently created tasks */
if (task->status != FLB_TASK_NEW) {
continue;
}
task->status = FLB_TASK_RUNNING;
/* A task contain one or more routes */
mk_list_foreach(r_head, &task->routes) {
route = mk_list_entry(r_head, struct flb_task_route, _head);
/*
* We have the Task and the Route, created a thread context for the
* data handling.
*/
th = flb_output_thread(task,
in,
route->out,
config,
task->buf, task->size,
task->tag,
task->tag_len);
flb_task_add_thread(th, task);
flb_thread_resume(th);
}
}
return 0;
}
/*
* The engine dispatch is responsible for:
*
* - Get chunks generated by input plugins.
* - For each set of records under the same tag, create a Task. A Task set
* a reference to the records and routes through output instances.
*/
int flb_engine_dispatch(uint64_t id, struct flb_input_instance *in,
struct flb_config *config)
{
int ret;
int t_err;
const char *buf_data;
size_t buf_size = 0;
const char *tag_buf;
int tag_len;
struct mk_list *tmp;
struct mk_list *head;
struct flb_input_plugin *p;
struct flb_input_chunk *ic;
struct flb_task *task = NULL;
p = in->p;
if (!p) {
return 0;
}
/* Look for chunks ready to go */
mk_list_foreach_safe(head, tmp, &in->chunks) {
ic = mk_list_entry(head, struct flb_input_chunk, _head);
if (ic->busy == FLB_TRUE) {
continue;
}
/* There is a match, get the buffer */
buf_data = flb_input_chunk_flush(ic, &buf_size);
if (buf_size == 0) {
/*
* Do not release the buffer since if allocated, it will be
* released when the task is destroyed.
*/
flb_input_chunk_release_lock(ic);
continue;
}
if (!buf_data) {
flb_input_chunk_release_lock(ic);
continue;
}
/* Get the the tag reference (chunk metadata) */
ret = flb_input_chunk_get_tag(ic, &tag_buf, &tag_len);
if (ret == -1) {
flb_input_chunk_release_lock(ic);
continue;
}
/* Create a task */
task = flb_task_create(id, buf_data, buf_size,
ic->in, ic,
tag_buf, tag_len,
config, &t_err);
if (!task) {
/*
* If task creation failed, check the error status flag. An error
* is associated with memory allocation or exhaustion of tasks_id,
* on that case the input chunk must be preserved and retried
* later. So we just release it busy lock.
*/
if (t_err == FLB_TRUE) {
flb_input_chunk_release_lock(ic);
}
continue;
}
}
/* Start the new enqueued Tasks */
tasks_start(in, config);
return 0;
}