Optimize sort keys by server in memcache client #8026
+125
−79
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes
When fetching a lot of keys from index cache using memcached, I see in our store gateway 13% CPU spent on
MemcachedJumpHashSelector.PickServer
. Among them most of the CPU spent onEach
which is the function to get all the memcached server addresses.For each key we call
PickServer
once and eachPickServer
call requires getting all memcached server addresses by callingEach
function. https://github.com/thanos-io/thanos/blob/main/pkg/cacheutil/memcached_server_selector.go#L65This is unnecessary because memcache server addresses update infrequently. When there are a lot of keys to fetch through memcached, it can be expensive to call
Each
every time.This PR I try to cache server addresses in
SetServers
so that we don't have to callEach
to get addresses every time. Instead we callEach
once in thesortKeysByServer
method to get all addresses and do jump hash using the fetched addresses to help reduce lock contention.Verification