2019-08-12 22:43:05 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package pieces
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-08-12 22:43:05 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/sync2"
|
2019-08-12 22:43:05 +01:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CacheService updates the space used cache
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Chore
|
2019-08-12 22:43:05 +01:00
|
|
|
type CacheService struct {
|
|
|
|
log *zap.Logger
|
|
|
|
usageCache *BlobsUsageCache
|
|
|
|
store *Store
|
|
|
|
loop sync2.Cycle
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates a new cache service that updates the space usage cache on startup and syncs the cache values to
|
|
|
|
// persistent storage on an interval
|
|
|
|
func NewService(log *zap.Logger, usageCache *BlobsUsageCache, pieces *Store, interval time.Duration) *CacheService {
|
|
|
|
return &CacheService{
|
|
|
|
log: log,
|
|
|
|
usageCache: usageCache,
|
|
|
|
store: pieces,
|
|
|
|
loop: *sync2.NewCycle(interval),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run recalculates the space used cache once and also runs a loop to sync the space used cache
|
|
|
|
// to persistent storage on an interval
|
|
|
|
func (service *CacheService) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
totalAtStart := service.usageCache.copyCacheTotals()
|
|
|
|
|
|
|
|
// recalculate the cache once
|
|
|
|
newTotal, newTotalBySatellite, err := service.store.SpaceUsedTotalAndBySatellite(ctx)
|
|
|
|
if err != nil {
|
|
|
|
service.log.Error("error getting current space used calculation: ", zap.Error(err))
|
|
|
|
}
|
2019-12-21 13:11:24 +00:00
|
|
|
newTrashTotal, err := service.store.SpaceUsedForTrash(ctx)
|
|
|
|
if err != nil {
|
|
|
|
service.log.Error("error getting current space for trash: ", zap.Error(err))
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
2019-12-21 13:11:24 +00:00
|
|
|
service.usageCache.Recalculate(ctx, newTotal,
|
|
|
|
totalAtStart.spaceUsedForPieces,
|
|
|
|
newTotalBySatellite,
|
|
|
|
totalAtStart.spaceUsedBySatellite,
|
|
|
|
newTrashTotal,
|
|
|
|
totalAtStart.spaceUsedForTrash,
|
|
|
|
)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
|
|
|
if err = service.store.spaceUsedDB.Init(ctx); err != nil {
|
|
|
|
service.log.Error("error during init space usage db: ", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return service.loop.Run(ctx, func(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
// on a loop sync the cache values to the db so that we have the them saved
|
|
|
|
// in the case that the storagenode restarts
|
|
|
|
if err := service.PersistCacheTotals(ctx); err != nil {
|
|
|
|
service.log.Error("error persisting cache totals to the database: ", zap.Error(err))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PersistCacheTotals saves the current totals of the space used cache to the database
|
|
|
|
// so that if the storagenode restarts it can retrieve the latest space used
|
|
|
|
// values without needing to recalculate since that could take a long time
|
|
|
|
func (service *CacheService) PersistCacheTotals(ctx context.Context) error {
|
|
|
|
cache := service.usageCache
|
2019-09-12 17:42:39 +01:00
|
|
|
cache.mu.Lock()
|
|
|
|
defer cache.mu.Unlock()
|
2019-12-21 13:11:24 +00:00
|
|
|
if err := service.store.spaceUsedDB.UpdatePieceTotal(ctx, cache.spaceUsedForPieces); err != nil {
|
2019-08-12 22:43:05 +01:00
|
|
|
return err
|
|
|
|
}
|
2019-12-21 13:11:24 +00:00
|
|
|
if err := service.store.spaceUsedDB.UpdatePieceTotalsForAllSatellites(ctx, cache.spaceUsedBySatellite); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := service.store.spaceUsedDB.UpdateTrashTotal(ctx, cache.spaceUsedForTrash); err != nil {
|
2019-08-12 22:43:05 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes the space used cache with the most recent values that were stored persistently
|
|
|
|
func (service *CacheService) Init(ctx context.Context) (err error) {
|
2019-12-21 13:11:24 +00:00
|
|
|
total, err := service.store.spaceUsedDB.GetPieceTotal(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
if err != nil {
|
|
|
|
service.log.Error("CacheServiceInit error during initializing space usage cache GetTotal:", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
totalBySatellite, err := service.store.spaceUsedDB.GetPieceTotalsForAllSatellites(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
if err != nil {
|
|
|
|
service.log.Error("CacheServiceInit error during initializing space usage cache GetTotalsForAllSatellites:", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
service.usageCache.init(total, totalBySatellite)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the loop
|
|
|
|
func (service *CacheService) Close() (err error) {
|
|
|
|
service.loop.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlobsUsageCache is a blob storage with a cache for storing
|
|
|
|
// totals of current space used
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Database
|
2019-08-12 22:43:05 +01:00
|
|
|
type BlobsUsageCache struct {
|
|
|
|
storage.Blobs
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
spaceUsedForPieces int64
|
|
|
|
spaceUsedForTrash int64
|
|
|
|
spaceUsedBySatellite map[storj.NodeID]int64
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBlobsUsageCache creates a new disk blob store with a space used cache
|
|
|
|
func NewBlobsUsageCache(blob storage.Blobs) *BlobsUsageCache {
|
|
|
|
return &BlobsUsageCache{
|
2019-12-21 13:11:24 +00:00
|
|
|
Blobs: blob,
|
|
|
|
spaceUsedBySatellite: map[storj.NodeID]int64{},
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBlobsUsageCacheTest creates a new disk blob store with a space used cache
|
2019-12-21 13:11:24 +00:00
|
|
|
func NewBlobsUsageCacheTest(blob storage.Blobs, piecesTotal, trashTotal int64, spaceUsedBySatellite map[storj.NodeID]int64) *BlobsUsageCache {
|
2019-08-12 22:43:05 +01:00
|
|
|
return &BlobsUsageCache{
|
2019-12-21 13:11:24 +00:00
|
|
|
Blobs: blob,
|
|
|
|
spaceUsedForPieces: piecesTotal,
|
|
|
|
spaceUsedForTrash: trashTotal,
|
|
|
|
spaceUsedBySatellite: spaceUsedBySatellite,
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blobs *BlobsUsageCache) init(total int64, totalBySatellite map[storj.NodeID]int64) {
|
|
|
|
blobs.mu.Lock()
|
|
|
|
defer blobs.mu.Unlock()
|
2019-12-21 13:11:24 +00:00
|
|
|
blobs.spaceUsedForPieces = total
|
|
|
|
blobs.spaceUsedBySatellite = totalBySatellite
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SpaceUsedBySatellite returns the current total space used for a specific
|
|
|
|
// satellite for all pieces (not including header bytes)
|
|
|
|
func (blobs *BlobsUsageCache) SpaceUsedBySatellite(ctx context.Context, satelliteID storj.NodeID) (int64, error) {
|
|
|
|
blobs.mu.Lock()
|
|
|
|
defer blobs.mu.Unlock()
|
2019-12-21 13:11:24 +00:00
|
|
|
return blobs.spaceUsedBySatellite[satelliteID], nil
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SpaceUsedForPieces returns the current total used space for
|
|
|
|
//// all pieces content (not including header bytes)
|
|
|
|
func (blobs *BlobsUsageCache) SpaceUsedForPieces(ctx context.Context) (int64, error) {
|
|
|
|
blobs.mu.Lock()
|
|
|
|
defer blobs.mu.Unlock()
|
2019-12-21 13:11:24 +00:00
|
|
|
return blobs.spaceUsedForPieces, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SpaceUsedForTrash returns the current total used space for the trash dir
|
|
|
|
func (blobs *BlobsUsageCache) SpaceUsedForTrash(ctx context.Context) (int64, error) {
|
|
|
|
blobs.mu.Lock()
|
|
|
|
defer blobs.mu.Unlock()
|
|
|
|
return blobs.spaceUsedForTrash, nil
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete gets the size of the piece that is going to be deleted then deletes it and
|
|
|
|
// updates the space used cache accordingly
|
|
|
|
func (blobs *BlobsUsageCache) Delete(ctx context.Context, blobRef storage.BlobRef) error {
|
2019-12-21 13:11:24 +00:00
|
|
|
_, pieceContentSize, err := blobs.pieceContentSize(ctx, blobRef)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := blobs.Blobs.Delete(ctx, blobRef); err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
satelliteID, err := storj.NodeIDFromBytes(blobRef.Namespace)
|
2019-08-12 22:43:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-21 13:11:24 +00:00
|
|
|
blobs.Update(ctx, satelliteID, -pieceContentSize, 0)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blobs *BlobsUsageCache) pieceContentSize(ctx context.Context, blobRef storage.BlobRef) (size int64, contentSize int64, err error) {
|
|
|
|
blobInfo, err := blobs.Stat(ctx, blobRef)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
2019-08-12 22:43:05 +01:00
|
|
|
pieceAccess, err := newStoredPieceAccess(nil, blobInfo)
|
|
|
|
if err != nil {
|
2019-12-21 13:11:24 +00:00
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return pieceAccess.Size(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates the cache totals with the piece content size
|
|
|
|
func (blobs *BlobsUsageCache) Update(ctx context.Context, satelliteID storj.NodeID, piecesDelta, trashDelta int64) {
|
|
|
|
blobs.mu.Lock()
|
|
|
|
defer blobs.mu.Unlock()
|
|
|
|
blobs.spaceUsedForPieces += piecesDelta
|
|
|
|
blobs.spaceUsedBySatellite[satelliteID] += piecesDelta
|
|
|
|
blobs.spaceUsedForTrash += trashDelta
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trash moves the ref to the trash and updates the cache
|
|
|
|
func (blobs *BlobsUsageCache) Trash(ctx context.Context, blobRef storage.BlobRef) error {
|
|
|
|
size, pieceContentSize, err := blobs.pieceContentSize(ctx, blobRef)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
2019-12-21 13:11:24 +00:00
|
|
|
|
|
|
|
err = blobs.Blobs.Trash(ctx, blobRef)
|
2019-08-12 22:43:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
satelliteID, err := storj.NodeIDFromBytes(blobRef.Namespace)
|
|
|
|
if err != nil {
|
2019-08-12 22:43:05 +01:00
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
blobs.Update(ctx, satelliteID, -pieceContentSize, size)
|
2019-08-12 22:43:05 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
// EmptyTrash empties the trash and updates the cache
|
|
|
|
func (blobs *BlobsUsageCache) EmptyTrash(ctx context.Context, namespace []byte, trashedBefore time.Time) (int64, [][]byte, error) {
|
|
|
|
satelliteID, err := storj.NodeIDFromBytes(namespace)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesEmptied, keys, err := blobs.Blobs.EmptyTrash(ctx, namespace, trashedBefore)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blobs.Update(ctx, satelliteID, 0, -bytesEmptied)
|
|
|
|
|
|
|
|
return bytesEmptied, keys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreTrash restores the trash for the namespace and updates the cache
|
|
|
|
func (blobs *BlobsUsageCache) RestoreTrash(ctx context.Context, namespace []byte) ([][]byte, error) {
|
|
|
|
satelliteID, err := storj.NodeIDFromBytes(namespace)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keysRestored, err := blobs.Blobs.RestoreTrash(ctx, namespace)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keysRestored {
|
|
|
|
size, contentSize, sizeErr := blobs.pieceContentSize(ctx, storage.BlobRef{
|
|
|
|
Key: key,
|
|
|
|
Namespace: namespace,
|
|
|
|
})
|
|
|
|
if sizeErr != nil {
|
|
|
|
err = errs.Combine(err, sizeErr)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
blobs.Update(ctx, satelliteID, contentSize, -size)
|
|
|
|
}
|
|
|
|
|
|
|
|
return keysRestored, err
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (blobs *BlobsUsageCache) copyCacheTotals() BlobsUsageCache {
|
2019-09-12 17:42:39 +01:00
|
|
|
blobs.mu.Lock()
|
|
|
|
defer blobs.mu.Unlock()
|
2019-08-12 22:43:05 +01:00
|
|
|
var copyMap = map[storj.NodeID]int64{}
|
2019-12-21 13:11:24 +00:00
|
|
|
for k, v := range blobs.spaceUsedBySatellite {
|
2019-08-12 22:43:05 +01:00
|
|
|
copyMap[k] = v
|
|
|
|
}
|
|
|
|
return BlobsUsageCache{
|
2019-12-21 13:11:24 +00:00
|
|
|
spaceUsedForPieces: blobs.spaceUsedForPieces,
|
|
|
|
spaceUsedForTrash: blobs.spaceUsedForTrash,
|
|
|
|
spaceUsedBySatellite: copyMap,
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recalculate estimates new totals for the space used cache. In order to get new totals for the
|
|
|
|
// space used cache, we had to iterate over all the pieces on disk. Since that can potentially take
|
|
|
|
// a long time, here we need to check if we missed any additions/deletions while we were iterating and
|
|
|
|
// estimate how many bytes missed then add those to the space used result of iteration.
|
2019-12-21 13:11:24 +00:00
|
|
|
func (blobs *BlobsUsageCache) Recalculate(ctx context.Context, newTotal, totalAtIterationStart int64, newTotalBySatellite,
|
|
|
|
totalBySatelliteAtIterationStart map[storj.NodeID]int64, newTrashTotal, trashTotalAtIterationStart int64) {
|
|
|
|
|
2019-08-12 22:43:05 +01:00
|
|
|
totalsAtIterationEnd := blobs.copyCacheTotals()
|
|
|
|
|
|
|
|
estimatedTotals := estimate(newTotal,
|
|
|
|
totalAtIterationStart,
|
2019-12-21 13:11:24 +00:00
|
|
|
totalsAtIterationEnd.spaceUsedForPieces,
|
2019-08-12 22:43:05 +01:00
|
|
|
)
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
estimatedTrash := estimate(newTrashTotal,
|
|
|
|
trashTotalAtIterationStart,
|
|
|
|
totalsAtIterationEnd.spaceUsedForTrash)
|
|
|
|
|
2019-08-12 22:43:05 +01:00
|
|
|
var estimatedTotalsBySatellite = map[storj.NodeID]int64{}
|
|
|
|
for ID, newTotal := range newTotalBySatellite {
|
|
|
|
estimatedNewTotal := estimate(newTotal,
|
|
|
|
totalBySatelliteAtIterationStart[ID],
|
2019-12-21 13:11:24 +00:00
|
|
|
totalsAtIterationEnd.spaceUsedBySatellite[ID],
|
2019-08-12 22:43:05 +01:00
|
|
|
)
|
|
|
|
// if the estimatedNewTotal is zero then there is no data stored
|
|
|
|
// for this satelliteID so don't add it to the cache
|
|
|
|
if estimatedNewTotal == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
estimatedTotalsBySatellite[ID] = estimatedNewTotal
|
|
|
|
}
|
|
|
|
|
|
|
|
// find any saIDs that are in totalsAtIterationEnd but not in newTotalSpaceUsedBySatellite
|
2019-12-21 13:11:24 +00:00
|
|
|
missedWhenIterationEnded := getMissed(totalsAtIterationEnd.spaceUsedBySatellite,
|
2019-08-12 22:43:05 +01:00
|
|
|
newTotalBySatellite,
|
|
|
|
)
|
|
|
|
if len(missedWhenIterationEnded) > 0 {
|
|
|
|
for ID := range missedWhenIterationEnded {
|
|
|
|
estimatedNewTotal := estimate(0,
|
|
|
|
totalBySatelliteAtIterationStart[ID],
|
2019-12-21 13:11:24 +00:00
|
|
|
totalsAtIterationEnd.spaceUsedBySatellite[ID],
|
2019-08-12 22:43:05 +01:00
|
|
|
)
|
|
|
|
if estimatedNewTotal == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
estimatedTotalsBySatellite[ID] = estimatedNewTotal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blobs.mu.Lock()
|
2019-12-21 13:11:24 +00:00
|
|
|
blobs.spaceUsedForPieces = estimatedTotals
|
|
|
|
blobs.spaceUsedForTrash = estimatedTrash
|
|
|
|
blobs.spaceUsedBySatellite = estimatedTotalsBySatellite
|
2019-08-12 22:43:05 +01:00
|
|
|
blobs.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func estimate(newSpaceUsedTotal, totalAtIterationStart, totalAtIterationEnd int64) int64 {
|
|
|
|
if newSpaceUsedTotal == totalAtIterationEnd {
|
|
|
|
return newSpaceUsedTotal
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we missed writes/deletes while iterating, we will assume that half of those missed occurred before
|
|
|
|
// the iteration and half occurred after. So here we add half of the delta to the result space used totals
|
|
|
|
// from the iteration to account for those missed.
|
|
|
|
spaceUsedDeltaDuringIteration := totalAtIterationEnd - totalAtIterationStart
|
|
|
|
estimatedTotal := newSpaceUsedTotal + (spaceUsedDeltaDuringIteration / 2)
|
|
|
|
if estimatedTotal < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return estimatedTotal
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMissed(endTotals, newTotals map[storj.NodeID]int64) map[storj.NodeID]int64 {
|
|
|
|
var missed = map[storj.NodeID]int64{}
|
|
|
|
for id, total := range endTotals {
|
|
|
|
if _, ok := newTotals[id]; !ok {
|
|
|
|
missed[id] = total
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return missed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close satisfies the pieces interface
|
|
|
|
func (blobs *BlobsUsageCache) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCreateV0 creates a new V0 blob that can be written. This is only appropriate in test situations.
|
|
|
|
func (blobs *BlobsUsageCache) TestCreateV0(ctx context.Context, ref storage.BlobRef) (_ storage.BlobWriter, err error) {
|
|
|
|
fStore := blobs.Blobs.(interface {
|
|
|
|
TestCreateV0(ctx context.Context, ref storage.BlobRef) (_ storage.BlobWriter, err error)
|
|
|
|
})
|
|
|
|
return fStore.TestCreateV0(ctx, ref)
|
|
|
|
}
|