-
Notifications
You must be signed in to change notification settings - Fork 0
/
vol.go
283 lines (244 loc) · 7.05 KB
/
vol.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
package bakemono
import (
"encoding/binary"
"errors"
"hash/crc32"
"log"
"os"
"time"
)
type Offset uint64
type segId uint64
// TODO(meta): flush/rebuild to A/B alternately
var (
HeaderSize = binary.Size(&VolHeaderFooter{})
DirSize = binary.Size(&Dir{})
)
// Vol is a volume represents a file on disk.
// structure: Meta_A(header, dirs, footer) + Meta_B(header, dirs, footer) + Data(Chunks)
// dirs are organized segment->bucket->dir logically.
type Vol struct {
Path string
Fp OffsetReaderWriterCloser
Dm *DirManager
WritePos Offset
Header *VolHeaderFooter
SectorSize uint32
Length Offset
ChunkAvgSize Offset // average chunk size, adjusted by user.
ChunksMaxNum Offset // max chunks num in this vol. calculated from ChunkAvgSize and Length
HeaderAOffset Offset
FooterAOffset Offset
HeaderBOffset Offset
FooterBOffset Offset
DataOffset Offset
DirAOffset Offset
closeCh chan struct{}
flushCh chan struct{}
}
// VolOptions to init a Vol.
// Note: do file open/truncate outside.
type VolOptions struct {
Fp OffsetReaderWriterCloser
FileSize Offset
ChunkAvgSize Offset
FlushMetaInterval time.Duration
}
// NewDefaultVolOptions creates a VolOptions with a file path.
// Note: It will create a file if not exists, and truncate it to the given sizeInternal.
func NewDefaultVolOptions(path string, fileSize, avgChunkSize uint64) (*VolOptions, error) {
log.Printf("creating vol options with file truncate, path: %s, fileSize: %d, avgChunkSize: %d", path, fileSize, avgChunkSize)
fp, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return nil, err
}
log.Printf("file opened, try to truncate to sizeInternal: %d", fileSize)
err = fp.Truncate(int64(fileSize))
if err != nil {
return nil, err
}
return &VolOptions{
Fp: fp,
FileSize: Offset(fileSize),
ChunkAvgSize: Offset(avgChunkSize),
FlushMetaInterval: 60 * time.Second,
}, nil
}
// Check checks if the VolOptions is valid.
func (cfg *VolOptions) Check() error {
if cfg.Fp == nil {
return errors.New("invalid config: Fp is nil")
}
if cfg.FileSize == 0 {
return errors.New("invalid config: FileSize is 0")
}
if cfg.ChunkAvgSize == 0 {
return errors.New("invalid config: ChunkAvgSize is 0")
}
return nil
}
func (v *Vol) Init(cfg *VolOptions) (corrupted bool, err error) {
log.Printf("initing vol, config: %+v", cfg)
err = cfg.Check()
if err != nil {
return false, err
}
// channel init
v.closeCh = make(chan struct{})
v.flushCh = make(chan struct{})
// storage interface
v.Fp = cfg.Fp
// dir manager size init. note: dir data setup in next step
v.Dm = &DirManager{}
expectedDirNum := (cfg.FileSize - 4*Offset(HeaderSize)) / (cfg.ChunkAvgSize + 2*Offset(DirSize))
v.ChunksMaxNum = v.Dm.Init(expectedDirNum)
// calculate vol offsets
v.prepareOffsets(cfg)
err = v.buildMetaFromFp()
if err != nil {
log.Printf("warn: build meta from fp failed, file may corrupted, err: %v", err)
corrupted = true
v.initEmptyMeta()
}
// sync meta to vol, avoid mutex for header
v.WritePos = v.DataOffset
// start sync flush thread
go v.SyncFlushLoop(cfg.FlushMetaInterval)
log.Printf("init vol done, corrupted: %v, err: %v", corrupted, err)
return corrupted, nil
}
// Close closes the Vol.
func (v *Vol) Close() error {
close(v.closeCh)
<-v.flushCh
return v.Fp.Close()
}
// SyncFlushLoop flushes metadata to disk periodically.
func (v *Vol) SyncFlushLoop(interval time.Duration) {
for {
select {
case <-v.closeCh:
close(v.flushCh)
return
case <-time.After(interval):
err := v.flushMetaToFp()
if err != nil {
log.Printf("error: flush meta to fp failed, err: %v", err)
} else {
log.Printf("info: flush meta to fp done")
}
}
}
}
// prepareOffsets calculates offsets and block numbers before initing a Vol.
func (v *Vol) prepareOffsets(cfg *VolOptions) {
v.ChunkAvgSize = cfg.ChunkAvgSize
v.SectorSize = 512
v.Length = cfg.FileSize
// calculate sizeInternal to allocate
// Meta_A(header, dirs, footer) + Meta_B(header, dirs, footer) + Data(Chunks)
HeaderFooterSize := Offset(HeaderSize)
DirSize := Offset(binary.Size(&Dir{}))
// TotalChunk init by DirManager
//TotalChunks := (cfg.FileSize - 4*HeaderFooterSize) / (cfg.ChunkAvgSize + 2*DirSize)
MetaSize := 2 * (2*HeaderFooterSize + v.ChunksMaxNum*DirSize)
DataSize := cfg.FileSize - MetaSize
log.Printf("initing vol: ChunksMaxNum: %d, MetaSize: %d, DataSize: %d, VolLength: %d", v.ChunksMaxNum, MetaSize, DataSize, v.Length)
// calculate offsets
v.HeaderAOffset = 0
v.FooterAOffset = HeaderFooterSize + v.ChunksMaxNum*DirSize
v.HeaderBOffset = v.FooterAOffset + HeaderFooterSize
v.FooterBOffset = v.HeaderBOffset + HeaderFooterSize + v.ChunksMaxNum*DirSize
v.DataOffset = MetaSize
v.DirAOffset = v.HeaderAOffset + HeaderFooterSize
log.Printf("initing vol: ActualLength: %d, ChunksMaxNum: %d", v.Length, v.ChunksMaxNum)
}
// buildMetaFromFp builds new empty metadata.
func (v *Vol) initEmptyMeta() {
v.Header = &VolHeaderFooter{
Magic: MagicBocchi,
CreateUnixTime: time.Now().Unix(),
WritePos: v.DataOffset,
SyncSerial: 0,
//WriteSerial: 0,
}
v.Dm.InitEmptyDirs()
}
// buildMetaFromFp builds metadata from io.
func (v *Vol) buildMetaFromFp() error {
h := &VolHeaderFooter{}
data := make([]byte, HeaderSize)
_, err := v.Fp.ReadAt(data, int64(v.HeaderAOffset))
if err != nil {
return err
}
err = h.UnmarshalBinary(data)
if err != nil {
return err
}
v.Header = h
DirSize := Offset(binary.Size(&Dir{})) * v.ChunksMaxNum
dirsRaw := make([]byte, DirSize)
_, err = v.Fp.ReadAt(dirsRaw, int64(v.DirAOffset))
if err != nil {
return err
}
DirsCheckSum := crc32.ChecksumIEEE(dirsRaw)
log.Printf("DirsCheckSum: %d, v.Header.DirsChecksum: %d", DirsCheckSum, v.Header.DirsChecksum)
if DirsCheckSum != v.Header.DirsChecksum {
return errors.New("invalid dir checksum")
}
err = v.Dm.UnmarshalBinary(dirsRaw)
if err != nil {
return err
}
return nil
}
// flushMetaToFp flushes metadata to io.
func (v *Vol) flushMetaToFp() error {
v.Header.Magic = MagicBocchi
v.Header.MajorVersion = MajorVersion
v.Header.MinorVersion = MinorVersion
v.Header.WritePos = v.WritePos
v.Header.SyncSerial++
dirsRaw, err := v.Dm.MarshalBinary()
if err != nil {
return err
}
v.Header.DirsChecksum = crc32.ChecksumIEEE(dirsRaw)
err = v.flushHeaderFooterToFp()
if err != nil {
return err
}
err = v.flushDirRawToFp(dirsRaw)
if err != nil {
return err
}
return nil
}
func (v *Vol) flushHeaderFooterToFp() error {
data, err := v.Header.MarshalBinary()
if err != nil {
return err
}
offsets := []Offset{v.HeaderAOffset, v.FooterAOffset, v.HeaderBOffset, v.FooterBOffset}
for _, off := range offsets {
_, err = v.Fp.WriteAt(data, int64(off))
if err != nil {
return err
}
}
return nil
}
func (v *Vol) flushDirRawToFp(data []byte) error {
// check if data size is correct
DirSize := Offset(binary.Size(&Dir{})) * v.ChunksMaxNum
if DirSize < Offset(len(data)) {
return errors.New("invalid dir data size")
}
_, err := v.Fp.WriteAt(data, int64(v.DirAOffset))
if err != nil {
return err
}
return nil
}