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"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
)
|
|
|
|
|
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-08-12 22:43:05 +01:00
|
|
|
type pieceSpaceUsedDB struct {
|
2019-08-21 15:32:25 +01:00
|
|
|
location string
|
|
|
|
SQLDB
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
// newPieceSpaceUsedDB returns a new instance of pieceSpaceUsedDB initialized with the specified database.
|
|
|
|
func newPieceSpaceUsedDB(db SQLDB, location string) *pieceSpaceUsedDB {
|
|
|
|
return &pieceSpaceUsedDB{
|
|
|
|
location: location,
|
|
|
|
SQLDB: db,
|
|
|
|
}
|
|
|
|
}
|
2019-08-12 22:43:05 +01:00
|
|
|
|
|
|
|
// Init creates the one total record if it doesn't already exist
|
|
|
|
func (db *pieceSpaceUsedDB) Init(ctx context.Context) (err error) {
|
2019-08-21 15:32:25 +01:00
|
|
|
row := db.QueryRow(`
|
2019-08-12 22:43:05 +01:00
|
|
|
SELECT total
|
|
|
|
FROM piece_space_used
|
|
|
|
WHERE satellite_id IS NULL;
|
|
|
|
`)
|
|
|
|
|
|
|
|
var total int64
|
|
|
|
err = row.Scan(&total)
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = db.createInitTotal(ctx)
|
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrPieceSpaceUsed.Wrap(err)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (db *pieceSpaceUsedDB) createInitTotal(ctx context.Context) (err error) {
|
2019-08-21 15:32:25 +01:00
|
|
|
_, err = db.Exec(`
|
2019-08-12 22:43:05 +01:00
|
|
|
INSERT INTO piece_space_used (total) VALUES (0)
|
|
|
|
`)
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrPieceSpaceUsed.Wrap(err)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTotal returns the total space used by all pieces stored on disk
|
|
|
|
func (db *pieceSpaceUsedDB) GetTotal(ctx context.Context) (_ int64, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
row := db.QueryRow(`
|
2019-08-12 22:43:05 +01:00
|
|
|
SELECT total
|
|
|
|
FROM piece_space_used
|
|
|
|
WHERE satellite_id IS NULL;
|
|
|
|
`)
|
|
|
|
|
|
|
|
var total int64
|
|
|
|
err = row.Scan(&total)
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return total, nil
|
|
|
|
}
|
2019-08-21 15:32:25 +01:00
|
|
|
return total, ErrPieceSpaceUsed.Wrap(err)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
return total, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTotalsForAllSatellites returns how much total space used by pieces stored on disk for each satelliteID
|
|
|
|
func (db *pieceSpaceUsedDB) GetTotalsForAllSatellites(ctx context.Context) (_ map[storj.NodeID]int64, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
rows, err := db.QueryContext(ctx, `
|
2019-08-12 22:43:05 +01:00
|
|
|
SELECT total, satellite_id
|
|
|
|
FROM piece_space_used
|
|
|
|
WHERE satellite_id IS NOT NULL
|
|
|
|
`)
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
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()) }()
|
|
|
|
|
|
|
|
totalBySatellite := map[storj.NodeID]int64{}
|
|
|
|
for rows.Next() {
|
|
|
|
var total int64
|
|
|
|
var satelliteID storj.NodeID
|
|
|
|
|
|
|
|
err = rows.Scan(&total, &satelliteID)
|
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrPieceSpaceUsed.Wrap(err)
|
2019-08-12 22:43:05 +01:00
|
|
|
}
|
|
|
|
totalBySatellite[satelliteID] = total
|
|
|
|
}
|
|
|
|
return totalBySatellite, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTotal updates the record for total spaced used with a new value
|
|
|
|
func (db *pieceSpaceUsedDB) UpdateTotal(ctx context.Context, newTotal int64) (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
|
|
|
UPDATE piece_space_used
|
|
|
|
SET total = ?
|
|
|
|
WHERE satellite_id IS NULL
|
2019-08-21 15:32:25 +01:00
|
|
|
`, newTotal)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTotalsForAllSatellites updates each record for total spaced used with a new value for each satelliteID
|
|
|
|
func (db *pieceSpaceUsedDB) UpdateTotalsForAllSatellites(ctx context.Context, newTotalsBySatellites map[storj.NodeID]int64) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
for satelliteID, newTotal := range newTotalsBySatellites {
|
|
|
|
if newTotal == 0 {
|
|
|
|
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, `
|
2019-08-12 22:43:05 +01:00
|
|
|
INSERT INTO piece_space_used (total, satellite_id)
|
|
|
|
VALUES (?, ?)
|
|
|
|
ON CONFLICT (satellite_id)
|
|
|
|
DO UPDATE SET total = ?
|
|
|
|
WHERE satellite_id = ?
|
2019-08-21 15:32:25 +01:00
|
|
|
`, newTotal, satelliteID, newTotal, satelliteID)
|
2019-08-12 22:43:05 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
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
|
|
|
|
}
|