2020-01-10 18:53:42 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package orders
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/sync2"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-01-10 18:53:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CacheData stores the amount of inline and allocated data
|
2020-07-16 15:18:02 +01:00
|
|
|
// for a bucket bandwidth rollup.
|
2020-01-10 18:53:42 +00:00
|
|
|
type CacheData struct {
|
|
|
|
Inline int64
|
|
|
|
Allocated int64
|
2020-06-26 20:29:33 +01:00
|
|
|
Settled int64
|
2021-05-29 23:16:12 +01:00
|
|
|
Dead int64
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// CacheKey is the key information for the cached map below.
|
2020-01-10 18:53:42 +00:00
|
|
|
type CacheKey struct {
|
2021-11-23 22:50:42 +00:00
|
|
|
ProjectID uuid.UUID
|
|
|
|
BucketName string
|
|
|
|
Action pb.PieceAction
|
|
|
|
IntervalStart int64
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// RollupData contains the pending rollups waiting to be flushed to the db.
|
2020-01-10 18:53:42 +00:00
|
|
|
type RollupData map[CacheKey]CacheData
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// RollupsWriteCache stores information needed to update bucket bandwidth rollups.
|
2020-01-10 18:53:42 +00:00
|
|
|
type RollupsWriteCache struct {
|
|
|
|
DB
|
2020-01-17 22:55:53 +00:00
|
|
|
batchSize int
|
|
|
|
wg sync.WaitGroup
|
|
|
|
log *zap.Logger
|
2020-01-10 18:53:42 +00:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
pendingRollups RollupData
|
2020-01-17 22:55:53 +00:00
|
|
|
stopped bool
|
2021-09-15 19:32:27 +01:00
|
|
|
flushing bool
|
2020-01-10 18:53:42 +00:00
|
|
|
|
|
|
|
nextFlushCompletion *sync2.Fence
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// NewRollupsWriteCache creates an RollupsWriteCache.
|
2020-01-10 18:53:42 +00:00
|
|
|
func NewRollupsWriteCache(log *zap.Logger, db DB, batchSize int) *RollupsWriteCache {
|
|
|
|
return &RollupsWriteCache{
|
|
|
|
DB: db,
|
|
|
|
batchSize: batchSize,
|
|
|
|
log: log,
|
|
|
|
pendingRollups: make(RollupData),
|
|
|
|
nextFlushCompletion: new(sync2.Fence),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// UpdateBucketBandwidthAllocation updates the rollups cache adding allocated data for a bucket bandwidth rollup.
|
2020-01-10 18:53:42 +00:00
|
|
|
func (cache *RollupsWriteCache) UpdateBucketBandwidthAllocation(ctx context.Context, projectID uuid.UUID, bucketName []byte, action pb.PieceAction, amount int64, intervalStart time.Time) error {
|
2021-05-29 23:16:12 +01:00
|
|
|
return cache.updateCacheValue(ctx, projectID, bucketName, action, amount, 0, 0, 0, intervalStart.UTC())
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// UpdateBucketBandwidthInline updates the rollups cache adding inline data for a bucket bandwidth rollup.
|
2020-01-10 18:53:42 +00:00
|
|
|
func (cache *RollupsWriteCache) UpdateBucketBandwidthInline(ctx context.Context, projectID uuid.UUID, bucketName []byte, action pb.PieceAction, amount int64, intervalStart time.Time) error {
|
2021-05-29 23:16:12 +01:00
|
|
|
return cache.updateCacheValue(ctx, projectID, bucketName, action, 0, amount, 0, 0, intervalStart.UTC())
|
2020-06-26 20:29:33 +01:00
|
|
|
}
|
|
|
|
|
2021-05-29 23:16:12 +01:00
|
|
|
// UpdateBucketBandwidthSettle updates the rollups cache adding settled data for a bucket bandwidth rollup - deadAmount is not used.
|
|
|
|
func (cache *RollupsWriteCache) UpdateBucketBandwidthSettle(ctx context.Context, projectID uuid.UUID, bucketName []byte, action pb.PieceAction, settledAmount, deadAmount int64, intervalStart time.Time) error {
|
|
|
|
return cache.updateCacheValue(ctx, projectID, bucketName, action, 0, 0, settledAmount, deadAmount, intervalStart.UTC())
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 18:28:00 +00:00
|
|
|
// resetCache should only be called after you have acquired the cache lock. It
|
2021-11-23 22:50:42 +00:00
|
|
|
// will reset the various cache values and return the pendingRollups.
|
|
|
|
func (cache *RollupsWriteCache) resetCache() RollupData {
|
2020-01-10 18:53:42 +00:00
|
|
|
pendingRollups := cache.pendingRollups
|
|
|
|
cache.pendingRollups = make(RollupData)
|
2021-09-15 19:32:27 +01:00
|
|
|
|
2021-11-23 22:50:42 +00:00
|
|
|
return pendingRollups
|
2020-01-17 18:28:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Flush resets cache then flushes the everything in the rollups write cache to the database.
|
2020-01-17 22:55:53 +00:00
|
|
|
func (cache *RollupsWriteCache) Flush(ctx context.Context) {
|
2020-01-17 18:28:00 +00:00
|
|
|
defer mon.Task()(&ctx)(nil)
|
2021-09-15 19:32:27 +01:00
|
|
|
|
2020-01-17 18:28:00 +00:00
|
|
|
cache.mu.Lock()
|
2021-09-15 19:32:27 +01:00
|
|
|
|
|
|
|
// while we're already flushing, wait for it to complete.
|
|
|
|
for cache.flushing {
|
|
|
|
done := cache.nextFlushCompletion.Done()
|
|
|
|
cache.mu.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cache.mu.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
cache.flushing = true
|
2021-11-23 22:50:42 +00:00
|
|
|
pendingRollups := cache.resetCache()
|
2021-09-15 19:32:27 +01:00
|
|
|
|
2020-01-17 22:55:53 +00:00
|
|
|
cache.mu.Unlock()
|
2021-09-15 19:32:27 +01:00
|
|
|
|
2021-11-23 22:50:42 +00:00
|
|
|
cache.flush(ctx, pendingRollups)
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 22:55:53 +00:00
|
|
|
// CloseAndFlush flushes anything in the cache and marks the cache as stopped.
|
|
|
|
func (cache *RollupsWriteCache) CloseAndFlush(ctx context.Context) error {
|
|
|
|
cache.mu.Lock()
|
|
|
|
cache.stopped = true
|
|
|
|
cache.mu.Unlock()
|
2021-09-15 19:32:27 +01:00
|
|
|
|
2020-01-17 22:55:53 +00:00
|
|
|
cache.wg.Wait()
|
|
|
|
|
|
|
|
cache.Flush(ctx)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// flush flushes the everything in the rollups write cache to the database.
|
2021-11-23 22:50:42 +00:00
|
|
|
func (cache *RollupsWriteCache) flush(ctx context.Context, pendingRollups RollupData) {
|
2020-01-14 00:36:12 +00:00
|
|
|
defer mon.Task()(&ctx)(nil)
|
|
|
|
|
2021-09-15 19:32:27 +01:00
|
|
|
if len(pendingRollups) > 0 {
|
|
|
|
rollups := make([]BucketBandwidthRollup, 0, len(pendingRollups))
|
|
|
|
for cacheKey, cacheData := range pendingRollups {
|
|
|
|
rollups = append(rollups, BucketBandwidthRollup{
|
2021-11-23 22:50:42 +00:00
|
|
|
ProjectID: cacheKey.ProjectID,
|
|
|
|
BucketName: cacheKey.BucketName,
|
|
|
|
IntervalStart: time.Unix(cacheKey.IntervalStart, 0),
|
|
|
|
Action: cacheKey.Action,
|
|
|
|
Inline: cacheData.Inline,
|
|
|
|
Allocated: cacheData.Allocated,
|
|
|
|
Settled: cacheData.Settled,
|
|
|
|
Dead: cacheData.Dead,
|
2021-09-15 19:32:27 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-23 22:50:42 +00:00
|
|
|
err := cache.DB.UpdateBandwidthBatch(ctx, rollups)
|
2021-09-15 19:32:27 +01:00
|
|
|
if err != nil {
|
|
|
|
mon.Event("rollups_write_cache_flush_lost")
|
|
|
|
cache.log.Error("MONEY LOST! Bucket bandwidth rollup batch flush failed.", zap.Error(err))
|
|
|
|
}
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cache.mu.Lock()
|
2021-09-15 19:32:27 +01:00
|
|
|
defer cache.mu.Unlock()
|
|
|
|
|
|
|
|
cache.nextFlushCompletion.Release()
|
|
|
|
cache.nextFlushCompletion = new(sync2.Fence)
|
|
|
|
cache.flushing = false
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
2021-05-29 23:16:12 +01:00
|
|
|
func (cache *RollupsWriteCache) updateCacheValue(ctx context.Context, projectID uuid.UUID, bucketName []byte, action pb.PieceAction, allocated, inline, settled, dead int64, intervalStart time.Time) error {
|
2020-01-14 00:36:12 +00:00
|
|
|
defer mon.Task()(&ctx)(nil)
|
|
|
|
|
2020-01-10 18:53:42 +00:00
|
|
|
cache.mu.Lock()
|
|
|
|
defer cache.mu.Unlock()
|
|
|
|
|
2020-01-17 22:55:53 +00:00
|
|
|
if cache.stopped {
|
|
|
|
return Error.New("RollupsWriteCache is stopped")
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:53:42 +00:00
|
|
|
key := CacheKey{
|
2021-11-23 22:50:42 +00:00
|
|
|
ProjectID: projectID,
|
|
|
|
BucketName: string(bucketName),
|
|
|
|
Action: action,
|
|
|
|
IntervalStart: time.Date(intervalStart.Year(), intervalStart.Month(), intervalStart.Day(), intervalStart.Hour(), 0, 0, 0, intervalStart.Location()).Unix(),
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 19:32:27 +01:00
|
|
|
// pevent unbounded memory memory growth if we're not flushing fast enough
|
|
|
|
// to keep up with incoming writes.
|
2020-01-10 18:53:42 +00:00
|
|
|
data, ok := cache.pendingRollups[key]
|
2021-09-15 19:32:27 +01:00
|
|
|
if !ok && len(cache.pendingRollups) >= cache.batchSize {
|
|
|
|
mon.Event("rollups_write_cache_update_lost")
|
|
|
|
cache.log.Error("MONEY LOST! Flushing too slow to keep up with demand.")
|
|
|
|
} else {
|
|
|
|
|
|
|
|
data.Allocated += allocated
|
|
|
|
data.Inline += inline
|
|
|
|
data.Settled += settled
|
2021-05-29 23:16:12 +01:00
|
|
|
data.Dead += dead
|
2021-09-15 19:32:27 +01:00
|
|
|
cache.pendingRollups[key] = data
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 19:32:27 +01:00
|
|
|
if len(cache.pendingRollups) < cache.batchSize {
|
2020-01-17 22:55:53 +00:00
|
|
|
return nil
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
2020-01-17 22:55:53 +00:00
|
|
|
|
2021-09-15 19:32:27 +01:00
|
|
|
if !cache.flushing {
|
|
|
|
cache.flushing = true
|
2021-11-23 22:50:42 +00:00
|
|
|
pendingRollups := cache.resetCache()
|
2021-09-15 19:32:27 +01:00
|
|
|
|
|
|
|
cache.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer cache.wg.Done()
|
2021-11-23 22:50:42 +00:00
|
|
|
cache.flush(ctx, pendingRollups)
|
2021-09-15 19:32:27 +01:00
|
|
|
}()
|
|
|
|
}
|
2020-01-17 22:55:53 +00:00
|
|
|
|
|
|
|
return nil
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 22:55:53 +00:00
|
|
|
// OnNextFlush waits until the next time a flush call is made, then closes
|
2020-01-10 18:53:42 +00:00
|
|
|
// the returned channel.
|
|
|
|
func (cache *RollupsWriteCache) OnNextFlush() <-chan struct{} {
|
|
|
|
cache.mu.Lock()
|
2021-09-15 19:32:27 +01:00
|
|
|
defer cache.mu.Unlock()
|
|
|
|
|
|
|
|
return cache.nextFlushCompletion.Done()
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentSize returns the current size of the cache.
|
|
|
|
func (cache *RollupsWriteCache) CurrentSize() int {
|
|
|
|
cache.mu.Lock()
|
|
|
|
defer cache.mu.Unlock()
|
2021-09-15 19:32:27 +01:00
|
|
|
|
|
|
|
return len(cache.pendingRollups)
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentData returns the contents of the cache.
|
|
|
|
func (cache *RollupsWriteCache) CurrentData() RollupData {
|
|
|
|
cache.mu.Lock()
|
|
|
|
defer cache.mu.Unlock()
|
2021-09-15 19:32:27 +01:00
|
|
|
|
2020-01-10 18:53:42 +00:00
|
|
|
copyCache := RollupData{}
|
|
|
|
for k, v := range cache.pendingRollups {
|
|
|
|
copyCache[k] = v
|
|
|
|
}
|
|
|
|
return copyCache
|
|
|
|
}
|