2019-07-24 18:26:43 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package gc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/bloomfilter"
|
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
2019-09-10 14:24:16 +01:00
|
|
|
"storj.io/storj/satellite/metainfo"
|
2019-07-24 18:26:43 +01:00
|
|
|
)
|
|
|
|
|
2019-09-10 14:24:16 +01:00
|
|
|
var _ metainfo.Observer = (*PieceTracker)(nil)
|
|
|
|
|
2019-07-24 18:26:43 +01:00
|
|
|
// PieceTracker implements the metainfo loop observer interface for garbage collection
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Observer
|
2019-07-24 18:26:43 +01:00
|
|
|
type PieceTracker struct {
|
|
|
|
log *zap.Logger
|
|
|
|
config Config
|
|
|
|
creationDate time.Time
|
2019-08-27 13:37:42 +01:00
|
|
|
// TODO: should we use int or int64 consistently for piece count (db type is int64)?
|
|
|
|
pieceCounts map[storj.NodeID]int
|
2019-07-24 18:26:43 +01:00
|
|
|
|
|
|
|
retainInfos map[storj.NodeID]*RetainInfo
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// NewPieceTracker instantiates a new gc piece tracker to be subscribed to the metainfo loop.
|
2019-07-24 18:26:43 +01:00
|
|
|
func NewPieceTracker(log *zap.Logger, config Config, pieceCounts map[storj.NodeID]int) *PieceTracker {
|
|
|
|
return &PieceTracker{
|
|
|
|
log: log,
|
|
|
|
config: config,
|
|
|
|
creationDate: time.Now().UTC(),
|
|
|
|
pieceCounts: pieceCounts,
|
|
|
|
|
|
|
|
retainInfos: make(map[storj.NodeID]*RetainInfo),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// RemoteSegment takes a remote segment found in metainfo and adds pieces to bloom filters.
|
2019-09-12 11:38:49 +01:00
|
|
|
func (pieceTracker *PieceTracker) RemoteSegment(ctx context.Context, path metainfo.ScopedPath, pointer *pb.Pointer) (err error) {
|
|
|
|
defer mon.Task()(&ctx, path.Raw)(&err)
|
2019-07-24 18:26:43 +01:00
|
|
|
|
|
|
|
remote := pointer.GetRemote()
|
|
|
|
pieces := remote.GetRemotePieces()
|
|
|
|
|
|
|
|
for _, piece := range pieces {
|
|
|
|
pieceID := remote.RootPieceId.Derive(piece.NodeId, piece.PieceNum)
|
|
|
|
pieceTracker.add(piece.NodeId, pieceID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Object returns nil because gc does not interact with remote objects.
|
2019-09-13 14:51:41 +01:00
|
|
|
func (pieceTracker *PieceTracker) Object(ctx context.Context, path metainfo.ScopedPath, pointer *pb.Pointer) (err error) {
|
2019-07-24 18:26:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// InlineSegment returns nil because we're only doing gc for storage nodes for now.
|
2019-09-12 11:38:49 +01:00
|
|
|
func (pieceTracker *PieceTracker) InlineSegment(ctx context.Context, path metainfo.ScopedPath, pointer *pb.Pointer) (err error) {
|
2019-07-24 18:26:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// adds a pieceID to the relevant node's RetainInfo.
|
2019-07-24 18:26:43 +01:00
|
|
|
func (pieceTracker *PieceTracker) add(nodeID storj.NodeID, pieceID storj.PieceID) {
|
|
|
|
if _, ok := pieceTracker.retainInfos[nodeID]; !ok {
|
|
|
|
// If we know how many pieces a node should be storing, use that number. Otherwise use default.
|
|
|
|
numPieces := pieceTracker.config.InitialPieces
|
|
|
|
if pieceTracker.pieceCounts[nodeID] > 0 {
|
|
|
|
numPieces = pieceTracker.pieceCounts[nodeID]
|
|
|
|
}
|
2020-05-11 06:26:32 +01:00
|
|
|
// limit size of bloom filter to ensure we are under the limit for RPC
|
2019-07-24 18:26:43 +01:00
|
|
|
filter := bloomfilter.NewOptimalMaxSize(numPieces, pieceTracker.config.FalsePositiveRate, 2*memory.MiB)
|
|
|
|
pieceTracker.retainInfos[nodeID] = &RetainInfo{
|
|
|
|
Filter: filter,
|
|
|
|
CreationDate: pieceTracker.creationDate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pieceTracker.retainInfos[nodeID].Filter.Add(pieceID)
|
|
|
|
pieceTracker.retainInfos[nodeID].Count++
|
|
|
|
}
|