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"
|
2021-04-21 13:42:57 +01:00
|
|
|
"storj.io/storj/satellite/metabase"
|
2020-11-30 19:34:42 +00:00
|
|
|
"storj.io/storj/satellite/orders"
|
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 {
|
2021-10-20 23:54:34 +01:00
|
|
|
Storage float64 `json:"storage"`
|
|
|
|
Egress int64 `json:"egress"`
|
|
|
|
SegmentCount float64 `json:"segmentCount"`
|
2021-10-28 16:50:06 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-17 15:32:34 +00:00
|
|
|
// ProjectObjectsSegments consist of period total objects and segments count for certain Project.
|
|
|
|
type ProjectObjectsSegments struct {
|
|
|
|
SegmentCount int64 `json:"segmentCount"`
|
|
|
|
ObjectCount int64 `json:"objectCount"`
|
|
|
|
}
|
|
|
|
|
2021-12-03 15:06:20 +00:00
|
|
|
// ProjectLimits contains the storage, bandwidth and segments limits.
|
2020-09-09 20:20:44 +01:00
|
|
|
type ProjectLimits struct {
|
|
|
|
Usage *int64
|
|
|
|
Bandwidth *int64
|
2021-12-03 15:06:20 +00:00
|
|
|
Segments *int64
|
2020-09-09 20:20:44 +01:00
|
|
|
}
|
|
|
|
|
2021-12-07 14:41:39 +00:00
|
|
|
// ProjectDailyUsage holds project daily usage.
|
|
|
|
type ProjectDailyUsage struct {
|
2021-12-17 14:47:35 +00:00
|
|
|
StorageUsage []ProjectUsageByDay `json:"storageUsage"`
|
|
|
|
AllocatedBandwidthUsage []ProjectUsageByDay `json:"allocatedBandwidthUsage"`
|
|
|
|
SettledBandwidthUsage []ProjectUsageByDay `json:"settledBandwidthUsage"`
|
2021-12-07 14:41:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProjectUsageByDay holds project daily usage.
|
|
|
|
type ProjectUsageByDay struct {
|
|
|
|
Date time.Time `json:"date"`
|
|
|
|
Value int64 `json:"value"`
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2021-10-20 23:54:34 +01:00
|
|
|
Storage float64
|
|
|
|
Egress float64
|
2021-10-28 16:50:06 +01:00
|
|
|
ObjectCount int64
|
2021-10-20 23:54:34 +01:00
|
|
|
SegmentCount int64
|
2019-11-15 14:27:44 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-06-30 10:58:26 +01:00
|
|
|
TotalStoredData float64
|
2019-11-15 14:27:44 +00:00
|
|
|
|
2021-06-30 10:58:26 +01:00
|
|
|
TotalSegments float64
|
|
|
|
ObjectCount float64
|
|
|
|
MetadataSize float64
|
2019-11-15 14:27:44 +00:00
|
|
|
|
|
|
|
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-11-30 19:34:42 +00:00
|
|
|
// ArchiveRollupsBefore archives rollups older than a given time and returns num storagenode and bucket bandwidth rollups archived.
|
|
|
|
ArchiveRollupsBefore(ctx context.Context, before time.Time, batchSize int) (numArchivedNodeBW int, err error)
|
|
|
|
// GetRollupsSince retrieves all archived bandwidth rollup records since a given time. A hard limit batch size is used for results.
|
|
|
|
GetRollupsSince(ctx context.Context, since time.Time) ([]StoragenodeBandwidthRollup, error)
|
|
|
|
// GetArchivedRollupsSince retrieves all archived bandwidth rollup records since a given time. A hard limit batch size is used for results.
|
|
|
|
GetArchivedRollupsSince(ctx context.Context, since time.Time) ([]StoragenodeBandwidthRollup, error)
|
2019-05-10 20:05:42 +01:00
|
|
|
}
|
|
|
|
|
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)
|
2021-05-25 14:12:01 +01:00
|
|
|
// GetProjectBandwidth returns project allocated bandwidth for the specified year, month and day.
|
2021-05-25 18:43:47 +01:00
|
|
|
GetProjectBandwidth(ctx context.Context, projectID uuid.UUID, year int, month time.Month, day int, asOfSystemInterval time.Duration) (int64, error)
|
2021-05-17 15:07:59 +01:00
|
|
|
// GetProjectDailyBandwidth returns bandwidth (allocated and settled) for the specified day.
|
2021-05-29 23:16:12 +01:00
|
|
|
GetProjectDailyBandwidth(ctx context.Context, projectID uuid.UUID, year int, month time.Month, day int) (int64, int64, int64, error)
|
2021-05-25 14:12:01 +01:00
|
|
|
// DeleteProjectBandwidthBefore deletes project bandwidth rollups before the given time
|
|
|
|
DeleteProjectBandwidthBefore(ctx context.Context, before time.Time) error
|
2021-12-17 14:47:35 +00:00
|
|
|
// GetProjectDailyUsageByDateRange returns daily allocated, settled bandwidth and storage usage for the specified date range.
|
2021-12-09 15:05:21 +00:00
|
|
|
GetProjectDailyUsageByDateRange(ctx context.Context, projectID uuid.UUID, from, to time.Time, crdbInterval time.Duration) (*ProjectDailyUsage, error)
|
2020-07-07 15:48:09 +01:00
|
|
|
|
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
|
2021-12-03 15:06:20 +00:00
|
|
|
// UpdateProjectSegmentLimit updates project segment limit.
|
|
|
|
UpdateProjectSegmentLimit(ctx context.Context, projectID uuid.UUID, limit int64) 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)
|
2021-12-03 15:06:20 +00:00
|
|
|
// GetProjectSegmentLimit returns the segment limit for a project ID.
|
|
|
|
GetProjectSegmentLimit(ctx context.Context, projectID uuid.UUID) (_ *int64, err 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)
|
2021-11-17 15:32:34 +00:00
|
|
|
// GetProjectObjectsSegments returns project objects and segments for specified period of time.
|
|
|
|
GetProjectObjectsSegments(ctx context.Context, projectID uuid.UUID) (*ProjectObjectsSegments, 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)
|
2020-11-30 19:34:42 +00:00
|
|
|
// ArchiveRollupsBefore archives rollups older than a given time and returns number of bucket bandwidth rollups archived.
|
|
|
|
ArchiveRollupsBefore(ctx context.Context, before time.Time, batchSize int) (numArchivedBucketBW int, err error)
|
|
|
|
// GetRollupsSince retrieves all archived bandwidth rollup records since a given time. A hard limit batch size is used for results.
|
|
|
|
GetRollupsSince(ctx context.Context, since time.Time) ([]orders.BucketBandwidthRollup, error)
|
|
|
|
// GetArchivedRollupsSince retrieves all archived bandwidth rollup records since a given time. A hard limit batch size is used for results.
|
|
|
|
GetArchivedRollupsSince(ctx context.Context, since time.Time) ([]orders.BucketBandwidthRollup, 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 {
|
2021-11-25 12:53:52 +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)
|
2021-11-25 12:53:52 +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)
|
2021-11-25 12:53:52 +00:00
|
|
|
// GetProjectSegmentUsage returns the project's segment usage.
|
|
|
|
GetProjectSegmentUsage(ctx context.Context, projectID uuid.UUID) (currentUsed int64, err error)
|
2021-12-14 13:49:33 +00:00
|
|
|
// UpdateProjectBandwidthUsage updates the project's bandwidth usage increasing
|
2020-12-21 17:27:12 +00:00
|
|
|
// 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
|
2021-11-25 12:53:52 +00:00
|
|
|
// UpdateProjectSegmentUsage updates the project's segment 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.
|
|
|
|
UpdateProjectSegmentUsage(ctx context.Context, projectID uuid.UUID, increment int64, ttl time.Duration) 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
|
|
|
|
}
|