forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompaction_value_separation_test.go
More file actions
305 lines (298 loc) · 8.67 KB
/
compaction_value_separation_test.go
File metadata and controls
305 lines (298 loc) · 8.67 KB
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
// Copyright 2025 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package pebble
import (
"testing"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/internal/manifest"
"github.com/cockroachdb/pebble/sstable"
"github.com/stretchr/testify/require"
)
// makeTestTableMeta creates a TableMetadata suitable for testing
// shouldWriteBlobFiles. It initializes the physical backing and populates
// properties relevant to value separation.
func makeTestTableMeta(
cmp base.Compare,
tableNum base.TableNum,
smallest, largest base.InternalKey,
blobRefDepth manifest.BlobReferenceDepth,
valSepMinSize uint64,
valSepBySuffixDisabled bool,
) *manifest.TableMetadata {
m := &manifest.TableMetadata{TableNum: tableNum, Size: 1024}
m.BlobReferenceDepth = blobRefDepth
m.ExtendPointKeyBounds(cmp, smallest, largest)
m.InitPhysicalBacking()
m.TableBacking.PopulateProperties(&sstable.Properties{
ValueSeparationMinSize: valSepMinSize,
ValueSeparationBySuffixDisabled: valSepBySuffixDisabled,
})
return m
}
func TestShouldWriteBlobFiles(t *testing.T) {
cmp := base.DefaultComparer.Compare
ikey := func(userKey string, seqNum uint64) base.InternalKey {
return base.MakeInternalKey([]byte(userKey), base.SeqNum(seqNum), base.InternalKeyKindSet)
}
defaultPolicy := ValueSeparationPolicy{
Enabled: true,
MinimumSize: 512,
MaxBlobReferenceDepth: 5,
}
defaultSpanPolicyFunc := SpanPolicyFunc(func(bounds base.UserKeyBounds) (base.SpanPolicy, error) {
return base.SpanPolicy{}, nil
})
type testCase struct {
name string
kind compactionKind
inputs []compactionLevel
policy ValueSeparationPolicy
spanPolicyFunc SpanPolicyFunc
wantWriteBlobs bool
wantReferenceDepth manifest.BlobReferenceDepth
wantAnnotations []string
}
tests := []testCase{
{
name: "flush",
kind: compactionKindFlush,
// Flushes always write new blob files regardless of inputs.
wantWriteBlobs: true,
wantReferenceDepth: 0,
},
{
// A virtual rewrite materializes a virtual table without writing
// new blob files. The reference depth is preserved unchanged.
name: "virtual-rewrite",
kind: compactionKindVirtualRewrite,
inputs: []compactionLevel{
{
level: 6,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
makeTestTableMeta(
cmp, 1, ikey("a", 5), ikey("m", 5), 3, 512, false),
}),
},
},
wantWriteBlobs: false,
wantReferenceDepth: 3,
},
{
name: "default-low-reference-depth",
kind: compactionKindDefault,
inputs: []compactionLevel{
{
level: 5,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
makeTestTableMeta(
cmp, 1, ikey("a", 5), ikey("m", 5), 3, 512, false),
}),
},
{
level: 6,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
makeTestTableMeta(
cmp, 2, ikey("a", 4), ikey("m", 4), 2, 512, false),
}),
},
},
wantWriteBlobs: false,
wantReferenceDepth: 5, // 3 + 2
},
{
name: "all-pre-valsep",
kind: compactionKindDefault,
inputs: []compactionLevel{
{
level: 5,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
makeTestTableMeta(
cmp, 1, ikey("a", 5), ikey("m", 5), 0, 0, false),
}),
},
{
level: 6,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
makeTestTableMeta(
cmp, 2, ikey("a", 4), ikey("m", 4), 0, 0, false),
}),
},
},
wantWriteBlobs: true,
wantReferenceDepth: 0,
wantAnnotations: []string{"write-blobs-input-depth-zero"},
},
{
// A compaction mixing post-valsep L5 files with pre-valsep L6 files
// should NOT force a rewrite of the L5 blob references.
name: "mixed-pre-and-post-valsep",
kind: compactionKindDefault,
inputs: []compactionLevel{
{
level: 5,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
makeTestTableMeta(
cmp, 1, ikey("a", 5), ikey("m", 5), 1, 512, false),
}),
},
{
level: 6,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
makeTestTableMeta(
cmp, 2, ikey("a", 4), ikey("m", 4), 0, 0, false),
}),
},
},
wantWriteBlobs: false,
wantReferenceDepth: 1, // max(1) + max(0)
},
{
name: "depth-exceeded",
kind: compactionKindDefault,
inputs: []compactionLevel{
{
level: 5,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
makeTestTableMeta(
cmp, 1, ikey("a", 5), ikey("m", 5), 3, 512, false),
}),
},
{
level: 6,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
makeTestTableMeta(
cmp, 2, ikey("a", 4), ikey("m", 4), 3, 512, false),
}),
},
},
// inputReferenceDepth = 3 + 3 = 6 > MaxBlobReferenceDepth (5)
wantWriteBlobs: true,
wantReferenceDepth: 0,
wantAnnotations: []string{"write-blobs-input-depth-exceeded"},
},
{
name: "min-size-mismatch",
kind: compactionKindDefault,
inputs: []compactionLevel{
{
level: 5,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
// MinSize=256 differs from policy MinimumSize=512.
makeTestTableMeta(
cmp, 1, ikey("a", 5), ikey("m", 5), 1, 256, false),
}),
},
{
level: 6,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
makeTestTableMeta(
cmp, 2, ikey("a", 4), ikey("m", 4), 1, 512, false),
}),
},
},
wantWriteBlobs: true,
wantReferenceDepth: 0,
wantAnnotations: []string{"write-blobs-min-size-mismatch"},
},
{
name: "suffix-disabled-mismatch",
kind: compactionKindDefault,
inputs: []compactionLevel{
{
level: 5,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
// BySuffixDisabled=true but the default span policy expects false.
makeTestTableMeta(
cmp, 1, ikey("a", 5), ikey("m", 5), 1, 512, true),
}),
},
{
level: 6,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
makeTestTableMeta(
cmp, 2, ikey("a", 4), ikey("m", 4), 1, 512, false),
}),
},
},
wantWriteBlobs: true,
wantReferenceDepth: 0,
wantAnnotations: []string{"write-blobs-suffix-disabled-mismatch"},
},
{
name: "multiple-span-policies",
kind: compactionKindDefault,
inputs: []compactionLevel{
{
level: 5,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
// File spans a→z but the span policy only covers a→m.
makeTestTableMeta(
cmp, 1, ikey("a", 5), ikey("z", 5), 1, 512, false),
}),
},
{
level: 6,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
makeTestTableMeta(
cmp, 2, ikey("a", 4), ikey("m", 4), 1, 512, false),
}),
},
},
spanPolicyFunc: func(bounds base.UserKeyBounds) (base.SpanPolicy, error) {
return base.SpanPolicy{
KeyRange: base.KeyRange{Start: []byte("a"), End: []byte("m")},
}, nil
},
wantWriteBlobs: true,
wantReferenceDepth: 0,
wantAnnotations: []string{"write-blobs-multiple-policies"},
},
{
name: "all-matching",
kind: compactionKindDefault,
inputs: []compactionLevel{
{
level: 5,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
makeTestTableMeta(
cmp, 1, ikey("a", 5), ikey("m", 5), 2, 512, false),
}),
},
{
level: 6,
files: manifest.NewLevelSliceKeySorted(cmp, []*manifest.TableMetadata{
makeTestTableMeta(
cmp, 2, ikey("a", 4), ikey("m", 4), 1, 512, false),
}),
},
},
wantWriteBlobs: false,
wantReferenceDepth: 3, // 2 + 1
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
policy := tt.policy
if policy == (ValueSeparationPolicy{}) {
policy = defaultPolicy
}
spanPolicyFunc := tt.spanPolicyFunc
if spanPolicyFunc == nil {
spanPolicyFunc = defaultSpanPolicyFunc
}
c := &tableCompaction{
kind: tt.kind,
inputs: tt.inputs,
}
writeBlobs, refDepth := shouldWriteBlobFiles(c, policy, spanPolicyFunc, cmp)
require.Equal(t, tt.wantWriteBlobs, writeBlobs, "writeBlobs")
require.Equal(t, tt.wantReferenceDepth, refDepth, "referenceDepth")
if tt.wantAnnotations == nil {
require.Empty(t, c.annotations, "annotations")
} else {
require.Equal(t, tt.wantAnnotations, c.annotations, "annotations")
}
})
}
}