-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a file lock to the data directory on startup to prevent multiple …
…agents. (#18483) (#18529) * Add a file lock to the data directory on startup to prevent multiple agents. * Add export comments to AppLocker. * Fix periodic to not block startup. (cherry picked from commit e1a4741)
- Loading branch information
1 parent
2ae668a
commit 06cf975
Showing
5 changed files
with
102 additions
and
13 deletions.
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