diff --git a/pkg/store/tsdb.go b/pkg/store/tsdb.go index e3f53695fd4..abf37f6b3d7 100644 --- a/pkg/store/tsdb.go +++ b/pkg/store/tsdb.go @@ -197,6 +197,7 @@ func (s *TSDBStore) Series(r *storepb.SeriesRequest, srv storepb.Store_SeriesSer Raw: &storepb.Chunk{ Type: storepb.Chunk_Encoding(chk.Chunk.Encoding() - 1), // Proto chunk encoding is one off to TSDB one. Data: chk.Chunk.Bytes(), + Hash: hashChunk(chk.Chunk.Bytes(), r.CalculateChunkChecksums), }, } frameBytesLeft -= c.Size() diff --git a/pkg/store/tsdb_test.go b/pkg/store/tsdb_test.go index ff3d69a5d2b..bbca7f72bf0 100644 --- a/pkg/store/tsdb_test.go +++ b/pkg/store/tsdb_test.go @@ -6,6 +6,7 @@ package store import ( "context" "fmt" + "github.com/cespare/xxhash" "io" "math" "math/rand" @@ -61,6 +62,61 @@ func TestTSDBStore_Info(t *testing.T) { testutil.Equals(t, int64(math.MaxInt64), resp.MaxTime) } +func TestTSDBStore_Series_ChunkChecksum(t *testing.T) { + defer testutil.TolerantVerifyLeak(t) + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + db, err := e2eutil.NewTSDB() + defer func() { testutil.Ok(t, db.Close()) }() + testutil.Ok(t, err) + + tsdbStore := NewTSDBStore(nil, db, component.Rule, labels.FromStrings("region", "eu-west")) + + appender := db.Appender(context.Background()) + + for i := 1; i <= 3; i++ { + _, err = appender.Append(0, labels.FromStrings("a", "1"), int64(i), float64(i)) + testutil.Ok(t, err) + } + err = appender.Commit() + testutil.Ok(t, err) + + srv := newStoreSeriesServer(ctx) + + req := &storepb.SeriesRequest{ + MinTime: 1, + MaxTime: 3, + Matchers: []storepb.LabelMatcher{ + {Type: storepb.LabelMatcher_EQ, Name: "a", Value: "1"}, + }, + } + err = tsdbStore.Series(req, srv) + testutil.Ok(t, err) + + for _, chk := range srv.SeriesSet[0].Chunks { + testutil.Equals(t, uint64(0), chk.Raw.Hash) + } + + req = &storepb.SeriesRequest{ + MinTime: 1, + MaxTime: 3, + Matchers: []storepb.LabelMatcher{ + {Type: storepb.LabelMatcher_EQ, Name: "a", Value: "1"}, + }, + CalculateChunkChecksums: true, + } + + err = tsdbStore.Series(req, srv) + testutil.Ok(t, err) + + for _, chk := range srv.SeriesSet[1].Chunks { + want := xxhash.Sum64(chk.Raw.Data) + testutil.Equals(t, want, chk.Raw.Hash) + } +} + func TestTSDBStore_Series(t *testing.T) { defer testutil.TolerantVerifyLeak(t)