From 162a2d0057df2f4952ac2a7bf0a211f9ed9212c4 Mon Sep 17 00:00:00 2001 From: Sandeep Sukhani Date: Wed, 8 Mar 2023 15:25:23 +0530 Subject: [PATCH] deletion: fix issue in processing delete requests with tsdb index (#8665) **What this PR does / why we need it**: When a delete request creates a new chunk from an existing chunk, we index it to the index store. In TSDB, we [drop](https://github.com/grafana/loki/blob/944b488bd2506012d149fc363401f9b7d51af3e7/pkg/storage/stores/tsdb/head_manager.go#L235-L237) `__name__` label, which was being added to chunkrefs for historical reasons. However, this was not done while indexing chunks from the deletion code, causing deletion to fail with the error [chunk insertion is only allowed on existing streams](https://github.com/grafana/loki/blob/619c2212f58c645661c7a004c084b839e91aec1b/pkg/storage/stores/tsdb/builder.go#L67). This PR handles the issue by dropping the `__name__` label and adding relevant tests. **Special notes for your reviewer**: I will add an integration test for deletion with TSDB once we have PR https://github.com/grafana/loki/pull/7754 merged since it does some groundwork to run integration tests with both `boltdb-shipper` and `tsdb`. **Checklist** - [x] Tests updated - [x] `CHANGELOG.md` updated --- CHANGELOG.md | 1 + pkg/storage/stores/tsdb/compactor.go | 7 ++++++- pkg/storage/stores/tsdb/compactor_test.go | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 557df7306b..4bf9cd1192 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ * [8251](https://github.com/grafana/loki/pull/8251) **sandeepsukhani** index-store: fix indexing of chunks overlapping multiple schemas. * [8151](https://github.com/grafana/loki/pull/8151) **sandeepsukhani** fix log deletion with line filters. * [8448](https://github.com/grafana/loki/pull/8448) **chaudum**: Fix bug in LogQL parser that caused certain queries that contain a vector expression to fail. +* [8448](https://github.com/grafana/loki/pull/8665) **sandeepsukhani**: deletion: fix issue in processing delete requests with tsdb index ##### Changes diff --git a/pkg/storage/stores/tsdb/compactor.go b/pkg/storage/stores/tsdb/compactor.go index f3862282a4..472074a76c 100644 --- a/pkg/storage/stores/tsdb/compactor.go +++ b/pkg/storage/stores/tsdb/compactor.go @@ -355,7 +355,12 @@ func (c *compactedIndex) ToIndexFile() (index_shipper.Index, error) { c.deleteChunks = nil for _, chk := range c.indexChunks { - err := c.builder.InsertChunk(chk.Metric.String(), index.ChunkMeta{ + // TSDB doesnt need the __name__="log" convention the old chunk store index used. + b := labels.NewBuilder(chk.Metric) + b.Del(labels.MetricName) + ls := b.Labels(nil) + + err := c.builder.InsertChunk(ls.String(), index.ChunkMeta{ Checksum: chk.Checksum, MinTime: int64(chk.From), MaxTime: int64(chk.Through), diff --git a/pkg/storage/stores/tsdb/compactor_test.go b/pkg/storage/stores/tsdb/compactor_test.go index 4b7edfd74a..2e1f99789b 100644 --- a/pkg/storage/stores/tsdb/compactor_test.go +++ b/pkg/storage/stores/tsdb/compactor_test.go @@ -691,6 +691,24 @@ func TestCompactedIndex(t *testing.T) { testCtx.lbls2.String(): buildChunkMetas(testCtx.shiftTableStart(0), testCtx.shiftTableStart(20)), }, }, + "__name__ label should get dropped while indexing chunks": { + addChunks: []chunk.Chunk{ + { + Metric: labels.NewBuilder(testCtx.lbls1).Set(labels.MetricName, "log").Labels(nil), + ChunkRef: chunkMetaToChunkRef(testCtx.userID, buildChunkMetas(testCtx.shiftTableStart(11), testCtx.shiftTableStart(11))[0], testCtx.lbls1), + Data: dummyChunkData{}, + }, + { + Metric: testCtx.lbls1, + ChunkRef: chunkMetaToChunkRef(testCtx.userID, buildChunkMetas(testCtx.shiftTableStart(12), testCtx.shiftTableStart(12))[0], testCtx.lbls1), + Data: dummyChunkData{}, + }, + }, + finalExpectedChunks: map[string]index.ChunkMetas{ + testCtx.lbls1.String(): buildChunkMetas(testCtx.shiftTableStart(0), testCtx.shiftTableStart(12)), + testCtx.lbls2.String(): buildChunkMetas(testCtx.shiftTableStart(0), testCtx.shiftTableStart(20)), + }, + }, "add some chunks out of table interval to a stream": { addChunks: []chunk.Chunk{ {