2019-11-19 16:30:28 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package testblobs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-06 09:56:46 +00:00
|
|
|
"sync"
|
2019-11-26 16:25:21 +00:00
|
|
|
"time"
|
2019-11-19 16:30:28 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-07-10 20:36:39 +01:00
|
|
|
"storj.io/common/storj"
|
2019-11-19 16:30:28 +00:00
|
|
|
"storj.io/storj/storagenode"
|
2023-04-05 18:03:06 +01:00
|
|
|
"storj.io/storj/storagenode/blobstore"
|
2019-11-19 16:30:28 +00:00
|
|
|
)
|
|
|
|
|
2023-04-05 18:03:06 +01:00
|
|
|
// ErrorBlobs is the interface of blobstore.Blobs with the SetError method added.
|
2023-02-03 21:03:27 +00:00
|
|
|
// This allows the BadDB{}.Blobs member to be replaced with something that has
|
|
|
|
// specific behavior changes.
|
|
|
|
type ErrorBlobs interface {
|
2023-04-05 18:03:06 +01:00
|
|
|
blobstore.Blobs
|
2023-02-03 21:03:27 +00:00
|
|
|
SetError(err error)
|
2023-04-11 14:09:32 +01:00
|
|
|
SetCheckError(err error)
|
2023-02-03 21:03:27 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 16:30:28 +00:00
|
|
|
// BadDB implements bad storage node DB.
|
|
|
|
type BadDB struct {
|
|
|
|
storagenode.DB
|
2023-02-03 21:03:27 +00:00
|
|
|
Blobs ErrorBlobs
|
2019-11-19 16:30:28 +00:00
|
|
|
log *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBadDB creates a new bad storage node DB.
|
|
|
|
// Use SetError to manually configure the error returned by all piece operations.
|
|
|
|
func NewBadDB(log *zap.Logger, db storagenode.DB) *BadDB {
|
|
|
|
return &BadDB{
|
|
|
|
DB: db,
|
2023-02-03 21:03:27 +00:00
|
|
|
Blobs: newBadBlobs(log, db.Pieces()),
|
2019-11-19 16:30:28 +00:00
|
|
|
log: log,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pieces returns the blob store.
|
2023-04-05 18:03:06 +01:00
|
|
|
func (bad *BadDB) Pieces() blobstore.Blobs {
|
2023-02-03 21:03:27 +00:00
|
|
|
return bad.Blobs
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 14:09:32 +01:00
|
|
|
// SetError sets an error to be returned for piece operations.
|
2019-11-19 16:30:28 +00:00
|
|
|
func (bad *BadDB) SetError(err error) {
|
2023-02-03 21:03:27 +00:00
|
|
|
bad.Blobs.SetError(err)
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 14:09:32 +01:00
|
|
|
// SetCheckError sets an error to be returned for check and verification operations.
|
|
|
|
func (bad *BadDB) SetCheckError(err error) {
|
|
|
|
bad.Blobs.SetCheckError(err)
|
|
|
|
}
|
|
|
|
|
2019-11-19 16:30:28 +00:00
|
|
|
// BadBlobs implements a bad blob store.
|
|
|
|
type BadBlobs struct {
|
2023-04-11 14:09:32 +01:00
|
|
|
err lockedErr
|
|
|
|
checkErr lockedErr
|
|
|
|
blobs blobstore.Blobs
|
|
|
|
log *zap.Logger
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 09:56:46 +00:00
|
|
|
type lockedErr struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Err returns the error.
|
|
|
|
func (m *lockedErr) Err() error {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
return m.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets the error.
|
|
|
|
func (m *lockedErr) Set(err error) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
m.err = err
|
|
|
|
}
|
|
|
|
|
2019-11-19 16:30:28 +00:00
|
|
|
// newBadBlobs creates a new bad blob store wrapping the provided blobs.
|
|
|
|
// Use SetError to manually configure the error returned by all operations.
|
2023-04-05 18:03:06 +01:00
|
|
|
func newBadBlobs(log *zap.Logger, blobs blobstore.Blobs) *BadBlobs {
|
2019-11-19 16:30:28 +00:00
|
|
|
return &BadBlobs{
|
|
|
|
log: log,
|
|
|
|
blobs: blobs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 14:09:32 +01:00
|
|
|
// SetError configures the blob store to return a specific error for all operations, except verification.
|
2020-11-06 09:56:46 +00:00
|
|
|
func (bad *BadBlobs) SetError(err error) {
|
|
|
|
bad.err.Set(err)
|
|
|
|
}
|
|
|
|
|
2023-04-11 14:09:32 +01:00
|
|
|
// SetCheckError configures the blob store to return a specific error for verification operations.
|
|
|
|
func (bad *BadBlobs) SetCheckError(err error) {
|
|
|
|
bad.checkErr.Set(err)
|
|
|
|
}
|
|
|
|
|
2019-11-19 16:30:28 +00:00
|
|
|
// Create creates a new blob that can be written optionally takes a size
|
|
|
|
// argument for performance improvements, -1 is unknown size.
|
2023-04-05 18:03:06 +01:00
|
|
|
func (bad *BadBlobs) Create(ctx context.Context, ref blobstore.BlobRef, size int64) (blobstore.BlobWriter, error) {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return nil, err
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
return bad.blobs.Create(ctx, ref, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the blob store and any resources associated with it.
|
|
|
|
func (bad *BadBlobs) Close() error {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return err
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
return bad.blobs.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens a reader with the specified namespace and key.
|
2023-04-05 18:03:06 +01:00
|
|
|
func (bad *BadBlobs) Open(ctx context.Context, ref blobstore.BlobRef) (blobstore.BlobReader, error) {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return nil, err
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
return bad.blobs.Open(ctx, ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenWithStorageFormat opens a reader for the already-located blob, avoiding the potential need
|
|
|
|
// to check multiple storage formats to find the blob.
|
2023-04-05 18:03:06 +01:00
|
|
|
func (bad *BadBlobs) OpenWithStorageFormat(ctx context.Context, ref blobstore.BlobRef, formatVer blobstore.FormatVersion) (blobstore.BlobReader, error) {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return nil, err
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
return bad.blobs.OpenWithStorageFormat(ctx, ref, formatVer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trash deletes the blob with the namespace and key.
|
2023-04-05 18:03:06 +01:00
|
|
|
func (bad *BadBlobs) Trash(ctx context.Context, ref blobstore.BlobRef) error {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return err
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
return bad.blobs.Trash(ctx, ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreTrash restores all files in the trash.
|
2019-12-21 13:11:24 +00:00
|
|
|
func (bad *BadBlobs) RestoreTrash(ctx context.Context, namespace []byte) ([][]byte, error) {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return nil, err
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
return bad.blobs.RestoreTrash(ctx, namespace)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// EmptyTrash empties the trash.
|
2019-12-21 13:11:24 +00:00
|
|
|
func (bad *BadBlobs) EmptyTrash(ctx context.Context, namespace []byte, trashedBefore time.Time) (int64, [][]byte, error) {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return 0, nil, err
|
2019-11-26 16:25:21 +00:00
|
|
|
}
|
|
|
|
return bad.blobs.EmptyTrash(ctx, namespace, trashedBefore)
|
|
|
|
}
|
|
|
|
|
2019-11-19 16:30:28 +00:00
|
|
|
// Delete deletes the blob with the namespace and key.
|
2023-04-05 18:03:06 +01:00
|
|
|
func (bad *BadBlobs) Delete(ctx context.Context, ref blobstore.BlobRef) error {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return err
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
return bad.blobs.Delete(ctx, ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteWithStorageFormat deletes the blob with the namespace, key, and format version.
|
2023-04-05 18:03:06 +01:00
|
|
|
func (bad *BadBlobs) DeleteWithStorageFormat(ctx context.Context, ref blobstore.BlobRef, formatVer blobstore.FormatVersion) error {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return err
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
return bad.blobs.DeleteWithStorageFormat(ctx, ref, formatVer)
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:50:40 +01:00
|
|
|
// DeleteNamespace deletes blobs of specific satellite, used after successful GE only.
|
|
|
|
func (bad *BadBlobs) DeleteNamespace(ctx context.Context, ref []byte) (err error) {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return err
|
2020-07-08 11:50:40 +01:00
|
|
|
}
|
|
|
|
return bad.blobs.DeleteNamespace(ctx, ref)
|
|
|
|
}
|
|
|
|
|
2019-11-19 16:30:28 +00:00
|
|
|
// Stat looks up disk metadata on the blob file.
|
2023-04-05 18:03:06 +01:00
|
|
|
func (bad *BadBlobs) Stat(ctx context.Context, ref blobstore.BlobRef) (blobstore.BlobInfo, error) {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return nil, err
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
return bad.blobs.Stat(ctx, ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatWithStorageFormat looks up disk metadata for the blob file with the given storage format
|
|
|
|
// version. This avoids the potential need to check multiple storage formats for the blob
|
|
|
|
// when the format is already known.
|
2023-04-05 18:03:06 +01:00
|
|
|
func (bad *BadBlobs) StatWithStorageFormat(ctx context.Context, ref blobstore.BlobRef, formatVer blobstore.FormatVersion) (blobstore.BlobInfo, error) {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return nil, err
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
return bad.blobs.StatWithStorageFormat(ctx, ref, formatVer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WalkNamespace executes walkFunc for each locally stored blob in the given namespace.
|
|
|
|
// If walkFunc returns a non-nil error, WalkNamespace will stop iterating and return the
|
|
|
|
// error immediately.
|
2023-04-05 18:03:06 +01:00
|
|
|
func (bad *BadBlobs) WalkNamespace(ctx context.Context, namespace []byte, walkFunc func(blobstore.BlobInfo) error) error {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return err
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
return bad.blobs.WalkNamespace(ctx, namespace, walkFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListNamespaces returns all namespaces that might be storing data.
|
|
|
|
func (bad *BadBlobs) ListNamespaces(ctx context.Context) ([][]byte, error) {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return make([][]byte, 0), err
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
return bad.blobs.ListNamespaces(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FreeSpace return how much free space left for writing.
|
2021-09-10 14:05:29 +01:00
|
|
|
func (bad *BadBlobs) FreeSpace(ctx context.Context) (int64, error) {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return 0, err
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
2021-09-10 14:05:29 +01:00
|
|
|
return bad.blobs.FreeSpace(ctx)
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
// SpaceUsedForBlobs adds up how much is used in all namespaces.
|
|
|
|
func (bad *BadBlobs) SpaceUsedForBlobs(ctx context.Context) (int64, error) {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return 0, err
|
2019-12-21 13:11:24 +00:00
|
|
|
}
|
|
|
|
return bad.blobs.SpaceUsedForBlobs(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SpaceUsedForBlobsInNamespace adds up how much is used in the given namespace.
|
|
|
|
func (bad *BadBlobs) SpaceUsedForBlobsInNamespace(ctx context.Context, namespace []byte) (int64, error) {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return 0, err
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
2019-12-21 13:11:24 +00:00
|
|
|
return bad.blobs.SpaceUsedForBlobsInNamespace(ctx, namespace)
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 13:11:24 +00:00
|
|
|
// SpaceUsedForTrash adds up how much is used in all namespaces.
|
|
|
|
func (bad *BadBlobs) SpaceUsedForTrash(ctx context.Context) (int64, error) {
|
2020-11-06 09:56:46 +00:00
|
|
|
if err := bad.err.Err(); err != nil {
|
|
|
|
return 0, err
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
2019-12-21 13:11:24 +00:00
|
|
|
return bad.blobs.SpaceUsedForTrash(ctx)
|
2019-11-19 16:30:28 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 14:09:32 +01:00
|
|
|
// CheckWritability tests writability of the storage directory by creating and deleting a file.
|
|
|
|
func (bad *BadBlobs) CheckWritability(ctx context.Context) error {
|
|
|
|
if err := bad.checkErr.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bad.blobs.CheckWritability(ctx)
|
|
|
|
}
|
|
|
|
|
2020-07-10 20:36:39 +01:00
|
|
|
// CreateVerificationFile creates a file to be used for storage directory verification.
|
2021-09-10 14:05:29 +01:00
|
|
|
func (bad *BadBlobs) CreateVerificationFile(ctx context.Context, id storj.NodeID) error {
|
2023-04-11 14:09:32 +01:00
|
|
|
if err := bad.checkErr.Err(); err != nil {
|
2020-11-06 09:56:46 +00:00
|
|
|
return err
|
2020-07-10 20:36:39 +01:00
|
|
|
}
|
2021-09-10 14:05:29 +01:00
|
|
|
return bad.blobs.CreateVerificationFile(ctx, id)
|
2020-07-10 20:36:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyStorageDir verifies that the storage directory is correct by checking for the existence and validity
|
|
|
|
// of the verification file.
|
2021-09-10 14:05:29 +01:00
|
|
|
func (bad *BadBlobs) VerifyStorageDir(ctx context.Context, id storj.NodeID) error {
|
2023-04-11 14:09:32 +01:00
|
|
|
if err := bad.checkErr.Err(); err != nil {
|
2020-11-06 09:56:46 +00:00
|
|
|
return err
|
2020-07-10 20:36:39 +01:00
|
|
|
}
|
2021-09-10 14:05:29 +01:00
|
|
|
return bad.blobs.VerifyStorageDir(ctx, id)
|
2020-07-10 20:36:39 +01:00
|
|
|
}
|