2019-10-16 17:50:29 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package live
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-21 17:27:12 +00:00
|
|
|
"errors"
|
2020-06-02 00:19:10 +01:00
|
|
|
"fmt"
|
2020-12-21 17:27:12 +00:00
|
|
|
"net/url"
|
2019-10-16 17:50:29 +01:00
|
|
|
"strconv"
|
2020-07-03 19:34:13 +01:00
|
|
|
"strings"
|
2020-06-02 00:19:10 +01:00
|
|
|
"time"
|
2019-10-16 17:50:29 +01:00
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
"github.com/go-redis/redis"
|
2019-10-16 17:50:29 +01:00
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-12-21 17:27:12 +00:00
|
|
|
"storj.io/storj/satellite/accounting"
|
2019-10-16 17:50:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type redisLiveAccounting struct {
|
|
|
|
client *redis.Client
|
|
|
|
}
|
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
// newRedisLiveAccounting returns a redisLiveAccounting cache instance.
|
|
|
|
//
|
|
|
|
// It returns accounting.ErrInvalidArgument if the connection address is invalid
|
|
|
|
// according to Redis.
|
|
|
|
//
|
|
|
|
// The function pings to the Redis server for verifying the connectivity but if
|
|
|
|
// it fails then it returns an instance and accounting.ErrSystemOrNetError
|
|
|
|
// because it means that Redis may not be operative at this precise moment but
|
|
|
|
// it may be in future method calls as it handles automatically reconnects.
|
|
|
|
func newRedisLiveAccounting(address string) (*redisLiveAccounting, error) {
|
|
|
|
redisurl, err := url.Parse(address)
|
2019-10-16 17:50:29 +01:00
|
|
|
if err != nil {
|
2020-12-21 17:27:12 +00:00
|
|
|
return nil, accounting.ErrInvalidArgument.New("address: invalid URL; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if redisurl.Scheme != "redis" {
|
|
|
|
return nil, accounting.ErrInvalidArgument.New("address: not a redis:// formatted address")
|
|
|
|
}
|
|
|
|
|
|
|
|
q := redisurl.Query()
|
|
|
|
db := q.Get("db")
|
|
|
|
if db == "" {
|
|
|
|
return nil, accounting.ErrInvalidArgument.New("address: a database number has to be specified")
|
2019-10-16 17:50:29 +01:00
|
|
|
}
|
2020-12-21 17:27:12 +00:00
|
|
|
|
|
|
|
dbn, err := strconv.Atoi(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, accounting.ErrInvalidArgument.New("address: invalid database number %s", db)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := redis.NewClient(&redis.Options{
|
|
|
|
Addr: redisurl.Host,
|
|
|
|
Password: q.Get("password"),
|
|
|
|
DB: dbn,
|
|
|
|
})
|
|
|
|
|
|
|
|
cache := &redisLiveAccounting{
|
2019-10-16 17:50:29 +01:00
|
|
|
client: client,
|
2020-12-21 17:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ping here to verify we are able to connect to Redis with the initialized client.
|
|
|
|
if err := client.Ping().Err(); err != nil {
|
|
|
|
return cache, accounting.ErrSystemOrNetError.New("Redis ping failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cache, nil
|
2019-10-16 17:50:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
return cache.getInt64(ctx, string(projectID[:]))
|
2020-06-02 00:19:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetProjectBandwidthUsage returns the current bandwidth usage
|
|
|
|
// from specific project.
|
|
|
|
func (cache *redisLiveAccounting) GetProjectBandwidthUsage(ctx context.Context, projectID uuid.UUID, now time.Time) (currentUsed int64, err error) {
|
2020-12-21 17:27:12 +00:00
|
|
|
defer mon.Task()(&ctx, projectID, now)(&err)
|
|
|
|
|
|
|
|
return cache.getInt64(ctx, createBandwidthProjectIDKey(projectID, now))
|
2020-06-02 00:19:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateProjectBandwidthUsage increment the bandwidth cache key value.
|
|
|
|
func (cache *redisLiveAccounting) UpdateProjectBandwidthUsage(ctx context.Context, projectID uuid.UUID, increment int64, ttl time.Duration, now time.Time) (err error) {
|
2020-12-21 17:27:12 +00:00
|
|
|
mon.Task()(&ctx, projectID, increment, ttl, now)(&err)
|
2020-06-02 00:19:10 +01:00
|
|
|
|
|
|
|
// The following script will increment the cache key
|
|
|
|
// by a specific value. If the key does not exist, it is
|
|
|
|
// set to 0 before performing the operation.
|
|
|
|
// The key expiration will be set only in the first iteration.
|
|
|
|
// To achieve this we compare the increment and key value,
|
|
|
|
// if they are equal its the first iteration.
|
|
|
|
// More details on rate limiter section: https://redis.io/commands/incr
|
|
|
|
script := fmt.Sprintf(`local current
|
|
|
|
current = redis.call("incrby", KEYS[1], "%d")
|
2020-12-21 17:27:12 +00:00
|
|
|
if tonumber(current) == %d then
|
2020-06-02 00:19:10 +01:00
|
|
|
redis.call("expire",KEYS[1], %d)
|
|
|
|
end
|
|
|
|
return current
|
|
|
|
`, increment, increment, int(ttl.Seconds()))
|
|
|
|
|
|
|
|
key := createBandwidthProjectIDKey(projectID, now)
|
2020-12-21 17:27:12 +00:00
|
|
|
err = cache.client.Eval(script, []string{key}).Err()
|
|
|
|
if err != nil {
|
|
|
|
return accounting.ErrSystemOrNetError.New("Redis eval failed: %w", err)
|
|
|
|
}
|
2020-06-02 00:19:10 +01:00
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
return nil
|
2020-06-02 00:19:10 +01:00
|
|
|
}
|
|
|
|
|
2019-10-16 17:50:29 +01:00
|
|
|
// 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)
|
2020-12-21 17:27:12 +00:00
|
|
|
|
|
|
|
_, err = cache.client.IncrBy(string(projectID[:]), spaceUsed).Result()
|
|
|
|
if err != nil {
|
|
|
|
return accounting.ErrSystemOrNetError.New("Redis incrby failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
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.
|
2020-12-21 17:27:12 +00:00
|
|
|
//
|
|
|
|
// TODO (https://storjlabs.atlassian.net/browse/IN-173): see if it possible to
|
|
|
|
// get key/value pairs with one single call.
|
2019-10-31 17:27:38 +00:00
|
|
|
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)
|
2020-12-21 17:27:12 +00:00
|
|
|
it := cache.client.Scan(0, "*", 0).Iterator()
|
|
|
|
for it.Next() {
|
|
|
|
key := it.Val()
|
2019-10-31 17:27:38 +00:00
|
|
|
|
2020-12-21 17:27:12 +00:00
|
|
|
// skip bandwidth keys
|
|
|
|
if strings.HasSuffix(key, "bandwidth") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
projectID, err := uuid.FromBytes([]byte(key))
|
|
|
|
if err != nil {
|
|
|
|
return nil, accounting.ErrUnexpectedValue.New("cannot parse the key as UUID; key=%q", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, seen := projects[projectID]; seen {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := cache.getInt64(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
if accounting.ErrKeyNotFound.Has(err) {
|
|
|
|
continue
|
2020-07-03 19:34:13 +01:00
|
|
|
}
|
2020-12-21 17:27:12 +00:00
|
|
|
|
|
|
|
return nil, err
|
2019-10-31 17:27:38 +00:00
|
|
|
}
|
2020-12-21 17:27:12 +00:00
|
|
|
|
|
|
|
projects[projectID] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
return projects, nil
|
2019-10-16 17:50:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close the DB connection.
|
|
|
|
func (cache *redisLiveAccounting) Close() error {
|
2020-12-21 17:27:12 +00:00
|
|
|
err := cache.client.Close()
|
|
|
|
if err != nil {
|
|
|
|
return accounting.ErrSystemOrNetError.New("Redis close failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cache *redisLiveAccounting) getInt64(ctx context.Context, key string) (_ int64, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
val, err := cache.client.Get(key).Bytes()
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, redis.Nil) {
|
|
|
|
return 0, accounting.ErrKeyNotFound.New("%q", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, accounting.ErrSystemOrNetError.New("Redis get failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
intval, err := strconv.ParseInt(string(val), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, accounting.ErrUnexpectedValue.New("cannot parse the value as int64; key=%q val=%q", key, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return intval, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createBandwidthProjectIDKey creates the bandwidth project key.
|
|
|
|
// The current month is combined with projectID to create a prefix.
|
|
|
|
func createBandwidthProjectIDKey(projectID uuid.UUID, now time.Time) string {
|
|
|
|
// Add current month as prefix
|
|
|
|
_, month, _ := now.Date()
|
|
|
|
key := append(projectID[:], byte(int(month)))
|
|
|
|
|
|
|
|
return string(key) + ":bandwidth"
|
2019-10-16 17:50:29 +01:00
|
|
|
}
|