-
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
Add a file lock to the data directory on startup to prevent multiple agents. #18483
Merged
Merged
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 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,50 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package application | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/gofrs/flock" | ||
) | ||
|
||
const lockFileName = "agent.lock" | ||
|
||
// ErrAppAlreadyRunning error returned when another elastic-agent is already holding the lock. | ||
var ErrAppAlreadyRunning = fmt.Errorf("another elastic-agent is already running") | ||
|
||
// AppLocker locks the agent.lock file inside the provided directory. | ||
type AppLocker struct { | ||
lock *flock.Flock | ||
} | ||
|
||
// NewAppLocker creates an AppLocker that locks the agent.lock file inside the provided directory. | ||
func NewAppLocker(dir string) *AppLocker { | ||
if _, err := os.Stat(dir); os.IsNotExist(err) { | ||
_ = os.Mkdir(dir, 0755) | ||
} | ||
return &AppLocker{ | ||
lock: flock.New(filepath.Join(dir, lockFileName)), | ||
} | ||
} | ||
|
||
// TryLock tries to grab the lock file and returns error if it cannot. | ||
func (a *AppLocker) TryLock() error { | ||
locked, err := a.lock.TryLock() | ||
if err != nil { | ||
return err | ||
} | ||
if !locked { | ||
return ErrAppAlreadyRunning | ||
} | ||
return nil | ||
} | ||
|
||
// Unlock releases the lock file. | ||
func (a *AppLocker) Unlock() error { | ||
return a.lock.Unlock() | ||
} |
This file contains 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,29 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package application | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAppLocker(t *testing.T) { | ||
tmp, _ := ioutil.TempDir("", "locker") | ||
defer os.RemoveAll(tmp) | ||
|
||
locker1 := NewAppLocker(tmp) | ||
locker2 := NewAppLocker(tmp) | ||
|
||
require.NoError(t, locker1.TryLock()) | ||
assert.Error(t, locker2.TryLock()) | ||
require.NoError(t, locker1.Unlock()) | ||
require.NoError(t, locker2.TryLock()) | ||
assert.Error(t, locker1.TryLock()) | ||
require.NoError(t, locker2.Unlock()) | ||
} |
This file contains 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 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 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 |
---|---|---|
|
@@ -42,7 +42,7 @@ func NewDownloader(config *artifact.Config) *Downloader { | |
func (e *Downloader) Download(_ context.Context, programName, version string) (string, error) { | ||
// create a destination directory root/program | ||
destinationDir := filepath.Join(e.config.TargetDirectory, programName) | ||
if err := os.MkdirAll(destinationDir, os.ModeDir); err != nil { | ||
if err := os.MkdirAll(destinationDir, 0755); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a drive-by fix on Mac |
||
return "", errors.New(err, "creating directory for downloaded artifact failed", errors.TypeFilesystem, errors.M(errors.MetaKeyPath, destinationDir)) | ||
} | ||
|
||
|
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.
can you make sure this is handled even if we are killed. defer statements are skipped if SIGINT or SIGTERM are received and it can prevent us from restarting
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 have verified that this does get called in all the cases defined below with signals.
So the defer does get called. I did find a bug in periodic that was preventing
app.Start
from returning to catch the signals. I have fixed that in my most recent commit.