Skip to content

[9.4](backport #51897) make statestore creation of meta.json atomic#51926

Merged
leehinman merged 2 commits into
9.4from
mergify/bp/9.4/pr-51897
Jul 14, 2026
Merged

[9.4](backport #51897) make statestore creation of meta.json atomic#51926
leehinman merged 2 commits into
9.4from
mergify/bp/9.4/pr-51897

Conversation

@mergify

@mergify mergify Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Proposed commit message

WHAT: writeMetaFile in libbeat/statestore/backend/memlog previously opened the destination meta.json with O_TRUNC, wrote the JSON body, synced, and closed — all in-place on the final path. The new implementation follows the same temp-file-then-rename pattern introduced for Filebeat's registrar in #51791: os.CreateTemp creates a sibling .tmp-* file, the JSON is written and synced to it, and agentfile.SafeFileRotate atomically renames it into the final position. A deferred cleanup always removes the temp file on any error path.

WHY: With the old write pattern, if the process was killed between the O_TRUNC (which immediately empties the file) and the final Close, meta.json would be left empty or partially written. On the next start, readMetaFile would fail to parse it and the store would refuse to open. Because the rename is atomic at the OS level, the new approach guarantees that meta.json is either the previous valid version or the newly completed one — never an empty or partial file.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works. Where relevant, I have used the stresstest.sh script to run them under stress conditions and race detector to verify their stability.
  • I have added an entry in ./changelog/fragments using the changelog tool.

Disruptive User Impact

None. The change is internal to the memlog backend store and the observable behavior (a valid meta.json on disk) is identical.

How to test this PR locally

go test ./libbeat/statestore/backend/memlog/... -run TestWriteMetaFileAtomic -v

Related issues

Use cases

Screenshots

Logs


This is an automatic backport of pull request #51897 done by [Mergify](https://mergify.com).

* make statestore creation of meta.json atomic

* add changelog

* fix linter issues

* address scenario that Anderson caught

* fix linter issue

(cherry picked from commit 052272d)
@mergify mergify Bot requested a review from a team as a code owner July 14, 2026 14:52
@mergify mergify Bot added the backport label Jul 14, 2026
@mergify mergify Bot requested review from AndersonQ and belimawr and removed request for a team July 14, 2026 14:52
@botelastic botelastic Bot added the needs_team Indicates that the issue/PR needs a Team:* label label Jul 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🤖 GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)
  • /test : Run the Buildkite pipeline.

@github-actions github-actions Bot added Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team bugfix labels Jul 14, 2026
@botelastic botelastic Bot removed the needs_team Indicates that the issue/PR needs a Team:* label label Jul 14, 2026
@infra-vault-gh-plugin-prod

Copy link
Copy Markdown

Pinging @elastic/elastic-agent-data-plane (Team:Elastic-Agent-Data-Plane)

@mergify

mergify Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@leehinman leehinman merged commit d325362 into 9.4 Jul 14, 2026
204 checks passed
@leehinman leehinman deleted the mergify/bp/9.4/pr-51897 branch July 14, 2026 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport bugfix Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant