2019-10-16 17:50:29 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package live
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-10-16 17:50:29 +01:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
"storj.io/storj/storage/redis"
|
|
|
|
)
|
|
|
|
|
|
|
|
type redisLiveAccounting struct {
|
|
|
|
log *zap.Logger
|
|
|
|
|
|
|
|
client *redis.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRedisLiveAccounting(log *zap.Logger, address string) (*redisLiveAccounting, error) {
|
|
|
|
client, err := redis.NewClientFrom(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
return &redisLiveAccounting{
|
|
|
|
log: log,
|
|
|
|
client: client,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProjectStorageUsage gets inline and remote storage totals for a given
|
|
|
|
// project, back to the time of the last accounting tally.
|
|
|
|
func (cache *redisLiveAccounting) GetProjectStorageUsage(ctx context.Context, projectID uuid.UUID) (totalUsed int64, err error) {
|
|
|
|
defer mon.Task()(&ctx, projectID)(&err)
|
2019-10-26 22:51:39 +01:00
|
|
|
val, err := cache.client.Get(ctx, projectID[:])
|
2019-10-16 17:50:29 +01:00
|
|
|
if err != nil {
|
|
|
|
if storage.ErrKeyNotFound.Has(err) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return 0, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
intval, err := strconv.Atoi(string(val))
|
|
|
|
return int64(intval), Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddProjectStorageUsage lets the live accounting know that the given
|
2019-10-31 17:27:38 +00:00
|
|
|
// project has just added spaceUsed bytes of storage (from the user's
|
|
|
|
// perspective; i.e. segment size).
|
|
|
|
func (cache *redisLiveAccounting) AddProjectStorageUsage(ctx context.Context, projectID uuid.UUID, spaceUsed int64) (err error) {
|
|
|
|
defer mon.Task()(&ctx, projectID, spaceUsed)(&err)
|
|
|
|
return cache.client.IncrBy(ctx, projectID[:], spaceUsed)
|
2019-10-16 17:50:29 +01:00
|
|
|
}
|
|
|
|
|
2019-10-31 17:27:38 +00:00
|
|
|
// GetAllProjectTotals iterates through the live accounting DB and returns a map of project IDs and totals.
|
|
|
|
func (cache *redisLiveAccounting) GetAllProjectTotals(ctx context.Context) (_ map[uuid.UUID]int64, err error) {
|
2019-10-16 17:50:29 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-10-31 17:27:38 +00:00
|
|
|
|
|
|
|
projects := make(map[uuid.UUID]int64)
|
|
|
|
|
|
|
|
err = cache.client.Iterate(ctx, storage.IterateOptions{Recurse: true}, func(ctx context.Context, it storage.Iterator) error {
|
|
|
|
var item storage.ListItem
|
|
|
|
for it.Next(ctx, &item) {
|
|
|
|
if item.Key == nil {
|
|
|
|
return Error.New("nil key")
|
|
|
|
}
|
|
|
|
id := new(uuid.UUID)
|
|
|
|
copy(id[:], item.Key[:])
|
|
|
|
intval, err := strconv.Atoi(string(item.Value))
|
|
|
|
if err != nil {
|
|
|
|
return Error.New("could not get total for project %s", id.String())
|
|
|
|
}
|
|
|
|
projects[*id] = int64(intval)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return projects, err
|
2019-10-16 17:50:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close the DB connection.
|
|
|
|
func (cache *redisLiveAccounting) Close() error {
|
|
|
|
return cache.client.Close()
|
|
|
|
}
|