2019-10-07 21:38:05 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package gracefulexit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-10-29 20:22:20 +00:00
|
|
|
"sync"
|
2019-10-07 21:38:05 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
2019-10-07 21:38:05 +01:00
|
|
|
"storj.io/storj/satellite/metainfo"
|
2020-01-08 13:40:19 +00:00
|
|
|
"storj.io/uplink/eestream"
|
2019-10-07 21:38:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ metainfo.Observer = (*PathCollector)(nil)
|
|
|
|
|
|
|
|
// PathCollector uses the metainfo loop to add paths to node reservoirs
|
|
|
|
//
|
|
|
|
// architecture: Observer
|
|
|
|
type PathCollector struct {
|
2019-10-29 20:22:20 +00:00
|
|
|
db DB
|
|
|
|
nodeIDMutex sync.Mutex
|
|
|
|
nodeIDStorage map[storj.NodeID]int64
|
|
|
|
buffer []TransferQueueItem
|
|
|
|
log *zap.Logger
|
|
|
|
batchSize int
|
2019-10-07 21:38:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPathCollector instantiates a path collector.
|
|
|
|
func NewPathCollector(db DB, nodeIDs storj.NodeIDList, log *zap.Logger, batchSize int) *PathCollector {
|
|
|
|
buffer := make([]TransferQueueItem, 0, batchSize)
|
|
|
|
collector := &PathCollector{
|
|
|
|
db: db,
|
|
|
|
log: log,
|
|
|
|
buffer: buffer,
|
|
|
|
batchSize: batchSize,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(nodeIDs) > 0 {
|
2019-10-29 20:22:20 +00:00
|
|
|
collector.nodeIDStorage = make(map[storj.NodeID]int64, len(nodeIDs))
|
2019-10-07 21:38:05 +01:00
|
|
|
for _, nodeID := range nodeIDs {
|
2019-10-29 20:22:20 +00:00
|
|
|
collector.nodeIDStorage[nodeID] = 0
|
2019-10-07 21:38:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return collector
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush persists the current buffer items to the database.
|
|
|
|
func (collector *PathCollector) Flush(ctx context.Context) (err error) {
|
|
|
|
return collector.flush(ctx, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteSegment takes a remote segment found in metainfo and creates a graceful exit transfer queue item if it doesn't exist already
|
|
|
|
func (collector *PathCollector) RemoteSegment(ctx context.Context, path metainfo.ScopedPath, pointer *pb.Pointer) (err error) {
|
2019-10-29 20:22:20 +00:00
|
|
|
if len(collector.nodeIDStorage) == 0 {
|
2019-10-07 21:38:05 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-29 20:22:20 +00:00
|
|
|
collector.nodeIDMutex.Lock()
|
|
|
|
defer collector.nodeIDMutex.Unlock()
|
|
|
|
|
2019-10-07 21:38:05 +01:00
|
|
|
numPieces := int32(len(pointer.GetRemote().GetRemotePieces()))
|
|
|
|
for _, piece := range pointer.GetRemote().GetRemotePieces() {
|
2019-10-29 20:22:20 +00:00
|
|
|
if _, ok := collector.nodeIDStorage[piece.NodeId]; !ok {
|
2019-10-07 21:38:05 +01:00
|
|
|
continue
|
|
|
|
}
|
2019-10-29 20:22:20 +00:00
|
|
|
redundancy, err := eestream.NewRedundancyStrategyFromProto(pointer.GetRemote().GetRedundancy())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pieceSize := eestream.CalcPieceSize(pointer.GetSegmentSize(), redundancy)
|
|
|
|
collector.nodeIDStorage[piece.NodeId] += pieceSize
|
2019-10-07 21:38:05 +01:00
|
|
|
|
|
|
|
item := TransferQueueItem{
|
|
|
|
NodeID: piece.NodeId,
|
|
|
|
Path: []byte(path.Raw),
|
|
|
|
PieceNum: piece.PieceNum,
|
2019-11-07 16:13:05 +00:00
|
|
|
RootPieceID: pointer.GetRemote().RootPieceId,
|
2019-11-26 17:04:48 +00:00
|
|
|
DurabilityRatio: float64(numPieces) / float64(pointer.GetRemote().GetRedundancy().GetTotal()),
|
2019-10-07 21:38:05 +01:00
|
|
|
}
|
2019-11-26 17:04:48 +00:00
|
|
|
|
2019-11-05 21:04:07 +00:00
|
|
|
collector.log.Debug("adding piece to transfer queue.", zap.Stringer("Node ID", piece.NodeId),
|
2019-10-07 21:38:05 +01:00
|
|
|
zap.String("path", path.Raw), zap.Int32("piece num", piece.GetPieceNum()),
|
|
|
|
zap.Int32("num pieces", numPieces), zap.Int32("total possible pieces", pointer.GetRemote().GetRedundancy().GetTotal()))
|
|
|
|
|
|
|
|
collector.buffer = append(collector.buffer, item)
|
|
|
|
err = collector.flush(ctx, collector.batchSize)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Object returns nil because the audit service does not interact with objects
|
|
|
|
func (collector *PathCollector) Object(ctx context.Context, path metainfo.ScopedPath, pointer *pb.Pointer) (err error) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InlineSegment returns nil because we're only auditing for storage nodes for now
|
|
|
|
func (collector *PathCollector) InlineSegment(ctx context.Context, path metainfo.ScopedPath, pointer *pb.Pointer) (err error) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (collector *PathCollector) flush(ctx context.Context, limit int) (err error) {
|
|
|
|
if len(collector.buffer) >= limit {
|
|
|
|
err = collector.db.Enqueue(ctx, collector.buffer)
|
|
|
|
collector.buffer = collector.buffer[:0]
|
|
|
|
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|