2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenodedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-08-08 02:47:30 +01:00
|
|
|
"os"
|
2019-04-08 17:46:38 +01:00
|
|
|
"time"
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-04-08 17:46:38 +01:00
|
|
|
"github.com/zeebo/errs"
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
2019-08-08 02:47:30 +01:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
"storj.io/storj/storage/filestore"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/storagenode/pieces"
|
|
|
|
)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
// ErrPieceInfo represents errors from the piece info database.
|
|
|
|
var ErrPieceInfo = errs.Class("v0pieceinfodb error")
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-09-18 17:17:28 +01:00
|
|
|
// PieceInfoDBName represents the database name.
|
|
|
|
const PieceInfoDBName = "pieceinfo"
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-09-18 17:17:28 +01:00
|
|
|
type v0PieceInfoDB struct {
|
2019-11-13 16:49:22 +00:00
|
|
|
dbContainerImpl
|
2019-08-21 15:32:25 +01:00
|
|
|
}
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
// Add inserts piece information into the database.
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *v0PieceInfoDB) Add(ctx context.Context, info *pieces.Info) (err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-07-09 01:33:50 +01:00
|
|
|
|
2020-04-08 13:08:57 +01:00
|
|
|
orderLimit, err := pb.Marshal(info.OrderLimit)
|
2019-07-11 21:51:40 +01:00
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrPieceInfo.Wrap(err)
|
2019-07-11 21:51:40 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 13:08:57 +01:00
|
|
|
uplinkPieceHash, err := pb.Marshal(info.UplinkPieceHash)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrPieceInfo.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 18:38:08 +01:00
|
|
|
var pieceExpiration *time.Time
|
|
|
|
if !info.PieceExpiration.IsZero() {
|
|
|
|
utcExpiration := info.PieceExpiration.UTC()
|
|
|
|
pieceExpiration = &utcExpiration
|
|
|
|
}
|
|
|
|
|
2019-07-09 22:33:45 +01:00
|
|
|
// TODO remove `uplink_cert_id` from DB
|
2019-08-21 15:32:25 +01:00
|
|
|
_, err = db.ExecContext(ctx, `
|
2019-03-18 10:55:06 +00:00
|
|
|
INSERT INTO
|
2019-07-16 17:31:29 +01:00
|
|
|
pieceinfo_(satellite_id, piece_id, piece_size, piece_creation, piece_expiration, order_limit, uplink_piece_hash, uplink_cert_id)
|
2019-07-11 21:51:40 +01:00
|
|
|
VALUES (?,?,?,?,?,?,?,?)
|
2019-08-21 15:32:25 +01:00
|
|
|
`, info.SatelliteID, info.PieceID, info.PieceSize, info.PieceCreation.UTC(), pieceExpiration, orderLimit, uplinkPieceHash, 0)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrPieceInfo.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *v0PieceInfoDB) getAllPiecesOwnedBy(ctx context.Context, blobStore storage.Blobs, satelliteID storj.NodeID) ([]v0StoredPieceAccess, error) {
|
|
|
|
rows, err := db.QueryContext(ctx, `
|
2019-08-08 02:47:30 +01:00
|
|
|
SELECT piece_id, piece_size, piece_creation, piece_expiration
|
2019-07-16 17:31:29 +01:00
|
|
|
FROM pieceinfo_
|
2019-08-08 02:47:30 +01:00
|
|
|
WHERE satellite_id = ?
|
2019-07-10 14:41:47 +01:00
|
|
|
ORDER BY piece_id
|
2019-08-21 15:32:25 +01:00
|
|
|
`, satelliteID)
|
2019-07-10 14:41:47 +01:00
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrPieceInfo.Wrap(err)
|
2019-07-10 14:41:47 +01:00
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
2019-08-08 02:47:30 +01:00
|
|
|
var pieceInfos []v0StoredPieceAccess
|
2019-07-10 14:41:47 +01:00
|
|
|
for rows.Next() {
|
2019-08-08 02:47:30 +01:00
|
|
|
pieceInfos = append(pieceInfos, v0StoredPieceAccess{
|
|
|
|
blobStore: blobStore,
|
|
|
|
satellite: satelliteID,
|
|
|
|
})
|
|
|
|
thisAccess := &pieceInfos[len(pieceInfos)-1]
|
|
|
|
err = rows.Scan(&thisAccess.pieceID, &thisAccess.pieceSize, &thisAccess.creationTime, &thisAccess.expirationTime)
|
2019-07-10 14:41:47 +01:00
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrPieceInfo.Wrap(err)
|
2019-07-10 14:41:47 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-16 14:36:50 +00:00
|
|
|
return pieceInfos, rows.Err()
|
2019-08-08 02:47:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// WalkSatelliteV0Pieces executes walkFunc for each locally stored piece, stored with storage
|
|
|
|
// format V0 in the namespace of the given satellite. If walkFunc returns a non-nil error,
|
|
|
|
// WalkSatelliteV0Pieces will stop iterating and return the error immediately. The ctx parameter
|
|
|
|
// parameter is intended specifically to allow canceling iteration early.
|
|
|
|
//
|
|
|
|
// If blobStore is nil, the .Stat() and .FullPath() methods of the provided StoredPieceAccess
|
|
|
|
// instances will not work, but otherwise everything should be ok.
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *v0PieceInfoDB) WalkSatelliteV0Pieces(ctx context.Context, blobStore storage.Blobs, satelliteID storj.NodeID, walkFunc func(pieces.StoredPieceAccess) error) (err error) {
|
2019-08-08 02:47:30 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
// TODO: is it worth paging this query? we hope that SNs will not yet have too many V0 pieces.
|
|
|
|
pieceInfos, err := db.getAllPiecesOwnedBy(ctx, blobStore, satelliteID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// note we must not keep a transaction open with the db when calling walkFunc; the callback
|
|
|
|
// might need to make db calls as well
|
|
|
|
for i := range pieceInfos {
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := walkFunc(&pieceInfos[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2019-07-10 14:41:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// Get gets piece information by satellite id and piece id.
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *v0PieceInfoDB) Get(ctx context.Context, satelliteID storj.NodeID, pieceID storj.PieceID) (_ *pieces.Info, err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-18 10:55:06 +00:00
|
|
|
info := &pieces.Info{}
|
|
|
|
info.SatelliteID = satelliteID
|
|
|
|
info.PieceID = pieceID
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
var orderLimit []byte
|
2019-03-18 10:55:06 +00:00
|
|
|
var uplinkPieceHash []byte
|
2019-07-24 18:26:43 +01:00
|
|
|
var pieceExpiration *time.Time
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
err = db.QueryRowContext(ctx, `
|
2019-07-11 21:51:40 +01:00
|
|
|
SELECT piece_size, piece_creation, piece_expiration, order_limit, uplink_piece_hash
|
2019-07-16 17:31:29 +01:00
|
|
|
FROM pieceinfo_
|
2019-03-18 10:55:06 +00:00
|
|
|
WHERE satellite_id = ? AND piece_id = ?
|
2019-08-21 15:32:25 +01:00
|
|
|
`, satelliteID, pieceID).Scan(&info.PieceSize, &info.PieceCreation, &pieceExpiration, &orderLimit, &uplinkPieceHash)
|
2019-07-11 21:51:40 +01:00
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrPieceInfo.Wrap(err)
|
2019-07-11 21:51:40 +01:00
|
|
|
}
|
|
|
|
|
2019-07-24 18:26:43 +01:00
|
|
|
if pieceExpiration != nil {
|
|
|
|
info.PieceExpiration = *pieceExpiration
|
|
|
|
}
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
info.OrderLimit = &pb.OrderLimit{}
|
2020-04-08 13:08:57 +01:00
|
|
|
err = pb.Unmarshal(orderLimit, info.OrderLimit)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrPieceInfo.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info.UplinkPieceHash = &pb.PieceHash{}
|
2020-04-08 13:08:57 +01:00
|
|
|
err = pb.Unmarshal(uplinkPieceHash, info.UplinkPieceHash)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrPieceInfo.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes piece information.
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *v0PieceInfoDB) Delete(ctx context.Context, satelliteID storj.NodeID, pieceID storj.PieceID) (err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
_, err = db.ExecContext(ctx, `
|
2019-07-16 17:31:29 +01:00
|
|
|
DELETE FROM pieceinfo_
|
2019-06-04 13:31:39 +01:00
|
|
|
WHERE satellite_id = ?
|
2019-05-08 12:11:59 +01:00
|
|
|
AND piece_id = ?
|
2019-08-21 15:32:25 +01:00
|
|
|
`, satelliteID, pieceID)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrPieceInfo.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 12:11:59 +01:00
|
|
|
// DeleteFailed marks piece as a failed deletion.
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *v0PieceInfoDB) DeleteFailed(ctx context.Context, satelliteID storj.NodeID, pieceID storj.PieceID, now time.Time) (err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
_, err = db.ExecContext(ctx, `
|
2019-07-16 17:31:29 +01:00
|
|
|
UPDATE pieceinfo_
|
2019-05-08 12:11:59 +01:00
|
|
|
SET deletion_failed_at = ?
|
2019-06-04 13:31:39 +01:00
|
|
|
WHERE satellite_id = ?
|
2019-05-08 12:11:59 +01:00
|
|
|
AND piece_id = ?
|
2019-08-21 15:32:25 +01:00
|
|
|
`, now.UTC(), satelliteID, pieceID)
|
2019-05-08 12:11:59 +01:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrPieceInfo.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
2019-04-08 17:46:38 +01:00
|
|
|
|
2019-08-08 02:47:30 +01:00
|
|
|
// GetExpired gets ExpiredInfo records for pieces that are expired.
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *v0PieceInfoDB) GetExpired(ctx context.Context, expiredAt time.Time, limit int64) (infos []pieces.ExpiredInfo, err error) {
|
2019-06-04 13:31:39 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-04-08 17:46:38 +01:00
|
|
|
|
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
|
2019-07-16 17:31:29 +01:00
|
|
|
FROM pieceinfo_
|
2019-07-12 20:29:09 +01:00
|
|
|
WHERE piece_expiration IS NOT NULL
|
2019-07-15 18:38:08 +01:00
|
|
|
AND piece_expiration < ?
|
|
|
|
AND ((deletion_failed_at IS NULL) OR deletion_failed_at <> ?)
|
2019-05-08 12:11:59 +01:00
|
|
|
ORDER BY satellite_id
|
|
|
|
LIMIT ?
|
2019-08-21 15:32:25 +01:00
|
|
|
`, expiredAt.UTC(), expiredAt.UTC(), limit)
|
2019-04-08 17:46:38 +01:00
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrPieceInfo.Wrap(err)
|
2019-04-08 17:46:38 +01:00
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
|
|
|
for rows.Next() {
|
2019-08-08 02:47:30 +01:00
|
|
|
info := pieces.ExpiredInfo{InPieceInfo: true}
|
|
|
|
err = rows.Scan(&info.SatelliteID, &info.PieceID)
|
2019-04-08 17:46:38 +01:00
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return infos, ErrPieceInfo.Wrap(err)
|
2019-04-08 17:46:38 +01:00
|
|
|
}
|
2019-05-08 12:11:59 +01:00
|
|
|
infos = append(infos, info)
|
2019-04-08 17:46:38 +01:00
|
|
|
}
|
2020-01-16 14:36:50 +00:00
|
|
|
return infos, rows.Err()
|
2019-04-08 17:46:38 +01:00
|
|
|
}
|
|
|
|
|
2019-08-08 02:47:30 +01:00
|
|
|
type v0StoredPieceAccess struct {
|
|
|
|
blobStore storage.Blobs
|
|
|
|
satellite storj.NodeID
|
|
|
|
pieceID storj.PieceID
|
|
|
|
pieceSize int64
|
|
|
|
creationTime time.Time
|
|
|
|
expirationTime *time.Time
|
|
|
|
blobInfo storage.BlobInfo
|
|
|
|
}
|
2019-04-08 17:46:38 +01:00
|
|
|
|
2019-08-08 02:47:30 +01:00
|
|
|
// PieceID returns the piece ID for the piece
|
|
|
|
func (v0Access v0StoredPieceAccess) PieceID() storj.PieceID {
|
|
|
|
return v0Access.pieceID
|
2019-07-09 01:33:50 +01:00
|
|
|
}
|
|
|
|
|
2019-08-08 02:47:30 +01:00
|
|
|
// Satellite returns the satellite ID that owns the piece
|
|
|
|
func (v0Access v0StoredPieceAccess) Satellite() (storj.NodeID, error) {
|
|
|
|
return v0Access.satellite, nil
|
2019-07-09 01:33:50 +01:00
|
|
|
}
|
|
|
|
|
2019-08-08 02:47:30 +01:00
|
|
|
// BlobRef returns the relevant storage.BlobRef locator for the piece
|
|
|
|
func (v0Access v0StoredPieceAccess) BlobRef() storage.BlobRef {
|
|
|
|
return storage.BlobRef{
|
|
|
|
Namespace: v0Access.satellite.Bytes(),
|
|
|
|
Key: v0Access.pieceID.Bytes(),
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 17:46:38 +01:00
|
|
|
|
2019-08-08 02:47:30 +01:00
|
|
|
func (v0Access v0StoredPieceAccess) fillInBlobAccess(ctx context.Context) error {
|
|
|
|
if v0Access.blobInfo == nil {
|
|
|
|
if v0Access.blobStore == nil {
|
|
|
|
return errs.New("this v0StoredPieceAccess instance has no blobStore reference, and cannot look up the relevant blob")
|
|
|
|
}
|
|
|
|
blobInfo, err := v0Access.blobStore.StatWithStorageFormat(ctx, v0Access.BlobRef(), v0Access.StorageFormatVersion())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v0Access.blobInfo = blobInfo
|
2019-05-08 12:11:59 +01:00
|
|
|
}
|
2019-08-08 02:47:30 +01:00
|
|
|
return nil
|
2019-04-08 17:46:38 +01:00
|
|
|
}
|
2019-06-20 12:52:32 +01:00
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
// Size gives the size of the piece, and the piece content size (not including the piece header, if applicable)
|
|
|
|
func (v0Access v0StoredPieceAccess) Size(ctx context.Context) (int64, int64, error) {
|
|
|
|
return v0Access.pieceSize, v0Access.pieceSize, nil
|
2019-08-08 02:47:30 +01:00
|
|
|
}
|
2019-06-20 12:52:32 +01:00
|
|
|
|
2019-08-08 02:47:30 +01:00
|
|
|
// CreationTime returns the piece creation time as given in the original order (which is not
|
|
|
|
// necessarily the same as the file mtime).
|
|
|
|
func (v0Access v0StoredPieceAccess) CreationTime(ctx context.Context) (time.Time, error) {
|
|
|
|
return v0Access.creationTime, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime returns the same thing as CreationTime for V0 blobs. The intent is for ModTime to
|
|
|
|
// be a little faster when CreationTime is too slow and the precision is not needed, but in
|
|
|
|
// this case we already have the exact creation time from the database.
|
|
|
|
func (v0Access v0StoredPieceAccess) ModTime(ctx context.Context) (time.Time, error) {
|
|
|
|
return v0Access.creationTime, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FullPath gives the full path to the on-disk blob file
|
|
|
|
func (v0Access v0StoredPieceAccess) FullPath(ctx context.Context) (string, error) {
|
|
|
|
if err := v0Access.fillInBlobAccess(ctx); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return v0Access.blobInfo.FullPath(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageFormatVersion indicates the storage format version used to store the piece
|
|
|
|
func (v0Access v0StoredPieceAccess) StorageFormatVersion() storage.FormatVersion {
|
|
|
|
return filestore.FormatV0
|
|
|
|
}
|
2019-06-20 12:52:32 +01:00
|
|
|
|
2019-08-08 02:47:30 +01:00
|
|
|
// Stat does a stat on the on-disk blob file
|
|
|
|
func (v0Access v0StoredPieceAccess) Stat(ctx context.Context) (os.FileInfo, error) {
|
|
|
|
if err := v0Access.fillInBlobAccess(ctx); err != nil {
|
|
|
|
return nil, err
|
2019-06-20 12:52:32 +01:00
|
|
|
}
|
2019-08-08 02:47:30 +01:00
|
|
|
return v0Access.blobInfo.Stat(ctx)
|
2019-06-20 12:52:32 +01:00
|
|
|
}
|