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

72 lines
1.6 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package internal // import "go.opentelemetry.io/collector/pdata/internal"
import (
"sync/atomic"
)
// State defines an ownership state of pmetric.Metrics, plog.Logs, ptrace.Traces or pprofile.Profiles.
type State struct {
refs atomic.Int32
state uint32
}
const (
defaultState uint32 = 0
stateReadOnlyBit = uint32(1 << 0)
statePipelineOwnedBit = uint32(1 << 1)
)
func NewState() *State {
st := &State{
state: defaultState,
}
st.refs.Store(1)
return st
}
func (st *State) MarkReadOnly() {
st.state |= stateReadOnlyBit
}
func (st *State) IsReadOnly() bool {
return st.state&stateReadOnlyBit != 0
}
// AssertMutable panics if the state is not StateMutable.
func (st *State) AssertMutable() {
if st.state&stateReadOnlyBit != 0 {
panic("invalid access to shared data")
}
}
// MarkPipelineOwned marks the data as owned by the pipeline, returns true if the data were
// previously not owned by the pipeline, otherwise false.
func (st *State) MarkPipelineOwned() bool {
if st.state&statePipelineOwnedBit != 0 {
return false
}
st.state |= statePipelineOwnedBit
return true
}
// Ref add one to the count of active references.
func (st *State) Ref() {
st.refs.Add(1)
}
// Unref returns true if reference count got to 0 which means no more active references,
// otherwise it returns false.
func (st *State) Unref() bool {
refs := st.refs.Add(-1)
switch {
case refs > 0:
return false
case refs == 0:
return true
default:
panic("Cannot unref freed data")
}
}