-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Refactor beats lockfile to use timeout, retry #34194
Refactor beats lockfile to use timeout, retry #34194
Conversation
Pinging @elastic/elastic-agent (Team:Elastic-Agent) |
This pull request does not have a backport label.
To fixup this pull request, you need to add the backport labels for the needed
|
if openErr != nil { | ||
err = lock.handleFailedCreate() | ||
for i := 0; i < lock.retryCount; i++ { | ||
gotLock, err := lock.fileLock.TryLock() |
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.
I don't think TryLock uses the os.O_EXCL
flag. That means the file could exist already, and I think that would lead to a race condition in the Unlock function with Unlock & Remove.
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.
Could you elaborate? I assume you mean another beat swooping in while one beat is trying to lock or remove the file?
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.
In the unlock code, it first unlocks, then does a remove. In between those lines of code another beat could create a new lock, but the file would be removed. This results in the new beat having an error if it goes to unlock because the lock file doesn't exist.
panic: unable to unlock data path file testing.lock: remove testing.lock: no such file or directory
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.
hmm, that's an interesting edge case. Gonna see if I can think of a non-awkward way to protect against that.
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.
Alright, made a change to remove the file first before we remove the lock. Going to see how the Windows CI reacts to that, but I imagine we'll want some manual testing, since I don't understand the Windows lockfile logic too well.
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.
Can we put some or all of the detail from the PR description directly in the description of the Lock function? For example adding this would help the next developer to understand how this works.
In a case where a beat has shutdown improperly and the lockfile remains, instead of looking up a PID, we rely on the OS to release the underlying lock for the dead process, which most OSes will generally do, after a set amount of time.
It may also be worth noting that putting the PID into the lock file failed. To some degree we have had several iterations on this code because the original code did not explain itself at all, so let's try to avoid creating that problem again.
Needs a changelog entry 📓 |
Hi Team, could you please clarify to me, if it will be in new release or backported to 8.6.0? Because, at the moment the issue ( |
Yes the plan is to put this in 8.6.1 as well as 8.7.0. |
Alright, tested manually on Linux, seems fine. |
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.
unfortunately on Windows we get this panic on unlock
panic: unable to remove data path file testing.lock: remove testing.lock: The process cannot access the file because it is being used by another process.
This pull request is now in conflicts. Could you fix it? 🙏
|
Made a few changed based on a discussion with @leehinman , tested on Linux/darwin |
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.
tried it on Windows, doesn't work. Lock followed by Unlock gives: panic: unable to remove data path file testing.lock: remove testing.lock: The process cannot access the file because it is being used by another process.
nevermind, it worked on Windows. I screwed up the build and got the version of the PR. |
libbeat/cmd/instance/locks/lock.go
Outdated
lock.logger.Debugf("%s shut down without removing previous lockfile and is currently in a zombie state, continuing", lock.beatName) | ||
return lock.recoverLockfile() | ||
// now unlock on windows. | ||
if runtime.GOOS == "windows" { |
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.
could we split this into 3 files? lock.go, unlock_posix.go, unlock_windows.go. Then we can have the Unlock
function in the OS specific file, and use build tags to only compile the "right" one. The big benefit is that the doc strings can be specific to the OS since we are switching behavior based on that and it will be easier to understand later on.
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.
Ah, good idea.
} | ||
|
||
// now unlock on windows. | ||
if runtime.GOOS == "windows" { |
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.
can we get rid of the runtime check? The build tags should mean this is the only "Unlock" implementation under Windows.
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.
Oh! Forgot to delete that...
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.
LGTM
* move lockfile logic to a retries * clean up * add changelog, update docs * change unlock operation, remove file first * fix tests * fix lock on windows * remove debug line * add docs * split out files * remove old OS checks * fix error * format (cherry picked from commit 21b6128) # Conflicts: # libbeat/cmd/instance/locks/lock.go
Hi!! |
This will be in 8.6.2 which is coming soon. |
…34435) * Refactor beats lockfile to use timeout, retry (#34194) * move lockfile logic to a retries * clean up * add changelog, update docs * change unlock operation, remove file first * fix tests * fix lock on windows * remove debug line * add docs * split out files * remove old OS checks * fix error * format (cherry picked from commit 21b6128) # Conflicts: # libbeat/cmd/instance/locks/lock.go * fix cherry pick --------- Co-authored-by: Alex K <[email protected]> Co-authored-by: Alex Kristiansen <[email protected]> Co-authored-by: Craig MacKenzie <[email protected]>
Hello!! |
|
The report of this still happening on 8.6.2 was a false alarm related to misconfiguration. |
* move lockfile logic to a retries * clean up * add changelog, update docs * change unlock operation, remove file first * fix tests * fix lock on windows * remove debug line * add docs * split out files * remove old OS checks * fix error * format
What does this PR do?
This PR substantially refactors the lockfile to remove the "PID check" system and instead retry the underlying lock operation. This is mainly to deal with an (apparently somewhat common) edge case beat will shutdown improperly, leaving the old lockfile around, and the container environment will restart with new PID namespace, allowing for a collision between the PID written in the lockfile, and another running process.
This change puts the lockfile logic back in the hands of the OS; we try to obtain a lock, and if we can't, we retry a set number of times. In a case where a beat has shutdown improperly and the lockfile remains, instead of looking up a PID, we rely on the OS to release the underlying lock for the dead process, which most OSes will generally do, after a set amount of time.
I still need to test this by hand on Windows and Darwin.
Why is it important?
This is meant to deal with a few edge cases in how PID handling works.
Checklist
CHANGELOG.next.asciidoc
orCHANGELOG-developer.next.asciidoc
.