1
1
package cache_test
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"math/rand"
6
7
"strconv"
@@ -9,23 +10,19 @@ import (
9
10
10
11
"github.com/go-kit/log"
11
12
"github.com/prometheus/common/model"
12
- "github.com/prometheus/prometheus/model/labels"
13
13
"github.com/stretchr/testify/require"
14
14
15
- "github.com/cortexproject/cortex/pkg/chunk"
16
15
"github.com/cortexproject/cortex/pkg/chunk/cache"
17
16
prom_chunk "github.com/cortexproject/cortex/pkg/chunk/encoding"
18
17
)
19
18
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 ) {
23
20
const chunkLen = 13 * 3600 // in seconds
24
21
25
22
// put a set of chunks, larger than background batch size, with varying timestamps and values
26
23
keys := []string {}
27
24
bufs := [][]byte {}
28
- chunks := []chunk .Chunk {}
25
+ chunks := []prom_chunk .Chunk {}
29
26
for i := 0 ; i < 111 ; i ++ {
30
27
ts := model .TimeFromUnix (int64 (i * chunkLen ))
31
28
promChunk , err := prom_chunk .NewForEncoding (prom_chunk .PrometheusXorChunk )
@@ -36,48 +33,30 @@ func fillCache(t *testing.T, cache cache.Cache) ([]string, []chunk.Chunk) {
36
33
})
37
34
require .NoError (t , err )
38
35
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 )
54
39
require .NoError (t , err )
55
40
56
41
// In order to be able to compare the expected chunk (this one) with the
57
42
// actual one (the one that will be fetched from the cache) we need to
58
43
// cleanup the chunk to avoid any internal references mismatch (ie. appender
59
44
// 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 ())
69
48
require .NoError (t , err )
70
49
71
- keys = append (keys , c . ExternalKey ( ))
72
- bufs = append (bufs , buf )
50
+ keys = append (keys , strconv . Itoa ( i ))
51
+ bufs = append (bufs , buf . Bytes () )
73
52
chunks = append (chunks , cleanChunk )
74
53
}
75
54
76
55
cache .Store (context .Background (), keys , bufs )
77
56
return keys , chunks
78
57
}
79
58
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 ) {
81
60
for i := 0 ; i < 100 ; i ++ {
82
61
index := rand .Intn (len (keys ))
83
62
key := keys [index ]
@@ -87,26 +66,26 @@ func testCacheSingle(t *testing.T, cache cache.Cache, keys []string, chunks []ch
87
66
require .Len (t , bufs , 1 )
88
67
require .Len (t , missingKeys , 0 )
89
68
90
- c , err := chunk . ParseExternalKey ( userID , found [ 0 ] )
69
+ c , err := prom_chunk . NewForEncoding ( prom_chunk . PrometheusXorChunk )
91
70
require .NoError (t , err )
92
- err = c .Decode ( chunk . NewDecodeContext (), bufs [0 ])
71
+ err = c .UnmarshalFromBuf ( bufs [0 ])
93
72
require .NoError (t , err )
94
73
require .Equal (t , chunks [index ], c )
95
74
}
96
75
}
97
76
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 ) {
99
78
// test getting them all
100
79
found , bufs , missingKeys := cache .Fetch (context .Background (), keys )
101
80
require .Len (t , found , len (keys ))
102
81
require .Len (t , bufs , len (keys ))
103
82
require .Len (t , missingKeys , 0 )
104
83
105
- result := []chunk .Chunk {}
84
+ result := []prom_chunk .Chunk {}
106
85
for i := range found {
107
- c , err := chunk . ParseExternalKey ( userID , found [ i ] )
86
+ c , err := prom_chunk . NewForEncoding ( prom_chunk . PrometheusXorChunk )
108
87
require .NoError (t , err )
109
- err = c .Decode ( chunk . NewDecodeContext (), bufs [i ])
88
+ err = c .UnmarshalFromBuf ( bufs [i ])
110
89
require .NoError (t , err )
111
90
result = append (result , c )
112
91
}
0 commit comments