forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix race in determining start port for cluster
The logic here is racy since multiple endtoend tests can create the file at around the same time and allocate the same base port. This then leads to flaky failing endtoend tests since not all ports can be used. Even with the listening check later on, this is still racy because the listen check might run at a later point still trying to allocate the same port for the same service in different endtoend test. This copies the Go filelock implementation, but only for unix systems since that's the only supported platform for Vitess. Go has a proposal open to make filelock available, but that is still pending in golang/go#33974. Once that is resolved, we can remove our copy of the implementation. Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
- Loading branch information
Showing
4 changed files
with
177 additions
and
10 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
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2018 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// Package filelock provides a platform-independent API for advisory file | ||
// locking. Calls to functions in this package on platforms that do not support | ||
// advisory locks will return errors for which IsNotSupported returns true. | ||
package filelock | ||
|
||
import ( | ||
"errors" | ||
"io/fs" | ||
"os" | ||
) | ||
|
||
// A File provides the minimal set of methods required to lock an open file. | ||
// File implementations must be usable as map keys. | ||
// The usual implementation is *os.File. | ||
type File interface { | ||
// Name returns the name of the file. | ||
Name() string | ||
|
||
// Fd returns a valid file descriptor. | ||
// (If the File is an *os.File, it must not be closed.) | ||
Fd() uintptr | ||
|
||
// Stat returns the FileInfo structure describing file. | ||
Stat() (fs.FileInfo, error) | ||
} | ||
|
||
// Lock places an advisory write lock on the file, blocking until it can be | ||
// locked. | ||
// | ||
// If Lock returns nil, no other process will be able to place a read or write | ||
// lock on the file until this process exits, closes f, or calls Unlock on it. | ||
// | ||
// If f's descriptor is already read- or write-locked, the behavior of Lock is | ||
// unspecified. | ||
// | ||
// Closing the file may or may not release the lock promptly. Callers should | ||
// ensure that Unlock is always called when Lock succeeds. | ||
func Lock(f File) error { | ||
return lock(f, writeLock) | ||
} | ||
|
||
// RLock places an advisory read lock on the file, blocking until it can be locked. | ||
// | ||
// If RLock returns nil, no other process will be able to place a write lock on | ||
// the file until this process exits, closes f, or calls Unlock on it. | ||
// | ||
// If f is already read- or write-locked, the behavior of RLock is unspecified. | ||
// | ||
// Closing the file may or may not release the lock promptly. Callers should | ||
// ensure that Unlock is always called if RLock succeeds. | ||
func RLock(f File) error { | ||
return lock(f, readLock) | ||
} | ||
|
||
// Unlock removes an advisory lock placed on f by this process. | ||
// | ||
// The caller must not attempt to unlock a file that is not locked. | ||
func Unlock(f File) error { | ||
return unlock(f) | ||
} | ||
|
||
// String returns the name of the function corresponding to lt | ||
// (Lock, RLock, or Unlock). | ||
func (lt lockType) String() string { | ||
switch lt { | ||
case readLock: | ||
return "RLock" | ||
case writeLock: | ||
return "Lock" | ||
default: | ||
return "Unlock" | ||
} | ||
} | ||
|
||
// IsNotSupported returns a boolean indicating whether the error is known to | ||
// report that a function is not supported (possibly for a specific input). | ||
// It is satisfied by ErrNotSupported as well as some syscall errors. | ||
func IsNotSupported(err error) bool { | ||
return isNotSupported(underlyingError(err)) | ||
} | ||
|
||
var ErrNotSupported = errors.New("operation not supported") | ||
|
||
// underlyingError returns the underlying error for known os error types. | ||
func underlyingError(err error) error { | ||
switch err := err.(type) { | ||
case *fs.PathError: | ||
return err.Err | ||
case *os.LinkError: | ||
return err.Err | ||
case *os.SyscallError: | ||
return err.Err | ||
} | ||
return err | ||
} |
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,42 @@ | ||
// Copyright 2018 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package filelock | ||
|
||
import ( | ||
"io/fs" | ||
"syscall" | ||
) | ||
|
||
type lockType int16 | ||
|
||
const ( | ||
readLock lockType = syscall.LOCK_SH | ||
writeLock lockType = syscall.LOCK_EX | ||
) | ||
|
||
func lock(f File, lt lockType) (err error) { | ||
for { | ||
err = syscall.Flock(int(f.Fd()), int(lt)) | ||
if err != syscall.EINTR { | ||
break | ||
} | ||
} | ||
if err != nil { | ||
return &fs.PathError{ | ||
Op: lt.String(), | ||
Path: f.Name(), | ||
Err: err, | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func unlock(f File) error { | ||
return lock(f, syscall.LOCK_UN) | ||
} | ||
|
||
func isNotSupported(err error) bool { | ||
return err == syscall.ENOSYS || err == syscall.ENOTSUP || err == syscall.EOPNOTSUPP || err == ErrNotSupported | ||
} |