-
Notifications
You must be signed in to change notification settings - Fork 1
/
range_cache.go
265 lines (200 loc) · 4.78 KB
/
range_cache.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
package lsvd
import (
"context"
"fmt"
"io"
"os"
lru "github.com/hashicorp/golang-lru/v2"
"golang.org/x/sys/unix"
)
type rangeCacheKey struct {
Seg SegmentId
Chunk int64
}
type RangeCache struct {
path string
f *os.File
chunk int64
max int64
fetch func(ctx context.Context, seg SegmentId, data []byte, off int64) error
lru *lru.Cache[rangeCacheKey, int64]
chunkBuf []byte
cacheRegion []byte
}
type RangeCacheOptions struct {
Path string
ChunkSize int64
MaxSize int64
Fetch func(ctx context.Context, seg SegmentId, data []byte, off int64) error
}
func NewRangeCache(opts RangeCacheOptions) (*RangeCache, error) {
f, err := os.Create(opts.Path)
if err != nil {
return nil, err
}
maxChunks := opts.MaxSize / opts.ChunkSize
if maxChunks == 0 {
return nil, fmt.Errorf("max size too small")
}
l, err := lru.New[rangeCacheKey, int64](int(maxChunks))
if err != nil {
return nil, err
}
fd := f.Fd()
data, err := unix.Mmap(int(fd), 0, int(opts.MaxSize), unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED)
if err != nil {
return nil, err
}
rc := &RangeCache{
path: opts.Path,
f: f,
chunk: opts.ChunkSize,
max: maxChunks,
fetch: opts.Fetch,
lru: l,
chunkBuf: make([]byte, opts.ChunkSize),
cacheRegion: data,
}
return rc, nil
}
func (r *RangeCache) Close() error {
if r.cacheRegion != nil {
unix.Munmap(r.cacheRegion)
r.cacheRegion = nil
}
if r.f != nil {
r.f.Close()
r.f = nil
}
return nil
}
func (r *RangeCache) ReadAt(ctx context.Context, seg SegmentId, buf []byte, off int64) (int, error) {
firstChunk := off / r.chunk
lastChunk := (off + int64(len(buf)) - 1) / r.chunk
tot := len(buf)
innerOff := off % r.chunk
chunkData := r.chunkBuf
for chunk := firstChunk; chunk <= lastChunk; chunk++ {
ok, mem := r.memChunk(seg, chunk)
if !ok {
extentCacheMiss.Inc()
err := r.fetch(ctx, seg, chunkData, chunk*r.chunk)
if err != nil {
return 0, err
}
_, err = r.saveChunk(seg, chunk, chunkData)
if err != nil {
return 0, err
}
mem = chunkData
} else {
extentCacheHits.Inc()
}
copied := copy(buf, mem[innerOff:])
if copied < len(buf) {
buf = buf[copied:]
}
// Reset back because we want to read from the front of all future chunks
innerOff = 0
}
return tot, nil
}
type CachePosition struct {
fd *os.File
off int64
size int64
}
func (r *RangeCache) CachePositions(ctx context.Context, seg SegmentId, total, off int64, ret []CachePosition) ([]CachePosition, error) {
firstChunk := off / r.chunk
lastChunk := (off + total - 1) / r.chunk
innerOff := off % r.chunk
chunkData := r.chunkBuf
left := total
for chunk := firstChunk; chunk <= lastChunk; chunk++ {
chunkOff := innerOff
// it's possible r.chunk is wrong for the last chunk, but because we're only request
// extents that fall within the chunk regardless, the bytes left will never run
// into the area that isn't actually there.
chunkLeft := r.chunk - chunkOff
var consumed int64
if chunkLeft >= left {
consumed = left
} else {
consumed = chunkLeft
}
off, ok := r.lru.Get(rangeCacheKey{seg, chunk})
if ok {
extentCacheHits.Inc()
} else {
extentCacheMiss.Inc()
err := r.fetch(ctx, seg, chunkData, chunk*r.chunk)
if err != nil {
return nil, err
}
off, err = r.saveChunk(seg, chunk, chunkData)
if err != nil {
return nil, err
}
}
ret = append(ret, CachePosition{
fd: r.f,
off: off + innerOff,
size: consumed,
})
left -= consumed
// Reset back because we want to read from the front of all future chunks
innerOff = 0
}
return ret, nil
}
func (r *RangeCache) memChunk(seg SegmentId, chunk int64) (bool, []byte) {
off, ok := r.lru.Get(rangeCacheKey{seg, chunk})
if !ok {
return false, nil
}
return true, r.cacheRegion[off : off+r.chunk]
}
func (r *RangeCache) readChunk(seg SegmentId, chunk int64, data []byte) (bool, error) {
off, ok := r.lru.Get(rangeCacheKey{seg, chunk})
if !ok {
return false, nil
}
n, err := r.f.ReadAt(data, off)
if err != nil {
return false, err
}
if n != len(data) {
return false, io.ErrShortWrite
}
return true, nil
}
func (r *RangeCache) saveChunk(seg SegmentId, chunk int64, data []byte) (int64, error) {
if r.lru.Len() < int(r.max) {
off, err := r.f.Seek(0, io.SeekCurrent)
if err != nil {
return 0, err
}
n, err := r.f.Write(data)
if err != nil {
return 0, err
}
if n != len(data) {
return 0, io.ErrShortWrite
}
r.lru.Add(rangeCacheKey{seg, chunk}, off)
return off, nil
}
_, off, ok := r.lru.RemoveOldest()
if !ok {
return 0, fmt.Errorf("misused lru is empty")
}
n, err := r.f.WriteAt(data, off)
if err != nil {
return 0, err
}
if n != len(data) {
return 0, io.ErrShortWrite
}
r.lru.Add(rangeCacheKey{seg, chunk}, off)
return off, nil
}