Repository navigation
in_tail: retain pending multiline records during backpressure #12895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
git-jxj
wants to merge
3
commits into
fluent:master
Choose a base branch
from
git-jxj:fix/tail-retry-multiline-after-pause
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+186
−7
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
tests/integration/scenarios/in_tail/tests/test_tail_multiline_backpressure.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Observe timer execution instead of assuming it from a delay.
If Fluent Bit is descheduled after the pause, Line 134 can finish before any paused timer callback runs. Starting the receiver can then clear backpressure before the callback executes. The regression test can pass without exercising the failure it must detect. Line 151 also does not establish that a later timer callback ran. (raw.githubusercontent.com)
Use bounded synchronization with observed timer execution, such as a callback counter or test notification. Observe the required paused callbacks before starting the receiver. Observe a subsequent callback before the final equality assertion.
Based on learnings, “Elapsed time does not establish readiness.”
Also applies to: 151-151
Source: Learnings