Skip to content

Commit

Permalink
remove vendored cortex chunk code (#6034)
Browse files Browse the repository at this point in the history
* remove vendored cortex chunk code

Signed-off-by: Kama Huang <[email protected]>

* fixed unit test

Signed-off-by: Kama Huang <[email protected]>

Signed-off-by: Kama Huang <[email protected]>
  • Loading branch information
kama910 authored Jan 12, 2023
1 parent 37e37cc commit 50a96eb
Show file tree
Hide file tree
Showing 25 changed files with 17 additions and 5,686 deletions.
6 changes: 3 additions & 3 deletions internal/cortex/chunk/cache/background_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ func TestBackground(t *testing.T) {
WriteBackBuffer: 100,
}, cache.NewMockCache(), nil)

keys, chunks := fillCache(t, c)
keys, bufs := fillCache(t, c)
cache.Flush(c)

testCacheSingle(t, c, keys, chunks)
testCacheMultiple(t, c, keys, chunks)
testCacheSingle(t, c, keys, bufs)
testCacheMultiple(t, c, keys, bufs)
testCacheMiss(t, c)
}
87 changes: 14 additions & 73 deletions internal/cortex/chunk/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,31 @@ package cache_test

import (
"context"
"fmt"
"math/rand"
"strconv"
"testing"
"time"

"github.com/go-kit/log"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/require"

"github.com/thanos-io/thanos/internal/cortex/chunk"
"github.com/thanos-io/thanos/internal/cortex/chunk/cache"
prom_chunk "github.com/thanos-io/thanos/internal/cortex/chunk/encoding"
)

const userID = "1"

func fillCache(t *testing.T, cache cache.Cache) ([]string, []chunk.Chunk) {
const chunkLen = 13 * 3600 // in seconds

// put a set of chunks, larger than background batch size, with varying timestamps and values
func fillCache(t *testing.T, cache cache.Cache) ([]string, [][]byte) {
keys := []string{}
bufs := [][]byte{}
chunks := []chunk.Chunk{}
for i := 0; i < 111; i++ {
ts := model.TimeFromUnix(int64(i * chunkLen))
promChunk := prom_chunk.New()
nc, err := promChunk.Add(model.SamplePair{
Timestamp: ts,
Value: model.SampleValue(i),
})
require.NoError(t, err)
require.Nil(t, nc)
c := chunk.NewChunk(
userID,
model.Fingerprint(1),
labels.Labels{
{Name: model.MetricNameLabel, Value: "foo"},
{Name: "bar", Value: "baz"},
},
promChunk,
ts,
ts.Add(chunkLen),
)

err = c.Encode()
require.NoError(t, err)
buf, err := c.Encoded()
require.NoError(t, err)

// In order to be able to compare the expected chunk (this one) with the
// actual one (the one that will be fetched from the cache) we need to
// cleanup the chunk to avoid any internal references mismatch (ie. appender
// pointer).
cleanChunk := chunk.Chunk{
UserID: c.UserID,
Fingerprint: c.Fingerprint,
From: c.From,
Through: c.Through,
Checksum: c.Checksum,
ChecksumSet: c.ChecksumSet,
}
err = cleanChunk.Decode(chunk.NewDecodeContext(), buf)
require.NoError(t, err)

keys = append(keys, c.ExternalKey())
bufs = append(bufs, buf)
chunks = append(chunks, cleanChunk)
keys = append(keys, fmt.Sprintf("test%d", i))
bufs = append(bufs, []byte(fmt.Sprintf("buf%d", i)))
}

cache.Store(context.Background(), keys, bufs)
return keys, chunks
return keys, bufs
}

func testCacheSingle(t *testing.T, cache cache.Cache, keys []string, chunks []chunk.Chunk) {
func testCacheSingle(t *testing.T, cache cache.Cache, keys []string, data [][]byte) {
for i := 0; i < 100; i++ {
index := rand.Intn(len(keys))
key := keys[index]
Expand All @@ -88,31 +38,22 @@ func testCacheSingle(t *testing.T, cache cache.Cache, keys []string, chunks []ch
require.Len(t, found, 1)
require.Len(t, bufs, 1)
require.Len(t, missingKeys, 0)

c, err := chunk.ParseExternalKey(userID, found[0])
require.NoError(t, err)
err = c.Decode(chunk.NewDecodeContext(), bufs[0])
require.NoError(t, err)
require.Equal(t, chunks[index], c)
require.Equal(t, data[index], bufs[0])
}
}

func testCacheMultiple(t *testing.T, cache cache.Cache, keys []string, chunks []chunk.Chunk) {
func testCacheMultiple(t *testing.T, cache cache.Cache, keys []string, data [][]byte) {
// test getting them all
found, bufs, missingKeys := cache.Fetch(context.Background(), keys)
require.Len(t, found, len(keys))
require.Len(t, bufs, len(keys))
require.Len(t, missingKeys, 0)

result := []chunk.Chunk{}
result := [][]byte{}
for i := range found {
c, err := chunk.ParseExternalKey(userID, found[i])
require.NoError(t, err)
err = c.Decode(chunk.NewDecodeContext(), bufs[i])
require.NoError(t, err)
result = append(result, c)
result = append(result, bufs[i])
}
require.Equal(t, chunks, result)
require.Equal(t, data, result)
}

func testCacheMiss(t *testing.T, cache cache.Cache) {
Expand All @@ -126,12 +67,12 @@ func testCacheMiss(t *testing.T, cache cache.Cache) {
}

func testCache(t *testing.T, cache cache.Cache) {
keys, chunks := fillCache(t, cache)
keys, bufs := fillCache(t, cache)
t.Run("Single", func(t *testing.T) {
testCacheSingle(t, cache, keys, chunks)
testCacheSingle(t, cache, keys, bufs)
})
t.Run("Multiple", func(t *testing.T) {
testCacheMultiple(t, cache, keys, chunks)
testCacheMultiple(t, cache, keys, bufs)
})
t.Run("Miss", func(t *testing.T) {
testCacheMiss(t, cache)
Expand Down
Loading

0 comments on commit 50a96eb

Please sign in to comment.