Skip to content

Commit

Permalink
bin: reload: filter: Provide additional validator before loading filt…
Browse files Browse the repository at this point in the history
…er plugins

.cb_pre_run should be provided for filter plugins that is easily to get faulty
statuses.
For now, it's going to be used only for filter_lua.

Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 authored and edsiper committed Nov 4, 2023
1 parent 8a4fe62 commit fc8b8e8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
1 change: 1 addition & 0 deletions include/fluent-bit/flb_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct flb_filter_plugin {
struct flb_config_map *config_map;

/* Callbacks */
int (*cb_pre_run) (struct flb_filter_instance *, struct flb_config *, void *);
int (*cb_init) (struct flb_filter_instance *, struct flb_config *, void *);
int (*cb_filter) (const void *, size_t,
const char *, int,
Expand Down
9 changes: 6 additions & 3 deletions include/fluent-bit/flb_reload.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_config_format.h>

#define FLB_RELOAD_IDLE 0
#define FLB_RELOAD_IN_PROGRESS 1
#define FLB_RELOAD_ABORTED 2
#define FLB_RELOAD_IDLE 0
#define FLB_RELOAD_IN_PROGRESS 1
#define FLB_RELOAD_ABORTED -1
#define FLB_RELOAD_HALTED -2
#define FLB_RELOAD_NOT_ENABLED -3
#define FLB_RELOAD_INVALID_CONTEXT -4

int flb_reload_property_check_all(struct flb_config *config);
int flb_reload_reconstruct_cf(struct flb_cf *src_cf, struct flb_cf *dest_cf);
Expand Down
9 changes: 9 additions & 0 deletions src/flb_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,15 @@ int flb_filter_init(struct flb_config *config, struct flb_filter_instance *ins)
return 0;
}

/* Run pre_run callback for the filter */
if (p->cb_pre_run) {
ret = p->cb_pre_run(ins, config, ins->data);
if (ret != 0) {
flb_error("Failed pre_run callback on filter %s", ins->name);
return -1;
}
}

/* Initialize the input */
if (p->cb_init) {
ret = p->cb_init(ins, config, ins->data);
Expand Down
35 changes: 24 additions & 11 deletions src/flb_reload.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <fluent-bit/flb_kv.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_plugin.h>
#include <fluent-bit/flb_reload.h>

#include <cfl/cfl.h>
#include <cfl/cfl_sds.h>
Expand Down Expand Up @@ -128,6 +129,7 @@ static int flb_filter_propery_check_all(struct flb_config *config)
struct mk_list *tmp;
struct mk_list *head;
struct flb_filter_instance *ins;
struct flb_filter_plugin *p;

/* Iterate all active input instance plugins */
mk_list_foreach_safe(head, tmp, &config->filters) {
Expand All @@ -145,6 +147,17 @@ static int flb_filter_propery_check_all(struct flb_config *config)
return -1;
}

/* Check actual values with additional validator */
p = ins->p;
/* Run pre_run callback for the filter */
if (p->cb_pre_run) {
ret = p->cb_pre_run(ins, config, ins->data);
if (ret != 0) {
flb_error("Failed pre_run callback on filter %s", ins->name);
return -1;
}
}

/* destroy config map (will be recreated at flb_start) */
if (ins->config_map) {
flb_config_map_destroy(ins->config_map);
Expand Down Expand Up @@ -205,7 +218,7 @@ int flb_reload_property_check_all(struct flb_config *config)
/* 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");
flb_error("[reload] check properties and additonal vaildations for filter plugins is failed");

return -1;
}
Expand Down Expand Up @@ -371,13 +384,13 @@ int flb_reload(flb_ctx_t *ctx, struct flb_cf *cf_opts)

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

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

if (old_config->ensure_thread_safety_on_hot_reloading) {
Expand All @@ -391,7 +404,7 @@ int flb_reload(flb_ctx_t *ctx, struct flb_cf *cf_opts)
*/
new_cf = flb_cf_create();
if (!new_cf) {
return -1;
return FLB_RELOAD_HALTED;
}

flb_info("reloading instance pid=%lu tid=%p", (long unsigned) getpid(), pthread_self());
Expand All @@ -406,7 +419,7 @@ int flb_reload(flb_ctx_t *ctx, struct flb_cf *cf_opts)
}
flb_cf_destroy(new_cf);
flb_error("[reload] reconstruct cf failed");
return -1;
return FLB_RELOAD_HALTED;
}
}

Expand All @@ -419,7 +432,7 @@ int flb_reload(flb_ctx_t *ctx, struct flb_cf *cf_opts)
flb_cf_destroy(new_cf);
flb_error("[reload] creating flb context is failed. Reloading is halted");

return -1;
return FLB_RELOAD_HALTED;
}

new_config = new_ctx->config;
Expand All @@ -446,7 +459,7 @@ int flb_reload(flb_ctx_t *ctx, struct flb_cf *cf_opts)
if (!new_cf) {
flb_sds_destroy(file);

return -1;
return FLB_RELOAD_HALTED;
}
}

Expand All @@ -462,7 +475,7 @@ int flb_reload(flb_ctx_t *ctx, struct flb_cf *cf_opts)
flb_destroy(new_ctx);
flb_error("[reload] reloaded config is invalid. Reloading is halted");

return -1;
return FLB_RELOAD_HALTED;
}
}

Expand All @@ -476,7 +489,7 @@ int flb_reload(flb_ctx_t *ctx, struct flb_cf *cf_opts)

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

return -1;
return FLB_RELOAD_HALTED;
}

/* Validate plugin properites before fluent-bit stops the old context. */
Expand All @@ -489,7 +502,7 @@ int flb_reload(flb_ctx_t *ctx, struct flb_cf *cf_opts)

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

return -1;
return FLB_RELOAD_HALTED;
}

/* Delete the original context of config format before replacing
Expand Down Expand Up @@ -518,7 +531,7 @@ int flb_reload(flb_ctx_t *ctx, struct flb_cf *cf_opts)

flb_error("[reload] loaded configuration contains error(s). Reloading is aborted");

return -1;
return FLB_RELOAD_ABORTED;
}

/* Store the new value of hot reloading times into the new context */
Expand Down
7 changes: 6 additions & 1 deletion src/fluent-bit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1400,9 +1400,14 @@ int flb_main(int argc, char **argv)
flb_bin_restarting = FLB_RELOAD_IDLE;
}
else {
flb_bin_restarting = FLB_RELOAD_ABORTED;
flb_bin_restarting = ret;
}
}

if (flb_bin_restarting == FLB_RELOAD_HALTED) {
sleep(1);
flb_bin_restarting = FLB_RELOAD_IDLE;
}
}

if (exit_signal) {
Expand Down

0 comments on commit fc8b8e8

Please sign in to comment.