Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed crash in the memcached servers selector when there's only 1 server #1975

Merged
merged 3 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion pkg/cacheutil/memcached_server_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
48 changes: 48 additions & 0 deletions pkg/cacheutil/memcached_server_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.NotOk(t, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We forgot to check that err is equal to test.expectedErr here :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for noticing it! Fixed

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)()

Expand Down