diff --git a/CHANGELOG.md b/CHANGELOG.md index d915df7e9c..34af658e1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ We use *breaking* word for marking changes that are not backward compatible (rel - [#1969](https://github.com/thanos-io/thanos/pull/1969) Sidecar: allow setting http connection pool size via flags - [#1967](https://github.com/thanos-io/thanos/issues/1967) Receive: Allow local TSDB compaction +- [#1975](https://github.com/thanos-io/thanos/pull/1975) Store Gateway: fixed panic caused by memcached servers selector when there's 1 memcached node ### Fixed diff --git a/pkg/cacheutil/memcached_server_selector.go b/pkg/cacheutil/memcached_server_selector.go index 1318574045..37b693367c 100644 --- a/pkg/cacheutil/memcached_server_selector.go +++ b/pkg/cacheutil/memcached_server_selector.go @@ -74,9 +74,12 @@ func (s *MemcachedJumpHashSelector) PickServer(key string) (net.Addr, error) { return nil, memcache.ErrNoServers } if len(addrs) == 1 { + picked := addrs[0] + addrs = (addrs)[:0] addrsPool.Put(&addrs) - return (addrs)[0], nil + + return picked, nil } // Pick a server using the jump hash. diff --git a/pkg/cacheutil/memcached_server_selector_test.go b/pkg/cacheutil/memcached_server_selector_test.go index a215604ece..466db1c401 100644 --- a/pkg/cacheutil/memcached_server_selector_test.go +++ b/pkg/cacheutil/memcached_server_selector_test.go @@ -37,6 +37,54 @@ func TestNatSort(t *testing.T) { testutil.Equals(t, expected, input) } +func TestMemcachedJumpHashSelector_PickServer(t *testing.T) { + defer leaktest.CheckTimeout(t, 10*time.Second)() + + tests := []struct { + addrs []string + key string + expectedAddr string + expectedErr error + }{ + { + addrs: []string{}, + key: "test-1", + expectedErr: memcache.ErrNoServers, + }, + { + addrs: []string{"127.0.0.1:11211"}, + key: "test-1", + expectedAddr: "127.0.0.1:11211", + }, + { + addrs: []string{"127.0.0.1:11211", "127.0.0.2:11211"}, + key: "test-1", + expectedAddr: "127.0.0.1:11211", + }, + { + addrs: []string{"127.0.0.1:11211", "127.0.0.2:11211"}, + key: "test-2", + expectedAddr: "127.0.0.2:11211", + }, + } + + s := MemcachedJumpHashSelector{} + + for _, test := range tests { + testutil.Ok(t, s.SetServers(test.addrs...)) + + actualAddr, err := s.PickServer(test.key) + + if test.expectedErr != nil { + testutil.Equals(t, test.expectedErr, err) + testutil.Equals(t, nil, actualAddr) + } else { + testutil.Ok(t, err) + testutil.Equals(t, test.expectedAddr, actualAddr.String()) + } + } +} + func TestMemcachedJumpHashSelector_Each_ShouldRespectServersOrdering(t *testing.T) { defer leaktest.CheckTimeout(t, 10*time.Second)()