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

[exporter/loadbalancing] Fix memory leaks #31050

Merged
merged 9 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
27 changes: 27 additions & 0 deletions .chloggen/goleak_loadbalancingexp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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. filelogreceiver)
component: loadbalancingexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix memory leaks on shutdown

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [31050]

# (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:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# 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: [user]
1 change: 1 addition & 0 deletions exporter/loadbalancingexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
go.opentelemetry.io/collector/semconv v0.93.1-0.20240202170612-7abb9622312d
go.opentelemetry.io/otel/metric v1.22.0
go.opentelemetry.io/otel/trace v1.22.0
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.26.0
k8s.io/api v0.28.4
Expand Down
1 change: 1 addition & 0 deletions exporter/loadbalancingexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions exporter/loadbalancingexporter/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ func endpointFound(endpoint string, endpoints []string) bool {
return false
}

func (lb *loadBalancerImp) Shutdown(context.Context) error {
func (lb *loadBalancerImp) Shutdown(ctx context.Context) error {
err := lb.res.shutdown(ctx)
lb.stopped = true
return nil
return err
}

func (lb *loadBalancerImp) Endpoint(identifier []byte) string {
Expand Down
1 change: 1 addition & 0 deletions exporter/loadbalancingexporter/loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func TestWithDNSResolverNoEndpoints(t *testing.T) {

err = p.Start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err)
defer func() { assert.NoError(t, p.Shutdown(context.Background())) }()

// test
e := p.Endpoint([]byte{128, 128, 0, 0})
Expand Down
5 changes: 3 additions & 2 deletions exporter/loadbalancingexporter/log_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ func (e *logExporterImp) Start(ctx context.Context, host component.Host) error {
return e.loadBalancer.Start(ctx, host)
}

func (e *logExporterImp) Shutdown(context.Context) error {
func (e *logExporterImp) Shutdown(ctx context.Context) error {
if !e.started {
return nil
}
err := e.loadBalancer.Shutdown(ctx)
e.started = false
e.shutdownWg.Wait()
return nil
return err
}

func (e *logExporterImp) ConsumeLogs(ctx context.Context, ld plog.Logs) error {
Expand Down
5 changes: 3 additions & 2 deletions exporter/loadbalancingexporter/metrics_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ func (e *metricExporterImp) Start(ctx context.Context, host component.Host) erro
return e.loadBalancer.Start(ctx, host)
}

func (e *metricExporterImp) Shutdown(context.Context) error {
func (e *metricExporterImp) Shutdown(ctx context.Context) error {
err := e.loadBalancer.Shutdown(ctx)
e.stopped = true
e.shutdownWg.Wait()
return nil
return err
}

func (e *metricExporterImp) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error {
Expand Down
17 changes: 17 additions & 0 deletions exporter/loadbalancingexporter/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package loadbalancingexporter

import (
"testing"

"go.uber.org/goleak"
)

// The IgnoreTopFunction call prevents catching the leak generated by opencensus
// defaultWorker.Start which at this time is part of the package's init call.
// See https://github.com/census-instrumentation/opencensus-go/issues/1191 for more information.
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"))
}
1 change: 1 addition & 0 deletions exporter/loadbalancingexporter/resolver_dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func TestCantResolve(t *testing.T) {

// verify
assert.NoError(t, err)
assert.NoError(t, res.shutdown(context.Background()))
}

func TestOnChange(t *testing.T) {
Expand Down
13 changes: 12 additions & 1 deletion exporter/loadbalancingexporter/resolver_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type staticResolver struct {
endpoints []string
onChangeCallbacks []func([]string)
once sync.Once // we trigger the onChange only once
mx sync.Mutex
}

func newStaticResolver(endpoints []string) (*staticResolver, error) {
Expand All @@ -49,13 +50,23 @@ func (r *staticResolver) start(ctx context.Context) error {
return err
}

func (r *staticResolver) shutdown(_ context.Context) error {
func (r *staticResolver) shutdown(context.Context) error {
r.endpoints = nil

r.mx.Lock()
defer r.mx.Unlock()
for _, callback := range r.onChangeCallbacks {
callback(r.endpoints)
}
crobert-1 marked this conversation as resolved.
Show resolved Hide resolved

return nil
}

func (r *staticResolver) resolve(ctx context.Context) ([]string, error) {
_ = stats.RecordWithTags(ctx, staticResolverMutators, mNumResolutions.M(1))

r.mx.Lock()
defer r.mx.Unlock()
r.once.Do(func() {
_ = stats.RecordWithTags(ctx, staticResolverMutators, mNumBackends.M(int64(len(r.endpoints))))

Expand Down
5 changes: 3 additions & 2 deletions exporter/loadbalancingexporter/trace_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ func (e *traceExporterImp) Start(ctx context.Context, host component.Host) error
return e.loadBalancer.Start(ctx, host)
}

func (e *traceExporterImp) Shutdown(context.Context) error {
func (e *traceExporterImp) Shutdown(ctx context.Context) error {
err := e.loadBalancer.Shutdown(ctx)
e.stopped = true
e.shutdownWg.Wait()
return nil
return err
}

func (e *traceExporterImp) ConsumeTraces(ctx context.Context, td ptrace.Traces) error {
Expand Down
Loading