Skip to content

Commit 435c4cd

Browse files
committed
Remove most chunk encoding/decoding code
Signed-off-by: Andrew Bloomgarden <[email protected]>
1 parent d738f3a commit 435c4cd

15 files changed

+47
-662
lines changed

pkg/chunk/cache/cache_test.go

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cache_test
22

33
import (
4+
"bytes"
45
"context"
56
"math/rand"
67
"strconv"
@@ -9,23 +10,19 @@ import (
910

1011
"github.com/go-kit/log"
1112
"github.com/prometheus/common/model"
12-
"github.com/prometheus/prometheus/model/labels"
1313
"github.com/stretchr/testify/require"
1414

15-
"github.com/cortexproject/cortex/pkg/chunk"
1615
"github.com/cortexproject/cortex/pkg/chunk/cache"
1716
prom_chunk "github.com/cortexproject/cortex/pkg/chunk/encoding"
1817
)
1918

20-
const userID = "1"
21-
22-
func fillCache(t *testing.T, cache cache.Cache) ([]string, []chunk.Chunk) {
19+
func fillCache(t *testing.T, cache cache.Cache) ([]string, []prom_chunk.Chunk) {
2320
const chunkLen = 13 * 3600 // in seconds
2421

2522
// put a set of chunks, larger than background batch size, with varying timestamps and values
2623
keys := []string{}
2724
bufs := [][]byte{}
28-
chunks := []chunk.Chunk{}
25+
chunks := []prom_chunk.Chunk{}
2926
for i := 0; i < 111; i++ {
3027
ts := model.TimeFromUnix(int64(i * chunkLen))
3128
promChunk, err := prom_chunk.NewForEncoding(prom_chunk.PrometheusXorChunk)
@@ -36,48 +33,30 @@ func fillCache(t *testing.T, cache cache.Cache) ([]string, []chunk.Chunk) {
3633
})
3734
require.NoError(t, err)
3835
require.Nil(t, nc)
39-
c := chunk.NewChunk(
40-
userID,
41-
model.Fingerprint(1),
42-
labels.Labels{
43-
{Name: model.MetricNameLabel, Value: "foo"},
44-
{Name: "bar", Value: "baz"},
45-
},
46-
promChunk,
47-
ts,
48-
ts.Add(chunkLen),
49-
)
50-
51-
err = c.Encode()
52-
require.NoError(t, err)
53-
buf, err := c.Encoded()
36+
37+
buf := bytes.NewBuffer(nil)
38+
err = promChunk.Marshal(buf)
5439
require.NoError(t, err)
5540

5641
// In order to be able to compare the expected chunk (this one) with the
5742
// actual one (the one that will be fetched from the cache) we need to
5843
// cleanup the chunk to avoid any internal references mismatch (ie. appender
5944
// pointer).
60-
cleanChunk := chunk.Chunk{
61-
UserID: c.UserID,
62-
Fingerprint: c.Fingerprint,
63-
From: c.From,
64-
Through: c.Through,
65-
Checksum: c.Checksum,
66-
ChecksumSet: c.ChecksumSet,
67-
}
68-
err = cleanChunk.Decode(chunk.NewDecodeContext(), buf)
45+
cleanChunk, err := prom_chunk.NewForEncoding(prom_chunk.PrometheusXorChunk)
46+
require.NoError(t, err)
47+
err = cleanChunk.UnmarshalFromBuf(buf.Bytes())
6948
require.NoError(t, err)
7049

71-
keys = append(keys, c.ExternalKey())
72-
bufs = append(bufs, buf)
50+
keys = append(keys, strconv.Itoa(i))
51+
bufs = append(bufs, buf.Bytes())
7352
chunks = append(chunks, cleanChunk)
7453
}
7554

7655
cache.Store(context.Background(), keys, bufs)
7756
return keys, chunks
7857
}
7958

80-
func testCacheSingle(t *testing.T, cache cache.Cache, keys []string, chunks []chunk.Chunk) {
59+
func testCacheSingle(t *testing.T, cache cache.Cache, keys []string, chunks []prom_chunk.Chunk) {
8160
for i := 0; i < 100; i++ {
8261
index := rand.Intn(len(keys))
8362
key := keys[index]
@@ -87,26 +66,26 @@ func testCacheSingle(t *testing.T, cache cache.Cache, keys []string, chunks []ch
8766
require.Len(t, bufs, 1)
8867
require.Len(t, missingKeys, 0)
8968

90-
c, err := chunk.ParseExternalKey(userID, found[0])
69+
c, err := prom_chunk.NewForEncoding(prom_chunk.PrometheusXorChunk)
9170
require.NoError(t, err)
92-
err = c.Decode(chunk.NewDecodeContext(), bufs[0])
71+
err = c.UnmarshalFromBuf(bufs[0])
9372
require.NoError(t, err)
9473
require.Equal(t, chunks[index], c)
9574
}
9675
}
9776

98-
func testCacheMultiple(t *testing.T, cache cache.Cache, keys []string, chunks []chunk.Chunk) {
77+
func testCacheMultiple(t *testing.T, cache cache.Cache, keys []string, chunks []prom_chunk.Chunk) {
9978
// test getting them all
10079
found, bufs, missingKeys := cache.Fetch(context.Background(), keys)
10180
require.Len(t, found, len(keys))
10281
require.Len(t, bufs, len(keys))
10382
require.Len(t, missingKeys, 0)
10483

105-
result := []chunk.Chunk{}
84+
result := []prom_chunk.Chunk{}
10685
for i := range found {
107-
c, err := chunk.ParseExternalKey(userID, found[i])
86+
c, err := prom_chunk.NewForEncoding(prom_chunk.PrometheusXorChunk)
10887
require.NoError(t, err)
109-
err = c.Decode(chunk.NewDecodeContext(), bufs[i])
88+
err = c.UnmarshalFromBuf(bufs[i])
11089
require.NoError(t, err)
11190
result = append(result, c)
11291
}

0 commit comments

Comments
 (0)