forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_journal.go
348 lines (295 loc) · 8.61 KB
/
disk_journal.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
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"fmt"
"os"
"path/filepath"
"reflect"
"strconv"
"github.com/keybase/kbfs/ioutil"
"github.com/keybase/kbfs/kbfscodec"
"github.com/pkg/errors"
)
// journalOrdinal is the ordinal used for naming journal entries.
type journalOrdinal uint64
// TODO: Define the zero journalOrdinal as invalid, once no existing
// journals use them.
const firstValidJournalOrdinal journalOrdinal = 1
func makeJournalOrdinal(s string) (journalOrdinal, error) {
if len(s) != 16 {
return 0, errors.Errorf("invalid journal ordinal %q", s)
}
u, err := strconv.ParseUint(s, 16, 64)
if err != nil {
return 0, errors.Wrapf(err, "failed to parse %q", s)
}
return journalOrdinal(u), nil
}
func (o journalOrdinal) String() string {
return fmt.Sprintf("%016x", uint64(o))
}
// diskJournal stores an ordered list of entries in a directory, which
// is assumed to not be used by anything else.
//
// The directory layout looks like:
//
// dir/EARLIEST
// dir/LATEST
// dir/0...000
// dir/0...001
// dir/0...fff
//
// Each file in dir is named with an ordinal and contains a generic
// serializable entry object. The files EARLIEST and LATEST point to
// the earliest and latest valid ordinal, respectively.
//
// This class is not goroutine-safe; it assumes that all
// synchronization is done at a higher level.
//
// TODO: Do all high-level operations atomically on the file-system
// level.
//
// TODO: Make IO ops cancellable.
type diskJournal struct {
codec kbfscodec.Codec
dir string
entryType reflect.Type
// The journal must be considered empty when either
// earliestValid or latestValid is false.
earliestValid bool
earliest journalOrdinal
latestValid bool
latest journalOrdinal
}
// makeDiskJournal returns a new diskJournal for the given directory.
func makeDiskJournal(
codec kbfscodec.Codec, dir string, entryType reflect.Type) (
*diskJournal, error) {
j := &diskJournal{
codec: codec,
dir: dir,
entryType: entryType,
}
earliest, err := j.readEarliestOrdinalFromDisk()
if ioutil.IsNotExist(err) {
// Continue with j.earliestValid = false.
} else if err != nil {
return nil, err
} else {
j.earliestValid = true
j.earliest = earliest
}
latest, err := j.readLatestOrdinalFromDisk()
if ioutil.IsNotExist(err) {
// Continue with j.latestValid = false.
} else if err != nil {
return nil, err
} else {
j.latestValid = true
j.latest = latest
}
return j, nil
}
// The functions below are for building various paths for the journal.
func (j diskJournal) earliestPath() string {
return filepath.Join(j.dir, "EARLIEST")
}
func (j diskJournal) latestPath() string {
return filepath.Join(j.dir, "LATEST")
}
func (j diskJournal) journalEntryPath(o journalOrdinal) string {
return filepath.Join(j.dir, o.String())
}
// The functions below are for reading and writing the earliest and
// latest ordinals. The read functions may return an error for which
// ioutil.IsNotExist() returns true.
func (j diskJournal) readOrdinalFromDisk(path string) (journalOrdinal, error) {
buf, err := ioutil.ReadFile(path)
if err != nil {
return 0, err
}
return makeJournalOrdinal(string(buf))
}
func (j *diskJournal) writeOrdinalToDisk(path string, o journalOrdinal) error {
return ioutil.WriteSerializedFile(path, []byte(o.String()), 0600)
}
func (j diskJournal) readEarliestOrdinalFromDisk() (journalOrdinal, error) {
return j.readOrdinalFromDisk(j.earliestPath())
}
func (j diskJournal) readLatestOrdinalFromDisk() (journalOrdinal, error) {
return j.readOrdinalFromDisk(j.latestPath())
}
func (j diskJournal) empty() bool {
return !j.earliestValid || !j.latestValid
}
// TODO: Change {read,write}{Earliest,Latest}Ordinal() to
// {get,set}{Earliest,Latest}Ordinal(), and have the getters return an
// isValid bool, or an invalid journalOrdinal instead of an error.
func (j diskJournal) readEarliestOrdinal() (journalOrdinal, error) {
if j.empty() {
return journalOrdinal(0), errors.WithStack(os.ErrNotExist)
}
return j.earliest, nil
}
func (j *diskJournal) writeEarliestOrdinal(o journalOrdinal) error {
err := j.writeOrdinalToDisk(j.earliestPath(), o)
if err != nil {
return err
}
j.earliestValid = true
j.earliest = o
return nil
}
func (j diskJournal) readLatestOrdinal() (journalOrdinal, error) {
if j.empty() {
return journalOrdinal(0), errors.WithStack(os.ErrNotExist)
}
return j.latest, nil
}
func (j *diskJournal) writeLatestOrdinal(o journalOrdinal) error {
err := j.writeOrdinalToDisk(j.latestPath(), o)
if err != nil {
return err
}
j.latestValid = true
j.latest = o
return nil
}
// clear completely removes the journal directory.
func (j *diskJournal) clear() error {
// Clear ordinals first to not leave the journal in a weird
// state if we crash in the middle of removing the files,
// assuming that file removal is atomic.
err := ioutil.Remove(j.earliestPath())
if err != nil {
return err
}
// If we crash here, on the next startup the journal will
// still be considered empty.
j.earliestValid = false
j.earliest = journalOrdinal(0)
err = ioutil.Remove(j.latestPath())
if err != nil {
return err
}
j.latestValid = false
j.latest = journalOrdinal(0)
// j.dir will be recreated on the next call to
// writeJournalEntry (via kbfscodec.SerializeToFile), which
// must always come before any ordinal write.
return ioutil.RemoveAll(j.dir)
}
// removeEarliest removes the earliest entry in the journal. If that
// entry was the last one, clear() is also called, and true is
// returned.
func (j *diskJournal) removeEarliest() (empty bool, err error) {
if j.empty() {
// TODO: Return a more meaningful error.
return false, errors.WithStack(os.ErrNotExist)
}
if j.earliest == j.latest {
err := j.clear()
if err != nil {
return false, err
}
return true, nil
}
oldEarliest := j.earliest
err = j.writeEarliestOrdinal(oldEarliest + 1)
if err != nil {
return false, err
}
// Garbage-collect the old entry. If we crash here and leave
// behind an entry, it'll be cleaned up the next time clear()
// is called.
p := j.journalEntryPath(oldEarliest)
err = ioutil.Remove(p)
if err != nil {
return false, err
}
return false, nil
}
// The functions below are for reading and writing journal entries.
func (j diskJournal) readJournalEntry(o journalOrdinal) (interface{}, error) {
p := j.journalEntryPath(o)
entry := reflect.New(j.entryType)
err := kbfscodec.DeserializeFromFile(j.codec, p, entry)
if err != nil {
return nil, err
}
return entry.Elem().Interface(), nil
}
func (j *diskJournal) writeJournalEntry(
o journalOrdinal, entry interface{}) error {
entryType := reflect.TypeOf(entry)
if entryType != j.entryType {
panic(errors.Errorf("Expected entry type %v, got %v",
j.entryType, entryType))
}
return kbfscodec.SerializeToFile(j.codec, entry, j.journalEntryPath(o))
}
// appendJournalEntry appends the given entry to the journal. If o is
// nil, then if the journal is empty, the new entry will have ordinal
// 0, and otherwise it will have ordinal equal to the successor of the
// latest ordinal. Otherwise, if o is non-nil, then if the journal is
// empty, the new entry will have ordinal *o, and otherwise it returns
// an error if *o is not the successor of the latest ordinal. If
// successful, appendJournalEntry returns the ordinal of the
// just-appended entry.
func (j *diskJournal) appendJournalEntry(
o *journalOrdinal, entry interface{}) (journalOrdinal, error) {
var next journalOrdinal
if j.empty() {
if o != nil {
next = *o
} else {
next = firstValidJournalOrdinal
}
} else {
next = j.latest + 1
if next == 0 {
// Rollover is almost certainly a bug.
return 0, errors.Errorf(
"Ordinal rollover for %+v", entry)
}
if o != nil && next != *o {
return 0, errors.Errorf(
"%v unexpectedly does not follow %v for %+v",
*o, j.latest, entry)
}
}
err := j.writeJournalEntry(next, entry)
if err != nil {
return 0, err
}
if j.empty() {
err := j.writeEarliestOrdinal(next)
if err != nil {
return 0, err
}
}
err = j.writeLatestOrdinal(next)
if err != nil {
return 0, err
}
return next, nil
}
// move moves the journal to the given directory, which should share
// the same parent directory as the current journal directory.
func (j *diskJournal) move(newDir string) (oldDir string, err error) {
err = ioutil.Rename(j.dir, newDir)
if err != nil && !ioutil.IsNotExist(err) {
return "", err
}
oldDir = j.dir
j.dir = newDir
return oldDir, nil
}
func (j diskJournal) length() uint64 {
if j.empty() {
return 0
}
return uint64(j.latest - j.earliest + 1)
}