Skip to content

Commit

Permalink
When we share a cache amongst multiple stores, ensure it is only stop…
Browse files Browse the repository at this point in the history
…ped once.

Signed-off-by: Tom Wilkie <tom.wilkie@gmail.com>
  • Loading branch information
tomwilkie committed Nov 19, 2018
1 parent b095fdc commit ff29d37
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
23 changes: 23 additions & 0 deletions cache/stop_once.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cache

import "sync"

type stopOnce struct {
once sync.Once
Cache
}

// StopOnce wraps a Cache and ensures its only stopped once.
func StopOnce(cache Cache) Cache {
return &stopOnce{
Cache: cache,
}
}

func (s *stopOnce) Stop() error {
var err error
s.once.Do(func() {
err = s.Cache.Stop()
})
return err
}
6 changes: 5 additions & 1 deletion storage/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func (cfg *Config) RegisterFlags(f *flag.FlagSet) {

// NewStore makes the storage clients based on the configuration.
func NewStore(cfg Config, storeCfg chunk.StoreConfig, schemaCfg chunk.SchemaConfig, limits *validation.Overrides) (chunk.Store, error) {
var tieredCache cache.Cache
var err error

// Building up from deprecated flags.
Expand All @@ -67,6 +66,7 @@ func NewStore(cfg Config, storeCfg chunk.StoreConfig, schemaCfg chunk.SchemaConf
}, memcache))
}

var tieredCache cache.Cache
if len(caches) > 0 {
tieredCache = cache.NewTiered(caches)
cfg.indexQueriesCacheConfig.DefaultValidity = cfg.IndexCacheValidity
Expand All @@ -77,6 +77,10 @@ func NewStore(cfg Config, storeCfg chunk.StoreConfig, schemaCfg chunk.SchemaConf
}
}

// Cache is shared by multiple stores, which means they will try and Stop
// it more than once. Wrap in a StopOnce to prevent this.
tieredCache = cache.StopOnce(tieredCache)

err = schemaCfg.Load()
if err != nil {
return nil, errors.Wrap(err, "error loading schema config")
Expand Down

0 comments on commit ff29d37

Please sign in to comment.