Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reset the basemap when the labelsbuilder is reset #11771

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pkg/logql/log/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ type BaseLabelsBuilder struct {
errDetails string

groups []string
baseMap map[string]string
parserKeyHints ParserHint // label key hints for metric queries that allows to limit parser extractions to only this list of labels.
without, noLabels bool
referencedStructuredMetadata bool
Expand All @@ -146,7 +147,6 @@ type BaseLabelsBuilder struct {
// LabelsBuilder is the same as labels.Builder but tailored for this package.
type LabelsBuilder struct {
base labels.Labels
baseMap map[string]string
buf labels.Labels
currentResult LabelsResult
groupedResult LabelsResult
Expand Down Expand Up @@ -211,6 +211,7 @@ func (b *BaseLabelsBuilder) Reset() {
}
b.err = ""
b.errDetails = ""
b.baseMap = nil
b.parserKeyHints.Reset()
}

Expand Down Expand Up @@ -481,9 +482,9 @@ func (b *LabelsBuilder) IntoMap(m map[string]string) {
if !b.hasDel() && !b.hasAdd() && !b.HasErr() {
if b.baseMap == nil {
b.baseMap = b.base.Map()
for k, v := range b.baseMap {
m[k] = v
}
}
for k, v := range b.baseMap {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also moved this out of the guard because consecutive calls to IntoMap turned out to also fail in addition to calls after Reset

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure of this change. I don't know if the intention of the builder and baseMap was that we only build a labelset once and then always return it, or if there are cases where we should allow different label sets to be passed to the same builder without calling Reset. I think only the latter would require these lines to move.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without that change, this test fails:

	t.Run("it can copy the map several times", func(t *testing.T) {
		b := NewBaseLabelsBuilder().ForLabels(lbs, lbs.Hash())

		m := map[string]string{}
		b.IntoMap(m)

		require.Equal(t, map[string]string{
			"namespace": "loki",
			"job":       "us-central1/loki",
			"cluster":   "us-central1",
			"ToReplace": "text",
		}, m)

		m2 := map[string]string{}
		b.IntoMap(m2)
		require.Equal(t, map[string]string{
			"namespace": "loki",
			"job":       "us-central1/loki",
			"cluster":   "us-central1",
			"ToReplace": "text",
		}, m2)
	})

I'd sort of expect to be able to make any number of copies of the map as long as I'm aware of what I'm passing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussed on slack, I had my own code mixed up and was misunderstanding the use case and change here

m[k] = v
}
return
}
Expand Down
58 changes: 58 additions & 0 deletions pkg/logql/log/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,64 @@ func TestLabelsBuilder_LabelsError(t *testing.T) {
require.Equal(t, labels.FromStrings("already", "in"), lbs)
}

func TestLabelsBuilder_IntoMap(t *testing.T) {
strs := []string{
"namespace", "loki",
"job", "us-central1/loki",
"cluster", "us-central1",
"ToReplace", "text",
}
lbs := labels.FromStrings(strs...)

t.Run("it still copies the map after a Reset", func(t *testing.T) {
b := NewBaseLabelsBuilder().ForLabels(lbs, lbs.Hash())

m := map[string]string{}
b.IntoMap(m)

require.Equal(t, map[string]string{
"namespace": "loki",
"job": "us-central1/loki",
"cluster": "us-central1",
"ToReplace": "text",
}, m)

b.Reset()

m2 := map[string]string{}
b.IntoMap(m2)
require.Equal(t, map[string]string{
"namespace": "loki",
"job": "us-central1/loki",
"cluster": "us-central1",
"ToReplace": "text",
}, m2)
})

t.Run("it can copy the map several times", func(t *testing.T) {
b := NewBaseLabelsBuilder().ForLabels(lbs, lbs.Hash())

m := map[string]string{}
b.IntoMap(m)

require.Equal(t, map[string]string{
"namespace": "loki",
"job": "us-central1/loki",
"cluster": "us-central1",
"ToReplace": "text",
}, m)

m2 := map[string]string{}
b.IntoMap(m2)
require.Equal(t, map[string]string{
"namespace": "loki",
"job": "us-central1/loki",
"cluster": "us-central1",
"ToReplace": "text",
}, m2)
})
}

func TestLabelsBuilder_LabelsResult(t *testing.T) {
strs := []string{
"namespace", "loki",
Expand Down
Loading