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/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)
|
|
|
|
|
2020-12-05 16:01:42 +00: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,
|
|
|
|
|
2020-11-27 09:44:19 +00:00
|
|
|
retainInfos: make(map[storj.NodeID]*RetainInfo, len(pieceCounts)),
|
2019-07-24 18:26:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// RemoteSegment takes a remote segment found in metainfo and adds pieces to bloom filters.
|
2020-10-27 06:59:14 +00:00
|
|
|
func (pieceTracker *PieceTracker) RemoteSegment(ctx context.Context, segment *metainfo.Segment) (err error) {
|
2020-09-02 08:16:58 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-07-24 18:26:43 +01:00
|
|
|
|
2020-10-27 06:59:14 +00:00
|
|
|
for _, piece := range segment.Pieces {
|
|
|
|
pieceID := segment.RootPieceID.Derive(piece.StorageNode, int32(piece.Number))
|
|
|
|
pieceTracker.add(piece.StorageNode, pieceID)
|
2019-07-24 18:26:43 +01:00
|
|
|
}
|
2020-10-27 06:59:14 +00:00
|
|
|
|
2019-07-24 18:26:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Object returns nil because gc does not interact with remote objects.
|
2020-10-27 06:59:14 +00:00
|
|
|
func (pieceTracker *PieceTracker) Object(ctx context.Context, object *metainfo.Object) (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.
|
2020-10-27 06:59:14 +00:00
|
|
|
func (pieceTracker *PieceTracker) InlineSegment(ctx context.Context, segment *metainfo.Segment) (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++
|
|
|
|
}
|