github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/storage/stores/tsdb/multi_file_index_test.go (about) 1 package tsdb 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/prometheus/common/model" 8 "github.com/prometheus/prometheus/model/labels" 9 "github.com/stretchr/testify/require" 10 11 "github.com/grafana/loki/pkg/storage/stores/tsdb/index" 12 ) 13 14 func TestMultiIndex(t *testing.T) { 15 cases := []LoadableSeries{ 16 { 17 Labels: mustParseLabels(`{foo="bar"}`), 18 Chunks: []index.ChunkMeta{ 19 { 20 MinTime: 0, 21 MaxTime: 3, 22 Checksum: 0, 23 }, 24 { 25 MinTime: 1, 26 MaxTime: 4, 27 Checksum: 1, 28 }, 29 { 30 MinTime: 2, 31 MaxTime: 5, 32 Checksum: 2, 33 }, 34 }, 35 }, 36 { 37 Labels: mustParseLabels(`{foo="bar", bazz="buzz"}`), 38 Chunks: []index.ChunkMeta{ 39 { 40 MinTime: 1, 41 MaxTime: 10, 42 Checksum: 3, 43 }, 44 }, 45 }, 46 { 47 // should be excluded due to bounds checking 48 Labels: mustParseLabels(`{foo="bar", bazz="bozz", bonk="borb"}`), 49 Chunks: []index.ChunkMeta{ 50 { 51 MinTime: 8, 52 MaxTime: 9, 53 Checksum: 4, 54 }, 55 }, 56 }, 57 } 58 59 // group 5 indices together, all with duplicate data 60 n := 5 61 var indices []Index 62 dir := t.TempDir() 63 for i := 0; i < n; i++ { 64 indices = append(indices, BuildIndex(t, dir, "fake", cases)) 65 } 66 67 idx, err := NewMultiIndex(indices...) 68 require.Nil(t, err) 69 70 t.Run("GetChunkRefs", func(t *testing.T) { 71 refs, err := idx.GetChunkRefs(context.Background(), "fake", 2, 5, nil, nil, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) 72 require.Nil(t, err) 73 74 expected := []ChunkRef{ 75 { 76 User: "fake", 77 Fingerprint: model.Fingerprint(mustParseLabels(`{foo="bar"}`).Hash()), 78 Start: 0, 79 End: 3, 80 Checksum: 0, 81 }, 82 { 83 User: "fake", 84 Fingerprint: model.Fingerprint(mustParseLabels(`{foo="bar"}`).Hash()), 85 Start: 1, 86 End: 4, 87 Checksum: 1, 88 }, 89 { 90 User: "fake", 91 Fingerprint: model.Fingerprint(mustParseLabels(`{foo="bar"}`).Hash()), 92 Start: 2, 93 End: 5, 94 Checksum: 2, 95 }, 96 { 97 User: "fake", 98 Fingerprint: model.Fingerprint(mustParseLabels(`{foo="bar", bazz="buzz"}`).Hash()), 99 Start: 1, 100 End: 10, 101 Checksum: 3, 102 }, 103 } 104 require.Equal(t, expected, refs) 105 }) 106 107 t.Run("Series", func(t *testing.T) { 108 xs, err := idx.Series(context.Background(), "fake", 2, 5, nil, nil, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) 109 require.Nil(t, err) 110 expected := []Series{ 111 { 112 Labels: mustParseLabels(`{foo="bar"}`), 113 Fingerprint: model.Fingerprint(mustParseLabels(`{foo="bar"}`).Hash()), 114 }, 115 { 116 Labels: mustParseLabels(`{foo="bar", bazz="buzz"}`), 117 Fingerprint: model.Fingerprint(mustParseLabels(`{foo="bar", bazz="buzz"}`).Hash()), 118 }, 119 } 120 121 require.Equal(t, expected, xs) 122 }) 123 124 t.Run("LabelNames", func(t *testing.T) { 125 // request data at the end of the tsdb range, but it should return all labels present 126 xs, err := idx.LabelNames(context.Background(), "fake", 8, 10) 127 require.Nil(t, err) 128 expected := []string{"bazz", "bonk", "foo"} 129 130 require.Equal(t, expected, xs) 131 }) 132 133 t.Run("LabelNamesWithMatchers", func(t *testing.T) { 134 // request data at the end of the tsdb range, but it should return all labels present 135 xs, err := idx.LabelNames(context.Background(), "fake", 8, 10, labels.MustNewMatcher(labels.MatchEqual, "bazz", "buzz")) 136 require.Nil(t, err) 137 expected := []string{"bazz", "foo"} 138 139 require.Equal(t, expected, xs) 140 }) 141 142 t.Run("LabelValues", func(t *testing.T) { 143 xs, err := idx.LabelValues(context.Background(), "fake", 1, 2, "bazz") 144 require.Nil(t, err) 145 expected := []string{"bozz", "buzz"} 146 147 require.Equal(t, expected, xs) 148 }) 149 150 t.Run("LabelValuesWithMatchers", func(t *testing.T) { 151 xs, err := idx.LabelValues(context.Background(), "fake", 1, 2, "bazz", labels.MustNewMatcher(labels.MatchEqual, "bonk", "borb")) 152 require.Nil(t, err) 153 expected := []string{"bozz"} 154 155 require.Equal(t, expected, xs) 156 }) 157 }