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

[interna/sharedcomponent] Use a ring buffer to restrict remembered status count #11826

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
25 changes: 25 additions & 0 deletions .chloggen/fix-sharedcomponent-memory-issue.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: internal/sharedcomponent

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fixed bug where sharedcomponent would use too much memory remembering all the previously reported statuses

# One or more tracking issues or pull requests related to the change
issues: [11826]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
29 changes: 21 additions & 8 deletions internal/sharedcomponent/sharedcomponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package sharedcomponent // import "go.opentelemetry.io/collector/internal/sharedcomponent"

import (
"container/ring"
"context"
"slices"
"sync"

"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -77,7 +77,7 @@
c.hostWrapper = &hostWrapper{
host: host,
sources: make([]componentstatus.Reporter, 0),
previousEvents: make([]*componentstatus.Event, 0),
previousEvents: ring.New(5),
}
statusReporter, isStatusReporter := host.(componentstatus.Reporter)
if isStatusReporter {
Expand Down Expand Up @@ -108,19 +108,30 @@
type hostWrapper struct {
host component.Host
sources []componentstatus.Reporter
previousEvents []*componentstatus.Event
previousEvents *ring.Ring
lock sync.Mutex
}

func (h *hostWrapper) GetExtensions() map[component.ID]component.Component {
return h.host.GetExtensions()
}

func (h *hostWrapper) hasEvent(e *componentstatus.Event) bool {
hasEvent := false
h.previousEvents.Do(func(a any) {
if previousEvent, ok := a.(*componentstatus.Event); ok && previousEvent == e {
bogdandrutu marked this conversation as resolved.
Show resolved Hide resolved
hasEvent = true
}

Check warning on line 124 in internal/sharedcomponent/sharedcomponent.go

View check run for this annotation

Codecov / codecov/patch

internal/sharedcomponent/sharedcomponent.go#L123-L124

Added lines #L123 - L124 were not covered by tests
})
return hasEvent
}

func (h *hostWrapper) Report(e *componentstatus.Event) {
// Only remember an event if it will be emitted and it has not been sent already.
h.lock.Lock()
if len(h.sources) > 0 && !slices.Contains(h.previousEvents, e) {
h.previousEvents = append(h.previousEvents, e)
if len(h.sources) > 0 && !h.hasEvent(e) {
h.previousEvents.Value = e
h.previousEvents = h.previousEvents.Next()
}
h.lock.Unlock()

Expand All @@ -133,9 +144,11 @@

func (h *hostWrapper) addSource(s componentstatus.Reporter) {
h.lock.Lock()
for _, e := range h.previousEvents {
s.Report(e)
}
h.previousEvents.Do(func(a any) {
if e, ok := a.(*componentstatus.Event); ok {
s.Report(e)
}
})
h.lock.Unlock()

h.lock.Lock()
Expand Down
Loading