forked from coreos/torus
/
replication.go
218 lines (196 loc) · 4.95 KB
/
replication.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
package blockset
import (
"bytes"
"encoding/binary"
"errors"
"strconv"
"golang.org/x/net/context"
"github.com/RoaringBitmap/roaring"
"github.com/coreos/torus"
)
type replicationBlockset struct {
rep int
sub blockset
repBlocks [][]torus.BlockRef
bs torus.BlockStore
}
var _ blockset = &replicationBlockset{}
func init() {
RegisterBlockset(Replication, func(opt string, bs torus.BlockStore, sub blockset) (blockset, error) {
if opt == "" {
clog.Debugf("using default replication (0) to unmarshal")
return newReplicationBlockset(sub, bs, 0), nil
}
r, err := strconv.Atoi(opt)
if err != nil {
clog.Errorf("unknown replication amount %s", opt)
return nil, errors.New("unknown replication amount: " + opt)
}
return newReplicationBlockset(sub, bs, r), nil
})
}
func newReplicationBlockset(sub blockset, bs torus.BlockStore, r int) *replicationBlockset {
return &replicationBlockset{
rep: r,
sub: sub,
bs: bs,
repBlocks: make([][]torus.BlockRef, r-1),
}
}
func (b *replicationBlockset) Length() int {
return b.sub.Length()
}
func (b *replicationBlockset) Kind() uint32 {
return uint32(Replication)
}
func (b *replicationBlockset) GetBlock(ctx context.Context, i int) ([]byte, error) {
if b.rep == 0 {
return nil, torus.ErrBlockUnavailable
}
newctx := context.WithValue(ctx, "replication", b.rep)
bytes, err := b.sub.GetBlock(newctx, i)
if err == nil || err == torus.ErrBlockNotExist {
return bytes, err
}
for rep := 0; rep < (b.rep - 1); rep++ {
bytes, err := b.bs.GetBlock(ctx, b.repBlocks[rep][i])
if err == nil {
return bytes, nil
}
}
return nil, torus.ErrBlockUnavailable
}
func (b *replicationBlockset) PutBlock(ctx context.Context, inode torus.INodeRef, i int, data []byte) error {
if b.rep == 0 {
return torus.ErrBlockUnavailable
}
err := b.sub.PutBlock(ctx, inode, i, data)
if err != nil {
return err
}
for rep := 0; rep < (b.rep - 1); rep++ {
newBlockID := b.makeID(inode)
err := b.bs.WriteBlock(ctx, newBlockID, data)
if err != nil {
return err
}
if i == len(b.repBlocks[rep]) {
b.repBlocks[rep] = append(b.repBlocks[rep], newBlockID)
} else {
b.repBlocks[rep][i] = newBlockID
}
}
return nil
}
func (b *replicationBlockset) makeID(i torus.INodeRef) torus.BlockRef {
return b.sub.makeID(i)
}
func (b *replicationBlockset) setStore(s torus.BlockStore) {
b.bs = s
b.sub.setStore(s)
}
func (b *replicationBlockset) getStore() torus.BlockStore {
return b.bs
}
func (b *replicationBlockset) Marshal() ([]byte, error) {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, int32(b.rep))
if err != nil {
return nil, err
}
for rep := 0; rep < (b.rep - 1); rep++ {
err := binary.Write(buf, binary.LittleEndian, int32(len(b.repBlocks[rep])))
if err != nil {
return nil, err
}
for _, x := range b.repBlocks[rep] {
_, err := buf.Write(x.ToBytes())
if err != nil {
return nil, err
}
}
}
return buf.Bytes(), nil
}
func (b *replicationBlockset) Unmarshal(data []byte) error {
r := bytes.NewReader(data)
var rep int32
err := binary.Read(r, binary.LittleEndian, &rep)
if err != nil {
return err
}
b.rep = int(rep)
b.repBlocks = make([][]torus.BlockRef, rep)
for rep := 0; rep < (b.rep - 1); rep++ {
var l int32
err := binary.Read(r, binary.LittleEndian, &l)
if err != nil {
return err
}
out := make([]torus.BlockRef, l)
for i := 0; i < int(l); i++ {
buf := make([]byte, torus.BlockRefByteSize)
_, err := r.Read(buf)
if err != nil {
return err
}
out[i] = torus.BlockRefFromBytes(buf)
}
b.repBlocks[rep] = out
}
return nil
}
func (b *replicationBlockset) GetSubBlockset() torus.Blockset { return b.sub }
func (b *replicationBlockset) GetLiveINodes() *roaring.Bitmap {
return b.sub.GetLiveINodes()
}
func (b *replicationBlockset) Truncate(lastIndex int, blocksize uint64) error {
err := b.sub.Truncate(lastIndex, blocksize)
if err != nil {
return err
}
if lastIndex <= len(b.repBlocks[0]) {
for i := range b.repBlocks {
b.repBlocks[i] = b.repBlocks[i][:lastIndex]
}
return nil
}
for i := range b.repBlocks {
toadd := lastIndex - len(b.repBlocks[0])
for toadd != 0 {
b.repBlocks[i] = append(b.repBlocks[i], torus.ZeroBlock())
toadd--
}
}
return nil
}
func (b *replicationBlockset) Trim(from, to int) error {
err := b.sub.Trim(from, to)
if err != nil {
return err
}
if from >= len(b.repBlocks[0]) {
return nil
}
if to > len(b.repBlocks[0]) {
to = len(b.repBlocks[0])
}
for i := from; i < to; i++ {
for _, blist := range b.repBlocks {
blist[i] = torus.ZeroBlock()
}
}
return nil
}
func (b *replicationBlockset) GetAllBlockRefs() []torus.BlockRef {
sub := b.sub.GetAllBlockRefs()
out := make([]torus.BlockRef, len(b.repBlocks[0])*len(b.repBlocks))
nblocks := len(b.repBlocks[0])
for i, list := range b.repBlocks {
copy(out[i*nblocks:], list)
}
return append(sub, out...)
}
func (b *replicationBlockset) String() string {
return "rep\n" + b.sub.String()
}