Skip to content

Commit 4c0bcf5

Browse files
committed
fix metadata encoding
1 parent 1b01c99 commit 4c0bcf5

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

pkg/experiment/block/metadata/metadata.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ var castagnoli = crc32.MakeTable(crc32.Castagnoli)
164164
// be_uint32 | size of the raw metadata
165165
// be_uint32 | CRC32 of the raw metadata and size
166166
func Encode(w io.Writer, md *metastorev1.BlockMeta) error {
167-
ww := crc32.New(castagnoli)
167+
crc := crc32.New(castagnoli)
168+
w = io.MultiWriter(w, crc)
168169
b, _ := md.MarshalVT()
169170
n, err := w.Write(b)
170171
if err != nil {
@@ -173,7 +174,7 @@ func Encode(w io.Writer, md *metastorev1.BlockMeta) error {
173174
if err = binary.Write(w, binary.BigEndian, uint32(n)); err != nil {
174175
return err
175176
}
176-
return binary.Write(w, binary.BigEndian, ww.Sum32())
177+
return binary.Write(w, binary.BigEndian, crc.Sum32())
177178
}
178179

179180
// Decode metadata encoded with Encode.
@@ -183,11 +184,12 @@ func Decode(b []byte, md *metastorev1.BlockMeta) error {
183184
}
184185
crc := binary.BigEndian.Uint32(b[len(b)-4:])
185186
size := binary.BigEndian.Uint32(b[len(b)-8 : len(b)-4])
186-
if size != uint32(len(b)-8) {
187+
off := len(b) - 8 - int(size)
188+
if off < 0 {
187189
return fmt.Errorf("%w: invalid size", ErrMetadataInvalid)
188190
}
189-
if crc32.Checksum(b[:len(b)-4], castagnoli) != crc {
191+
if crc32.Checksum(b[off:len(b)-4], castagnoli) != crc {
190192
return fmt.Errorf("%w: invalid CRC", ErrMetadataInvalid)
191193
}
192-
return md.UnmarshalVT(b[:len(b)-8])
194+
return md.UnmarshalVT(b[off : len(b)-8])
193195
}

pkg/experiment/block/metadata/metadata_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/oklog/ulid"
88
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
910

1011
metastorev1 "github.com/grafana/pyroscope/api/gen/proto/go/metastore/v1"
1112
)
@@ -216,3 +217,28 @@ func TestMetadataStrings_Export(t *testing.T) {
216217

217218
assert.Equal(t, expected, md)
218219
}
220+
221+
func TestMetadata_EncodeDecode(t *testing.T) {
222+
md := &metastorev1.BlockMeta{
223+
Id: "1",
224+
Tenant: 0,
225+
CreatedBy: 1,
226+
Datasets: []*metastorev1.Dataset{
227+
{Tenant: 2, Name: 3, Labels: []int32{2, 4, 3, 5, 6, 2, 4, 3, 5, 7, 2, 4, 3, 5, 8}},
228+
{Tenant: 9, Name: 10, Labels: []int32{2, 4, 10, 5, 7, 2, 4, 10, 5, 8, 2, 4, 10, 5, 11}},
229+
},
230+
StringTable: []string{
231+
"", "ingester",
232+
"tenant-a", "dataset-a", "service_name", "__profile_type__", "1", "2", "3",
233+
"tenant-b", "dataset-b", "4",
234+
},
235+
}
236+
237+
var buf bytes.Buffer
238+
require.NoError(t, Encode(&buf, md))
239+
240+
var d metastorev1.BlockMeta
241+
raw := append([]byte("garbage"), buf.Bytes()...)
242+
require.NoError(t, Decode(raw, &d))
243+
assert.Equal(t, md, &d)
244+
}

0 commit comments

Comments
 (0)