-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
316 lines (262 loc) · 7.31 KB
/
api.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
package rnas
import (
"fmt"
"io"
"sync"
"time"
"github.com/klauspost/reedsolomon"
log "github.com/sirupsen/logrus"
)
func (c *Config) Put(filepath string, _size int64, reader io.Reader) error {
size := size_t(_size)
log.Infof("Put object to %s with size %d", filepath, size)
now := time.Now()
enc, err := reedsolomon.New(c.K, c.M)
if err != nil {
return err
}
fs := &FileStripe{Size: size, ConfigName: c.Name, Filepath: filepath, StripeConfig: c.StripeConfig }
n := fs.K + fs.M
// numDataShards := int((size + size_t(fs.StripeDepth) - 1) / size_t(fs.StripeDepth))
numStripes := int((size + size_t(fs.StripeDepth * fs.K) - 1) / size_t(fs.StripeDepth * fs.K))
// numShards := numStripes * n
err = saveFileStripe(fs)
if err != nil {
return err
}
// log.Infof("- put object (%d shards) (%d stripes), size %d", numShards, numStripes, size)
var done sync.WaitGroup
done.Add(numStripes)
log.Debug("numStripes:", numStripes)
stripeIndex := 0
for i := size_t(0); i < size; stripeIndex++{
left := size - i
shardSize := max(min(fs.StripeDepth, int(left / size_t(fs.K))), c.MinDepth)
// stripeWidth := shardSize * n
i += size_t(shardSize) * size_t(fs.K)
log.Debugf("- handle stripe %d, shard size: %d", stripeIndex, shardSize)
shards := make([]Shard, n)
data := make([][]byte, n)
// init shards and data
for j := 0; j < n; j++ {
data[j] = make([]byte, shardSize)
shards[j] = Shard{
fileID: fs.ID,
shardIndex: stripeIndex * n + j,
serverID: c.slots[j],
}
}
// copy data
for j := 0; j < fs.K; j++ {
shards[j].dataShard = true
n, err := reader.Read(data[j])
if n > 0 && n < len(data[j]) {
break
}
if err != nil {
//todo goto err
return fmt.Errorf("error while reading the data: %v", err)
}
}
// encode and send stripe
go func(data [][]byte, shards []Shard) {
// encode
log.Debug("- start to encode stripe")
now := time.Now()
err := enc.Encode(data)
end := time.Since(now)
if err != nil {
log.Fatal("bad encoding:", err)
}
log.Debugf("- encode stripe cost %v", end)
var wg sync.WaitGroup
wg.Add(n)
// send
for i := 0; i < n; i++ {
shard := &shards[i]
shard.shardHashname = hashMD5(data[i])
server := c.maps[shards[i].serverID]
go func(shard *Shard, data []byte) {
err := server.PutShard(shard, data);
if err != nil {
log.Errorf("error when put shard to server[%s]: %v", server.Id, err)
} else {
err := saveShard(shard)
if err != nil {
log.Errorf("saveShard error: %v", err)
}
}
wg.Done()
}(shard, data[i])
}
wg.Wait()
done.Done()
}(data, shards)
}
done.Wait()
end := time.Since(now)
fmt.Printf("%s has been put, took %v, speed %.2fB/s\n", filepath, end, float64(size) / float64(end.Seconds()))
return nil
}
type StripeData struct {
size int
data [][]byte
err error
}
type ShardData struct {
data []byte
shard *Shard
}
func (c *Config) ReadStream(filepath string) (io.Reader, error) {
log.Infof("Get object from %s", filepath)
fs, err := getFileStripe(filepath)
if err != nil {
return nil,err
}
allShards, err := getShards(fs.ID)
if err != nil {
return nil, err
}
n := fs.K + fs.M
numShards := len(allShards)
if numShards % n != 0 {
return nil, fmt.Errorf("bad shards number: %d", numShards)
}
// numDataShards := int((fs.Size + size_t(fs.StripeDepth) - 1) / size_t(fs.StripeDepth))
numStripes := numShards / n
// stripeDataWidth := fs.K * int(fs.StripeDepth)
log.Infof("- get object = (%d shards) = %d stripes, size %d", numStripes * n, numStripes, fs.Size)
var data sync.Map
pr, pw := io.Pipe()
// check data and send them to receiver
go func() {
for i := 0; i < numStripes; i++ {
var stripe StripeData
log.Debugf("- waiting for stripe %d", i)
for {
v, ok := data.Load(i)
if ok {
stripe = v.(StripeData)
break
}
time.Sleep(time.Millisecond)
}
if stripe.err != nil {
pw.CloseWithError(stripe.err)
return
}
written := 0
for _, data := range stripe.data {
if written == stripe.size {
// more data left, end
break
}
size := min(len(data), stripe.size - written)
pw.Write(data[:size])
written += size
}
log.Debugf("- read %d bytes from stripe %d", written, i)
}
pw.Close()
}()
stripeIndex := 0
// get stripes
for i := size_t(0); i < fs.Size; stripeIndex++ {
left := fs.Size - i
shardSize := max(min(fs.StripeDepth, int(left / size_t(fs.K))), c.MinDepth)
dataChan := make(chan ShardData, n)
log.Debugf("- start to retrieve stripe %d, shard size: %d", stripeIndex, shardSize)
i += size_t(shardSize) * size_t(fs.K)
// get shards
for j := 0; j < n; j++ {
shard := &allShards[j + stripeIndex * n]
server, ok := c.maps[shard.serverID]
if !ok {
// server not exists, may be removed?
log.Warnf("server[%s] doesn't exist. skip", shard.serverID)
dataChan <- ShardData{nil, shard}
continue
}
// get shard
go func(shard *Shard, shardSize int) {
data := make([]byte, shardSize)
_, err := server.GetShard(shard, data);
if err != nil {
log.Warnf("retrieve shard %d error: %v", shard.shardIndex, err)
dataChan <- ShardData{nil, shard}
} else {
if hashMD5(data) != shard.shardHashname {
log.Fatalf("bad data when verifying shard %d: %s", shard.shardIndex, shard.shardHashname)
} else {
log.Debugf("shard %d verified pass", shard.shardIndex)
}
dataChan <- ShardData{data, shard}
}
}(shard, shardSize)
}
go func(stripeIndex int, shardSize int, left size_t) {
stripe := make([][]byte, n)
received := 0
dataReceived := 0
validReceived := 0
resultChan := make(chan [][]byte, 3)
done := false
var fixStatus chan bool
for !done {
// cannot fix
if received == n && (fixStatus == nil || !<- fixStatus) {
break
}
select {
case v := <- dataChan:
received++
if v.data != nil {
validReceived++
if v.shard.dataShard {
dataReceived++
}
stripe[v.shard.shardIndex - stripeIndex * n] = v.data
}
// receive all data
if dataReceived == fs.K {
resultChan <- stripe
}
// only triggered once
if validReceived == fs.K && dataReceived < fs.K && fixStatus == nil {
fixStatus = make(chan bool)
copyStripe := make([][]byte, n)
copy(copyStripe, stripe)
// fix goroutine
go func(data [][]byte) {
log.Info("- got enough shards, begin to restore data in parallel")
enc, err := reedsolomon.New(c.K, c.M)
if err != nil {
log.Error("failed to init decoder")
fixStatus <- false
return
}
err = enc.ReconstructData(data)
if err != nil {
log.Error("failed to restore data")
fixStatus <- false
return
}
log.Info("- fix done")
fixStatus <- true
resultChan <- data
}(copyStripe)
}
case validStripe := <-resultChan:
log.Infof("- got all shards for stripe %d", stripeIndex)
done = true
size := min(left, size_t(shardSize * fs.K))
data.Store(stripeIndex, StripeData{size:int(size), data: validStripe[:fs.K]})
}
}
if !done {
data.Store(stripeIndex, StripeData{err: fmt.Errorf("stripe %d has left us permanently", stripeIndex)})
}
}(stripeIndex, shardSize, left)
}
return pr,nil
}