From 4d59c483a45f14dc2e015faf4f5aa797005baa42 Mon Sep 17 00:00:00 2001 From: yeya24 Date: Tue, 9 Mar 2021 08:38:00 -0500 Subject: [PATCH] check label existence Signed-off-by: yeya24 --- pkg/compactv2/compactor_test.go | 10 ++++------ pkg/compactv2/modifiers.go | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/pkg/compactv2/compactor_test.go b/pkg/compactv2/compactor_test.go index e5c979a0539..46594c434c0 100644 --- a/pkg/compactv2/compactor_test.go +++ b/pkg/compactv2/compactor_test.go @@ -377,8 +377,6 @@ func TestCompactor_WriteSeries_e2e(t *testing.T) { }, })}, expected: []seriesSamples{ - {lset: labels.Labels{{Name: "a", Value: ""}, {Name: "foo", Value: "bat"}}, - chunks: [][]sample{{{0, 0}, {1, 1}, {2, 2}}, {{10, 11}, {11, 11}, {20, 20}}}}, {lset: labels.Labels{{Name: "a", Value: "1"}}, chunks: [][]sample{{{0, 0}, {1, 1}, {2, 2}}, {{10, 11}, {11, 11}, {20, 20}}}}, {lset: labels.Labels{{Name: "a", Value: "2"}}, @@ -390,11 +388,11 @@ func TestCompactor_WriteSeries_e2e(t *testing.T) { {lset: labels.Labels{{Name: "foo", Value: "bat"}}, chunks: [][]sample{{{0, 0}, {1, 1}, {2, 2}}, {{10, 11}, {11, 11}, {20, 20}}}}, }, - expectedChanges: "Deleted {a=\"2\", foo=\"bar\"} [{0 20}]\nDeleted {a=\"3\", foo=\"baz\"} [{0 20}]\n", + expectedChanges: "Deleted {a=\"\", foo=\"bat\"} [{0 20}]\nDeleted {a=\"2\", foo=\"bar\"} [{0 20}]\nDeleted {a=\"3\", foo=\"baz\"} [{0 20}]\n", expectedStats: tsdb.BlockStats{ - NumSamples: 36, - NumSeries: 6, - NumChunks: 12, + NumSamples: 30, + NumSeries: 5, + NumChunks: 10, }, }, } { diff --git a/pkg/compactv2/modifiers.go b/pkg/compactv2/modifiers.go index aef4ec065ea..43edaea9320 100644 --- a/pkg/compactv2/modifiers.go +++ b/pkg/compactv2/modifiers.go @@ -5,6 +5,7 @@ package compactv2 import ( "github.com/pkg/errors" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/tsdb" "github.com/prometheus/prometheus/tsdb/chunkenc" @@ -61,10 +62,11 @@ SeriesLoop: DeletionsLoop: for _, deletions := range d.d.deletions { for _, m := range deletions.Matchers { - v := lbls.Get(m.Name) + v, exist := getLabelValue(lbls, m.Name) - // Only if all matchers in the deletion request are matched can we proceed to deletion. - if v == "" || !m.Matches(v) { + // We skip when the label doesn't exist or not all + // matchers in the deletion request are matched can we proceed to deletion. + if !exist || !m.Matches(v) { continue DeletionsLoop } } @@ -107,6 +109,17 @@ SeriesLoop: return false } +// getLabelValue get the label value by the given label name and returns +// the label existence by returning a bool. +func getLabelValue(ls labels.Labels, name string) (string, bool) { + for _, l := range ls { + if l.Name == name { + return l.Value, true + } + } + return "", false +} + // intersection returns intersection between interval and range of intervals. func intersection(i tombstones.Interval, dranges tombstones.Intervals) tombstones.Intervals { var ret tombstones.Intervals