Skip to content

Commit

Permalink
check label existence
Browse files Browse the repository at this point in the history
Signed-off-by: yeya24 <[email protected]>
  • Loading branch information
yeya24 committed Mar 9, 2021
1 parent 33073b3 commit 4d59c48
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
10 changes: 4 additions & 6 deletions pkg/compactv2/compactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
Expand All @@ -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,
},
},
} {
Expand Down
19 changes: 16 additions & 3 deletions pkg/compactv2/modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4d59c48

Please sign in to comment.