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

invariants: fix non-inlined empty function call #4209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion internal/invalidating/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// MaybeWrapIfInvariants wraps some iterators with an invalidating iterator.
// MaybeWrapIfInvariants does nothing in non-invariant builds.
func MaybeWrapIfInvariants(iter base.InternalIterator) base.InternalIterator {
if invariants.Sometimes(10) {
if invariants.Enabled && invariants.Sometimes(10) {
return NewIter(iter)
}
return iter
Expand Down
11 changes: 2 additions & 9 deletions internal/invariants/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package invariants

import (
"math/rand/v2"
"runtime"

"github.com/cockroachdb/pebble/internal/buildtags"
Expand All @@ -22,13 +21,6 @@ const Enabled = buildtags.Race || buildtags.Invariants
// RaceEnabled is true if we were built with the "race" build tag.
const RaceEnabled = buildtags.Race

// Sometimes returns true percent% of the time if invariants are Enabled (i.e.
// we were built with the "invariants" or "race" build tags). Otherwise, always
// returns false.
func Sometimes(percent int) bool {
return Enabled && rand.Uint32N(100) < uint32(percent)
}

// UseFinalizers is true if we want to use finalizers for assertions around
// object lifetime and cleanup. This happens when the invariants or tracing tags
// are set, but we exclude race builds because we historically ran into some
Expand All @@ -43,7 +35,8 @@ const UseFinalizers = !buildtags.Race && (buildtags.Invariants || buildtags.Trac
// bugs related to finalizers.
//
// This function is a no-op if UseFinalizers is false and it should inline to
// nothing.
// nothing. However, note that it might not inline so in very hot paths it's
// best to check UseFinalizers first.
func SetFinalizer(obj, finalizer interface{}) {
if UseFinalizers {
runtime.SetFinalizer(obj, finalizer)
Expand Down
7 changes: 7 additions & 0 deletions internal/invariants/off.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

package invariants

// Sometimes returns true percent% of the time if invariants are Enabled (i.e.
// we were built with the "invariants" or "race" build tags). Otherwise, always
// returns false.
func Sometimes(percent int) bool {
return false
}

// CloseChecker is used to check that objects are closed exactly once. It is
// empty and does nothing in non-invariant builds.
//
Expand Down
9 changes: 9 additions & 0 deletions internal/invariants/on.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

package invariants

import "math/rand/v2"

// Sometimes returns true percent% of the time if invariants are Enabled (i.e.
// we were built with the "invariants" or "race" build tags). Otherwise, always
// returns false.
func Sometimes(percent int) bool {
return rand.Uint32N(100) < uint32(percent)
}

// CloseChecker is used to check that objects are closed exactly once. It is
// empty and does nothing in non-invariant builds.
//
Expand Down
29 changes: 16 additions & 13 deletions objstorage/objstorageprovider/vfs_readable.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ func newFileReadable(
fs: fs,
readaheadConfig: readaheadConfig,
}
invariants.SetFinalizer(r, func(obj interface{}) {
if obj.(*fileReadable).file != nil {
fmt.Fprintf(os.Stderr, "Readable was not closed")
os.Exit(1)
}
})
if invariants.UseFinalizers {
invariants.SetFinalizer(r, func(obj interface{}) {
if obj.(*fileReadable).file != nil {
fmt.Fprintf(os.Stderr, "Readable was not closed")
os.Exit(1)
}
})
}
return r, nil
}

Expand Down Expand Up @@ -104,13 +106,14 @@ var _ objstorage.ReadHandle = (*vfsReadHandle)(nil)
var readHandlePool = sync.Pool{
New: func() interface{} {
i := &vfsReadHandle{}
// Note: this is a no-op if invariants are disabled or race is enabled.
invariants.SetFinalizer(i, func(obj interface{}) {
if obj.(*vfsReadHandle).r != nil {
fmt.Fprintf(os.Stderr, "ReadHandle was not closed")
os.Exit(1)
}
})
if invariants.UseFinalizers {
invariants.SetFinalizer(i, func(obj interface{}) {
if obj.(*vfsReadHandle).r != nil {
fmt.Fprintf(os.Stderr, "ReadHandle was not closed")
os.Exit(1)
}
})
}
return i
},
}
Expand Down
14 changes: 8 additions & 6 deletions sstable/colblk/keyspan.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,14 @@ func NewKeyspanIter(
var keyspanIterPool = sync.Pool{
New: func() interface{} {
i := &KeyspanIter{}
invariants.SetFinalizer(i, func(obj interface{}) {
if i := obj.(*KeyspanIter); i.handle.Valid() {
fmt.Fprintf(os.Stderr, "KeyspanIter.handle is not nil: %#v\n", i.handle)
os.Exit(1)
}
})
if invariants.UseFinalizers {
invariants.SetFinalizer(i, func(obj interface{}) {
if i := obj.(*KeyspanIter); i.handle.Valid() {
fmt.Fprintf(os.Stderr, "KeyspanIter.handle is not nil: %#v\n", i.handle)
os.Exit(1)
}
})
}
return i
},
}
Expand Down
4 changes: 2 additions & 2 deletions sstable/colblk/uints.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (b *UintBuilder) Reset() {
b.stats.maximum = 0
// We could reset all values as a precaution, but it has a visible cost
// in benchmarks.
if invariants.Sometimes(50) {
if invariants.Enabled && invariants.Sometimes(50) {
for i := 0; i < b.array.n; i++ {
b.array.elems.set(i, math.MaxUint64)
}
Expand Down Expand Up @@ -302,7 +302,7 @@ func (b *UintBuilder) determineEncoding(rows int) (_ UintEncoding, deltaBase uin
// Note that b.stats.minimum includes all rows set so far so it might be
// strictly smaller than all values up to [rows]; but it is still a suitable
// base for b.stats.encoding.
if invariants.Sometimes(1) && rows > 0 {
if invariants.Enabled && invariants.Sometimes(1) && rows > 0 {
if enc, _ := b.recalculateEncoding(rows); enc != b.stats.encoding {
panic(fmt.Sprintf("fast and slow paths don't agree: %s vs %s", b.stats.encoding, enc))
}
Expand Down
20 changes: 12 additions & 8 deletions sstable/reader_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,18 @@ func init() {
singleLevelIterRowBlockPool = sync.Pool{
New: func() interface{} {
i := &singleLevelIteratorRowBlocks{pool: &singleLevelIterRowBlockPool}
// Note: this is a no-op if invariants are disabled or race is enabled.
invariants.SetFinalizer(i, checkSingleLevelIterator[rowblk.IndexIter, *rowblk.IndexIter, rowblk.Iter, *rowblk.Iter])
if invariants.UseFinalizers {
invariants.SetFinalizer(i, checkSingleLevelIterator[rowblk.IndexIter, *rowblk.IndexIter, rowblk.Iter, *rowblk.Iter])
}
return i
},
}
twoLevelIterRowBlockPool = sync.Pool{
New: func() interface{} {
i := &twoLevelIteratorRowBlocks{pool: &twoLevelIterRowBlockPool}
// Note: this is a no-op if invariants are disabled or race is enabled.
invariants.SetFinalizer(i, checkTwoLevelIterator[rowblk.IndexIter, *rowblk.IndexIter, rowblk.Iter, *rowblk.Iter])
if invariants.UseFinalizers {
invariants.SetFinalizer(i, checkTwoLevelIterator[rowblk.IndexIter, *rowblk.IndexIter, rowblk.Iter, *rowblk.Iter])
}
return i
},
}
Expand All @@ -173,8 +175,9 @@ func init() {
i := &singleLevelIteratorColumnBlocks{
pool: &singleLevelIterColumnBlockPool,
}
// Note: this is a no-op if invariants are disabled or race is enabled.
invariants.SetFinalizer(i, checkSingleLevelIterator[colblk.IndexIter, *colblk.IndexIter, colblk.DataBlockIter, *colblk.DataBlockIter])
if invariants.UseFinalizers {
invariants.SetFinalizer(i, checkSingleLevelIterator[colblk.IndexIter, *colblk.IndexIter, colblk.DataBlockIter, *colblk.DataBlockIter])
}
return i
},
}
Expand All @@ -183,8 +186,9 @@ func init() {
i := &twoLevelIteratorColumnBlocks{
pool: &twoLevelIterColumnBlockPool,
}
// Note: this is a no-op if invariants are disabled or race is enabled.
invariants.SetFinalizer(i, checkTwoLevelIterator[colblk.IndexIter, *colblk.IndexIter, colblk.DataBlockIter, *colblk.DataBlockIter])
if invariants.UseFinalizers {
invariants.SetFinalizer(i, checkTwoLevelIterator[colblk.IndexIter, *colblk.IndexIter, colblk.DataBlockIter, *colblk.DataBlockIter])
}
return i
},
}
Expand Down
5 changes: 3 additions & 2 deletions sstable/rowblk/rowblk_fragment_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ var _ keyspan.FragmentIterator = (*fragmentIter)(nil)
var fragmentBlockIterPool = sync.Pool{
New: func() interface{} {
i := &fragmentIter{}
// Note: this is a no-op if invariants are disabled or race is enabled.
invariants.SetFinalizer(i, checkFragmentBlockIterator)
if invariants.UseFinalizers {
invariants.SetFinalizer(i, checkFragmentBlockIterator)
}
return i
},
}
Expand Down
Loading