-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3getObject.go
More file actions
412 lines (345 loc) · 12.6 KB
/
s3getObject.go
File metadata and controls
412 lines (345 loc) · 12.6 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
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
package test
import (
"errors"
"io"
"net/http"
"test/util"
"time"
"github.com/golang/glog"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
)
// S3GetObject is the class to handle object get and head
type S3GetObject struct {
ctx context.Context
w http.ResponseWriter
r *http.Request
s3io CloudIO
bkname string
objname string
requuid string
objmd *ObjectMD
// the read offset
off int64
// data part of the currBlock
currPart dataPartReadResult
// the current cached data block
currBlock dataBlockReadResult
// sanity check, whether needs to wait for the outgoing block prefetch
waitBlock bool
// channel to wait till the background prefetch complete
blockChan chan dataBlockReadResult
// sanity check, whether needs to wait for the outgoing part prefetch
waitPart bool
partChan chan dataPartReadResult
}
type dataBlockReadResult struct {
partNum int
blkIdx int
blkmd5 string
buf []byte
n int
status int
errmsg string
}
type dataPartReadResult struct {
// data part name
partName string
partNum int
part *DataPart
status int
errmsg string
}
// NewS3GetObject creates a S3GetObject instance
func NewS3GetObject(ctx context.Context, r *http.Request, s3io CloudIO,
md *ObjectMD, bkname string, objname string) *S3GetObject {
s := new(S3GetObject)
s.ctx = ctx
s.requuid = s.requuid
s.r = r
s.s3io = s3io
s.objmd = md
s.bkname = bkname
s.objname = objname
return s
}
func (d *S3GetObject) isLastBlock(partNum int, blkIdx int) bool {
data := d.objmd.Data
totalParts := len(data.DataParts)
lastPart := data.DataParts[totalParts-1]
if partNum == totalParts-1 {
l := len(lastPart.Blocks)
if l == 0 {
// last part may not have any block
glog.V(5).Infoln("last part has no data, totalParts", totalParts, d.bkname, d.objname)
return true
}
glog.V(5).Infoln("reach the last block, totalParts", totalParts, "block", blkIdx, d.bkname, d.objname)
return blkIdx == l-1
}
return false
}
// check to make sure the read block is in currPart
func (d *S3GetObject) isValidReadBlock(partNum int, blkIdx int) bool {
// sanity check
if partNum != d.currPart.partNum {
glog.Errorln("SanityError - ", d.requuid, "read block in part", partNum,
"not in currPart", d.currPart.partName, d.bkname, d.objname)
return false
}
// sanity check
blkCount := len(d.currPart.part.Blocks)
if blkIdx >= blkCount {
glog.Errorln("SanityError - ", d.requuid, "read unexist block",
blkIdx, "in part", d.currPart.partName, blkCount, d.bkname, d.objname)
return false
}
return true
}
func (d *S3GetObject) readBlock(partNum int, blkIdx int, b []byte) dataBlockReadResult {
res := dataBlockReadResult{partNum: partNum, blkIdx: blkIdx, buf: b}
// sanity check
if !d.isValidReadBlock(partNum, blkIdx) {
res := dataBlockReadResult{status: InternalError, errmsg: "read unexist block"}
return res
}
dataPart := d.currPart.part
res.blkmd5 = dataPart.Blocks[blkIdx]
res.n, res.status, res.errmsg = d.s3io.ReadDataBlockRange(res.blkmd5, 0, res.buf)
glog.V(2).Infoln("read block done", d.requuid, "part", partNum, "block", blkIdx,
res.blkmd5, res.n, res.status, res.errmsg, d.bkname, d.objname)
if res.status == StatusOK && res.n != int(d.objmd.Data.BlockSize) &&
!d.isLastBlock(partNum, blkIdx) {
// read less data, could only happen for the last block
glog.Errorln("not read full block", d.requuid, res.n,
d.objmd.Data.BlockSize, dataPart.Name, blkIdx, d.bkname, d.objname)
res.status = InternalError
res.errmsg = "read less data for a full block"
}
return res
}
func (d *S3GetObject) prefetchBlock(partNum int, blk int, b []byte) {
glog.V(5).Infoln("prefetchBlock start", d.requuid,
"part", partNum, "block", blk, d.bkname, d.objname)
res := d.readBlock(partNum, blk, b)
select {
case d.blockChan <- res:
glog.V(5).Infoln("prefetchBlock sent to chan done", d.requuid, blk, d.bkname, d.objname)
case <-d.ctx.Done():
glog.Errorln("prefetchBlock canceled", d.requuid, partNum, blk, d.bkname, d.objname)
case <-time.After(RWTimeOutSecs * time.Second):
glog.Errorln("stop prefetchBlock, timeout", d.requuid, partNum, blk, d.bkname, d.objname)
}
}
func (d *S3GetObject) prefetchPart(partNum int) {
glog.V(5).Infoln("prefetchPart start", d.requuid, partNum, d.bkname, d.objname)
partName := util.GenPartName(d.objmd.Uuid, partNum)
res := dataPartReadResult{partName: partName, partNum: partNum,
status: StatusOK, errmsg: StatusOKStr}
b, status, errmsg := d.s3io.ReadDataPart(d.objmd.Smd.Bucket, partName)
if status == StatusOK {
part := &DataPart{}
err := proto.Unmarshal(b, part)
if err != nil {
glog.Errorln("failed to Unmarshal DataPart", d.requuid, partNum, err, d.bkname, d.objname)
res.status = InternalError
res.errmsg = "failed to Unmarshal DataPart"
} else {
glog.V(5).Infoln("prefetchPart success", d.requuid, partNum, len(part.Blocks), d.bkname, d.objname)
res.part = part
}
} else {
glog.Errorln("failed to ReadDataPart", d.requuid, partNum, status, errmsg, d.bkname, d.objname)
res.status = status
res.errmsg = errmsg
}
select {
case d.partChan <- res:
glog.V(5).Infoln("prefetchPart sent to chan done", d.requuid, partNum, d.bkname, d.objname)
case <-d.ctx.Done():
glog.Errorln("prefetchPart canceled", d.requuid, partNum, d.bkname, d.objname)
case <-time.After(RWTimeOutSecs * time.Second):
glog.Errorln("stop prefetchPart, timeout", d.requuid, partNum, d.bkname, d.objname)
}
}
func (d *S3GetObject) waitPrefetchBlock(partNum int, blkInPart int) error {
select {
case nextBlock := <-d.blockChan:
d.waitBlock = false
if nextBlock.status != StatusOK {
glog.Errorln("failed to prefetch block", d.requuid,
"part", nextBlock.partNum, "block", nextBlock.blkIdx, nextBlock.status, nextBlock.errmsg)
return errors.New(nextBlock.errmsg)
}
// sanity check
if nextBlock.partNum != partNum || nextBlock.blkIdx != blkInPart {
glog.Errorln("the prefetch block is not the next read block", d.requuid,
"part", nextBlock.partNum, "block", nextBlock.blkIdx,
"target part", partNum, "block", blkInPart, d.bkname, d.objname)
return errors.New(InternalErrorStr)
}
glog.V(5).Infoln("get the prefetch block", d.requuid,
"part", partNum, "block", blkInPart, "read offset", d.off, d.bkname, d.objname)
// the next block is back, switch the current block to the next block
oldbuf := d.currBlock.buf
d.currBlock = nextBlock
// prefetch the next block if necessary
if d.currBlock.status == StatusOK && !d.isLastBlock(partNum, blkInPart) {
if blkInPart == int(d.objmd.Data.MaxBlocks-1) {
// read the last block in the currPart, wait prefetch part
err := d.waitPrefetchPart()
if err != nil {
return err
}
// prefetch the first block in the next part
if len(d.currPart.part.Blocks) != 0 {
d.waitBlock = true
go d.prefetchBlock(d.currPart.partNum, 0, oldbuf)
}
} else {
// prefetch the next block in the same part
d.waitBlock = true
go d.prefetchBlock(partNum, blkInPart+1, oldbuf)
}
}
return nil
case <-time.After(RWTimeOutSecs * time.Second):
glog.Errorln("waitPrefetchBlock timeout", d.requuid, "part", partNum,
"block", blkInPart, "read offset", d.off, d.bkname, d.objname)
return errors.New("waitPrefetchBlock timeout")
}
}
func (d *S3GetObject) waitPrefetchPart() error {
totalParts := len(d.objmd.Data.DataParts)
if !d.waitPart {
// no more part to prefetch, the current part must be the last-1 part.
if d.currPart.partNum != totalParts-2 {
glog.Errorln("SanityError - ", d.requuid, "currPart", d.currPart.partNum,
"is not the last-2 part, totalParts", totalParts, d.bkname, d.objname)
return errors.New(InternalErrorStr)
}
// set the currPart to the last part
glog.V(2).Infoln("set the last part as currPart", d.requuid,
"currPart", d.currPart.partNum, "totalParts", totalParts, d.bkname, d.objname)
part := d.objmd.Data.DataParts[totalParts-1]
res := dataPartReadResult{partName: part.Name, partNum: totalParts - 1, part: part,
status: StatusOK, errmsg: StatusOKStr}
d.currPart = res
return nil
}
glog.V(5).Infoln("wait the prefetch part", d.requuid, "currPart", d.currPart.partNum, d.bkname, d.objname)
select {
case nextPart := <-d.partChan:
d.waitPart = false
if nextPart.status != StatusOK {
glog.Errorln("failed to prefetch part", d.requuid,
"part", nextPart.partNum, nextPart.status, nextPart.errmsg)
return errors.New(nextPart.errmsg)
}
glog.V(5).Infoln("get the prefetch part", d.requuid,
"part", nextPart.partNum, "totalParts", totalParts, d.bkname, d.objname)
// the next block is back, switch the current block to the next block
d.currPart = nextPart
// if not last-1 part, prefetch the next part
if d.currPart.partNum < totalParts-2 {
d.waitPart = true
go d.prefetchPart(d.currPart.partNum + 1)
}
return nil
case <-d.ctx.Done():
glog.Errorln("waitPrefetchPart canceled", d.requuid, "currPart", d.currPart.partNum, d.bkname, d.objname)
return errors.New("canceled")
case <-time.After(RWTimeOutSecs * time.Second):
glog.Errorln("waitPrefetchPart timeout", d.requuid, "currPart", d.currPart.partNum, d.bkname, d.objname)
return errors.New("read timeout")
}
}
func (d *S3GetObject) Read(p []byte) (n int, err error) {
if d.off >= d.objmd.Smd.Size {
glog.V(1).Infoln("finish read object data", d.requuid, d.bkname, d.objname)
return 0, io.EOF
}
// compute the corresponding data block and offset inside data block
blockNum := int(d.off / int64(d.objmd.Data.BlockSize))
blockOff := int(d.off % int64(d.objmd.Data.BlockSize))
partNum := blockNum / int(d.objmd.Data.MaxBlocks)
blkIdx := blockNum % int(d.objmd.Data.MaxBlocks)
// if current block is read out, wait for the next block
if partNum > d.currBlock.partNum || blkIdx > d.currBlock.blkIdx {
// sanity check, the prefetch task should be sent already
if !d.waitBlock {
glog.Errorln("no prefetch task", d.requuid, "part", partNum,
"block", blkIdx, "read offset", d.off, d.bkname, d.objname)
return 0, errors.New("InternalError, no prefetch task")
}
glog.V(5).Infoln("wait the prefetch block", d.requuid, "part", partNum,
"block", blkIdx, "read offset", d.off, d.bkname, d.objname)
err := d.waitPrefetchBlock(partNum, blkIdx)
if err != nil {
return 0, err
}
}
if RandomFI() && !FIRandomSleep() {
glog.Errorln("FI error at Read", d.requuid, "part", partNum,
"block", blkIdx, "read offset", d.off, d.bkname, d.objname)
return 0, errors.New("FI error")
}
// check the current block read status
if d.currBlock.status != StatusOK {
glog.Errorln("read data block failed", d.requuid, "part", partNum, "block", blkIdx,
"read offset", d.off, d.currBlock.status, d.currBlock.errmsg, d.bkname, d.objname)
return 0, errors.New(d.currBlock.errmsg)
}
// fill data from the current block
glog.V(2).Infoln("fill data from currBlock", d.requuid, "part", partNum,
"block", blkIdx, blockOff, "block len", d.currBlock.n, "read offset", d.off, d.bkname, d.objname)
endOff := blockOff + len(p)
if endOff <= d.currBlock.n {
// currBlock has more data than p
glog.V(5).Infoln("currBlock has enough data", d.requuid,
"block", blkIdx, blockOff, "end", endOff)
copy(p, d.currBlock.buf[blockOff:endOff])
n = len(p)
} else {
// p could have more data than the rest in currBlock
// TODO copy the rest data from the next block
glog.V(5).Infoln("read the end of currBlock", d.requuid,
"block", blkIdx, blockOff, "end", endOff)
copy(p, d.currBlock.buf[blockOff:d.currBlock.n])
n = d.currBlock.n - blockOff
}
d.off += int64(n)
if d.off == d.objmd.Smd.Size {
return n, io.EOF
}
return n, nil
}
// GetObject prepares the object read
func (d *S3GetObject) GetObject() (status int, errmsg string) {
// synchronously read the first block
b := make([]byte, d.objmd.Data.BlockSize)
d.currPart = dataPartReadResult{partName: util.GenPartName(d.objmd.Uuid, 0), partNum: 0,
part: d.objmd.Data.DataParts[0], status: StatusOK, errmsg: StatusOKStr}
d.currBlock = d.readBlock(0, 0, b)
// check the first block read status
if d.currBlock.status != StatusOK {
glog.Errorln("read first data block failed",
d.requuid, d.objmd.Data.DataParts[0].Blocks[0],
d.currBlock.status, d.currBlock.errmsg, d.bkname, d.objname)
return d.currBlock.status, d.currBlock.errmsg
}
// if there are more data to read, start the prefetch task
if d.objmd.Smd.Size > int64(d.objmd.Data.BlockSize) {
d.blockChan = make(chan dataBlockReadResult)
nextbuf := make([]byte, d.objmd.Data.BlockSize)
d.waitBlock = true
go d.prefetchBlock(0, 1, nextbuf)
}
// if there are more than 2 parts, start the prefetch task
if len(d.objmd.Data.DataParts) > 2 {
d.partChan = make(chan dataPartReadResult)
d.waitPart = true
go d.prefetchPart(1)
}
return StatusOK, StatusOKStr
}