Skip to content

Commit 4f3d299

Browse files
committed
test + lint
Signed-off-by: alanprot <[email protected]>
1 parent 6e9db73 commit 4f3d299

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

pkg/ingester/ingester_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5523,6 +5523,88 @@ func TestExpendedPostingsCacheIsolation(t *testing.T) {
55235523
wg.Wait()
55245524
}
55255525

5526+
func TestExpendedPostingsCacheGlobalLimit(t *testing.T) {
5527+
cfg := defaultIngesterTestConfig(t)
5528+
maxBytes := int64(1024)
5529+
users := []string{"test1", "test2"}
5530+
cfg.BlocksStorageConfig.TSDB.BlockRanges = []time.Duration{2 * time.Hour}
5531+
cfg.BlocksStorageConfig.TSDB.PostingsCache = cortex_tsdb.TSDBPostingsCacheConfig{
5532+
Blocks: cortex_tsdb.PostingsCacheConfig{
5533+
Ttl: time.Hour,
5534+
MaxBytes: maxBytes,
5535+
Enabled: true,
5536+
},
5537+
Head: cortex_tsdb.PostingsCacheConfig{
5538+
Ttl: time.Hour,
5539+
MaxBytes: maxBytes,
5540+
Enabled: true,
5541+
},
5542+
}
5543+
5544+
cfg.LifecyclerConfig.JoinAfter = 0
5545+
5546+
r := prometheus.NewRegistry()
5547+
i, err := prepareIngesterWithBlocksStorage(t, cfg, r)
5548+
require.NoError(t, err)
5549+
require.NoError(t, services.StartAndAwaitRunning(context.Background(), i))
5550+
defer services.StopAndAwaitTerminated(context.Background(), i) //nolint:errcheck
5551+
5552+
metricNames := []string{"metric1", "metric2"}
5553+
5554+
// Generate 4 hours of data so we have 1 block + head
5555+
totalSamples := 4 * 60
5556+
var samples = make([]cortexpb.Sample, 0, totalSamples)
5557+
5558+
for i := 0; i < totalSamples; i++ {
5559+
samples = append(samples, cortexpb.Sample{
5560+
Value: float64(i),
5561+
TimestampMs: int64(i * 60 * 1000),
5562+
})
5563+
}
5564+
5565+
lbls := make([]labels.Labels, 0, len(samples))
5566+
for j := 0; j < 10; j++ {
5567+
for i := 0; i < len(samples); i++ {
5568+
lbls = append(lbls, labels.FromStrings(labels.MetricName, metricNames[i%len(metricNames)], "a", fmt.Sprintf("aaa%v", j)))
5569+
}
5570+
}
5571+
5572+
for i := len(samples); i < len(lbls); i++ {
5573+
samples = append(samples, samples[i%len(samples)])
5574+
}
5575+
5576+
for _, u := range users {
5577+
ctx := user.InjectOrgID(context.Background(), u)
5578+
req := cortexpb.ToWriteRequest(lbls, samples, nil, nil, cortexpb.API)
5579+
_, err = i.Push(ctx, req)
5580+
require.NoError(t, err)
5581+
5582+
i.compactBlocks(ctx, false, nil)
5583+
5584+
s := &mockQueryStreamServer{ctx: ctx}
5585+
5586+
err = i.QueryStream(&client.QueryRequest{
5587+
StartTimestampMs: 0,
5588+
EndTimestampMs: math.MaxInt64,
5589+
Matchers: []*client.LabelMatcher{
5590+
{
5591+
Type: client.EQUAL,
5592+
Name: "__name__",
5593+
Value: strings.Repeat("a", int(maxBytes/2)), // make sure the size it bigger than the max size
5594+
},
5595+
},
5596+
}, s)
5597+
}
5598+
5599+
err = testutil.GatherAndCompare(r, bytes.NewBufferString(`
5600+
# HELP cortex_ingester_expanded_postings_cache_evicts_total Total number of evictions in the cache, excluding items that got evicted due to TTL.
5601+
# TYPE cortex_ingester_expanded_postings_cache_evicts_total counter
5602+
cortex_ingester_expanded_postings_cache_evicts_total{cache="block",reason="full"} 1
5603+
cortex_ingester_expanded_postings_cache_evicts_total{cache="head",reason="full"} 1
5604+
`), "cortex_ingester_expanded_postings_cache_evicts_total")
5605+
require.NoError(t, err)
5606+
}
5607+
55265608
func TestExpendedPostingsCache(t *testing.T) {
55275609
cfg := defaultIngesterTestConfig(t)
55285610
cfg.BlocksStorageConfig.TSDB.BlockRanges = []time.Duration{2 * time.Hour}

pkg/storage/tsdb/expanded_postings_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strconv"
99
"strings"
1010
"sync"
11-
"sync/atomic"
1211
"time"
1312

1413
"github.com/oklog/ulid"
@@ -19,6 +18,7 @@ import (
1918
"github.com/prometheus/prometheus/tsdb"
2019
"github.com/prometheus/prometheus/tsdb/index"
2120
"github.com/segmentio/fasthash/fnv1a"
21+
"go.uber.org/atomic"
2222

2323
"github.com/cortexproject/cortex/pkg/util/extract"
2424
logutil "github.com/cortexproject/cortex/pkg/util/log"

pkg/storage/tsdb/expanded_postings_cache_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"strings"
77
"sync"
8-
"sync/atomic"
98
"testing"
109
"time"
1110

@@ -14,6 +13,7 @@ import (
1413
"github.com/prometheus/client_golang/prometheus/testutil"
1514
"github.com/prometheus/prometheus/model/labels"
1615
"github.com/stretchr/testify/require"
16+
"go.uber.org/atomic"
1717
)
1818

1919
func TestCacheKey(t *testing.T) {

0 commit comments

Comments
 (0)