From e7c4cb07cdda4b9f13d25a335d2237759e54d685 Mon Sep 17 00:00:00 2001 From: "xinjun.jiang" Date: Wed, 2 Dec 2026 10:03:49 +0800 Subject: [PATCH 1/3] multiline: defer automatic flush while an input is paused Check backpressure before flushing each group for timer-owned inputs. Keep the pending group intact if a preceding append paused the input. Signed-off-by: xinjun.jiang --- include/fluent-bit/multiline/flb_ml.h | 3 +++ src/multiline/flb_ml.c | 27 ++++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/include/fluent-bit/multiline/flb_ml.h b/include/fluent-bit/multiline/flb_ml.h index c9c10e0ee43..1d351745dcc 100644 --- a/include/fluent-bit/multiline/flb_ml.h +++ b/include/fluent-bit/multiline/flb_ml.h @@ -284,6 +284,8 @@ struct flb_ml_group { struct mk_list _head; /* link to struct flb_ml->groups list */ }; +struct flb_input_instance; + struct flb_ml { flb_sds_t name; /* name of this multiline setup */ int flush_ms; /* max flush interval found in groups/parsers */ @@ -292,6 +294,7 @@ struct flb_ml { struct flb_log_event_encoder log_event_encoder; struct flb_log_event_decoder log_event_decoder; struct flb_config *config; /* Fluent Bit context */ + struct flb_input_instance *auto_flush_input; /* defer timed flush while paused */ /* Limit for concatenated multiline messages */ size_t buffer_limit; diff --git a/src/multiline/flb_ml.c b/src/multiline/flb_ml.c index a533f0a29f8..e46891e1adc 100644 --- a/src/multiline/flb_ml.c +++ b/src/multiline/flb_ml.c @@ -18,6 +18,7 @@ */ #include +#include #include #include #include @@ -97,9 +98,9 @@ int flb_ml_type_lookup(char *str) return type; } -void flb_ml_flush_parser_instance(struct flb_ml *ml, - struct flb_ml_parser_ins *parser_i, - uint64_t stream_id, int forced_flush) +static void flush_parser_instance(struct flb_ml_parser_ins *parser_i, + uint64_t stream_id, int forced_flush, + struct flb_input_instance *input) { struct mk_list *head; struct mk_list *head_group; @@ -114,13 +115,25 @@ void flb_ml_flush_parser_instance(struct flb_ml *ml, /* Iterate stream groups */ mk_list_foreach(head_group, &mst->groups) { + /* Flushing the preceding group may have paused the input. */ + if (input && flb_input_paused(input) == FLB_TRUE) { + return; + } group = mk_list_entry(head_group, struct flb_ml_stream_group, _head); flb_ml_flush_stream_group(parser_i->ml_parser, mst, group, forced_flush); } } } -void flb_ml_flush_pending(struct flb_ml *ml, uint64_t now, int forced_flush) +void flb_ml_flush_parser_instance(struct flb_ml *ml, + struct flb_ml_parser_ins *parser_i, + uint64_t stream_id, int forced_flush) +{ + flush_parser_instance(parser_i, stream_id, forced_flush, NULL); +} + +static void flush_pending(struct flb_ml *ml, uint64_t now, + struct flb_input_instance *input) { struct mk_list *head; struct flb_ml_parser_ins *parser_i; @@ -135,7 +148,7 @@ void flb_ml_flush_pending(struct flb_ml *ml, uint64_t now, int forced_flush) /* iterate group parser instances */ mk_list_foreach(head, &group->parsers) { parser_i = mk_list_entry(head, struct flb_ml_parser_ins, _head); - flb_ml_flush_parser_instance(ml, parser_i, 0, forced_flush); + flush_parser_instance(parser_i, 0, FLB_TRUE, input); } } @@ -144,7 +157,7 @@ void flb_ml_flush_pending_now(struct flb_ml *ml) uint64_t now; now = time_ms_now(); - flb_ml_flush_pending(ml, now, FLB_TRUE); + flush_pending(ml, now, NULL); } static void cb_ml_flush_timer(struct flb_config *ctx, void *data) @@ -161,7 +174,7 @@ static void cb_ml_flush_timer(struct flb_config *ctx, void *data) * Iterate over all streams and groups and for a flush for expired groups * which has not flushed in the last N milliseconds. */ - flb_ml_flush_pending(ml, now, FLB_TRUE); + flush_pending(ml, now, ml->auto_flush_input); } int flb_ml_register_context(struct flb_ml_stream_group *group, From 6eac5cdb146eef44747151882576f439b2b9d905 Mon Sep 17 00:00:00 2001 From: "xinjun.jiang" Date: Wed, 2 Dec 2026 10:03:54 +0800 Subject: [PATCH 2/3] in_tail: keep pending multiline data during backpressure Tell the multiline timer which input receives flushed records. Pending records can be delivered on a later timer tick after Tail resumes. Signed-off-by: xinjun.jiang --- plugins/in_tail/tail_config.c | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/in_tail/tail_config.c b/plugins/in_tail/tail_config.c index 467eb14689e..caf7a87352a 100644 --- a/plugins/in_tail/tail_config.c +++ b/plugins/in_tail/tail_config.c @@ -488,6 +488,7 @@ struct flb_tail_config *flb_tail_config_create(struct flb_input_instance *ins, } /* Enable auto-flush routine */ + ctx->ml_ctx->auto_flush_input = ins; ret = flb_ml_auto_flush_init(ctx->ml_ctx); if (ret == -1) { flb_plg_error(ctx->ins, "could not start multiline auto-flush"); From 0936ae499d8e79d42d603616c9d67154d7dfefc0 Mon Sep 17 00:00:00 2001 From: "xinjun.jiang" Date: Wed, 2 Dec 2026 10:03:58 +0800 Subject: [PATCH 3/3] tests: integration: cover Tail multiline backpressure Exercise both storage modes and both pause triggers with a retrying HTTP output. Recovery delivers each complete record exactly once. Signed-off-by: xinjun.jiang --- .../tests/test_tail_multiline_backpressure.py | 162 ++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 tests/integration/scenarios/in_tail/tests/test_tail_multiline_backpressure.py diff --git a/tests/integration/scenarios/in_tail/tests/test_tail_multiline_backpressure.py b/tests/integration/scenarios/in_tail/tests/test_tail_multiline_backpressure.py new file mode 100644 index 00000000000..ddda5b631c9 --- /dev/null +++ b/tests/integration/scenarios/in_tail/tests/test_tail_multiline_backpressure.py @@ -0,0 +1,162 @@ +"""Keep pending Tail multiline records through output backpressure.""" + +import json +import time +from collections import Counter +from pathlib import Path + +import pytest +import requests + +from server.http_server import data_storage, http_server_run +from test_in_tail_001 import flatten_records +from utils.test_service import FluentBitTestService + + +MULTILINE_FLUSH_MS = 1000 + + +@pytest.mark.parametrize( + "storage_type, limit_reason", + [("memory", "mem buf overlimit"), ("filesystem", "storage buf overlimit")], + ids=["mem-buf-limit", "storage-chunk-limit"], +) +@pytest.mark.parametrize("pause_trigger", ["file-read", "multiline-timer"]) +def test_in_tail_delivers_pending_multiline_after_resume(tmp_path, storage_type, limit_reason, + pause_trigger): + if pause_trigger == "file-read": + # Each start closes the previous record. The completed records cause the + # pause when Tail appends the read batch, leaving the last record pending. + entries = [("stdout", "first"), ("stdout", "second"), ("stdout", "last")] + else: + # Both groups remain pending in one file. The first group's timer + # append pauses Tail before the same timer tries the second group. + entries = [("stdout", "first"), ("stderr", "last")] + + log_path = tmp_path / "input.log" + expected = Counter() + lines = [] + for stream, name in entries: + start = f"ERROR {name} " + "x" * 80 + continuation = f" {name} continuation" + lines.extend(json.dumps({"stream": stream, "log": line}) + for line in (start, continuation)) + expected[(stream, f"{start}\n{continuation}")] += 1 + log_path.write_text("\n".join(lines) + "\n", encoding="utf-8") + + storage = "" + pause_limit = "mem_buf_limit: 70" + if storage_type == "filesystem": + storage = """ + storage.path: ${TAIL_TEST_STORAGE_PATH} + storage.max_chunks_up: 1""" + pause_limit = "storage.pause_on_chunks_overlimit: true" + + config = tmp_path / "tail.yaml" + config.write_text( + f"""service: + flush: 0.2 + grace: 1 + log_level: debug + http_server: on + http_listen: 127.0.0.1 + http_port: ${{FLUENT_BIT_HTTP_MONITORING_PORT}} + scheduler.base: 1 + scheduler.cap: 1{storage} +parsers: + - name: tail_json + format: json +multiline_parsers: + - name: tail_multiline + type: regex + parser: tail_json + key_content: log + key_group: stream + flush_timeout: {MULTILINE_FLUSH_MS} + rules: + - state: start_state + regex: '/^ERROR /' + next_state: cont + - state: cont + regex: '/^ /' + next_state: cont +pipeline: + inputs: + - name: tail + path: ${{TAIL_TEST_INPUT_PATH}} + tag: tail.backpressure + read_from_head: true + multiline.parser: tail_multiline + storage.type: {storage_type} + {pause_limit} + outputs: + - name: http + match: '*' + host: 127.0.0.1 + port: ${{TEST_SUITE_HTTP_PORT}} + uri: /data + format: json + json_date_key: false + retry_limit: false +""", + encoding="utf-8", + ) + service = FluentBitTestService( + str(config), + extra_env={ + "TAIL_TEST_INPUT_PATH": log_path, + "TAIL_TEST_STORAGE_PATH": tmp_path / "storage", + }, + ) + + def received(): + return Counter( + (record["stream"], record["log"]) + for record in flatten_records(data_storage["payloads"]) + ) + + receiver = None + try: + service.start() + log_file = Path(service.flb.log_file) + pause = f"tail.0 paused ({limit_reason}" + resume = f"tail.0 resume ({limit_reason}" + + def wait_for_log(message): + service.wait_for_condition( + lambda: message in log_file.read_text(encoding="utf-8", errors="replace"), + timeout=15, interval=0.05, description=message, + ) + + wait_for_log(pause) + wait_for_log("failed to flush chunk") + # Keep the output unavailable across at least two multiline timer ticks. + time.sleep(2.1 * MULTILINE_FLUSH_MS / 1000) + assert resume not in log_file.read_text(encoding="utf-8", errors="replace") + + # Starting the receiver releases backpressure without another file write. + receiver_url = f"http://127.0.0.1:{service.test_suite_http_port}" + receiver = http_server_run(service.test_suite_http_port) + service.wait_for_http_endpoint(f"{receiver_url}/ping", interval=0.1) + wait_for_log(resume) + try: + service.wait_for_condition( + lambda: sum(received().values()) >= len(entries), + timeout=10, interval=0.05, description="all multiline records", + ) + except TimeoutError: + assert received() == expected + + # A later timer tick must not emit a second copy. + time.sleep(1.5 * MULTILINE_FLUSH_MS / 1000) + assert received() == expected + finally: + try: + service.stop() + finally: + if receiver is not None: + try: + requests.post(f"{receiver_url}/shutdown", timeout=2) + except requests.RequestException: + pass + receiver.join(timeout=2)