-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
cmd_refresh_range_bench_test.go
308 lines (284 loc) · 9.88 KB
/
cmd_refresh_range_bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package batcheval_test
import (
"context"
"fmt"
"math/rand"
"os"
"path/filepath"
"testing"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/batcheval"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
"github.com/cockroachdb/errors/oserror"
"github.com/cockroachdb/pebble/vfs"
"github.com/stretchr/testify/require"
)
// BenchmarkRefreshRange benchmarks ranged refresh requests with different LSM
// shapes and refresh windows. It was heavily adapted from BenchmarkCatchUpScan,
// which was itself heavily adapted from code in pkg/storage.
func BenchmarkRefreshRange(b *testing.B) {
defer log.Scope(b).Close(b)
numKeys := 1_000_000
valueBytes := 64
dataOpts := map[string]benchDataOptions{
// linear-keys is one of our best-case scenarios. In
// this case, each newly written row is at a key
// following the previously written row and at a later
// timestamp. Further, once compacted, all of the SSTs
// should be in L5 and L6. As a result, the time-based
// optimization can exclude SSTs fairly easily.
"linear-keys": {
numKeys: numKeys,
valueBytes: valueBytes,
},
// random-keys is our worst case. We write keys in
// random order but with timestamps that keep marching
// forward. Once compacted, most of the data is in L5
// and L6. So, we have very few overlapping SSTs and
// most SSTs in our lower level will have at least 1
// key that needs to be included in our scan, despite
// the time based optimization.
"random-keys": {
randomKeyOrder: true,
numKeys: numKeys,
valueBytes: valueBytes,
},
// mixed-case is a middling case.
//
// This case is trying to simulate a larger store, but
// with fewer bytes. If we did not reduce
// LBaseMaxBytes, almost all data would be in Lbase or
// L6, and TBI would be ineffective. By reducing
// LBaseMaxBytes, the data should spread out over more
// levels, like in a real store. The LSM state
// depicted below shows that this was only partially
// successful.
//
// We return a read only engine to prevent read-based
// compactions after the initial data generation.
"mixed-case": {
randomKeyOrder: true,
numKeys: numKeys,
valueBytes: valueBytes,
readOnlyEngine: true,
lBaseMaxBytes: 256,
},
}
for name, do := range dataOpts {
b.Run(name, func(b *testing.B) {
tsPercents := []float64{0.0, 0.50, 0.75, 0.95, 0.99}
for _, refreshFrom := range tsPercents {
for _, refreshTo := range tsPercents {
if refreshTo < refreshFrom {
continue
}
name := fmt.Sprintf("refresh_window=[%2.2f,%2.2f]", refreshFrom*100, refreshTo*100)
b.Run(name, func(b *testing.B) {
tsForPercent := func(p float64) hlc.Timestamp {
walltime := int64(5 * (float64(numKeys)*p + 1)) // see setupData
return hlc.Timestamp{WallTime: walltime}
}
runRefreshRangeBenchmark(b, setupMVCCPebble, benchOptions{
refreshFrom: tsForPercent(refreshFrom), // exclusive
refreshTo: tsForPercent(refreshTo).Next(), // inclusive
dataOpts: do,
})
})
}
}
})
}
}
func runRefreshRangeBenchmark(b *testing.B, emk engineMaker, opts benchOptions) {
ctx := context.Background()
eng, _ := setupData(ctx, b, emk, opts.dataOpts)
defer eng.Close()
st := cluster.MakeTestingClusterSettings()
evalCtx := (&batcheval.MockEvalCtx{ClusterSettings: st}).EvalContext()
startKey := roachpb.Key(encoding.EncodeUvarintAscending([]byte("key-"), uint64(0)))
endKey := roachpb.Key(encoding.EncodeUvarintAscending([]byte("key-"), uint64(opts.dataOpts.numKeys)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
func() {
var resp roachpb.RefreshRangeResponse
_, err := batcheval.RefreshRange(ctx, eng, batcheval.CommandArgs{
EvalCtx: evalCtx,
Args: &roachpb.RefreshRangeRequest{
RequestHeader: roachpb.RequestHeader{
Key: startKey,
EndKey: endKey,
},
RefreshFrom: opts.refreshFrom,
},
Header: roachpb.Header{
Txn: &roachpb.Transaction{
TxnMeta: enginepb.TxnMeta{
WriteTimestamp: opts.refreshTo,
},
ReadTimestamp: opts.refreshTo,
},
Timestamp: opts.refreshTo,
},
}, &resp)
// If the refresh window was empty, we expect the refresh to scan the
// entire span and succeed. Otherwise, it will short-circuit as soon
// as it hits a conflict and return an error.
emptyWindow := opts.refreshTo.Equal(opts.refreshFrom.Next())
if emptyWindow {
require.NoError(b, err)
} else {
require.Error(b, err)
require.Regexp(b, "encountered recently written committed value", err)
}
}()
}
}
type benchDataOptions struct {
numKeys int
valueBytes int
randomKeyOrder bool
readOnlyEngine bool
lBaseMaxBytes int64
}
type benchOptions struct {
refreshFrom hlc.Timestamp
refreshTo hlc.Timestamp
dataOpts benchDataOptions
}
type engineMaker func(testing.TB, string, int64, bool) storage.Engine
func setupMVCCPebble(b testing.TB, dir string, lBaseMaxBytes int64, readOnly bool) storage.Engine {
opts := storage.DefaultPebbleOptions()
opts.FS = vfs.Default
opts.LBaseMaxBytes = lBaseMaxBytes
opts.ReadOnly = readOnly
peb, err := storage.NewPebble(
context.Background(),
storage.PebbleConfig{
StorageConfig: base.StorageConfig{Dir: dir, Settings: cluster.MakeTestingClusterSettings()},
Opts: opts,
})
if err != nil {
b.Fatalf("could not create new pebble instance at %s: %+v", dir, err)
}
return peb
}
// setupData data writes numKeys keys. One version of each key
// is written. The write timestamp starts at 5ns and then in 5ns
// increments. This allows scans at various times, starting at t=5ns,
// and continuing to t=5ns*(numKeys+1). The goal of this is to
// approximate an append-only type workload.
//
// A read-only engine can be returned if opts.readOnlyEngine is
// set. The goal of this is to prevent read-triggered compactions that
// might change the distribution of data across levels.
//
// The creation of the database is time consuming, especially for
// larger numbers of versions. The database is persisted between runs
// and stored in the current directory.
func setupData(
ctx context.Context, b *testing.B, emk engineMaker, opts benchDataOptions,
) (storage.Engine, string) {
// Include the current version in the fixture name, or we may inadvertently
// run against a left-over fixture that is no longer supported.
verStr := fmt.Sprintf("v%s", clusterversion.TestingBinaryVersion.String())
orderStr := "linear"
if opts.randomKeyOrder {
orderStr = "random"
}
readOnlyStr := ""
if opts.readOnlyEngine {
readOnlyStr = "_readonly"
}
loc := fmt.Sprintf("refresh_range_bench_data_%s_%s%s_%d_%d_%d",
verStr, orderStr, readOnlyStr, opts.numKeys, opts.valueBytes, opts.lBaseMaxBytes)
exists := true
if _, err := os.Stat(loc); oserror.IsNotExist(err) {
exists = false
} else if err != nil {
b.Fatal(err)
}
absPath, err := filepath.Abs(loc)
if err != nil {
absPath = loc
}
if exists {
log.Infof(ctx, "using existing refresh range benchmark data: %s", absPath)
testutils.ReadAllFiles(filepath.Join(loc, "*"))
return emk(b, loc, opts.lBaseMaxBytes, opts.readOnlyEngine), loc
}
eng := emk(b, loc, opts.lBaseMaxBytes, false)
log.Infof(ctx, "creating refresh range benchmark data: %s", absPath)
// Generate the same data every time.
rng := rand.New(rand.NewSource(1449168817))
keys := make([]roachpb.Key, opts.numKeys)
order := make([]int, 0, opts.numKeys)
for i := 0; i < opts.numKeys; i++ {
keys[i] = encoding.EncodeUvarintAscending([]byte("key-"), uint64(i))
order = append(order, i)
}
if opts.randomKeyOrder {
rng.Shuffle(len(order), func(i, j int) {
order[i], order[j] = order[j], order[i]
})
}
writeKey := func(batch storage.Batch, idx int, pos int) {
key := keys[idx]
value := roachpb.MakeValueFromBytes(randutil.RandBytes(rng, opts.valueBytes))
value.InitChecksum(key)
ts := hlc.Timestamp{WallTime: int64((pos + 1) * 5)}
if err := storage.MVCCPut(ctx, batch, nil /* ms */, key, ts, hlc.ClockTimestamp{}, value, nil); err != nil {
b.Fatal(err)
}
}
batch := eng.NewBatch()
for i, idx := range order {
// Output the keys in ~20 batches. If we used a single batch to output all
// of the keys rocksdb would create a single sstable. We want multiple
// sstables in order to exercise filtering of which sstables are examined
// during iterator seeking. We fix the number of batches we output so that
// optimizations which change the data size result in the same number of
// sstables.
if scaled := len(order) / 20; i > 0 && (i%scaled) == 0 {
log.Infof(ctx, "committing (%d/~%d) (%d/%d)", i/scaled, 20, i, len(order))
if err := batch.Commit(false /* sync */); err != nil {
b.Fatal(err)
}
batch.Close()
batch = eng.NewBatch()
if err := eng.Flush(); err != nil {
b.Fatal(err)
}
}
writeKey(batch, idx, i)
}
if err := batch.Commit(false /* sync */); err != nil {
b.Fatal(err)
}
batch.Close()
if err := eng.Flush(); err != nil {
b.Fatal(err)
}
if opts.readOnlyEngine {
eng.Close()
eng = emk(b, loc, opts.lBaseMaxBytes, opts.readOnlyEngine)
}
return eng, loc
}