2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-10-05 14:34:15 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"math/big"
|
|
|
|
"sync"
|
2019-03-23 10:52:51 +00:00
|
|
|
"time"
|
2018-10-05 14:34:15 +01:00
|
|
|
|
2019-03-23 10:52:51 +00:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
2018-10-05 14:34:15 +01:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/eestream"
|
|
|
|
"storj.io/storj/pkg/pb"
|
2019-01-10 16:35:18 +00:00
|
|
|
"storj.io/storj/pkg/pointerdb"
|
2018-10-05 14:34:15 +01:00
|
|
|
"storj.io/storj/pkg/storage/meta"
|
2018-10-25 21:28:16 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-10-05 14:34:15 +01:00
|
|
|
)
|
|
|
|
|
2018-10-10 19:25:46 +01:00
|
|
|
// Stripe keeps track of a stripe's index and its parent segment
|
|
|
|
type Stripe struct {
|
2019-03-18 10:55:06 +00:00
|
|
|
Index int64
|
|
|
|
Segment *pb.Pointer
|
2019-03-28 20:09:23 +00:00
|
|
|
SegmentPath storj.Path
|
2018-10-10 19:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cursor keeps track of audit location in pointer db
|
|
|
|
type Cursor struct {
|
2019-03-28 20:09:23 +00:00
|
|
|
pointerdb *pointerdb.Service
|
|
|
|
lastPath storj.Path
|
|
|
|
mutex sync.Mutex
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-10-10 19:25:46 +01:00
|
|
|
// NewCursor creates a Cursor which iterates over pointer db
|
2019-03-28 20:09:23 +00:00
|
|
|
func NewCursor(pointerdb *pointerdb.Service) *Cursor {
|
2019-03-18 10:55:06 +00:00
|
|
|
return &Cursor{
|
2019-03-28 20:09:23 +00:00
|
|
|
pointerdb: pointerdb,
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NextStripe returns a random stripe to be audited
|
2018-10-10 19:25:46 +01:00
|
|
|
func (cursor *Cursor) NextStripe(ctx context.Context) (stripe *Stripe, err error) {
|
|
|
|
cursor.mutex.Lock()
|
|
|
|
defer cursor.mutex.Unlock()
|
2018-10-05 14:34:15 +01:00
|
|
|
|
2019-01-10 16:35:18 +00:00
|
|
|
var pointerItems []*pb.ListResponse_Item
|
2018-10-25 21:28:16 +01:00
|
|
|
var path storj.Path
|
2018-10-10 19:25:46 +01:00
|
|
|
var more bool
|
2018-10-05 14:34:15 +01:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
pointerItems, more, err = cursor.pointerdb.List("", cursor.lastPath, "", true, 0, meta.None)
|
2018-10-05 14:34:15 +01:00
|
|
|
if err != nil {
|
2018-10-10 19:25:46 +01:00
|
|
|
return nil, err
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-11-07 01:16:43 +00:00
|
|
|
if len(pointerItems) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 14:34:15 +01:00
|
|
|
pointerItem, err := getRandomPointer(pointerItems)
|
|
|
|
if err != nil {
|
2018-10-10 19:25:46 +01:00
|
|
|
return nil, err
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
path = pointerItem.Path
|
|
|
|
|
|
|
|
// keep track of last path listed
|
|
|
|
if !more {
|
2018-10-25 21:28:16 +01:00
|
|
|
cursor.lastPath = ""
|
2018-10-05 14:34:15 +01:00
|
|
|
} else {
|
2018-10-25 21:28:16 +01:00
|
|
|
cursor.lastPath = pointerItems[len(pointerItems)-1].Path
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// get pointer info
|
2019-03-18 10:55:06 +00:00
|
|
|
pointer, err := cursor.pointerdb.Get(path)
|
2019-01-19 18:58:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-23 10:52:51 +00:00
|
|
|
//delete expired items rather than auditing them
|
|
|
|
if expiration := pointer.GetExpirationDate(); expiration != nil {
|
|
|
|
t, err := ptypes.Timestamp(expiration)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if t.Before(time.Now()) {
|
|
|
|
return nil, cursor.pointerdb.Delete(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 01:16:43 +00:00
|
|
|
if pointer.GetType() != pb.Pointer_REMOTE {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-11-20 17:09:35 +00:00
|
|
|
if pointer.GetSegmentSize() == 0 {
|
2018-11-07 16:51:36 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
index, err := getRandomStripe(pointer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-10 16:35:18 +00:00
|
|
|
return &Stripe{
|
2019-03-18 10:55:06 +00:00
|
|
|
Index: index,
|
|
|
|
Segment: pointer,
|
2019-03-28 20:09:23 +00:00
|
|
|
SegmentPath: path,
|
2019-01-10 16:35:18 +00:00
|
|
|
}, nil
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
func getRandomStripe(pointer *pb.Pointer) (index int64, err error) {
|
|
|
|
redundancy, err := eestream.NewRedundancyStrategyFromProto(pointer.GetRemote().GetRedundancy())
|
2018-10-05 14:34:15 +01:00
|
|
|
if err != nil {
|
2019-03-18 10:55:06 +00:00
|
|
|
return 0, err
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
2018-11-07 21:23:05 +00:00
|
|
|
|
|
|
|
// the last segment could be smaller than stripe size
|
2019-03-18 10:55:06 +00:00
|
|
|
if pointer.GetSegmentSize() < int64(redundancy.StripeSize()) {
|
2018-11-07 21:23:05 +00:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
randomStripeIndex, err := rand.Int(rand.Reader, big.NewInt(pointer.GetSegmentSize()/int64(redundancy.StripeSize())))
|
2018-10-05 14:34:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
return randomStripeIndex.Int64(), nil
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
2019-01-10 16:35:18 +00:00
|
|
|
func getRandomPointer(pointerItems []*pb.ListResponse_Item) (pointer *pb.ListResponse_Item, err error) {
|
2018-10-05 14:34:15 +01:00
|
|
|
randomNum, err := rand.Int(rand.Reader, big.NewInt(int64(len(pointerItems))))
|
|
|
|
if err != nil {
|
2019-01-10 16:35:18 +00:00
|
|
|
return &pb.ListResponse_Item{}, err
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
return pointerItems[randomNum.Int64()], nil
|
|
|
|
}
|