-
Notifications
You must be signed in to change notification settings - Fork 176
Lock upgrade marker #8254
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
Merged
Merged
Lock upgrade marker #8254
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
733e11b
Lock upgrade marker at creation
pchila 8d2516c
WIP - lock update marker file when reading/writing from watcher
pchila f66dc21
Add test with external locking process
pchila 54e3176
Introduce yet another file locker
pchila 1b578ff
Run update marker lock unit tests in parallel
pchila 8498b72
Unify AppLock and FileLock implementations
pchila 588122c
fixup! Unify AppLock and FileLock implementations
pchila fbfd6ab
fixup! fixup! Unify AppLock and FileLock implementations
pchila 2113ca2
linting
pchila a32c2ee
fixup! linting
pchila 0b75e26
fixup! fixup! linting
pchila 33d8228
Add godoc for FileLocker
pchila 33b7791
Handle possible error at AppLocker creation
pchila 2fa86b1
Expose Locked() function through FileLocker and add tests for double …
pchila f381841
split lockMarkerFile() creation and locking
pchila ee52547
fixup! Handle possible error at AppLocker creation
pchila 955b09a
changelog
pchila 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License 2.0; | ||
// you may not use this file except in compliance with the Elastic License 2.0. | ||
|
||
package filelock | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/gofrs/flock" | ||
) | ||
|
||
var ( | ||
ErrZeroTimeout = errors.New("must specify a non-zero timeout for a blocking file locker") | ||
ErrNotLocked = errors.New("file not locked") | ||
) | ||
|
||
// FileLocker is a thin wrapper around "github.com/gofrs/flock" Flock providing both blocking and non-blocking file locking. | ||
// It exposes a simplified Lock*/Unlock interface and by default is non-blocking. | ||
// If it's not possible to acquire a lock on the specified file ErrNotLocked (directly or wrapped in another error) is returned by default. | ||
// It's possible to customize FileLocker behavior specifying one or more FileLockerOption at creation time. | ||
type FileLocker struct { | ||
fileLock *flock.Flock | ||
blocking bool | ||
timeout time.Duration | ||
customNotLockedError error | ||
} | ||
|
||
func NewFileLocker(lockFilePath string, opts ...FileLockerOption) (*FileLocker, error) { | ||
flocker := &FileLocker{fileLock: flock.New(lockFilePath)} | ||
for _, opt := range opts { | ||
if err := opt(flocker); err != nil { | ||
return nil, fmt.Errorf("applying options to new file locker: %w", err) | ||
} | ||
} | ||
return flocker, nil | ||
} | ||
|
||
// Lock() will attempt to lock the configured lockfile. Depending on the options specified at FileLocker creation this | ||
// call can be blocking or non-blocking. In order to use a blocking FileLocker a timeout must be specified at creation | ||
// specifying WithTimeout() option. | ||
// Even in case of a blocking FileLocker the maximum duration of the locking attempt will be the timeout specified at creation. | ||
// If no lock can be acquired ErrNotLocked error will be returned by default, unless a custom "not locked" error has been | ||
// specified with WithCustomNotLockedError at creation. | ||
func (fl *FileLocker) Lock() error { | ||
return fl.LockContext(context.Background()) | ||
} | ||
|
||
// LockWithContext() will attempt to lock the configured lockfile. It has the same semantics as Lock(), additionally it | ||
// allows passing a context as an argument to back out of locking attempts when context expires (useful in case of a | ||
// blocking FileLocker) | ||
func (fl *FileLocker) LockContext(ctx context.Context) error { | ||
var locked bool | ||
var err error | ||
|
||
if fl.blocking { | ||
timeoutCtx, cancel := context.WithTimeout(ctx, fl.timeout) | ||
defer cancel() | ||
locked, err = fl.fileLock.TryLockContext(timeoutCtx, time.Second) | ||
} else { | ||
locked, err = fl.fileLock.TryLock() | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("locking %s: %w", fl.fileLock.Path(), err) | ||
} | ||
if !locked { | ||
if fl.customNotLockedError != nil { | ||
return fmt.Errorf("failed locking %s: %w", fl.fileLock.Path(), fl.customNotLockedError) | ||
} | ||
return fmt.Errorf("failed locking %s: %w", fl.fileLock.Path(), ErrNotLocked) | ||
} | ||
return nil | ||
} | ||
|
||
func (fl *FileLocker) Unlock() error { | ||
return fl.fileLock.Unlock() | ||
} | ||
|
||
func (fl *FileLocker) Locked() bool { | ||
return fl.fileLock.Locked() | ||
} | ||
|
||
type FileLockerOption func(locker *FileLocker) error | ||
|
||
// WithCustomNotLockedError will set a custom error to be returned when it's not possible to acquire a lock | ||
func WithCustomNotLockedError(customError error) FileLockerOption { | ||
return func(locker *FileLocker) error { | ||
locker.customNotLockedError = customError | ||
return nil | ||
} | ||
} | ||
|
||
// WithTimeout will set the FileLocker to be blocking and will enforce a non-zero timeout. | ||
// If a zero timeout is passed this option will error out, failing the FileLocker creation. | ||
func WithTimeout(timeout time.Duration) FileLockerOption { | ||
return func(locker *FileLocker) error { | ||
|
||
if timeout == 0 { | ||
return ErrZeroTimeout | ||
} | ||
|
||
locker.blocking = true | ||
locker.timeout = timeout | ||
|
||
return nil | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.