-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblock_manager.go
193 lines (160 loc) · 3.98 KB
/
block_manager.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
package nitro
import (
"fmt"
"io"
"os"
"path/filepath"
"sync"
"sync/atomic"
"syscall"
)
var useLinuxHolePunch = false
// TODO: Reopen fds on error
type BlockManager interface {
DeleteBlock(bptr blockPtr) error
WriteBlock(bs []byte, shard int) (blockPtr, error)
ReadBlock(bptr blockPtr, buf []byte) error
}
func newBlockPtr(shard int, off int64) blockPtr {
off |= int64(shard) << 55
return blockPtr(off)
}
func (ptr blockPtr) Offset() int64 {
off := int64(ptr) & ^(0xff << 55)
return off
}
func (ptr blockPtr) Shard() int {
shard := int(int64(ptr) >> 55)
return shard
}
type fileBlockManager struct {
wlocks []sync.Mutex
wfds []*os.File
rfds []*os.File
wpos []int64
freeBlocks [][]int64
}
func newFileBlockManager(nfiles int, path string) (*fileBlockManager, error) {
var fd *os.File
var err error
fbm := &fileBlockManager{}
defer func() {
if err != nil {
for _, wfd := range fbm.wfds {
wfd.Close()
}
for _, rfd := range fbm.rfds {
rfd.Close()
}
}
}()
fbm.wlocks = make([]sync.Mutex, nfiles)
fbm.wpos = make([]int64, nfiles)
fbm.freeBlocks = make([][]int64, nfiles)
for i := 0; i < nfiles; i++ {
fpath := filepath.Join(path, fmt.Sprintf("blockstore-%d.data", i))
fd, err = os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
return nil, err
}
fbm.wfds = append(fbm.wfds, fd)
fbm.wpos[i], err = fd.Seek(0, 2)
if err != nil {
return nil, err
}
fbm.wpos[i] += fbm.wpos[i] % blockSize
fd, err = os.Open(fpath)
if err != nil {
return nil, err
}
fbm.rfds = append(fbm.rfds, fd)
fbm.freeBlocks[i] = make([]int64, 0)
}
return fbm, err
}
func (fbm *fileBlockManager) DeleteBlock(bptr blockPtr) error {
shard := bptr.Shard()
if useLinuxHolePunch {
return punchHole(fbm.wfds[shard], bptr.Offset(), blockSize)
}
fbm.wlocks[shard].Lock()
defer fbm.wlocks[shard].Unlock()
fbm.freeBlocks[shard] = append(fbm.freeBlocks[shard], bptr.Offset())
return nil
}
func (fbm *fileBlockManager) WriteBlock(bs []byte, shard int) (blockPtr, error) {
shard = shard % len(fbm.wpos)
fbm.wlocks[shard].Lock()
var pos int64
flist := fbm.freeBlocks[shard]
if !useLinuxHolePunch && len(flist) > 0 {
pos = flist[len(flist)-1]
flist = flist[0 : len(flist)-1]
fbm.freeBlocks[shard] = flist
} else {
pos = fbm.wpos[shard]
fbm.wpos[shard] += blockSize
}
fbm.wlocks[shard].Unlock()
_, err := fbm.wfds[shard].WriteAt(bs, pos)
if err != nil {
return 0, err
}
bptr := newBlockPtr(shard, pos)
return bptr, nil
}
func (fbm *fileBlockManager) ReadBlock(bptr blockPtr, buf []byte) error {
shard := bptr.Shard()
n, err := fbm.rfds[shard].ReadAt(buf, bptr.Offset())
if err == io.EOF {
for ; n < len(buf); n++ {
buf[n] = 0
}
err = nil
}
return err
}
type mmapBlockManager struct {
file *os.File
offset int64
data []byte
}
const maxFileOffset = 16000000000000 // 16TB
func newMmapBlockManager(dir string) (*mmapBlockManager, error) {
// TODO: Ability to reuse file and update offset
file := filepath.Join(dir, "blockstore-mmap.data")
mbm := new(mmapBlockManager)
if f, err := os.Create(file); err == nil {
if _, err := f.WriteAt([]byte("EOF"), maxFileOffset); err != nil {
return nil, err
}
f.Close()
}
if f, err := os.OpenFile(file, os.O_RDWR, 0755); err != nil {
return nil, err
} else {
mbm.file = f
mbm.data, err = syscall.Mmap(int(f.Fd()), 0,
maxFileOffset, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
if err != nil {
return nil, err
}
}
return mbm, nil
}
func (mbm *mmapBlockManager) WriteBlock(bs []byte, shard int) (blockPtr, error) {
pos := atomic.AddInt64(&mbm.offset, blockSize)
pos -= blockSize
copy(mbm.data[pos:], bs)
bptr := newBlockPtr(0, pos)
return bptr, nil
}
func (mbm *mmapBlockManager) DeleteBlock(bptr blockPtr) error {
pos := bptr.Offset()
return mmapPunchHole(mbm.data[pos : pos+blockSize])
}
func (mbm mmapBlockManager) ReadBlock(bptr blockPtr, buf []byte) error {
pos := bptr.Offset()
copy(buf[:blockSize], mbm.data[pos:pos+blockSize])
return nil
}