2020-10-19 23:32:39 +00:00
|
|
|
package submatview
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-02-23 20:28:43 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
|
2020-10-19 23:32:39 +00:00
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
|
|
|
"github.com/hashicorp/consul/lib/ttlcache"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Store struct {
|
2021-02-23 20:28:43 +00:00
|
|
|
logger hclog.Logger
|
2020-10-19 23:32:39 +00:00
|
|
|
lock sync.RWMutex
|
2021-02-22 19:29:45 +00:00
|
|
|
byKey map[string]entry
|
2020-10-19 23:32:39 +00:00
|
|
|
expiryHeap *ttlcache.ExpiryHeap
|
2021-04-22 18:08:09 +00:00
|
|
|
|
|
|
|
// idleTTL is the duration of time an entry should remain in the Store after the
|
|
|
|
// last request for that entry has been terminated. It is a field on the struct
|
|
|
|
// so that it can be patched in tests without need a lock.
|
|
|
|
idleTTL time.Duration
|
2020-10-19 23:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 19:29:45 +00:00
|
|
|
type entry struct {
|
|
|
|
materializer *Materializer
|
|
|
|
expiry *ttlcache.Entry
|
2021-02-22 21:27:18 +00:00
|
|
|
stop func()
|
2021-02-23 17:18:44 +00:00
|
|
|
// requests is the count of active requests using this entry. This entry will
|
2021-02-22 22:43:52 +00:00
|
|
|
// remain in the store as long as this count remains > 0.
|
2021-02-23 17:18:44 +00:00
|
|
|
requests int
|
2021-02-22 19:29:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 20:28:43 +00:00
|
|
|
func NewStore(logger hclog.Logger) *Store {
|
2020-10-19 23:32:39 +00:00
|
|
|
return &Store{
|
2021-02-23 20:28:43 +00:00
|
|
|
logger: logger,
|
2021-02-22 19:29:45 +00:00
|
|
|
byKey: make(map[string]entry),
|
2020-10-19 23:32:39 +00:00
|
|
|
expiryHeap: ttlcache.NewExpiryHeap(),
|
2021-04-22 18:08:09 +00:00
|
|
|
idleTTL: 20 * time.Minute,
|
2020-10-19 23:32:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 21:27:18 +00:00
|
|
|
// Run the expiration loop until the context is cancelled.
|
|
|
|
func (s *Store) Run(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
s.lock.RLock()
|
|
|
|
timer := s.expiryHeap.Next()
|
|
|
|
s.lock.RUnlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
timer.Stop()
|
|
|
|
return
|
|
|
|
case <-s.expiryHeap.NotifyCh:
|
|
|
|
timer.Stop()
|
|
|
|
continue
|
|
|
|
|
|
|
|
case <-timer.Wait():
|
|
|
|
s.lock.Lock()
|
|
|
|
|
|
|
|
he := timer.Entry
|
|
|
|
s.expiryHeap.Remove(he.Index())
|
|
|
|
|
|
|
|
e := s.byKey[he.Key()]
|
2021-02-22 22:43:52 +00:00
|
|
|
|
2021-02-23 17:18:44 +00:00
|
|
|
// Only stop the materializer if there are no active requests.
|
|
|
|
if e.requests == 0 {
|
2021-02-22 22:43:52 +00:00
|
|
|
e.stop()
|
|
|
|
delete(s.byKey, he.Key())
|
|
|
|
}
|
2021-02-22 21:27:18 +00:00
|
|
|
|
|
|
|
s.lock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: godoc
|
2021-02-23 17:18:44 +00:00
|
|
|
type Request interface {
|
|
|
|
cache.Request
|
2021-04-19 22:47:07 +00:00
|
|
|
NewMaterializer() (*Materializer, error)
|
2021-02-23 17:18:44 +00:00
|
|
|
Type() string
|
|
|
|
}
|
2021-02-22 19:29:45 +00:00
|
|
|
|
2020-10-19 23:32:39 +00:00
|
|
|
// Get a value from the store, blocking if the store has not yet seen the
|
|
|
|
// req.Index value.
|
|
|
|
// See agent/cache.Cache.Get for complete documentation.
|
2021-02-23 20:02:28 +00:00
|
|
|
func (s *Store) Get(ctx context.Context, req Request) (Result, error) {
|
2021-02-22 19:29:45 +00:00
|
|
|
info := req.CacheInfo()
|
2021-04-19 22:47:07 +00:00
|
|
|
key, e, err := s.getEntry(req)
|
|
|
|
if err != nil {
|
|
|
|
return Result{}, err
|
|
|
|
}
|
2021-02-23 17:18:44 +00:00
|
|
|
defer s.releaseEntry(key)
|
2021-02-22 19:29:45 +00:00
|
|
|
|
2021-02-23 19:27:24 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, info.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
result, err := e.materializer.getFromView(ctx, info.MinIndex)
|
|
|
|
|
|
|
|
// TODO: does context.DeadlineExceeded need to be translated into a nil error
|
|
|
|
// to match the old interface?
|
2021-02-23 17:18:44 +00:00
|
|
|
|
2021-02-23 20:02:28 +00:00
|
|
|
return result, err
|
2020-10-19 23:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify the updateCh when there are updates to the entry identified by req.
|
|
|
|
// See agent/cache.Cache.Notify for complete documentation.
|
2021-02-23 19:27:24 +00:00
|
|
|
//
|
|
|
|
// Request.CacheInfo().Timeout is ignored because it is not really relevant in
|
|
|
|
// this case. Instead set a deadline on the context.
|
2020-10-19 23:32:39 +00:00
|
|
|
func (s *Store) Notify(
|
|
|
|
ctx context.Context,
|
2021-02-22 19:29:45 +00:00
|
|
|
req Request,
|
2020-10-19 23:32:39 +00:00
|
|
|
correlationID string,
|
|
|
|
updateCh chan<- cache.UpdateEvent,
|
|
|
|
) error {
|
2021-02-22 19:29:45 +00:00
|
|
|
info := req.CacheInfo()
|
2021-04-19 22:47:07 +00:00
|
|
|
key, e, err := s.getEntry(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-22 19:29:45 +00:00
|
|
|
|
|
|
|
go func() {
|
2021-02-23 17:18:44 +00:00
|
|
|
defer s.releaseEntry(key)
|
2021-02-22 22:43:52 +00:00
|
|
|
|
2021-02-23 17:18:44 +00:00
|
|
|
index := info.MinIndex
|
2021-02-22 19:29:45 +00:00
|
|
|
for {
|
2021-02-23 19:27:24 +00:00
|
|
|
result, err := e.materializer.getFromView(ctx, index)
|
2021-02-22 19:29:45 +00:00
|
|
|
switch {
|
|
|
|
case ctx.Err() != nil:
|
|
|
|
return
|
|
|
|
case err != nil:
|
|
|
|
// TODO: cache.Notify sends errors on updateCh, should this do the same?
|
2021-02-23 20:28:43 +00:00
|
|
|
// It seems like only fetch errors would ever get sent along and eventually
|
|
|
|
// logged, so sending may not provide any benefit here.
|
|
|
|
|
|
|
|
s.logger.Warn("handling error in Store.Notify",
|
|
|
|
"error", err,
|
|
|
|
"request-type", req.Type(),
|
|
|
|
"index", index)
|
2021-02-22 19:29:45 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
index = result.Index
|
|
|
|
u := cache.UpdateEvent{
|
|
|
|
CorrelationID: correlationID,
|
|
|
|
Result: result.Value,
|
|
|
|
Meta: cache.ResultMeta{Index: result.Index},
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case updateCh <- u:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-10-19 23:32:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 17:18:44 +00:00
|
|
|
// getEntry from the store, and increment the requests counter. releaseEntry
|
|
|
|
// must be called when the request is finished to decrement the counter.
|
2021-04-19 22:47:07 +00:00
|
|
|
func (s *Store) getEntry(req Request) (string, entry, error) {
|
2021-02-23 17:18:44 +00:00
|
|
|
info := req.CacheInfo()
|
|
|
|
key := makeEntryKey(req.Type(), info)
|
2020-10-19 23:32:39 +00:00
|
|
|
|
|
|
|
s.lock.Lock()
|
2021-02-22 19:29:45 +00:00
|
|
|
defer s.lock.Unlock()
|
2021-02-22 22:43:52 +00:00
|
|
|
e, ok := s.byKey[key]
|
2021-02-22 19:29:45 +00:00
|
|
|
if ok {
|
2021-02-23 17:18:44 +00:00
|
|
|
e.requests++
|
|
|
|
s.byKey[key] = e
|
2021-04-19 22:47:07 +00:00
|
|
|
return key, e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mat, err := req.NewMaterializer()
|
|
|
|
if err != nil {
|
|
|
|
return "", e, err
|
2020-10-19 23:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 21:27:18 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2021-02-22 22:43:52 +00:00
|
|
|
go mat.Run(ctx)
|
2021-02-22 21:27:18 +00:00
|
|
|
|
2021-02-22 22:43:52 +00:00
|
|
|
e = entry{
|
|
|
|
materializer: mat,
|
|
|
|
stop: cancel,
|
2021-02-23 17:18:44 +00:00
|
|
|
requests: 1,
|
2021-02-22 22:43:52 +00:00
|
|
|
}
|
2021-02-22 19:29:45 +00:00
|
|
|
s.byKey[key] = e
|
2021-04-19 22:47:07 +00:00
|
|
|
return key, e, nil
|
2020-10-19 23:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 17:18:44 +00:00
|
|
|
// releaseEntry decrements the request count and starts an expiry timer if the
|
|
|
|
// count has reached 0. Must be called once for every call to getEntry.
|
|
|
|
func (s *Store) releaseEntry(key string) {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
e := s.byKey[key]
|
|
|
|
e.requests--
|
|
|
|
s.byKey[key] = e
|
|
|
|
|
|
|
|
if e.requests > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.expiry.Index() == ttlcache.NotIndexed {
|
2021-04-22 18:08:09 +00:00
|
|
|
e.expiry = s.expiryHeap.Add(key, s.idleTTL)
|
2021-02-23 17:18:44 +00:00
|
|
|
s.byKey[key] = e
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-22 18:08:09 +00:00
|
|
|
s.expiryHeap.Update(e.expiry.Index(), s.idleTTL)
|
2020-10-19 23:32:39 +00:00
|
|
|
}
|
2021-02-22 22:43:52 +00:00
|
|
|
|
2021-02-23 17:18:44 +00:00
|
|
|
// makeEntryKey matches agent/cache.makeEntryKey, but may change in the future.
|
|
|
|
func makeEntryKey(typ string, r cache.RequestInfo) string {
|
|
|
|
return fmt.Sprintf("%s/%s/%s/%s", typ, r.Datacenter, r.Token, r.Key)
|
2021-02-22 22:43:52 +00:00
|
|
|
}
|