forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
series_file_test.go
382 lines (320 loc) · 9.86 KB
/
series_file_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package tsdb_test
import (
"bytes"
"fmt"
"os"
"path"
"testing"
"github.com/influxdata/influxdb/v2/models"
"github.com/influxdata/influxdb/v2/tsdb"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
"golang.org/x/sync/errgroup"
)
func TestParseSeriesKeyInto(t *testing.T) {
name := []byte("cpu")
tags := models.NewTags(map[string]string{"region": "east", "server": "a"})
key := tsdb.AppendSeriesKey(nil, name, tags)
dst := make(models.Tags, 0)
gotName, gotTags := tsdb.ParseSeriesKeyInto(key, dst)
if !bytes.Equal(gotName, name) {
t.Fatalf("got %q, expected %q", gotName, name)
}
if got, exp := len(gotTags), 2; got != exp {
t.Fatalf("got tags length %d, expected %d", got, exp)
} else if got, exp := gotTags, tags; !got.Equal(exp) {
t.Fatalf("got tags %v, expected %v", got, exp)
}
dst = make(models.Tags, 0, 5)
_, gotTags = tsdb.ParseSeriesKeyInto(key, dst)
if got, exp := len(gotTags), 2; got != exp {
t.Fatalf("got tags length %d, expected %d", got, exp)
} else if got, exp := cap(gotTags), 5; got != exp {
t.Fatalf("got tags capacity %d, expected %d", got, exp)
} else if got, exp := gotTags, tags; !got.Equal(exp) {
t.Fatalf("got tags %v, expected %v", got, exp)
}
dst = make(models.Tags, 1)
_, gotTags = tsdb.ParseSeriesKeyInto(key, dst)
if got, exp := len(gotTags), 2; got != exp {
t.Fatalf("got tags length %d, expected %d", got, exp)
} else if got, exp := gotTags, tags; !got.Equal(exp) {
t.Fatalf("got tags %v, expected %v", got, exp)
}
}
// Ensure that broken series files are closed
func TestSeriesFile_Open_WhenFileCorrupt_ShouldReturnErr(t *testing.T) {
f := NewBrokenSeriesFile([]byte{0, 0, 0, 0, 0})
defer f.Close()
f.Logger = zaptest.NewLogger(t)
err := f.Open()
if err == nil {
t.Fatalf("should report error")
}
}
// Ensure series file contains the correct set of series.
func TestSeriesFile_Series(t *testing.T) {
sfile := MustOpenSeriesFile(t)
defer sfile.Close()
series := []Series{
{Name: []byte("cpu"), Tags: models.NewTags(map[string]string{"region": "east"})},
{Name: []byte("cpu"), Tags: models.NewTags(map[string]string{"region": "west"})},
{Name: []byte("mem"), Tags: models.NewTags(map[string]string{"region": "east"})},
}
for _, s := range series {
if _, err := sfile.CreateSeriesListIfNotExists([][]byte{[]byte(s.Name)}, []models.Tags{s.Tags}); err != nil {
t.Fatal(err)
}
}
// Verify total number of series is correct.
if n := sfile.SeriesCount(); n != 3 {
t.Fatalf("unexpected series count: %d", n)
}
// Verify all series exist.
for i, s := range series {
if seriesID := sfile.SeriesID(s.Name, s.Tags, nil); seriesID == 0 {
t.Fatalf("series does not exist: i=%d", i)
}
}
// Verify non-existent series doesn't exist.
if sfile.HasSeries([]byte("foo"), models.NewTags(map[string]string{"region": "north"}), nil) {
t.Fatal("series should not exist")
}
}
// Ensure series file can be compacted.
func TestSeriesFileCompactor(t *testing.T) {
sfile := MustOpenSeriesFile(t)
defer sfile.Close()
// Disable automatic compactions.
for _, p := range sfile.Partitions() {
p.CompactThreshold = 0
}
var names [][]byte
var tagsSlice []models.Tags
for i := 0; i < 10000; i++ {
names = append(names, []byte(fmt.Sprintf("m%d", i)))
tagsSlice = append(tagsSlice, models.NewTags(map[string]string{"foo": "bar"}))
}
if _, err := sfile.CreateSeriesListIfNotExists(names, tagsSlice); err != nil {
t.Fatal(err)
}
// Verify total number of series is correct.
if n := sfile.SeriesCount(); n != uint64(len(names)) {
t.Fatalf("unexpected series count: %d", n)
}
// Compact in-place for each partition.
for _, p := range sfile.Partitions() {
compactor := tsdb.NewSeriesPartitionCompactor()
if err := compactor.Compact(p); err != nil {
t.Fatal(err)
}
}
// Verify all series exist.
for i := range names {
if seriesID := sfile.SeriesID(names[i], tagsSlice[i], nil); seriesID == 0 {
t.Fatalf("series does not exist: %s,%s", names[i], tagsSlice[i].String())
}
}
}
// Ensure series file deletions persist across compactions.
func TestSeriesFile_DeleteSeriesID(t *testing.T) {
sfile := MustOpenSeriesFile(t)
defer sfile.Close()
ids0, err := sfile.CreateSeriesListIfNotExists([][]byte{[]byte("m1")}, []models.Tags{nil})
if err != nil {
t.Fatal(err)
} else if _, err := sfile.CreateSeriesListIfNotExists([][]byte{[]byte("m2")}, []models.Tags{nil}); err != nil {
t.Fatal(err)
} else if err := sfile.ForceCompact(); err != nil {
t.Fatal(err)
}
// Delete and ensure deletion.
if err := sfile.DeleteSeriesID(ids0[0]); err != nil {
t.Fatal(err)
} else if _, err := sfile.CreateSeriesListIfNotExists([][]byte{[]byte("m1")}, []models.Tags{nil}); err != nil {
t.Fatal(err)
} else if !sfile.IsDeleted(ids0[0]) {
t.Fatal("expected deletion before compaction")
}
if err := sfile.ForceCompact(); err != nil {
t.Fatal(err)
} else if !sfile.IsDeleted(ids0[0]) {
t.Fatal("expected deletion after compaction")
}
if err := sfile.Reopen(); err != nil {
t.Fatal(err)
} else if !sfile.IsDeleted(ids0[0]) {
t.Fatal("expected deletion after reopen")
}
}
func TestSeriesFile_Compaction(t *testing.T) {
sfile := MustOpenSeriesFile(t)
defer sfile.Close()
var segmentPaths []string
for _, p := range sfile.Partitions() {
for _, ss := range p.Segments() {
segmentPaths = append(segmentPaths, ss.Path())
}
}
sfileSize := func() (res int64) {
for _, p := range segmentPaths {
fi, err := os.Stat(p)
require.NoError(t, err)
res += fi.Size()
}
return
}
// Generate a bunch of keys.
var mms [][]byte
var tagSets []models.Tags
for i := 0; i < 1000; i++ {
mms = append(mms, []byte("cpu"))
tagSets = append(tagSets, models.NewTags(map[string]string{"region": fmt.Sprintf("r%d", i)}))
}
// Add all to the series file.
ids, err := sfile.CreateSeriesListIfNotExists(mms, tagSets)
require.NoError(t, err)
// Delete a subset of keys.
for i, id := range ids {
if i%10 == 0 {
require.NoError(t, sfile.DeleteSeriesID(id))
}
}
// Check total series count.
require.Equal(t, 1000, int(sfile.SeriesCount()))
// Compact all segments.
var paths []string
for _, p := range sfile.Partitions() {
for _, ss := range p.Segments() {
require.NoError(t, ss.CompactToPath(ss.Path()+".tmp", p.Index()))
paths = append(paths, ss.Path())
}
}
// Close index.
require.NoError(t, sfile.SeriesFile.Close())
// Compute total size of all series data.
origSize := sfileSize()
// Overwrite files.
for _, path := range paths {
require.NoError(t, os.Rename(path+".tmp", path))
}
// Check size of compacted series data.
// We do this before reopening the index because on Windows, opening+mmap'ing the series
// file will cause the file to grow back to its original size.
newSize := sfileSize()
// Verify new size is smaller.
require.Greater(t, origSize, newSize)
// Reopen index.
sfile.SeriesFile = tsdb.NewSeriesFile(sfile.SeriesFile.Path())
require.NoError(t, sfile.SeriesFile.Open())
// Ensure series status is correct.
for i, id := range ids {
require.Equal(t, (i%10) == 0, sfile.IsDeleted(id))
}
// Check total series count.
require.Equal(t, 900, int(sfile.SeriesCount()))
}
var cachedCompactionSeriesFile *SeriesFile
func BenchmarkSeriesFile_Compaction(b *testing.B) {
const n = 1000000
if cachedCompactionSeriesFile == nil {
sfile := MustOpenSeriesFile(b)
// Generate a bunch of keys.
var ids []uint64
for i := 0; i < n; i++ {
tmp, err := sfile.CreateSeriesListIfNotExists([][]byte{[]byte("cpu")}, []models.Tags{models.NewTags(map[string]string{"region": fmt.Sprintf("r%d", i)})})
if err != nil {
b.Fatal(err)
}
ids = append(ids, tmp...)
}
// Delete a subset of keys.
for i := 0; i < len(ids); i += 10 {
if err := sfile.DeleteSeriesID(ids[i]); err != nil {
b.Fatal(err)
}
}
cachedCompactionSeriesFile = sfile
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Compact all segments in parallel.
var g errgroup.Group
for _, p := range cachedCompactionSeriesFile.Partitions() {
for _, segment := range p.Segments() {
p, segment := p, segment
g.Go(func() error {
return segment.CompactToPath(segment.Path()+".tmp", p.Index())
})
}
}
if err := g.Wait(); err != nil {
b.Fatal(err)
}
}
}
// Series represents name/tagset pairs that are used in testing.
type Series struct {
Name []byte
Tags models.Tags
Deleted bool
}
// SeriesFile is a test wrapper for tsdb.SeriesFile.
type SeriesFile struct {
*tsdb.SeriesFile
}
// NewSeriesFile returns a new instance of SeriesFile with a temporary file path.
func NewSeriesFile() *SeriesFile {
dir, err := os.MkdirTemp("", "tsdb-series-file-")
if err != nil {
panic(err)
}
return &SeriesFile{SeriesFile: tsdb.NewSeriesFile(dir)}
}
func NewBrokenSeriesFile(content []byte) *SeriesFile {
sFile := NewSeriesFile()
fPath := sFile.Path()
sFile.Open()
sFile.SeriesFile.Close()
segPath := path.Join(fPath, "00", "0000")
if _, err := os.Stat(segPath); os.IsNotExist(err) {
panic(err)
}
err := os.WriteFile(segPath, content, 0777)
if err != nil {
panic(err)
}
return sFile
}
// MustOpenSeriesFile returns a new, open instance of SeriesFile. Panic on error.
func MustOpenSeriesFile(tb testing.TB) *SeriesFile {
tb.Helper()
f := NewSeriesFile()
f.Logger = zaptest.NewLogger(tb)
if err := f.Open(); err != nil {
panic(err)
}
return f
}
// Close closes the log file and removes it from disk.
func (f *SeriesFile) Close() error {
defer os.RemoveAll(f.Path())
return f.SeriesFile.Close()
}
// Reopen close & reopens the series file.
func (f *SeriesFile) Reopen() error {
if err := f.SeriesFile.Close(); err != nil {
return err
}
f.SeriesFile = tsdb.NewSeriesFile(f.SeriesFile.Path())
return f.SeriesFile.Open()
}
// ForceCompact executes an immediate compaction across all partitions.
func (f *SeriesFile) ForceCompact() error {
for _, p := range f.Partitions() {
if err := tsdb.NewSeriesPartitionCompactor().Compact(p); err != nil {
return err
}
}
return nil
}