2019-08-12 22:43:05 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenodedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2020-07-14 14:04:38 +01:00
|
|
|
"errors"
|
2019-08-12 22:43:05 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/storj"
|
2020-01-07 23:34:51 +00:00
|
|
|
"storj.io/storj/storagenode/pieces"
|
2019-08-12 22:43:05 +01:00
|
|
|
)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
// ErrPieceSpaceUsed represents errors from the piece spaced used database.
|
|
|
|
var ErrPieceSpaceUsed = errs.Class("piece space used error")
|
|
|
|
|
2019-09-18 17:17:28 +01:00
|
|
|
// PieceSpaceUsedDBName represents the database name.
|
|
|
|
const PieceSpaceUsedDBName = "piece_spaced_used"
|
2019-08-12 22:43:05 +01:00
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
// trashTotalRowName is the special "satellite_id" used in the db to represent
|
|
|
|
// the total stored for trash. Similar to how we use NULL as a special value
|
|
|
|
// for satellite_id to represent the total for pieces, this value is used to
|
|
|
|
// identify the row storing the total for trash.
|
|
|
|
//
|
|
|
|
// It is intentionally an otherwise-invalid satellite_id (not 32 bytes) so that
|
2020-07-16 15:18:02 +01:00
|
|
|
// it cannot conflict with real satellite_id names.
|
2019-12-21 13:11:24 +00:00
|
|
|
const trashTotalRowName = "trashtotal"
|
|
|
|
|
2019-09-18 17:17:28 +01:00
|
|
|
type pieceSpaceUsedDB struct {
|
2019-11-13 16:49:22 +00:00
|
|
|
dbContainerImpl
|
2019-08-21 15:32:25 +01:00
|
|
|
}
|
2019-08-12 22:43:05 +01:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Init creates the total pieces and total trash records if they don't already exist.
|
2019-08-12 22:43:05 +01:00
|
|
|
func (db *pieceSpaceUsedDB) Init(ctx context.Context) (err error) {
|
2020-01-14 11:07:35 +00:00
|
|
|
totalPiecesRow := db.QueryRowContext(ctx, `
|
2019-08-12 22:43:05 +01:00
|
|
|
SELECT total
|
|
|
|
FROM piece_space_used
|
2019-12-21 13:11:24 +00:00
|
|
|
WHERE satellite_id IS NULL
|
|
|
|
AND satellite_id IS NOT ?;
|
|
|
|
`, trashTotalRowName)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
var piecesTotal int64
|
|
|
|
err = totalPiecesRow.Scan(&piecesTotal)
|
|
|
|
if err != nil {
|
2020-07-14 14:04:38 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2019-12-21 13:11:24 +00:00
|
|
|
err = db.createInitTotalPieces(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return ErrPieceSpaceUsed.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 11:07:35 +00:00
|
|
|
totalTrashRow := db.QueryRowContext(ctx, `
|
2019-12-21 13:11:24 +00:00
|
|
|
SELECT total
|
|
|
|
FROM piece_space_used
|
|
|
|
WHERE satellite_id = ?;
|
|
|
|
`, trashTotalRowName)
|
|
|
|
|
|
|
|
var trashTotal int64
|
|
|
|
err = totalTrashRow.Scan(&trashTotal)
|
2019-08-12 22:43:05 +01:00
|
|
|
if err != nil {
|
2020-07-14 14:04:38 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2019-12-21 13:11:24 +00:00
|
|
|
err = db.createInitTotalTrash(ctx)
|
2019-08-12 22:43:05 +01:00
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrPieceSpaceUsed.Wrap(err)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-21 13:11:24 +00:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrPieceSpaceUsed.Wrap(err)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
func (db *pieceSpaceUsedDB) createInitTotalPieces(ctx context.Context) (err error) {
|
2020-01-14 11:07:35 +00:00
|
|
|
_, err = db.ExecContext(ctx, `
|
2020-01-07 23:34:51 +00:00
|
|
|
INSERT INTO piece_space_used (total, content_size) VALUES (0, 0)
|
2019-08-12 22:43:05 +01:00
|
|
|
`)
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrPieceSpaceUsed.Wrap(err)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
func (db *pieceSpaceUsedDB) createInitTotalTrash(ctx context.Context) (err error) {
|
2020-01-14 11:07:35 +00:00
|
|
|
_, err = db.ExecContext(ctx, `
|
2020-01-07 23:34:51 +00:00
|
|
|
INSERT INTO piece_space_used (total, content_size, satellite_id) VALUES (0, 0, ?)
|
2019-12-21 13:11:24 +00:00
|
|
|
`, trashTotalRowName)
|
|
|
|
return ErrPieceSpaceUsed.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// GetPieceTotal returns the total space used (total and contentSize) for all pieces stored.
|
2020-01-07 23:34:51 +00:00
|
|
|
func (db *pieceSpaceUsedDB) GetPieceTotals(ctx context.Context) (total int64, contentSize int64, err error) {
|
2019-08-12 22:43:05 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-01-14 11:07:35 +00:00
|
|
|
row := db.QueryRowContext(ctx, `
|
2020-01-07 23:34:51 +00:00
|
|
|
SELECT total, content_size
|
2019-08-12 22:43:05 +01:00
|
|
|
FROM piece_space_used
|
|
|
|
WHERE satellite_id IS NULL;
|
|
|
|
`)
|
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
err = row.Scan(&total, &contentSize)
|
2019-08-12 22:43:05 +01:00
|
|
|
if err != nil {
|
2020-07-14 14:04:38 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2020-01-07 23:34:51 +00:00
|
|
|
return 0, 0, nil
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
2020-01-07 23:34:51 +00:00
|
|
|
return 0, 0, ErrPieceSpaceUsed.Wrap(err)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
2020-01-07 23:34:51 +00:00
|
|
|
return total, contentSize, nil
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// GetTrashTotal returns the total space used by all trash.
|
2020-01-07 23:34:51 +00:00
|
|
|
func (db *pieceSpaceUsedDB) GetTrashTotal(ctx context.Context) (total int64, err error) {
|
2019-12-21 13:11:24 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-01-14 11:07:35 +00:00
|
|
|
row := db.QueryRowContext(ctx, `
|
2019-12-21 13:11:24 +00:00
|
|
|
SELECT total
|
|
|
|
FROM piece_space_used
|
|
|
|
WHERE satellite_id = ?
|
|
|
|
`, trashTotalRowName)
|
|
|
|
|
|
|
|
err = row.Scan(&total)
|
|
|
|
if err != nil {
|
2020-07-14 14:04:38 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2019-12-21 13:11:24 +00:00
|
|
|
return total, nil
|
|
|
|
}
|
|
|
|
return total, ErrPieceSpaceUsed.Wrap(err)
|
|
|
|
}
|
|
|
|
return total, nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// GetPieceTotalsForAllSatellites returns how much space used by pieces stored for each satelliteID.
|
2020-01-07 23:34:51 +00:00
|
|
|
func (db *pieceSpaceUsedDB) GetPieceTotalsForAllSatellites(ctx context.Context) (_ map[storj.NodeID]pieces.SatelliteUsage, err error) {
|
2019-08-12 22:43:05 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
rows, err := db.QueryContext(ctx, `
|
2020-01-07 23:34:51 +00:00
|
|
|
SELECT total, content_size, satellite_id
|
2019-08-12 22:43:05 +01:00
|
|
|
FROM piece_space_used
|
|
|
|
WHERE satellite_id IS NOT NULL
|
2019-12-21 13:11:24 +00:00
|
|
|
AND satellite_id IS NOT ?
|
|
|
|
`, trashTotalRowName)
|
2019-08-12 22:43:05 +01:00
|
|
|
if err != nil {
|
2020-07-14 14:04:38 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2019-08-12 22:43:05 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrPieceSpaceUsed.Wrap(err)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
totalBySatellite := map[storj.NodeID]pieces.SatelliteUsage{}
|
2019-08-12 22:43:05 +01:00
|
|
|
for rows.Next() {
|
2020-01-07 23:34:51 +00:00
|
|
|
var total, contentSize int64
|
2019-08-12 22:43:05 +01:00
|
|
|
var satelliteID storj.NodeID
|
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
err = rows.Scan(&total, &contentSize, &satelliteID)
|
2019-08-12 22:43:05 +01:00
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrPieceSpaceUsed.Wrap(err)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
2020-01-07 23:34:51 +00:00
|
|
|
totalBySatellite[satelliteID] = pieces.SatelliteUsage{
|
|
|
|
Total: total,
|
|
|
|
ContentSize: contentSize,
|
|
|
|
}
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
2020-01-16 14:36:50 +00:00
|
|
|
return totalBySatellite, rows.Err()
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// UpdatePieceTotals updates the record for total spaced used with new total and contentSize values.
|
2020-01-07 23:34:51 +00:00
|
|
|
func (db *pieceSpaceUsedDB) UpdatePieceTotals(ctx context.Context, newTotal, newContentSize int64) (err error) {
|
2019-08-12 22:43:05 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
_, err = db.ExecContext(ctx, `
|
2019-08-12 22:43:05 +01:00
|
|
|
UPDATE piece_space_used
|
2020-01-07 23:34:51 +00:00
|
|
|
SET total = ?, content_size = ?
|
2019-08-12 22:43:05 +01:00
|
|
|
WHERE satellite_id IS NULL
|
2020-01-07 23:34:51 +00:00
|
|
|
`, newTotal, newContentSize)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrPieceSpaceUsed.Wrap(err)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// UpdateTrashTotal updates the record for total spaced used with a new value.
|
2019-12-21 13:11:24 +00:00
|
|
|
func (db *pieceSpaceUsedDB) UpdateTrashTotal(ctx context.Context, newTotal int64) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
_, err = db.ExecContext(ctx, `
|
|
|
|
UPDATE piece_space_used
|
|
|
|
SET total = ?
|
|
|
|
WHERE satellite_id = ?
|
|
|
|
`, newTotal, trashTotalRowName)
|
|
|
|
|
|
|
|
return ErrPieceSpaceUsed.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// UpdatePieceTotalsForAllSatellites updates each record with new values for each satelliteID.
|
2020-01-07 23:34:51 +00:00
|
|
|
func (db *pieceSpaceUsedDB) UpdatePieceTotalsForAllSatellites(ctx context.Context, newTotalsBySatellites map[storj.NodeID]pieces.SatelliteUsage) (err error) {
|
2019-08-12 22:43:05 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-01-07 23:34:51 +00:00
|
|
|
for satelliteID, vals := range newTotalsBySatellites {
|
|
|
|
if vals.ContentSize == 0 && vals.Total == 0 {
|
2019-08-12 22:43:05 +01:00
|
|
|
if err := db.deleteTotalBySatellite(ctx, satelliteID); err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrPieceSpaceUsed.Wrap(err)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
_, err = db.ExecContext(ctx, `
|
2020-01-07 23:34:51 +00:00
|
|
|
INSERT INTO piece_space_used (total, content_size, satellite_id)
|
|
|
|
VALUES (?, ?, ?)
|
2019-08-12 22:43:05 +01:00
|
|
|
ON CONFLICT (satellite_id)
|
2020-01-07 23:34:51 +00:00
|
|
|
DO UPDATE SET total = ?, content_size = ?
|
2019-08-12 22:43:05 +01:00
|
|
|
WHERE satellite_id = ?
|
2020-01-07 23:34:51 +00:00
|
|
|
`, vals.Total, vals.ContentSize, satelliteID, vals.Total, vals.ContentSize, satelliteID)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-14 14:04:38 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2019-08-12 22:43:05 +01:00
|
|
|
continue
|
|
|
|
}
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrPieceSpaceUsed.Wrap(err)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *pieceSpaceUsedDB) deleteTotalBySatellite(ctx context.Context, satelliteID storj.NodeID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
_, err = db.ExecContext(ctx, `
|
2019-08-12 22:43:05 +01:00
|
|
|
DELETE FROM piece_space_used
|
|
|
|
WHERE satellite_id = ?
|
|
|
|
`, satelliteID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|