2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-08-24 14:06:27 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package eestream
|
|
|
|
|
|
|
|
import (
|
2019-06-04 12:36:27 +01:00
|
|
|
"context"
|
2018-09-07 15:37:13 +01:00
|
|
|
"fmt"
|
2018-08-24 14:06:27 +01:00
|
|
|
"io"
|
2018-09-07 15:37:13 +01:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2018-08-24 14:06:27 +01:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/vivint/infectious"
|
2019-07-31 15:38:44 +01:00
|
|
|
"go.uber.org/zap"
|
2019-06-04 12:36:27 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
2018-08-24 14:06:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// StripeReader can read and decodes stripes from a set of readers
|
|
|
|
type StripeReader struct {
|
2019-06-14 10:16:31 +01:00
|
|
|
scheme ErasureScheme
|
|
|
|
cond *sync.Cond
|
|
|
|
readerCount int
|
|
|
|
bufs map[int]*PieceBuffer
|
|
|
|
inbufs map[int][]byte
|
|
|
|
inmap map[int][]byte
|
|
|
|
errmap map[int]error
|
|
|
|
forceErrorDetection bool
|
2018-08-24 14:06:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewStripeReader creates a new StripeReader from the given readers, erasure
|
|
|
|
// scheme and max buffer memory.
|
2019-07-31 15:38:44 +01:00
|
|
|
func NewStripeReader(log *zap.Logger, rs map[int]io.ReadCloser, es ErasureScheme, mbm int, forceErrorDetection bool) *StripeReader {
|
2018-10-03 14:03:12 +01:00
|
|
|
readerCount := len(rs)
|
|
|
|
|
|
|
|
r := &StripeReader{
|
2019-06-14 10:16:31 +01:00
|
|
|
scheme: es,
|
|
|
|
cond: sync.NewCond(&sync.Mutex{}),
|
|
|
|
readerCount: readerCount,
|
|
|
|
bufs: make(map[int]*PieceBuffer, readerCount),
|
|
|
|
inbufs: make(map[int][]byte, readerCount),
|
|
|
|
inmap: make(map[int][]byte, readerCount),
|
|
|
|
errmap: make(map[int]error, readerCount),
|
|
|
|
forceErrorDetection: forceErrorDetection,
|
2018-10-03 14:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bufSize := mbm / readerCount
|
2018-09-27 12:52:18 +01:00
|
|
|
bufSize -= bufSize % es.ErasureShareSize()
|
|
|
|
if bufSize < es.ErasureShareSize() {
|
|
|
|
bufSize = es.ErasureShareSize()
|
2018-08-24 14:06:27 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 11:45:19 +01:00
|
|
|
for i := range rs {
|
2018-09-27 12:52:18 +01:00
|
|
|
r.inbufs[i] = make([]byte, es.ErasureShareSize())
|
2019-07-31 15:38:44 +01:00
|
|
|
r.bufs[i] = NewPieceBuffer(log, make([]byte, bufSize), es.ErasureShareSize(), r.cond)
|
2018-09-27 11:45:19 +01:00
|
|
|
// Kick off a goroutine each reader to be copied into a PieceBuffer.
|
2018-08-24 14:06:27 +01:00
|
|
|
go func(r io.Reader, buf *PieceBuffer) {
|
|
|
|
_, err := io.Copy(buf, r)
|
|
|
|
if err != nil {
|
|
|
|
buf.SetError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buf.SetError(io.EOF)
|
2018-09-27 11:45:19 +01:00
|
|
|
}(rs[i], r.bufs[i])
|
2018-08-24 14:06:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the StripeReader and all PieceBuffers.
|
|
|
|
func (r *StripeReader) Close() error {
|
|
|
|
errs := make(chan error, len(r.bufs))
|
|
|
|
for _, buf := range r.bufs {
|
|
|
|
go func(c io.Closer) {
|
|
|
|
errs <- c.Close()
|
|
|
|
}(buf)
|
|
|
|
}
|
|
|
|
var first error
|
|
|
|
for range r.bufs {
|
|
|
|
err := <-errs
|
|
|
|
if err != nil && first == nil {
|
|
|
|
first = Error.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return first
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadStripe reads and decodes the num-th stripe and concatenates it to p. The
|
|
|
|
// return value is the updated byte slice.
|
2019-06-04 12:36:27 +01:00
|
|
|
func (r *StripeReader) ReadStripe(ctx context.Context, num int64, p []byte) (_ []byte, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-08-24 14:06:27 +01:00
|
|
|
for i := range r.inmap {
|
|
|
|
delete(r.inmap, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.cond.L.Lock()
|
|
|
|
defer r.cond.L.Unlock()
|
|
|
|
|
2018-09-07 15:37:13 +01:00
|
|
|
for r.pendingReaders() {
|
2019-06-04 12:36:27 +01:00
|
|
|
for r.readAvailableShares(ctx, num) == 0 {
|
2018-08-24 14:06:27 +01:00
|
|
|
r.cond.Wait()
|
|
|
|
}
|
|
|
|
if r.hasEnoughShares() {
|
|
|
|
out, err := r.scheme.Decode(p, r.inmap)
|
|
|
|
if err != nil {
|
|
|
|
if r.shouldWaitForMore(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
}
|
2018-09-07 15:37:13 +01:00
|
|
|
// could not read enough shares to attempt a decode
|
2018-10-03 14:03:12 +01:00
|
|
|
return nil, r.combineErrs(num)
|
2018-08-24 14:06:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// readAvailableShares reads the available num-th erasure shares from the piece
|
|
|
|
// buffers without blocking. The return value n is the number of erasure shares
|
|
|
|
// read.
|
2019-06-04 12:36:27 +01:00
|
|
|
func (r *StripeReader) readAvailableShares(ctx context.Context, num int64) (n int) {
|
|
|
|
defer mon.Task()(&ctx)(nil)
|
2018-09-27 11:45:19 +01:00
|
|
|
for i, buf := range r.bufs {
|
2018-09-07 15:37:13 +01:00
|
|
|
if r.inmap[i] != nil || r.errmap[i] != nil {
|
2018-08-24 14:06:27 +01:00
|
|
|
continue
|
|
|
|
}
|
2018-09-27 11:45:19 +01:00
|
|
|
if buf.HasShare(num) {
|
|
|
|
err := buf.ReadShare(num, r.inbufs[i])
|
2018-08-24 14:06:27 +01:00
|
|
|
if err != nil {
|
2018-09-07 15:37:13 +01:00
|
|
|
r.errmap[i] = err
|
2018-08-24 14:06:27 +01:00
|
|
|
} else {
|
|
|
|
r.inmap[i] = r.inbufs[i]
|
|
|
|
}
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:37:13 +01:00
|
|
|
// pendingReaders checks if there are any pending readers to get a share from.
|
|
|
|
func (r *StripeReader) pendingReaders() bool {
|
2018-11-27 08:17:57 +00:00
|
|
|
goodReaders := r.readerCount - len(r.errmap)
|
|
|
|
return goodReaders >= r.scheme.RequiredCount() && goodReaders > len(r.inmap)
|
2018-09-07 15:37:13 +01:00
|
|
|
}
|
|
|
|
|
2018-08-24 14:06:27 +01:00
|
|
|
// hasEnoughShares check if there are enough erasure shares read to attempt
|
|
|
|
// a decode.
|
|
|
|
func (r *StripeReader) hasEnoughShares() bool {
|
|
|
|
return len(r.inmap) >= r.scheme.RequiredCount()+1 ||
|
2019-06-14 10:16:31 +01:00
|
|
|
(!r.forceErrorDetection && len(r.inmap) == r.scheme.RequiredCount() && !r.pendingReaders())
|
2018-08-24 14:06:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// shouldWaitForMore checks the returned decode error if it makes sense to wait
|
|
|
|
// for more erasure shares to attempt an error correction.
|
|
|
|
func (r *StripeReader) shouldWaitForMore(err error) bool {
|
|
|
|
// check if the error is due to error detection
|
|
|
|
if !infectious.NotEnoughShares.Contains(err) &&
|
|
|
|
!infectious.TooManyErrors.Contains(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// check if there are more input buffers to wait for
|
2018-09-07 15:37:13 +01:00
|
|
|
return r.pendingReaders()
|
|
|
|
}
|
|
|
|
|
|
|
|
// combineErrs makes a useful error message from the errors in errmap.
|
|
|
|
// combineErrs always returns an error.
|
2018-10-03 14:03:12 +01:00
|
|
|
func (r *StripeReader) combineErrs(num int64) error {
|
2018-09-07 15:37:13 +01:00
|
|
|
if len(r.errmap) == 0 {
|
|
|
|
return Error.New("programmer error: no errors to combine")
|
|
|
|
}
|
|
|
|
errstrings := make([]string, 0, len(r.errmap))
|
|
|
|
for i, err := range r.errmap {
|
2018-10-03 14:03:12 +01:00
|
|
|
errstrings = append(errstrings, fmt.Sprintf("\nerror retrieving piece %02d: %v", i, err))
|
2018-09-07 15:37:13 +01:00
|
|
|
}
|
|
|
|
sort.Strings(errstrings)
|
2018-10-03 14:03:12 +01:00
|
|
|
return Error.New("failed to download stripe %d: %s", num, strings.Join(errstrings, ""))
|
2018-08-24 14:06:27 +01:00
|
|
|
}
|