Skip to content

Commit 2740979

Browse files
authored
Check TestChangesetBatchSize buffer growth without AllocsPerRun (#4426)
`TestChangesetBatchSize` occasionally reports one allocation for the sized batch on the race shard. It measures buffer growth with `testing.AllocsPerRun`, which divides the process-wide malloc count by the number of runs. So allocations made by other goroutines during the measured window count as the code under test, and with only 5 runs a handful of them is enough to round up to 1. Locally the zero check failed 9 of 300 runs under `-race -count=300` and never without `-race`. The test now checks growth directly. It fills a batch sized by `changesetBatchSize` and requires its buffer capacity to be unchanged afterwards (pebble's `Batch.grow` always reallocates to a larger capacity). As the non-vacuity control, a batch whose buffer is one byte smaller than what the fill wrote must grow. Both checks are deterministic, so they run under `-race` as well, which matters because CI runs this package only with `-race`. The control also no longer depends on what the batch pool hands back, which made the old unsized control fail under `-count` > 1. Flaked in: https://github.com/sei-protocol/sei-chain/actions/runs/36553689722/job/109357642903
1 parent 7e5857e commit 2740979

1 file changed

Lines changed: 33 additions & 28 deletions

File tree

‎sei-db/db_engine/pebbledb/mvcc/batch_test.go‎

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/cockroachdb/pebble/v2"
11+
"github.com/cockroachdb/pebble/v2/batchrepr"
1112
"github.com/cockroachdb/pebble/v2/vfs"
1213
"github.com/sei-protocol/sei-chain/sei-db/proto"
1314
"github.com/stretchr/testify/require"
@@ -106,8 +107,8 @@ func TestEncodeMVCC(t *testing.T) {
106107
}
107108

108109
// TestChangesetBatchSize checks that a batch sized by changesetBatchSize takes
109-
// every pair of its changesets without growing its buffer, and that an unsized
110-
// one does grow, so the check is not vacuous.
110+
// every pair of its changesets without growing its buffer, and that a buffer
111+
// one byte smaller does grow, so the check is not vacuous.
111112
func TestChangesetBatchSize(t *testing.T) {
112113
const version = 9
113114
var pairs []*proto.KVPair
@@ -123,37 +124,41 @@ func TestChangesetBatchSize(t *testing.T) {
123124
{Name: "evm", Changeset: proto.ChangeSet{Pairs: pairs[100:]}},
124125
}
125126

126-
fillAllocs := func(t *testing.T, bufSize int) float64 {
127+
fill := func(t *testing.T, b *Batch) {
127128
t.Helper()
128-
db := newMemDB(t)
129-
const runs = 5
130-
batches := make([]*Batch, runs+1) // AllocsPerRun makes one warm-up call
131-
for i := range batches {
132-
b, err := NewBatch(db, version, bufSize, false, "test")
133-
require.NoError(t, err)
134-
batches[i] = b
135-
t.Cleanup(func() { require.NoError(t, b.Close()) })
136-
}
137-
next := 0
138-
return testing.AllocsPerRun(runs, func() {
139-
b := batches[next]
140-
next++
141-
for _, cs := range changesets {
142-
for _, pair := range cs.Changeset.Pairs {
143-
if pair.Value == nil {
144-
require.NoError(t, b.Delete(cs.Name, pair.Key))
145-
} else {
146-
require.NoError(t, b.Set(cs.Name, pair.Key, pair.Value))
147-
}
129+
for _, cs := range changesets {
130+
for _, pair := range cs.Changeset.Pairs {
131+
if pair.Value == nil {
132+
require.NoError(t, b.Delete(cs.Name, pair.Key))
133+
} else {
134+
require.NoError(t, b.Set(cs.Name, pair.Key, pair.Value))
148135
}
149136
}
150-
var versionBz [VersionSize]byte
151-
require.NoError(t, b.pb.Set([]byte(latestVersionKey), versionBz[:], nil))
152-
})
137+
}
138+
var versionBz [VersionSize]byte
139+
require.NoError(t, b.pb.Set([]byte(latestVersionKey), versionBz[:], nil))
140+
}
141+
db := newMemDB(t)
142+
newBatch := func(t *testing.T, bufSize int) *Batch {
143+
t.Helper()
144+
b, err := NewBatch(db, version, bufSize, false, "test")
145+
require.NoError(t, err)
146+
t.Cleanup(func() { require.NoError(t, b.Close()) })
147+
return b
153148
}
154149

155-
require.Zero(t, fillAllocs(t, changesetBatchSize(changesets, version)))
156-
require.Positive(t, fillAllocs(t, 0))
150+
sized := newBatch(t, changesetBatchSize(changesets, version))
151+
sizedCap := cap(sized.pb.Repr())
152+
fill(t, sized)
153+
require.Equal(t, sizedCap, cap(sized.pb.Repr()), "sized batch grew its buffer")
154+
155+
// A buffer one byte short of what the fill wrote must grow.
156+
written := len(sized.pb.Repr())
157+
short := newBatch(t, 0)
158+
require.NoError(t, short.pb.SetRepr(make([]byte, batchrepr.HeaderLen, written-1)))
159+
fill(t, short)
160+
require.Equal(t, written, len(short.pb.Repr()))
161+
require.Greater(t, cap(short.pb.Repr()), written-1)
157162
}
158163

159164
// TestBatchSingleUse checks that a written or closed Batch refuses further use

0 commit comments

Comments
 (0)