-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.go
More file actions
204 lines (178 loc) · 4.95 KB
/
state.go
File metadata and controls
204 lines (178 loc) · 4.95 KB
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
package radix
import (
"bufio"
"bytes"
"fmt"
"io"
"runtime"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/golang/snappy"
)
// writeState writes the complete index state to the given filename.
// It assumes are allocators are locked by caller.
func (idx *Tree) writeState(out io.Writer) (int, error) {
written := 0
// write metadata: blocksize, number of blocks, number of allocators, root node location
n, err := out.Write([]byte(fmt.Sprintf("objects %d\nblocksize %d\nallocated_blocks %d\nallocators %d\nroot_node %d\nepoch %d\n", atomic.LoadInt64(&idx.liveObjects), blockSize, idx.nextFreeBlock, len(idx.allocators), atomic.LoadUint64(&idx.root), atomic.LoadUint64(idx.manager.globalEpoch))))
if err != nil {
return 0, err
}
written += n
// write allocator states
for i := range idx.allocators {
n, err := out.Write([]byte(fmt.Sprintf("allocator %d\n", i)))
if err != nil {
return n + written, err
}
written += n
n, err = idx.allocators[i].writeState(out)
if err != nil {
return n + written, err
}
written += n
}
// compress and write index blocks concurrently
var workers sync.WaitGroup
type writeJob struct {
block int
buf []byte
}
toCompress := make(chan int)
toWrite := make(chan *writeJob, 32)
jobPool := sync.Pool{New: func() interface{} { return &writeJob{buf: make([]byte, blockSize)} }}
for i := 0; i < runtime.NumCPU(); i++ {
workers.Add(1)
go func() {
for b := range toCompress {
src := byteSliceFromUint64(idx.blocks[b].data)
j := jobPool.Get().(*writeJob)
j.buf = snappy.Encode(j.buf[0:], src)
j.block = b
toWrite <- j
}
workers.Done()
}()
}
writerDone := make(chan error)
go func() {
// writer
var writeTime time.Duration
for job := range toWrite {
n, err := out.Write([]byte(fmt.Sprintf("block %d\ncompression snappy\nsize %d\n", job.block, len(job.buf))))
if err != nil {
writerDone <- err
return
}
written += n
start := time.Now()
n, err = out.Write(job.buf)
jobPool.Put(job)
if err != nil {
writerDone <- err
return
}
written += n
writeTime += time.Since(start)
}
writerDone <- nil
}()
next := int(atomic.LoadUint64(&idx.nextFreeBlock))
for i := 0; i < next; i++ {
toCompress <- i
}
close(toCompress)
workers.Wait()
close(toWrite)
err = <-writerDone
return written, err
}
// loadState rebuilds index state from data previously written with writeState.
// It assumes the index is empty.
func (idx *Tree) loadState(data io.Reader) error {
var (
reader = bufio.NewReader(data)
allocators int
nextBlock = -1
readBuf []byte
)
// reset block map
idx.blockMap = make(map[int]int)
for {
line, err := reader.ReadBytes('\n')
if err == io.EOF {
break
}
if err != nil {
return err
}
// trim \n
line = line[:len(line)-1]
space := bytes.IndexByte(line, ' ')
v, _ := strconv.ParseUint(string(line[space+1:]), 10, 64)
switch string(line[:space]) {
case "objects":
idx.liveObjects = int64(v)
case "blocksize":
// in case loaded blocksize differs from existing ones, we need to throw away all blocks
if v != blockSize {
return fmt.Errorf("Cannot lode state file. Unsupported blocksize %d", v)
}
case "allocated_blocks":
idx.nextFreeBlock = v
case "epoch":
atomic.StoreUint64(idx.manager.globalEpoch, v)
case "root_node":
idx.root = v
case "allocators":
allocators = int(v)
idx.manager = newEpochManager(allocators)
idx.allocators = make([]*Allocator, allocators)
idx.allocatorQueue = newSlots(allocators)
for i := range idx.allocators {
idx.allocators[i] = newAllocator(idx, i, blockSize, idx.newBlocks)
}
case "allocator":
// allocator X
if int(v) > len(idx.allocators)-1 {
return fmt.Errorf("Corrupted index state file? Can't load allocator id %d since there are only %d allocators", v, len(idx.allocators))
}
if err := idx.allocators[v].loadState(reader); err != nil {
return err
}
idx.blockMap[idx.allocators[v].block] = blockAllocated
case "block":
// block X
if int(v) > len(idx.blocks)-1 {
return fmt.Errorf("Corrupted index state file? Can't load data block id %d since there are only %d blocks", v, len(idx.blocks))
}
nextBlock = int(v)
case "compression":
case "size":
if nextBlock < 0 {
return fmt.Errorf("Corrupted index state file? size header before first block")
}
if cap(readBuf) >= int(v) {
readBuf = readBuf[:int(v)]
} else {
readBuf = make([]byte, int(v))
}
if _, err := io.ReadFull(reader, readBuf); err != nil {
return err
}
idx.blocks[nextBlock] = &nodeBlock{
data: make([]uint64, blockSize>>3),
}
dst := byteSliceFromUint64(idx.blocks[nextBlock].data)
if _, err := snappy.Decode(dst, readBuf); err != nil {
return fmt.Errorf("Corrupted index state file? Decompressing data block failed : %w", err)
}
if idx.blockMap[int(nextBlock)] != blockAllocated {
idx.blockMap[int(nextBlock)] = blockWritten
}
}
}
return nil
}