Skip to content

Commit

Permalink
Not allow duplicated backends
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Protasio <alanprot@gmail.com>
  • Loading branch information
alanprot committed Jul 12, 2023
1 parent e0fba4f commit 12acde1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
19 changes: 14 additions & 5 deletions pkg/storage/tsdb/index_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
supportedIndexCacheBackends = []string{IndexCacheBackendInMemory, IndexCacheBackendMemcached, IndexCacheBackendRedis}

errUnsupportedIndexCacheBackend = errors.New("unsupported index cache backend")
errDuplicatedIndexCacheBackend = errors.New("duplicated index cache backend")
errNoIndexCacheAddresses = errors.New("no index cache backend addresses")
)

Expand All @@ -51,10 +52,10 @@ func (cfg *IndexCacheConfig) RegisterFlags(f *flag.FlagSet) {
}

func (cfg *IndexCacheConfig) RegisterFlagsWithPrefix(f *flag.FlagSet, prefix string) {
f.StringVar(&cfg.Backend, prefix+"backend", IndexCacheBackendDefault, fmt.Sprintf("The index cache backend type. " +
"Multiple cache backend can be provided as a comma-separated ordered list to enable the implementation of a cache hierarchy. " +
f.StringVar(&cfg.Backend, prefix+"backend", IndexCacheBackendDefault, fmt.Sprintf("The index cache backend type. "+
"Multiple cache backend can be provided as a comma-separated ordered list to enable the implementation of a cache hierarchy. "+
"Supported values: %s.",
strings.Join(supportedIndexCacheBackends, ", ")))
strings.Join(supportedIndexCacheBackends, ", ")))

cfg.InMemory.RegisterFlagsWithPrefix(f, prefix+"inmemory.")
cfg.Memcached.RegisterFlagsWithPrefix(f, prefix+"memcached.")
Expand All @@ -64,13 +65,19 @@ func (cfg *IndexCacheConfig) RegisterFlagsWithPrefix(f *flag.FlagSet, prefix str
// Validate the config.
func (cfg *IndexCacheConfig) Validate() error {

splitedBackends := strings.Split(cfg.Backend, ",")
splitBackends := strings.Split(cfg.Backend, ",")
configuredBackends := map[string]struct{}{}

for _, backend := range splitedBackends {
for _, backend := range splitBackends {
if !util.StringsContain(supportedIndexCacheBackends, backend) {
return errUnsupportedIndexCacheBackend
}

if _, ok := configuredBackends[backend]; ok {
return errors.WithMessagef(errDuplicatedIndexCacheBackend, "duplicated backend: %v", backend)
}


if backend == IndexCacheBackendMemcached {
if err := cfg.Memcached.Validate(); err != nil {
return err
Expand All @@ -80,6 +87,8 @@ func (cfg *IndexCacheConfig) Validate() error {
return err
}
}

configuredBackends[backend] = struct{}{}
}

return nil
Expand Down
19 changes: 16 additions & 3 deletions pkg/storage/tsdb/multilevel_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func Test_MultiIndexCacheInstantiation(t *testing.T) {
testCases := map[string]struct {
cfg IndexCacheConfig
expectedType storecache.IndexCache
expectedValidationError error
}{
"instantiate single backends": {
cfg: IndexCacheConfig{
Expand All @@ -41,13 +42,25 @@ func Test_MultiIndexCacheInstantiation(t *testing.T) {
},
expectedType: newMultiLevelCache(),
},
"should not allow duplicate backends": {
cfg: IndexCacheConfig{
Backend: "inmemory,inmemory",
},
expectedType: &storecache.InMemoryIndexCache{},
expectedValidationError: errDuplicatedIndexCacheBackend,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
reg := prometheus.NewRegistry()
c, err := NewIndexCache(tc.cfg, log.NewNopLogger(), reg)
require.NoError(t, err)
require.IsType(t, tc.expectedType, c)
if tc.expectedValidationError == nil {
require.NoError(t, tc.cfg.Validate())
c, err := NewIndexCache(tc.cfg, log.NewNopLogger(), reg)
require.NoError(t, err)
require.IsType(t, tc.expectedType, c)
} else {
require.ErrorIs(t, tc.cfg.Validate(), errDuplicatedIndexCacheBackend)
}
})
}
}
Expand Down

0 comments on commit 12acde1

Please sign in to comment.