2019-08-08 02:47:30 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenodedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/storj"
|
2019-08-08 02:47:30 +01:00
|
|
|
"storj.io/storj/storagenode/pieces"
|
|
|
|
)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
// ErrPieceExpiration represents errors from the piece expiration database.
|
2021-04-28 09:06:17 +01:00
|
|
|
var ErrPieceExpiration = errs.Class("pieceexpirationdb")
|
2019-08-21 15:32:25 +01:00
|
|
|
|
2019-09-18 17:17:28 +01:00
|
|
|
// PieceExpirationDBName represents the database filename.
|
|
|
|
const PieceExpirationDBName = "piece_expiration"
|
2019-08-08 02:47:30 +01:00
|
|
|
|
2019-09-18 17:17:28 +01:00
|
|
|
type pieceExpirationDB struct {
|
2019-11-13 16:49:22 +00:00
|
|
|
dbContainerImpl
|
2019-08-21 15:32:25 +01:00
|
|
|
}
|
2019-08-08 02:47:30 +01:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// GetExpired gets piece IDs that expire or have expired before the given time.
|
2019-08-08 02:47:30 +01:00
|
|
|
func (db *pieceExpirationDB) GetExpired(ctx context.Context, expiresBefore time.Time, limit int64) (expiredPieceIDs []pieces.ExpiredInfo, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
rows, err := db.QueryContext(ctx, `
|
2019-08-08 02:47:30 +01:00
|
|
|
SELECT satellite_id, piece_id
|
|
|
|
FROM piece_expirations
|
|
|
|
WHERE piece_expiration < ?
|
|
|
|
AND ((deletion_failed_at IS NULL) OR deletion_failed_at <> ?)
|
2019-11-20 16:28:49 +00:00
|
|
|
AND trash = 0
|
2019-08-08 02:47:30 +01:00
|
|
|
LIMIT ?
|
|
|
|
`, expiresBefore.UTC(), expiresBefore.UTC(), limit)
|
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrPieceExpiration.Wrap(err)
|
2019-08-08 02:47:30 +01:00
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var satelliteID storj.NodeID
|
|
|
|
var pieceID storj.PieceID
|
|
|
|
err = rows.Scan(&satelliteID, &pieceID)
|
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrPieceExpiration.Wrap(err)
|
2019-08-08 02:47:30 +01:00
|
|
|
}
|
|
|
|
expiredPieceIDs = append(expiredPieceIDs, pieces.ExpiredInfo{
|
|
|
|
SatelliteID: satelliteID,
|
|
|
|
PieceID: pieceID,
|
|
|
|
InPieceInfo: false,
|
|
|
|
})
|
|
|
|
}
|
2020-01-16 14:36:50 +00:00
|
|
|
return expiredPieceIDs, rows.Err()
|
2019-08-08 02:47:30 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// SetExpiration sets an expiration time for the given piece ID on the given satellite.
|
2019-08-08 02:47:30 +01:00
|
|
|
func (db *pieceExpirationDB) SetExpiration(ctx context.Context, satellite storj.NodeID, pieceID storj.PieceID, expiresAt time.Time) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
_, err = db.ExecContext(ctx, `
|
2019-08-08 02:47:30 +01:00
|
|
|
INSERT INTO piece_expirations(satellite_id, piece_id, piece_expiration)
|
|
|
|
VALUES (?,?,?)
|
|
|
|
`, satellite, pieceID, expiresAt.UTC())
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrPieceExpiration.Wrap(err)
|
2019-08-08 02:47:30 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// DeleteExpiration removes an expiration record for the given piece ID on the given satellite.
|
2019-08-08 02:47:30 +01:00
|
|
|
func (db *pieceExpirationDB) DeleteExpiration(ctx context.Context, satelliteID storj.NodeID, pieceID storj.PieceID) (found bool, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
result, err := db.ExecContext(ctx, `
|
2019-08-08 02:47:30 +01:00
|
|
|
DELETE FROM piece_expirations
|
|
|
|
WHERE satellite_id = ? AND piece_id = ?
|
|
|
|
`, satelliteID, pieceID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
numRows, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return numRows > 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFailed marks an expiration record as having experienced a failure in deleting the piece
|
2020-07-16 15:18:02 +01:00
|
|
|
// from the disk.
|
2019-08-08 02:47:30 +01:00
|
|
|
func (db *pieceExpirationDB) DeleteFailed(ctx context.Context, satelliteID storj.NodeID, pieceID storj.PieceID, when time.Time) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
_, err = db.ExecContext(ctx, `
|
2019-08-08 02:47:30 +01:00
|
|
|
UPDATE piece_expirations
|
|
|
|
SET deletion_failed_at = ?
|
|
|
|
WHERE satellite_id = ?
|
|
|
|
AND piece_id = ?
|
|
|
|
`, when.UTC(), satelliteID, pieceID)
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrPieceExpiration.Wrap(err)
|
2019-08-08 02:47:30 +01:00
|
|
|
}
|
2019-11-20 16:28:49 +00:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Trash marks a piece expiration as "trashed".
|
2019-11-20 16:28:49 +00:00
|
|
|
func (db *pieceExpirationDB) Trash(ctx context.Context, satelliteID storj.NodeID, pieceID storj.PieceID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
_, err = db.ExecContext(ctx, `
|
|
|
|
UPDATE piece_expirations
|
|
|
|
SET trash = 1
|
|
|
|
WHERE satellite_id = ?
|
|
|
|
AND piece_id = ?
|
|
|
|
`, satelliteID, pieceID)
|
|
|
|
return ErrPieceExpiration.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Restore restores all trashed pieces.
|
2019-11-20 16:28:49 +00:00
|
|
|
func (db *pieceExpirationDB) RestoreTrash(ctx context.Context, satelliteID storj.NodeID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
_, err = db.ExecContext(ctx, `
|
|
|
|
UPDATE piece_expirations
|
|
|
|
SET trash = 0
|
|
|
|
WHERE satellite_id = ?
|
|
|
|
AND trash = 1
|
|
|
|
`, satelliteID)
|
|
|
|
return ErrPieceExpiration.Wrap(err)
|
|
|
|
}
|