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

Don't use zeropool for postings cache key building #4869

Merged
merged 3 commits into from
Apr 28, 2023
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* [ENHANCEMENT] Distributor: make `__meta_tenant_id` label available in relabeling rules configured via `metric_relabel_configs`. #4725
* [ENHANCEMENT] Querier: reduce CPU utilisation when shuffle sharding is enabled with large shard sizes. #4851
* [ENHANCEMENT] Packaging: facilitate configuration management by instructing systemd to start mimir with a configuration file. #4810
* [ENHANCEMENT] Store-gateway: reduce memory allocations when looking up postings from cache. #4861
* [ENHANCEMENT] Store-gateway: reduce memory allocations when looking up postings from cache. #4861 #4869
* [BUGFIX] Metadata API: Mimir will now return an empty object when no metadata is available, matching Prometheus. #4782
* [BUGFIX] Store-gateway: add collision detection on expanded postings and individual postings cache keys. #4770

Expand Down
19 changes: 12 additions & 7 deletions pkg/storegateway/indexcache/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/base64"
"fmt"
"strconv"
"sync"
"time"
"unsafe"

Expand All @@ -21,7 +22,6 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/util/zeropool"
"golang.org/x/crypto/blake2b"

"github.com/grafana/mimir/pkg/storage/sharding"
Expand All @@ -32,10 +32,10 @@ const (
)

var (
postingsCacheKeyLabelHashBufferPool = zeropool.New[[]byte](func() []byte {
// We assume the label name/value pair is typically not longer than 1KB.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Didn't you like my comment? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I initially changed to return new([]byte) and allocate a max(expectedLen, 1024) in the usage, but that seemed unnecessary complex to me, so I lost the comment when I switched back. Recovered it now :)

return make([]byte, 0, 1024)
})
postingsCacheKeyLabelHashBufferPool = sync.Pool{New: func() any {
b := make([]byte, 1024)
return &b
}}
)

// RemoteIndexCache is a memcached or redis based index cache.
Expand Down Expand Up @@ -183,7 +183,9 @@ func postingsCacheKeyLabelHash(l labels.Label) [blake2b.Size256]byte {
expectedLen := len(l.Name) + len(separator) + len(l.Value)

// Get a buffer from the pool and fill it with the label name/value pair to hash.
buf := postingsCacheKeyLabelHashBufferPool.Get()
bp := postingsCacheKeyLabelHashBufferPool.Get().(*[]byte)
buf := *bp

if cap(buf) < expectedLen {
buf = make([]byte, expectedLen)
} else {
Expand All @@ -203,7 +205,10 @@ func postingsCacheKeyLabelHash(l labels.Label) [blake2b.Size256]byte {
// Use cryptographically hash functions to avoid hash collisions
// which would end up in wrong query results.
hash := blake2b.Sum256(buf)
postingsCacheKeyLabelHashBufferPool.Put(buf)

// Reuse the same pointer to put the buffer back into the pool.
*bp = buf
postingsCacheKeyLabelHashBufferPool.Put(bp)

return hash
}
Expand Down