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

Index Cache: Change cache key for postings #6405

Merged
merged 9 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -53,6 +53,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
- [#6322](https://github.com/thanos-io/thanos/pull/6322) Logging: Avoid expensive log.Valuer evaluation for disallowed levels.
- [#6358](https://github.com/thanos-io/thanos/pull/6358) Query: Add +Inf bucket to query duration metrics
- [#6363](https://github.com/thanos-io/thanos/pull/6363) Store: Check context error when expanding postings.
- [#6405](https://github.com/thanos-io/thanos/pull/6405) Index Cache: Change postings cache key due to the streaming snappy encoding change.
yeya24 marked this conversation as resolved.
Show resolved Hide resolved

### Removed

Expand Down
1 change: 1 addition & 0 deletions pkg/store/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (c cacheKey) string() string {
// which would end up in wrong query results.
lbl := c.key.(cacheKeyPostings)
lblHash := blake2b.Sum256([]byte(lbl.Name + ":" + lbl.Value))
// Add : at the end to force using a new cache key for postings.
yeya24 marked this conversation as resolved.
Show resolved Hide resolved
return "P:" + c.block + ":" + base64.RawURLEncoding.EncodeToString(lblHash[0:])
case cacheKeySeries:
return "S:" + c.block + ":" + strconv.FormatUint(uint64(c.key.(cacheKeySeries)), 10)
Expand Down
20 changes: 17 additions & 3 deletions pkg/store/cache/memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ const (
memcachedDefaultTTL = 24 * time.Hour
)

const (
compressionSchemeStreamedSnappy = "dvs"
)

// RemoteIndexCache is a memcached-based index cache.
type RemoteIndexCache struct {
logger log.Logger
memcached cacheutil.RemoteCacheClient

compressionScheme string

// Metrics.
postingRequests prometheus.Counter
seriesRequests prometheus.Counter
Expand All @@ -37,8 +43,9 @@ type RemoteIndexCache struct {
// NewRemoteIndexCache makes a new RemoteIndexCache.
func NewRemoteIndexCache(logger log.Logger, cacheClient cacheutil.RemoteCacheClient, reg prometheus.Registerer) (*RemoteIndexCache, error) {
c := &RemoteIndexCache{
logger: logger,
memcached: cacheClient,
logger: logger,
memcached: cacheClient,
compressionScheme: compressionSchemeStreamedSnappy, // Hardcode it for now. Expose it once we supporter different types of compressions.
yeya24 marked this conversation as resolved.
Show resolved Hide resolved
}

requests := promauto.With(reg).NewCounterVec(prometheus.CounterOpts{
Expand All @@ -65,6 +72,9 @@ func NewRemoteIndexCache(logger log.Logger, cacheClient cacheutil.RemoteCacheCli
// asynchronously stored in the cache.
func (c *RemoteIndexCache) StorePostings(blockID ulid.ULID, l labels.Label, v []byte) {
key := cacheKey{blockID.String(), cacheKeyPostings(l)}.string()
yeya24 marked this conversation as resolved.
Show resolved Hide resolved
if len(c.compressionScheme) > 0 {
key += ":" + c.compressionScheme
}

if err := c.memcached.SetAsync(key, v, memcachedDefaultTTL); err != nil {
level.Error(c.logger).Log("msg", "failed to cache postings in memcached", "err", err)
Expand All @@ -78,8 +88,12 @@ func (c *RemoteIndexCache) FetchMultiPostings(ctx context.Context, blockID ulid.
keys := make([]string, 0, len(lbls))

blockIDKey := blockID.String()
suffix := ""
if len(c.compressionScheme) > 0 {
suffix += ":" + c.compressionScheme
}
for _, lbl := range lbls {
key := cacheKey{blockIDKey, cacheKeyPostings(lbl)}.string()
key := cacheKey{blockIDKey, cacheKeyPostings(lbl)}.string() + suffix
keys = append(keys, key)
}

Expand Down