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

cloud: test server detects leaked storage instances #135226

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
6 changes: 5 additions & 1 deletion pkg/cloud/cloudtestutils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "cloudtestutils",
srcs = ["cloud_test_helpers.go"],
srcs = [
"cloud_test_helpers.go",
"leak_detector_storage_factory.go",
],
importpath = "github.com/cockroachdb/cockroach/pkg/cloud/cloudtestutils",
visibility = ["//visibility:public"],
deps = [
Expand All @@ -14,6 +17,7 @@ go_library(
"//pkg/settings/cluster",
"//pkg/sql/isql",
"//pkg/testutils",
"//pkg/util/closetest",
"//pkg/util/ioctx",
"//pkg/util/randutil",
"//pkg/util/sysutil",
Expand Down
39 changes: 39 additions & 0 deletions pkg/cloud/cloudtestutils/leak_detector_storage_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cloudtestutils

import (
"github.com/cockroachdb/cockroach/pkg/cloud"
"github.com/cockroachdb/cockroach/pkg/util/closetest"
)

type StorageLeakDetector struct {
tracker *closetest.AllocationTracker
}

func NewStorageLeakDetector() *StorageLeakDetector {
return &StorageLeakDetector { closetest.NewTracker("cloud.ExternalStorage") }
}

func (s *StorageLeakDetector) Check() error {
return s.tracker.CheckLeaks()
}

func (s *StorageLeakDetector) Wrap(storage cloud.ExternalStorage) cloud.ExternalStorage {
if storage == nil {
return nil
}
return &wrappedStorage{
storage,
s.tracker.TrackAllocation(3),
}
}

type wrappedStorage struct {
cloud.ExternalStorage
allocation *closetest.Allocation
}

// Close implements cloud.ExternalStorage.
func (w *wrappedStorage) Close() error {
w.allocation.Close()
return w.ExternalStorage.Close()
}
1 change: 1 addition & 0 deletions pkg/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ go_library(
"//pkg/build",
"//pkg/cloud",
"//pkg/cloud/cloudpb",
"//pkg/cloud/cloudtestutils",
"//pkg/cloud/externalconn",
"//pkg/clusterversion",
"//pkg/config",
Expand Down
18 changes: 17 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/blobs"
"github.com/cockroachdb/cockroach/pkg/blobs/blobspb"
"github.com/cockroachdb/cockroach/pkg/cloud"
"github.com/cockroachdb/cockroach/pkg/cloud/cloudpb"
"github.com/cockroachdb/cockroach/pkg/cloud/cloudtestutils"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/inspectz"
Expand Down Expand Up @@ -671,9 +674,22 @@ func NewServer(cfg Config, stopper *stop.Stopper) (serverctl.ServerStartupInterf

// Create an ExternalStorageBuilder. This is only usable after Start() where
// we initialize all the configuration params.
leakChecker := cloudtestutils.NewStorageLeakDetector()
externalStorageBuilder := &externalStorageBuilder{}
externalStorage := externalStorageBuilder.makeExternalStorage
externalStorage := func(ctx context.Context, dest cloudpb.ExternalStorage, opts ...cloud.ExternalStorageOption) (cloud.ExternalStorage, error) {
storage, err := externalStorageBuilder.makeExternalStorage(ctx, dest, opts...)
return leakChecker.Wrap(storage), err
}
externalStorageFromURI := externalStorageBuilder.makeExternalStorageFromURI
externalStorageFromURI = func(ctx context.Context, uri string, user username.SQLUsername, opts ...cloud.ExternalStorageOption) (cloud.ExternalStorage, error) {
storage, err := externalStorageBuilder.makeExternalStorageFromURI(ctx, uri, user, opts...)
return leakChecker.Wrap(storage), err
}
stopper.AddCloser(stop.CloserFn(func() {
if err := leakChecker.Check(); err != nil {
panic(err)
}
}))

protectedtsKnobs, _ := cfg.TestingKnobs.ProtectedTS.(*protectedts.TestingKnobs)
protectedtsProvider, err := ptprovider.New(ptprovider.Config{
Expand Down
1 change: 1 addition & 0 deletions pkg/testutils/serverutils/test_server_shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ type TestFataler interface {
Fatalf(format string, args ...interface{})
Errorf(format string, args ...interface{})
FailNow()
Cleanup(func())
}

// StartServerOnlyE is like StartServerOnly() but it lets
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/closetest/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "closetest",
srcs = ["close_tracker.go"],
importpath = "github.com/cockroachdb/cockroach/pkg/util/closetest",
visibility = ["//visibility:public"],
deps = ["@com_github_cockroachdb_errors//:errors"],
)
95 changes: 95 additions & 0 deletions pkg/util/closetest/close_tracker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package closetest

import (
"fmt"
"path"
"runtime"
"strings"
"sync"

"github.com/cockroachdb/errors"
)

type AllocationTracker struct {
name string
mu struct {
sync.Mutex
total int
allocations map[int]*Allocation
}
}

func NewTracker(name string) *AllocationTracker {
tracker := &AllocationTracker{
name: name,
}
tracker.mu.allocations = map[int]*Allocation{}
return tracker
}

func (a *AllocationTracker) CheckLeaks() error {
a.mu.Lock()
defer a.mu.Unlock()

if len(a.mu.allocations) == 0 {
return nil
}

leakingStacks := map[stack]int {}
for _, leakedAllocation := range a.mu.allocations {
leakingStacks[leakedAllocation.stack] = leakingStacks[leakedAllocation.stack] + 1
}

var builder strings.Builder
for leakedStack, count := range leakingStacks {
fmt.Fprintf(&builder, "found %d copies of stack: \n", count)

frames := runtime.CallersFrames(leakedStack.callers[:leakedStack.size])
for index := 0; ; index++ {
frame, more := frames.Next()
fmt.Fprintf(&builder, "%d: %s %s:%d\n", index, path.Base(frame.Function), path.Base(frame.File), frame.Line)
if !more {
break
}
}
}

return errors.Newf(
"leaked %d of %d %s instances from %d unique allocation stacks:\n%s",
len(a.mu.allocations), a.mu.total, a.name, len(leakingStacks), builder.String())
}

func (a *AllocationTracker) TrackAllocation(ignoreCallers int) *Allocation {
a.mu.Lock()
defer a.mu.Unlock()

id := a.mu.total
a.mu.total += 1

allocation := &Allocation {
tracker: a,
id: id,
}
allocation.stack.size = runtime.Callers(ignoreCallers + 1, allocation.stack.callers[:])

a.mu.allocations[id] = allocation

return allocation
}

type stack struct {
callers [32]uintptr
size int
}

type Allocation struct {
tracker *AllocationTracker
stack
id int
}

func (a *Allocation) Close() {
a.tracker.mu.Lock()
defer a.tracker.mu.Unlock()
delete(a.tracker.mu.allocations, a.id)
}
Loading