Debian Codegraph @

tsdb/index/tsi1/partition_test.go

raw
1 package tsi1_test
2
3 import (
4 "fmt"
5 "io/ioutil"
6 "os"
7 "path/filepath"
8 "testing"
9
10 "github.com/influxdata/influxdb/tsdb"
11 "github.com/influxdata/influxdb/tsdb/index/tsi1"
12 )
13
14 func TestPartition_Open(t *testing.T) {
15 sfile := MustOpenSeriesFile()
16 defer sfile.Close()
17
18 // Opening a fresh index should set the MANIFEST version to current version.
19 p := NewPartition(sfile.SeriesFile)
20 t.Run("open new index", func(t *testing.T) {
21 if err := p.Open(); err != nil {
22 t.Fatal(err)
23 }
24
25 // Check version set appropriately.
26 if got, exp := p.Manifest().Version, 1; got != exp {
27 t.Fatalf("got index version %d, expected %d", got, exp)
28 }
29 })
30
31 // Reopening an open index should return an error.
32 t.Run("reopen open index", func(t *testing.T) {
33 err := p.Open()
34 if err == nil {
35 p.Close()
36 t.Fatal("didn't get an error on reopen, but expected one")
37 }
38 p.Close()
39 })
40
41 // Opening an incompatible index should return an error.
42 incompatibleVersions := []int{-1, 0, 2}
43 for _, v := range incompatibleVersions {
44 t.Run(fmt.Sprintf("incompatible index version: %d", v), func(t *testing.T) {
45 p = NewPartition(sfile.SeriesFile)
46 // Manually create a MANIFEST file for an incompatible index version.
47 mpath := filepath.Join(p.Path(), tsi1.ManifestFileName)
48 m := tsi1.NewManifest(mpath)
49 m.Levels = nil
50 m.Version = v // Set example MANIFEST version.
51 if _, err := m.Write(); err != nil {
52 t.Fatal(err)
53 }
54
55 // Log the MANIFEST file.
56 data, err := ioutil.ReadFile(mpath)
57 if err != nil {
58 panic(err)
59 }
60 t.Logf("Incompatible MANIFEST: %s", data)
61
62 // Opening this index should return an error because the MANIFEST has an
63 // incompatible version.
64 err = p.Open()
65 if err != tsi1.ErrIncompatibleVersion {
66 p.Close()
67 t.Fatalf("got error %v, expected %v", err, tsi1.ErrIncompatibleVersion)
68 }
69 })
70 }
71 }
72
73 func TestPartition_Manifest(t *testing.T) {
74 t.Run("current MANIFEST", func(t *testing.T) {
75 sfile := MustOpenSeriesFile()
76 defer sfile.Close()
77
78 p := MustOpenPartition(sfile.SeriesFile)
79 if got, exp := p.Manifest().Version, tsi1.Version; got != exp {
80 t.Fatalf("got MANIFEST version %d, expected %d", got, exp)
81 }
82 })
83 }
84
85 // Partition is a test wrapper for tsi1.Partition.
86 type Partition struct {
87 *tsi1.Partition
88 }
89
90 // NewPartition returns a new instance of Partition at a temporary path.
91 func NewPartition(sfile *tsdb.SeriesFile) *Partition {
92 return &Partition{Partition: tsi1.NewPartition(sfile, MustTempPartitionDir())}
93 }
94
95 // MustOpenPartition returns a new, open index. Panic on error.
96 func MustOpenPartition(sfile *tsdb.SeriesFile) *Partition {
97 p := NewPartition(sfile)
98 if err := p.Open(); err != nil {
99 panic(err)
100 }
101 return p
102 }
103
104 // Close closes and removes the index directory.
105 func (p *Partition) Close() error {
106 defer os.RemoveAll(p.Path())
107 return p.Partition.Close()
108 }
109
110 // Reopen closes and opens the index.
111 func (p *Partition) Reopen() error {
112 if err := p.Partition.Close(); err != nil {
113 return err
114 }
115
116 sfile, path := p.SeriesFile(), p.Path()
117 p.Partition = tsi1.NewPartition(sfile, path)
118 return p.Open()
119 }