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

[WIP] Index cache clients : Dedup Index Cache requests #7236

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 13 additions & 13 deletions pkg/cacheutil/memcached_client.go
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/thanos-io/thanos/pkg/extprom"
"github.com/thanos-io/thanos/pkg/gate"
"github.com/thanos-io/thanos/pkg/model"
"golang.org/x/sync/singleflight"
)

const (
Expand Down Expand Up @@ -61,8 +62,9 @@ var (
)

var (
_ RemoteCacheClient = (*memcachedClient)(nil)
_ RemoteCacheClient = (*RedisClient)(nil)
_ RemoteCacheClient = (*memcachedClient)(nil)
_ RemoteCacheClient = (*RedisClient)(nil)
sf singleflight.Group
)

// RemoteCacheClient is a high level client to interact with remote cache.
Expand Down Expand Up @@ -433,22 +435,20 @@ func (c *memcachedClient) GetMulti(ctx context.Context, keys []string) map[strin
return nil
}

batches, err := c.getMultiBatched(ctx, keys)
// Use singleflight to cache the result of fetching multiple keys.
result, err, _ := sf.Do(strings.Join(keys, ","), func() (interface{}, error) {
return c.getMultiBatched(ctx, keys)
})
if err != nil {
level.Warn(c.logger).Log("msg", "failed to fetch items from memcached", "numKeys", len(keys), "firstKey", keys[0], "err", err)

// In case we have both results and an error, it means some batch requests
// failed and other succeeded. In this case we prefer to log it and move on,
// given returning some results from the cache is better than returning
// nothing.
if len(batches) == 0 {
return nil
}
return nil
}

items := result.([]map[string]*memcache.Item)

hits := map[string][]byte{}
for _, items := range batches {
for key, item := range items {
for _, batch := range items {
for key, item := range batch {
hits[key] = item.Value
}
}
Expand Down