2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenodedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2019-04-08 17:46:38 +01:00
|
|
|
"time"
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-04-08 17:46:38 +01:00
|
|
|
"github.com/zeebo/errs"
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
"storj.io/storj/storagenode/pieces"
|
|
|
|
)
|
|
|
|
|
2019-04-02 08:54:09 +01:00
|
|
|
type pieceinfo struct{ *InfoDB }
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
// PieceInfo returns database for storing piece information
|
|
|
|
func (db *DB) PieceInfo() pieces.DB { return db.info.PieceInfo() }
|
|
|
|
|
|
|
|
// PieceInfo returns database for storing piece information
|
2019-04-02 08:54:09 +01:00
|
|
|
func (db *InfoDB) PieceInfo() pieces.DB { return &pieceinfo{db} }
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
// Add inserts piece information into the database.
|
|
|
|
func (db *pieceinfo) Add(ctx context.Context, info *pieces.Info) error {
|
|
|
|
certdb := db.CertDB()
|
|
|
|
certid, err := certdb.Include(ctx, info.Uplink)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
uplinkPieceHash, err := proto.Marshal(info.UplinkPieceHash)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer db.locked()()
|
|
|
|
|
2019-05-08 12:11:59 +01:00
|
|
|
_, err = db.db.ExecContext(ctx, db.Rebind(`
|
2019-03-18 10:55:06 +00:00
|
|
|
INSERT INTO
|
|
|
|
pieceinfo(satellite_id, piece_id, piece_size, piece_expiration, uplink_piece_hash, uplink_cert_id)
|
|
|
|
VALUES (?,?,?,?,?,?)
|
2019-05-08 12:11:59 +01:00
|
|
|
`), info.SatelliteID, info.PieceID, info.PieceSize, info.PieceExpiration, uplinkPieceHash, certid)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
return ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get gets piece information by satellite id and piece id.
|
|
|
|
func (db *pieceinfo) Get(ctx context.Context, satelliteID storj.NodeID, pieceID storj.PieceID) (*pieces.Info, error) {
|
|
|
|
info := &pieces.Info{}
|
|
|
|
info.SatelliteID = satelliteID
|
|
|
|
info.PieceID = pieceID
|
|
|
|
|
|
|
|
var uplinkPieceHash []byte
|
|
|
|
var uplinkIdentity []byte
|
|
|
|
|
|
|
|
db.mu.Lock()
|
2019-05-08 12:11:59 +01:00
|
|
|
err := db.db.QueryRowContext(ctx, db.Rebind(`
|
2019-03-18 10:55:06 +00:00
|
|
|
SELECT piece_size, piece_expiration, uplink_piece_hash, certificate.peer_identity
|
|
|
|
FROM pieceinfo
|
|
|
|
INNER JOIN certificate ON pieceinfo.uplink_cert_id = certificate.cert_id
|
|
|
|
WHERE satellite_id = ? AND piece_id = ?
|
2019-05-08 12:11:59 +01:00
|
|
|
`), satelliteID, pieceID).Scan(&info.PieceSize, &info.PieceExpiration, &uplinkPieceHash, &uplinkIdentity)
|
2019-03-18 10:55:06 +00:00
|
|
|
db.mu.Unlock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info.UplinkPieceHash = &pb.PieceHash{}
|
|
|
|
err = proto.Unmarshal(uplinkPieceHash, info.UplinkPieceHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info.Uplink, err = decodePeerIdentity(uplinkIdentity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes piece information.
|
|
|
|
func (db *pieceinfo) Delete(ctx context.Context, satelliteID storj.NodeID, pieceID storj.PieceID) error {
|
|
|
|
defer db.locked()()
|
|
|
|
|
2019-05-08 12:11:59 +01:00
|
|
|
_, err := db.db.ExecContext(ctx, db.Rebind(`
|
|
|
|
DELETE FROM pieceinfo
|
|
|
|
WHERE satellite_id = ?
|
|
|
|
AND piece_id = ?
|
|
|
|
`), satelliteID, pieceID)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
return ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-05-08 12:11:59 +01:00
|
|
|
// DeleteFailed marks piece as a failed deletion.
|
|
|
|
func (db *pieceinfo) DeleteFailed(ctx context.Context, satelliteID storj.NodeID, pieceID storj.PieceID, now time.Time) error {
|
2019-03-18 10:55:06 +00:00
|
|
|
defer db.locked()()
|
|
|
|
|
2019-05-08 12:11:59 +01:00
|
|
|
_, err := db.db.ExecContext(ctx, db.Rebind(`
|
|
|
|
UPDATE pieceinfo
|
|
|
|
SET deletion_failed_at = ?
|
|
|
|
WHERE satellite_id = ?
|
|
|
|
AND piece_id = ?
|
|
|
|
`), now, satelliteID, pieceID)
|
|
|
|
|
|
|
|
return ErrInfo.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
2019-04-08 17:46:38 +01:00
|
|
|
|
2019-05-08 12:11:59 +01:00
|
|
|
// GetExpired gets pieceinformation identites that are expired.
|
|
|
|
func (db *pieceinfo) GetExpired(ctx context.Context, expiredAt time.Time, limit int64) (infos []pieces.ExpiredInfo, err error) {
|
|
|
|
defer db.locked()()
|
2019-04-08 17:46:38 +01:00
|
|
|
|
2019-05-08 12:11:59 +01:00
|
|
|
rows, err := db.db.QueryContext(ctx, db.Rebind(`
|
|
|
|
SELECT satellite_id, piece_id, piece_size
|
|
|
|
FROM pieceinfo
|
|
|
|
WHERE piece_expiration < ? AND ((deletion_failed_at IS NULL) OR deletion_failed_at <> ?)
|
|
|
|
ORDER BY satellite_id
|
|
|
|
LIMIT ?
|
|
|
|
`), expiredAt, expiredAt, limit)
|
2019-04-08 17:46:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, ErrInfo.Wrap(err)
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
|
|
|
for rows.Next() {
|
2019-05-08 12:11:59 +01:00
|
|
|
info := pieces.ExpiredInfo{}
|
|
|
|
err = rows.Scan(&info.SatelliteID, &info.PieceID, &info.PieceSize)
|
2019-04-08 17:46:38 +01:00
|
|
|
if err != nil {
|
2019-05-08 12:11:59 +01:00
|
|
|
return infos, ErrInfo.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
|
|
|
}
|
2019-05-08 12:11:59 +01:00
|
|
|
return infos, nil
|
2019-04-08 17:46:38 +01:00
|
|
|
}
|
|
|
|
|
2019-05-08 12:11:59 +01:00
|
|
|
// SpaceUsed calculates disk space used by all pieces
|
|
|
|
func (db *pieceinfo) SpaceUsed(ctx context.Context) (int64, error) {
|
2019-04-08 17:46:38 +01:00
|
|
|
defer db.locked()()
|
|
|
|
|
2019-05-08 12:11:59 +01:00
|
|
|
var sum *int64
|
|
|
|
err := db.db.QueryRowContext(ctx, db.Rebind(`
|
|
|
|
SELECT SUM(piece_size)
|
|
|
|
FROM pieceinfo
|
|
|
|
`)).Scan(&sum)
|
2019-04-08 17:46:38 +01:00
|
|
|
|
2019-05-08 12:11:59 +01:00
|
|
|
if err == sql.ErrNoRows || sum == nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return *sum, err
|
2019-04-08 17:46:38 +01:00
|
|
|
}
|