-
Notifications
You must be signed in to change notification settings - Fork 178
/
file.go
421 lines (390 loc) · 11.2 KB
/
file.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
420
421
package fastcache
import (
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime"
"github.com/golang/snappy"
)
// SaveToFile atomically saves cache data to the given filePath using a single
// CPU core.
//
// SaveToFile may be called concurrently with other operations on the cache.
//
// The saved data may be loaded with LoadFromFile*.
//
// See also SaveToFileConcurrent for faster saving to file.
func (c *Cache) SaveToFile(filePath string) error {
return c.SaveToFileConcurrent(filePath, 1)
}
// SaveToFileConcurrent saves cache data to the given filePath using concurrency
// CPU cores.
//
// SaveToFileConcurrent may be called concurrently with other operations
// on the cache.
//
// The saved data may be loaded with LoadFromFile*.
//
// See also SaveToFile.
func (c *Cache) SaveToFileConcurrent(filePath string, concurrency int) error {
// Create dir if it doesn't exist.
dir := filepath.Dir(filePath)
if _, err := os.Stat(dir); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("cannot stat %q: %s", dir, err)
}
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("cannot create dir %q: %s", dir, err)
}
}
// Save cache data into a temporary directory.
tmpDir, err := ioutil.TempDir(dir, "fastcache.tmp.")
if err != nil {
return fmt.Errorf("cannot create temporary dir inside %q: %s", dir, err)
}
defer func() {
if tmpDir != "" {
_ = os.RemoveAll(tmpDir)
}
}()
gomaxprocs := runtime.GOMAXPROCS(-1)
if concurrency <= 0 || concurrency > gomaxprocs {
concurrency = gomaxprocs
}
if err := c.save(tmpDir, concurrency); err != nil {
return fmt.Errorf("cannot save cache data to temporary dir %q: %s", tmpDir, err)
}
// Remove old filePath contents, since os.Rename may return
// error if filePath dir exists.
if err := os.RemoveAll(filePath); err != nil {
return fmt.Errorf("cannot remove old contents at %q: %s", filePath, err)
}
if err := os.Rename(tmpDir, filePath); err != nil {
return fmt.Errorf("cannot move temporary dir %q to %q: %s", tmpDir, filePath, err)
}
tmpDir = ""
return nil
}
// LoadFromFile loads cache data from the given filePath.
//
// See SaveToFile* for saving cache data to file.
func LoadFromFile(filePath string) (*Cache, error) {
return load(filePath, 0)
}
// LoadFromFileOrNew tries loading cache data from the given filePath.
//
// The function falls back to creating new cache with the given maxBytes
// capacity if error occurs during loading the cache from file.
func LoadFromFileOrNew(filePath string, maxBytes int) *Cache {
c, err := load(filePath, maxBytes)
if err == nil {
return c
}
return New(maxBytes)
}
func (c *Cache) save(dir string, workersCount int) error {
if err := saveMetadata(c, dir); err != nil {
return err
}
// Save buckets by workersCount concurrent workers.
workCh := make(chan int, workersCount)
results := make(chan error)
for i := 0; i < workersCount; i++ {
go func(workerNum int) {
results <- saveBuckets(c.buckets[:], workCh, dir, workerNum)
}(i)
}
// Feed workers with work
for i := range c.buckets[:] {
workCh <- i
}
close(workCh)
// Read results.
var err error
for i := 0; i < workersCount; i++ {
result := <-results
if result != nil && err == nil {
err = result
}
}
return err
}
func load(filePath string, maxBytes int) (*Cache, error) {
maxBucketChunks, err := loadMetadata(filePath)
if err != nil {
return nil, err
}
if maxBytes > 0 {
maxBucketBytes := uint64((maxBytes + bucketsCount - 1) / bucketsCount)
expectedBucketChunks := (maxBucketBytes + chunkSize - 1) / chunkSize
if maxBucketChunks != expectedBucketChunks {
return nil, fmt.Errorf("cache file %s contains maxBytes=%d; want %d", filePath, maxBytes, expectedBucketChunks*chunkSize*bucketsCount)
}
}
// Read bucket files from filePath dir.
d, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("cannot open %q: %s", filePath, err)
}
defer func() {
_ = d.Close()
}()
fis, err := d.Readdir(-1)
if err != nil {
return nil, fmt.Errorf("cannot read files from %q: %s", filePath, err)
}
results := make(chan error)
workersCount := 0
var c Cache
for _, fi := range fis {
fn := fi.Name()
if fi.IsDir() || !dataFileRegexp.MatchString(fn) {
continue
}
workersCount++
go func(dataPath string) {
results <- loadBuckets(c.buckets[:], dataPath, maxBucketChunks)
}(filePath + "/" + fn)
}
err = nil
for i := 0; i < workersCount; i++ {
result := <-results
if result != nil && err == nil {
err = result
}
}
if err != nil {
return nil, err
}
// Initialize buckets, which could be missing due to incomplete or corrupted files in the cache.
// It is better initializing such buckets instead of returning error, since the rest of buckets
// contain valid data.
for i := range c.buckets[:] {
b := &c.buckets[i]
if len(b.chunks) == 0 {
b.chunks = make([][]byte, maxBucketChunks)
b.m = make(map[uint64]uint64)
}
}
return &c, nil
}
func saveMetadata(c *Cache, dir string) error {
metadataPath := dir + "/metadata.bin"
metadataFile, err := os.Create(metadataPath)
if err != nil {
return fmt.Errorf("cannot create %q: %s", metadataPath, err)
}
defer func() {
_ = metadataFile.Close()
}()
maxBucketChunks := uint64(cap(c.buckets[0].chunks))
if err := writeUint64(metadataFile, maxBucketChunks); err != nil {
return fmt.Errorf("cannot write maxBucketChunks=%d to %q: %s", maxBucketChunks, metadataPath, err)
}
return nil
}
func loadMetadata(dir string) (uint64, error) {
metadataPath := dir + "/metadata.bin"
metadataFile, err := os.Open(metadataPath)
if err != nil {
return 0, fmt.Errorf("cannot open %q: %s", metadataPath, err)
}
defer func() {
_ = metadataFile.Close()
}()
maxBucketChunks, err := readUint64(metadataFile)
if err != nil {
return 0, fmt.Errorf("cannot read maxBucketChunks from %q: %s", metadataPath, err)
}
if maxBucketChunks == 0 {
return 0, fmt.Errorf("invalid maxBucketChunks=0 read from %q", metadataPath)
}
return maxBucketChunks, nil
}
var dataFileRegexp = regexp.MustCompile(`^data\.\d+\.bin$`)
func saveBuckets(buckets []bucket, workCh <-chan int, dir string, workerNum int) error {
dataPath := fmt.Sprintf("%s/data.%d.bin", dir, workerNum)
dataFile, err := os.Create(dataPath)
if err != nil {
return fmt.Errorf("cannot create %q: %s", dataPath, err)
}
defer func() {
_ = dataFile.Close()
}()
zw := snappy.NewBufferedWriter(dataFile)
for bucketNum := range workCh {
if err := writeUint64(zw, uint64(bucketNum)); err != nil {
return fmt.Errorf("cannot write bucketNum=%d to %q: %s", bucketNum, dataPath, err)
}
if err := buckets[bucketNum].Save(zw); err != nil {
return fmt.Errorf("cannot save bucket[%d] to %q: %s", bucketNum, dataPath, err)
}
}
if err := zw.Close(); err != nil {
return fmt.Errorf("cannot close snappy.Writer for %q: %s", dataPath, err)
}
return nil
}
func loadBuckets(buckets []bucket, dataPath string, maxChunks uint64) error {
dataFile, err := os.Open(dataPath)
if err != nil {
return fmt.Errorf("cannot open %q: %s", dataPath, err)
}
defer func() {
_ = dataFile.Close()
}()
zr := snappy.NewReader(dataFile)
for {
bucketNum, err := readUint64(zr)
if err == io.EOF {
// Reached the end of file.
return nil
}
if bucketNum >= uint64(len(buckets)) {
return fmt.Errorf("unexpected bucketNum read from %q: %d; must be smaller than %d", dataPath, bucketNum, len(buckets))
}
if err := buckets[bucketNum].Load(zr, maxChunks); err != nil {
return fmt.Errorf("cannot load bucket[%d] from %q: %s", bucketNum, dataPath, err)
}
}
}
func (b *bucket) Save(w io.Writer) error {
b.mu.Lock()
b.cleanLocked()
b.mu.Unlock()
b.mu.RLock()
defer b.mu.RUnlock()
// Store b.idx, b.gen and b.m to w.
bIdx := b.idx
bGen := b.gen
chunksLen := 0
for _, chunk := range b.chunks {
if chunk == nil {
break
}
chunksLen++
}
kvs := make([]byte, 0, 2*8*len(b.m))
var u64Buf [8]byte
for k, v := range b.m {
binary.LittleEndian.PutUint64(u64Buf[:], k)
kvs = append(kvs, u64Buf[:]...)
binary.LittleEndian.PutUint64(u64Buf[:], v)
kvs = append(kvs, u64Buf[:]...)
}
if err := writeUint64(w, bIdx); err != nil {
return fmt.Errorf("cannot write b.idx: %s", err)
}
if err := writeUint64(w, bGen); err != nil {
return fmt.Errorf("cannot write b.gen: %s", err)
}
if err := writeUint64(w, uint64(len(kvs))/2/8); err != nil {
return fmt.Errorf("cannot write len(b.m): %s", err)
}
if _, err := w.Write(kvs); err != nil {
return fmt.Errorf("cannot write b.m: %s", err)
}
// Store b.chunks to w.
if err := writeUint64(w, uint64(chunksLen)); err != nil {
return fmt.Errorf("cannot write len(b.chunks): %s", err)
}
for chunkIdx := 0; chunkIdx < chunksLen; chunkIdx++ {
chunk := b.chunks[chunkIdx][:chunkSize]
if _, err := w.Write(chunk); err != nil {
return fmt.Errorf("cannot write b.chunks[%d]: %s", chunkIdx, err)
}
}
return nil
}
func (b *bucket) Load(r io.Reader, maxChunks uint64) error {
if maxChunks == 0 {
return fmt.Errorf("the number of chunks per bucket cannot be zero")
}
bIdx, err := readUint64(r)
if err != nil {
return fmt.Errorf("cannot read b.idx: %s", err)
}
bGen, err := readUint64(r)
if err != nil {
return fmt.Errorf("cannot read b.gen: %s", err)
}
kvsLen, err := readUint64(r)
if err != nil {
return fmt.Errorf("cannot read len(b.m): %s", err)
}
kvsLen *= 2 * 8
kvs := make([]byte, kvsLen)
if _, err := io.ReadFull(r, kvs); err != nil {
return fmt.Errorf("cannot read b.m: %s", err)
}
m := make(map[uint64]uint64, kvsLen/2/8)
for len(kvs) > 0 {
k := binary.LittleEndian.Uint64(kvs)
kvs = kvs[8:]
v := binary.LittleEndian.Uint64(kvs)
kvs = kvs[8:]
m[k] = v
}
maxBytes := maxChunks * chunkSize
if maxBytes >= maxBucketSize {
return fmt.Errorf("too big maxBytes=%d; should be smaller than %d", maxBytes, maxBucketSize)
}
chunks := make([][]byte, maxChunks)
chunksLen, err := readUint64(r)
if err != nil {
return fmt.Errorf("cannot read len(b.chunks): %s", err)
}
if chunksLen > uint64(maxChunks) {
return fmt.Errorf("chunksLen=%d cannot exceed maxChunks=%d", chunksLen, maxChunks)
}
currChunkIdx := bIdx / chunkSize
if currChunkIdx > 0 && currChunkIdx >= chunksLen {
return fmt.Errorf("too big bIdx=%d; should be smaller than %d", bIdx, chunksLen*chunkSize)
}
for chunkIdx := uint64(0); chunkIdx < chunksLen; chunkIdx++ {
chunk := getChunk()
chunks[chunkIdx] = chunk
if _, err := io.ReadFull(r, chunk); err != nil {
// Free up allocated chunks before returning the error.
for _, chunk := range chunks {
if chunk != nil {
putChunk(chunk)
}
}
return fmt.Errorf("cannot read b.chunks[%d]: %s", chunkIdx, err)
}
}
// Adjust len for the chunk pointed by currChunkIdx.
if chunksLen > 0 {
chunkLen := bIdx % chunkSize
chunks[currChunkIdx] = chunks[currChunkIdx][:chunkLen]
}
b.mu.Lock()
for _, chunk := range b.chunks {
putChunk(chunk)
}
b.chunks = chunks
b.m = m
b.idx = bIdx
b.gen = bGen
b.mu.Unlock()
return nil
}
func writeUint64(w io.Writer, u uint64) error {
var u64Buf [8]byte
binary.LittleEndian.PutUint64(u64Buf[:], u)
_, err := w.Write(u64Buf[:])
return err
}
func readUint64(r io.Reader) (uint64, error) {
var u64Buf [8]byte
if _, err := io.ReadFull(r, u64Buf[:]); err != nil {
return 0, err
}
u := binary.LittleEndian.Uint64(u64Buf[:])
return u, nil
}