diff --git a/src/flb_output.c b/src/flb_output.c index 73b26358f2b..2c4bd2de771 100644 --- a/src/flb_output.c +++ b/src/flb_output.c @@ -1842,6 +1842,16 @@ int flb_output_enable_multi_threading(struct flb_output_instance *ins, struct fl { /* Multi-threading enabled ? (through 'workers' property) */ if (ins->tp_workers > 0) { + /* + * Proxy (Go) outputs reach this function twice: from the proxy + * cb_init and from flb_output_init_all(). The pool is created once + * per instance, so flb_output_thread_pool_destroy() stops and joins + * every worker thread. + */ + if (ins->tp) { + return 0; + } + if(flb_output_thread_pool_create(config, ins) != 0) { flb_output_instance_destroy(ins); return -1; diff --git a/tests/runtime_shell/conf/proxy_logs_workers_reload.conf b/tests/runtime_shell/conf/proxy_logs_workers_reload.conf new file mode 100644 index 00000000000..b0f904b0d61 --- /dev/null +++ b/tests/runtime_shell/conf/proxy_logs_workers_reload.conf @@ -0,0 +1,22 @@ +[SERVICE] + Flush 1 + Grace 2 + Log_Level info + Daemon Off + Hot_Reload On + +[INPUT] + Name dummy + Dummy {"message": "test log entry", "level": "info"} + Rate 5 + Tag test.logs + +[OUTPUT] + Name test_logs_go + Match test.logs + Workers 1 + +[OUTPUT] + Name test_logs_go + Match test.logs + Workers 1 diff --git a/tests/runtime_shell/proxy_logs_expect.sh b/tests/runtime_shell/proxy_logs_expect.sh index 37dd8ef58ed..f957f19427f 100755 --- a/tests/runtime_shell/proxy_logs_expect.sh +++ b/tests/runtime_shell/proxy_logs_expect.sh @@ -62,5 +62,99 @@ test_proxy_logs_compatibility() { return "$FLB_STATUS" } +# Count the lines in a file that contain a fixed string. +count_lines() { + grep -Fc "$1" "$2" || : +} + +# Wait until a fixed string appears a number of times in a file, while the +# process is alive. Return 1 on timeout or when the process exits. +wait_for_count() { + WAIT_PATTERN=$1 + WAIT_COUNT=$2 + WAIT_FILE=$3 + WAIT_PID=$4 + WAIT_SECONDS=0 + while [ "$(count_lines "$WAIT_PATTERN" "$WAIT_FILE")" -lt "$WAIT_COUNT" ]; do + if ! kill -0 "$WAIT_PID" 2>/dev/null || [ "$WAIT_SECONDS" -ge 30 ]; then + return 1 + fi + WAIT_SECONDS=$((WAIT_SECONDS + 1)) + sleep 1 + done + return 0 +} + +# Go outputs with workers get one thread pool per instance, and hot reload +# stops every worker of the old context before it is freed. +test_proxy_output_workers_reload() { + # build_test_plugins.sh enables errexit, and a failed shunit2 assertion + # returns non-zero. Run without it so every assertion reports and + # Fluent Bit is always stopped. + set +e + + if ! STDOUT_OUTPUT_FILE=$(mktemp "${TMPDIR:-/tmp}/test_logs_workers_reload.XXXXXX"); then + fail "could not create the Fluent Bit log file" + return 1 + fi + OUTPUTS=2 + RELOADS=2 + STARTED="worker #0 started" + STOPPED="thread worker #0 stopped" + RELOADED="successful reload done" + + "$FLB_BIN" -e "$FLB_ROOT/build/test_logs_go.so" -c "$FLB_RUNTIME_SHELL_CONF/proxy_logs_workers_reload.conf" > "$STDOUT_OUTPUT_FILE" 2>&1 & + FLB_PID=$! + + if ! wait_for_count "$STARTED" "$OUTPUTS" "$STDOUT_OUTPUT_FILE" "$FLB_PID"; then + fail "Go output workers did not start" + fi + # Let every thread pool of the first context report its workers. + sleep 2 + assertEquals "one worker per Go output instance after startup" \ + "$OUTPUTS" "$(count_lines "$STARTED" "$STDOUT_OUTPUT_FILE")" + + RELOAD=1 + while [ "$RELOAD" -le "$RELOADS" ]; do + kill -HUP "$FLB_PID" 2>/dev/null + if ! wait_for_count "$RELOADED" "$RELOAD" "$STDOUT_OUTPUT_FILE" "$FLB_PID"; then + fail "reload $RELOAD did not complete" + fi + # Give the timers of any worker left from the old context time to run. + sleep 3 + kill -0 "$FLB_PID" 2>/dev/null + assertEquals "Fluent Bit is running after reload $RELOAD" 0 "$?" + RELOAD=$((RELOAD + 1)) + done + + kill -TERM "$FLB_PID" 2>/dev/null + SHUTDOWN_WAIT=0 + while kill -0 "$FLB_PID" 2>/dev/null; do + if [ "$SHUTDOWN_WAIT" -ge 15 ]; then + fail "Fluent Bit shutdown exceeded 15 seconds" + kill -KILL "$FLB_PID" 2>/dev/null + break + fi + SHUTDOWN_WAIT=$((SHUTDOWN_WAIT + 1)) + sleep 1 + done + wait "$FLB_PID" + FLB_STATUS=$? + STARTED_COUNT=$(count_lines "$STARTED" "$STDOUT_OUTPUT_FILE") + STOPPED_COUNT=$(count_lines "$STOPPED" "$STDOUT_OUTPUT_FILE") + EXPECTED_COUNT=$((OUTPUTS * (RELOADS + 1))) + + assertEquals "Fluent Bit exits cleanly" 0 "$FLB_STATUS" + assertEquals "one worker per Go output instance per context" \ + "$EXPECTED_COUNT" "$STARTED_COUNT" + assertEquals "every started worker is stopped" \ + "$STARTED_COUNT" "$STOPPED_COUNT" + + # Print the Fluent Bit log without the records written by the Go output. + grep -v '^map\[' "$STDOUT_OUTPUT_FILE" + + rm -f "$STDOUT_OUTPUT_FILE" +} + # Load the runtime shell environment . "$FLB_RUNTIME_SHELL_PATH/runtime_shell.env"