forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
open.go
371 lines (339 loc) · 9.27 KB
/
open.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2012 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 (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"sync"
"github.com/petermattis/pebble/internal/arenaskl"
"github.com/petermattis/pebble/internal/rate"
"github.com/petermattis/pebble/internal/record"
"github.com/petermattis/pebble/vfs"
)
var dbNumAlloc = struct {
sync.Mutex
seq uint64
}{seq: 1}
func allocDBNum() uint64 {
dbNumAlloc.Lock()
num := dbNumAlloc.seq
dbNumAlloc.seq++
dbNumAlloc.Unlock()
return num
}
func createDB(dirname string, opts *Options) (retErr error) {
const manifestFileNum = 1
ve := versionEdit{
comparatorName: opts.Comparer.Name,
nextFileNumber: manifestFileNum + 1,
}
manifestFilename := dbFilename(dirname, fileTypeManifest, manifestFileNum)
f, err := opts.FS.Create(manifestFilename)
if err != nil {
return fmt.Errorf("pebble: could not create %q: %v", manifestFilename, err)
}
defer func() {
if retErr != nil {
opts.FS.Remove(manifestFilename)
}
}()
defer f.Close()
recWriter := record.NewWriter(f)
w, err := recWriter.Next()
if err != nil {
return err
}
err = ve.encode(w)
if err != nil {
return err
}
err = recWriter.Close()
if err != nil {
return err
}
return setCurrentFile(dirname, opts.FS, manifestFileNum)
}
// Open opens a LevelDB whose files live in the given directory.
func Open(dirname string, opts *Options) (*DB, error) {
opts = opts.EnsureDefaults()
d := &DB{
dbNum: allocDBNum(),
dirname: dirname,
walDirname: opts.WALDir,
opts: opts,
cmp: opts.Comparer.Compare,
equal: opts.Comparer.Equal,
merge: opts.Merger.Merge,
split: opts.Comparer.Split,
abbreviatedKey: opts.Comparer.AbbreviatedKey,
logRecycler: logRecycler{limit: opts.MemTableStopWritesThreshold + 1},
}
if d.equal == nil {
d.equal = bytes.Equal
}
tableCacheSize := opts.MaxOpenFiles - numNonTableCacheFiles
if tableCacheSize < minTableCacheSize {
tableCacheSize = minTableCacheSize
}
d.tableCache.init(d.dbNum, dirname, opts.FS, d.opts, tableCacheSize, defaultTableCacheHitBuffer)
d.newIters = d.tableCache.newIters
d.commit = newCommitPipeline(commitEnv{
logSeqNum: &d.mu.versions.logSeqNum,
visibleSeqNum: &d.mu.versions.visibleSeqNum,
apply: d.commitApply,
write: d.commitWrite,
})
d.compactionLimiter = rate.NewLimiter(rate.Limit(d.opts.MinCompactionRate), d.opts.MinCompactionRate)
d.flushLimiter = rate.NewLimiter(rate.Limit(d.opts.MinFlushRate), d.opts.MinFlushRate)
d.mu.nextJobID = 1
d.mu.mem.cond.L = &d.mu.Mutex
d.mu.mem.mutable = newMemTable(d.opts)
d.mu.mem.queue = append(d.mu.mem.queue, d.mu.mem.mutable)
d.mu.cleaner.cond.L = &d.mu.Mutex
d.mu.compact.cond.L = &d.mu.Mutex
d.mu.compact.pendingOutputs = make(map[uint64]struct{})
d.mu.snapshots.init()
d.largeBatchThreshold = (d.opts.MemTableSize - int(d.mu.mem.mutable.emptySize)) / 2
d.mu.Lock()
defer d.mu.Unlock()
// Lock the database directory.
err := opts.FS.MkdirAll(dirname, 0755)
if err != nil {
return nil, err
}
fileLock, err := opts.FS.Lock(dbFilename(dirname, fileTypeLock, 0))
if err != nil {
return nil, err
}
defer func() {
if fileLock != nil {
fileLock.Close()
}
}()
d.dataDir, err = opts.FS.OpenDir(dirname)
if err != nil {
return nil, err
}
if d.walDirname == "" {
d.walDirname = d.dirname
}
if d.walDirname == d.dirname {
d.walDir = d.dataDir
} else {
err := opts.FS.MkdirAll(d.walDirname, 0755)
if err != nil {
return nil, err
}
d.walDir, err = opts.FS.OpenDir(d.walDirname)
}
if _, err := opts.FS.Stat(dbFilename(dirname, fileTypeCurrent, 0)); os.IsNotExist(err) {
// Create the DB if it did not already exist.
if err := createDB(dirname, opts); err != nil {
return nil, err
}
if err := d.dataDir.Sync(); err != nil {
return nil, err
}
} else if err != nil {
return nil, fmt.Errorf("pebble: database %q: %v", dirname, err)
} else if opts.ErrorIfDBExists {
return nil, fmt.Errorf("pebble: database %q already exists", dirname)
}
// Load the version set.
err = d.mu.versions.load(dirname, opts, &d.mu.Mutex)
if err != nil {
return nil, err
}
ls, err := opts.FS.List(d.walDirname)
if err != nil {
return nil, err
}
if d.dirname != d.walDirname {
ls2, err := opts.FS.List(d.dirname)
if err != nil {
return nil, err
}
ls = append(ls, ls2...)
}
// Replay any newer log files than the ones named in the manifest.
type fileNumAndName struct {
num uint64
name string
}
var logFiles []fileNumAndName
for _, filename := range ls {
ft, fn, ok := parseDBFilename(filename)
if !ok {
continue
}
switch ft {
case fileTypeLog:
if fn >= d.mu.versions.logNumber || fn == d.mu.versions.prevLogNumber {
logFiles = append(logFiles, fileNumAndName{fn, filename})
}
case fileTypeOptions:
if err := checkOptions(opts, filepath.Join(dirname, filename)); err != nil {
return nil, err
}
}
}
sort.Slice(logFiles, func(i, j int) bool {
return logFiles[i].num < logFiles[j].num
})
var ve versionEdit
for _, lf := range logFiles {
maxSeqNum, err := d.replayWAL(&ve, opts.FS, filepath.Join(d.walDirname, lf.name), lf.num)
if err != nil {
return nil, err
}
d.mu.versions.markFileNumUsed(lf.num)
if d.mu.versions.logSeqNum < maxSeqNum {
d.mu.versions.logSeqNum = maxSeqNum
}
}
d.mu.versions.visibleSeqNum = d.mu.versions.logSeqNum
// Create an empty .log file.
ve.logNumber = d.mu.versions.nextFileNum()
d.mu.log.queue = append(d.mu.log.queue, ve.logNumber)
logFile, err := opts.FS.Create(dbFilename(d.walDirname, fileTypeLog, ve.logNumber))
if err != nil {
return nil, err
}
if err := d.walDir.Sync(); err != nil {
return nil, err
}
logFile = vfs.NewSyncingFile(logFile, vfs.SyncingFileOptions{
BytesPerSync: d.opts.BytesPerSync,
PreallocateSize: d.walPreallocateSize(),
})
d.mu.log.LogWriter = record.NewLogWriter(logFile, ve.logNumber)
d.mu.versions.metrics.WAL.Files++
// Write a new manifest to disk.
if err := d.mu.versions.logAndApply(0, &ve, d.dataDir); err != nil {
return nil, err
}
d.updateReadStateLocked()
// Write the current options to disk.
d.optionsFileNum = d.mu.versions.nextFileNum()
optionsFile, err := opts.FS.Create(dbFilename(dirname, fileTypeOptions, d.optionsFileNum))
if err != nil {
return nil, err
}
if _, err := optionsFile.Write([]byte(opts.String())); err != nil {
return nil, err
}
optionsFile.Close()
if err := d.dataDir.Sync(); err != nil {
return nil, err
}
jobID := d.mu.nextJobID
d.mu.nextJobID++
d.scanObsoleteFiles(ls)
d.deleteObsoleteFiles(jobID)
d.maybeScheduleFlush()
d.maybeScheduleCompaction()
d.fileLock, fileLock = fileLock, nil
return d, nil
}
// replayWAL replays the edits in the specified log file.
//
// d.mu must be held when calling this, but the mutex may be dropped and
// re-acquired during the course of this method.
func (d *DB) replayWAL(
ve *versionEdit,
fs vfs.FS,
filename string,
logNum uint64,
) (maxSeqNum uint64, err error) {
file, err := fs.Open(filename)
if err != nil {
return 0, err
}
defer file.Close()
var (
b Batch
buf bytes.Buffer
mem *memTable
rr = record.NewReader(file, logNum)
)
for {
r, err := rr.Next()
if err == nil {
_, err = io.Copy(&buf, r)
}
if err != nil {
// It is common to encounter a zeroed or invalid chunk due to WAL
// preallocation and WAL recycling. We need to distinguish these errors
// from EOF in order to recognize that the record was truncated, but want
// to otherwise treat them like EOF.
if err == io.EOF || err == record.ErrZeroedChunk || err == record.ErrInvalidChunk {
break
}
return 0, err
}
if buf.Len() < batchHeaderLen {
return 0, fmt.Errorf("pebble: corrupt log file %q", filename)
}
// TODO(peter): If the batch is too large to fit in the memtable, flush the
// existing memtable and write the batch as a separate L0 table.
b = Batch{}
b.storage.data = buf.Bytes()
b.refreshMemTableSize()
seqNum := b.seqNum()
maxSeqNum = seqNum + uint64(b.count())
if mem == nil {
mem = newMemTable(d.opts)
}
for {
err := mem.prepare(&b)
if err == arenaskl.ErrArenaFull {
// TODO(peter): write the memtable to disk.
panic(err)
}
if err != nil {
return 0, err
}
break
}
if err := mem.apply(&b, seqNum); err != nil {
return 0, err
}
mem.unref()
buf.Reset()
}
if mem != nil && !mem.empty() {
c := newFlush(d.opts, d.mu.versions.currentVersion(),
1 /* base level */, []flushable{mem})
newVE, pendingOutputs, err := d.runCompaction(c)
if err != nil {
return 0, err
}
ve.newFiles = append(ve.newFiles, newVE.newFiles...)
// Strictly speaking, it's too early to delete from d.pendingOutputs, but
// we are replaying the log file, which happens before Open returns, so
// there is no possibility of deleteObsoleteFiles being called concurrently
// here.
for _, fileNum := range pendingOutputs {
delete(d.mu.compact.pendingOutputs, fileNum)
}
}
return maxSeqNum, nil
}
func checkOptions(opts *Options, path string) error {
f, err := opts.FS.Open(path)
if err != nil {
return err
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return err
}
return opts.Check(string(data))
}