Files
a47746c5a7 Switch the dictionaries of dictionary tables only once (#14709)
Right now, when merging profiles, we switch dictionary entries when they
are used.

For example, for each sample, we do:

* Run `switchDictionary` for that sample.
* Switch all the sample indices dictionaries.
* Run `switchDictionary` for the stack associated with that sample.
* Update the sample's stack ID with the one provided by the new
dictionary.

This has a pretty big issue, as when stacks are associated to multiple
samples (which is very common in production workloads, but was missed in
tests), we will try switching the stack's dictionary multiple times.

* Switch the stack's dictionary, works fine.
* Switch it again. Now, we try assigning indices from the source
dictionary when they have meaning in the destination one.

Instead, we now do the following:

* Run `switchDictionary` for the stacks.
* Loop through samples, and run `switchDictionary` for each of them.
* Retrieve the stack. Because we already switched its dictionary, we get
a stack already pointing at the new dictionary.
* Update the sample's stack ID with the one provided by the new
dictionary.

<sub>This investigation and PR were proudly done **without** the use of
any LLM.</sub>

---------

Signed-off-by: Florian Lehner <[email protected]>
Co-authored-by: Florian Lehner <[email protected]>
2026-03-10 09:50:52 +00:00

310 lines
7.0 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package pprofile
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/internal/testutil"
)
func TestMappingEqual(t *testing.T) {
for _, tt := range []struct {
name string
orig Mapping
dest Mapping
want bool
}{
{
name: "empty mappings",
orig: NewMapping(),
dest: NewMapping(),
want: true,
},
{
name: "non-empty identical mappings",
orig: buildMapping(1, 2, 3, 4, []int32{1, 2}),
dest: buildMapping(1, 2, 3, 4, []int32{1, 2}),
want: true,
},
{
name: "with different MemoryStart",
orig: buildMapping(1, 2, 3, 4, []int32{1, 2}),
dest: buildMapping(2, 2, 3, 4, []int32{1, 2}),
want: false,
},
{
name: "with different MemoryLimit",
orig: buildMapping(1, 2, 3, 4, []int32{1, 2}),
dest: buildMapping(1, 3, 3, 4, []int32{1, 2}),
want: false,
},
{
name: "with different FileOffset",
orig: buildMapping(1, 2, 3, 4, []int32{1, 2}),
dest: buildMapping(1, 2, 4, 4, []int32{1, 2}),
want: false,
},
{
name: "with different FilenameStrindex",
orig: buildMapping(1, 2, 3, 4, []int32{1, 2}),
dest: buildMapping(1, 2, 3, 5, []int32{1, 2}),
want: false,
},
{
name: "with different AttributeIndices",
orig: buildMapping(1, 2, 3, 4, []int32{1, 2}),
dest: buildMapping(1, 2, 3, 4, []int32{1, 3}),
want: false,
},
} {
t.Run(tt.name, func(t *testing.T) {
if tt.want {
assert.True(t, tt.orig.Equal(tt.dest))
} else {
assert.False(t, tt.orig.Equal(tt.dest))
}
})
}
}
func TestMappingSwitchDictionary(t *testing.T) {
for _, tt := range []struct {
name string
mapping Mapping
src ProfilesDictionary
dst ProfilesDictionary
wantMapping Mapping
wantDictionary ProfilesDictionary
wantErr error
}{
{
name: "with an empty mapping",
mapping: NewMapping(),
src: NewProfilesDictionary(),
dst: NewProfilesDictionary(),
wantMapping: NewMapping(),
wantDictionary: NewProfilesDictionary(),
},
{
name: "with an existing filename",
mapping: func() Mapping {
m := NewMapping()
m.SetFilenameStrindex(1)
return m
}(),
src: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.StringTable().Append("", "test")
return d
}(),
dst: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.StringTable().Append("", "foo")
return d
}(),
wantMapping: func() Mapping {
m := NewMapping()
m.SetFilenameStrindex(2)
return m
}(),
wantDictionary: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.StringTable().Append("", "foo", "test")
return d
}(),
},
{
name: "with a filename index that does not match anything",
mapping: func() Mapping {
m := NewMapping()
m.SetFilenameStrindex(1)
return m
}(),
src: NewProfilesDictionary(),
dst: NewProfilesDictionary(),
wantMapping: func() Mapping {
m := NewMapping()
m.SetFilenameStrindex(1)
return m
}(),
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid filename index 1"),
},
{
name: "with a filename index equal to the source table length (boundary condition)",
mapping: func() Mapping {
m := NewMapping()
m.SetFilenameStrindex(2)
return m
}(),
src: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.StringTable().Append("", "test")
return d
}(),
dst: NewProfilesDictionary(),
wantMapping: func() Mapping {
m := NewMapping()
m.SetFilenameStrindex(2)
return m
}(),
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid filename index 2"),
},
{
name: "with an existing attribute",
mapping: func() Mapping {
m := NewMapping()
m.AttributeIndices().Append(1)
return m
}(),
src: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.StringTable().Append("", "test")
d.AttributeTable().AppendEmpty()
a := d.AttributeTable().AppendEmpty()
a.SetKeyStrindex(1)
return d
}(),
dst: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.StringTable().Append("", "test")
d.AttributeTable().AppendEmpty()
a := d.AttributeTable().AppendEmpty()
a.SetKeyStrindex(1)
return d
}(),
wantMapping: func() Mapping {
m := NewMapping()
m.AttributeIndices().Append(1)
return m
}(),
wantDictionary: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.StringTable().Append("", "test")
d.AttributeTable().AppendEmpty()
a := d.AttributeTable().AppendEmpty()
a.SetKeyStrindex(1)
return d
}(),
},
{
name: "with an attribute index that does not match anything",
mapping: func() Mapping {
m := NewMapping()
m.AttributeIndices().Append(1)
return m
}(),
src: NewProfilesDictionary(),
dst: NewProfilesDictionary(),
wantMapping: func() Mapping {
m := NewMapping()
m.AttributeIndices().Append(1)
return m
}(),
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid attribute index 1"),
},
{
name: "with an attribute index equal to the source table length (boundary condition)",
mapping: func() Mapping {
m := NewMapping()
m.AttributeIndices().Append(2)
return m
}(),
src: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.AttributeTable().AppendEmpty()
d.AttributeTable().AppendEmpty()
return d
}(),
dst: NewProfilesDictionary(),
wantMapping: func() Mapping {
m := NewMapping()
m.AttributeIndices().Append(2)
return m
}(),
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid attribute index 2"),
},
} {
t.Run(tt.name, func(t *testing.T) {
m := tt.mapping
dst := tt.dst
err := m.switchDictionary(tt.src, dst)
if tt.wantErr == nil {
require.NoError(t, err)
} else {
require.Equal(t, tt.wantErr, err)
}
assert.Equal(t, tt.wantMapping, m)
assert.Equal(t, tt.wantDictionary, dst)
})
}
}
func BenchmarkMappingSwitchDictionary(b *testing.B) {
testutil.SkipMemoryBench(b)
m := NewMapping()
m.AttributeIndices().Append(1, 2)
src := NewProfilesDictionary()
src.StringTable().Append("", "test", "foo")
src.AttributeTable().AppendEmpty()
src.AttributeTable().AppendEmpty().SetKeyStrindex(1)
src.AttributeTable().AppendEmpty().SetKeyStrindex(2)
b.ReportAllocs()
for b.Loop() {
b.StopTimer()
dst := NewProfilesDictionary()
dst.StringTable().Append("", "foo")
dst.AttributeTable().AppendEmpty()
dst.AttributeTable().AppendEmpty().SetKeyStrindex(1)
b.StartTimer()
_ = m.switchDictionary(src, dst)
}
}
func buildMapping(memStart, memLimit, fileOffset uint64, filenameIdx int32, attrIdxs []int32) Mapping {
m := NewMapping()
m.SetMemoryStart(memStart)
m.SetMemoryLimit(memLimit)
m.SetFileOffset(fileOffset)
m.SetFilenameStrindex(filenameIdx)
m.AttributeIndices().FromRaw(attrIdxs)
return m
}