2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-26 21:49:55 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
2018-12-12 21:24:08 +00:00
|
|
|
"context"
|
2018-12-14 14:27:21 +00:00
|
|
|
"time"
|
2018-12-12 21:24:08 +00:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/storj"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-03-10 20:42:11 +00:00
|
|
|
"storj.io/storj/satellite/compensation"
|
2020-08-31 11:14:20 +01:00
|
|
|
"storj.io/storj/satellite/metainfo/metabase"
|
2018-12-07 14:46:42 +00:00
|
|
|
)
|
2018-12-05 14:03:23 +00:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// RollupStats is a convenience alias.
|
2019-01-16 19:30:33 +00:00
|
|
|
type RollupStats map[time.Time]map[storj.NodeID]*Rollup
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// StoragenodeStorageTally mirrors dbx.StoragenodeStorageTally, allowing us to use that struct without leaking dbx.
|
2019-05-10 20:05:42 +01:00
|
|
|
type StoragenodeStorageTally struct {
|
2019-01-16 19:30:33 +00:00
|
|
|
ID int64
|
|
|
|
NodeID storj.NodeID
|
|
|
|
IntervalEndTime time.Time
|
|
|
|
DataTotal float64
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// StoragenodeBandwidthRollup mirrors dbx.StoragenodeBandwidthRollup, allowing us to use the struct without leaking dbx.
|
2019-04-04 16:20:59 +01:00
|
|
|
type StoragenodeBandwidthRollup struct {
|
|
|
|
NodeID storj.NodeID
|
|
|
|
IntervalStart time.Time
|
|
|
|
Action uint
|
|
|
|
Settled uint64
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Rollup mirrors dbx.AccountingRollup, allowing us to use that struct without leaking dbx.
|
2019-01-16 19:30:33 +00:00
|
|
|
type Rollup struct {
|
|
|
|
ID int64
|
|
|
|
NodeID storj.NodeID
|
|
|
|
StartTime time.Time
|
|
|
|
PutTotal int64
|
|
|
|
GetTotal int64
|
|
|
|
GetAuditTotal int64
|
|
|
|
GetRepairTotal int64
|
|
|
|
PutRepairTotal int64
|
|
|
|
AtRestTotal float64
|
|
|
|
}
|
2019-01-10 11:41:57 +00:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// StorageNodePeriodUsage represents a statement for a node for a compensation period.
|
2020-03-10 20:42:11 +00:00
|
|
|
type StorageNodePeriodUsage struct {
|
|
|
|
NodeID storj.NodeID
|
|
|
|
AtRestTotal float64
|
|
|
|
GetTotal int64
|
|
|
|
PutTotal int64
|
|
|
|
GetRepairTotal int64
|
|
|
|
PutRepairTotal int64
|
|
|
|
GetAuditTotal int64
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// StorageNodeUsage is node at rest space usage over a period of time.
|
2019-08-08 14:47:04 +01:00
|
|
|
type StorageNodeUsage struct {
|
2019-07-02 11:42:09 +01:00
|
|
|
NodeID storj.NodeID
|
2019-08-08 14:47:04 +01:00
|
|
|
StorageUsed float64
|
2019-07-02 11:42:09 +01:00
|
|
|
|
2019-08-08 14:47:04 +01:00
|
|
|
Timestamp time.Time
|
2019-07-02 11:42:09 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// ProjectUsage consist of period total storage, egress
|
2020-07-16 15:18:02 +01:00
|
|
|
// and objects count per hour for certain Project in bytes.
|
2019-11-15 14:27:44 +00:00
|
|
|
type ProjectUsage struct {
|
2020-03-04 13:23:10 +00:00
|
|
|
Storage float64 `json:"storage"`
|
|
|
|
Egress int64 `json:"egress"`
|
|
|
|
ObjectCount float64 `json:"objectCount"`
|
2019-11-15 14:27:44 +00:00
|
|
|
|
2020-03-04 13:23:10 +00:00
|
|
|
Since time.Time `json:"since"`
|
|
|
|
Before time.Time `json:"before"`
|
2019-11-15 14:27:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 20:20:44 +01:00
|
|
|
// ProjectLimits contains the storage and bandwidth limits.
|
|
|
|
type ProjectLimits struct {
|
|
|
|
Usage *int64
|
|
|
|
Bandwidth *int64
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// BucketUsage consist of total bucket usage for period.
|
2019-11-15 14:27:44 +00:00
|
|
|
type BucketUsage struct {
|
|
|
|
ProjectID uuid.UUID
|
|
|
|
BucketName string
|
|
|
|
|
|
|
|
Storage float64
|
|
|
|
Egress float64
|
|
|
|
ObjectCount int64
|
|
|
|
|
|
|
|
Since time.Time
|
|
|
|
Before time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// BucketUsageCursor holds info for bucket usage
|
2020-07-16 15:18:02 +01:00
|
|
|
// cursor pagination.
|
2019-11-15 14:27:44 +00:00
|
|
|
type BucketUsageCursor struct {
|
|
|
|
Search string
|
|
|
|
Limit uint
|
|
|
|
Page uint
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// BucketUsagePage represents bucket usage page result.
|
2019-11-15 14:27:44 +00:00
|
|
|
type BucketUsagePage struct {
|
|
|
|
BucketUsages []BucketUsage
|
|
|
|
|
|
|
|
Search string
|
|
|
|
Limit uint
|
|
|
|
Offset uint64
|
|
|
|
|
|
|
|
PageCount uint
|
|
|
|
CurrentPage uint
|
|
|
|
TotalCount uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// BucketUsageRollup is total bucket usage info
|
2020-07-16 15:18:02 +01:00
|
|
|
// for certain period.
|
2019-11-15 14:27:44 +00:00
|
|
|
type BucketUsageRollup struct {
|
|
|
|
ProjectID uuid.UUID
|
|
|
|
BucketName []byte
|
|
|
|
|
|
|
|
RemoteStoredData float64
|
|
|
|
InlineStoredData float64
|
|
|
|
|
|
|
|
RemoteSegments float64
|
|
|
|
InlineSegments float64
|
|
|
|
ObjectCount float64
|
|
|
|
MetadataSize float64
|
|
|
|
|
|
|
|
RepairEgress float64
|
|
|
|
GetEgress float64
|
|
|
|
AuditEgress float64
|
|
|
|
|
|
|
|
Since time.Time
|
|
|
|
Before time.Time
|
|
|
|
}
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// StoragenodeAccounting stores information about bandwidth and storage usage for storage nodes.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Database
|
2019-05-10 20:05:42 +01:00
|
|
|
type StoragenodeAccounting interface {
|
|
|
|
// SaveTallies records tallies of data at rest
|
|
|
|
SaveTallies(ctx context.Context, latestTally time.Time, nodeData map[storj.NodeID]float64) error
|
|
|
|
// GetTallies retrieves all tallies
|
|
|
|
GetTallies(ctx context.Context) ([]*StoragenodeStorageTally, error)
|
|
|
|
// GetTalliesSince retrieves all tallies since latestRollup
|
|
|
|
GetTalliesSince(ctx context.Context, latestRollup time.Time) ([]*StoragenodeStorageTally, error)
|
|
|
|
// GetBandwidthSince retrieves all bandwidth rollup entires since latestRollup
|
2020-11-29 16:13:06 +00:00
|
|
|
GetBandwidthSince(ctx context.Context, latestRollup time.Time, cb func(context.Context, *StoragenodeBandwidthRollup) error) error
|
2019-05-10 20:05:42 +01:00
|
|
|
// SaveRollup records tally and bandwidth rollup aggregations to the database
|
2019-02-01 18:50:12 +00:00
|
|
|
SaveRollup(ctx context.Context, latestTally time.Time, stats RollupStats) error
|
2019-05-10 20:05:42 +01:00
|
|
|
// LastTimestamp records and returns the latest last tallied time.
|
|
|
|
LastTimestamp(ctx context.Context, timestampType string) (time.Time, error)
|
|
|
|
// QueryPaymentInfo queries Nodes and Accounting_Rollup on nodeID
|
2019-01-17 18:34:13 +00:00
|
|
|
QueryPaymentInfo(ctx context.Context, start time.Time, end time.Time) ([]*CSVRow, error)
|
2020-03-10 20:42:11 +00:00
|
|
|
// QueryStorageNodePeriodUsage returns accounting statements for nodes for a given compensation period
|
|
|
|
QueryStorageNodePeriodUsage(ctx context.Context, period compensation.Period) ([]StorageNodePeriodUsage, error)
|
2019-08-08 14:47:04 +01:00
|
|
|
// QueryStorageNodeUsage returns slice of StorageNodeUsage for given period
|
|
|
|
QueryStorageNodeUsage(ctx context.Context, nodeID storj.NodeID, start time.Time, end time.Time) ([]StorageNodeUsage, error)
|
2019-05-10 20:05:42 +01:00
|
|
|
// DeleteTalliesBefore deletes all tallies prior to some time
|
|
|
|
DeleteTalliesBefore(ctx context.Context, latestRollup time.Time) error
|
|
|
|
}
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// ProjectAccounting stores information about bandwidth and storage usage for projects.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Database
|
2019-05-10 20:05:42 +01:00
|
|
|
type ProjectAccounting interface {
|
|
|
|
// SaveTallies saves the latest project info
|
2020-08-31 11:14:20 +01:00
|
|
|
SaveTallies(ctx context.Context, intervalStart time.Time, bucketTallies map[metabase.BucketLocation]*BucketTally) error
|
2019-09-12 18:31:50 +01:00
|
|
|
// GetTallies retrieves all tallies
|
|
|
|
GetTallies(ctx context.Context) ([]BucketTally, error)
|
2019-05-10 20:05:42 +01:00
|
|
|
// CreateStorageTally creates a record for BucketStorageTally in the accounting DB table
|
|
|
|
CreateStorageTally(ctx context.Context, tally BucketStorageTally) error
|
|
|
|
// GetAllocatedBandwidthTotal returns the sum of GET bandwidth usage allocated for a projectID in the past time frame
|
2019-06-25 16:58:42 +01:00
|
|
|
GetAllocatedBandwidthTotal(ctx context.Context, projectID uuid.UUID, from time.Time) (int64, error)
|
2020-05-04 19:05:08 +01:00
|
|
|
// GetProjectAllocatedBandwidth returns project allocated bandwidth for the specified year and month.
|
|
|
|
GetProjectAllocatedBandwidth(ctx context.Context, projectID uuid.UUID, year int, month time.Month) (int64, error)
|
2020-07-07 15:48:09 +01:00
|
|
|
// DeleteProjectAllocatedBandwidthBefore deletes project bandwidth rollups before the given time
|
|
|
|
DeleteProjectAllocatedBandwidthBefore(ctx context.Context, before time.Time) error
|
|
|
|
|
2019-05-10 20:05:42 +01:00
|
|
|
// GetStorageTotals returns the current inline and remote storage usage for a projectID
|
|
|
|
GetStorageTotals(ctx context.Context, projectID uuid.UUID) (int64, int64, error)
|
2019-12-10 16:12:49 +00:00
|
|
|
// UpdateProjectUsageLimit updates project usage limit.
|
|
|
|
UpdateProjectUsageLimit(ctx context.Context, projectID uuid.UUID, limit memory.Size) error
|
2020-05-12 14:01:15 +01:00
|
|
|
// UpdateProjectBandwidthLimit updates project bandwidth limit.
|
|
|
|
UpdateProjectBandwidthLimit(ctx context.Context, projectID uuid.UUID, limit memory.Size) error
|
2019-11-25 14:18:04 +00:00
|
|
|
// GetProjectStorageLimit returns project storage usage limit.
|
2020-09-06 00:02:12 +01:00
|
|
|
GetProjectStorageLimit(ctx context.Context, projectID uuid.UUID) (*int64, error)
|
2019-11-25 14:18:04 +00:00
|
|
|
// GetProjectBandwidthLimit returns project bandwidth usage limit.
|
2020-09-06 00:02:12 +01:00
|
|
|
GetProjectBandwidthLimit(ctx context.Context, projectID uuid.UUID) (*int64, error)
|
2020-09-09 20:20:44 +01:00
|
|
|
// GetProjectLimits returns current project limit for both storage and bandwidth.
|
|
|
|
GetProjectLimits(ctx context.Context, projectID uuid.UUID) (ProjectLimits, error)
|
2019-11-25 14:18:04 +00:00
|
|
|
// GetProjectTotal returns project usage summary for specified period of time.
|
2019-11-15 14:27:44 +00:00
|
|
|
GetProjectTotal(ctx context.Context, projectID uuid.UUID, since, before time.Time) (*ProjectUsage, error)
|
2019-11-25 14:18:04 +00:00
|
|
|
// GetBucketUsageRollups returns usage rollup per each bucket for specified period of time.
|
2019-11-15 14:27:44 +00:00
|
|
|
GetBucketUsageRollups(ctx context.Context, projectID uuid.UUID, since, before time.Time) ([]BucketUsageRollup, error)
|
2019-11-25 14:18:04 +00:00
|
|
|
// GetBucketTotals returns per bucket usage summary for specified period of time.
|
2019-11-15 14:27:44 +00:00
|
|
|
GetBucketTotals(ctx context.Context, projectID uuid.UUID, cursor BucketUsageCursor, since, before time.Time) (*BucketUsagePage, error)
|
2018-11-26 21:49:55 +00:00
|
|
|
}
|
2019-10-16 17:50:29 +01:00
|
|
|
|
|
|
|
// Cache stores live information about project storage which has not yet been synced to ProjectAccounting.
|
|
|
|
//
|
2020-12-21 17:27:12 +00:00
|
|
|
// All the implementations must follow the convention of returning errors of one
|
|
|
|
// of the classes defined in this package.
|
|
|
|
//
|
|
|
|
// All the methods return:
|
|
|
|
//
|
|
|
|
// ErrInvalidArgument: an implementation may return if some parameter contain a
|
|
|
|
// value which isn't accepted, nonetheless, not all the implementations impose
|
|
|
|
// the same constraints on them.
|
|
|
|
//
|
|
|
|
// ErrSystemOrNetError: any method will return this if there is an error with
|
|
|
|
// the underlining system or the network.
|
|
|
|
//
|
|
|
|
// ErrKeyNotFound: returned when a key is not found.
|
|
|
|
//
|
|
|
|
// ErrUnexpectedValue: returned when a key or value stored in the underlying
|
|
|
|
// system isn't of the expected format or type according the business domain.
|
|
|
|
//
|
2019-10-16 17:50:29 +01:00
|
|
|
// architecture: Database
|
|
|
|
type Cache interface {
|
2020-12-21 17:27:12 +00:00
|
|
|
// GetProjectStorageUsage returns the project's storage usage.
|
2019-10-16 17:50:29 +01:00
|
|
|
GetProjectStorageUsage(ctx context.Context, projectID uuid.UUID) (totalUsed int64, err error)
|
2020-12-21 17:27:12 +00:00
|
|
|
// GetProjectBandwidthUsage returns the project's bandwidth usage.
|
2020-06-02 00:19:10 +01:00
|
|
|
GetProjectBandwidthUsage(ctx context.Context, projectID uuid.UUID, now time.Time) (currentUsed int64, err error)
|
2020-12-21 17:27:12 +00:00
|
|
|
// UpdateProjectBandthUsage updates the project's bandwidth usage increasing
|
|
|
|
// it. The projectID is inserted to the increment when it doesn't exists,
|
|
|
|
// hence this method will never return ErrKeyNotFound error's class.
|
2020-06-02 00:19:10 +01:00
|
|
|
UpdateProjectBandwidthUsage(ctx context.Context, projectID uuid.UUID, increment int64, ttl time.Duration, now time.Time) error
|
2020-12-21 17:27:12 +00:00
|
|
|
// AddProjectStorageUsage adds to the projects storage usage the spacedUsed.
|
|
|
|
// The projectID is inserted to the spaceUsed when it doesn't exists, hence
|
|
|
|
// this method will never return ErrKeyNotFound.
|
2019-10-31 17:27:38 +00:00
|
|
|
AddProjectStorageUsage(ctx context.Context, projectID uuid.UUID, spaceUsed int64) error
|
2020-12-21 17:27:12 +00:00
|
|
|
// GetAllProjectTotals return the total projects' storage used space.
|
2019-10-31 17:27:38 +00:00
|
|
|
GetAllProjectTotals(ctx context.Context) (map[uuid.UUID]int64, error)
|
2020-12-21 17:27:12 +00:00
|
|
|
// Close the client, releasing any open resources. Once it's called any other
|
|
|
|
// method must be called.
|
2019-10-16 17:50:29 +01:00
|
|
|
Close() error
|
|
|
|
}
|