forked from fuyao-w/papillon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_snapshot.go
More file actions
332 lines (307 loc) · 6.83 KB
/
file_snapshot.go
File metadata and controls
332 lines (307 loc) · 6.83 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
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
package papillon
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"hash"
"hash/crc64"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
)
const (
dirMode = 0755
testFile = "snapShotTest"
tmpFileSuffix = ".tmp"
metaFile = "meta.json"
snapshotFile = "snapshot.bin"
)
type (
FileWithSync interface {
fs.File
Sync() error
}
FileSnapshot struct {
dir string
noSync bool
retainCount int
}
FileSnapshotSink struct {
snapshotStore *FileSnapshot
close bool
meta *fileSnapshotMeta
dir string
parentPath string
noSync bool
hash hash.Hash64
file FileWithSync
writer *bufio.Writer
}
fileSnapshotMeta struct {
*SnapshotMeta
CRC []byte
}
bufferReader struct {
buf *bufio.Reader
file FileWithSync
}
)
func newCRC64() hash.Hash64 {
return crc64.New(crc64.MakeTable(crc64.ECMA))
}
func sortMetaList(list []*fileSnapshotMeta) {
sort.Slice(list, func(i, j int) bool {
if list[i].Term != list[j].Term {
return list[i].Term > list[j].Term
}
if list[i].Index != list[j].Index {
return list[i].Index > list[j].Index
}
return list[i].ID > list[j].ID
})
}
func (b *bufferReader) Read(p []byte) (n int, err error) {
return b.buf.Read(p)
}
func (b *bufferReader) Close() error {
return b.file.Close()
}
func NewFileSnapshot(dirPath string, noSync bool, retainCount int) (*FileSnapshot, error) {
if err := os.MkdirAll(dirPath, dirMode); err != nil && !os.IsExist(err) {
return nil, err
}
snapshot := &FileSnapshot{
dir: dirPath,
noSync: noSync,
retainCount: retainCount,
}
if err := snapshot.testCreatePermission(); err != nil {
return nil, fmt.Errorf("test create permissions failed :%s", err)
}
return snapshot, nil
}
func (f *FileSnapshot) testCreatePermission() error {
filePath := filepath.Join(f.dir, testFile)
file, err := os.Create(filePath)
if err != nil {
return err
}
_ = file.Close()
return os.Remove(filePath)
}
func (f *FileSnapshot) readMeta(id string) (meta *fileSnapshotMeta, err error) {
metaFilePath := filepath.Join(f.dir, id, metaFile)
file, err := os.Open(metaFilePath)
if err != nil {
return nil, err
}
err = json.NewDecoder(file).Decode(&meta)
return
}
func (f *FileSnapshot) Open(id string) (*SnapshotMeta, io.ReadCloser, error) {
meta, err := f.readMeta(id)
if err != nil {
return nil, nil, err
}
snapshotFilePath := filepath.Join(f.dir, id, snapshotFile)
file, err := os.Open(snapshotFilePath)
defer func() {
if err != nil {
file.Close()
}
}()
if err != nil {
return nil, nil, err
}
hash64 := newCRC64()
if _, err = io.Copy(hash64, file); err != nil {
return nil, nil, err
}
if !bytes.Equal(hash64.Sum(nil), meta.CRC) {
return nil, nil, errors.New("CRC mismatch")
}
if _, err = file.Seek(0, 0); err != nil {
return nil, nil, err
}
return meta.SnapshotMeta, &bufferReader{
buf: bufio.NewReader(file),
file: file,
}, nil
}
func (f *FileSnapshot) List() (list []*SnapshotMeta, err error) {
metaList, err := f.getSnapshots()
if err != nil {
return nil, err
}
for _, meta := range metaList {
list = append(list, meta.SnapshotMeta)
if len(list) == f.retainCount {
break
}
}
return
}
func (f *FileSnapshot) getSnapshots() (metaList []*fileSnapshotMeta, err error) {
dirList, err := os.ReadDir(f.dir)
if err != nil {
return nil, err
}
for _, entry := range dirList {
if !entry.IsDir() {
continue
}
if strings.HasSuffix(entry.Name(), tmpFileSuffix) {
continue
}
meta, err := f.readMeta(entry.Name())
if err != nil {
return nil, err
}
metaList = append(metaList, meta)
}
sortMetaList(metaList)
return
}
func (s *FileSnapshotSink) writeMeta() error {
fileName := filepath.Join(s.dir, metaFile)
file, err := os.Create(fileName)
if err != nil {
return err
}
defer file.Close()
writer := bufio.NewWriter(file)
enc := json.NewEncoder(file)
enc.SetIndent("", " ")
if err = enc.Encode(s.meta); err != nil {
return err
}
if err = writer.Flush(); err != nil {
return err
}
if !s.noSync {
if err = file.Sync(); err != nil {
return err
}
}
return nil
}
func (f *FileSnapshot) Create(version SnapshotVersion, index, term uint64, configuration ClusterInfo, configurationIndex uint64, rpc RpcInterface) (SnapshotSink, error) {
name := snapshotName(term, index)
snapshotDir := filepath.Join(f.dir, name+tmpFileSuffix)
if err := os.MkdirAll(snapshotDir, dirMode); err != nil {
return nil, err
}
sink := &FileSnapshotSink{
snapshotStore: f,
dir: snapshotDir,
noSync: f.noSync,
parentPath: f.dir,
meta: &fileSnapshotMeta{
SnapshotMeta: &SnapshotMeta{
Version: version,
ID: name,
Index: index,
Term: term,
Configuration: configuration,
ConfigurationIndex: configurationIndex,
},
},
hash: newCRC64(),
}
if err := sink.writeMeta(); err != nil {
return nil, err
}
snapshotPath := filepath.Join(snapshotDir, snapshotFile)
if file, err := os.Create(snapshotPath); err != nil {
return nil, err
} else {
sink.file = file
sink.writer = bufio.NewWriter(io.MultiWriter(sink.hash, file))
}
return sink, nil
}
func (f *FileSnapshotSink) Write(p []byte) (n int, err error) {
return f.writer.Write(p)
}
func (f *FileSnapshotSink) Close() error {
if f.close {
return nil
}
f.close = true
if err := f.finalize(); err != nil {
return err
}
if err := f.writeMeta(); err != nil {
return err
}
newPath := strings.TrimSuffix(f.dir, tmpFileSuffix)
if err := os.Rename(f.dir, newPath); err != nil {
return err
}
if err := func() error {
if !(!f.noSync && runtime.GOOS != "windows") {
return nil
}
// 对于目录也需要执行 fsync 操作 https://man7.org/linux/man-pages/man2/fsync.2.html
file, err := os.Open(f.parentPath)
if err != nil {
return err
}
defer file.Close()
return file.Sync()
}(); err != nil {
return err
}
return f.snapshotStore.reapSnapshot()
}
func (f *FileSnapshotSink) ID() string {
return f.meta.ID
}
func (f *FileSnapshotSink) Cancel() error {
if err := f.finalize(); err != nil {
return err
}
return os.RemoveAll(f.dir)
}
func (f *FileSnapshotSink) finalize() error {
if err := f.writer.Flush(); err != nil {
return err
}
if !f.noSync {
if err := f.file.Sync(); err != nil {
return err
}
}
fileInfo, err := f.file.Stat()
if err != nil {
return err
}
if err = f.file.Close(); err != nil {
return err
}
f.meta.Size = fileInfo.Size()
f.meta.CRC = f.hash.Sum(nil)
return nil
}
func (f *FileSnapshot) reapSnapshot() error {
metaList, err := f.getSnapshots()
if err != nil {
return err
}
if len(metaList) < f.retainCount {
return nil
}
for _, meta := range metaList[f.retainCount:] {
filePath := filepath.Join(f.dir, meta.ID)
if err := os.RemoveAll(filePath); err != nil {
return err
}
}
return nil
}