Skip to content

Commit

Permalink
bloom client records cache locality score
Browse files Browse the repository at this point in the history
Signed-off-by: Owen Diehl <[email protected]>
  • Loading branch information
owen-d committed Apr 3, 2024
1 parent d39dc09 commit 24ef2cb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
33 changes: 22 additions & 11 deletions pkg/bloomgateway/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,12 @@ type Client interface {
}

type GatewayClient struct {
cfg ClientConfig
limits Limits
logger log.Logger
pool *ringclient.Pool
ring ring.ReadRing
cfg ClientConfig
limits Limits
logger log.Logger
metrics *clientMetrics
pool *ringclient.Pool
ring ring.ReadRing
}

func NewClient(
Expand Down Expand Up @@ -206,11 +207,12 @@ func NewClient(
}

return &GatewayClient{
cfg: cfg,
logger: logger,
limits: limits,
pool: clientpool.NewPool("bloom-gateway", cfg.PoolConfig, cfg.Ring, ringclient.PoolAddrFunc(poolFactory), logger, metricsNamespace),
ring: readRing,
cfg: cfg,
logger: logger,
limits: limits,
metrics: newClientMetrics(registerer),
pool: clientpool.NewPool("bloom-gateway", cfg.PoolConfig, cfg.Ring, ringclient.PoolAddrFunc(poolFactory), logger, metricsNamespace),
ring: readRing,
}, nil
}

Expand All @@ -231,7 +233,7 @@ func shuffleAddrs(addrs []string) []string {

// FilterChunkRefs implements Client
func (c *GatewayClient) FilterChunks(ctx context.Context, tenant string, from, through model.Time, groups []*logproto.GroupedChunkRefs, plan plan.QueryPlan) ([]*logproto.GroupedChunkRefs, error) {
if !c.limits.BloomGatewayEnabled(tenant) {
if !c.limits.BloomGatewayEnabled(tenant) || len(groups) == 0 {
return groups, nil
}

Expand All @@ -248,6 +250,15 @@ func (c *GatewayClient) FilterChunks(ctx context.Context, tenant string, from, t
}
servers = partitionByReplicationSet(groups, servers)

// `% instances / % kesypace `. Ideally converges to 1,
// with theoretical max of `1 + 2/n_replicas` since the left and right keyspace bounds
// may lightly touch neighboring replicas.
firstFp, lastFp := groups[0].Fingerprint, groups[len(groups)-1].Fingerprint
pctKeyspace := float64(lastFp-firstFp) / float64(math.MaxUint64)
pctInstances := float64(len(servers)) / float64(len(rs.Instances))
cacheLocalityScore := pctInstances / pctKeyspace
c.metrics.cacheLocalityScore.Observe(cacheLocalityScore)

results := make([][]*logproto.GroupedChunkRefs, len(servers))
count := 0
err = concurrency.ForEachJob(ctx, len(servers), len(servers), func(ctx context.Context, i int) error {
Expand Down
17 changes: 17 additions & 0 deletions pkg/bloomgateway/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bloomgateway
import (
"time"

"github.com/grafana/loki/v3/pkg/util/constants"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
Expand All @@ -12,6 +13,22 @@ type metrics struct {
*serverMetrics
}

type clientMetrics struct {
cacheLocalityScore prometheus.Histogram
}

func newClientMetrics(registerer prometheus.Registerer) *clientMetrics {
return &clientMetrics{
cacheLocalityScore: promauto.With(registerer).NewHistogram(prometheus.HistogramOpts{
Namespace: constants.Loki,
Subsystem: "bloom_gateway_client",
Name: "cache_locality_score",
Help: "Cache locality score of the bloom filter, as measured by % of keyspace touched / % of bloom_gws required",
Buckets: prometheus.ExponentialBucketsRange(1, 100, 8),
}),
}
}

type serverMetrics struct {
inflightRequests prometheus.Summary
requestedSeries prometheus.Histogram
Expand Down

0 comments on commit 24ef2cb

Please sign in to comment.