Skip to content

Commit

Permalink
fix: Invalidate caches when pipeline wrappers are disabled (#12903)
Browse files Browse the repository at this point in the history
  • Loading branch information
MasslessParticle authored May 6, 2024
1 parent db7c05c commit a772ed7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/storage/chunk/cache/resultscache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewResultsCache(
next: next,
cache: c,
limits: limits,
keyGen: keyGen,
keyGen: NewPipelineWrapperKeygen(keyGen),
cacheGenNumberLoader: cacheGenNumberLoader,
retentionEnabled: retentionEnabled,
extractor: extractor,
Expand Down
23 changes: 23 additions & 0 deletions pkg/storage/chunk/cache/resultscache/pipelinewrapper_keygen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package resultscache

import (
"context"
"github.com/grafana/loki/v3/pkg/util/httpreq"
)

type PipelineWrapperKeyGenerator struct {
inner KeyGenerator
}

func NewPipelineWrapperKeygen(inner KeyGenerator) KeyGenerator {
return &PipelineWrapperKeyGenerator{inner: inner}
}

func (kg *PipelineWrapperKeyGenerator) GenerateCacheKey(ctx context.Context, userID string, r Request) string {
innerKey := kg.inner.GenerateCacheKey(ctx, userID, r)

if httpreq.ExtractHeader(ctx, httpreq.LokiDisablePipelineWrappersHeader) == "true" {
return "pipeline-disabled:" + innerKey
}
return innerKey
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package resultscache

import (
"context"
"github.com/grafana/loki/v3/pkg/util/httpreq"
"github.com/stretchr/testify/require"
"testing"
)

func TestPipelineWrapperKeygen(t *testing.T) {
kg := &stubKeygen{key: "cache-key"}
keygen := NewPipelineWrapperKeygen(kg)

t.Run("it does nothing if pipeline wrappers aren't disabled", func(t *testing.T) {
key := keygen.GenerateCacheKey(context.Background(), "", nil)
require.Equal(t, "cache-key", key)
})

t.Run("it changes the key when pipeline wrappers are disabled", func(t *testing.T) {
ctx := httpreq.InjectHeader(context.Background(), httpreq.LokiDisablePipelineWrappersHeader, "true")
key := keygen.GenerateCacheKey(ctx, "", nil)
require.Equal(t, "pipeline-disabled:cache-key", key)
})
}

type stubKeygen struct {
key string
}

func (k *stubKeygen) GenerateCacheKey(_ context.Context, _ string, _ Request) string {
return k.key
}

0 comments on commit a772ed7

Please sign in to comment.