github.com/grafana/pyroscope@v1.18.0/pkg/block/metadata/metadata_test.go (about) 1 package metadata 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/oklog/ulid/v2" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 11 metastorev1 "github.com/grafana/pyroscope/api/gen/proto/go/metastore/v1" 12 ) 13 14 func TestMetadata_New(t *testing.T) { 15 blockID := ulid.MustNew(123, bytes.NewReader([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11})).String() 16 strings := NewStringTable() 17 md := &metastorev1.BlockMeta{ 18 FormatVersion: 0, 19 Id: blockID, 20 Tenant: 0, 21 Shard: 1, 22 CompactionLevel: 0, 23 MinTime: 123, 24 MaxTime: 456, 25 CreatedBy: strings.Put("ingester-a"), 26 Size: 567, 27 Datasets: nil, 28 StringTable: nil, 29 } 30 31 b := NewLabelBuilder(strings) 32 for _, tenant := range []string{"tenant-a", "tenant-b"} { 33 for _, dataset := range []string{"service_a", "service_b"} { 34 ds := &metastorev1.Dataset{ 35 Tenant: strings.Put(tenant), 36 Name: strings.Put(dataset), 37 MinTime: 123, 38 MaxTime: 456, 39 TableOfContents: []uint64{1, 2, 3}, 40 Size: 567, 41 Labels: nil, 42 } 43 for _, n := range []string{"cpu", "memory"} { 44 b.WithLabelSet("service_name", dataset, "__profile_type__", n) 45 } 46 ds.Labels = b.Build() 47 md.Datasets = append(md.Datasets, ds) 48 } 49 } 50 md.StringTable = strings.Strings 51 52 expected := &metastorev1.BlockMeta{ 53 FormatVersion: 0, 54 Id: "000000003V000G40R40M30E209", 55 Tenant: 0, 56 Shard: 1, 57 CompactionLevel: 0, 58 MinTime: 123, 59 MaxTime: 456, 60 CreatedBy: 1, 61 Size: 567, 62 Datasets: []*metastorev1.Dataset{ 63 { 64 Tenant: 2, 65 Name: 3, 66 MinTime: 123, 67 MaxTime: 456, 68 TableOfContents: []uint64{1, 2, 3}, 69 Size: 567, 70 Labels: []int32{2, 4, 3, 5, 6, 2, 4, 3, 5, 7}, 71 }, 72 { 73 Tenant: 2, 74 Name: 8, 75 MinTime: 123, 76 MaxTime: 456, 77 TableOfContents: []uint64{1, 2, 3}, 78 Size: 567, 79 Labels: []int32{2, 4, 8, 5, 6, 2, 4, 8, 5, 7}, 80 }, 81 { 82 Tenant: 9, 83 Name: 3, 84 MinTime: 123, 85 MaxTime: 456, 86 TableOfContents: []uint64{1, 2, 3}, 87 Size: 567, 88 Labels: []int32{2, 4, 3, 5, 6, 2, 4, 3, 5, 7}, 89 }, 90 { 91 Tenant: 9, 92 Name: 8, 93 MinTime: 123, 94 MaxTime: 456, 95 TableOfContents: []uint64{1, 2, 3}, 96 Size: 567, 97 Labels: []int32{2, 4, 8, 5, 6, 2, 4, 8, 5, 7}, 98 }, 99 }, 100 StringTable: []string{ 101 "", "ingester-a", 102 "tenant-a", "service_a", "service_name", "__profile_type__", "cpu", "memory", 103 "service_b", "tenant-b", 104 }, 105 } 106 107 assert.Equal(t, expected, md) 108 } 109 110 func TestMetadataStrings_Import(t *testing.T) { 111 md1 := &metastorev1.BlockMeta{ 112 Id: "block_id", 113 Tenant: 0, 114 CreatedBy: 1, 115 Datasets: []*metastorev1.Dataset{ 116 {Tenant: 2, Name: 3, Labels: []int32{2, 10, 3, 11, 4, 2, 10, 3, 11, 5, 2, 10, 3, 11, 6}}, 117 {Tenant: 7, Name: 8, Labels: []int32{2, 10, 8, 11, 5, 2, 10, 8, 11, 6, 2, 10, 8, 11, 9}}, 118 }, 119 StringTable: []string{ 120 "", "ingester", 121 "tenant-a", "dataset-a", "1", "2", "3", 122 "tenant-b", "dataset-b", "4", 123 "service_name", "__profile_type__", 124 }, 125 } 126 127 table := NewStringTable() 128 md1c := md1.CloneVT() 129 table.Import(md1c) 130 assert.Equal(t, md1, md1c) 131 assert.Equal(t, table.Strings, md1.StringTable) 132 133 // Exactly the same metadata. 134 md2 := md1.CloneVT() 135 table.Import(md2) 136 assert.Len(t, md2.StringTable, 12) 137 assert.Len(t, table.Strings, 12) 138 assert.Equal(t, table.Strings, md2.StringTable) 139 140 md3 := &metastorev1.BlockMeta{ 141 Id: "block_id_3", 142 Tenant: 0, 143 CreatedBy: 1, 144 Datasets: []*metastorev1.Dataset{ 145 {Tenant: 2, Name: 3, Labels: []int32{2, 10, 3, 11, 4, 2, 10, 3, 11, 5, 2, 10, 3, 11, 6}}, 146 {Tenant: 7, Name: 8, Labels: []int32{2, 10, 8, 11, 4, 2, 10, 8, 11, 9}}, 147 }, 148 StringTable: []string{ 149 "", "ingester", 150 "tenant-a", "dataset-a", "1", "2", "3", 151 "tenant-c", "dataset-c", "5", 152 "service_name", "__profile_type__", 153 }, 154 } 155 156 table.Import(md3) 157 expected := &metastorev1.BlockMeta{ 158 Id: "block_id_3", 159 Tenant: 0, 160 CreatedBy: 1, 161 Datasets: []*metastorev1.Dataset{ 162 {Tenant: 2, Name: 3, Labels: []int32{2, 10, 3, 11, 4, 2, 10, 3, 11, 5, 2, 10, 3, 11, 6}}, 163 {Tenant: 12, Name: 13, Labels: []int32{2, 10, 13, 11, 4, 2, 10, 13, 11, 14}}, 164 }, 165 StringTable: []string{ 166 "", "ingester", 167 "tenant-a", "dataset-a", "1", "2", "3", 168 "tenant-c", "dataset-c", "5", 169 "service_name", "__profile_type__", 170 }, 171 } 172 173 assert.Equal(t, expected, md3) 174 assert.Len(t, table.Strings, 15) 175 } 176 177 func TestMetadataStrings_Export(t *testing.T) { 178 table := NewStringTable() 179 for _, s := range []string{ 180 "", "x1", "x2", "x3", "x4", "x5", 181 "ingester", 182 "tenant-a", "dataset-a", "1", "2", "3", 183 "tenant-b", "dataset-b", "4", 184 "service_name", "__profile_type__", 185 } { 186 table.Put(s) 187 } 188 189 md := &metastorev1.BlockMeta{ 190 Id: "1", 191 Tenant: 0, 192 CreatedBy: 6, 193 Datasets: []*metastorev1.Dataset{ 194 {Tenant: 7, Name: 8, Labels: []int32{2, 15, 8, 16, 9, 2, 15, 8, 16, 10, 2, 15, 8, 16, 11}}, 195 {Tenant: 12, Name: 13, Labels: []int32{2, 15, 13, 16, 10, 2, 15, 13, 16, 11, 2, 15, 13, 16, 14}}, 196 }, 197 } 198 199 table.Export(md) 200 201 expected := &metastorev1.BlockMeta{ 202 Id: "1", 203 Tenant: 0, 204 CreatedBy: 1, 205 Datasets: []*metastorev1.Dataset{ 206 {Tenant: 2, Name: 3, Labels: []int32{2, 4, 3, 5, 6, 2, 4, 3, 5, 7, 2, 4, 3, 5, 8}}, 207 {Tenant: 9, Name: 10, Labels: []int32{2, 4, 10, 5, 7, 2, 4, 10, 5, 8, 2, 4, 10, 5, 11}}, 208 }, 209 StringTable: []string{ 210 "", "ingester", 211 "tenant-a", "dataset-a", "service_name", "__profile_type__", "1", "2", "3", 212 "tenant-b", "dataset-b", "4", 213 }, 214 } 215 216 assert.Equal(t, expected, md) 217 } 218 219 func TestMetadata_EncodeDecode(t *testing.T) { 220 md := &metastorev1.BlockMeta{ 221 Id: "1", 222 Tenant: 0, 223 CreatedBy: 1, 224 Datasets: []*metastorev1.Dataset{ 225 {Tenant: 2, Name: 3, Labels: []int32{2, 4, 3, 5, 6, 2, 4, 3, 5, 7, 2, 4, 3, 5, 8}}, 226 {Tenant: 9, Name: 10, Labels: []int32{2, 4, 10, 5, 7, 2, 4, 10, 5, 8, 2, 4, 10, 5, 11}}, 227 }, 228 StringTable: []string{ 229 "", "ingester", 230 "tenant-a", "dataset-a", "service_name", "__profile_type__", "1", "2", "3", 231 "tenant-b", "dataset-b", "4", 232 }, 233 } 234 235 var buf bytes.Buffer 236 require.NoError(t, Encode(&buf, md)) 237 238 var d metastorev1.BlockMeta 239 raw := append([]byte("garbage"), buf.Bytes()...) 240 require.NoError(t, Decode(raw, &d)) 241 assert.Equal(t, md, &d) 242 }