restic/src/restic/lock.go

305 lines
7.2 KiB
Go
Raw Normal View History

2015-06-24 16:17:01 +00:00
package restic
import (
"fmt"
2015-06-24 16:17:01 +00:00
"os"
"os/signal"
"os/user"
"sync"
"syscall"
2016-09-01 19:13:06 +00:00
"testing"
2015-06-24 16:17:01 +00:00
"time"
2016-09-01 20:17:37 +00:00
"restic/errors"
2016-08-29 19:38:34 +00:00
"restic/debug"
2015-06-24 16:17:01 +00:00
)
// Lock represents a process locking the repository for an operation.
//
// There are two types of locks: exclusive and non-exclusive. There may be many
// different non-exclusive locks, but at most one exclusive lock, which can
// only be acquired while no non-exclusive lock is held.
2015-07-12 19:02:00 +00:00
//
// A lock must be refreshed regularly to not be considered stale, this must be
// triggered by regularly calling Refresh.
2015-06-24 16:17:01 +00:00
type Lock struct {
Time time.Time `json:"time"`
Exclusive bool `json:"exclusive"`
Hostname string `json:"hostname"`
Username string `json:"username"`
PID int `json:"pid"`
UID uint32 `json:"uid,omitempty"`
GID uint32 `json:"gid,omitempty"`
2016-08-31 18:29:54 +00:00
repo Repository
lockID *ID
2015-06-24 16:17:01 +00:00
}
// ErrAlreadyLocked is returned when NewLock or NewExclusiveLock are unable to
// acquire the desired lock.
type ErrAlreadyLocked struct {
otherLock *Lock
}
func (e ErrAlreadyLocked) Error() string {
return fmt.Sprintf("repository is already locked by %v", e.otherLock)
}
// IsAlreadyLocked returns true iff err is an instance of ErrAlreadyLocked.
func IsAlreadyLocked(err error) bool {
2016-08-29 19:38:34 +00:00
if _, ok := errors.Cause(err).(ErrAlreadyLocked); ok {
return true
}
return false
}
2015-06-24 16:17:01 +00:00
// NewLock returns a new, non-exclusive lock for the repository. If an
// exclusive lock is already held by another process, ErrAlreadyLocked is
// returned.
2016-08-31 18:29:54 +00:00
func NewLock(repo Repository) (*Lock, error) {
2015-06-24 16:17:01 +00:00
return newLock(repo, false)
}
// NewExclusiveLock returns a new, exclusive lock for the repository. If
// another lock (normal and exclusive) is already held by another process,
// ErrAlreadyLocked is returned.
2016-08-31 18:29:54 +00:00
func NewExclusiveLock(repo Repository) (*Lock, error) {
2015-06-24 16:17:01 +00:00
return newLock(repo, true)
}
2016-09-01 19:13:06 +00:00
var waitBeforeLockCheck = 200 * time.Millisecond
// TestSetLockTimeout can be used to reduce the lock wait timeout for tests.
func TestSetLockTimeout(t testing.TB, d time.Duration) {
t.Logf("setting lock timeout to %v", d)
waitBeforeLockCheck = d
}
2015-06-27 12:26:33 +00:00
2016-08-31 18:29:54 +00:00
func newLock(repo Repository, excl bool) (*Lock, error) {
2015-06-24 16:17:01 +00:00
lock := &Lock{
Time: time.Now(),
PID: os.Getpid(),
Exclusive: excl,
repo: repo,
}
hn, err := os.Hostname()
if err == nil {
lock.Hostname = hn
}
if err = lock.fillUserInfo(); err != nil {
return nil, err
}
if err = lock.checkForOtherLocks(); err != nil {
return nil, err
}
lockID, err := lock.createLock()
2015-06-24 16:17:01 +00:00
if err != nil {
return nil, err
}
lock.lockID = &lockID
2015-06-27 12:26:33 +00:00
time.Sleep(waitBeforeLockCheck)
if err = lock.checkForOtherLocks(); err != nil {
lock.Unlock()
return nil, err
2015-06-27 12:26:33 +00:00
}
2015-06-24 16:17:01 +00:00
return lock, nil
}
func (l *Lock) fillUserInfo() error {
usr, err := user.Current()
if err != nil {
return nil
}
l.Username = usr.Username
l.UID, l.GID, err = uidGidInt(*usr)
return err
2015-06-24 16:17:01 +00:00
}
// checkForOtherLocks looks for other locks that currently exist in the repository.
//
// If an exclusive lock is to be created, checkForOtherLocks returns an error
// if there are any other locks, regardless if exclusive or not. If a
// non-exclusive lock is to be created, an error is only returned when an
// exclusive lock is found.
func (l *Lock) checkForOtherLocks() error {
2016-08-31 18:29:54 +00:00
return eachLock(l.repo, func(id ID, lock *Lock, err error) error {
if l.lockID != nil && id.Equal(*l.lockID) {
2015-06-27 12:26:33 +00:00
return nil
}
2015-06-24 16:17:01 +00:00
// ignore locks that cannot be loaded
if err != nil {
return nil
}
if l.Exclusive {
return ErrAlreadyLocked{otherLock: lock}
2015-06-24 16:17:01 +00:00
}
if !l.Exclusive && lock.Exclusive {
return ErrAlreadyLocked{otherLock: lock}
2015-06-24 16:17:01 +00:00
}
return nil
})
}
2016-08-31 18:29:54 +00:00
func eachLock(repo Repository, f func(ID, *Lock, error) error) error {
2015-06-24 16:17:01 +00:00
done := make(chan struct{})
defer close(done)
2016-08-31 18:29:54 +00:00
for id := range repo.List(LockFile, done) {
2015-06-24 16:17:01 +00:00
lock, err := LoadLock(repo, id)
err = f(id, lock, err)
if err != nil {
return err
}
}
return nil
}
// createLock acquires the lock by creating a file in the repository.
2016-08-31 18:29:54 +00:00
func (l *Lock) createLock() (ID, error) {
id, err := l.repo.SaveJSONUnpacked(LockFile, l)
2015-06-24 16:17:01 +00:00
if err != nil {
2016-08-31 18:29:54 +00:00
return ID{}, err
2015-06-24 16:17:01 +00:00
}
2015-07-12 19:02:00 +00:00
return id, nil
2015-06-24 16:17:01 +00:00
}
// Unlock removes the lock from the repository.
func (l *Lock) Unlock() error {
if l == nil || l.lockID == nil {
return nil
}
2016-08-31 18:29:54 +00:00
return l.repo.Backend().Remove(LockFile, l.lockID.String())
2015-06-24 16:17:01 +00:00
}
var staleTimeout = 30 * time.Minute
// Stale returns true if the lock is stale. A lock is stale if the timestamp is
// older than 30 minutes or if it was created on the current machine and the
// process isn't alive any more.
func (l *Lock) Stale() bool {
2016-08-16 19:59:43 +00:00
debug.Log("Lock.Stale", "testing if lock %v for process %d is stale", l, l.PID)
2016-08-29 19:38:34 +00:00
if time.Since(l.Time) > staleTimeout {
2015-06-24 16:17:01 +00:00
debug.Log("Lock.Stale", "lock is stale, timestamp is too old: %v\n", l.Time)
return true
}
hn, err := os.Hostname()
if err != nil {
debug.Log("Lock.Stale", "unable to find current hostnanme: %v", err)
// since we cannot find the current hostname, assume that the lock is
// not stale.
return false
}
if hn != l.Hostname {
// lock was created on a different host, assume the lock is not stale.
return false
}
// check if we can reach the process retaining the lock
exists := l.processExists()
if !exists {
debug.Log("Lock.Stale", "could not reach process, %d, lock is probably stale\n", l.PID)
2015-06-24 16:17:01 +00:00
return true
}
debug.Log("Lock.Stale", "lock not stale\n")
return false
}
2015-07-12 19:02:00 +00:00
// Refresh refreshes the lock by creating a new file in the backend with a new
// timestamp. Afterwards the old lock is removed.
func (l *Lock) Refresh() error {
2015-07-12 20:10:01 +00:00
debug.Log("Lock.Refresh", "refreshing lock %v", l.lockID.Str())
2015-07-12 19:02:00 +00:00
id, err := l.createLock()
if err != nil {
return err
}
2016-08-31 18:29:54 +00:00
err = l.repo.Backend().Remove(LockFile, l.lockID.String())
2015-07-12 19:02:00 +00:00
if err != nil {
return err
}
2015-07-12 20:10:01 +00:00
debug.Log("Lock.Refresh", "new lock ID %v", id.Str())
l.lockID = &id
2015-07-12 19:02:00 +00:00
return nil
}
func (l Lock) String() string {
text := fmt.Sprintf("PID %d on %s by %s (UID %d, GID %d)\nlock was created at %s (%s ago)\nstorage ID %v",
l.PID, l.Hostname, l.Username, l.UID, l.GID,
l.Time.Format("2006-01-02 15:04:05"), time.Since(l.Time),
l.lockID.Str())
return text
}
2015-06-24 16:17:01 +00:00
// listen for incoming SIGHUP and ignore
var ignoreSIGHUP sync.Once
func init() {
ignoreSIGHUP.Do(func() {
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGHUP)
for s := range c {
debug.Log("lock.ignoreSIGHUP", "Signal received: %v\n", s)
}
}()
})
}
// LoadLock loads and unserializes a lock from a repository.
2016-08-31 18:29:54 +00:00
func LoadLock(repo Repository, id ID) (*Lock, error) {
2015-06-24 16:17:01 +00:00
lock := &Lock{}
2016-08-31 18:29:54 +00:00
if err := repo.LoadJSONUnpacked(LockFile, id, lock); err != nil {
2015-06-24 16:17:01 +00:00
return nil, err
}
lock.lockID = &id
2015-06-24 16:17:01 +00:00
return lock, nil
}
// RemoveStaleLocks deletes all locks detected as stale from the repository.
2016-08-31 18:29:54 +00:00
func RemoveStaleLocks(repo Repository) error {
return eachLock(repo, func(id ID, lock *Lock, err error) error {
2015-06-24 16:17:01 +00:00
// ignore locks that cannot be loaded
if err != nil {
return nil
}
if lock.Stale() {
2016-08-31 18:29:54 +00:00
return repo.Backend().Remove(LockFile, id.String())
2015-06-24 16:17:01 +00:00
}
return nil
})
}
2015-07-12 19:02:00 +00:00
// RemoveAllLocks removes all locks forcefully.
2016-08-31 18:29:54 +00:00
func RemoveAllLocks(repo Repository) error {
return eachLock(repo, func(id ID, lock *Lock, err error) error {
return repo.Backend().Remove(LockFile, id.String())
})
}