/
blockStore.go
419 lines (346 loc) · 8.83 KB
/
blockStore.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package sbs
import (
"bytes"
"compress/zlib"
"encoding/binary"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"time"
"github.com/hashicorp/raft"
"github.com/kpango/fastime"
"github.com/sirupsen/logrus"
volumesAPI "github.com/tcfw/vpc/pkg/api/v1/volumes"
)
//StoreCommandOp block command operation
type StoreCommandOp uint16
const (
//OpWrite write block
OpWrite StoreCommandOp = iota
//OpZeros replace block with zeros
OpZeros
//OpTrim _may_ be discarded
OpTrim
//OpFlush asked to persist all data
OpFlush
//BlockSize block size per raft log
BlockSize = 16 * 1 << 10 //16KB
//BlocksPerFile maximum number of blocks that should be in a file
BlocksPerFile = 100 * 1 << 20 / BlockSize //100MB files
blockMaxSize uint64 = BlockSize * BlocksPerFile
//VolDescSizeMultiplier is the conversion factor for the volume description to bytes
VolDescSizeMultiplier = 1 << 30 //volume definition size is in GB
)
//OutOfBounds when the given offset+data len is greater than allocated volume size
type OutOfBounds uint64
func (oob OutOfBounds) Error() string {
return fmt.Sprintf("offset %d out of bounds", oob)
}
//StoreCommand block store mutation command
type StoreCommand struct {
Op StoreCommandOp `json:"op"`
Offset uint64 `json:"offset"`
Length uint32 `json:"length"`
Data []byte `json:"data,omitempty"`
}
//Encode encodes the block command to binary formats transmission
func (bsc *StoreCommand) Encode() ([]byte, error) {
p := bytes.NewBuffer(nil)
binary.Write(p, binary.LittleEndian, bsc.Op)
binary.Write(p, binary.LittleEndian, bsc.Offset)
binary.Write(p, binary.LittleEndian, bsc.Length)
length := len(bsc.Data)
for length > 0 {
n, err := p.Write(bsc.Data)
if err != nil {
return nil, err
}
length -= n
}
return p.Bytes(), nil
}
//Decode decodes the block command from transmission
func (bsc *StoreCommand) Decode(p []byte) error {
bsc.Op = StoreCommandOp(binary.LittleEndian.Uint16(p[0:2]))
bsc.Offset = binary.LittleEndian.Uint64(p[2:10])
bsc.Length = binary.LittleEndian.Uint32(p[10:14])
bsc.Data = p[14:]
return nil
}
//BlockStore block store backing e.g. local disks
type BlockStore struct {
log *logrus.Logger
backing map[uint64]*block
BaseDir string
def *volumesAPI.Volume
mu sync.RWMutex
size uint64
}
type block struct {
f *os.File
mu sync.Mutex
lastTouched time.Time
}
func (b *block) Close() error {
return b.f.Close()
}
//NewBlockStore provides a new store for volumes
func NewBlockStore(peerID string, d *volumesAPI.Volume, baseDir string, l *logrus.Logger) *BlockStore {
dir := fmt.Sprintf("%s/%s/%s", baseDir, peerID, d.GetId())
os.MkdirAll(fmt.Sprintf("%s/blocks", dir), os.FileMode(0740)|os.ModeDir)
return &BlockStore{
def: d, log: l,
backing: map[uint64]*block{},
BaseDir: dir,
size: uint64(d.Size * VolDescSizeMultiplier),
}
}
//SizeOnDisk goes through each block and queries the underlying filesystem for it's current size
func (b *BlockStore) SizeOnDisk() (uint64, error) {
var fSize uint64
err := filepath.Walk(fmt.Sprintf("%s/blocks", b.BaseDir), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
fSize += uint64(info.Size())
}
return err
})
return fSize, err
}
//Apply log is invoked once a log entry is committed and commits
//the change to the block store
func (b *BlockStore) Apply(l *raft.Log) interface{} {
switch l.Type {
case raft.LogCommand:
cmd := &StoreCommand{}
if err := cmd.Decode(l.Data); err != nil {
return err
}
return b.apply(cmd)
case raft.LogNoop, raft.LogAddPeerDeprecated:
return nil
default:
return fmt.Errorf("unsupported log type")
}
}
//Snapshot is used to support log compaction
func (b *BlockStore) Snapshot() (raft.FSMSnapshot, error) {
return &blockStoreSnapshotter{store: b}, nil
}
//Restore is used to restore the block store data from a snapshot
func (b *BlockStore) Restore(r io.ReadCloser) error {
b.log.Info("Starting raft snapshot restore")
buf := make([]byte, BlockSize)
var seek uint64
//unwrap snapshot via zlib
zReader, err := zlib.NewReader(r)
if err != nil {
return fmt.Errorf("failed to open snapshot: %s", err)
}
defer zReader.Close()
defer r.Close()
for {
n, err := zReader.Read(buf)
if err == io.EOF {
break
}
if err != nil {
return err
}
if err := b.write(&StoreCommand{
Op: OpWrite,
Offset: seek,
Length: uint32(n),
Data: buf[:n],
}); err != nil {
return err
}
seek += uint64(n)
}
return nil
}
func (b *BlockStore) clearAllLocalStorage() error {
b.Close()
b.mu.Lock()
defer b.mu.Unlock()
return os.RemoveAll(b.BaseDir)
}
//apply switches out the various commands
func (b *BlockStore) apply(c *StoreCommand) error {
switch c.Op {
case OpZeros, OpWrite:
return b.write(c)
case OpFlush:
return b.flush()
default:
return fmt.Errorf("unknown op")
}
}
//write applies the write operations to the offsets in the block set
func (b *BlockStore) write(c *StoreCommand) error {
//vol bounds check
if c.Offset+uint64(c.Length) > b.size {
return OutOfBounds(c.Offset + uint64(c.Length))
}
//Allow for lengths larger than the given bytes
if c.Op == OpZeros {
d := make([]byte, c.Length)
copy(d, c.Data)
c.Data = d
}
//support writes that span multiple blocks
length := uint64(c.Length)
offset := c.Offset
var bStart uint64
var nextBlockStart uint64
for length > 0 {
bn := b.blockOffset(offset)
bl, err := b.getBlock(bn)
if err != nil {
return err
}
bLen := length
nextBlockStart = (bn + 1) * blockMaxSize
if bLen+offset > nextBlockStart {
bLen = nextBlockStart - offset
}
fOffset := offset % blockMaxSize
bl.mu.Lock()
n, err := bl.f.WriteAt(c.Data[bStart:bStart+bLen], int64(fOffset))
if err != nil {
bl.mu.Unlock()
return err
}
bl.lastTouched = fastime.Now()
bl.mu.Unlock()
n64 := uint64(n)
length -= n64
offset += n64
bStart += n64
}
return nil
}
//ReadAt allows for the block store to be read from block sets
func (b *BlockStore) ReadAt(p []byte, offset uint64) (int, error) {
//bounds check
if offset > b.size {
return 0, OutOfBounds(offset)
}
if offset < 0 {
return 0, fmt.Errorf("invalid offset")
}
length := uint64(len(p))
var nn uint64
var nextBlockStart uint64
for length > 0 {
bn := b.blockOffset(offset)
bl, err := b.getBlock(bn)
if err != nil {
return 0, fmt.Errorf("failed to get block: %s", err)
}
bLen := length
nextBlockStart = (bn + 1) * blockMaxSize
if bLen+offset > nextBlockStart {
bLen = nextBlockStart - offset
}
fOffset := offset % blockMaxSize
bl.mu.Lock()
n, err := bl.f.ReadAt(p[nn:nn+bLen], int64(fOffset))
if err != nil {
bl.mu.Unlock()
return 0, fmt.Errorf("failed to read at local offset %d: %s", fOffset, err)
}
bl.lastTouched = fastime.Now()
bl.mu.Unlock()
n64 := uint64(n)
nn += n64
offset += n64
length -= n64
}
return int(nn), nil
}
//flush forces os to sync blocks to persistent storage
func (b *BlockStore) flush() error {
for _, bl := range b.backing {
bl.mu.Lock()
if err := bl.f.Sync(); err != nil {
bl.mu.Unlock()
return err
}
bl.mu.Unlock()
}
return nil
}
//getBlock storage of individual block sets
func (b *BlockStore) getBlock(n uint64) (*block, error) {
b.mu.RLock()
if block, ok := b.backing[n]; ok {
b.mu.RUnlock()
return block, nil
}
b.mu.RUnlock()
fLoc := fmt.Sprintf("%s/blocks/%d.raw", b.BaseDir, n)
f, err := os.OpenFile(fLoc, os.O_CREATE|os.O_RDWR, os.FileMode(0640))
if err != nil {
return nil, err
}
if err := f.Truncate(int64(blockMaxSize)); err != nil {
return nil, err
}
bl := &block{f: f}
b.mu.Lock()
defer b.mu.Unlock()
b.backing[n] = bl
return bl, nil
}
//Close closes all open blocks
func (b *BlockStore) Close() {
b.mu.Lock()
defer b.mu.Unlock()
for i, block := range b.backing {
block.Close()
delete(b.backing, i)
}
}
//blockOffset which file number the offset should be in
func (b *BlockStore) blockOffset(offset uint64) uint64 {
return offset / blockMaxSize
}
//blockStoreSnapshotter helps create snapshots of the block store
type blockStoreSnapshotter struct {
store *BlockStore
seek uint64
}
// Persist should dump all necessary state to the WriteCloser 'sink',
// and call sink.Close() when finished or call sink.Cancel() on error.
func (bss *blockStoreSnapshotter) Persist(sink raft.SnapshotSink) error {
buf := make([]byte, BlockSize)
//Wrap snapshots in zlip compression
zWrite := zlib.NewWriter(sink)
for bss.seek < bss.store.size {
n, err := bss.store.ReadAt(buf, bss.seek)
if err == io.EOF {
break
}
if err != nil {
return err
}
nWrite, err := zWrite.Write(buf[:n])
if err != nil {
return err
}
bss.seek += uint64(nWrite)
}
zWrite.Close()
sink.Close()
return nil
}
// Release is invoked when we are finished with the snapshot.
func (bss *blockStoreSnapshotter) Release() {
bss.store.Close()
return
}