2017-08-03 17:24:27 +00:00
|
|
|
package inmem
|
2015-04-14 19:04:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2015-04-14 19:04:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type InmemHABackend struct {
|
2017-08-03 17:24:27 +00:00
|
|
|
physical.Backend
|
2016-04-26 03:10:32 +00:00
|
|
|
locks map[string]string
|
2018-02-05 01:37:57 +00:00
|
|
|
l *sync.Mutex
|
2016-04-26 03:10:32 +00:00
|
|
|
cond *sync.Cond
|
2016-08-19 20:45:17 +00:00
|
|
|
logger log.Logger
|
2015-04-14 19:04:15 +00:00
|
|
|
}
|
|
|
|
|
2017-02-17 14:15:35 +00:00
|
|
|
type TransactionalInmemHABackend struct {
|
2017-08-03 17:24:27 +00:00
|
|
|
physical.Transactional
|
2017-02-17 14:15:35 +00:00
|
|
|
InmemHABackend
|
|
|
|
}
|
|
|
|
|
2015-04-14 19:04:15 +00:00
|
|
|
// NewInmemHA constructs a new in-memory HA backend. This is only for testing.
|
2017-08-03 17:24:27 +00:00
|
|
|
func NewInmemHA(_ map[string]string, logger log.Logger) (physical.Backend, error) {
|
|
|
|
be, err := NewInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-14 19:04:15 +00:00
|
|
|
in := &InmemHABackend{
|
2017-08-03 17:24:27 +00:00
|
|
|
Backend: be,
|
2017-02-17 14:15:35 +00:00
|
|
|
locks: make(map[string]string),
|
|
|
|
logger: logger,
|
2018-02-05 01:37:57 +00:00
|
|
|
l: new(sync.Mutex),
|
2017-02-17 14:15:35 +00:00
|
|
|
}
|
2018-02-05 01:37:57 +00:00
|
|
|
in.cond = sync.NewCond(in.l)
|
2017-08-03 17:24:27 +00:00
|
|
|
return in, nil
|
2017-02-17 14:15:35 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
func NewTransactionalInmemHA(_ map[string]string, logger log.Logger) (physical.Backend, error) {
|
|
|
|
transInmem, err := NewTransactionalInmem(nil, logger)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-17 14:15:35 +00:00
|
|
|
inmemHA := InmemHABackend{
|
|
|
|
Backend: transInmem,
|
|
|
|
locks: make(map[string]string),
|
|
|
|
logger: logger,
|
2018-02-05 01:37:57 +00:00
|
|
|
l: new(sync.Mutex),
|
2017-02-17 14:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
in := &TransactionalInmemHABackend{
|
|
|
|
InmemHABackend: inmemHA,
|
2017-08-03 17:24:27 +00:00
|
|
|
Transactional: transInmem.(physical.Transactional),
|
2015-04-14 19:04:15 +00:00
|
|
|
}
|
2018-02-05 01:37:57 +00:00
|
|
|
in.cond = sync.NewCond(in.l)
|
2017-08-03 17:24:27 +00:00
|
|
|
return in, nil
|
2015-04-14 19:04:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LockWith is used for mutual exclusion based on the given key.
|
2017-08-03 17:24:27 +00:00
|
|
|
func (i *InmemHABackend) LockWith(key, value string) (physical.Lock, error) {
|
2015-04-14 23:36:53 +00:00
|
|
|
l := &InmemLock{
|
|
|
|
in: i,
|
|
|
|
key: key,
|
|
|
|
value: value,
|
2015-04-14 19:04:15 +00:00
|
|
|
}
|
2015-04-14 23:36:53 +00:00
|
|
|
return l, nil
|
2015-04-14 19:04:15 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 01:48:22 +00:00
|
|
|
// LockMapSize is used in some tests to determine whether this backend has ever
|
|
|
|
// been used for HA purposes rather than simply for storage
|
|
|
|
func (i *InmemHABackend) LockMapSize() int {
|
|
|
|
return len(i.locks)
|
|
|
|
}
|
|
|
|
|
2016-07-18 17:19:58 +00:00
|
|
|
// HAEnabled indicates whether the HA functionality should be exposed.
|
|
|
|
// Currently always returns true.
|
|
|
|
func (i *InmemHABackend) HAEnabled() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-04-14 19:04:15 +00:00
|
|
|
// InmemLock is an in-memory Lock implementation for the HABackend
|
|
|
|
type InmemLock struct {
|
2015-04-14 23:36:53 +00:00
|
|
|
in *InmemHABackend
|
|
|
|
key string
|
|
|
|
value string
|
2015-04-14 19:04:15 +00:00
|
|
|
|
|
|
|
held bool
|
|
|
|
leaderCh chan struct{}
|
|
|
|
l sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *InmemLock) Lock(stopCh <-chan struct{}) (<-chan struct{}, error) {
|
|
|
|
i.l.Lock()
|
|
|
|
defer i.l.Unlock()
|
|
|
|
if i.held {
|
|
|
|
return nil, fmt.Errorf("lock already held")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt an async acquisition
|
|
|
|
didLock := make(chan struct{})
|
|
|
|
releaseCh := make(chan bool, 1)
|
|
|
|
go func() {
|
2015-04-14 23:36:53 +00:00
|
|
|
// Wait to acquire the lock
|
|
|
|
i.in.l.Lock()
|
|
|
|
_, ok := i.in.locks[i.key]
|
|
|
|
for ok {
|
|
|
|
i.in.cond.Wait()
|
|
|
|
_, ok = i.in.locks[i.key]
|
|
|
|
}
|
|
|
|
i.in.locks[i.key] = i.value
|
|
|
|
i.in.l.Unlock()
|
|
|
|
|
|
|
|
// Signal that lock is held
|
2015-04-14 19:04:15 +00:00
|
|
|
close(didLock)
|
|
|
|
|
|
|
|
// Handle an early abort
|
|
|
|
release := <-releaseCh
|
|
|
|
if release {
|
2015-04-14 23:36:53 +00:00
|
|
|
i.in.l.Lock()
|
|
|
|
delete(i.in.locks, i.key)
|
|
|
|
i.in.l.Unlock()
|
|
|
|
i.in.cond.Broadcast()
|
2015-04-14 19:04:15 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for lock acquisition or shutdown
|
|
|
|
select {
|
|
|
|
case <-didLock:
|
|
|
|
releaseCh <- false
|
|
|
|
case <-stopCh:
|
|
|
|
releaseCh <- true
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the leader channel
|
|
|
|
i.held = true
|
|
|
|
i.leaderCh = make(chan struct{})
|
|
|
|
return i.leaderCh, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *InmemLock) Unlock() error {
|
|
|
|
i.l.Lock()
|
|
|
|
defer i.l.Unlock()
|
|
|
|
|
|
|
|
if !i.held {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
close(i.leaderCh)
|
|
|
|
i.leaderCh = nil
|
|
|
|
i.held = false
|
2015-04-14 23:36:53 +00:00
|
|
|
|
|
|
|
i.in.l.Lock()
|
|
|
|
delete(i.in.locks, i.key)
|
|
|
|
i.in.l.Unlock()
|
|
|
|
i.in.cond.Broadcast()
|
2015-04-14 19:04:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-04-14 23:36:53 +00:00
|
|
|
|
|
|
|
func (i *InmemLock) Value() (bool, string, error) {
|
|
|
|
i.in.l.Lock()
|
|
|
|
val, ok := i.in.locks[i.key]
|
|
|
|
i.in.l.Unlock()
|
|
|
|
return ok, val, nil
|
|
|
|
}
|