forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_set_test.go
299 lines (262 loc) · 9.85 KB
/
version_set_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
// Copyright 2020 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 (
"io"
"testing"
"time"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/internal/manifest"
"github.com/cockroachdb/pebble/objstorage/objstorageprovider"
"github.com/cockroachdb/pebble/record"
"github.com/cockroachdb/pebble/sstable"
"github.com/cockroachdb/pebble/vfs"
"github.com/stretchr/testify/require"
)
func writeAndIngest(t *testing.T, mem vfs.FS, d *DB, k InternalKey, v []byte, filename string) {
path := mem.PathJoin("ext", filename)
f, err := mem.Create(path)
require.NoError(t, err)
w := sstable.NewWriter(objstorageprovider.NewFileWritable(f), sstable.WriterOptions{})
require.NoError(t, w.Add(k, v))
require.NoError(t, w.Close())
require.NoError(t, d.Ingest([]string{path}))
}
// TestLatestRefCounting sanity checks the ref counting implementation for
// FileMetadata.latestRefs, and makes sure that the zombie table implementation
// works when the version edit contains virtual sstables. It also checks that
// we're adding the physical sstable to the obsolete tables list iff the file is
// truly obsolete.
func TestLatestRefCounting(t *testing.T) {
mem := vfs.NewMem()
require.NoError(t, mem.MkdirAll("ext", 0755))
opts := &Options{
FS: mem,
MaxManifestFileSize: 1,
DisableAutomaticCompactions: true,
}
d, err := Open("", opts)
require.NoError(t, err)
err = d.Set([]byte{'a'}, []byte{'a'}, nil)
require.NoError(t, err)
err = d.Set([]byte{'b'}, []byte{'b'}, nil)
require.NoError(t, err)
err = d.Flush()
require.NoError(t, err)
iter := d.mu.versions.currentVersion().Levels[0].Iter()
var f *fileMetadata = iter.First()
require.NotNil(t, f)
require.Equal(t, 1, int(f.LatestRefs()))
require.Equal(t, 0, len(d.mu.versions.obsoleteTables))
// Grab some new file nums.
d.mu.Lock()
f1 := d.mu.versions.nextFileNum
f2 := f1 + 1
d.mu.versions.nextFileNum += 2
d.mu.Unlock()
m1 := &manifest.FileMetadata{
FileBacking: f.FileBacking,
FileNum: f1,
CreationTime: time.Now().Unix(),
Size: f.Size / 2,
SmallestSeqNum: f.SmallestSeqNum,
LargestSeqNum: f.LargestSeqNum,
Smallest: base.MakeInternalKey([]byte{'a'}, f.Smallest.SeqNum(), InternalKeyKindSet),
Largest: base.MakeInternalKey([]byte{'a'}, f.Smallest.SeqNum(), InternalKeyKindSet),
HasPointKeys: true,
Virtual: true,
}
m2 := &manifest.FileMetadata{
FileBacking: f.FileBacking,
FileNum: f2,
CreationTime: time.Now().Unix(),
Size: f.Size / 2,
SmallestSeqNum: f.SmallestSeqNum,
LargestSeqNum: f.LargestSeqNum,
Smallest: base.MakeInternalKey([]byte{'b'}, f.Largest.SeqNum(), InternalKeyKindSet),
Largest: base.MakeInternalKey([]byte{'b'}, f.Largest.SeqNum(), InternalKeyKindSet),
HasPointKeys: true,
Virtual: true,
}
m1.LargestPointKey = m1.Largest
m1.SmallestPointKey = m1.Smallest
m2.LargestPointKey = m2.Largest
m2.SmallestPointKey = m2.Smallest
m1.ValidateVirtual(f)
m2.ValidateVirtual(f)
fileMetrics := func(ve *versionEdit) map[int]*LevelMetrics {
metrics := newFileMetrics(ve.NewFiles)
for de, f := range ve.DeletedFiles {
lm := metrics[de.Level]
if lm == nil {
lm = &LevelMetrics{}
metrics[de.Level] = lm
}
metrics[de.Level].NumFiles--
metrics[de.Level].Size -= int64(f.Size)
}
return metrics
}
d.mu.Lock()
defer d.mu.Unlock()
applyVE := func(ve *versionEdit) error {
d.mu.versions.logLock()
jobID := d.mu.nextJobID
d.mu.nextJobID++
err := d.mu.versions.logAndApply(jobID, ve, fileMetrics(ve), false, func() []compactionInfo {
return d.getInProgressCompactionInfoLocked(nil)
})
d.updateReadStateLocked(nil)
return err
}
// Virtualize f.
ve := manifest.VersionEdit{}
d1 := manifest.DeletedFileEntry{Level: 0, FileNum: f.FileNum}
n1 := manifest.NewFileEntry{Level: 0, Meta: m1}
n2 := manifest.NewFileEntry{Level: 0, Meta: m2}
ve.DeletedFiles = make(map[manifest.DeletedFileEntry]*manifest.FileMetadata)
ve.DeletedFiles[d1] = f
ve.NewFiles = append(ve.NewFiles, n1)
ve.NewFiles = append(ve.NewFiles, n2)
ve.CreatedBackingTables = append(ve.CreatedBackingTables, f.FileBacking)
require.NoError(t, applyVE(&ve))
// 2 latestRefs from 2 virtual sstables in the latest version which refer
// to the physical sstable.
require.Equal(t, 2, int(m1.LatestRefs()))
require.Equal(t, 0, len(d.mu.versions.obsoleteTables))
require.Equal(t, 1, len(d.mu.versions.fileBackingMap))
_, ok := d.mu.versions.fileBackingMap[f.FileNum]
require.True(t, ok)
require.Equal(t, f.Size, m2.FileBacking.Atomic.VirtualizedSize.Load())
// Make sure that f is not present in zombie list, because it is not yet a
// zombie.
require.Equal(t, 0, len(d.mu.versions.zombieTables))
// Delete the virtual sstable m1.
ve = manifest.VersionEdit{}
d1 = manifest.DeletedFileEntry{Level: 0, FileNum: m1.FileNum}
ve.DeletedFiles = make(map[manifest.DeletedFileEntry]*manifest.FileMetadata)
ve.DeletedFiles[d1] = m1
require.NoError(t, applyVE(&ve))
// Only one virtual sstable in the latest version, confirm that the latest
// version ref counting is correct.
require.Equal(t, 1, int(m2.LatestRefs()))
require.Equal(t, 0, len(d.mu.versions.zombieTables))
require.Equal(t, 0, len(d.mu.versions.obsoleteTables))
require.Equal(t, 1, len(d.mu.versions.fileBackingMap))
_, ok = d.mu.versions.fileBackingMap[f.FileNum]
require.True(t, ok)
require.Equal(t, m2.Size, m2.FileBacking.Atomic.VirtualizedSize.Load())
// Move m2 from L0 to L6 to test the move compaction case.
ve = manifest.VersionEdit{}
d1 = manifest.DeletedFileEntry{Level: 0, FileNum: m2.FileNum}
n1 = manifest.NewFileEntry{Level: 6, Meta: m2}
ve.DeletedFiles = make(map[manifest.DeletedFileEntry]*manifest.FileMetadata)
ve.DeletedFiles[d1] = m2
ve.NewFiles = append(ve.NewFiles, n1)
require.NoError(t, applyVE(&ve))
require.Equal(t, 1, int(m2.LatestRefs()))
require.Equal(t, 0, len(d.mu.versions.zombieTables))
require.Equal(t, 0, len(d.mu.versions.obsoleteTables))
require.Equal(t, 1, len(d.mu.versions.fileBackingMap))
_, ok = d.mu.versions.fileBackingMap[f.FileNum]
require.True(t, ok)
require.Equal(t, m2.Size, m2.FileBacking.Atomic.VirtualizedSize.Load())
// Delete m2 from L6.
ve = manifest.VersionEdit{}
d1 = manifest.DeletedFileEntry{Level: 6, FileNum: m2.FileNum}
ve.DeletedFiles = make(map[manifest.DeletedFileEntry]*manifest.FileMetadata)
ve.DeletedFiles[d1] = m2
require.NoError(t, applyVE(&ve))
// All virtual sstables are gone.
require.Equal(t, 0, int(m2.LatestRefs()))
require.Equal(t, 1, len(d.mu.versions.zombieTables))
require.Equal(t, f.Size, d.mu.versions.zombieTables[f.FileNum])
require.Equal(t, 0, len(d.mu.versions.fileBackingMap))
_, ok = d.mu.versions.fileBackingMap[f.FileNum]
require.False(t, ok)
require.Equal(t, 0, int(m2.FileBacking.Atomic.VirtualizedSize.Load()))
// Make sure that the backing file is added to the obsolete tables list.
require.Equal(t, 1, len(d.mu.versions.obsoleteTables))
}
func TestVersionSetCheckpoint(t *testing.T) {
mem := vfs.NewMem()
require.NoError(t, mem.MkdirAll("ext", 0755))
opts := &Options{
FS: mem,
MaxManifestFileSize: 1,
}
d, err := Open("", opts)
require.NoError(t, err)
// Multiple manifest files are created such that the latest one must have a correct snapshot
// of the preceding state for the DB to be opened correctly and see the written data.
// Snapshot has no files, so first edit will cause manifest rotation.
writeAndIngest(t, mem, d, base.MakeInternalKey([]byte("a"), 0, InternalKeyKindSet), []byte("b"), "a")
// Snapshot has no files, and manifest has an edit from the previous ingest,
// so this second ingest will cause manifest rotation.
writeAndIngest(t, mem, d, base.MakeInternalKey([]byte("c"), 0, InternalKeyKindSet), []byte("d"), "c")
require.NoError(t, d.Close())
d, err = Open("", opts)
require.NoError(t, err)
checkValue := func(k string, expected string) {
v, closer, err := d.Get([]byte(k))
require.NoError(t, err)
require.Equal(t, expected, string(v))
closer.Close()
}
checkValue("a", "b")
checkValue("c", "d")
require.NoError(t, d.Close())
}
func TestVersionSetSeqNums(t *testing.T) {
mem := vfs.NewMem()
require.NoError(t, mem.MkdirAll("ext", 0755))
opts := &Options{
FS: mem,
MaxManifestFileSize: 1,
}
d, err := Open("", opts)
require.NoError(t, err)
// Snapshot has no files, so first edit will cause manifest rotation.
writeAndIngest(t, mem, d, base.MakeInternalKey([]byte("a"), 0, InternalKeyKindSet), []byte("b"), "a")
// Snapshot has no files, and manifest has an edit from the previous ingest,
// so this second ingest will cause manifest rotation.
writeAndIngest(t, mem, d, base.MakeInternalKey([]byte("c"), 0, InternalKeyKindSet), []byte("d"), "c")
require.NoError(t, d.Close())
d, err = Open("", opts)
require.NoError(t, err)
defer d.Close()
// Check that the manifest has the correct LastSeqNum, equalling the highest
// observed SeqNum.
filenames, err := mem.List("")
require.NoError(t, err)
var manifest vfs.File
for _, filename := range filenames {
fileType, _, ok := base.ParseFilename(mem, filename)
if ok && fileType == fileTypeManifest {
manifest, err = mem.Open(filename)
require.NoError(t, err)
}
}
require.NotNil(t, manifest)
defer manifest.Close()
rr := record.NewReader(manifest, 0 /* logNum */)
lastSeqNum := uint64(0)
for {
r, err := rr.Next()
if err == io.EOF {
break
}
require.NoError(t, err)
var ve versionEdit
err = ve.Decode(r)
require.NoError(t, err)
if ve.LastSeqNum != 0 {
lastSeqNum = ve.LastSeqNum
}
}
// 2 ingestions happened, so LastSeqNum should equal 2.
require.Equal(t, uint64(2), lastSeqNum)
// logSeqNum is always one greater than the last assigned sequence number.
require.Equal(t, d.mu.versions.atomic.logSeqNum, lastSeqNum+1)
}