summaryrefslogtreecommitdiffstats
path: root/fluent-bit/src/flb_reload.c
blob: 641fa4a66d0173c7df68bb73594194189fca54d2 (plain)
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2023 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.
 */


#include <fluent-bit/flb_error.h>
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_lib.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_filter.h>
#include <fluent-bit/flb_output.h>
#include <fluent-bit/flb_custom.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_config_format.h>
#include <fluent-bit/flb_kv.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_plugin.h>

#include <cfl/cfl.h>
#include <cfl/cfl_sds.h>
#include <cfl/cfl_variant.h>
#include <cfl/cfl_kvlist.h>

static int flb_input_propery_check_all(struct flb_config *config)
{
    int ret;
    struct mk_list *tmp;
    struct mk_list *head;
    struct flb_input_instance *ins;
    struct flb_input_plugin *p;

    /* Iterate all active input instance plugins */
    mk_list_foreach_safe(head, tmp, &config->inputs) {
        ins = mk_list_entry(head, struct flb_input_instance, _head);
        p = ins->p;

        /* Skip pseudo input plugins */
        if (!p) {
            continue;
        }

        /* Check net property */
        ret = flb_input_net_property_check(ins, config);
        if (ret == -1) {
            return -1;
        }

        /* Check plugin property */
        ret = flb_input_plugin_property_check(ins, config);
        if (ret == -1) {
            return -1;
        }

        /* destroy net config map (will be recreated at flb_start) */
        if (ins->net_config_map) {
            flb_config_map_destroy(ins->net_config_map);
            ins->net_config_map = NULL;
        }

        /* destroy config map (will be recreated at flb_start) */
        if (ins->config_map) {
            flb_config_map_destroy(ins->config_map);
            ins->config_map = NULL;
        }
    }

    return 0;
}

static int flb_output_propery_check_all(struct flb_config *config)
{
    int ret;
    struct mk_list *tmp;
    struct mk_list *head;
    struct flb_output_instance *ins;

    /* Iterate all active input instance plugins */
    mk_list_foreach_safe(head, tmp, &config->outputs) {
        ins = mk_list_entry(head, struct flb_output_instance, _head);

        /* Check net property */
        ret = flb_output_net_property_check(ins, config);
        if (ret == -1) {
            return -1;
        }

        /* Check plugin property */
        ret = flb_output_plugin_property_check(ins, config);
        if (ret == -1) {
            return -1;
        }

        /* destroy net config map (will be recreated at flb_start) */
        if (ins->net_config_map) {
            flb_config_map_destroy(ins->net_config_map);
            ins->net_config_map = NULL;
        }

        /* destroy config map (will be recreated at flb_start) */
        if (ins->config_map) {
            flb_config_map_destroy(ins->config_map);
            ins->config_map = NULL;
        }
    }

    return 0;
}

static int flb_filter_propery_check_all(struct flb_config *config)
{
    int ret;
    struct mk_list *tmp;
    struct mk_list *head;
    struct flb_filter_instance *ins;

    /* Iterate all active input instance plugins */
    mk_list_foreach_safe(head, tmp, &config->filters) {
        ins = mk_list_entry(head, struct flb_filter_instance, _head);

        if (flb_filter_match_property_existence(ins) == FLB_FALSE) {
            flb_error("[filter] NO match rule for %s filter instance, halting to reload.",
                     ins->name);
            return -1;
        }

        /* Check plugin property */
        ret = flb_filter_plugin_property_check(ins, config);
        if (ret == -1) {
            return -1;
        }

        /* destroy config map (will be recreated at flb_start) */
        if (ins->config_map) {
            flb_config_map_destroy(ins->config_map);
            ins->config_map = NULL;
        }
    }

    return 0;
}

static int flb_custom_propery_check_all(struct flb_config *config)
{
    int ret;
    struct mk_list *tmp;
    struct mk_list *head;
    struct flb_custom_instance *ins;

    /* Iterate all active input instance plugins */
    mk_list_foreach_safe(head, tmp, &config->customs) {
        ins = mk_list_entry(head, struct flb_custom_instance, _head);

        /* Check plugin property */
        ret = flb_custom_plugin_property_check(ins, config);
        if (ret == -1) {
            return -1;
        }

        /* destroy config map (will be recreated at flb_start) */
        if (ins->config_map) {
            flb_config_map_destroy(ins->config_map);
            ins->config_map = NULL;
        }
    }

    return 0;
}

int flb_reload_property_check_all(struct flb_config *config)
{
    int ret = 0;

    /* Check properties of custom plugins */
    ret = flb_custom_propery_check_all(config);
    if (ret == -1) {
        flb_error("[reload] check properties for custom plugins is failed");

        return -1;
    }

    /* Check properties of input plugins */
    ret = flb_input_propery_check_all(config);
    if (ret == -1) {
        flb_error("[reload] check properties for input plugins is failed");

        return -1;
    }

    /* Check properties of filter plugins */
    ret = flb_filter_propery_check_all(config);
    if (ret == -1) {
        flb_error("[reload] check properties for filter plugins is failed");

        return -1;
    }

    /* Check properties of output plugins */
    ret = flb_output_propery_check_all(config);
    if (ret == -1) {
        flb_error("[reload] check properties for output plugins is failed");

        return -1;
    }

    return 0;
}

/*
 * Hot reload
 * ----------
 * Reload a Fluent Bit instance by using a new 'config_format' context.
 *
 *  1. As a first step, the config format is validated against the 'config maps',
 *     this will check that all configuration properties are valid.
 */

static int recreate_cf_section(struct flb_cf_section *s, struct flb_cf *cf)
{
    struct mk_list *head;
    struct cfl_list *p_head;
    struct cfl_kvpair *kv;
    struct flb_cf_group *g;
    struct flb_cf_section *new_s;
    struct flb_cf_group *new_g;
    struct cfl_variant *var = NULL;

    new_s = flb_cf_section_create(cf, s->name, flb_sds_len(s->name));
    if (cfl_list_size(&s->properties->list) > 0) {
        cfl_list_foreach(p_head, &s->properties->list) {
            var = NULL;
            kv = cfl_list_entry(p_head, struct cfl_kvpair, _head);
            var = flb_cf_section_property_add(cf, new_s->properties,
                                              kv->key, cfl_sds_len(kv->key),
                                              kv->val->data.as_string, cfl_sds_len(kv->val->data.as_string));

            if (var == NULL) {
                flb_error("[reload] recreating section '%s' property '%s' is failed", s->name, kv->key);
                return -1;
            }
        }
    }

    if (mk_list_size(&s->groups) <= 0) {
        return 0;
    }

    mk_list_foreach(head, &s->groups) {
        g = mk_list_entry(head, struct flb_cf_group, _head);
        new_g = flb_cf_group_create(cf, new_s, g->name, flb_sds_len(g->name));

        if (cfl_list_size(&g->properties->list) > 0) {
            cfl_list_foreach(p_head, &g->properties->list) {
                var = NULL;
                kv = cfl_list_entry(p_head, struct cfl_kvpair, _head);
                var = flb_cf_section_property_add(cf, new_g->properties,
                                                  kv->key, cfl_sds_len(kv->key),
                                                  kv->val->data.as_string, cfl_sds_len(kv->val->data.as_string));
                if (var == NULL) {
                    flb_error("[reload] recreating group '%s' property '%s' is failed", g->name, kv->key);
                    return -1;
                }
            }
        }
    }

    return 0;
}

int flb_reload_reconstruct_cf(struct flb_cf *src_cf, struct flb_cf *dest_cf)
{
    struct mk_list *head;
    struct flb_cf_section *s;
    struct flb_kv *kv;

    mk_list_foreach(head, &src_cf->sections) {
        s = mk_list_entry(head, struct flb_cf_section, _head);
        if (recreate_cf_section(s, dest_cf) != 0) {
            return -1;
        }
    }

    /* Copy and store env. (For yaml cf.) */
    mk_list_foreach(head, &src_cf->env) {
        kv = mk_list_entry(head, struct flb_kv, _head);
        if (!flb_cf_env_property_add(dest_cf,
                                     kv->key, cfl_sds_len(kv->key),
                                     kv->val, cfl_sds_len(kv->val))) {
            return -1;
        }

    }

    /* Copy and store metas. (For old fluent-bit cf.) */
    mk_list_foreach(head, &src_cf->metas) {
        kv = mk_list_entry(head, struct flb_kv, _head);
        if (!flb_kv_item_create_len(&dest_cf->metas,
                                    kv->key, cfl_sds_len(kv->key),
                                    kv->val, cfl_sds_len(kv->val))) {
            return -1;
        }

    }

    return 0;
}

#ifdef FLB_HAVE_STREAM_PROCESSOR
static int flb_reload_reconstruct_sp(struct flb_config *src, struct flb_config *dest)
{
    struct mk_list *head;
    struct flb_slist_entry *e;

    /* Check for pre-configured Tasks (command line) */
    mk_list_foreach(head, &src->stream_processor_tasks) {
        e = mk_list_entry(head, struct flb_slist_entry, _head);
        flb_slist_add(&dest->stream_processor_tasks, e->str);
    }

    return 0;
}
#endif

static int flb_reload_reinstantiate_external_plugins(struct flb_config *src, struct flb_config *dest)
{
    int ret;
    struct mk_list *head;
    struct flb_slist_entry *e;

    /* Check for pre-configured Tasks (command line) */
    mk_list_foreach(head, &src->external_plugins) {
        e = mk_list_entry(head, struct flb_slist_entry, _head);
        flb_info("[reload] slist externals %s", e->str);
        /* Load the new config format context to config context. */
        ret = flb_plugin_load_router(e->str, dest);
        if (ret != 0) {
            return -1;
        }
        flb_slist_add(&dest->external_plugins, e->str);
    }

    return 0;
}

int flb_reload(flb_ctx_t *ctx, struct flb_cf *cf_opts)
{
    int ret;
    flb_sds_t file = NULL;
    struct flb_config *old_config;
    struct flb_config *new_config;
    flb_ctx_t *new_ctx = NULL;
    struct flb_cf *new_cf;
    struct flb_cf *original_cf;
    int verbose;
    int reloaded_count = 0;

    if (ctx == NULL) {
        flb_error("[reload] given flb context is NULL");
        return -2;
    }

    old_config = ctx->config;
    if (old_config->enable_hot_reload != FLB_TRUE) {
        flb_warn("[reload] hot reloading is not enabled");
        return -3;
    }

    if (old_config->ensure_thread_safety_on_hot_reloading) {
        old_config->grace = -1;
    }

    /* Normally, we should create a service section before using this cf
     * context. However, this context of config format will be used
     * for copying contents from other one. So, we just need to create
     * a new cf instance here.
     */
    new_cf = flb_cf_create();
    if (!new_cf) {
        return -1;
    }

    flb_info("reloading instance pid=%lu tid=%p", (long unsigned) getpid(), pthread_self());

    if (old_config->conf_path_file) {
        file = flb_sds_create(old_config->conf_path_file);
    }
    if (cf_opts != NULL) {
        if (flb_reload_reconstruct_cf(cf_opts, new_cf) != 0) {
            if (file != NULL) {
                flb_sds_destroy(file);
            }
            flb_error("[reload] reconstruct cf failed");
            return -1;
        }
    }

    /* Create another instance */
    new_ctx = flb_create();
    if (new_ctx == NULL) {
        if (file != NULL) {
            flb_sds_destroy(file);
        }
        flb_cf_destroy(new_cf);
        flb_error("[reload] creating flb context is failed. Reloading is halted");

        return -1;
    }

    new_config = new_ctx->config;

    /* Inherit verbose from the old ctx instance */
    verbose = ctx->config->verbose;
    new_config->verbose = verbose;
    /* Increment and store the number of hot reloaded times */
    reloaded_count = ctx->config->hot_reloaded_count + 1;

#ifdef FLB_HAVE_STREAM_PROCESSOR
    /* Inherit stream processor definitions from command line */
    flb_reload_reconstruct_sp(old_config, new_config);
#endif

    /* Create another config format context */
    if (file != NULL) {
        new_cf = flb_cf_create_from_file(new_cf, file);

        if (!new_cf) {
            flb_sds_destroy(file);

            return -1;
        }
    }

    /* Load external plugins via command line */
    if (mk_list_size(&old_config->external_plugins) > 0) {
        ret = flb_reload_reinstantiate_external_plugins(old_config, new_config);
        if (ret == -1) {
            if (file != NULL) {
                flb_sds_destroy(file);
            }
            flb_cf_destroy(new_cf);
            flb_stop(new_ctx);
            flb_destroy(new_ctx);
            flb_error("[reload] reloaded config is invalid. Reloading is halted");

            return -1;
        }
    }

    /* Load the new config format context to config context. */
    ret = flb_config_load_config_format(new_config, new_cf);
    if (ret != 0) {
        flb_sds_destroy(file);
        flb_cf_destroy(new_cf);
        flb_stop(new_ctx);
        flb_destroy(new_ctx);

        flb_error("[reload] reloaded config format is invalid. Reloading is halted");

        return -1;
    }

    /* Validate plugin properites before fluent-bit stops the old context. */
    ret = flb_reload_property_check_all(new_config);
    if (ret != 0) {
        flb_sds_destroy(file);
        flb_cf_destroy(new_cf);
        flb_stop(new_ctx);
        flb_destroy(new_ctx);

        flb_error("[reload] reloaded config is invalid. Reloading is halted");

        return -1;
    }

    /* Delete the original context of config format before replacing
     * with the new one. */
    original_cf = new_config->cf_main;
    flb_cf_destroy(original_cf);

    new_config->cf_main = new_cf;
    new_config->cf_opts = cf_opts;

    if (file != NULL) {
        new_config->conf_path_file = file;
    }

    flb_info("[reload] stop everything of the old context");
    flb_stop(ctx);
    flb_destroy(ctx);

    flb_info("[reload] start everything");

    ret = flb_start(new_ctx);

    /* Store the new value of hot reloading times into the new context */
    if (ret == 0) {
        new_config->hot_reloaded_count = reloaded_count;
        flb_debug("[reload] hot reloaded %d time(s)", reloaded_count);
    }

    return 0;
}